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.
21 #ifdef HAVE_SYS_PRCTL_H
22 #include <sys/prctl.h>
24 #define PR_GET_DUMPABLE 3
26 #if (!defined(HAVE_PRCTL) && defined(linux))
27 #include <sys/syscall.h>
29 #ifdef HAVE_SYS_STAT_H
34 int blkid_debug_mask = 0;
37 char *blkid_safe_getenv(const char *arg)
39 if ((getuid() != geteuid()) || (getgid() != getegid()))
42 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
45 #if (defined(linux) && defined(SYS_prctl))
46 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
51 #ifdef HAVE___SECURE_GETENV
52 return __secure_getenv(arg);
58 #if 0 /* ifdef CONFIG_BLKID_DEBUG */
59 static blkid_debug_dump_cache(int mask, blkid_cache cache)
64 printf("cache: NULL\n");
68 printf("cache: time = %lu\n", cache->bic_time);
69 printf("cache: flags = 0x%08X\n", cache->bic_flags);
71 list_for_each(p, &cache->bic_devs) {
72 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
73 blkid_debug_dump_dev(dev);
78 #ifdef CONFIG_BLKID_DEBUG
79 void blkid_init_debug(int mask)
81 if (blkid_debug_mask & DEBUG_INIT)
86 char *dstr = getenv("BLKID_DEBUG");
89 blkid_debug_mask = strtoul(dstr, 0, 0);
91 blkid_debug_mask = mask;
94 printf("libblkid: debug mask set to 0x%04x.\n", blkid_debug_mask);
96 blkid_debug_mask |= DEBUG_INIT;
100 /* returns allocated path to cache */
101 char *blkid_get_cache_filename(struct blkid_config *conf)
105 filename = blkid_safe_getenv("BLKID_FILE");
107 filename = blkid_strdup(filename);
109 filename = blkid_strdup(conf->cachefile);
111 struct blkid_config *c = blkid_read_config(NULL);
113 filename = blkid_strdup(BLKID_CACHE_FILE);
115 filename = c->cachefile; /* already allocated */
117 blkid_free_config(c);
123 int blkid_get_cache(blkid_cache *ret_cache, const char *filename)
129 DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n",
130 filename ? filename : "default cache"));
132 if (!(cache = (blkid_cache) calloc(1, sizeof(struct blkid_struct_cache))))
133 return -BLKID_ERR_MEM;
135 INIT_LIST_HEAD(&cache->bic_devs);
136 INIT_LIST_HEAD(&cache->bic_tags);
138 if (filename && !*filename)
141 cache->bic_filename = blkid_strdup(filename);
143 cache->bic_filename = blkid_get_cache_filename(NULL);
145 blkid_read_cache(cache);
150 void blkid_put_cache(blkid_cache cache)
155 (void) blkid_flush_cache(cache);
157 DBG(DEBUG_CACHE, printf("freeing cache struct\n"));
159 /* DBG(DEBUG_CACHE, blkid_debug_dump_cache(cache)); */
161 while (!list_empty(&cache->bic_devs)) {
162 blkid_dev dev = list_entry(cache->bic_devs.next,
163 struct blkid_struct_dev,
168 while (!list_empty(&cache->bic_tags)) {
169 blkid_tag tag = list_entry(cache->bic_tags.next,
170 struct blkid_struct_tag,
173 while (!list_empty(&tag->bit_names)) {
174 blkid_tag bad = list_entry(tag->bit_names.next,
175 struct blkid_struct_tag,
178 DBG(DEBUG_CACHE, printf("warning: unfreed tag %s=%s\n",
179 bad->bit_name, bad->bit_val));
185 blkid_free_probe(cache->probe);
187 free(cache->bic_filename);
191 void blkid_gc_cache(blkid_cache cache)
193 struct list_head *p, *pnext;
199 list_for_each_safe(p, pnext, &cache->bic_devs) {
200 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
203 if (stat(dev->bid_name, &st) < 0) {
205 printf("freeing %s\n", dev->bid_name));
207 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
210 printf("Device %s exists\n", dev->bid_name));
216 int main(int argc, char** argv)
218 blkid_cache cache = NULL;
221 blkid_init_debug(DEBUG_ALL);
224 fprintf(stderr, "Usage: %s [filename] \n", argv[0]);
228 if ((ret = blkid_get_cache(&cache, argv[1])) < 0) {
229 fprintf(stderr, "error %d parsing cache file %s\n", ret,
230 argv[1] ? argv[1] : BLKID_CACHE_FILE);
233 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
234 fprintf(stderr, "%s: error creating cache (%d)\n",
238 if ((ret = blkid_probe_all(cache) < 0))
239 fprintf(stderr, "error probing devices\n");
241 blkid_put_cache(cache);