aboutsummaryrefslogtreecommitdiff
path: root/examples/put
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-08-20 00:07:05 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-08-22 02:27:34 +0200
commit43e89fb23943b5ffb6854f290592c29cd079bf46 (patch)
tree3a295c16aebdcbea5ce733fe61ba8ba639bc4f41 /examples/put
parent34b62bd0c47c915a12ff1b81f52b123fc3eb4a69 (diff)
Move signal handling to processes
So far, libweb installed a signal handler so as to handle SIGTERM, SIGPIPE and SIGINT signals so that processes would not have to care about such details. However, it is not advisable for libraries to install signal handlers, as signals are handled on a per-process basis. The previous approach would be incompatible if several instances of the library were allocated by the same process. Unfortunately, this has the undesired side effect of adding the boilerplate code into the process.
Diffstat (limited to 'examples/put')
-rw-r--r--examples/put/main.c80
1 files changed, 74 insertions, 6 deletions
diff --git a/examples/put/main.c b/examples/put/main.c
index cf38248..3d6454d 100644
--- a/examples/put/main.c
+++ b/examples/put/main.c
@@ -1,10 +1,21 @@
+/* As of FreeBSD 13.2, sigaction(2) still conforms to IEEE Std
+ * 1003.1-1990 (POSIX.1), which did not define SA_RESTART.
+ * FreeBSD supports it as an extension, but then _POSIX_C_SOURCE must
+ * not be defined. */
+#ifndef __FreeBSD__
+#define _POSIX_C_SOURCE 200809L
+#endif
+
#include <dynstr.h>
#include <libweb/handler.h>
#include <libweb/html.h>
#include <libweb/http.h>
+#include <errno.h>
+#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
static int on_put(const struct http_payload *const pl,
struct http_response *const r, void *const user)
@@ -41,6 +52,59 @@ static int on_length(const unsigned long long len,
return 0;
}
+struct handler *handler;
+
+static void handle_signal(const int signum)
+{
+ switch (signum)
+ {
+ case SIGINT:
+ /* Fall through. */
+ case SIGTERM:
+ handler_notify_close(handler);
+ break;
+
+ default:
+ break;
+ }
+}
+
+static int init_signals(void)
+{
+ struct sigaction sa =
+ {
+ .sa_handler = handle_signal,
+ .sa_flags = SA_RESTART
+ };
+
+ sigemptyset(&sa.sa_mask);
+
+ static const struct signal
+ {
+ int signal;
+ const char *name;
+ } signals[] =
+ {
+ {.signal = SIGINT, .name = "SIGINT"},
+ {.signal = SIGTERM, .name = "SIGTERM"},
+ {.signal = SIGPIPE, .name = "SIGPIPE"}
+ };
+
+ for (size_t i = 0; i < sizeof signals / sizeof *signals; i++)
+ {
+ const struct signal *const s = &signals[i];
+
+ if (sigaction(s->signal, &sa, NULL))
+ {
+ fprintf(stderr, "%s: sigaction(2) %s: %s\n",
+ __func__, s->name, strerror(errno));
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
int main(int argc, char *argv[])
{
int ret = EXIT_FAILURE;
@@ -50,17 +114,21 @@ int main(int argc, char *argv[])
.length = on_length
};
- struct handler *const h = handler_alloc(&cfg);
static const char *const urls[] = {"/*"};
- if (!h)
+ if (!(handler = handler_alloc(&cfg)))
{
fprintf(stderr, "%s: handler_alloc failed\n", __func__);
goto end;
}
+ else if (init_signals())
+ {
+ fprintf(stderr, "%s: init_signals failed\n", __func__);
+ goto end;
+ }
for (size_t i = 0; i < sizeof urls / sizeof *urls; i++)
- if (handler_add(h, urls[i], HTTP_OP_PUT, on_put, NULL))
+ if (handler_add(handler, urls[i], HTTP_OP_PUT, on_put, NULL))
{
fprintf(stderr, "%s: handler_add failed\n", __func__);
goto end;
@@ -68,7 +136,7 @@ int main(int argc, char *argv[])
unsigned short outport;
- if (handler_listen(h, 0, &outport))
+ if (handler_listen(handler, 0, &outport))
{
fprintf(stderr, "%s: handler_listen failed\n", __func__);
goto end;
@@ -76,7 +144,7 @@ int main(int argc, char *argv[])
printf("Listening on port %hu\n", outport);
- if (handler_loop(h))
+ if (handler_loop(handler))
{
fprintf(stderr, "%s: handler_loop failed\n", __func__);
goto end;
@@ -85,6 +153,6 @@ int main(int argc, char *argv[])
ret = EXIT_SUCCESS;
end:
- handler_free(h);
+ handler_free(handler);
return ret;
}