From: Sung-hun Kim Date: Mon, 19 Sep 2022 10:51:54 +0000 (+0900) Subject: monitor: Use the systemd Unix socket X-Git-Tag: accepted/tizen/unified/20220920.110715^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5641d5d99c307ab05eb93a2697f025866754e6ce;p=platform%2Fcore%2Fsystem%2Fpass.git monitor: Use the systemd Unix socket 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 --- diff --git a/src/monitor/request-handler.c b/src/monitor/request-handler.c index f5a2138..3ffc258 100644 --- a/src/monitor/request-handler.c +++ b/src/monitor/request-handler.c @@ -39,6 +39,9 @@ #include #include #include +#include + +#include #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; } diff --git a/systemd/pass-resource-monitor.socket.in b/systemd/pass-resource-monitor.socket.in index 88f021a..533a720 100644 --- a/systemd/pass-resource-monitor.socket.in +++ b/systemd/pass-resource-monitor.socket.in @@ -8,3 +8,7 @@ ListenStream=/run/.pass-resource-monitor.socket SocketMode=0777 SmackLabelIPIn=* SmackLabelIPOut=@ +Service=pass.service + +[Install] +WantedBy=sockets.target