Added daemonization
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
|||||||
work/*.o
|
work/*.o
|
||||||
build/swebs
|
build/swebs
|
||||||
|
example/logs
|
||||||
|
example/swebs.pid
|
||||||
|
|||||||
1357
example/logs
1357
example/logs
File diff suppressed because it is too large
Load Diff
@@ -1,3 +1,3 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
../build/swebs -s sitefile -o logs -p 8000 -b 100
|
../build/swebs -B -P swebs.pid -s sitefile -o logs -p 8000 -b 100
|
||||||
|
|||||||
25
src/main.c
25
src/main.c
@@ -21,11 +21,14 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include <util.h>
|
#include <util.h>
|
||||||
@@ -39,8 +42,10 @@ int main(int argc, char **argv) {
|
|||||||
int processes = sysconf(_SC_NPROCESSORS_ONLN) + 1;
|
int processes = sysconf(_SC_NPROCESSORS_ONLN) + 1;
|
||||||
uint16_t port = 443;
|
uint16_t port = 443;
|
||||||
int backlog = 100;
|
int backlog = 100;
|
||||||
|
bool shouldDaemonize = false;
|
||||||
|
char *pidfile = "/run/swebs.pid";
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int c = getopt(argc, argv, "o:j:s:p:b:c:hl");
|
int c = getopt(argc, argv, "o:j:s:p:b:c:BP:hl");
|
||||||
if (c == -1)
|
if (c == -1)
|
||||||
break;
|
break;
|
||||||
switch (c) {
|
switch (c) {
|
||||||
@@ -59,6 +64,12 @@ int main(int argc, char **argv) {
|
|||||||
case 'b':
|
case 'b':
|
||||||
backlog = atoi(optarg);
|
backlog = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
|
case 'B':
|
||||||
|
shouldDaemonize = true;
|
||||||
|
break;
|
||||||
|
case 'P':
|
||||||
|
pidfile = optarg;
|
||||||
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
printf(
|
printf(
|
||||||
"swebs Copyright (C) 2022 Nate Choe\n"
|
"swebs Copyright (C) 2022 Nate Choe\n"
|
||||||
@@ -82,6 +93,9 @@ int main(int argc, char **argv) {
|
|||||||
" -s [site file] Use that site file (required)\n"
|
" -s [site file] Use that site file (required)\n"
|
||||||
" -p [port] Set the port (default: 443)\n"
|
" -p [port] Set the port (default: 443)\n"
|
||||||
" -b [backlog] Set the socket backlog (default: 100)\n"
|
" -b [backlog] Set the socket backlog (default: 100)\n"
|
||||||
|
" -B Run swebs in the background and daemonize\n"
|
||||||
|
" -P [pidfile] Specify PID file if daemonizing\n"
|
||||||
|
" (defualt: /run/swebs.pid)\n"
|
||||||
" -l Show some legal details\n"
|
" -l Show some legal details\n"
|
||||||
" -h Show this help message\n"
|
" -h Show this help message\n"
|
||||||
);
|
);
|
||||||
@@ -92,6 +106,7 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (sitefile == NULL) {
|
if (sitefile == NULL) {
|
||||||
fprintf(stderr, "No sitefile configured\n");
|
fprintf(stderr, "No sitefile configured\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@@ -118,6 +133,14 @@ int main(int argc, char **argv) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (shouldDaemonize) {
|
||||||
|
if (daemon(1, 0) < 0)
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
FILE *pid = fopen(pidfile, "w");
|
||||||
|
fprintf(pid, "%d\n", getpid());
|
||||||
|
fclose(pid);
|
||||||
|
}
|
||||||
|
|
||||||
if (initLogging(logout)) {
|
if (initLogging(logout)) {
|
||||||
fprintf(stderr, "Couldn't open logs file %s\n", logout);
|
fprintf(stderr, "Couldn't open logs file %s\n", logout);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
|||||||
Reference in New Issue
Block a user