From 3b00c7be0e8834402ed93eb42b3a93302076c5ff Mon Sep 17 00:00:00 2001 From: Mikhail Osipov Date: Fri, 18 Oct 2019 17:43:38 +0300 Subject: skel --- daemon.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 daemon.c (limited to 'daemon.c') diff --git a/daemon.c b/daemon.c new file mode 100644 index 0000000..305755d --- /dev/null +++ b/daemon.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include +#include + +#include "tunnel.h" +#include "macro.h" + +static void sighandler(int signo __unused) +{ + unlink(TUNNEL_SOCK_PATH); + exit(EXIT_FAILURE); +} + +static void tunnel_daemon_loop(int sock) +{ + for (;;) { + int fd = accept(sock, NULL, NULL); + if (fd < 0) { + warn("accept"); + continue; + } + + FILE *fp = fdopen(fd, "w"); + if (fp) { + fprintf(fp, "hello from %d", getpid()); + fclose(fp); + } + } +} + +int tunnel_daemon(int sock) +{ + switch (fork()) { + case -1: + warn("fork"); + return -1; + case 0: + break; + default: + return 0; + } + + if (setsid() < 0) + err(EXIT_FAILURE, "setsid"); + + chdir("/"); + + struct sigaction sa = { + .sa_handler = sighandler + }; + + sigaction(SIGTERM, &sa, NULL); + sigaction(SIGINT, &sa, NULL); + +#if 0 + int fd = open("/dev/null", O_RDWR); + if (fd < 0) + err(EXIT_FAILURE, "open /dev/null"); + + dup2(fd, STDIN_FILENO); + dup2(fd, STDOUT_FILENO); + dup2(fd, STDERR_FILENO); + + if (fd > 2) + close(fd); +#endif + + tunnel_daemon_loop(sock); + + return 0; +} -- cgit v1.2.3-70-g09d2