Whamcloud - gitweb
Remove trailing whitespace for the entire source tree
[tools/e2fsprogs.git] / lib / blkid / cache.c
index 9312416..7c95726 100644 (file)
  * %End-Header%
  */
 
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
 #include <stdlib.h>
+#include <string.h>
+#ifdef HAVE_SYS_PRCTL_H
+#include <sys/prctl.h>
+#else
+#define PR_GET_DUMPABLE 3
+#endif
+#if (!defined(HAVE_PRCTL) && defined(linux))
+#include <sys/syscall.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
 #include "blkidP.h"
 
-int blkid_debug_mask;
+int blkid_debug_mask = 0;
+
 
-blkid_cache blkid_new_cache(void)
+static char *safe_getenv(const char *arg)
+{
+       if ((getuid() != geteuid()) || (getgid() != getegid()))
+               return NULL;
+#if HAVE_PRCTL
+       if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
+               return NULL;
+#else
+#if (defined(linux) && defined(SYS_prctl))
+       if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
+               return NULL;
+#endif
+#endif
+
+#ifdef HAVE___SECURE_GETENV
+       return __secure_getenv(arg);
+#else
+       return getenv(arg);
+#endif
+}
+
+#if 0 /* ifdef CONFIG_BLKID_DEBUG */
+static blkid_debug_dump_cache(int mask, blkid_cache cache)
+{
+       struct list_head *p;
+
+       if (!cache) {
+               printf("cache: NULL\n");
+               return;
+       }
+
+       printf("cache: time = %lu\n", cache->bic_time);
+       printf("cache: flags = 0x%08X\n", cache->bic_flags);
+
+       list_for_each(p, &cache->bic_devs) {
+               blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
+               blkid_debug_dump_dev(dev);
+       }
+}
+#endif
+
+int blkid_get_cache(blkid_cache *ret_cache, const char *filename)
 {
        blkid_cache cache;
 
@@ -29,15 +89,27 @@ blkid_cache blkid_new_cache(void)
        }
 #endif
 
-       DBG(DEBUG_CACHE, printf("initializing empty cache\n"));
+       DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n",
+                               filename ? filename : "default cache"));
 
        if (!(cache = (blkid_cache) calloc(1, sizeof(struct blkid_struct_cache))))
-               return NULL;
+               return -BLKID_ERR_MEM;
 
        INIT_LIST_HEAD(&cache->bic_devs);
        INIT_LIST_HEAD(&cache->bic_tags);
 
-       return cache;
+       if (filename && !strlen(filename))
+               filename = 0;
+       if (!filename)
+               filename = safe_getenv("BLKID_FILE");
+       if (!filename)
+               filename = BLKID_CACHE_FILE;
+       cache->bic_filename = blkid_strdup(filename);
+
+       blkid_read_cache(cache);
+
+       *ret_cache = cache;
+       return 0;
 }
 
 void blkid_put_cache(blkid_cache cache)
@@ -48,8 +120,8 @@ void blkid_put_cache(blkid_cache cache)
        (void) blkid_flush_cache(cache);
 
        DBG(DEBUG_CACHE, printf("freeing cache struct\n"));
-       
-       /* DEB_DUMP_CACHE(cache); */
+
+       /* DBG(DEBUG_CACHE, blkid_debug_dump_cache(cache)); */
 
        while (!list_empty(&cache->bic_devs)) {
                blkid_dev dev = list_entry(cache->bic_devs.next,
@@ -65,7 +137,7 @@ void blkid_put_cache(blkid_cache cache)
 
                while (!list_empty(&tag->bit_names)) {
                        blkid_tag bad = list_entry(tag->bit_names.next,
-                                                  struct blkid_struct_tag, 
+                                                  struct blkid_struct_tag,
                                                   bit_names);
 
                        DBG(DEBUG_CACHE, printf("warning: unfreed tag %s=%s\n",
@@ -76,10 +148,35 @@ void blkid_put_cache(blkid_cache cache)
        }
        if (cache->bic_filename)
                free(cache->bic_filename);
-       
+
        free(cache);
 }
 
+void blkid_gc_cache(blkid_cache cache)
+{
+       struct list_head *p, *pnext;
+       struct stat st;
+
+       if (!cache)
+               return;
+
+       list_for_each_safe(p, pnext, &cache->bic_devs) {
+               blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
+               if (!p)
+                       break;
+               if (stat(dev->bid_name, &st) < 0) {
+                       DBG(DEBUG_CACHE,
+                           printf("freeing %s\n", dev->bid_name));
+                       blkid_free_dev(dev);
+                       cache->bic_flags |= BLKID_BIC_FL_CHANGED;
+               } else {
+                       DBG(DEBUG_CACHE,
+                           printf("Device %s exists\n", dev->bid_name));
+               }
+       }
+}
+
+
 #ifdef TEST_PROGRAM
 int main(int argc, char** argv)
 {
@@ -97,8 +194,9 @@ int main(int argc, char** argv)
                        argv[1] ? argv[1] : BLKID_CACHE_FILE);
                exit(1);
        }
-       if ((cache = blkid_new_cache()) == NULL) {
-               fprintf(stderr, "%s: error creating cache\n", argv[0]);
+       if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
+               fprintf(stderr, "%s: error creating cache (%d)\n",
+                       argv[0], ret);
                exit(1);
        }
        if ((ret = blkid_probe_all(cache) < 0))