-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon.c
174 lines (149 loc) · 4.13 KB
/
daemon.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
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#define RUNNINGDIR "."
#define LOGFILE "logs.txt"
/* Global variables */
int lockfile;
/* Headers */
void logIt(char *msg);
void closeDaemon();
void restartDaemon();
void daemonSignalsHandler(int sig);
void daemonize();
void logIt(char *msg){
FILE *logfile;
time_t ltime; /* calendar time */
char timedate[26];
ltime=time(NULL); /* get current cal time */
strftime(timedate, 26, "%Y-%m-%d %H:%M:%S", localtime(<ime));
if ((logfile = fopen(LOGFILE, "a")) == NULL)
exit(EXIT_FAILURE);
fprintf(logfile, "[%s] %s\n", timedate, msg);
fclose(logfile);
}
void closeDaemon(){
logIt("[OK] Exiting...");
close(lockfile);
exit(EXIT_SUCCESS);
}
void restartDaemon(){
}
void daemonSignalsHandler(int sig){
switch(sig) {
case SIGHUP:
/* We restart the daemon */
logIt("[OK] Restarting...");
restartDaemon();
break;
case SIGTERM:
/* We stop the daemon */
closeDaemon();
exit(EXIT_SUCCESS);
break;
case SIGINT:
/* Ctrl c : we do nothing */
break;
case SIGCHLD:
/* Child terminates */
break;
}
}
void daemonize(){
/* Our process ID and Session ID */
pid_t pid, sid;
/* lock file */
char pidstr[10];
/* Signals */
sigset_t sig_proc;
struct sigaction action;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
printf("[ERROR] Fork failed.\n");
exit(EXIT_FAILURE);
}
/* If we got a good PID, then we can exit the parent process. */
if (pid > 0) {
printf("[OK] Launching...\n");
exit(EXIT_SUCCESS);
}
/* Change the file mode mask */
umask(027);
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
/* Log the failure */
printf("[ERROR] setsid() failed.\n");
exit(EXIT_FAILURE);
}
/* Change the current working directory */
if ((chdir(RUNNINGDIR)) < 0){
printf("[ERROR] Cannot chdir to the running directory.\n");
exit(EXIT_FAILURE);
}
/* Create pid file & lock it */
lockfile=open("pid.lock",O_RDWR|O_CREAT,0640);
if (lockfile < 0){
printf("[ERROR] Cannot open logfile. Exiting...\n");
logIt("[ERROR] Cannot open logfile. Exiting...");
exit(EXIT_FAILURE);
}
if (lockf(lockfile,F_TLOCK,0 ) < 0){
printf("[ERROR] The daemon seems to be launched already. Exiting...\n");
logIt("[ERROR] The daemon seems to be launched already. Exiting...");
exit(EXIT_FAILURE);
}
sprintf(pidstr, "%d\n", getpid());
write(lockfile, pidstr, strlen(pidstr));
/* Handle the signals */
sigemptyset(&sig_proc);
action.sa_mask=sig_proc;
action.sa_flags=0;
action.sa_handler = daemonSignalsHandler;
sigaction(SIGINT, &action,0);
sigaction(SIGTERM, &action,0);
sigaction(SIGHUP, &action,0);
sigaction(SIGCHLD, &action,0);
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
/* Daemon-specific initialization goes here */
logIt("[OK] Initialized.");
}
int main(int argc, char *args[]){
/* Checking arguments */
if (argc != 2 || (strcmp("start", args[1]) && strcmp("stop", args[1])
&& strcmp("restart", args[1]))){
printf("[Help] ./daemon <start|stop|restart>");
exit(EXIT_FAILURE);
}
/* Stopping the daemon */
if (!strcmp("stop", args[1])){
}
/* Restarting the daemon */
if (!strcmp("restart", args[1])){
}
/* Starting the daemon */
if (!strcmp("start", args[1])){
/* Daemonizing */
daemonize();
/* The Big Loop */
while (1) {
sleep(10);
exit(EXIT_SUCCESS);
}
}
/* Closing the daemon */
closeDaemon();
exit(EXIT_SUCCESS);
}