chiark / gitweb /
unit: remove union Unit
[elogind.git] / src / socket.c
index 1f5e0673371b67e56468236071f2cc45956f8773..a9ab82c898931d175861ca2dc457469037d70010 100644 (file)
@@ -64,7 +64,7 @@ static void socket_init(Unit *u) {
         Socket *s = SOCKET(u);
 
         assert(u);
-        assert(u->meta.load_state == UNIT_STUB);
+        assert(u->load_state == UNIT_STUB);
 
         s->backlog = SOMAXCONN;
         s->timeout_usec = DEFAULT_TIMEOUT_USEC;
@@ -79,8 +79,8 @@ static void socket_init(Unit *u) {
         s->mark = -1;
 
         exec_context_init(&s->exec_context);
-        s->exec_context.std_output = u->meta.manager->default_std_output;
-        s->exec_context.std_error = u->meta.manager->default_std_error;
+        s->exec_context.std_output = u->manager->default_std_output;
+        s->exec_context.std_error = u->manager->default_std_error;
 
         s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
 }
@@ -98,7 +98,6 @@ static void socket_unwatch_control_pid(Socket *s) {
 static void socket_done(Unit *u) {
         Socket *s = SOCKET(u);
         SocketPort *p;
-        Meta *i;
 
         assert(s);
 
@@ -120,7 +119,7 @@ static void socket_done(Unit *u) {
 
         socket_unwatch_control_pid(s);
 
-        s->service = NULL;
+        unit_ref_unset(&s->service);
 
         free(s->tcp_congestion);
         s->tcp_congestion = NULL;
@@ -129,16 +128,6 @@ static void socket_done(Unit *u) {
         s->bind_to_device = NULL;
 
         unit_unwatch_timer(u, &s->timer_watch);
-
-        /* Make sure no service instance refers to us anymore. */
-        LIST_FOREACH(units_by_type, i, u->meta.manager->units_by_type[UNIT_SERVICE]) {
-                Service *service = (Service *) i;
-
-                if (service->accept_socket == s)
-                        service->accept_socket = NULL;
-
-                set_remove(service->configured_sockets, s);
-        }
 }
 
 static int socket_instantiate_service(Socket *s) {
@@ -153,7 +142,7 @@ static int socket_instantiate_service(Socket *s) {
          * here. For Accept=no this is mostly a NOP since the service
          * is figured out at load time anyway. */
 
-        if (s->service)
+        if (UNIT_DEREF(s->service))
                 return 0;
 
         assert(s->accept);
@@ -180,9 +169,10 @@ static int socket_instantiate_service(Socket *s) {
         }
 #endif
 
-        u->meta.no_gc = true;
-        s->service = SERVICE(u);
-        return 0;
+        u->no_gc = true;
+        unit_ref_set(&s->service, u);
+
+        return unit_add_two_dependencies(UNIT(s), UNIT_BEFORE, UNIT_TRIGGERS, u, false);
 }
 
 static bool have_non_accept_socket(Socket *s) {
@@ -226,7 +216,7 @@ static int socket_verify(Socket *s) {
                 return -EINVAL;
         }
 
-        if (s->accept && s->service) {
+        if (s->accept && UNIT_DEREF(s->service)) {
                 log_error("Explicit service configuration for accepting sockets not supported on %s. Refusing.", s->meta.id);
                 return -EINVAL;
         }
@@ -278,7 +268,7 @@ int socket_add_one_mount_link(Socket *s, Mount *m) {
 }
 
 static int socket_add_mount_links(Socket *s) {
-        Meta *other;
+        Unit *other;
         int r;
 
         assert(s);
@@ -339,21 +329,28 @@ static int socket_load(Unit *u) {
         int r;
 
         assert(u);
-        assert(u->meta.load_state == UNIT_STUB);
+        assert(u->load_state == UNIT_STUB);
 
         if ((r = unit_load_fragment_and_dropin(u)) < 0)
                 return r;
 
         /* This is a new unit? Then let's add in some extras */
-        if (u->meta.load_state == UNIT_LOADED) {
+        if (u->load_state == UNIT_LOADED) {
 
                 if (have_non_accept_socket(s)) {
 
-                        if (!s->service)
-                                if ((r = unit_load_related_unit(u, ".service", (Unit**) &s->service)) < 0)
+                        if (!UNIT_DEREF(s->service)) {
+                                Unit *x;
+
+                                r = unit_load_related_unit(u, ".service", &x);
+                                if (r < 0)
                                         return r;
 
-                        if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(s->service), true)) < 0)
+                                unit_ref_set(&s->service, x);
+                        }
+
+                        r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(s->service), true);
+                        if (r < 0)
                                 return r;
                 }
 
@@ -912,8 +909,9 @@ static int socket_open_fds(Socket *s) {
                                 if ((r = socket_instantiate_service(s)) < 0)
                                         return r;
 
-                                if (s->service && s->service->exec_command[SERVICE_EXEC_START]) {
-                                        r = label_get_create_label_from_exe(s->service->exec_command[SERVICE_EXEC_START]->path, &label);
+                                if (UNIT_DEREF(s->service) &&
+                                    SERVICE(UNIT_DEREF(s->service))->exec_command[SERVICE_EXEC_START]) {
+                                        r = label_get_create_label_from_exe(SERVICE(UNIT_DEREF(s->service))->exec_command[SERVICE_EXEC_START]->path, &label);
 
                                         if (r < 0) {
                                                 if (r != -EPERM)
@@ -1380,26 +1378,20 @@ static void socket_enter_running(Socket *s, int cfd) {
         }
 
         if (cfd < 0) {
+                Iterator i;
+                Unit *u;
                 bool pending = false;
-                Meta *i;
 
                 /* If there's already a start pending don't bother to
                  * do anything */
-                LIST_FOREACH(units_by_type, i, s->meta.manager->units_by_type[UNIT_SERVICE]) {
-                        Service *service = (Service *) i;
-
-                        if (!set_get(service->configured_sockets, s))
-                                continue;
-
-                        if (!unit_pending_active(UNIT(service)))
-                                continue;
-
-                        pending = true;
-                        break;
-                }
+                SET_FOREACH(u, s->meta.dependencies[UNIT_TRIGGERS], i)
+                        if (unit_pending_active(u)) {
+                                pending = true;
+                                break;
+                        }
 
                 if (!pending)
-                        if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s->service), JOB_REPLACE, true, &error, NULL)) < 0)
+                        if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT_DEREF(s->service), JOB_REPLACE, true, &error, NULL)) < 0)
                                 goto fail;
 
                 socket_set_state(s, SOCKET_RUNNING);
@@ -1434,13 +1426,13 @@ static void socket_enter_running(Socket *s, int cfd) {
                         goto fail;
                 }
 
-                if ((r = unit_add_name(UNIT(s->service), name)) < 0) {
+                if ((r = unit_add_name(UNIT_DEREF(s->service), name)) < 0) {
                         free(name);
                         goto fail;
                 }
 
-                service = s->service;
-                s->service = NULL;
+                service = SERVICE(UNIT_DEREF(s->service));
+                unit_ref_unset(&s->service);
                 s->n_accepted ++;
 
                 service->meta.no_gc = false;
@@ -1523,23 +1515,27 @@ static int socket_start(Unit *u) {
                 return 0;
 
         /* Cannot run this without the service being around */
-        if (s->service) {
-                if (s->service->meta.load_state != UNIT_LOADED) {
-                        log_error("Socket service %s not loaded, refusing.", s->service->meta.id);
+        if (UNIT_DEREF(s->service)) {
+                Service *service;
+
+                service = SERVICE(UNIT_DEREF(s->service));
+
+                if (service->meta.load_state != UNIT_LOADED) {
+                        log_error("Socket service %s not loaded, refusing.", service->meta.id);
                         return -ENOENT;
                 }
 
                 /* If the service is already active we cannot start the
                  * socket */
-                if (s->service->state != SERVICE_DEAD &&
-                    s->service->state != SERVICE_FAILED &&
-                    s->service->state != SERVICE_AUTO_RESTART) {
-                        log_error("Socket service %s already active, refusing.", s->service->meta.id);
+                if (service->state != SERVICE_DEAD &&
+                    service->state != SERVICE_FAILED &&
+                    service->state != SERVICE_AUTO_RESTART) {
+                        log_error("Socket service %s already active, refusing.", service->meta.id);
                         return -EBUSY;
                 }
 
 #ifdef HAVE_SYSV_COMPAT
-                if (s->service->sysv_path) {
+                if (service->sysv_path) {
                         log_error("Using SysV services for socket activation is not supported. Refusing.");
                         return -ENOENT;
                 }
@@ -1791,14 +1787,14 @@ static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
         if (s->state != SOCKET_LISTENING)
                 return;
 
-        log_debug("Incoming traffic on %s", u->meta.id);
+        log_debug("Incoming traffic on %s", u->id);
 
         if (events != EPOLLIN) {
 
                 if (events & EPOLLHUP)
-                        log_error("%s: Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.", u->meta.id);
+                        log_error("%s: Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.", u->id);
                 else
-                        log_error("%s: Got unexpected poll event (0x%x) on socket.", u->meta.id, events);
+                        log_error("%s: Got unexpected poll event (0x%x) on socket.", u->id, events);
 
                 goto fail;
         }
@@ -1850,11 +1846,11 @@ static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
         }
 
         log_full(success ? LOG_DEBUG : LOG_NOTICE,
-                 "%s control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
+                 "%s control process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status);
         s->failure = s->failure || !success;
 
         if (s->control_command && s->control_command->command_next && success) {
-                log_debug("%s running next command for state %s", u->meta.id, socket_state_to_string(s->state));
+                log_debug("%s running next command for state %s", u->id, socket_state_to_string(s->state));
                 socket_run_next(s, success);
         } else {
                 s->control_command = NULL;
@@ -1863,7 +1859,7 @@ static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
                 /* No further commands for this step, so let's figure
                  * out what to do next */
 
-                log_debug("%s got final SIGCHLD for state %s", u->meta.id, socket_state_to_string(s->state));
+                log_debug("%s got final SIGCHLD for state %s", u->id, socket_state_to_string(s->state));
 
                 switch (s->state) {
 
@@ -1912,52 +1908,52 @@ static void socket_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
         switch (s->state) {
 
         case SOCKET_START_PRE:
-                log_warning("%s starting timed out. Terminating.", u->meta.id);
+                log_warning("%s starting timed out. Terminating.", u->id);
                 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
                 break;
 
         case SOCKET_START_POST:
-                log_warning("%s starting timed out. Stopping.", u->meta.id);
+                log_warning("%s starting timed out. Stopping.", u->id);
                 socket_enter_stop_pre(s, false);
                 break;
 
         case SOCKET_STOP_PRE:
-                log_warning("%s stopping timed out. Terminating.", u->meta.id);
+                log_warning("%s stopping timed out. Terminating.", u->id);
                 socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, false);
                 break;
 
         case SOCKET_STOP_PRE_SIGTERM:
                 if (s->exec_context.send_sigkill) {
-                        log_warning("%s stopping timed out. Killing.", u->meta.id);
+                        log_warning("%s stopping timed out. Killing.", u->id);
                         socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, false);
                 } else {
-                        log_warning("%s stopping timed out. Skipping SIGKILL. Ignoring.", u->meta.id);
+                        log_warning("%s stopping timed out. Skipping SIGKILL. Ignoring.", u->id);
                         socket_enter_stop_post(s, false);
                 }
                 break;
 
         case SOCKET_STOP_PRE_SIGKILL:
-                log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
+                log_warning("%s still around after SIGKILL. Ignoring.", u->id);
                 socket_enter_stop_post(s, false);
                 break;
 
         case SOCKET_STOP_POST:
-                log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
+                log_warning("%s stopping timed out (2). Terminating.", u->id);
                 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
                 break;
 
         case SOCKET_FINAL_SIGTERM:
                 if (s->exec_context.send_sigkill) {
-                        log_warning("%s stopping timed out (2). Killing.", u->meta.id);
+                        log_warning("%s stopping timed out (2). Killing.", u->id);
                         socket_enter_signal(s, SOCKET_FINAL_SIGKILL, false);
                 } else {
-                        log_warning("%s stopping timed out (2). Skipping SIGKILL. Ignoring.", u->meta.id);
+                        log_warning("%s stopping timed out (2). Skipping SIGKILL. Ignoring.", u->id);
                         socket_enter_dead(s, false);
                 }
                 break;
 
         case SOCKET_FINAL_SIGKILL:
-                log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id);
+                log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->id);
                 socket_enter_dead(s, false);
                 break;
 
@@ -2118,6 +2114,7 @@ DEFINE_STRING_TABLE_LOOKUP(socket_exec_command, SocketExecCommand);
 
 const UnitVTable socket_vtable = {
         .suffix = ".socket",
+        .object_size = sizeof(Socket),
         .sections =
                 "Unit\0"
                 "Socket\0"