]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
daemon: implement mkdir -p directly in lldpd
authorVincent Bernat <vincent@bernat.ch>
Wed, 8 Aug 2018 21:06:39 +0000 (23:06 +0200)
committerVincent Bernat <vincent@bernat.ch>
Wed, 8 Aug 2018 21:12:21 +0000 (23:12 +0200)
It's difficult to know the path to mkdir. If we use the one from
autoconf (@mkdir_p@), we get the path from the host, not the target.
If we hardcode `/bin/mkdir`, we may not work on platforms like NixOS.

See https://github.com/NixOS/nixpkgs/issues/44507.

src/daemon/lldpd.service.in
src/daemon/priv.c

index 429183030c09201b55fb9049df0d5a408ed15723..920a1311975d302549bdb4ea90b4b3ea21a5d82b 100644 (file)
@@ -9,7 +9,6 @@ Type=notify
 NotifyAccess=main
 EnvironmentFile=-/etc/default/lldpd
 EnvironmentFile=-/etc/sysconfig/lldpd
-ExecStartPre=/bin/mkdir -p @PRIVSEP_CHROOT@
 ExecStart=@sbindir@/lldpd $DAEMON_ARGS $LLDPD_OPTIONS
 Restart=on-failure
 PrivateTmp=yes
index 2b28bbd6947455af8d3414e8b0b6e793c5562bfb..bf9fb550fb0dd634ce43e044056f3a8ea1f08bda 100644 (file)
@@ -510,18 +510,40 @@ sig_chld(int sig)
        priv_exit_rc_status(rc, status);
 }
 
+/* Create a directory recursively. */
+static int mkdir_p(const char *pathname, mode_t mode)
+{
+       char path[PATH_MAX+1], current[PATH_MAX+1];
+       char *tok;
+
+       if (strlcpy(path, pathname, sizeof(path)) >= sizeof(path)) {
+               errno = ENAMETOOLONG;
+               return -1;
+       }
+
+       /* Use strtok which will provides non-empty tokens only. */
+       if (path[0] == '/') current[0] = '/';
+       tok = strtok(path, "/");
+       while (tok) {
+               strcat(current, tok);
+               if (mkdir(current, mode) != 0 && errno != EEXIST)
+                       return -1;
+               strcat(current, "/");
+               tok = strtok(NULL, "/");
+       }
+
+       errno = 0;
+       return 0;
+}
+
 /* Initialization */
 #define LOCALTIME "/etc/localtime"
 static void
 priv_setup_chroot(const char *chrootdir)
 {
        /* Create chroot if it does not exist */
-       if (mkdir(chrootdir, 0755) == -1) {
-               if (errno != EEXIST)
-                       fatal("privsep", "unable to create chroot directory");
-       } else {
-               log_info("privsep", "created chroot directory %s",
-                   chrootdir);
+       if (mkdir_p(chrootdir, 0755) == -1) {
+               fatal("privsep", "unable to create chroot directory");
        }
 
        /* Check if /etc/localtime exists in chroot or outside chroot */