]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: honor FileDescriptorName= too for Accept=yes sockets
authorMike Yuan <me@yhndnzj.com>
Sun, 25 Aug 2024 21:21:47 +0000 (23:21 +0200)
committerMike Yuan <me@yhndnzj.com>
Mon, 26 Aug 2024 13:40:15 +0000 (15:40 +0200)
So far we manually hardcoded $LISTEN_FDNAMES to "varlink" in various
varlink service units we ship, even though FileDescriptorName=varlink
is specified in associated socket units already, because
FileDescriptorName= is currently silently ignored when combined with
Accept=yes. Let's step away from this, which seems saner.

Note that this is technically a compat break, but a mostly negligible
one as there shall be few users setting FileDescriptorName= but
still expecting LISTEN_FDNAMES=connection in the actual executable.

Preparation for #34080

NEWS
man/systemd.socket.xml
src/core/service.c
src/core/socket.c
src/core/socket.h

diff --git a/NEWS b/NEWS
index 947b831001d13ebf58962ea7237aca24e0e2e5cc..3bc3c721217a855c0d303daad0c62ab67dd370e1 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -31,6 +31,10 @@ CHANGES WITH 257 in spe:
           by default when combined with --scope, will be changed in a future
           release to be enabled by default.
 
+        * The FileDescriptorName= setting for socket units is now honored by
+          Accept=yes sockets too, where it was previously silently ignored and
+          "connection" was used unconditionally.
+
         * systemd-logind now always obeys inhibitor locks, where previously it
           ignored locks taken by the caller or when the caller was root. A
           privileged caller can always close the other sessions, remove the
index 67384bfcc4df1a5b3eb596ee36bcfb7fcdfa1f09..e9fd39bf7f2485156523209810065ae77d7d909b 100644 (file)
 
       <varlistentry>
         <term><varname>FileDescriptorName=</varname></term>
-        <listitem><para>Assigns a name to all file descriptors this
-        socket unit encapsulates. This is useful to help activated
-        services identify specific file descriptors, if multiple fds
-        are passed. Services may use the
+        <listitem><para>Assigns a name to all file descriptors this socket unit encapsulates.
+        This is useful to help activated services identify specific file descriptors, if multiple fds are passed.
+        Services may use the
         <citerefentry><refentrytitle>sd_listen_fds_with_names</refentrytitle><manvolnum>3</manvolnum></citerefentry>
-        call to acquire the names configured for the received file
-        descriptors. Names may contain any ASCII character, but must
-        exclude control characters and <literal>:</literal>, and must
-        be at most 255 characters in length. If this setting is not
-        used, the file descriptor name defaults to the name of the
-        socket unit, including its <filename>.socket</filename>
-        suffix.</para>
+        call to acquire the names configured for the received file descriptors. Names may contain any ASCII character,
+        but must exclude control characters and <literal>:</literal>, and must be at most 255 characters in length.
+        If this setting is not used, the file descriptor name defaults to the name of the socket unit
+        (including its <filename>.socket</filename> suffix) when <varname>Accept=no</varname>,
+        <literal>connection</literal> otherwise.</para>
 
         <xi:include href="version-info.xml" xpointer="v227"/></listitem>
       </varlistentry>
index eda355ad9edb0d9271bf4cd418c79847e4ef06bc..6d3118b96393a73332ca6b66a359b873cf57b7d8 100644 (file)
@@ -1426,6 +1426,7 @@ static int service_collect_fds(
         assert(n_storage_fds);
 
         if (s->socket_fd >= 0) {
+                Socket *sock = ASSERT_PTR(SOCKET(UNIT_DEREF(s->accept_socket)));
 
                 /* Pass the per-connection socket */
 
@@ -1433,7 +1434,7 @@ static int service_collect_fds(
                 if (!rfds)
                         return -ENOMEM;
 
-                rfd_names = strv_new("connection");
+                rfd_names = strv_new(socket_fdname(sock));
                 if (!rfd_names)
                         return -ENOMEM;
 
index 333079277b3670f3bd07c63aaae897f82db5e89a..a1553bcc68bf422394f3826d475ad834e7c047b7 100644 (file)
@@ -2393,10 +2393,9 @@ static void socket_enter_running(Socket *s, int cfd_in) {
                 s->n_accepted++;
 
                 r = service_set_socket_fd(SERVICE(service), cfd, s, p, s->selinux_context_from_net);
+                if (ERRNO_IS_NEG_DISCONNECT(r))
+                        return;
                 if (r < 0) {
-                        if (ERRNO_IS_DISCONNECT(r))
-                                return;
-
                         log_unit_warning_errno(UNIT(s), r, "Failed to set socket on service: %m");
                         goto fail;
                 }
@@ -3419,17 +3418,22 @@ static int socket_get_timeout(Unit *u, usec_t *timeout) {
         return 1;
 }
 
-char* socket_fdname(Socket *s) {
+const char* socket_fdname(Socket *s) {
         assert(s);
 
-        /* Returns the name to use for $LISTEN_NAMES. If the user
-         * didn't specify anything specifically, use the socket unit's
-         * name as fallback. */
+        /* Returns the name to use for $LISTEN_FDNAMES. If the user didn't specify anything specifically,
+         * use the socket unit's name as fallback for Accept=no sockets, "connection" otherwise. */
+
+        if (s->fdname)
+                return s->fdname;
+
+        if (s->accept)
+                return "connection";
 
-        return s->fdname ?: UNIT(s)->id;
+        return UNIT(s)->id;
 }
 
-static PidRef *socket_control_pid(Unit *u) {
+static PidRefsocket_control_pid(Unit *u) {
         return &ASSERT_PTR(SOCKET(u))->control_pid;
 }
 
index d4b39d559d81b970863e4d904a87a83a1e0c3365..f21fc7f54469d29d66d8ae9517429279e6d976fb 100644 (file)
@@ -185,7 +185,7 @@ int socket_port_to_address(const SocketPort *s, char **ret);
 
 int socket_load_service_unit(Socket *s, int cfd, Unit **ret);
 
-char* socket_fdname(Socket *s);
+const char* socket_fdname(Socket *s);
 
 extern const UnitVTable socket_vtable;