summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Christian Pohle2021-11-25 14:37:41 +0100
committerMax Christian Pohle2021-11-25 14:37:41 +0100
commit0ac71ac773d3740b380f3cb48ab970cf1fd364be (patch)
treef2444a3e5883c533cac38dec757546364154c973
parentdb864e290ba1ec4acd74371b150e7770585ef284 (diff)
downloadohmycgi-0ac71ac773d3740b380f3cb48ab970cf1fd364be.tar.bz2
ohmycgi-0ac71ac773d3740b380f3cb48ab970cf1fd364be.zip
Daemonize and allow the build of .debug and .release versions
-rw-r--r--.gitignore2
-rw-r--r--Jenkinsfile3
-rw-r--r--Makefile6
-rw-r--r--main.c18
-rw-r--r--main.h3
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 @@
1main 1main
2main.debug
3main.release
2 4
diff --git a/Jenkinsfile b/Jenkinsfile
index f452d6b..62f63cb 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -9,7 +9,8 @@ pipeline {
9 } 9 }
10 stage('Build') { 10 stage('Build') {
11 steps { 11 steps {
12 sh 'make main' 12 sh 'make main BUILD=release'
13 sh 'make main BUILD=debug'
13 } 14 }
14 } 15 }
15 } 16 }
diff --git a/Makefile b/Makefile
index 2458ed9..14c362c 100644
--- a/Makefile
+++ b/Makefile
@@ -12,13 +12,13 @@ CFLAGS += -I/usr/local/include
12FILES := cgi.c http_parser.c 12FILES := cgi.c http_parser.c
13 13
14test: main 14test: main
15 ./main 15 ./main.${BUILD}
16 16
17main: clean 17main: clean
18 $(CC) $(CFLAGS) -o $@ $(FILES) main.c $(LIBS) 18 $(CC) $(CFLAGS) -o $@.${BUILD} $(FILES) main.c $(LIBS)
19 19
20clean: 20clean:
21 rm -f ./main 21 rm -f ./main.${BUILD}
22 22
23# %: 23# %:
24# $(CC) $(CFLAGS) -o $@ $(FILES) main.c $(LIBS) 24# $(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) {
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)
diff --git a/main.h b/main.h
index 6588a40..af156f6 100644
--- a/main.h
+++ b/main.h
@@ -48,6 +48,7 @@
48#include <arpa/inet.h> 48#include <arpa/inet.h>
49#include <fcntl.h> 49#include <fcntl.h>
50#include <sys/stat.h> 50#include <sys/stat.h>
51#include <syslog.h>
51// #include <pthread.h> // maybe later 52// #include <pthread.h> // maybe later
52// }}} 53// }}}
53 54
@@ -83,4 +84,4 @@ void next_part(Http_Header * http_header, const char * content, size_t content_s
83void send_answer(Http_Header * http_header, int fd_socket); 84void send_answer(Http_Header * http_header, int fd_socket);
84void parse_http(size_t new_socket, char * request, size_t request_length); 85void parse_http(size_t new_socket, char * request, size_t request_length);
85 86
86// modeline for vim: shiftwidth=2 tabstop=2 number foldmethod=marker 87// modeline for vim: shiftwidth=2 tabstop=2 number foldmethod=marker foldenable
..