-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
svctl.c
153 lines (139 loc) · 3.74 KB
/
svctl.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* svctl -- Interact with sysmgr services
*
* Copyright (C) 2020 Cem Keylan <cem@ckyln.com>
*
* 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 <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <dirent.h>
#include <libgen.h>
#include "util.h"
#include "config.h"
static char *argv0;
void
usage(int exitnum)
{
printf("usage: %s [operation] [service...]\n\n", argv0);
fputs(" Operations:\n"
" start/stop/restart Start/stop/restart services\n"
" once Start services once\n"
" status Check service statuses\n"
" up/down Same as start/stop\n\n"
"sysmgr-"VERSION"\n", stdout);
exit(exitnum);
}
int check_rundir(char *dir)
{
DIR *rundirectory;
rundirectory = opendir(dir);
if (rundirectory == NULL)
return -1;
closedir(rundirectory);
return 0;
}
int handle_service(char *operation, char *name)
{
struct service sv;
sv_init(&sv, basename(name));
pid_t pid;
if (strcmp(operation, "start") == 0 || strcmp(operation, "up") == 0) {
if(sv_check(&sv, 1) == 0)
return 0;
return rm_rf(sv.svrundir);
} else if (strcmp(operation, "stop") == 0 || strcmp(operation, "down") == 0) {
if(sv_check(&sv, 1) != 0)
return 0;
pid = getsyspid(&sv);
switch (pid) {
case -1:
perror(NULL);
return -1;
default:
sv_writelock(sv.lockfile, SIGTERM);
kill(pid, SIGTERM);
unlink(sv.syspidfile);
unlink(sv.pidfile);
}
} else if (strcmp(operation, "kill") == 0) {
pid = getsyspid(&sv);
switch (pid) {
case -1:
perror(NULL);
return 1;
default:
sv_writelock(sv.lockfile, SIGKILL);
kill(pid, SIGUSR1);
unlink(sv.syspidfile);
unlink(sv.pidfile);
}
} else if (strcmp(operation, "once") == 0) {
if(sv_check(&sv, 1) == 0) {
if (sv_writelock(sv.lockfile, 0) != 0) {
perror(sv.lockfile);
die("Failed to write lock.");
}
} else {
rm_rf(sv.svrundir);
sleep(1);
return sv_writelock(sv.lockfile, 0);
}
} else if (strcmp(operation, "restart") == 0) {
handle_service("stop", name);
while (1) {
if (sv_check(&sv, 1) != 0)
break;
}
handle_service("start", name);
} else if (strcmp(operation, "stat") == 0 || strcmp(operation, "status") == 0) {
if (sv_check(&sv, 1) < 0) {
fprintf(stderr, "%s: DOWN\n", name);
return -1;
} else
fprintf(stderr, "%s: UP\n", name);
} else
die("Unknown operation: %s", operation);
return 0;
}
int
main(int argc, char *argv[])
{
/* Variable used by other functions but not in this file itself */
(void)(sysdir_default);
char *rundir;
argv0 = argv[0];
int i, exitnum;
if (argc < 2 || strncmp(argv[1], "-", 1) == 0)
usage(0);
else if (argc < 3)
usage(1);
/* Check if the RUNDIR exists */
rundir = getenv_fallback("RUNDIR", rundir_default);
if (check_rundir(rundir) != 0)
die("%s could not be found, are you sure sysmgr is running?", rundir);
if (strncmp(argv[1], "stat", 4) == 0) {
exitnum = 0;
for (i=2; i < argc; i++)
if(handle_service(argv[1], argv[i]) != 0)
exitnum = 1;
exit(exitnum);
}
for (i=2; i < argc; i++)
if (handle_service(argv[1], argv[i]) != 0)
die("Couldn't %s %s", argv[1], argv[i]);
return 0;
}