hermes

HTTP GET/HEAD-only web server for static content.
git clone https://git.francescosaccone.com/hermes
Log | Files | Refs | README | LICENSE

commit ef45714ddd140f811857ec3852298558b61a7038
parent 151576ac8e05971d1654e0d9bdcfa9a8a4a63bb4
Author: Francesco Saccone <francesco@francescosaccone.com>
Date:   Mon, 31 Mar 2025 14:03:51 +0200

feat: fork the process after socket creation

We will need two processed: the one where the privileges are
dropped to server the files and the one which will end the program,
where root privileges are required to close the socket.

Signed-off-by: Francesco Saccone <francesco@francescosaccone.com>

Diffstat:
Mhermes.c | 20++++++++++++++++++++
1 file changed, 20 insertions(+), 0 deletions(-)

diff --git a/hermes.c b/hermes.c @@ -3,6 +3,8 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/wait.h> +#include <unistd.h> #include "socket.h" #include "utils.h" @@ -27,6 +29,7 @@ main(int argc, char *argv[]) { server_socket_fd; struct passwd *user; struct group *group; + pid_t pid; for (i = 1; i < argc; i++) { char *argument = argv[i]; @@ -106,6 +109,23 @@ main(int argc, char *argv[]) { server_socket_fd = create_socket(port); + pid = fork(); + + switch (pid) { + case -1: + critical("error: could not fork process."); + break; + case 0: + /* child process */ + break; + default: + /* parent process: wait for the child process to exit */ + int child_status; + + waitpid(pid, &child_status, 0); + break; + } + while (1) { int client_socket_fd = accept_client(server_socket_fd), buffer_size = 104857600 * sizeof(char); /* i.e. 100 MiB */