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