test: rename fserv -> serv, more features to be added

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2022-04-03 11:55:11 +02:00
1 parent 01a007b388
commit b4af86255d
5 files changed
+15 -15

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
/* Basic forking UNIX daemon with no options to run in foreground */
#include <err.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void sig(int signo)
{
warnx("We got signal %d ...", signo);
exit(0);
}
static void pidfile(char *nm)
{
char fn[80];
pid_t pid;
FILE *fp;
if (!nm)
nm = "serv";
snprintf(fn, sizeof(fn), "%s%s.pid", _PATH_VARRUN, nm);
pid = getpid();
warnx("Creating PID file %s with %d", fn, pid);
fp = fopen(fn, "w");
if (!fp)
exit(1);
fprintf(fp, "%d\n", pid);
fclose(fp);
}
int main(void)
{
/* Daemonize, fork to background etc. */
if (daemon(0, 1))
return 1;
/* Signal handlers first *then* PID file */
signal(SIGTERM, sig);
/* Tell finit where we really are */
pidfile(NULL);
warnx("Entering while(1) loop");
while (1)
sleep(1);
warnx("Leaving ...");
return 0;
}