]> git.ozlabs.org Git - petitboot/blobdiff - devices/udev-helper.c
Resolve device paths in kernel and initrd locations.
[petitboot] / devices / udev-helper.c
index f0b80b3a0c330a344ca8a21a215dc44326d812e4..661fb08b4ef9c2021984109b78d2d3af68fe9943 100644 (file)
@@ -1,4 +1,6 @@
 
+#define _GNU_SOURCE
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -33,7 +35,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 +183,92 @@ int connect_to_socket()
 #endif
 }
 
-int mount_device(const char *dev_path, char *mount_path)
+struct device_map {
+       char *dev, *mnt;
+};
+
+#define DEVICE_MAP_SIZE 32
+static struct device_map device_map[DEVICE_MAP_SIZE];
+
+const char *mountpoint_for_device(const char *dev_path)
 {
-       char *dir;
+       int i;
        const char *basename;
-       int pid, status, rc = -1;
 
-       basename = strrchr(dev_path, '/');
-       if (basename)
-               basename++;
-       else
+       /* shorten '/dev/foo' to 'foo' */
+       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;
+
+       /* check existing entries in the map */
+       for (i = 0; (i < DEVICE_MAP_SIZE) && device_map[i].dev; i++)
+               if (!strcmp(device_map[i].dev, basename))
+                       return device_map[i].mnt;
+
+       if (i == DEVICE_MAP_SIZE)
+               return NULL;
+
+       device_map[i].dev = strdup(dev_path);
+       asprintf(&device_map[i].mnt, "%s/%s", TMP_DIR, basename);
+       return device_map[i].mnt;
+}
+
+/**
+ * Resolve a path given in a config file, to a path in the local filesystem.
+ * Paths may be of the form:
+ *  device:path (eg /dev/sda:/boot/vmlinux)
+ *
+ * or just a path:
+ *  /boot/vmlinux
+ * - in this case, the default mountpoint is used.
+ *
+ * Returns a newly-allocated string containing a full path to the file in path
+ */
+char *resolve_path(const char *path, const char *default_mountpoint)
+{
+       char *ret;
+       const char *devpath, *sep;
+
+       sep = strchr(path, ':');
+       if (!sep) {
+               devpath = default_mountpoint;
+               asprintf(&ret, "%s/%s", devpath, path);
+       } else {
+               /* copy just the device name into tmp */
+               char *dev = strndup(path, sep - path);
+               devpath = mountpoint_for_device(dev);
+               asprintf(&ret, "%s/%s", devpath, sep + 1);
+               free(dev);
        }
 
+       return ret;
+}
+
+int mount_device(const char *dev_path)
+{
+       const char *dir;
+       int pid, status, rc = -1;
+       struct stat statbuf;
+
+       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));
@@ -220,13 +286,10 @@ int mount_device(const char *dev_path, char *mount_path)
                goto out;
        }
 
-       if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
-               strcpy(mount_path, dir);
+       if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
                rc = 0;
-       }
 
 out:
-       free(dir);
        return rc;
 }
 
@@ -323,7 +386,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);
@@ -348,9 +411,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 +457,35 @@ static int poll_device_plug(const char *dev_path,
 
        /* Polling loop for optical drive */
        for (; (*optical) != 0; ) {
-               printf("poll for optical drive insertion ...\n");
+               pb_log("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");
+                       pb_log("not an optical drive, fallback...\n");
                        break;
                }
                *optical = 1;
                if (rc == CDS_DISC_OK)
                        return EXIT_SUCCESS;
 
-               printf("no... waiting\n");
+               pb_log("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");
+               pb_log("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");
+               pb_log("no... waiting\n");
                detach_and_sleep(REMOVABLE_SLEEP_DELAY);
        }
 }
@@ -432,7 +495,7 @@ static int poll_device_unplug(const char *dev_path, int optical)
        int rc, fd;
 
        for (;optical;) {
-               printf("poll for optical drive removal ...\n");
+               pb_log("poll for optical drive removal ...\n");
                fd = open(dev_path, O_RDONLY|O_NONBLOCK);
                if (fd < 0)
                        return EXIT_FAILURE;
@@ -440,20 +503,20 @@ 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");
+               pb_log("no... waiting\n");
                detach_and_sleep(REMOVABLE_SLEEP_DELAY);
        }
 
        /* Fall back to bare open() */
        for (;;) {
-               printf("poll for non-optical drive removal ...\n");
+               pb_log("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");
+               pb_log("no... waiting\n");
                detach_and_sleep(REMOVABLE_SLEEP_DELAY);
        }
 }
@@ -489,6 +552,8 @@ int main(int argc, char **argv)
        action = getenv("ACTION");
 
        logf = fopen("/var/tmp/petitboot-udev-helpers.log", "a");
+       if (!logf)
+               logf = stdout;
        pb_log("%d started\n", getpid());
        rc = EXIT_SUCCESS;