]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/basic/socket-label.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / basic / socket-label.c
index 4099ea6f9ff6254f5ea2b6565c0dc7988d822a6f..20be406371ca686b7fcfd9249f03bff09baa0cf0 100644 (file)
@@ -1,5 +1,4 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
 ***/
 
 #include <errno.h>
+#include <netinet/in.h>
+#include <stdbool.h>
 #include <stddef.h>
 #include <string.h>
-#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/un.h>
 #include <unistd.h>
 
+#include "alloc-util.h"
 #include "fd-util.h"
+#include "log.h"
 #include "macro.h"
 #include "missing.h"
 #include "mkdir.h"
 #include "selinux-util.h"
 #include "socket-util.h"
-#include "util.h"
+#include "umask-util.h"
 
 int socket_address_listen(
                 const SocketAddress *a,
@@ -80,7 +84,7 @@ int socket_address_listen(
                         return -errno;
         }
 
-        if (socket_address_family(a) == AF_INET || socket_address_family(a) == AF_INET6) {
+        if (IN_SET(socket_address_family(a), AF_INET, AF_INET6)) {
                 if (bind_to_device)
                         if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0)
                                 return -errno;
@@ -109,28 +113,24 @@ int socket_address_listen(
                 return -errno;
 
         if (socket_address_family(a) == AF_UNIX && a->sockaddr.un.sun_path[0] != 0) {
-                mode_t old_mask;
-
                 /* Create parents */
-                mkdir_parents_label(a->sockaddr.un.sun_path, directory_mode);
+                (void) mkdir_parents_label(a->sockaddr.un.sun_path, directory_mode);
 
                 /* Enforce the right access mode for the socket */
-                old_mask = umask(~ socket_mode);
-
-                r = mac_selinux_bind(fd, &a->sockaddr.sa, a->size);
-
-                if (r < 0 && errno == EADDRINUSE) {
-                        /* Unlink and try again */
-                        unlink(a->sockaddr.un.sun_path);
-                        r = bind(fd, &a->sockaddr.sa, a->size);
+                RUN_WITH_UMASK(~socket_mode) {
+                        r = mac_selinux_bind(fd, &a->sockaddr.sa, a->size);
+                        if (r == -EADDRINUSE) {
+                                /* Unlink and try again */
+                                unlink(a->sockaddr.un.sun_path);
+                                if (bind(fd, &a->sockaddr.sa, a->size) < 0)
+                                        return -errno;
+                        } else if (r < 0)
+                                return r;
                 }
-
-                umask(old_mask);
-        } else
-                r = bind(fd, &a->sockaddr.sa, a->size);
-
-        if (r < 0)
-                return -errno;
+        } else {
+                if (bind(fd, &a->sockaddr.sa, a->size) < 0)
+                        return -errno;
+        }
 
         if (socket_address_can_accept(a))
                 if (listen(fd, backlog) < 0)
@@ -142,7 +142,7 @@ int socket_address_listen(
         return r;
 }
 
-int make_socket_fd(int log_level, const char* address, int flags) {
+int make_socket_fd(int log_level, const char* address, int type, int flags) {
         SocketAddress a;
         int fd, r;
 
@@ -150,7 +150,9 @@ int make_socket_fd(int log_level, const char* address, int flags) {
         if (r < 0)
                 return log_error_errno(r, "Failed to parse socket address \"%s\": %m", address);
 
-        fd = socket_address_listen(&a, flags, SOMAXCONN, SOCKET_ADDRESS_DEFAULT,
+        a.type = type;
+
+        fd = socket_address_listen(&a, type | flags, SOMAXCONN, SOCKET_ADDRESS_DEFAULT,
                                    NULL, false, false, false, 0755, 0644, NULL);
         if (fd < 0 || log_get_max_level() >= log_level) {
                 _cleanup_free_ char *p = NULL;