]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
lldpd: use vfork+exec instead of fork+exec feature/noprivsep
authorVincent Bernat <bernat@luffy.cx>
Sun, 15 Dec 2013 12:07:19 +0000 (13:07 +0100)
committerVincent Bernat <bernat@luffy.cx>
Tue, 31 Dec 2013 14:18:33 +0000 (15:18 +0100)
This will help compatibility with systems not having a working `fork()`
implementation. For those who do, we will still use `fork()`.

configure.ac
src/daemon/lldpd.c
src/daemon/lldpd.h

index 553a17c38cb2d3b55bf06da5f594e23560550ce3..dcaaaa6077ea835efbaa9cb425768638d4632b2a 100644 (file)
@@ -101,6 +101,7 @@ lldp_CHECK_ALIGNOF
 AC_CONFIG_LIBOBJ_DIR([src/compat])
 AC_FUNC_MALLOC
 AC_FUNC_REALLOC
+AC_FUNC_FORK
 AC_SEARCH_LIBS([setproctitle], [util bsd])
 AC_REPLACE_FUNCS([setproctitle])
 AC_CHECK_FUNCS([setproctitle_init])
index eff4455c8d04900195dc2ba5efb9e9e359bf0284..f3cc7fe14dab914beb1fde0187119033bb0836aa 100644 (file)
@@ -645,11 +645,11 @@ lldpd_get_lsb_release() {
                return NULL;
        }
 
-       if ((pid = fork()) < 0) {
+       pid = vfork();
+       switch (pid) {
+       case -1:
                log_warn("localchassis", "unable to fork");
                return NULL;
-       }
-       switch (pid) {
        case 0:
                /* Child, exec lsb_release */
                close(pipefd[0]);
@@ -661,7 +661,7 @@ lldpd_get_lsb_release() {
                        if (pipefd[1] > 2) close(pipefd[1]);
                        execvp("lsb_release", command);
                }
-               exit(127);
+               _exit(127);
                break;
        default:
                /* Father, read the output from the children */
@@ -1108,9 +1108,15 @@ lldpd_exit(struct lldpd *cfg)
 static pid_t
 lldpd_configure(int debug, const char *path, const char *ctlname)
 {
-       pid_t lldpcli = fork();
+       pid_t lldpcli = vfork();
        int devnull;
 
+       char sdebug[debug + 3];
+       memset(sdebug, 'd', debug + 3);
+       sdebug[debug + 2] = '\0';
+       sdebug[0] = '-'; sdebug[1] = 's';
+       log_debug("main", "invoke %s %s", path, sdebug);
+
        switch (lldpcli) {
        case -1:
                log_warn("main", "unable to fork");
@@ -1118,27 +1124,20 @@ lldpd_configure(int debug, const char *path, const char *ctlname)
        case 0:
                /* Child, exec lldpcli */
                if ((devnull = open("/dev/null", O_RDWR, 0)) != -1) {
-                       char sdebug[debug + 3];
-                       memset(sdebug, 'd', debug + 3);
-                       sdebug[debug + 2] = '\0';
-                       sdebug[0] = '-'; sdebug[1] = 's';
-
                        dup2(devnull,   STDIN_FILENO);
                        dup2(devnull,   STDOUT_FILENO);
                        if (devnull > 2) close(devnull);
 
-                       log_debug("main", "invoke %s %s", path, sdebug);
-                       if (execl(path, "lldpcli", sdebug,
-                               "-u", ctlname,
-                               "-c", SYSCONFDIR "/lldpd.conf",
-                               "-c", SYSCONFDIR "/lldpd.d",
-                               "resume",
-                               NULL) == -1) {
-                               log_warn("main", "unable to execute %s", path);
-                               log_warnx("main", "configuration is incomplete, lldpd needs to be unpaused");
-                       }
+                       execl(path, "lldpcli", sdebug,
+                           "-u", ctlname,
+                           "-c", SYSCONFDIR "/lldpd.conf",
+                           "-c", SYSCONFDIR "/lldpd.d",
+                           "resume",
+                           NULL);
+                       log_warn("main", "unable to execute %s", path);
+                       log_warnx("main", "configuration is incomplete, lldpd needs to be unpaused");
                }
-               exit(127);
+               _exit(127);
                break;
        default:
                /* Father, don't do anything stupid */
index 56835763177544f1af9fdbf49a1b4c05879fc976..38bb0fbf417c4e043416b91a436581cb7c5cb77f 100644 (file)
 #include <netinet/in.h>
 #include <sys/un.h>
 
+#if HAVE_VFORK_H
+# include <vfork.h>
+#endif
+#if HAVE_WORKING_FORK
+# define vfork fork
+#endif
+
 #include "lldp-tlv.h"
 #if defined (ENABLE_CDP) || defined (ENABLE_FDP)
 #  include "cdp.h"