-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunsyssv.c
111 lines (97 loc) · 2.28 KB
/
runsyssv.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
/* runsyssv -- Run and control service scripts
*
* 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 <libgen.h>
#include <string.h>
#include <dirent.h>
#include "util.h"
#include "config.h"
static char *argv0, *lockfile, *svrundir;
static pid_t syspid, svpid;
void
usage(int exitnum)
{
printf("usage: %s [svfile]\n\nsysmgr-%s\n", argv0, VERSION);
exit(exitnum);
}
void
term(int sig)
{
switch(sig) {
case SIGUSR1:
kill(svpid, SIGKILL);
break;
default:
kill(svpid, SIGTERM);
break;
}
if (sv_writelock(lockfile, sig) != 0)
rm_rf(svrundir);
exit(0);
}
int
main(int argc, char *argv[])
{
/* Variables used by other functions but not in this file itself */
(void)(sysdir_default);
(void)(rundir_default);
argv0 = argv[0];
if (argc == 2) {
if (strncmp(argv[1], "-", 1) == 0)
usage(0);
} else
usage(1);
syspid = getpid();
struct service sv;
sv_init(&sv, basename(argv[1]));
DIR *dir;
if ((dir = opendir(sv.svrundir)) != NULL) {
closedir(dir);
return 1;
}
mkdirp(sv.svrundir);
int sv_signals[] = {SIGTERM, SIGINT, SIGHUP, SIGQUIT, SIGABRT};
lockfile = sv.lockfile;
svrundir = sv.svrundir;
svpid = fork();
switch(svpid) {
case -1:
die("fork:");
break;
case 0:
execvp(sv.svfile, argv);
perror(sv.svfile);
break;
default:
writesvpid(sv.syspidfile, syspid);
writesvpid(sv.pidfile, svpid);
for (long unsigned int i=0; i < sizeof(sv_signals); i++)
signal(sv_signals[i], term);
wait(NULL);
if (rm_rf(sv.svrundir) != 0)
return 1;
break;
}
return 0;
}