From 0ac71ac773d3740b380f3cb48ab970cf1fd364be Mon Sep 17 00:00:00 2001 From: Max Christian Pohle Date: Thu, 25 Nov 2021 14:37:41 +0100 Subject: Daemonize and allow the build of .debug and .release versions --- .gitignore | 2 ++ Jenkinsfile | 3 ++- Makefile | 6 +++--- main.c | 18 ++++++++++++++++++ main.h | 3 ++- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 1a94556..53e9de3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ main +main.debug +main.release diff --git a/Jenkinsfile b/Jenkinsfile index f452d6b..62f63cb 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -9,7 +9,8 @@ pipeline { } stage('Build') { steps { - sh 'make main' + sh 'make main BUILD=release' + sh 'make main BUILD=debug' } } } diff --git a/Makefile b/Makefile index 2458ed9..14c362c 100644 --- a/Makefile +++ b/Makefile @@ -12,13 +12,13 @@ CFLAGS += -I/usr/local/include FILES := cgi.c http_parser.c test: main - ./main + ./main.${BUILD} main: clean - $(CC) $(CFLAGS) -o $@ $(FILES) main.c $(LIBS) + $(CC) $(CFLAGS) -o $@.${BUILD} $(FILES) main.c $(LIBS) clean: - rm -f ./main + rm -f ./main.${BUILD} # %: # $(CC) $(CFLAGS) -o $@ $(FILES) main.c $(LIBS) 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) { return NULL; } + +int become_daemon(const char * name) { + if(fork()) { + exit(EXIT_SUCCESS); + } else { + setsid(); + if(fork()) { + exit(EXIT_SUCCESS); + } else { + syslog(0, "daemon '%s' is running with pid %d", name, getpid()); + return EXIT_SUCCESS; + } + } +} + + static int serve(int server_fd) { struct sockaddr_in address; @@ -127,6 +143,8 @@ int main(const int argc, char const * argv[]) { ? err(errno, NULL) : listen(server_fd, SOMAXCONN) ? err(errno, NULL) + : become_daemon(argv[0]) + ? err(errno, NULL) : serve(server_fd) ? err(errno, NULL) : exit(EXIT_SUCCESS) diff --git a/main.h b/main.h index 6588a40..af156f6 100644 --- a/main.h +++ b/main.h @@ -48,6 +48,7 @@ #include #include #include +#include // #include // maybe later // }}} @@ -83,4 +84,4 @@ void next_part(Http_Header * http_header, const char * content, size_t content_s void send_answer(Http_Header * http_header, int fd_socket); void parse_http(size_t new_socket, char * request, size_t request_length); -// modeline for vim: shiftwidth=2 tabstop=2 number foldmethod=marker +// modeline for vim: shiftwidth=2 tabstop=2 number foldmethod=marker foldenable -- cgit v1.2.3