-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.c
More file actions
254 lines (212 loc) · 5.51 KB
/
init.c
File metadata and controls
254 lines (212 loc) · 5.51 KB
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
/*
* The Minger Email Address Verification Daemon
* Copyright (C) 2012 David De Grave <david@ledav.net>
*
* http://sourceforge.net/projects/mingerd
*
* 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 http://www.gnu.org/licenses/.
*/
#include "init.h"
#include "sigevents.h"
#include "common.h"
#include "log.h"
#include "main.h"
#include "emails.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <sys/file.h>
#include <errno.h>
static int init_pid = 0;
static bool debug = false;
static int makepid(char *file)
{
int fd, pidtmp, readlen;
char pid[7];
if ( (fd = open(file, O_CREAT|O_RDWR, 0644)) < 0 ) return -1;
if ( flock(fd, LOCK_NB|LOCK_EX) < 0 ) {
if ( (readlen = read(fd, pid, sizeof(pid)-1)) < 0 ) {
close(fd);
return -1;
}
pid[readlen] = 0;
if ( ! (pidtmp = atoi(pid)) ) {
pidtmp = -1;
errno = ENODATA;
}
close(fd);
return pidtmp;
}
sprintf(pid, "%i\n", getpid());
write(fd, pid, strlen(pid));
/* FD is kept open and locked ... */
return 0;
}
static int killpidfile(const char *file, int sig)
/* <0 - Error and errno is set.
* =0 - Corresponding process is not running.
* >0 - Pid of the process.
*/
{
int fd, pidtmp = 0, len;
char pid[7];
if ( (fd = open(file, O_RDONLY)) >= 0 ) {
/* File Exists */
if ( flock(fd, LOCK_NB|LOCK_EX) < 0 ) {
/* File is locked */
if ( (len = read(fd, pid, sizeof(pid)-1)) >= 0 ) {
/* File must contain the pid */
pid[len] = 0;
if ( ! (pidtmp = atoi(pid)) ) {
pidtmp = -1;
errno = ENODATA;
}else{
/* File contains a pid. Trying to send the signal... */
if ( kill(pidtmp, sig) < 0 ) pidtmp = -1;
}
}
else pidtmp = -1; /* Keep errno of 'read' */
}
close(fd);
}
return pidtmp;
}
static pid_t startdaemon(void)
{
register int i;
pid_t cid;
if ( (cid = fork()) < 0 ) return -1; /* ERROR */
else
if ( cid > 0 ) exit(0); /* PARENT */
/* CHILD */
setsid();
chdir("/");
umask(0);
for ( i=0 ; i < NOFILE ; i++ ) close(i);
errno = 0;
#ifdef SIGTTOU
signal(SIGTTOU, SIG_IGN);
#endif
#ifdef SIGTTIN
signal(SIGTTIN, SIG_IGN);
#endif
#ifdef SIGTSTP
signal(SIGTSTP, SIG_IGN);
#endif
return getpid();
}
static void remove_pid(void)
{
if ( getpid() == init_pid ) unlink(PID_FILE);
}
static void print_help(const char *proc)
{
printf(
P_NAME_LONG" v"P_VERSION" made by David De Grave.\n"
"\n"
"Usage:\n"
" %s [-hdu]\n"
"\n"
"Options:\n"
" -h Display this help screen.\n"
" -d Turn the debugging mode on (no daemonize + more logging on terminal).\n"
" -u Unload itself cleanly from memory.\n"
"\n", proc);
}
static void process_parameters(int argc, char **argv)
{
char opt;
int r;
while ( (opt = getopt(argc, argv, "dhu")) > 0 ) {
switch ( opt ) {
case 'd':
debug = true;
config.logpriority = PL_DEBUG;
break;
case 'u':
if ( ! (r = killpidfile(PID_FILE, SIGTERM)) ) {
puts(P_NAME_SHORT" is not running ...");
exit(1);
}else
if ( r > 0 ) {
while ( kill(r, 0) == 0 ) sleep(1); /* Wait until daemon is unloaded */
puts(P_NAME_SHORT" unloaded ...");
}else{
printf("Error reading %s: %m\n", PID_FILE);
exit(2);
}
exit(0);
default:
print_help(argv[0]);
exit(0);
}
}
}
int main(int argc, char **argv)
{
int r;
if ( read_config(&config, CFG_FILE) < 0 ) {
printf("Error reading configuration file "CFG_FILE" !\n");
exit(10);
}
if ( argc > 1 ) process_parameters(argc, argv);
if ( ! debug ) {
if ( (init_pid = startdaemon()) < 0 ) {
perror("startdaemon()");
exit(11);
}
if ( (stdlog = startlog(LOG_FILE, P_NAME_SHORT, PL_PID, config.logpriority)) == NULL ) {
/* BUG: In case logfile cannot be open, no errors are shown due to the previous
* call to startdaemon() who closes all the file descriptors
*/
perror("startlog()");
exit(12);
}
}else{
init_pid = getpid();
stdlog = restartlog(1, P_NAME_SHORT, PL_PID, config.logpriority);
}
if ( (r = makepid(PID_FILE)) < 0 ) {
printlog(stdlog, PL_ERR, "Error creating pid file ...");
exit(13);
}else
if ( r > 0 ) {
printlog(stdlog, PL_ERR, P_NAME_SHORT" is already running ... (pid %d)", r);
exit(14);
}
printlog(stdlog, PL_NOTICE, P_NAME_SHORT" v"P_VERSION" is running ...");
atexit(remove_pid);
install_sigevents();
if ( load_sysuser_cache(PASSWDF) < 0
|| load_aliases_cache(ALIASESF) < 0 ) {
printlog(stdlog, PL_ERR, "Error loading caches !");
exit(15);
}
printlog(stdlog, PL_DEBUG, "Active config: "
"listenip[%s] listenport[%d] logpriority[%d] anonymous[%d] secret[%s]"
, config.listenip, config.listenport, config.logpriority, config.anonymous
, config.secret);
do {
r = mingerd_process(&config);
}
while ( r > 0 );
printlog(stdlog, PL_NOTICE, P_NAME_SHORT" ended ...");
return r < 0 ? -r : 0;
}