]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
homed: use notify_socket_prepare()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 31 Mar 2025 16:32:03 +0000 (01:32 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 31 Mar 2025 18:58:34 +0000 (03:58 +0900)
This also make it use autobind notify socket.

src/home/homed-home.c
src/home/homed-manager.c
src/home/homed-manager.h

index 67854723510f260f4cfd030d25424f1beff7001b..0f970ac2d034cd9a033877398a44e5bd4ae349ba 100644 (file)
@@ -1308,21 +1308,9 @@ static int home_start_work(
         if (r < 0)
                 return r;
         if (r == 0) {
-                _cleanup_free_ char *joined = NULL;
-                const char *suffix, *unix_path;
-
                 /* Child */
 
-                suffix = getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
-                if (suffix) {
-                        joined = strjoin("/run/systemd/home/notify.", suffix);
-                        if (!joined)
-                                return log_oom();
-                        unix_path = joined;
-                } else
-                        unix_path = "/run/systemd/home/notify";
-
-                if (setenv("NOTIFY_SOCKET", unix_path, 1) < 0) {
+                if (setenv("NOTIFY_SOCKET", h->manager->notify_socket_path, /* overwrite = */ true) < 0) {
                         log_error_errno(errno, "Failed to set $NOTIFY_SOCKET: %m");
                         _exit(EXIT_FAILURE);
                 }
index 5a56b3de2378fbf787a99c1e29d8b6ec5616f20d..9723725e43910918adac3fc6c4c2bc75dfc6245d 100644 (file)
@@ -291,7 +291,7 @@ Manager* manager_free(Manager *m) {
         m->device_monitor = sd_device_monitor_unref(m->device_monitor);
 
         m->inotify_event_source = sd_event_source_unref(m->inotify_event_source);
-        m->notify_socket_event_source = sd_event_source_unref(m->notify_socket_event_source);
+        m->notify_socket_path = mfree(m->notify_socket_path);
         m->deferred_rescan_event_source = sd_event_source_unref(m->deferred_rescan_event_source);
         m->deferred_gc_event_source = sd_event_source_unref(m->deferred_gc_event_source);
         m->deferred_auto_login_event_source = sd_event_source_unref(m->deferred_auto_login_event_source);
@@ -1142,64 +1142,23 @@ static int on_notify_socket(sd_event_source *s, int fd, uint32_t revents, void *
 }
 
 static int manager_listen_notify(Manager *m) {
-        _cleanup_close_ int fd = -EBADF;
-        union sockaddr_union sa = {
-                .un.sun_family = AF_UNIX,
-                .un.sun_path = "/run/systemd/home/notify",
-        };
-        const char *suffix;
         int r;
 
         assert(m);
-        assert(!m->notify_socket_event_source);
-
-        suffix = getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
-        if (suffix) {
-                _cleanup_free_ char *unix_path = NULL;
-
-                unix_path = strjoin("/run/systemd/home/notify.", suffix);
-                if (!unix_path)
-                        return log_oom();
-                r = sockaddr_un_set_path(&sa.un, unix_path);
-                if (r < 0)
-                        return log_error_errno(r, "Socket path %s does not fit in sockaddr_un: %m", unix_path);
-        }
-
-        fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
-        if (fd < 0)
-                return log_error_errno(errno, "Failed to create listening socket: %m");
-
-        (void) mkdir_parents(sa.un.sun_path, 0755);
-        (void) sockaddr_un_unlink(&sa.un);
-
-        if (bind(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0)
-                return log_error_errno(errno, "Failed to bind to socket: %m");
+        assert(!m->notify_socket_path);
 
-        r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true);
+        r = notify_socket_prepare(
+                        m->event,
+                        SD_EVENT_PRIORITY_NORMAL - 5, /* Make sure we process sd_notify() before SIGCHLD for
+                                                       * any worker, so that we always know the error number
+                                                       * of a client before it exits. */
+                        on_notify_socket,
+                        m,
+                        &m->notify_socket_path);
         if (r < 0)
-                return log_error_errno(r, "Failed to enable SO_PASSCRED on notify socket: %m");
+                return log_error_errno(r, "Failed to prepare notify socket: %m");
 
-        r = setsockopt_int(fd, SOL_SOCKET, SO_PASSPIDFD, true);
-        if (r < 0)
-                log_warning_errno(r, "Failed to enable SO_PASSPIDFD on notify socket, ignoring: %m");
-
-        r = sd_event_add_io(m->event, &m->notify_socket_event_source, fd, EPOLLIN, on_notify_socket, m);
-        if (r < 0)
-                return log_error_errno(r, "Failed to allocate event source for notify socket: %m");
-
-        (void) sd_event_source_set_description(m->notify_socket_event_source, "notify-socket");
-
-        /* Make sure we process sd_notify() before SIGCHLD for any worker, so that we always know the error
-         * number of a client before it exits. */
-        r = sd_event_source_set_priority(m->notify_socket_event_source, SD_EVENT_PRIORITY_NORMAL - 5);
-        if (r < 0)
-                return log_error_errno(r, "Failed to alter priority of NOTIFY_SOCKET event source: %m");
-
-        r = sd_event_source_set_io_fd_own(m->notify_socket_event_source, true);
-        if (r < 0)
-                return log_error_errno(r, "Failed to pass ownership of notify socket: %m");
-
-        return TAKE_FD(fd);
+        return 0;
 }
 
 static int manager_add_device(Manager *m, sd_device *d) {
index b780094d0d8c4cae35591f0765f387253ec98c9e..2b4bd38adb38c98b85b64befdebc4367fc0b22b5 100644 (file)
@@ -42,8 +42,7 @@ struct Manager {
 
         sd_event_source *inotify_event_source;
 
-        /* An event source we receive sd_notify() messages from our worker from */
-        sd_event_source *notify_socket_event_source;
+        char *notify_socket_path;
 
         sd_device_monitor *device_monitor;