feat: daemon module functional (I hope)

This commit is contained in:
Gu://em_ 2025-11-26 15:54:02 +01:00
parent 1771f72ecb
commit d2076a5fd5
8 changed files with 101 additions and 30 deletions

View file

@ -16,7 +16,8 @@ MODULES_SRCS = src/config/config.c \
src/http/http.c \ src/http/http.c \
src/http/headers.c \ src/http/headers.c \
src/logger/logs.c \ src/logger/logs.c \
src/logger/errors.c src/logger/errors.c \
src/daemon/daemon.c
SRCS = $(UTILS_SRCS) \ SRCS = $(UTILS_SRCS) \
$(MODULES_SRCS) \ $(MODULES_SRCS) \
src/main.c src/main.c

View file

@ -1,20 +1,56 @@
#include "daemon.h" #include "daemon.h"
#include <signal.h> #include <signal.h>
#include <stdio.h>
#include <stdlib.h>
void stop_daemon(int pid) #include "../server/server.h"
static struct config *config;
// === Functions
void daemon_init(struct config *cfg)
{ {
if (pid != -1) config = cfg;
}
int get_pid()
{
FILE *stream = fopen(config->pid_file, "r");
if (stream == NULL)
return -2;
char buf[10];
size_t nread = fread(buf, sizeof(char), 10, stream);
if (nread > 8) // PID is max 8 chars
return -3;
buf[nread] = '\0';
int res = atoi(buf);
fclose(stream);
return res;
}
void stop_daemon(void)
{
int pid = get_pid();
if (pid > 0)
kill(pid, SIGINT); kill(pid, SIGINT);
} }
int start_daemon(void) int start_daemon(void)
{} {
start_server("localhost", config->servers->port);
return 0;
}
int restart_daemon(int pid) int restart_daemon(void)
{ {
// Attempt to kill process // Attempt to kill process
stop_daemon(pid); stop_daemon();
// Start again // Start again
return start_daemon(); return start_daemon();

View file

@ -5,22 +5,26 @@
/* @brief /* @brief
* *
* @param * @return
*/ */
void stop_daemon(); int get_pid();
/* @brief
*/
void daemon_init(struct config* cfg);
/* @brief
*/
void stop_daemon(void);
/* @brief /* @brief
*
* @param
*/ */
int start_daemon(void); int start_daemon(void);
/* @brief /* @brief
*
* @param
* *
* @return * @return
*/ */
int restart_daemon(); int restart_daemon(void);
#endif // ! DAEMON_H #endif // ! DAEMON_H

View file

@ -48,18 +48,20 @@ static ssize_t parse_reqline(struct http_request *res, struct string *req)
return ERR_HTTP_INVALID_INPUT; return ERR_HTTP_INVALID_INPUT;
// Target (path) // Target (path)
skipped += read_word(req, i, &res->target); skipped = read_word(req, i, &res->target);
if (skipped <= 0) if (skipped <= 0)
return ERR_HTTP_INVALID_INPUT; return ERR_HTTP_INVALID_INPUT;
i += skipped;
// Skip space // Skip space
if (req->data[i++] != ' ') if (req->data[i++] != ' ')
return ERR_HTTP_INVALID_INPUT; return ERR_HTTP_INVALID_INPUT;
// Protocol // Protocol
skipped += read_word(req, i, &res->protocol); skipped = read_word(req, i, &res->protocol);
if (skipped <= 0) if (skipped <= 0)
return ERR_HTTP_INVALID_INPUT; return ERR_HTTP_INVALID_INPUT;
i += skipped;
// CRLF (EOL) // CRLF (EOL)
if (req->data[i++] != '\r' && req->data[i++] != '\n') if (req->data[i++] != '\r' && req->data[i++] != '\n')
@ -178,7 +180,7 @@ struct http_response *generate_response(struct http_request *req)
return NULL; return NULL;
// Protocol // Protocol
char *protocol = "HTTP/1.1"; char *protocol = HTTP_VERSION;
res->protocol = string_create(protocol, strlen(protocol)); res->protocol = string_create(protocol, strlen(protocol));
// Status code // Status code

View file

@ -106,6 +106,6 @@ void destroy_request(struct http_request *req);
* *
* @param resp * @param resp
*/ */
void destroy_response(struct response *resp); void destroy_response(struct http_response *resp);
#endif // ! HTTP_H #endif // ! HTTP_H

View file

@ -1,6 +1,7 @@
#include <getopt.h> #include <getopt.h>
#include "config/config.h" #include "config/config.h"
#include "daemon/daemon.h"
#include "http/http.h" #include "http/http.h"
#include "logger/logs.h" #include "logger/logs.h"
#include "server/server.h" #include "server/server.h"
@ -21,22 +22,24 @@ int main(int argc, char **argv)
// Start server // Start server
switch (config->daemon) switch (config->daemon)
{ {
NO_OPTION: case NO_OPTION:
start_server("localhost", config->servers->port); start_server("localhost", config->servers->port);
break; break;
START: case START:
start_daemon(); start_daemon();
break; break;
RESTART: case RESTART:
restart_daemon(); restart_daemon();
break;
STOP: case STOP:
stop_daemon(); stop_daemon();
break;
default: default:
return 2; return 2;
} }
return 0; return 0;

View file

@ -1,6 +1,7 @@
#include "files.h" #include "files.h"
// #include <stdio.h> #include <stdio.h>
// #include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
// #include "../string/string.h" // #include "../string/string.h"
@ -36,3 +37,25 @@ int is_directory(const char *path)
// int nread; // int nread;
// while ((fgets(buf, BUFFER_SIZE, stream))) // while ((fgets(buf, BUFFER_SIZE, stream)))
// } // }
// TODO not implemented
bool check_filename(struct string *path)
{
if (path == NULL || path->size <= 0)
return false;
return true;
}
ssize_t get_file_content_size(const char *path)
{
FILE *stream = fopen(path, "r");
if (stream == NULL)
return -2;
fseek(stream, 0, SEEK_END);
ssize_t res = ftell(stream);
fclose(stream);
return res;
}

View file

@ -11,6 +11,8 @@
#include <stddef.h> #include <stddef.h>
#include <sys/types.h> #include <sys/types.h>
#include "../string/string.h"
// === Functions // === Functions
/* /*