]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/socket: do not assign another fd to SocketPort which already has a fd on deseria...
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 8 Jun 2021 01:23:47 +0000 (10:23 +0900)
committerLennart Poettering <lennart@poettering.net>
Tue, 8 Jun 2021 16:23:47 +0000 (18:23 +0200)
Otherwise, if a socket address is duplicated, then the previous fd is
closed.

Fixes #19843.

src/core/socket.c

index 10777e4e8082e29febdf8c2eee7347173b9c9b8f..a64bf7360cd344d238c5396afe66cdb174ab4bc7 100644 (file)
@@ -2626,13 +2626,6 @@ static int socket_serialize(Unit *u, FILE *f, FDSet *fds) {
         return 0;
 }
 
-static void socket_port_take_fd(SocketPort *p, FDSet *fds, int fd) {
-        assert(p);
-
-        safe_close(p->fd);
-        p->fd = fdset_remove(fds, fd);
-}
-
 static int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
         Socket *s = SOCKET(u);
 
@@ -2694,13 +2687,20 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
 
                 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
                         log_unit_debug(u, "Failed to parse fifo value: %s", value);
-                else
+                else {
+                        bool found = false;
+
                         LIST_FOREACH(port, p, s->ports)
-                                if (p->type == SOCKET_FIFO &&
+                                if (p->fd < 0 &&
+                                    p->type == SOCKET_FIFO &&
                                     path_equal_or_files_same(p->path, value+skip, 0)) {
-                                        socket_port_take_fd(p, fds, fd);
+                                        p->fd = fdset_remove(fds, fd);
+                                        found = true;
                                         break;
                                 }
+                        if (!found)
+                                log_unit_debug(u, "No matching fifo socket found: %s", value+skip);
+                }
 
         } else if (streq(key, "special")) {
                 int fd, skip = 0;
@@ -2708,13 +2708,20 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
 
                 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
                         log_unit_debug(u, "Failed to parse special value: %s", value);
-                else
+                else {
+                        bool found = false;
+
                         LIST_FOREACH(port, p, s->ports)
-                                if (p->type == SOCKET_SPECIAL &&
+                                if (p->fd < 0 &&
+                                    p->type == SOCKET_SPECIAL &&
                                     path_equal_or_files_same(p->path, value+skip, 0)) {
-                                        socket_port_take_fd(p, fds, fd);
+                                        p->fd = fdset_remove(fds, fd);
+                                        found = true;
                                         break;
                                 }
+                        if (!found)
+                                log_unit_debug(u, "No matching special socket found: %s", value+skip);
+                }
 
         } else if (streq(key, "mqueue")) {
                 int fd, skip = 0;
@@ -2722,13 +2729,20 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
 
                 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
                         log_unit_debug(u, "Failed to parse mqueue value: %s", value);
-                else
+                else {
+                        bool found = false;
+
                         LIST_FOREACH(port, p, s->ports)
-                                if (p->type == SOCKET_MQUEUE &&
+                                if (p->fd < 0 &&
+                                    p->type == SOCKET_MQUEUE &&
                                     streq(p->path, value+skip)) {
-                                        socket_port_take_fd(p, fds, fd);
+                                        p->fd = fdset_remove(fds, fd);
+                                        found = true;
                                         break;
                                 }
+                        if (!found)
+                                log_unit_debug(u, "No matching mqueue socket found: %s", value+skip);
+                }
 
         } else if (streq(key, "socket")) {
                 int fd, type, skip = 0;
@@ -2736,12 +2750,20 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
 
                 if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd))
                         log_unit_debug(u, "Failed to parse socket value: %s", value);
-                else
+                else {
+                        bool found = false;
+
                         LIST_FOREACH(port, p, s->ports)
-                                if (socket_address_is(&p->address, value+skip, type)) {
-                                        socket_port_take_fd(p, fds, fd);
+                                if (p->fd < 0 &&
+                                    socket_address_is(&p->address, value+skip, type)) {
+                                        p->fd = fdset_remove(fds, fd);
+                                        found = true;
                                         break;
                                 }
+                        if (!found)
+                                log_unit_debug(u, "No matching %s socket found: %s",
+                                               socket_address_type_to_string(type), value+skip);
+                }
 
         } else if (streq(key, "netlink")) {
                 int fd, skip = 0;
@@ -2749,12 +2771,19 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
 
                 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
                         log_unit_debug(u, "Failed to parse socket value: %s", value);
-                else
+                else {
+                        bool found = false;
+
                         LIST_FOREACH(port, p, s->ports)
-                                if (socket_address_is_netlink(&p->address, value+skip)) {
-                                        socket_port_take_fd(p, fds, fd);
+                                if (p->fd < 0 &&
+                                    socket_address_is_netlink(&p->address, value+skip)) {
+                                        p->fd = fdset_remove(fds, fd);
+                                        found = true;
                                         break;
                                 }
+                        if (!found)
+                                log_unit_debug(u, "No matching netlink socket found: %s", value+skip);
+                }
 
         } else if (streq(key, "ffs")) {
                 int fd, skip = 0;
@@ -2762,13 +2791,20 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
 
                 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
                         log_unit_debug(u, "Failed to parse ffs value: %s", value);
-                else
+                else {
+                        bool found = false;
+
                         LIST_FOREACH(port, p, s->ports)
-                                if (p->type == SOCKET_USB_FUNCTION &&
+                                if (p->fd < 0 &&
+                                    p->type == SOCKET_USB_FUNCTION &&
                                     path_equal_or_files_same(p->path, value+skip, 0)) {
-                                        socket_port_take_fd(p, fds, fd);
+                                        p->fd = fdset_remove(fds, fd);
+                                        found = true;
                                         break;
                                 }
+                        if (!found)
+                                log_unit_debug(u, "No matching ffs socket found: %s", value+skip);
+                }
 
         } else
                 log_unit_debug(UNIT(s), "Unknown serialization key: %s", key);