chiark / gitweb /
udev: really exclude device-mapper from block device ownership event locking
[elogind.git] / src / activate / activate.c
index 5263969fcf572c1e1829fc59da8f87c849443a89..8c582739976a2e4116b99cb1721c95083575db1c 100644 (file)
@@ -38,7 +38,7 @@
 static char** arg_listen = NULL;
 static bool arg_accept = false;
 static char** arg_args = NULL;
 static char** arg_listen = NULL;
 static bool arg_accept = false;
 static char** arg_args = NULL;
-static char** arg_environ = NULL;
+static char** arg_setenv = NULL;
 
 static int add_epoll(int epoll_fd, int fd) {
         struct epoll_event ev = {
 
 static int add_epoll(int epoll_fd, int fd) {
         struct epoll_event ev = {
@@ -51,29 +51,12 @@ static int add_epoll(int epoll_fd, int fd) {
 
         ev.data.fd = fd;
         r = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
 
         ev.data.fd = fd;
         r = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
-        if (r < 0)
-                log_error("Failed to add event on epoll fd:%d for fd:%d: %m",
-                          epoll_fd, fd);
-        return -errno;
-}
-
-static int make_socket_fd(const char* address, int flags) {
-        SocketAddress a;
-        int fd, r;
-
-        r = socket_address_parse(&a, address);
         if (r < 0) {
         if (r < 0) {
-                log_error("Failed to parse socket: %s", strerror(-r));
-                return r;
-        }
-
-        fd = socket_address_listen(&a, flags, SOMAXCONN, SOCKET_ADDRESS_DEFAULT, NULL, false, false, 0755, 0644, NULL);
-        if (fd < 0) {
-                log_error("Failed to listen: %s", strerror(-r));
-                return fd;
+                log_error("Failed to add event on epoll fd:%d for fd:%d: %m", epoll_fd, fd);
+                return -errno;
         }
 
         }
 
-        return fd;
+        return 0;
 }
 
 static int open_sockets(int *epoll_fd, bool accept) {
 }
 
 static int open_sockets(int *epoll_fd, bool accept) {
@@ -99,6 +82,17 @@ static int open_sockets(int *epoll_fd, bool accept) {
                 }
         }
 
                 }
         }
 
+        /* Close logging and all other descriptors */
+        if (arg_listen) {
+                int except[3 + n];
+
+                for (fd = 0; fd < SD_LISTEN_FDS_START + n; fd++)
+                        except[fd] = fd;
+
+                log_close();
+                close_all_fds(except, 3 + n);
+        }
+
         /** Note: we leak some fd's on error here. I doesn't matter
          *  much, since the program will exit immediately anyway, but
          *  would be a pain to fix.
         /** Note: we leak some fd's on error here. I doesn't matter
          *  much, since the program will exit immediately anyway, but
          *  would be a pain to fix.
@@ -106,8 +100,9 @@ static int open_sockets(int *epoll_fd, bool accept) {
 
         STRV_FOREACH(address, arg_listen) {
 
 
         STRV_FOREACH(address, arg_listen) {
 
-                fd = make_socket_fd(*address, SOCK_STREAM | (arg_accept*SOCK_CLOEXEC));
+                fd = make_socket_fd(LOG_DEBUG, *address, SOCK_STREAM | (arg_accept*SOCK_CLOEXEC));
                 if (fd < 0) {
                 if (fd < 0) {
+                        log_open();
                         log_error("Failed to open '%s': %s", *address, strerror(-fd));
                         return fd;
                 }
                         log_error("Failed to open '%s': %s", *address, strerror(-fd));
                         return fd;
                 }
@@ -116,6 +111,9 @@ static int open_sockets(int *epoll_fd, bool accept) {
                 count ++;
         }
 
                 count ++;
         }
 
+        if (arg_listen)
+                log_open();
+
         *epoll_fd = epoll_create1(EPOLL_CLOEXEC);
         if (*epoll_fd < 0) {
                 log_error("Failed to create epoll object: %m");
         *epoll_fd = epoll_create1(EPOLL_CLOEXEC);
         if (*epoll_fd < 0) {
                 log_error("Failed to create epoll object: %m");
@@ -145,14 +143,14 @@ static int launch(char* name, char **argv, char **env, int fds) {
         char **s;
         unsigned i;
 
         char **s;
         unsigned i;
 
-        length = strv_length(arg_environ);
+        length = strv_length(arg_setenv);
 
         /* PATH, TERM, HOME, USER, LISTEN_FDS, LISTEN_PID, NULL */
         envp = new0(char *, length + 7);
         if (!envp)
                 return log_oom();
 
 
         /* PATH, TERM, HOME, USER, LISTEN_FDS, LISTEN_PID, NULL */
         envp = new0(char *, length + 7);
         if (!envp)
                 return log_oom();
 
-        STRV_FOREACH(s, arg_environ) {
+        STRV_FOREACH(s, arg_setenv) {
                 if (strchr(*s, '='))
                         envp[n_env++] = *s;
                 else {
                 if (strchr(*s, '='))
                         envp[n_env++] = *s;
                 else {
@@ -270,10 +268,10 @@ static void sigchld_hdl(int sig, siginfo_t *t, void *data) {
 
 static int install_chld_handler(void) {
         int r;
 
 static int install_chld_handler(void) {
         int r;
-        struct sigaction act;
-        zero(act);
-        act.sa_flags = SA_SIGINFO;
-        act.sa_sigaction = sigchld_hdl;
+        struct sigaction act = {
+                .sa_flags = SA_SIGINFO,
+                .sa_sigaction = sigchld_hdl,
+        };
 
         r = sigaction(SIGCHLD, &act, 0);
         if (r < 0)
 
         r = sigaction(SIGCHLD, &act, 0);
         if (r < 0)
@@ -285,12 +283,11 @@ static int help(void) {
         printf("%s [OPTIONS...]\n\n"
                "Listen on sockets and launch child on connection.\n\n"
                "Options:\n"
         printf("%s [OPTIONS...]\n\n"
                "Listen on sockets and launch child on connection.\n\n"
                "Options:\n"
-               "  -l --listen=ADDR     Listen for raw connections at ADDR\n"
-               "  -a --accept          Spawn separate child for each connection\n"
-               "  -h --help            Show this help and exit\n"
-               "  -E --environment=NAME[=VALUE]\n"
-               "                       Pass an environment variable to children\n"
-               "  --version            Print version string and exit\n"
+               "  -l --listen=ADDR         Listen for raw connections at ADDR\n"
+               "  -a --accept              Spawn separate child for each connection\n"
+               "  -h --help                Show this help and exit\n"
+               "  -E --setenv=NAME[=VALUE] Pass an environment variable to children\n"
+               "  --version                Print version string and exit\n"
                "\n"
                "Note: file descriptors from sd_listen_fds() will be passed through.\n"
                , program_invocation_short_name
                "\n"
                "Note: file descriptors from sd_listen_fds() will be passed through.\n"
                , program_invocation_short_name
@@ -305,11 +302,12 @@ static int parse_argv(int argc, char *argv[]) {
         };
 
         static const struct option options[] = {
         };
 
         static const struct option options[] = {
-                { "help",         no_argument,       NULL, 'h'           },
-                { "version",      no_argument,       NULL, ARG_VERSION   },
-                { "listen",       required_argument, NULL, 'l'           },
-                { "accept",       no_argument,       NULL, 'a'           },
-                { "environment",  required_argument, NULL, 'E'           },
+                { "help",        no_argument,       NULL, 'h'           },
+                { "version",     no_argument,       NULL, ARG_VERSION   },
+                { "listen",      required_argument, NULL, 'l'           },
+                { "accept",      no_argument,       NULL, 'a'           },
+                { "setenv",      required_argument, NULL, 'E'           },
+                { "environment", required_argument, NULL, 'E'           }, /* alias */
                 {}
         };
 
                 {}
         };
 
@@ -341,7 +339,7 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case 'E': {
                         break;
 
                 case 'E': {
-                        int r = strv_extend(&arg_environ, optarg);
+                        int r = strv_extend(&arg_setenv, optarg);
                         if (r < 0)
                                 return r;
 
                         if (r < 0)
                                 return r;