First attempt at running as a daemon.

This commit is contained in:
Jacob Windle 2020-01-14 10:40:13 -05:00
parent 1b6d185d39
commit a9cd8a4b15

21
main.c
View File

@ -142,13 +142,14 @@ int read_config_file() {
/** /**
* Wake up on SIGALRM to do our thing. * Wake up on SIGALRM to do our thing.
*/ */
void run_loop(int signal) { void run_loop() {
const sigset_t sigset; sigset_t sigset;
sigaddset(&sigset, SIGALRM); sigaddset(&sigset, SIGALRM);
sigaddset(&sigset, SIGINT); sigaddset(&sigset, SIGINT);
int signo; int signo;
alarm(HB_PERIOD);
for (;;){ for (;;){
sigwait(&sigset, &signo); sigwait(&sigset, &signo);
@ -168,9 +169,12 @@ void run_loop(int signal) {
*/ */
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int run_as_daemon = 0;
if (getuid() != 0) if (getuid() != 0)
{ {
fprintf(stderr, "hb: Must run as root using sudo!\n"); fprintf(stderr, "hb: Must run as root using sudo!\n");
exit(1);
} }
// Process our command line arguments. // Process our command line arguments.
@ -186,7 +190,6 @@ int main(int argc, char **argv)
blockHost(argv[++i]); blockHost(argv[++i]);
} }
// Replaces a host // Replaces a host
// TODO: this currently duplicates the whole file and appends it to the end.
else if (ARG_IS("edit")) else if (ARG_IS("edit"))
{ {
replacehost(argv[++i], argv[++i]); replacehost(argv[++i], argv[++i]);
@ -209,16 +212,18 @@ int main(int argc, char **argv)
} }
else if (ARG_IS("-config")) { else if (ARG_IS("-config")) {
CONFIG = argv[++i]; CONFIG = argv[++i];
read_config_file();
} }
else if (ARG_IS("-period")) { else if (ARG_IS("-period")) {
HB_PERIOD = atoi(argv[++i]); HB_PERIOD = atoi(argv[++i]);
} }
else if (ARG_IS("-daemon")) {
// Daemonize this process, allowing for hosts file to be automagically managed. run_as_daemon = 1;
else {
// daemonize();
} }
}
if (run_as_daemon) {
run_loop();
} }
free_list(&hosts); free_list(&hosts);