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