]> git.ozlabs.org Git - petitboot/blobdiff - devices/udev-helper.c
Make kboot_parser global options array static
[petitboot] / devices / udev-helper.c
index f0b80b3a0c330a344ca8a21a215dc44326d812e4..87a7fc19ab431827c15ec1615cd94a23b5b5d113 100644 (file)
@@ -1,4 +1,6 @@
 
+#define _GNU_SOURCE
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -17,6 +19,7 @@
 #include <sys/ioctl.h>
 
 #include "parser.h"
+#include "paths.h"
 #include "petitboot-paths.h"
 
 /* Define below to operate without the frontend */
@@ -33,7 +36,7 @@ void pb_log(const char *fmt, ...)
        va_list ap;
 
        va_start(ap, fmt);
-       fprintf(logf, fmt, ap);
+       vfprintf(logf, fmt, ap);
        va_end(ap);
 }
 
@@ -181,28 +184,120 @@ int connect_to_socket()
 #endif
 }
 
-int mount_device(const char *dev_path, char *mount_path)
+static int mkdir_recursive(const char *dir)
 {
-       char *dir;
-       const char *basename;
+       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)
+{
+       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)) {
-               pb_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) {
                pb_log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
@@ -221,12 +316,11 @@ int mount_device(const char *dev_path, char *mount_path)
        }
 
        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
-               strcpy(mount_path, dir);
+               setup_device_links(dev_path);
                rc = 0;
        }
 
 out:
-       free(dir);
        return rc;
 }
 
@@ -323,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);
@@ -336,7 +430,8 @@ static int is_removable_device(const char *sysfs_path)
 
 static int is_ignored_device(const char *devname)
 {
-       static const char *ignored_devices[] = { "/dev/ram", NULL };
+       static const char *ignored_devices[] =
+               { "/dev/ram", "/dev/loop", NULL };
        const char **dev;
 
        for (dev = ignored_devices; *dev; dev++)
@@ -348,9 +443,9 @@ static int is_ignored_device(const char *devname)
 
 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)) {
+       if (mount_device(dev_path)) {
                pb_log("failed to mount %s\n", dev_path);
                return EXIT_FAILURE;
        }
@@ -394,35 +489,30 @@ 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");
                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");
                detach_and_sleep(REMOVABLE_SLEEP_DELAY);
        }
 }
@@ -432,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;
@@ -440,20 +529,17 @@ 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");
                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");
                detach_and_sleep(REMOVABLE_SLEEP_DELAY);
        }
 }
@@ -488,7 +574,9 @@ int main(int argc, char **argv)
 
        action = getenv("ACTION");
 
-       logf = fopen("/var/tmp/petitboot-udev-helpers.log", "a");
+       logf = fopen("/var/log/petitboot-udev-helpers.log", "a");
+       if (!logf)
+               logf = stdout;
        pb_log("%d started\n", getpid());
        rc = EXIT_SUCCESS;
 
@@ -497,6 +585,8 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
+       set_mount_base(TMP_DIR);
+
        if (connect_to_socket())
                return EXIT_FAILURE;