X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=devices%2Fudev-helper.c;h=a55d3780906e15d5274fec996b2bf6ce8906bd5d;hb=aab818c10b1fa68b968cabc852680f8ec0fe1360;hp=4ec054b38411ad8e053a2f3171de89dc2e865e84;hpb=9abd11523d69b821f034bb0cb597880c2b61210e;p=petitboot diff --git a/devices/udev-helper.c b/devices/udev-helper.c index 4ec054b..a55d378 100644 --- a/devices/udev-helper.c +++ b/devices/udev-helper.c @@ -1,4 +1,6 @@ +#define _GNU_SOURCE + #include #include #include @@ -17,6 +19,7 @@ #include #include "parser.h" +#include "paths.h" #include "petitboot-paths.h" /* Define below to operate without the frontend */ @@ -181,28 +184,29 @@ int connect_to_socket() #endif } -int mount_device(const char *dev_path, char *mount_path) +int mount_device(const char *dev_path) { - char *dir; - const char *basename; + 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)); @@ -220,13 +224,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; } @@ -348,9 +349,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; } @@ -488,7 +489,7 @@ 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()); @@ -499,6 +500,8 @@ int main(int argc, char **argv) return EXIT_FAILURE; } + set_mount_base(TMP_DIR); + if (connect_to_socket()) return EXIT_FAILURE;