cache.c revision 78c7d0efae91cde1a992f69c1a0157b39e971670
1/* 2 * cache.c - allocation/initialization/free routines for cache 3 * 4 * Copyright (C) 2001 Andreas Dilger 5 * Copyright (C) 2003 Theodore Ts'o 6 * 7 * %Begin-Header% 8 * This file may be redistributed under the terms of the 9 * GNU Lesser General Public License. 10 * %End-Header% 11 */ 12 13#if HAVE_UNISTD_H 14#include <unistd.h> 15#endif 16#ifdef HAVE_ERRNO_H 17#include <errno.h> 18#endif 19#include <stdlib.h> 20#include <string.h> 21#ifdef HAVE_SYS_PRCTL_H 22#include <sys/prctl.h> 23#else 24#define PR_GET_DUMPABLE 3 25#endif 26#if (!defined(HAVE_PRCTL) && defined(linux)) 27#include <sys/syscall.h> 28#endif 29#include "blkidP.h" 30 31int blkid_debug_mask = 0; 32 33 34static char *safe_getenv(const char *arg) 35{ 36 if ((getuid() != geteuid()) || (getgid() != getegid())) 37 return NULL; 38#if HAVE_PRCTL 39 if (prctl(PR_GET_DUMPABLE) == 0) 40 return NULL; 41#else 42#if (defined(linux) && defined(SYS_prctl)) 43 if (syscall(SYS_prctl, PR_GET_DUMPABLE) == 0) 44 return NULL; 45#endif 46#endif 47 48#ifdef HAVE___SECURE_GETENV 49 return __secure_getenv("BLKID_FILE"); 50#else 51 return getenv("BLKID_FILE"); 52#endif 53} 54 55#if 0 /* ifdef CONFIG_BLKID_DEBUG */ 56static blkid_debug_dump_cache(int mask, blkid_cache cache) 57{ 58 struct list_head *p; 59 60 if (!cache) { 61 printf("cache: NULL\n"); 62 return; 63 } 64 65 printf("cache: time = %lu\n", cache->bic_time); 66 printf("cache: flags = 0x%08X\n", cache->bic_flags); 67 68 list_for_each(p, &cache->bic_devs) { 69 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs); 70 blkid_debug_dump_dev(dev); 71 } 72} 73#endif 74 75int blkid_get_cache(blkid_cache *ret_cache, const char *filename) 76{ 77 blkid_cache cache; 78 79#ifdef CONFIG_BLKID_DEBUG 80 if (!(blkid_debug_mask & DEBUG_INIT)) { 81 char *dstr = getenv("BLKID_DEBUG"); 82 83 if (dstr) 84 blkid_debug_mask = strtoul(dstr, 0, 0); 85 blkid_debug_mask |= DEBUG_INIT; 86 } 87#endif 88 89 DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n", 90 filename ? filename : "default cache")); 91 92 if (!(cache = (blkid_cache) calloc(1, sizeof(struct blkid_struct_cache)))) 93 return -BLKID_ERR_MEM; 94 95 INIT_LIST_HEAD(&cache->bic_devs); 96 INIT_LIST_HEAD(&cache->bic_tags); 97 98 if (filename && !strlen(filename)) 99 filename = 0; 100 if (!filename) 101 filename = safe_getenv("BLKID_FILE"); 102 if (!filename) 103 filename = BLKID_CACHE_FILE; 104 cache->bic_filename = blkid_strdup(filename); 105 106 blkid_read_cache(cache); 107 108 *ret_cache = cache; 109 return 0; 110} 111 112void blkid_put_cache(blkid_cache cache) 113{ 114 if (!cache) 115 return; 116 117 (void) blkid_flush_cache(cache); 118 119 DBG(DEBUG_CACHE, printf("freeing cache struct\n")); 120 121 /* DBG(DEBUG_CACHE, blkid_debug_dump_cache(cache)); */ 122 123 while (!list_empty(&cache->bic_devs)) { 124 blkid_dev dev = list_entry(cache->bic_devs.next, 125 struct blkid_struct_dev, 126 bid_devs); 127 blkid_free_dev(dev); 128 } 129 130 while (!list_empty(&cache->bic_tags)) { 131 blkid_tag tag = list_entry(cache->bic_tags.next, 132 struct blkid_struct_tag, 133 bit_tags); 134 135 while (!list_empty(&tag->bit_names)) { 136 blkid_tag bad = list_entry(tag->bit_names.next, 137 struct blkid_struct_tag, 138 bit_names); 139 140 DBG(DEBUG_CACHE, printf("warning: unfreed tag %s=%s\n", 141 bad->bit_name, bad->bit_val)); 142 blkid_free_tag(bad); 143 } 144 blkid_free_tag(tag); 145 } 146 if (cache->bic_filename) 147 free(cache->bic_filename); 148 149 free(cache); 150} 151 152#ifdef TEST_PROGRAM 153int main(int argc, char** argv) 154{ 155 blkid_cache cache = NULL; 156 int ret; 157 158 blkid_debug_mask = DEBUG_ALL; 159 if ((argc > 2)) { 160 fprintf(stderr, "Usage: %s [filename] \n", argv[0]); 161 exit(1); 162 } 163 164 if ((ret = blkid_get_cache(&cache, argv[1])) < 0) { 165 fprintf(stderr, "error %d parsing cache file %s\n", ret, 166 argv[1] ? argv[1] : BLKID_CACHE_FILE); 167 exit(1); 168 } 169 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) { 170 fprintf(stderr, "%s: error creating cache (%d)\n", 171 argv[0], ret); 172 exit(1); 173 } 174 if ((ret = blkid_probe_all(cache) < 0)) 175 fprintf(stderr, "error probing devices\n"); 176 177 blkid_put_cache(cache); 178 179 return ret; 180} 181#endif 182