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