-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyscd.c
executable file
·84 lines (76 loc) · 1.78 KB
/
myscd.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
#include "myscd.h"
struct config g_config;
char *g_config_file = NULL;
extern char *optarg;
int main(int argc, char *argv[])
{
char *command = NULL;
char optchar;
int status;
while ((optchar = getopt(argc, argv, "c:k:")) != -1)
{
switch (optchar)
{
case 'c':
g_config_file = strdup(optarg);
break;
case 'k':
command = strdup(optarg);
break;
default:
usage();
return 0;
}
}
openlog(g_config_file, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
if (g_config_file == NULL || command == NULL)
{
usage();
return 0;
}
config_parser(&g_config);
openlog(g_config.syslog_identity, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
status = check_daemon();
if (strncmp("start", command, 5) == 0)
{
if (!status)
{
syslog(LOG_NOTICE, "start_daemon()");
start_daemon();
}
else
fprintf(stderr, "already running\n");
}
else if (strncmp("stop", command, 4) == 0)
{
if (status)
{
syslog(LOG_NOTICE, "stop_daemon()");
stop_daemon();
}
else
fprintf(stderr, "not running\n");
}
else if (strncmp("restart", command, 7) == 0)
{
if (status)
{
syslog(LOG_NOTICE, "restart");
stop_daemon();
sleep(1);
start_daemon();
}
else
{
fprintf(stderr, "not running\ntrying to start...\n");
syslog(LOG_NOTICE, "start_daemon()");
start_daemon();
}
}
else
{
fprintf(stderr, "unknown command\n");
usage();
}
return 0;
}