#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
{
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");
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);
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;
error_out_close:
close(*sock);
error_out:
+ bzero(address, sizeof(*address));
+
return -EIO;
}