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)
|
$(TARGET): $(OBJS)
|
||||||
$(CC) -o $@ $(OBJS) $(LDFLAGS) $(LDLIBS)
|
$(CC) -o $@ $(OBJS) $(LDFLAGS) $(LDLIBS)
|
||||||
|
|
||||||
check:
|
check: $(TARGET)
|
||||||
dash tests/run.sh
|
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: CFLAGS += $(CFLAGS_DBG)
|
||||||
debug: $(OBJS)
|
debug: $(OBJS)
|
||||||
|
|
@ -45,5 +51,7 @@ asan: $(OBJS)
|
||||||
$(CC) -o $(TARGET) $(OBJS) $(LDFLAGS) $(LDLIBS)
|
$(CC) -o $(TARGET) $(OBJS) $(LDFLAGS) $(LDLIBS)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
- pkill -9 $(TARGET)
|
||||||
|
$(RM) tests/$(TARGET) tests/out.log
|
||||||
$(RM) $(TARGET)
|
$(RM) $(TARGET)
|
||||||
$(RM) $(OBJS)
|
$(RM) $(OBJS)
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "../utils/time/fmt_time.h"
|
#include "../utils/time/fmt_time.h"
|
||||||
#include "logs.h"
|
#include "logs.h"
|
||||||
|
|
@ -16,10 +17,13 @@ static struct logs_config config;
|
||||||
|
|
||||||
// === Functions
|
// === 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;
|
config.enabled = enabled;
|
||||||
|
if (logfile_fd == STDOUT_FILENO)
|
||||||
|
config.logfile_fd = STDERR_FILENO;
|
||||||
config.logfile_fd = logfile_fd;
|
config.logfile_fd = logfile_fd;
|
||||||
|
config.server_cfg = serv_cfg;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_err()
|
void print_err()
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,12 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "../config/config.h"
|
||||||
|
|
||||||
/* @brief Initialize the error logging submodule
|
/* @brief Initialize the error logging submodule
|
||||||
* @warning Do not use 'as is', use log_init() instead
|
* @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
|
/* @brief Retrieves the last error with errno and prints the corresponding
|
||||||
* error message in the logs and stderr
|
* error message in the logs and stderr
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "../utils/time/fmt_time.h"
|
#include "../utils/time/fmt_time.h"
|
||||||
#include "errors.h"
|
#include "errors.h"
|
||||||
|
|
@ -20,13 +21,21 @@ int log_init(struct config *global_config)
|
||||||
int return_value = 0;
|
int return_value = 0;
|
||||||
config.enabled = global_config->log;
|
config.enabled = global_config->log;
|
||||||
config.server_cfg = global_config->servers;
|
config.server_cfg = global_config->servers;
|
||||||
config.logfile_fd = open(global_config->log_file, O_WRONLY);
|
if (global_config->log_file != NULL)
|
||||||
if (config.logfile_fd <= 0)
|
|
||||||
{
|
{
|
||||||
config.enabled = false;
|
config.logfile_fd = open(global_config->log_file, O_WRONLY);
|
||||||
return_value = 1;
|
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;
|
return return_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,30 @@
|
||||||
// === Definitions
|
// === Definitions
|
||||||
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#define _POSIX_C_SOURCE 200112L
|
#define _POSIX_C_SOURCE 200112L
|
||||||
|
|
||||||
#define BUFFER_SIZE 1024
|
#define BUFFER_SIZE 1024
|
||||||
|
|
||||||
// === Includes
|
// === Includes
|
||||||
|
#include "server.h"
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "../http/http.h"
|
#include "../http/http.h"
|
||||||
#include "../logger/errors.h"
|
#include "../logger/errors.h"
|
||||||
#include "server.h"
|
#include "../logger/logs.h"
|
||||||
// #include "../logger/logs.h"
|
// #include "../logger/logs.h"
|
||||||
|
|
||||||
|
// === Static variables
|
||||||
|
|
||||||
|
int server_socket = 0;
|
||||||
|
|
||||||
// === Static functions
|
// === Static functions
|
||||||
|
|
||||||
// Creates and bind the server communication socket
|
// 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);
|
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
|
// === Functions
|
||||||
|
|
||||||
void start_server(const char *host, const char *port)
|
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)
|
if (server_socket == -1)
|
||||||
// TODO log that
|
// TODO log that
|
||||||
return;
|
return;
|
||||||
|
|
@ -115,6 +137,17 @@ void start_server(const char *host, const char *port)
|
||||||
if (err == -1)
|
if (err == -1)
|
||||||
return;
|
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
|
// Main loop
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
|
|
@ -134,5 +167,10 @@ void start_server(const char *host, const char *port)
|
||||||
close(client_fd);
|
close(client_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stop_server();
|
||||||
|
}
|
||||||
|
|
||||||
|
void stop_server(void)
|
||||||
|
{
|
||||||
close(server_socket);
|
close(server_socket);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,17 @@
|
||||||
|
|
||||||
// #define _POSIX_C_SOURCE 200112L
|
// #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
|
* @param port
|
||||||
*/
|
*/
|
||||||
void start_server(const char *host, const char *port);
|
void start_server(const char *host, const char *port);
|
||||||
|
|
||||||
|
/* @brief Stops the currently running HTTP server
|
||||||
|
*/
|
||||||
|
void stop_server(void);
|
||||||
|
|
||||||
#endif // ! SERVER_H
|
#endif // ! SERVER_H
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue