diff --git a/src/kiwmi/ipc.c b/src/kiwmi/ipc.c new file mode 100644 index 0000000..b5efbad --- /dev/null +++ b/src/kiwmi/ipc.c @@ -0,0 +1,52 @@ +/* Copyright (c), Niclas Meyer + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include "ipc.h" + +#include +#include +#include + +#include +#include +#include + +#include "common.h" + +int g_sock_fd; + +void +init_socket(void) +{ + struct sockaddr_un sock_addr; + + memset(&sock_addr, 0, sizeof(sock_addr)); + + char *sock_path = getenv(SOCK_ENV_VAR); + + if (sock_path) { + strncpy(sock_addr.sun_path, sock_path, sizeof(sock_addr.sun_path)); + } else { + strncpy(sock_addr.sun_path, SOCK_DEF_PATH, sizeof(sock_addr.sun_path)); + } + + sock_addr.sun_family = AF_UNIX; + + if ((g_sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { + die("failed to create socket\n"); + } + + unlink(sock_addr.sun_path); + + if (bind(g_sock_fd, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) < 0) { + die("failed to bind socket\n"); + } + + if (listen(g_sock_fd, 1) < 0) { + die("failed to listen to socket\n"); + } +} diff --git a/src/kiwmi/ipc.h b/src/kiwmi/ipc.h new file mode 100644 index 0000000..1b27fc5 --- /dev/null +++ b/src/kiwmi/ipc.h @@ -0,0 +1,15 @@ +/* Copyright (c), Niclas Meyer + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#ifndef IPC_H +#define IPC_H + +void init_socket(void); + +extern int g_sock_fd; + +#endif /* IPC_H */ diff --git a/src/kiwmi/main.c b/src/kiwmi/main.c index 9020111..422056c 100644 --- a/src/kiwmi/main.c +++ b/src/kiwmi/main.c @@ -5,10 +5,12 @@ #include #include #include +#include #include #include #include "common.h" +#include "ipc.h" static void exec_config(const char *path); static void sig_handler(int sig); @@ -68,7 +70,23 @@ main(int argc, char *argv[]) signal(SIGCHLD, sig_handler); signal(SIGPIPE, SIG_IGN); + init_socket(); + exec_config(config_path); + + int max_fd = g_sock_fd + 1; + fd_set file_descriptors; + + while (g_is_about_to_quit) { + FD_ZERO(&file_descriptors); + FD_SET(g_sock_fd, &file_descriptors); + + select(max_fd, &file_descriptors, NULL, NULL, NULL); + + if (FD_ISSET(g_sock_fd, &file_descriptors)) { + // TODO: handle client event + } + } } static void