Home | History | Annotate | Download | only in blkid
      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 #include "config.h"
     14 #if HAVE_UNISTD_H
     15 #include <unistd.h>
     16 #endif
     17 #ifdef HAVE_ERRNO_H
     18 #include <errno.h>
     19 #endif
     20 #include <stdlib.h>
     21 #include <string.h>
     22 #ifdef HAVE_SYS_PRCTL_H
     23 #include <sys/prctl.h>
     24 #else
     25 #define PR_GET_DUMPABLE 3
     26 #endif
     27 #if (!defined(HAVE_PRCTL) && defined(linux))
     28 #include <sys/syscall.h>
     29 #endif
     30 #ifdef HAVE_SYS_STAT_H
     31 #include <sys/stat.h>
     32 #endif
     33 #include "blkidP.h"
     34 
     35 int blkid_debug_mask = 0;
     36 
     37 
     38 static char *safe_getenv(const char *arg)
     39 {
     40 	if ((getuid() != geteuid()) || (getgid() != getegid()))
     41 		return NULL;
     42 #if HAVE_PRCTL
     43 	if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
     44 		return NULL;
     45 #else
     46 #if (defined(linux) && defined(SYS_prctl))
     47 	if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
     48 		return NULL;
     49 #endif
     50 #endif
     51 
     52 #if defined(HAVE_SECURE_GETENV)
     53 	return secure_getenv(arg);
     54 #elif defined(HAVE___SECURE_GETENV)
     55 	return __secure_getenv(arg);
     56 #else
     57 	return getenv(arg);
     58 #endif
     59 }
     60 
     61 #if 0 /* ifdef CONFIG_BLKID_DEBUG */
     62 static blkid_debug_dump_cache(int mask, blkid_cache cache)
     63 {
     64 	struct list_head *p;
     65 
     66 	if (!cache) {
     67 		printf("cache: NULL\n");
     68 		return;
     69 	}
     70 
     71 	printf("cache: time = %lu\n", cache->bic_time);
     72 	printf("cache: flags = 0x%08X\n", cache->bic_flags);
     73 
     74 	list_for_each(p, &cache->bic_devs) {
     75 		blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
     76 		blkid_debug_dump_dev(dev);
     77 	}
     78 }
     79 #endif
     80 
     81 int blkid_get_cache(blkid_cache *ret_cache, const char *filename)
     82 {
     83 	blkid_cache cache;
     84 
     85 #ifdef CONFIG_BLKID_DEBUG
     86 	if (!(blkid_debug_mask & DEBUG_INIT)) {
     87 		char *dstr = getenv("BLKID_DEBUG");
     88 
     89 		if (dstr)
     90 			blkid_debug_mask = strtoul(dstr, 0, 0);
     91 		blkid_debug_mask |= DEBUG_INIT;
     92 	}
     93 #endif
     94 
     95 	DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n",
     96 				filename ? filename : "default cache"));
     97 
     98 	if (!(cache = (blkid_cache) calloc(1, sizeof(struct blkid_struct_cache))))
     99 		return -BLKID_ERR_MEM;
    100 
    101 	INIT_LIST_HEAD(&cache->bic_devs);
    102 	INIT_LIST_HEAD(&cache->bic_tags);
    103 
    104 	if (filename && !strlen(filename))
    105 		filename = 0;
    106 	if (!filename)
    107 		filename = safe_getenv("BLKID_FILE");
    108 	if (!filename)
    109 		filename = BLKID_CACHE_FILE;
    110 	cache->bic_filename = blkid_strdup(filename);
    111 
    112 	blkid_read_cache(cache);
    113 
    114 	*ret_cache = cache;
    115 	return 0;
    116 }
    117 
    118 void blkid_put_cache(blkid_cache cache)
    119 {
    120 	if (!cache)
    121 		return;
    122 
    123 	(void) blkid_flush_cache(cache);
    124 
    125 	DBG(DEBUG_CACHE, printf("freeing cache struct\n"));
    126 
    127 	/* DBG(DEBUG_CACHE, blkid_debug_dump_cache(cache)); */
    128 
    129 	while (!list_empty(&cache->bic_devs)) {
    130 		blkid_dev dev = list_entry(cache->bic_devs.next,
    131 					   struct blkid_struct_dev,
    132 					    bid_devs);
    133 		blkid_free_dev(dev);
    134 	}
    135 
    136 	while (!list_empty(&cache->bic_tags)) {
    137 		blkid_tag tag = list_entry(cache->bic_tags.next,
    138 					   struct blkid_struct_tag,
    139 					   bit_tags);
    140 
    141 		while (!list_empty(&tag->bit_names)) {
    142 			blkid_tag bad = list_entry(tag->bit_names.next,
    143 						   struct blkid_struct_tag,
    144 						   bit_names);
    145 
    146 			DBG(DEBUG_CACHE, printf("warning: unfreed tag %s=%s\n",
    147 						bad->bit_name, bad->bit_val));
    148 			blkid_free_tag(bad);
    149 		}
    150 		blkid_free_tag(tag);
    151 	}
    152 	free(cache->bic_filename);
    153 
    154 	free(cache);
    155 }
    156 
    157 void blkid_gc_cache(blkid_cache cache)
    158 {
    159 	struct list_head *p, *pnext;
    160 	struct stat st;
    161 
    162 	if (!cache)
    163 		return;
    164 
    165 	list_for_each_safe(p, pnext, &cache->bic_devs) {
    166 		blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
    167 		if (stat(dev->bid_name, &st) < 0) {
    168 			DBG(DEBUG_CACHE,
    169 			    printf("freeing %s\n", dev->bid_name));
    170 			blkid_free_dev(dev);
    171 			cache->bic_flags |= BLKID_BIC_FL_CHANGED;
    172 		} else {
    173 			DBG(DEBUG_CACHE,
    174 			    printf("Device %s exists\n", dev->bid_name));
    175 		}
    176 	}
    177 }
    178 
    179 
    180 #ifdef TEST_PROGRAM
    181 int main(int argc, char** argv)
    182 {
    183 	blkid_cache cache = NULL;
    184 	int ret;
    185 
    186 	blkid_debug_mask = DEBUG_ALL;
    187 	if ((argc > 2)) {
    188 		fprintf(stderr, "Usage: %s [filename] \n", argv[0]);
    189 		exit(1);
    190 	}
    191 
    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);
    195 		exit(1);
    196 	}
    197 	if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
    198 		fprintf(stderr, "%s: error creating cache (%d)\n",
    199 			argv[0], ret);
    200 		exit(1);
    201 	}
    202 	if ((ret = blkid_probe_all(cache) < 0))
    203 		fprintf(stderr, "error probing devices\n");
    204 
    205 	blkid_put_cache(cache);
    206 
    207 	return ret;
    208 }
    209 #endif
    210