-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsysmgr.c
123 lines (104 loc) · 2.79 KB
/
sysmgr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* sysmgr -- A simplistic service supervisor.
*
* Copyright (C) 2020 Cem Keylan <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <dirent.h>
#include <string.h>
#include "util.h"
#include "config.h"
static char *argv0, *rundir, *sysdir;
void
term(int sig)
{
/* We are ignoring the sig variable */
(void)(sig);
DIR *dir;
struct dirent *ent;
if ((dir = opendir(rundir)) == NULL)
die("%s:", rundir);
while ((ent = readdir(dir)) != NULL) {
char realfile[PATH_MAX];
sprintf(realfile, "%s/%s", rundir, ent->d_name);
if (strncmp(ent->d_name, ".", 1) == 0 ||
strcmp(ent->d_name, "pid") == 0)
continue;
struct service sv;
sv_init(&sv, ent->d_name);
pid_t pid = getsyspid(&sv);
if (pid == -1) {
perror(sv.name);
continue;
}
if (kill(pid, SIGTERM) != 0)
perror("kill");
}
sleep(1);
rm_rf(rundir);
exit(0);
}
void
usage(void)
{
printf("usage: %s\n\nSee sysmgr(8) for detailed information.\n\nVersion: %s\n", argv0, VERSION);
printf("Service Directory: %s\nRun directory: %s\n", sysdir, rundir);
exit(1);
}
int main(int argc, char *argv[])
{
argv0 = argv[0];
char sysmgr_pidfile[PATH_MAX];
sysdir = getenv_fallback("SYSDIR", sysdir_default);
rundir = getenv_fallback("RUNDIR", rundir_default);
snprintf(sysmgr_pidfile, PATH_MAX, "%s/pid", rundir);
if (argc > 1)
usage();
mkdirp(rundir);
/* Trap signals */
int sv_signals[] = {SIGTERM, SIGINT, SIGHUP, SIGQUIT, SIGABRT};
for (long unsigned int i=0; i < sizeof(sv_signals); i++)
signal(sv_signals[i], term);
pid_t pid = getpid();
if (writesvpid(sysmgr_pidfile, pid) != 0)
die("%s:", sysmgr_pidfile);
/* Go to the service directory and get all the service entries. */
DIR *dir;
struct dirent *ent;
while(1) {
if ((dir = opendir(sysdir)) == NULL)
die("%s:", sysdir);
while ((ent = readdir(dir)) != NULL) {
if (strncmp(ent->d_name, ".", 1) == 0)
continue;
struct service sv;
sv_init(&sv, ent->d_name);
if (sv_check(&sv, 0) != 0) {
sv_start(&sv);
}
}
closedir(dir);
sleep(1);
}
wait(NULL);
term(SIGTERM);
return 0;
}