summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/main.c b/main.c
index 8eb6f40..906be15 100644
--- a/main.c
+++ b/main.c
@@ -80,6 +80,22 @@ static void * answer_request(size_t new_socket) {
80 return NULL; 80 return NULL;
81} 81}
82 82
83
84int become_daemon(const char * name) {
85 if(fork()) {
86 exit(EXIT_SUCCESS);
87 } else {
88 setsid();
89 if(fork()) {
90 exit(EXIT_SUCCESS);
91 } else {
92 syslog(0, "daemon '%s' is running with pid %d", name, getpid());
93 return EXIT_SUCCESS;
94 }
95 }
96}
97
98
83static int serve(int server_fd) 99static int serve(int server_fd)
84{ 100{
85 struct sockaddr_in address; 101 struct sockaddr_in address;
@@ -127,6 +143,8 @@ int main(const int argc, char const * argv[]) {
127 ? err(errno, NULL) 143 ? err(errno, NULL)
128 : listen(server_fd, SOMAXCONN) 144 : listen(server_fd, SOMAXCONN)
129 ? err(errno, NULL) 145 ? err(errno, NULL)
146 : become_daemon(argv[0])
147 ? err(errno, NULL)
130 : serve(server_fd) 148 : serve(server_fd)
131 ? err(errno, NULL) 149 ? err(errno, NULL)
132 : exit(EXIT_SUCCESS) 150 : exit(EXIT_SUCCESS)
..