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.
22 #ifdef HAVE_SYS_PRCTL_H
23 #include <sys/prctl.h>
25 #define PR_GET_DUMPABLE 3
27 #if (!defined(HAVE_PRCTL) && defined(linux))
28 #include <sys/syscall.h>
30 #ifdef HAVE_SYS_STAT_H
35 int blkid_debug_mask = 0;
38 static char *safe_getenv(const char *arg)
40 if ((getuid() != geteuid()) || (getgid() != getegid()))
43 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
46 #if (defined(linux) && defined(SYS_prctl))
47 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
52 #ifdef HAVE___SECURE_GETENV
53 return __secure_getenv(arg);
59 #if 0 /* ifdef CONFIG_BLKID_DEBUG */
60 static blkid_debug_dump_cache(int mask, blkid_cache cache)
65 printf("cache: NULL\n");
69 printf("cache: time = %lu\n", cache->bic_time);
70 printf("cache: flags = 0x%08X\n", cache->bic_flags);
72 list_for_each(p, &cache->bic_devs) {
73 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
74 blkid_debug_dump_dev(dev);
79 int blkid_get_cache(blkid_cache *ret_cache, const char *filename)
83 #ifdef CONFIG_BLKID_DEBUG
84 if (!(blkid_debug_mask & DEBUG_INIT)) {
85 char *dstr = getenv("BLKID_DEBUG");
88 blkid_debug_mask = strtoul(dstr, 0, 0);
89 blkid_debug_mask |= DEBUG_INIT;
93 DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n",
94 filename ? filename : "default cache"));
96 if (!(cache = (blkid_cache) calloc(1, sizeof(struct blkid_struct_cache))))
97 return -BLKID_ERR_MEM;
99 INIT_LIST_HEAD(&cache->bic_devs);
100 INIT_LIST_HEAD(&cache->bic_tags);
102 if (filename && !strlen(filename))
105 filename = safe_getenv("BLKID_FILE");
107 filename = BLKID_CACHE_FILE;
108 cache->bic_filename = blkid_strdup(filename);
110 blkid_read_cache(cache);
116 void blkid_put_cache(blkid_cache cache)
121 (void) blkid_flush_cache(cache);
123 DBG(DEBUG_CACHE, printf("freeing cache struct\n"));
125 /* DBG(DEBUG_CACHE, blkid_debug_dump_cache(cache)); */
127 while (!list_empty(&cache->bic_devs)) {
128 blkid_dev dev = list_entry(cache->bic_devs.next,
129 struct blkid_struct_dev,
134 while (!list_empty(&cache->bic_tags)) {
135 blkid_tag tag = list_entry(cache->bic_tags.next,
136 struct blkid_struct_tag,
139 while (!list_empty(&tag->bit_names)) {
140 blkid_tag bad = list_entry(tag->bit_names.next,
141 struct blkid_struct_tag,
144 DBG(DEBUG_CACHE, printf("warning: unfreed tag %s=%s\n",
145 bad->bit_name, bad->bit_val));
150 free(cache->bic_filename);
155 void blkid_gc_cache(blkid_cache cache)
157 struct list_head *p, *pnext;
163 list_for_each_safe(p, pnext, &cache->bic_devs) {
164 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
167 if (stat(dev->bid_name, &st) < 0) {
169 printf("freeing %s\n", dev->bid_name));
171 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
174 printf("Device %s exists\n", dev->bid_name));
181 int main(int argc, char** argv)
183 blkid_cache cache = NULL;
186 blkid_debug_mask = DEBUG_ALL;
188 fprintf(stderr, "Usage: %s [filename] \n", argv[0]);
192 if ((ret = blkid_get_cache(&cache, argv[1])) < 0) {
193 fprintf(stderr, "error %d parsing cache file %s\n", ret,
194 argv[1] ? argv[1] : BLKID_CACHE_FILE);
197 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
198 fprintf(stderr, "%s: error creating cache (%d)\n",
202 if ((ret = blkid_probe_all(cache) < 0))
203 fprintf(stderr, "error probing devices\n");
205 blkid_put_cache(cache);