add signal management
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 9 May 2023 15:47:26 +0000 (17:47 +0200)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 9 May 2023 15:47:26 +0000 (17:47 +0200)
server.c
server.h
signal.c [new file with mode: 0644]
signal.h [new file with mode: 0644]
webserver.c

index 9a4d83ee74900941dc339431c3c2d294369d3e28..711f472072027be472342aa4f51b78beba70a1d9 100644 (file)
--- a/server.c
+++ b/server.c
@@ -32,7 +32,6 @@ typedef SOCKET socket_t;
 typedef int socket_t;
 #define closesocket close
 #define ERRNO errno
-#define INVALID_SOCKET -1
 #define SOCKET_ERROR -1
 #endif
 
@@ -108,3 +107,5 @@ void close_listening_socket (socket_t sock)
 }
 
 /* vim: set ts=4 sw=4 et: */
+
+/* vim: set ts=4 sw=4 et: */
index 962cb00bea18e14af532e6e295db60d1add4e2e1..0738e5a41f9832f96a861c93d3510effeb443437 100644 (file)
--- a/server.h
+++ b/server.h
@@ -6,6 +6,7 @@
 typedef SOCKET socket_t;
 #else /* Posix */
 typedef int socket_t;
+#define INVALID_SOCKET -1
 #endif
 
 socket_t open_listening_socket (int port);
@@ -14,3 +15,5 @@ void close_listening_socket (socket_t sock);
 #endif /* __SERVER_H__ */
 
 /* vim: set ts=4 sw=4 et: */
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/signal.c b/signal.c
new file mode 100644 (file)
index 0000000..bdeb7a9
--- /dev/null
+++ b/signal.c
@@ -0,0 +1,21 @@
+#include <signal.h>
+#include <stdio.h>
+
+#include "signal.h"
+
+void _signal_handler (int sig) {
+    printf ("signal: %d\n", sig);
+}
+
+void (*sigh_hangup) (int) = &_signal_handler;
+void (*sigh_terminate) (int) = &_signal_handler;
+
+void __attribute__((constructor)) init_signal (void)
+{
+#ifdef _POSIX
+    signal (SIGHUP, sigh_hangup);
+#endif /* _POSIX */
+    signal (SIGTERM, sigh_terminate);
+}
+
+/* vi:set tabstop=4 expandtab shiftwidth=4: this line set vi mode */
diff --git a/signal.h b/signal.h
new file mode 100644 (file)
index 0000000..f4f3309
--- /dev/null
+++ b/signal.h
@@ -0,0 +1,7 @@
+#ifndef __SIGNAL_H__
+#define __SIGNAL_H__
+
+extern void (*sigh_hangup) (int);
+extern void (*sigh_terminate) (int);
+
+#endif /* __SIGNAL_H__ */
index 0673b9cf2190329426c4c3b465d1ed84c8b3820e..825b847f728e9f3aec82dc5db4e352559404ee3e 100644 (file)
@@ -1,6 +1,6 @@
 /* depend: */
 /* cflags: */
-/* linker: color.o debug.o server.o */
+/* linker: color.o debug.o server.o signal.o */
 
 #include <assert.h>
 #include <stdio.h>
@@ -8,6 +8,7 @@
 
 #include "debug.h"
 #include "server.h"
+#include "signal.h"
 
 /* types */
 
@@ -21,6 +22,7 @@
 
 char *progname = NULL;
 int port = 8080;
+socket_t sock = INVALID_SOCKET;
 
 /* help function */
 
@@ -35,12 +37,19 @@ int usage (int ret)
     return ret;
 }
 
+void stop_server (__attribute__((unused)) int sig)
+{
+    if (sock != INVALID_SOCKET) {
+        close_listening_socket (sock);
+        exit (0);
+    }
+}
+
 /* main function */
 
 int main (int argc, char *argv[])
 {
     int i = 0;
-    int ret = 0;
 
     /* program name */
 
@@ -91,19 +100,19 @@ int main (int argc, char *argv[])
     }
 
     VERBOSE (DEBUG, fprintf (stdout, "Initializing socket\n"));
-    socket_t sock = open_listening_socket (port);
-    if (sock == (socket_t)-1) {
+    sock = open_listening_socket (port);
+    if (sock == INVALID_SOCKET) {
         VERBOSE (ERROR, fprintf (stderr, "Can't open listening socket\n"));
         return 1;
     }
 
     VERBOSE (INFO, fprintf (stdout, "Listening socket on port %d\n", port));
-    sleep (2);
+    sleep (20);
 
     VERBOSE (DEBUG, fprintf (stdout, "Closing socket\n"));
     close_listening_socket (sock);
 
-    return ret;
+    return 2;
 }
 
 // test: webserver.exe -h
@@ -114,7 +123,8 @@ int main (int argc, char *argv[])
 // test: webserver.exe -v 2>&1 | grep -q 'missing verbose level'
 // test: webserver.exe -p 2>&1 | grep -q 'missing port number'
 // test: webserver.exe -p -1 2>&1 | grep -q 'incorrect port number'
-// test: webserver.exe | grep -q 'Listening socket on port 8080'
-// test: webserver.exe -p 8000 | grep -q 'Listening socket on port 8000'
+// test: ( webserver.exe & pid=$!; ( sleep 1; kill -TERM $pid ) ) | grep -q 'Listening socket on port 8080'
+// test: ( webserver.exe -p 8008 & pid=$!; ( sleep 1; kill -TERM $pid ) ) | grep -q 'Listening socket on port 8008'
+// test: webserver.exe & pid=$!; sleep 1; kill -INT $pid; ps aux | grep -q [w]ebserver.exe && kill -TERM $pid || rc=1; test x$rc = x1
 
 /* vim: set ts=4 sw=4 et: */