Ignore ramdisk devices.
[petitboot] / devices / udev-helper.c
index da12129316142814462162674d4e7aabdd69513f..84b2bef95947cd16ea70ce5d311998f250ff5bd9 100644 (file)
@@ -26,6 +26,7 @@
 
 extern struct parser native_parser;
 extern struct parser yaboot_parser;
+extern struct parser kboot_parser;
 static FILE *logf;
 static int sock;
 
@@ -33,6 +34,7 @@ static int sock;
 static struct parser *parsers[] = {
        &native_parser,
        &yaboot_parser,
+       &kboot_parser,
        NULL
 };
 
@@ -48,7 +50,7 @@ static void iterate_parsers(const char *devpath, const char *mountpoint)
                log("\ttrying parser '%s'\n", parsers[i]->name);
                /* just use a dummy device path for now */
                if (parsers[i]->parse(devpath, mountpoint))
-                       return;
+                       /*return*/;
        }
        log("\tno boot_options found\n");
 }
@@ -370,6 +372,8 @@ enum generic_icon_type guess_device_type(void)
        const char *bus = getenv("ID_BUS");
        if (type && streq(type, "cd"))
                return ICON_TYPE_OPTICAL;
+       if (!bus)
+               return ICON_TYPE_UNKNOWN;
        if (streq(bus, "usb"))
                return ICON_TYPE_USB;
        if (streq(bus, "ata") || streq(bus, "scsi"))
@@ -397,6 +401,18 @@ static int is_removable_device(const char *sysfs_path)
        return strtol(buf, NULL, 10);
 }
 
+static int is_ignored_device(const char *devname)
+{
+       static const char *ignored_devices[] = { "/dev/ram", NULL };
+       const char **dev;
+
+       for (dev = ignored_devices; *dev; dev++)
+               if (!strncmp(devname, *dev, strlen(*dev)))
+                       return 1;
+
+       return 0;
+}
+
 static int found_new_device(const char *dev_path)
 {
        char mountpoint[PATH_MAX];
@@ -413,6 +429,31 @@ static int found_new_device(const char *dev_path)
        return EXIT_SUCCESS;
 }
 
+static void detach_and_sleep(int sec)
+{
+       static int forked = 0;
+       int rc = 0;
+
+       if (sec <= 0)
+               return;
+
+       if (!forked) {
+               log("running in background...");
+               rc = fork();
+               forked = 1;
+       }
+
+       if (rc == 0) {
+               sleep(sec);
+
+       } else if (rc == -1) {
+               perror("fork()");
+               exit(EXIT_FAILURE);
+       } else {
+               exit(EXIT_SUCCESS);
+       }
+}
+
 static int poll_device_plug(const char *dev_path,
                            int *optical)
 {
@@ -435,7 +476,7 @@ static int poll_device_plug(const char *dev_path,
                        return EXIT_SUCCESS;
 
                printf("no... waiting\n");
-               sleep(REMOVABLE_SLEEP_DELAY);
+               detach_and_sleep(REMOVABLE_SLEEP_DELAY);
        }
 
        /* Fall back to bare open() */
@@ -449,7 +490,7 @@ static int poll_device_plug(const char *dev_path,
                if (fd >= 0)
                        return EXIT_SUCCESS;
                printf("no... waiting\n");
-               sleep(REMOVABLE_SLEEP_DELAY);
+               detach_and_sleep(REMOVABLE_SLEEP_DELAY);
        }
 }
 
@@ -467,7 +508,7 @@ static int poll_device_unplug(const char *dev_path, int optical)
                if (rc != CDS_DISC_OK)
                        return EXIT_SUCCESS;
                printf("no... waiting\n");
-               sleep(REMOVABLE_SLEEP_DELAY);
+               detach_and_sleep(REMOVABLE_SLEEP_DELAY);
        }
 
        /* Fall back to bare open() */
@@ -480,7 +521,7 @@ static int poll_device_unplug(const char *dev_path, int optical)
                if (fd < 0)
                        return EXIT_SUCCESS;
                printf("no... waiting\n");
-               sleep(REMOVABLE_SLEEP_DELAY);
+               detach_and_sleep(REMOVABLE_SLEEP_DELAY);
        }
 }
 
@@ -503,7 +544,7 @@ static int poll_removable_device(const char *sysfs_path,
                /* Unmount it repeatedly, if needs be */
                while (mounted && !unmount_device(dev_path))
                        ;
-               sleep(1);
+               detach_and_sleep(1);
        }
 }
 
@@ -512,9 +553,6 @@ int main(int argc, char **argv)
        char *dev_path, *action;
        int rc;
 
-       /*if (fork())
-               return EXIT_SUCCESS;
-               */
        action = getenv("ACTION");
 
        logf = stdout;
@@ -547,6 +585,9 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
+       if (is_ignored_device(dev_path))
+               return EXIT_SUCCESS;
+
        if (streq(action, "add")) {
                char *sysfs_path = getenv("DEVPATH");
                if (sysfs_path && is_removable_device(sysfs_path))