2 * cache.c - allocation/initialization/free routines for cache
4 * Copyright (C) 2001 Andreas Dilger
5 * Copyright (C) 2003 Theodore Ts'o
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
14 #if HAVE_SECURE_GETENV
25 #ifdef HAVE_SYS_PRCTL_H
26 #include <sys/prctl.h>
28 #define PR_GET_DUMPABLE 3
30 #if (!defined(HAVE_PRCTL) && defined(linux))
31 #include <sys/syscall.h>
33 #ifdef HAVE_SYS_STAT_H
38 int blkid_debug_mask = 0;
41 static char *safe_getenv(const char *arg)
43 if ((getuid() != geteuid()) || (getgid() != getegid()))
46 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
49 #if (defined(linux) && defined(SYS_prctl))
50 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
55 #if defined(HAVE_SECURE_GETENV)
56 return secure_getenv(arg);
57 #elif defined(HAVE___SECURE_GETENV)
58 return __secure_getenv(arg);
64 #if 0 /* ifdef CONFIG_BLKID_DEBUG */
65 static blkid_debug_dump_cache(int mask, blkid_cache cache)
70 printf("cache: NULL\n");
74 printf("cache: time = %lu\n", cache->bic_time);
75 printf("cache: flags = 0x%08X\n", cache->bic_flags);
77 list_for_each(p, &cache->bic_devs) {
78 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
79 blkid_debug_dump_dev(dev);
84 int blkid_get_cache(blkid_cache *ret_cache, const char *filename)
88 #ifdef CONFIG_BLKID_DEBUG
89 if (!(blkid_debug_mask & DEBUG_INIT)) {
90 char *dstr = getenv("BLKID_DEBUG");
93 blkid_debug_mask = strtoul(dstr, 0, 0);
94 blkid_debug_mask |= DEBUG_INIT;
98 DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n",
99 filename ? filename : "default cache"));
101 if (!(cache = (blkid_cache) calloc(1, sizeof(struct blkid_struct_cache))))
102 return -BLKID_ERR_MEM;
104 INIT_LIST_HEAD(&cache->bic_devs);
105 INIT_LIST_HEAD(&cache->bic_tags);
107 if (filename && !strlen(filename))
110 filename = safe_getenv("BLKID_FILE");
112 filename = BLKID_CACHE_FILE;
113 cache->bic_filename = blkid_strdup(filename);
115 blkid_read_cache(cache);
121 void blkid_put_cache(blkid_cache cache)
126 (void) blkid_flush_cache(cache);
128 DBG(DEBUG_CACHE, printf("freeing cache struct\n"));
130 /* DBG(DEBUG_CACHE, blkid_debug_dump_cache(cache)); */
132 while (!list_empty(&cache->bic_devs)) {
133 blkid_dev dev = list_entry(cache->bic_devs.next,
134 struct blkid_struct_dev,
139 while (!list_empty(&cache->bic_tags)) {
140 blkid_tag tag = list_entry(cache->bic_tags.next,
141 struct blkid_struct_tag,
144 while (!list_empty(&tag->bit_names)) {
145 blkid_tag bad = list_entry(tag->bit_names.next,
146 struct blkid_struct_tag,
149 DBG(DEBUG_CACHE, printf("warning: unfreed tag %s=%s\n",
150 bad->bit_name, bad->bit_val));
155 free(cache->bic_filename);
160 void blkid_gc_cache(blkid_cache cache)
162 struct list_head *p, *pnext;
168 list_for_each_safe(p, pnext, &cache->bic_devs) {
169 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
172 if (stat(dev->bid_name, &st) < 0) {
174 printf("freeing %s\n", dev->bid_name));
176 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
179 printf("Device %s exists\n", dev->bid_name));
186 int main(int argc, char** argv)
188 blkid_cache cache = NULL;
191 blkid_debug_mask = DEBUG_ALL;
193 fprintf(stderr, "Usage: %s [filename] \n", argv[0]);
197 if ((ret = blkid_get_cache(&cache, argv[1])) < 0) {
198 fprintf(stderr, "error %d parsing cache file %s\n", ret,
199 argv[1] ? argv[1] : BLKID_CACHE_FILE);
202 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
203 fprintf(stderr, "%s: error creating cache (%d)\n",
207 if ((ret = blkid_probe_all(cache) < 0))
208 fprintf(stderr, "error probing devices\n");
210 blkid_put_cache(cache);