-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathprocess_ctrl.c
387 lines (342 loc) · 9.15 KB
/
process_ctrl.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Copyright (c) 2020 YuQing <384681@qq.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the Lesser GNU General Public License, version 3
* or later ("LGPL"), as published by the Free Software Foundation.
*
* 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.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "shared_func.h"
#include "logger.h"
#include "process_ctrl.h"
int get_pid_from_file(const char *pidFilename, pid_t *pid)
{
char buff[32];
int64_t file_size;
int result;
if (access(pidFilename, F_OK) != 0) {
return errno != 0 ? errno : EPERM;
}
file_size = sizeof(buff);
if ((result=getFileContentEx(pidFilename, buff, 0, &file_size)) != 0) {
return result;
}
*pid = strtol(buff, NULL, 10);
if (*pid == 0) {
return EINVAL;
}
return 0;
}
int write_to_pid_file(const char *pidFilename)
{
char buff[32];
int len;
len = sprintf(buff, "%d", (int)getpid());
return writeToFile(pidFilename, buff, len);
}
int delete_pid_file(const char *pidFilename)
{
int result;
pid_t pid = 0;
if ((result=get_pid_from_file(pidFilename, &pid)) != 0) {
return result;
}
if (pid != getpid()) {
fprintf(stderr, "pid file: %s not mine, pid: %d != mine: %d",
pidFilename, (int)pid, (int)getpid());
return ESRCH;
}
if (unlink(pidFilename) == 0) {
return 0;
}
else {
fprintf(stderr, "unlink file: %s fail, "
"errno: %d, error info: %s!\n",
pidFilename, errno, strerror(errno));
return errno != 0 ? errno : ENOENT;
}
}
static int do_stop(const char *pidFilename, const bool bShowError, pid_t *pid)
{
int result;
if ((result=get_pid_from_file(pidFilename, pid)) != 0) {
if (bShowError) {
if (result == ENOENT) {
fprintf(stderr, "pid file: %s not exist!\n", pidFilename);
}
else {
fprintf(stderr, "get pid from file: %s fail, " \
"errno: %d, error info: %s\n",
pidFilename, result, strerror(result));
}
}
return result;
}
if (kill(*pid, SIGTERM) == 0) {
return 0;
}
else {
result = errno != 0 ? errno : EPERM;
if (bShowError || result != ESRCH) {
fprintf(stderr, "kill pid: %d fail, errno: %d, error info: %s\n",
(int)*pid, result, strerror(result));
}
return result;
}
}
int process_stop_ex(const char *pidFilename,
const bool bShowError, bool *force)
{
#define MAX_WAIT_COUNT 300
pid_t pid = 0;
int result;
int sig;
int i;
*force = false;
if ((result=do_stop(pidFilename, bShowError, &pid)) != 0) {
return result;
}
fprintf(stderr, "waiting for pid [%d] exit ...\n", (int)pid);
for (i=0; i<MAX_WAIT_COUNT; i++) {
sig = (i % 10 == 0) ? SIGTERM : 0;
if (kill(pid, sig) != 0) {
break;
}
fc_sleep_ms(100);
}
if (i == MAX_WAIT_COUNT) {
if (kill(pid, SIGKILL) == 0) {
fprintf(stderr, "waiting for pid [%d] exit timeout, "
"force kill!\n", (int)pid);
*force = true;
fc_sleep_ms(100);
}
}
fprintf(stderr, "pid [%d] exit.\n\n", (int)pid);
return 0;
}
int process_restart(const char *pidFilename)
{
const bool bShowError = false;
bool force;
int result;
result = process_stop_ex(pidFilename, bShowError, &force);
if (result == ENOENT || result == ESRCH) {
result = 0;
} else if (result == 0) {
if (force) {
sleep(1);
}
fprintf(stderr, "starting ...\n");
}
return result;
}
static const char *process_get_exename(const char* program)
{
const char *exename;
exename = strrchr(program, '/');
if (exename != NULL) {
return exename + 1;
}
else {
return program;
}
}
static const char *get_exename_by_pid(const pid_t pid, char *buff,
const int buff_size, int *result)
{
char cmdfile[MAX_PATH_SIZE];
int64_t cmdsz;
cmdsz = buff_size;
sprintf(cmdfile, "/proc/%d/cmdline", pid);
if ((*result=getFileContentEx(cmdfile, buff, 0, &cmdsz)) != 0) {
fprintf(stderr, "read file %s fail, errno: %d, error info: %s\n",
cmdfile, *result, strerror(*result));
return NULL;
}
return process_get_exename(buff);
}
int process_start(const char* pidFilename)
{
pid_t pid = 0;
int result;
if ((result=get_pid_from_file(pidFilename, &pid)) != 0) {
if (result == ENOENT) {
return 0;
}
else {
fprintf(stderr, "get pid from file: %s fail, " \
"errno: %d, error info: %s\n",
pidFilename, result, strerror(result));
return result;
}
}
if (kill(pid, 0) == 0) {
if (access("/proc", F_OK) == 0) {
char cmdline[MAX_PATH_SIZE];
char argv0[MAX_PATH_SIZE];
const char *exename1, *exename2;
exename1 = get_exename_by_pid(pid, cmdline, sizeof(cmdline), &result);
if (exename1 == NULL) {
return result;
}
exename2 = get_exename_by_pid(getpid(), argv0, sizeof(argv0), &result);
if (exename2 == NULL) {
return result;
}
if (strcmp(exename1, exename2) == 0) {
fprintf(stderr, "process %s already running, pid: %d\n",
argv0, (int)pid);
return EEXIST;
}
return 0;
}
else {
fprintf(stderr, "process already running, pid: %d\n", (int)pid);
return EEXIST;
}
}
else if (errno == ENOENT || errno == ESRCH) {
return 0;
}
else {
result = errno != 0 ? errno : EPERM;
fprintf(stderr, "kill pid: %d fail, errno: %d, error info: %s\n",
(int)pid, errno, strerror(errno));
return result;
}
}
int process_exist(const char *pidFilename, pid_t *pid)
{
int result;
if ((result=get_pid_from_file(pidFilename, pid)) != 0) {
if (result == ENOENT) {
return result;
}
else {
fprintf(stderr, "get pid from file: %s fail, " \
"errno: %d, error info: %s\n",
pidFilename, result, strerror(result));
return result;
}
}
if (kill(*pid, 0) == 0) {
return 0;
}
else if (errno == ENOENT || errno == ESRCH) {
return ENOENT;
}
else {
result = errno != 0 ? errno : EPERM;
fprintf(stderr, "kill pid: %d fail, errno: %d, error info: %s\n",
(int)*pid, result, strerror(result));
return result;
}
}
int get_base_path_from_conf_file_ex(const char *filename, char *base_path,
const int path_size, const int noent_log_level)
{
char *pBasePath;
string_t path_string;
IniContext iniContext;
int result;
if ((result=iniLoadFromFileEx(filename, &iniContext,
FAST_INI_ANNOTATION_DISABLE, NULL, 0,
FAST_INI_FLAGS_NONE)) != 0)
{
logError("file: "__FILE__", line: %d, " \
"load conf file \"%s\" fail, ret code: %d", \
__LINE__, filename, result);
return result;
}
do
{
pBasePath = iniGetStrValue(NULL, "base_path", &iniContext);
if (pBasePath == NULL || *pBasePath == '\0')
{
logError("file: "__FILE__", line: %d, " \
"conf file \"%s\" must have item " \
"\"base_path\"!", __LINE__, filename);
result = ENOENT;
break;
}
FC_SET_STRING(path_string, pBasePath);
normalize_path(NULL, &path_string, base_path, path_size);
chopPath(base_path);
if (!fileExists(base_path))
{
result = errno != 0 ? errno : ENOENT;
log_it_ex(&g_log_context, noent_log_level,
"file: "__FILE__", line: %d, "
"\"%s\" can't be accessed, error info: %s",
__LINE__, base_path, STRERROR(result));
break;
}
if (!isDir(base_path))
{
logError("file: "__FILE__", line: %d, " \
"\"%s\" is not a directory!", \
__LINE__, base_path);
result = ENOTDIR;
break;
}
} while (0);
iniFreeContext(&iniContext);
return result;
}
int process_action(const char *pidFilename, const char *action, bool *stop)
{
int result;
pid_t pid;
*stop = false;
if (action == NULL)
{
return 0;
}
if (strcmp(action, "stop") == 0)
{
*stop = true;
return process_stop(pidFilename);
}
else if (strcmp(action, "status") == 0)
{
*stop = true;
result = process_exist(pidFilename, &pid);
switch (result) {
case 0:
printf("Running, pid: %d\n", (int)pid);
break;
case ENOENT:
printf("NOT running\n");
break;
default:
printf("Unkown status\n");
break;
}
return result;
}
else if (strcmp(action, "restart") == 0)
{
return process_restart(pidFilename);
}
else if (strcmp(action, "start") == 0)
{
return process_start(pidFilename);
}
else
{
fprintf(stderr, "invalid action: %s\n", action);
return EINVAL;
}
}