monitor: Use the systemd Unix socket 82/281482/2 accepted/tizen/unified/20220920.110715
authorSung-hun Kim <sfoon.kim@samsung.com>
Mon, 19 Sep 2022 10:51:54 +0000 (19:51 +0900)
committerSeung-Woo Kim <sw0312.kim@samsung.com>
Tue, 20 Sep 2022 00:53:33 +0000 (00:53 +0000)
Systemd creates an Unix socket when the system is booted on
by using existing configuration files in /usr/lib/systemd.

Previously, the resource monitor always creates a new Unix
socket whether the socket exists or not. From now on, the
resource monitor checks that the Unix socket is existed on
the given path. If so, it uses the Unix socket instead of
open a newer one. By doing so, the permission of the Unix
socket can follow the given configuration denoted in the
Systemd socket configuration file.

Change-Id: I1eab46cd238340cc9700e1a2d52481270c06b8b1
Signed-off-by: Sung-hun Kim <sfoon.kim@samsung.com>
src/monitor/request-handler.c
systemd/pass-resource-monitor.socket.in

index f5a213840a0ccc566bde521269d6937ddecba6a2..3ffc258f0389064d615a7a858307d96f831a31f6 100644 (file)
@@ -39,6 +39,9 @@
 #include <netinet/in.h>
 #include <sys/time.h>
 #include <assert.h>
+#include <fcntl.h>
+
+#include <systemd/sd-daemon.h>
 
 #define PENDING_MAX 3
 #define REQUEST_SERVER_PORT 10001
@@ -1083,10 +1086,30 @@ static int init_unix_socket(int *sock, struct sockaddr_un *address, int *addrlen
 {
        const char *server_unix_socket_path = "/run/.pass-resource-monitor.socket";
        int opt = true;
+       int n = sd_listen_fds(0);
+       int fd;
+       int file_mode;
 
        if (!sock || !address || !addrlen)
                return -EINVAL;
 
+       bzero(address, sizeof(*address));
+       address->sun_family = AF_UNIX;
+       strncpy(address->sun_path, server_unix_socket_path, sizeof(address->sun_path));
+       address->sun_path[sizeof(address->sun_path) - 1] = '\0';
+
+       /* use the existing systemd unix socket */
+       if (n > 0) {
+               for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
+                       if (sd_is_socket_unix(fd, SOCK_STREAM, 1, server_unix_socket_path, 0) > 0) {
+                               *sock = fd;
+                               *addrlen = sizeof(*address);
+                               return 0;
+                       }
+               }
+       }
+
+       /* make a new unix socket */
        *sock = socket(AF_UNIX, SOCK_STREAM, 0);
        if (*sock < 0) {
                _E("Failed to initialize socket");
@@ -1099,11 +1122,6 @@ static int init_unix_socket(int *sock, struct sockaddr_un *address, int *addrlen
                goto error_out_close;
        }
 
-       bzero(address, sizeof(*address));
-       address->sun_family = AF_UNIX;
-       strncpy(address->sun_path, server_unix_socket_path, sizeof(address->sun_path));
-       address->sun_path[sizeof(address->sun_path) - 1] = '\0';
-
        if (!access(server_unix_socket_path, F_OK))
                unlink(server_unix_socket_path);
 
@@ -1112,6 +1130,12 @@ static int init_unix_socket(int *sock, struct sockaddr_un *address, int *addrlen
                goto error_out_close;
        }
 
+       file_mode = (S_IRWXU | S_IRWXG | S_IRWXO);
+       if (chmod(server_unix_socket_path, file_mode) < 0) {
+               _E("Failed to change the file mode of %s", server_unix_socket_path);
+               goto error_out_close;
+       }
+
        if (listen(*sock, PENDING_MAX) < 0) {
                _E("Failed to begin listenning");
                goto error_out_close;
@@ -1124,6 +1148,8 @@ static int init_unix_socket(int *sock, struct sockaddr_un *address, int *addrlen
 error_out_close:
        close(*sock);
 error_out:
+       bzero(address, sizeof(*address));
+
        return -EIO;
 }
 
index 88f021a95d44745ca86f1428deef6b21ef020e96..533a720c7f64802ff12e8e4bd92f395ea64daf7e 100644 (file)
@@ -8,3 +8,7 @@ ListenStream=/run/.pass-resource-monitor.socket
 SocketMode=0777
 SmackLabelIPIn=*
 SmackLabelIPOut=@
+Service=pass.service
+
+[Install]
+WantedBy=sockets.target