mirror of
https://github.com/troglobit/finit.git
synced 2026-10-25 12:04:36 +00:00
test: extend start-stop-serv test w/ all supported combinations
serv: add support for foregrounding, running with and without PID file,
including custom PID filename.
test: verify Finit can start & monitor services that:
1) Fork and creates a PID file in a known location
2) Don't fork and don't create a PID file, but Finit does
3) Don't fork but create a PID file
4) Don't fork and create custom named PID file
Note: Finit cannot support a service that forks and doesn't create a PID
file. This combination is impossible to support without tracking
all processes created in /proc -- which Finit does not do atm.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
1 parent
b4af86255d
commit
27ba3b27f5
3 files changed
+120
-38
No files matched your search
+68
-13
@@ -1,4 +1,9 @@
|
||||
/* Basic forking UNIX daemon with no options to run in foreground */
|
||||
/*
|
||||
* Basic UNIX daemon
|
||||
*
|
||||
* Options to run in foreground and to create a PID file. When running
|
||||
* in foregrund it does not create a PID file by default.
|
||||
*/
|
||||
|
||||
#include <err.h>
|
||||
#include <paths.h>
|
||||
@@ -8,43 +13,93 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define PROGNM "serv"
|
||||
|
||||
static void sig(int signo)
|
||||
{
|
||||
warnx("We got signal %d ...", signo);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
static void pidfile(char *nm)
|
||||
static void pidfile(char *pidfn)
|
||||
{
|
||||
char fn[80];
|
||||
pid_t pid;
|
||||
FILE *fp;
|
||||
|
||||
if (!nm)
|
||||
nm = "serv";
|
||||
|
||||
snprintf(fn, sizeof(fn), "%s%s.pid", _PATH_VARRUN, nm);
|
||||
if (!pidfn) {
|
||||
snprintf(fn, sizeof(fn), "%s%s.pid", _PATH_VARRUN, PROGNM);
|
||||
pidfn = fn;
|
||||
}
|
||||
pid = getpid();
|
||||
warnx("Creating PID file %s with %d", fn, pid);
|
||||
warnx("Creating PID file %s with %d", pidfn, pid);
|
||||
|
||||
fp = fopen(fn, "w");
|
||||
fp = fopen(pidfn, "w");
|
||||
if (!fp)
|
||||
exit(1);
|
||||
fprintf(fp, "%d\n", pid);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
static int usage(int rc)
|
||||
{
|
||||
FILE *fp = rc ? stderr : stdout;
|
||||
|
||||
fprintf(fp,
|
||||
"%s [-nhp] [-P FILE]\n"
|
||||
"\n"
|
||||
" -n Run in foreground\n"
|
||||
" -h Show help text (this)\n"
|
||||
" -p Create PID file despite running in foreground\n"
|
||||
" -P FILE Create PID file using FILE\n"
|
||||
"\n"
|
||||
"By default this program daemonizes itself to the background, and,\n"
|
||||
"when it's done setting up its signal handler(s), creates a PID file\n"
|
||||
"to let the rest of the system know it's done. When the program runs\n"
|
||||
"in the foreground it does not create a PID file by default.\n",
|
||||
PROGNM);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int do_background = 1;
|
||||
int do_pidfile = 1;
|
||||
char *pidfn = NULL;
|
||||
int c;
|
||||
|
||||
while ((c = getopt(argc, argv, "hnpP:")) != EOF) {
|
||||
switch (c) {
|
||||
case 'h':
|
||||
return usage(0);
|
||||
case 'n':
|
||||
do_background = 0;
|
||||
do_pidfile--;
|
||||
break;
|
||||
case 'p':
|
||||
do_pidfile++;
|
||||
break;
|
||||
case 'P':
|
||||
pidfn = optarg;
|
||||
break;
|
||||
default:
|
||||
return usage(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Daemonize, fork to background etc. */
|
||||
if (daemon(0, 1))
|
||||
return 1;
|
||||
if (do_background) {
|
||||
if (daemon(0, 1))
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Signal handlers first *then* PID file */
|
||||
signal(SIGTERM, sig);
|
||||
|
||||
/* Tell finit where we really are */
|
||||
pidfile(NULL);
|
||||
/* Tell world where we are, but not if bg w/o pid file */
|
||||
if (do_pidfile == 1)
|
||||
pidfile(pidfn);
|
||||
|
||||
warnx("Entering while(1) loop");
|
||||
while (1)
|
||||
|
||||
Reference in new issue
Block a user