-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathappf_main.c
473 lines (390 loc) · 9.9 KB
/
appf_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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*****************************************************************************/
/* _____ _ ______ _____ */
/* / ___| | | | ___ \ __ \ */
/* \ `--. _ __ ___ __ _ _ __| |_| |_/ / | \/ */
/* `--. \ '_ ` _ \ / _` | '__| __| /| | __ */
/* /\__/ / | | | | | (_| | | | |_| |\ \| |_\ \ */
/* \____/|_| |_| |_|\__,_|_| \__\_| \_|\____/ Inc. */
/* */
/*****************************************************************************/
/* */
/* copyright 2016 by SmartRG, Inc. */
/* Santa Barbara, CA */
/* */
/*****************************************************************************/
/* */
/* Author: Colin Whittaker */
/* */
/* Purpose: Application Framework Library for building daemons */
/* */
/*****************************************************************************/
#include <appf.h>
af_daemon_t _af_default_daemon = {
};
af_daemon_t *_af_daemon = &_af_default_daemon;
af_daemon_t *af_daemon_set( af_daemon_t *ctx )
{
af_daemon_t *ret = _af_daemon;
_af_daemon = ctx;
return ret;
}
int af_log_signal_open()
{
int fd;
char fn[128];
strcpy( fn, "/var/log/" );
strcat( fn, _af_daemon->appname );
strcat( fn, ".log" );
fd = open( fn, O_RDWR | O_APPEND | O_CREAT, 0644 );
return fd;
}
void af_log_signal( int fd, char *data )
{
if ( write( fd, data, strlen( data ) ) != strlen(data) )
{
// an error happened.
}
}
char *af_itoa( int num, char *str, int base )
{
int i = 0;
int isNegative = 0;
/* Handle 0 explicitely, otherwise empty string is printed for 0 */
if ( num == 0 )
{
str[i++] = '0';
str[i] = '\0';
return str;
}
// In standard itoa(), negative numbers are handled only with
// base 10. Otherwise numbers are considered unsigned.
if ( num < 0 && base == 10 )
{
isNegative = 1;
num = -num;
}
// Process individual digits
while ( num != 0 )
{
int rem = num % base;
str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
num = num/base;
}
// If number is negative, append '-'
if ( isNegative )
str[i++] = '-';
str[i] = '\0'; // Append string terminator
// Reverse the string
//reverse(str, i);
//void reverse(char str[], int length)
{
int start = 0;
int end = i -1;
while (start < end)
{
// swap(*(str+start), *(str+end));
{
char x = *(str+start);
*(str+start) = *(str+end);
*(str+end) = x;
}
start++;
end--;
}
}
return str;
}
void af_seg_fault_handler (int signo, siginfo_t* p_siginfo, void* dummy3)
{
// af_log_print( LOG_ERR, "=== Received %s", af_signal_to_str(signo));
int fd;
/* We will traverse this handler twice, once for the
* error signal (SIGSEGV, SIGBUS, SIGFPE, etc), and
* a second time with a SIGABRT due to the call to
* abort() below.
*/
if (signo == SIGABRT) {
// This has to be a 'return'
// A call to exit() will not produce a core file
return;
}
fd = af_log_signal_open();
if ( fd > 0 )
{
af_log_signal( fd, "[af_seg_fault_handler]\n" );
#ifdef BACKTRACE_AVAILABLE
{
int size, i;
void *array[MAX_FRAMES];
char buf[32];
// char **strings;
size = backtrace (array, MAX_FRAMES);
// strings = backtrace_symbols (array, size);
af_log_signal( fd, "Backtrace.....\n" );
for (i = 0; i < size; i++)
{
// af_log_print( ILOG_ERR, "%s", strings[i] );
// af_signal_log( fd, strings[i] );
af_log_signal( fd, af_itoa( (int)(ptrdiff_t)array[i], buf, 16 ) );
af_log_signal( fd, "\n" );
}
}
#endif
close(fd);
}
closelog();
/* generate a core file */
// af_log_print( ILOG_ERR, "Generating core file and exiting");
abort();
exit(1);
}
void _af_setup_signals( void )
{
struct sigaction act;
// Set signals to proper defaults
act.sa_sigaction = af_seg_fault_handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
if (sigaction(SIGSEGV, &act, (struct sigaction *)NULL) < 0)
{
af_fatal("sigaction() failed, errno=%d (%s)", errno, strerror(errno) );
}
if (sigaction(SIGABRT, &act, (struct sigaction *)NULL) < 0)
{
af_fatal("sigaction() failed, errno=%d (%s)", errno, strerror(errno) );
}
if (sigaction(SIGILL, &act, (struct sigaction *)NULL) < 0)
{
af_fatal("sigaction() failed, errno=%d (%s)", errno, strerror(errno) );
}
if (sigaction(SIGFPE, &act, (struct sigaction *)NULL) < 0)
{
af_fatal("sigaction() failed, errno=%d (%s)", errno, strerror(errno) );
}
if (sigaction(SIGBUS, &act, (struct sigaction *)NULL) < 0)
{
af_fatal("sigaction() failed, errno=%d (%s)", errno, strerror(errno) );
}
if (sigaction(SIGSYS, &act, (struct sigaction *)NULL) < 0)
{
af_fatal("sigaction() failed, errno=%d (%s)", errno, strerror(errno) );
}
signal(SIGINT, _af_daemon->sig_handler);
signal(SIGTERM, _af_daemon->sig_handler);
signal(SIGQUIT, _af_daemon->sig_handler);
signal(SIGHUP, _af_daemon->sig_handler);
/* always ignore SIGPIPE */
signal(SIGPIPE, SIG_IGN );
}
int af_enable_core_dump( rlim_t limit )
{
int rc;
struct rlimit rlim;
if ( limit == 0 )
limit = CORE_LIMIT;
rlim.rlim_cur = limit;
rlim.rlim_max = limit;
rc = setrlimit(RLIMIT_CORE, &rlim);
if (rc != 0) {
af_log_print( LOG_ERR, "ERROR: setrlimit() returned %d", rc);
}
return rc;
}
void _af_daemonize( void )
{
int cnt, fd;
pid_t pid, pgid;
if ((pid = fork()) < 0)
{
af_fatal("Daemonizing failed: Can not spawn child "
"process, exiting...");
}
else if (pid == 0)
{ /* child */
/* create session and set process group ID */
if ((pgid = setsid()) < 0)
af_fatal("Daemonizing failed: Can not setsid, exiting...");
/* get rid of our controlling terminal */
if ((pid = fork()) < 0)
{
af_fatal("Daemonizing failed: Can not respawn "
"child process, exiting...");
}
else if (pid == 0)
{
/* child */
if (chdir("/") != 0)
{
af_fatal("Daemonizing failed: Can not change "
"working directory, exiting...");
}
(void)umask(077);
/* close all open files */
for (cnt = 0; cnt < sysconf(_SC_OPEN_MAX); cnt++)
close(cnt);
/* open stdin, stdout, and stderr */
if ((fd = open("/dev/null", O_RDWR, 0)) == 0) {
for (cnt = 1; cnt < 3; cnt++)
if (dup(fd) != cnt)
{
af_fatal("Can not open file "
"descriptor %d, exiting...", fd);
}
}
else
af_fatal("Can not open file descriptor %d, exiting...", cnt);
}
else
{
/* parent (controlling terminal) */
exit(0);
}
}
else
{
/* parent */
exit(0);
}
/* demonized */
af_enable_core_dump(CORE_LIMIT);
}
int _af_write_pid( void )
{
FILE *fh;
/* make check that pid_file has been set */
if ( _af_daemon->pid_file == NULL)
{
return 0;
}
if ( af_exec_is_running( _af_daemon->pid_file, _af_daemon->appname ) )
{
af_log_print( LOG_EMERG, "another instance of %s is already running, exiting", _af_daemon->appname );
return -1;
}
else
{
struct stat pid_stat;
/* delete the old PID file if it exists */
if ( stat( _af_daemon->pid_file, &pid_stat ) == 0 )
{
if ( S_ISREG( pid_stat.st_mode ))
{
af_log_print( APPF_MASK_MAIN+LOG_INFO, "PID file found, but process dead, removing");
unlink( _af_daemon->pid_file );
}
}
}
/* create the pidfile */
if ( (fh = fopen( _af_daemon->pid_file, "w"))==NULL)
{
af_log_print( LOG_ERR, "Failed to create pid file %s", _af_daemon->pid_file );
return -1;
}
fprintf(fh, "%d", getpid());
fclose(fh);
chmod( _af_daemon->pid_file, 0644);
return 0;
}
extern void _af_log_init( void );
int af_daemon_start( void )
{
// Check daemon context is set.
if ( _af_daemon == NULL )
{
printf("Fatal error af_set_daemon() not called");
exit(-1);
}
// setup signals
_af_setup_signals( );
// daemonize
if ( _af_daemon->daemonize )
{
_af_daemonize( );
}
_af_daemon->timers.fd = -1;
// setup logging
_af_log_init( );
// Create PID file
return _af_write_pid( );
}
char *_af_get_next_arg( char *string, int *len )
{
char *argv;
char quote = 0;
char iquote = 0;
/* find the begining of a word */
while ( *string && isspace(*string) )
{
string++;
}
if ( *string == '"' || *string == '\'' )
{
quote = *string;
string++;
}
*len = 0;
if ( *string == '\0' )
{
/* There is no argument. */
return NULL;
}
argv = string;
/* find the end of the word */
while ( *string )
{
if ( !quote && !iquote && isspace(*string) )
break;
if ( *string == quote )
{
break;
}
// Check for internal quotes and keep them
if ( *string == '"' || *string == '\'' )
{
if ( iquote )
{
if ( iquote == *string )
{
// terminate it
iquote = 0;
}
// else it will just pass as a quote inside a quote.
// multiple nesting is not supported.
}
else
{
// begin quoted section, don't exit prematurely
iquote = *string;
}
}
string++;
(*len)++;
}
/* null terminate the arg */
*string = 0;
return argv;
}
int af_parse_argv( char *buffer, char **argv, int max )
{
int len, cnt;
char *ptr, *end;
if ( !buffer )
return 0;
cnt = 0;
ptr = buffer;
end = buffer+strlen(buffer);
do
{
ptr = _af_get_next_arg( ptr, &len );
if ( ptr == NULL )
{
// we have no arg
break;
}
argv[cnt++] = ptr;
ptr += (len + 1); //skip past the null termination
} while ( (ptr < end) && (cnt < max-1) );
// Last argv needs to be null.
argv[cnt] = NULL;
return cnt;
}