]> git.ozlabs.org Git - petitboot/blobdiff - devices/udev-helper.c
Make kboot_parser global options array static
[petitboot] / devices / udev-helper.c
index 6a9a42bac12e9c626e1c768dd7664edf759ac74c..87a7fc19ab431827c15ec1615cd94a23b5b5d113 100644 (file)
@@ -1,6 +1,9 @@
 
+#define _GNU_SOURCE
+
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <stdint.h>
 #include <unistd.h>
 #include <sys/types.h>
@@ -15,7 +18,8 @@
 #include <linux/cdrom.h>
 #include <sys/ioctl.h>
 
-#include "udev-helper.h"
+#include "parser.h"
+#include "paths.h"
 #include "petitboot-paths.h"
 
 /* Define below to operate without the frontend */
 /* Delay in seconds between polling of removable devices */
 #define REMOVABLE_SLEEP_DELAY  2
 
-extern struct parser native_parser;
-extern struct parser yaboot_parser;
-extern struct parser kboot_parser;
 static FILE *logf;
 static int sock;
 
-/* array of parsers, ordered by priority */
-static struct parser *parsers[] = {
-       &native_parser,
-       &yaboot_parser,
-       &kboot_parser,
-       NULL
-};
-
-#define log(...) fprintf(logf, __VA_ARGS__)
-
-static void iterate_parsers(const char *devpath, const char *mountpoint)
+void pb_log(const char *fmt, ...)
 {
-       int i;
-
-       log("trying parsers for %s@%s\n", devpath, mountpoint);
+       va_list ap;
 
-       for (i = 0; parsers[i]; i++) {
-               log("\ttrying parser '%s'\n", parsers[i]->name);
-               /* just use a dummy device path for now */
-               if (parsers[i]->parse(devpath, mountpoint))
-                       /*return*/;
-       }
-       log("\tno boot_options found\n");
+       va_start(ap, fmt);
+       vfprintf(logf, fmt, ap);
+       va_end(ap);
 }
 
 static void print_boot_option(const struct boot_option *opt)
 {
-       log("\tname: %s\n", opt->name);
-       log("\tdescription: %s\n", opt->description);
-       log("\tboot_image: %s\n", opt->boot_image_file);
-       log("\tinitrd: %s\n", opt->initrd_file);
-       log("\tboot_args: %s\n", opt->boot_args);
+       pb_log("\tname: %s\n", opt->name);
+       pb_log("\tdescription: %s\n", opt->description);
+       pb_log("\tboot_image: %s\n", opt->boot_image_file);
+       pb_log("\tinitrd: %s\n", opt->initrd_file);
+       pb_log("\tboot_args: %s\n", opt->boot_args);
 
 }
 
 static void print_device(const struct device *dev)
 {
-       log("\tid: %s\n", dev->id);
-       log("\tname: %s\n", dev->name);
-       log("\tdescription: %s\n", dev->description);
-       log("\tboot_image: %s\n", dev->icon_file);
-}
-
-
-void free_device(struct device *dev)
-{
-       if (!dev)
-               return;
-       if (dev->id)
-               free(dev->id);
-       if (dev->name)
-               free(dev->name);
-       if (dev->description)
-               free(dev->description);
-       if (dev->icon_file)
-               free(dev->icon_file);
-       free(dev);
-}
-
-void free_boot_option(struct boot_option *opt)
-{
-       if (!opt)
-               return;
-       if (opt->name)
-               free(opt->name);
-       if (opt->description)
-               free(opt->description);
-       if (opt->icon_file)
-               free(opt->icon_file);
-       if (opt->boot_image_file)
-               free(opt->boot_image_file);
-       if (opt->initrd_file)
-               free(opt->initrd_file);
-       if (opt->boot_args)
-               free(opt->boot_args);
-       free(opt);
+       pb_log("\tid: %s\n", dev->id);
+       pb_log("\tname: %s\n", dev->name);
+       pb_log("\tdescription: %s\n", dev->description);
+       pb_log("\tboot_image: %s\n", dev->icon_file);
 }
 
 static int write_action(int fd, enum device_action action)
@@ -122,7 +72,7 @@ static int write_string(int fd, const char *str)
        if (!str) {
                len_buf = 0;
                if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
-                       log("write failed: %s\n", strerror(errno));
+                       pb_log("write failed: %s\n", strerror(errno));
                        return -1;
                }
                return 0;
@@ -130,20 +80,20 @@ static int write_string(int fd, const char *str)
 
        len = strlen(str);
        if (len > (1ull << (sizeof(len_buf) * 8 - 1))) {
-               log("string too large\n");
+               pb_log("string too large\n");
                return -1;
        }
 
        len_buf = __cpu_to_be32(len);
        if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
-               log("write failed: %s\n", strerror(errno));
+               pb_log("write failed: %s\n", strerror(errno));
                return -1;
        }
 
        while (pos < len) {
                int rc = write(fd, str, len - pos);
                if (rc <= 0) {
-                       log("write failed: %s\n", strerror(errno));
+                       pb_log("write failed: %s\n", strerror(errno));
                        return -1;
                }
                pos += rc;
@@ -157,7 +107,7 @@ int add_device(const struct device *dev)
 {
        int rc;
 
-       log("device added:\n");
+       pb_log("device added:\n");
        print_device(dev);
        rc = write_action(sock, DEV_ACTION_ADD_DEVICE) ||
                write_string(sock, dev->id) ||
@@ -166,7 +116,7 @@ int add_device(const struct device *dev)
                write_string(sock, dev->icon_file);
 
        if (rc)
-               log("error writing device %s to socket\n", dev->name);
+               pb_log("error writing device %s to socket\n", dev->name);
 
        return rc;
 }
@@ -175,7 +125,7 @@ int add_boot_option(const struct boot_option *opt)
 {
        int rc;
 
-       log("boot option added:\n");
+       pb_log("boot option added:\n");
        print_boot_option(opt);
 
        rc = write_action(sock, DEV_ACTION_ADD_OPTION) ||
@@ -188,7 +138,7 @@ int add_boot_option(const struct boot_option *opt)
                write_string(sock, opt->boot_args);
 
        if (rc)
-               log("error writing boot option %s to socket\n", opt->name);
+               pb_log("error writing boot option %s to socket\n", opt->name);
 
        return rc;
 }
@@ -207,7 +157,7 @@ int connect_to_socket()
 
        fd = socket(PF_UNIX, SOCK_STREAM, 0);
        if (fd == -1) {
-               log("can't create socket: %s\n", strerror(errno));
+               pb_log("can't create socket: %s\n", strerror(errno));
                return -1;
        }
 
@@ -215,7 +165,7 @@ int connect_to_socket()
        strcpy(addr.sun_path, PBOOT_DEVICE_SOCKET);
 
        if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
-               log("can't connect to %s: %s\n",
+               pb_log("can't connect to %s: %s\n",
                                addr.sun_path, strerror(errno));
                return -1;
        }
@@ -226,7 +176,7 @@ int connect_to_socket()
        int fd;
        fd = open("./debug_socket", O_WRONLY | O_CREAT, 0640);
        if (fd < 0) {
-               log("can't create output file: %s\n", strerror(errno));
+               pb_log("can't create output file: %s\n", strerror(errno));
                return -1;
        }
        sock = fd;
@@ -234,31 +184,123 @@ int connect_to_socket()
 #endif
 }
 
-int mount_device(const char *dev_path, char *mount_path)
+static int mkdir_recursive(const char *dir)
+{
+       char *str, *sep;
+       int mode = 0755;
+       struct stat statbuf;
+
+       pb_log("mkdir_recursive(%s)\n", dir);
+
+       if (!*dir)
+               return 0;
+
+       if (!stat(dir, &statbuf)) {
+               if (!S_ISDIR(statbuf.st_mode)) {
+                       pb_log("%s: %s exists, but isn't a directory\n",
+                                       __func__, dir);
+                       return -1;
+               }
+               return 0;
+       }
+
+       str = strdup(dir);
+       sep = strchr(*str == '/' ? str + 1 : str, '/');
+
+       while (1) {
+
+               /* terminate the path at sep */
+               if (sep)
+                       *sep = '\0';
+               pb_log("mkdir(%s)\n", str);
+
+               if (mkdir(str, mode) && errno != EEXIST) {
+                       pb_log("mkdir(%s): %s\n", str, strerror(errno));
+                       return -1;
+               }
+
+               if (!sep)
+                       break;
+
+               /* reset dir to the full path */
+               strcpy(str, dir);
+               sep = strchr(sep + 1, '/');
+       }
+
+       free(str);
+
+       return 0;
+}
+
+static void setup_device_links(const char *device)
 {
-       char *dir;
-       const char *basename;
+       struct link {
+               char *env, *dir;
+       } *link, links[] = {
+               {
+                       .env = "ID_FS_UUID",
+                       .dir = "disk/by-uuid"
+               },
+               {
+                       .env = "ID_FS_LABEL",
+                       .dir = "disk/by-label"
+               },
+               {
+                       .env = NULL
+               }
+       };
+
+       for (link = links; link->env; link++) {
+               char *value, *dir, *path;
+
+               value = getenv(link->env);
+               if (!value)
+                       continue;
+
+               value = encode_label(value);
+               dir = join_paths(TMP_DIR, link->dir);
+               path = join_paths(dir, value);
+
+               if (!mkdir_recursive(dir)) {
+                       unlink(path);
+                       if (symlink(mountpoint_for_device(device), path)) {
+                               pb_log("symlink(%s): %s\n",
+                                               path, strerror(errno));
+                       }
+               }
+
+               free(path);
+               free(dir);
+               free(value);
+       }
+}
+
+int mount_device(const char *dev_path)
+{
+       const char *dir;
        int pid, status, rc = -1;
+       struct stat statbuf;
 
-       basename = strrchr(dev_path, '/');
-       if (basename)
-               basename++;
-       else
-               basename = dev_path;
-       /* create a unique mountpoint */
-       dir = malloc(strlen(TMP_DIR) + 13 + strlen(basename));
-       sprintf(dir, "%s/mnt-%s-XXXXXX", TMP_DIR, basename);
-
-       if (!mkdtemp(dir)) {
-               log("failed to create temporary directory in %s: %s",
-                               TMP_DIR, strerror(errno));
-               goto out;
+       dir = mountpoint_for_device(dev_path);
+
+       if (stat(dir, &statbuf)) {
+               if (mkdir(dir, 0755)) {
+                       pb_log("couldn't create directory %s: %s\n",
+                                       dir, strerror(errno));
+                       goto out;
+               }
+       } else {
+               if (!S_ISDIR(statbuf.st_mode)) {
+                       pb_log("mountpoint %s exists, "
+                                       "but isn't a directory\n", dir);
+                       goto out;
+               }
        }
 
+
        pid = fork();
        if (pid == -1) {
-               log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
+               pb_log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
                goto out;
        }
 
@@ -268,17 +310,17 @@ int mount_device(const char *dev_path, char *mount_path)
        }
 
        if (waitpid(pid, &status, 0) == -1) {
-               log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
+               pb_log("%s: waitpid failed: %s\n", __FUNCTION__,
+                               strerror(errno));
                goto out;
        }
 
        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
-               strcpy(mount_path, dir);
+               setup_device_links(dev_path);
                rc = 0;
        }
 
 out:
-       free(dir);
        return rc;
 }
 
@@ -289,7 +331,7 @@ static int unmount_device(const char *dev_path)
        pid = fork();
 
        if (pid == -1) {
-               log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
+               pb_log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
                return -1;
        }
 
@@ -299,7 +341,8 @@ static int unmount_device(const char *dev_path)
        }
 
        if (waitpid(pid, &status, 0) == -1) {
-               log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
+               pb_log("%s: waitpid failed: %s\n", __FUNCTION__,
+                               strerror(errno));
                return -1;
        }
 
@@ -308,22 +351,6 @@ static int unmount_device(const char *dev_path)
        return rc;
 }
 
-const char *generic_icon_file(enum generic_icon_type type)
-{
-       switch (type) {
-       case ICON_TYPE_DISK:
-               return artwork_pathname("hdd.png");
-       case ICON_TYPE_USB:
-               return artwork_pathname("usbpen.png");
-       case ICON_TYPE_OPTICAL:
-               return artwork_pathname("cdrom.png");
-       case ICON_TYPE_NETWORK:
-       case ICON_TYPE_UNKNOWN:
-               break;
-       }
-       return artwork_pathname("hdd.png");
-}
-
 static const struct device fake_boot_devices[] =
 {
        {
@@ -390,7 +417,7 @@ static int is_removable_device(const char *sysfs_path)
 
        sprintf(full_path, "/sys/%s/removable", sysfs_path);    
        fd = open(full_path, O_RDONLY);
-       printf(" -> removable check on %s, fd=%d\n", full_path, fd);
+       pb_log(" -> removable check on %s, fd=%d\n", full_path, fd);
        if (fd < 0)
                return 0;
        buf_len = read(fd, buf, 79);
@@ -401,22 +428,60 @@ 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", "/dev/loop", 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];
+       const char *mountpoint = mountpoint_for_device(dev_path);
 
-       if (mount_device(dev_path, mountpoint)) {
-               log("failed to mount %s\n", dev_path);
+       if (mount_device(dev_path)) {
+               pb_log("failed to mount %s\n", dev_path);
                return EXIT_FAILURE;
        }
 
-       log("mounted %s at %s\n", dev_path, mountpoint);
+       pb_log("mounted %s at %s\n", dev_path, mountpoint);
 
        iterate_parsers(dev_path, mountpoint);
 
        return EXIT_SUCCESS;
 }
 
+static void detach_and_sleep(int sec)
+{
+       static int forked = 0;
+       int rc = 0;
+
+       if (sec <= 0)
+               return;
+
+       if (!forked) {
+               pb_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)
 {
@@ -424,36 +489,31 @@ static int poll_device_plug(const char *dev_path,
 
        /* Polling loop for optical drive */
        for (; (*optical) != 0; ) {
-               printf("poll for optical drive insertion ...\n");
                fd = open(dev_path, O_RDONLY|O_NONBLOCK);
                if (fd < 0)
                        return EXIT_FAILURE;
                rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
                close(fd);
-               if (rc == -1) {
-                       printf("not an optical drive, fallback...\n");
+               if (rc == -1)
                        break;
-               }
+
                *optical = 1;
                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() */
        *optical = 0;
        for (;;) {
-               printf("poll for non-optical drive insertion ...\n");
                fd = open(dev_path, O_RDONLY);
                if (fd < 0 && errno != ENOMEDIUM)
                        return EXIT_FAILURE;
                close(fd);
                if (fd >= 0)
                        return EXIT_SUCCESS;
-               printf("no... waiting\n");
-               sleep(REMOVABLE_SLEEP_DELAY);
+               detach_and_sleep(REMOVABLE_SLEEP_DELAY);
        }
 }
 
@@ -462,7 +522,6 @@ static int poll_device_unplug(const char *dev_path, int optical)
        int rc, fd;
 
        for (;optical;) {
-               printf("poll for optical drive removal ...\n");
                fd = open(dev_path, O_RDONLY|O_NONBLOCK);
                if (fd < 0)
                        return EXIT_FAILURE;
@@ -470,21 +529,18 @@ static int poll_device_unplug(const char *dev_path, int optical)
                close(fd);
                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() */
        for (;;) {
-               printf("poll for non-optical drive removal ...\n");
                fd = open(dev_path, O_RDONLY);
                if (fd < 0 && errno != ENOMEDIUM)
                        return EXIT_FAILURE;
                close(fd);
                if (fd < 0)
                        return EXIT_SUCCESS;
-               printf("no... waiting\n");
-               sleep(REMOVABLE_SLEEP_DELAY);
+               detach_and_sleep(REMOVABLE_SLEEP_DELAY);
        }
 }
 
@@ -507,7 +563,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);
        }
 }
 
@@ -516,24 +572,26 @@ int main(int argc, char **argv)
        char *dev_path, *action;
        int rc;
 
-       /*if (fork())
-               return EXIT_SUCCESS;
-               */
        action = getenv("ACTION");
 
-       logf = stdout;
+       logf = fopen("/var/log/petitboot-udev-helpers.log", "a");
+       if (!logf)
+               logf = stdout;
+       pb_log("%d started\n", getpid());
        rc = EXIT_SUCCESS;
 
        if (!action) {
-               log("missing environment?\n");
+               pb_log("missing environment?\n");
                return EXIT_FAILURE;
        }
 
+       set_mount_base(TMP_DIR);
+
        if (connect_to_socket())
                return EXIT_FAILURE;
 
        if (streq(action, "fake")) {
-               log("fake mode");
+               pb_log("fake mode");
 
                add_device(&fake_boot_devices[0]);
                add_boot_option(&fake_boot_options[0]);
@@ -547,10 +605,13 @@ int main(int argc, char **argv)
 
        dev_path = getenv("DEVNAME");
        if (!dev_path) {
-               log("missing environment?\n");
+               pb_log("missing environment?\n");
                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))
@@ -558,7 +619,7 @@ int main(int argc, char **argv)
                else
                        rc = found_new_device(dev_path);
        } else if (streq(action, "remove")) {
-               log("%s removed\n", dev_path);
+               pb_log("%s removed\n", dev_path);
 
                remove_device(dev_path);
 
@@ -567,24 +628,8 @@ int main(int argc, char **argv)
                        ;
 
        } else {
-               log("invalid action '%s'\n", action);
+               pb_log("invalid action '%s'\n", action);
                rc = EXIT_FAILURE;
        }
        return rc;
 }
-
-/* convenience function for parsers */
-char *join_paths(const char *a, const char *b)
-{
-       char *full_path;
-
-       full_path = malloc(strlen(a) + strlen(b) + 2);
-
-       strcpy(full_path, a);
-       if (b[0] != '/')
-               strcat(full_path, "/");
-       strcat(full_path, b);
-
-       return full_path;
-}
-