Added socket initialization
This commit is contained in:
parent
8faa8ca57c
commit
2d76debb4e
3 changed files with 85 additions and 0 deletions
52
src/kiwmi/ipc.c
Normal file
52
src/kiwmi/ipc.c
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
/* Copyright (c), Niclas Meyer <niclas@countingsort.com>
|
||||||
|
*
|
||||||
|
* 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 <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#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");
|
||||||
|
}
|
||||||
|
}
|
15
src/kiwmi/ipc.h
Normal file
15
src/kiwmi/ipc.h
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
/* Copyright (c), Niclas Meyer <niclas@countingsort.com>
|
||||||
|
*
|
||||||
|
* 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 */
|
|
@ -5,10 +5,12 @@
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include <sys/select.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include "ipc.h"
|
||||||
|
|
||||||
static void exec_config(const char *path);
|
static void exec_config(const char *path);
|
||||||
static void sig_handler(int sig);
|
static void sig_handler(int sig);
|
||||||
|
@ -68,7 +70,23 @@ main(int argc, char *argv[])
|
||||||
signal(SIGCHLD, sig_handler);
|
signal(SIGCHLD, sig_handler);
|
||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
||||||
|
|
||||||
|
init_socket();
|
||||||
|
|
||||||
exec_config(config_path);
|
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
|
static void
|
||||||
|
|
Loading…
Add table
Reference in a new issue