feat: daemon module functional (I hope)
This commit is contained in:
parent
1771f72ecb
commit
d2076a5fd5
8 changed files with 101 additions and 30 deletions
|
|
@ -16,7 +16,8 @@ MODULES_SRCS = src/config/config.c \
|
|||
src/http/http.c \
|
||||
src/http/headers.c \
|
||||
src/logger/logs.c \
|
||||
src/logger/errors.c
|
||||
src/logger/errors.c \
|
||||
src/daemon/daemon.c
|
||||
SRCS = $(UTILS_SRCS) \
|
||||
$(MODULES_SRCS) \
|
||||
src/main.c
|
||||
|
|
|
|||
|
|
@ -1,20 +1,56 @@
|
|||
#include "daemon.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);
|
||||
}
|
||||
|
||||
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
|
||||
stop_daemon(pid);
|
||||
stop_daemon();
|
||||
|
||||
// Start again
|
||||
return start_daemon();
|
||||
|
|
|
|||
|
|
@ -5,22 +5,26 @@
|
|||
|
||||
/* @brief
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
void stop_daemon();
|
||||
int get_pid();
|
||||
|
||||
/* @brief
|
||||
*/
|
||||
void daemon_init(struct config* cfg);
|
||||
|
||||
/* @brief
|
||||
*/
|
||||
void stop_daemon(void);
|
||||
|
||||
/* @brief
|
||||
*
|
||||
* @param
|
||||
*/
|
||||
int start_daemon(void);
|
||||
|
||||
/* @brief
|
||||
*
|
||||
* @param
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int restart_daemon();
|
||||
int restart_daemon(void);
|
||||
|
||||
#endif // ! DAEMON_H
|
||||
|
|
|
|||
|
|
@ -48,18 +48,20 @@ static ssize_t parse_reqline(struct http_request *res, struct string *req)
|
|||
return ERR_HTTP_INVALID_INPUT;
|
||||
|
||||
// Target (path)
|
||||
skipped += read_word(req, i, &res->target);
|
||||
skipped = read_word(req, i, &res->target);
|
||||
if (skipped <= 0)
|
||||
return ERR_HTTP_INVALID_INPUT;
|
||||
i += skipped;
|
||||
|
||||
// Skip space
|
||||
if (req->data[i++] != ' ')
|
||||
return ERR_HTTP_INVALID_INPUT;
|
||||
|
||||
// Protocol
|
||||
skipped += read_word(req, i, &res->protocol);
|
||||
skipped = read_word(req, i, &res->protocol);
|
||||
if (skipped <= 0)
|
||||
return ERR_HTTP_INVALID_INPUT;
|
||||
i += skipped;
|
||||
|
||||
// CRLF (EOL)
|
||||
if (req->data[i++] != '\r' && req->data[i++] != '\n')
|
||||
|
|
@ -178,7 +180,7 @@ struct http_response *generate_response(struct http_request *req)
|
|||
return NULL;
|
||||
|
||||
// Protocol
|
||||
char *protocol = "HTTP/1.1";
|
||||
char *protocol = HTTP_VERSION;
|
||||
res->protocol = string_create(protocol, strlen(protocol));
|
||||
|
||||
// Status code
|
||||
|
|
|
|||
|
|
@ -106,6 +106,6 @@ void destroy_request(struct http_request *req);
|
|||
*
|
||||
* @param resp
|
||||
*/
|
||||
void destroy_response(struct response *resp);
|
||||
void destroy_response(struct http_response *resp);
|
||||
|
||||
#endif // ! HTTP_H
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include <getopt.h>
|
||||
|
||||
#include "config/config.h"
|
||||
#include "daemon/daemon.h"
|
||||
#include "http/http.h"
|
||||
#include "logger/logs.h"
|
||||
#include "server/server.h"
|
||||
|
|
@ -21,19 +22,21 @@ int main(int argc, char **argv)
|
|||
// Start server
|
||||
switch (config->daemon)
|
||||
{
|
||||
NO_OPTION:
|
||||
case NO_OPTION:
|
||||
start_server("localhost", config->servers->port);
|
||||
break;
|
||||
|
||||
START:
|
||||
case START:
|
||||
start_daemon();
|
||||
break;
|
||||
|
||||
RESTART:
|
||||
case RESTART:
|
||||
restart_daemon();
|
||||
break;
|
||||
|
||||
STOP:
|
||||
case STOP:
|
||||
stop_daemon();
|
||||
break;
|
||||
|
||||
default:
|
||||
return 2;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "files.h"
|
||||
|
||||
// #include <stdio.h>
|
||||
#include <stdio.h>
|
||||
// #include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
// #include "../string/string.h"
|
||||
|
|
@ -36,3 +37,25 @@ int is_directory(const char *path)
|
|||
// int nread;
|
||||
// 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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@
|
|||
#include <stddef.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "../string/string.h"
|
||||
|
||||
// === Functions
|
||||
|
||||
/*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue