-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
359 lines (309 loc) · 7.96 KB
/
main.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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* main.c
* Copyright (C) Damiano Scaramuzza 2012 <[email protected]>
*
* psentinel 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.
*
* psentinel 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 <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 <stdarg.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include "psentinel.h"
#include "networking.h"
#include "checkers.h"
#include "general.h"
#include "minischeduler.h"
/*Global settings */
unsigned char to_demonize = 0;
unsigned char to_disable = 0;
unsigned char to_command_mode = 0;
unsigned char to_printhelp = 1;
char * config_file = NULL;
char * pid_file = NULL;
char * socket_file = NULL;
char * default_working_dir =NULL;
/* When a SIGUSR1 signal arrives, set this variable. */
volatile sig_atomic_t sleep_status = 1;
unsigned short int pausable_chk_port = 0;
unsigned short int chk_port = 0;
int log_level = LOG_ERR;
configuration connections;
char * safe_getcwd()
{
int size = 100;
char *buffer = (char *) malloc(size*sizeof(char));
while (1)
{
char *value = getcwd (buffer, size);
if (value != 0)
return buffer;
size *= 2;
free (buffer);
buffer = (char *) malloc (size*sizeof(char));
}
}
/*
This function is called for daemonize the proces only
(option -D)
*/
int daemonize(configuration * connections)
{
FILE * file;
char saved_pid[11];
int status;
/* Our process ID and Session ID */
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0)
{
return (DAEMONIZE_FAILURE);
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0)
{
return (DAEMONIZE_SUCCESS);
}
/* Change the file mode mask */
umask(0);
// ok we save child pid
connections->running_pid = getpid();
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0)
{
/* Log the failure */
return (DAEMONIZE_FAILURE);
}
default_working_dir=safe_getcwd();
/* Change the current working directory */
if ((chdir("/")) < 0)
{
/* Log the failure */
return (DAEMONIZE_FAILURE);
}
//update pid value in pid file
if (pid_file)
{
file = fopen(pid_file, "w+");
if (!file)
{
printf("ERROR: Cannot open %s file.Check permission \n", pid_file);
return (DAEMONIZE_FAILURE);
}
sprintf(saved_pid, "%d", connections->running_pid);
status = fputs(saved_pid, file);
if (status == EOF)
{
printf("ERROR: Cannot write on %s file.Check permission \n", pid_file);
return (DAEMONIZE_FAILURE);
}
fclose(file);
}
else
{
printf("ERROR: You have to specify a pid file.Check configuration \n");
return (DAEMONIZE_FAILURE);
}
/* Close out the standard file descriptors but we reopen toward /dev/null
* to avoid that exec command without stdio/stdin/stderr could be problematic
* often commands complains without stdout/stdin/stderr */
freopen("/dev/null", "r", stdin);
freopen("/dev/null", "w", stdout);
freopen("/dev/null", "w", stderr);
/* Ok we are in daemon */
return (DAEMONIZE_INDAEMON);
}
/*General log function to abstract the log facility pointing to syslog or
* printf depending on to_demonize setting
*/
void Log(int priority, char * format, ...)
{
va_list ap;
va_start (ap, format);
if (to_demonize == 0)
vprintf(format, ap);
else
vsyslog(priority, format, ap);
va_end (ap);
}
void sleep_signal(int sig)
{
sleep_status = !sleep_status;
if (sleep_status)
Log(LOG_DEBUG, "Sleep enabled\n");
else
Log(LOG_DEBUG, "Sleep disabled\n");
}
//function used to clear resources
void term_signal(int sig)
{
int status;
Log(LOG_DEBUG, "SIGTERM trapped.Clean all things\n");
status = clean_all(&connections);
status = remove(pid_file);
if (status)
{
status = errno;
Log(LOG_ERR, "Error removing PID file %s, Err(%s)", pid_file, strerror(status));
}
status = remove(socket_file);
if (status)
{
status = errno;
Log(LOG_ERR, "Error removing Socket file %s, Err(%s)", socket_file, strerror(status));
}
exit(status);
}
void pipe_signal(int sig)
{
Log(LOG_DEBUG, "SIGPIPE trapped.Ignore it\n");
}
int initialize_timers(configuration * connections)
{
int configured_ports = connections->last_slot;
int i;
int status = 0;
for (i = 0; i <= configured_ports; i++)
{
if (connections->single_conn[i].timer.chk_function)
{
status = make_timer(&(connections->single_conn[i]));
if (status)
{
Log(LOG_ERR, "Error creating timer for service %s", connections->single_conn[i].service_name);
}
}
}
return status;
}
int main(int argc, char **argv)
{
int status;
struct sigaction usr_action;
struct sigaction term_action;
struct sigaction pipe_action;
sigset_t block_mask;
int socks_status;
connections.last_slot = -1;
connections.running_pid = getpid();
status = init(argc, argv, &connections);
if (status == INIT_FAILURE)
{
if (to_printhelp == 1)
print_help(argc, argv);
exit(EXIT_FAILURE);
}
if (status == INIT_SUCCESS && to_printhelp == 1)
{
print_help(argc, argv);
exit(EXIT_SUCCESS);
}
if (to_command_mode)
{
status = execute_command(&connections);
exit(status);
}
Log(LOG_NOTICE, "Program started by User %d \n", getuid());
/* Daemon-specific initialization */
if (to_demonize)
{
status = daemonize(&connections);
switch (status)
{
case DAEMONIZE_FAILURE:
/*something gone wrong exit from parent */
printf("Failed to daemonize.Check permissions\n");
exit(EXIT_FAILURE);
break;
case DAEMONIZE_SUCCESS:
/*ok we are the parent, we can exit gracefully */
Log(LOG_INFO, "Starting as a daemon.Parent exit\n");
printf("Starting as a daemon.Parent exit\n");
exit(EXIT_SUCCESS);
break;
case DAEMONIZE_INDAEMON:
Log(LOG_INFO, "Running as Daemon.Log level: %d\n",log_level);
setlogmask(LOG_UPTO (log_level));
openlog(PROGRAM_NAME, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
Log(LOG_DEBUG,"Working dir: %s\n",default_working_dir);
break;
}
}
else
{
default_working_dir=safe_getcwd();
Log(LOG_DEBUG,"Working dir: %s\n",default_working_dir);
}
/* Establish the signal handler. */
sigfillset(&block_mask);
usr_action.sa_handler = sleep_signal;
usr_action.sa_mask = block_mask;
usr_action.sa_flags = 0;
sigaction(SIGUSR1, &usr_action, NULL);
term_action.sa_handler = term_signal;
term_action.sa_mask = block_mask;
term_action.sa_flags = 0;
sigaction(SIGTERM, &term_action, NULL);
pipe_action.sa_handler = pipe_signal;
pipe_action.sa_mask = block_mask;
pipe_action.sa_flags = 0;
sigaction(SIGPIPE, &pipe_action, NULL);
Log(LOG_INFO, "Init networking\n");
status = initialize_network(&connections);
Log(LOG_INFO, "Init timers\n");
status = initialize_timers(&connections);
Log(LOG_INFO,"Init data file\n");
status = init_datafile(&connections);
/* The Big Loop */
while (1)
{
Log(LOG_DEBUG, "Main loop\n");
rebuild_connection_set(&connections);
socks_status = select(connections.max_socket + 1, &connections.socket_set, NULL, NULL, NULL);
if (socks_status < 0)
{
status = errno;
/* We had an error, signal it */
if (status != EINTR)
{
Log(LOG_ERR, "Error in select socket (%s) \n", strerror(status));
exit(EXIT_FAILURE);
}
}
else
if (socks_status == 0)
{
/* Nothing to do probably a staying alive msg in debug mode */
}
else
{
read_sockets(&connections);
}
}
closelog();
exit(EXIT_SUCCESS);
}