This commit is contained in:
2021-03-01 16:39:46 +01:00
parent 0453269f24
commit 5b651516d2
35 changed files with 1013 additions and 14 deletions

View File

@@ -0,0 +1,13 @@
CFLAGS = -g
all: daemon
clean:
rm -f helpers.o daemon.o daemon
helpers.o: helpers.c helpers.h
daemon.o: daemon.c helpers.h
daemon: daemon.o helpers.o
$(CC) -o daemon daemon.o helpers.o

View File

@@ -0,0 +1,54 @@
#include <stdio.h>
#include <signal.h>
#include "helpers.h"
void got_signal(int s) {
/*
* For demonstration purposes only!
*
* In real-world applications avoid the execution of complex
* tasks in signal handlers, this cries for race conditions and
* exploits.
*
* https://web.archive.org/web/20070204064240/http://www.bindview.com:80/
% Services/Razor/Papers/2001/signals.cfm
*/
static int count = 2;
fprintf(stderr, "Caught Ctrl-C, press %i more time%s to really"
" quit.\n", count, count == 1 ? "" : "s");
if (count-- > 1) {
/* Catch subsequent SIGINTs */
signal(SIGINT, got_signal);
}
else {
/* Don't catch SIGINT anymore */
signal(SIGINT, SIG_DFL);
}
}
int main(void) {
/*
* Initialization...
*/
void load_cfg(int x) // Wrapper um die inkompatibilität durch die fehlerhafte methodendeklaration zu beheben
{
load_config();
}
load_config();
signal(SIGHUP, load_cfg);
signal(SIGINT, got_signal);
signal(SIGKILL, SIG_IGN); // Unaufhaltsam, da SIGKILL sich ja eben genau nicht um das Programm kümmern soll, sondern einfach immer töten soll
signal(SIGTERM, SIG_IGN); // Aufhaltbar
/*
* Do some work.
*/
main_loop();
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,103 @@
/*
* This is just some code behind the curtain.
* You really don't need to touch this.
*
*/
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include "helpers.h"
int do_quit = 0;
int do_reload = 0;
int load_config(void) {
static char firsttime_message[] = "Loading";
static char subsequent_message[] = "Reloading";
static char *message = firsttime_message;
fprintf(stderr, "%s configuration... ", message);
fprintf(stderr, "done.\n");
message = subsequent_message;
do_reload = 0;
return 1;
}
int main_loop(void) {
fd_set rfds;
struct timeval tv;
int retval;
char buf[1024];
ssize_t len;
#if 0
/* fork() to the background */
if(fork()) return 0;
#endif
fprintf(stderr, "I'm just a simple daemon, my PID is: %i\n",
getpid());
while (!do_quit) {
/*
* Let's just do our really simple job:
* Copy stdin to stdout (just another cat(1)).
*/
if (do_reload) {
load_config();
}
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = select(1, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */
if (retval == -1) {
if (errno == EINTR) {
continue;
}
perror("select()");
continue;
}
if (retval == 0) {
/* fprintf(stderr, "Timeout reached.\n"); */
continue;
}
if (!FD_ISSET(0, &rfds)) {
fprintf(stderr, "BUG: Empty read-set, this should"
" never happen. Never ever. Really! :-)\n");
continue;
}
len = read(0, buf, sizeof(buf) - 1);
if (len < 0) {
perror("read()");
continue;
}
buf[len] = 0;
printf("%s", buf);
if (len == 0) {
do_quit = 1;
}
}
return 1;
}

View File

@@ -0,0 +1,7 @@
#ifndef _HELPERS_H
#define _HELPERS_H
int load_config(void); /* load/reload our daemon's configuration */
int main_loop(void); /* our daemon's main task */
#endif /* _HELPERS_H */

Binary file not shown.