fix: signal handling for SIGINT, default logs to stdout
This commit is contained in:
parent
9ecc9ed9a7
commit
f16bdbdf73
6 changed files with 81 additions and 14 deletions
|
|
@ -31,8 +31,14 @@ TARGET=httpd
|
|||
$(TARGET): $(OBJS)
|
||||
$(CC) -o $@ $(OBJS) $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
check:
|
||||
dash tests/run.sh
|
||||
check: $(TARGET)
|
||||
cp $(TARGET) tests/$(TARGET)
|
||||
cd tests
|
||||
python3 -m venv env
|
||||
env/bin/python -m pip install requests
|
||||
env/bin/python -m pip install pytest
|
||||
env/bin/python -m pip install pytest-timeout
|
||||
- env/bin/pytest
|
||||
|
||||
debug: CFLAGS += $(CFLAGS_DBG)
|
||||
debug: $(OBJS)
|
||||
|
|
@ -45,5 +51,7 @@ asan: $(OBJS)
|
|||
$(CC) -o $(TARGET) $(OBJS) $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
clean:
|
||||
- pkill -9 $(TARGET)
|
||||
$(RM) tests/$(TARGET) tests/out.log
|
||||
$(RM) $(TARGET)
|
||||
$(RM) $(OBJS)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../utils/time/fmt_time.h"
|
||||
#include "logs.h"
|
||||
|
|
@ -16,10 +17,13 @@ static struct logs_config config;
|
|||
|
||||
// === Functions
|
||||
|
||||
void errlog_init(bool enabled, int logfile_fd)
|
||||
void errlog_init(bool enabled, int logfile_fd, struct server_config *serv_cfg)
|
||||
{
|
||||
config.enabled = enabled;
|
||||
if (logfile_fd == STDOUT_FILENO)
|
||||
config.logfile_fd = STDERR_FILENO;
|
||||
config.logfile_fd = logfile_fd;
|
||||
config.server_cfg = serv_cfg;
|
||||
}
|
||||
|
||||
void print_err()
|
||||
|
|
|
|||
|
|
@ -4,10 +4,12 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../config/config.h"
|
||||
|
||||
/* @brief Initialize the error logging submodule
|
||||
* @warning Do not use 'as is', use log_init() instead
|
||||
*/
|
||||
void errlog_init(bool enabled, int logfile_fd);
|
||||
void errlog_init(bool enabled, int logfile_fd, struct server_config *serv_cfg);
|
||||
|
||||
/* @brief Retrieves the last error with errno and prints the corresponding
|
||||
* error message in the logs and stderr
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../utils/time/fmt_time.h"
|
||||
#include "errors.h"
|
||||
|
|
@ -20,13 +21,21 @@ int log_init(struct config *global_config)
|
|||
int return_value = 0;
|
||||
config.enabled = global_config->log;
|
||||
config.server_cfg = global_config->servers;
|
||||
if (global_config->log_file != NULL)
|
||||
{
|
||||
config.logfile_fd = open(global_config->log_file, O_WRONLY);
|
||||
if (config.logfile_fd <= 0)
|
||||
{
|
||||
config.enabled = false;
|
||||
return_value = 1;
|
||||
}
|
||||
errlog_init(config.enabled, config.logfile_fd);
|
||||
}
|
||||
else
|
||||
{
|
||||
config.logfile_fd = STDOUT_FILENO;
|
||||
}
|
||||
|
||||
errlog_init(config.enabled, config.logfile_fd, config.server_cfg);
|
||||
return return_value;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,23 +1,30 @@
|
|||
// === Definitions
|
||||
|
||||
#include <netinet/in.h>
|
||||
#define _POSIX_C_SOURCE 200112L
|
||||
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
// === Includes
|
||||
#include "server.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../http/http.h"
|
||||
#include "../logger/errors.h"
|
||||
#include "server.h"
|
||||
#include "../logger/logs.h"
|
||||
// #include "../logger/logs.h"
|
||||
|
||||
// === Static variables
|
||||
|
||||
int server_socket = 0;
|
||||
|
||||
// === Static functions
|
||||
|
||||
// Creates and bind the server communication socket
|
||||
|
|
@ -102,11 +109,26 @@ static void get_ip(struct sockaddr *client_addr, char *res)
|
|||
inet_ntop(AF_INET, &ipAddr, res, INET_ADDRSTRLEN);
|
||||
}
|
||||
|
||||
static void signal_handler(int signal)
|
||||
{
|
||||
print_log("signal received: %d", signal);
|
||||
switch (signal)
|
||||
{
|
||||
case SIGINT: {
|
||||
print_log("Stopping server...");
|
||||
stop_server();
|
||||
exit(0);
|
||||
}
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// === Functions
|
||||
|
||||
void start_server(const char *host, const char *port)
|
||||
{
|
||||
int server_socket = get_socket(host, port);
|
||||
server_socket = get_socket(host, port);
|
||||
if (server_socket == -1)
|
||||
// TODO log that
|
||||
return;
|
||||
|
|
@ -115,6 +137,17 @@ void start_server(const char *host, const char *port)
|
|||
if (err == -1)
|
||||
return;
|
||||
|
||||
// Signal handling
|
||||
struct sigaction siga;
|
||||
siga.sa_flags = 0;
|
||||
siga.sa_handler = signal_handler;
|
||||
// initialize mask
|
||||
if (sigemptyset(&siga.sa_mask) < 0)
|
||||
// TODO log that
|
||||
return;
|
||||
if (sigaction(SIGINT, &siga, NULL) == -1)
|
||||
return;
|
||||
|
||||
// Main loop
|
||||
while (1)
|
||||
{
|
||||
|
|
@ -134,5 +167,10 @@ void start_server(const char *host, const char *port)
|
|||
close(client_fd);
|
||||
}
|
||||
|
||||
stop_server();
|
||||
}
|
||||
|
||||
void stop_server(void)
|
||||
{
|
||||
close(server_socket);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,11 +3,17 @@
|
|||
|
||||
// #define _POSIX_C_SOURCE 200112L
|
||||
|
||||
/* @brief
|
||||
/* @brief Starts the HTTP server
|
||||
*
|
||||
* @param hpstname
|
||||
* @warn Make sure to initialize modules before calling
|
||||
*
|
||||
* @param hostname
|
||||
* @param port
|
||||
*/
|
||||
void start_server(const char *host, const char *port);
|
||||
|
||||
/* @brief Stops the currently running HTTP server
|
||||
*/
|
||||
void stop_server(void);
|
||||
|
||||
#endif // ! SERVER_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue