unix/fiss

src/fsvs/fsvs.c in master
Repositories | Summary | Log | Files | LICENSE

fsvs.c (1417B) download


 1
 2#include "config.h"
 3#include "message.h"
 4#include "service.h"
 5#include "util.h"
 6
 7#include <getopt.h>
 8#include <stdio.h>
 9#include <sys/wait.h>
10#include <unistd.h>
11
12
13const char* current_prog(void) {
14	return "fsvs";
15}
16
17static const struct option long_options[] = {
18	{ "version", no_argument, 0, 'V' },
19	{ "once", no_argument, 0, 'o' },
20	{ 0 }
21};
22
23static void signal_interrupt(int signum) {
24	(void) signum;
25
26	daemon_running = false;
27}
28
29int main(int argc, char** argv) {
30	int  c;
31	bool once = false;
32	while ((c = getopt_long(argc, argv, ":Vo", long_options, NULL)) > 0) {
33		switch (c) {
34			case 'V':
35				print_version_exit();
36				break;
37			case 'o':
38				once = true;
39				break;
40			default:
41			case '?':
42				if (optopt)
43					fprint(1, "error: invalid option -%c\n", optopt);
44				else
45					fprint(1, "error: invalid option %s\n", argv[optind - 1]);
46				print_usage_exit(PROG_FSVS, 1);
47		}
48	}
49
50	argv += optind;
51	argc -= optind;
52	if (argc == 0) {
53		fprint(1, "error: missing <service-dir>\n");
54		print_usage_exit(PROG_FSVS, 1);
55	} else if (argc == 1) {
56		fprint(1, "error: missing <runlevel>\n");
57		print_usage_exit(PROG_FSVS, 1);
58	} else if (argc > 2) {
59		fprint(1, "error: too many arguments\n");
60		print_usage_exit(PROG_FSVS, 1);
61	}
62
63	struct sigaction sa = { 0 };
64	sa.sa_handler       = signal_interrupt;
65	sigaction(SIGINT, &sa, NULL);
66	sigaction(SIGTERM, &sa, NULL);
67
68	return service_supervise(argv[0], argv[1], once);
69}