#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; }