|
@@ -40,6 +40,12 @@ struct hijack_options {
|
40
|
40
|
|
41
|
41
|
static int daemonised = 0;
|
42
|
42
|
|
|
43
|
+static int signalled = 0;
|
|
44
|
+
|
|
45
|
+static void flag_signalled ( int signal __attribute__ (( unused )) ) {
|
|
46
|
+ signalled = 1;
|
|
47
|
+}
|
|
48
|
+
|
43
|
49
|
/**
|
44
|
50
|
* Log error message
|
45
|
51
|
*
|
|
@@ -363,13 +369,21 @@ static int listen_for_hijackers ( struct hijack_listener *listener,
|
363
|
369
|
|
364
|
370
|
logmsg ( LOG_INFO, "Listening on %s\n", listener->sun.sun_path );
|
365
|
371
|
|
366
|
|
- while ( 1 ) {
|
367
|
|
- /* Accept new connection */
|
|
372
|
+ while ( ! signalled ) {
|
|
373
|
+ /* Accept new connection, interruptibly */
|
|
374
|
+ siginterrupt ( SIGINT, 1 );
|
|
375
|
+ siginterrupt ( SIGHUP, 1 );
|
368
|
376
|
fd = accept ( listener->fd, NULL, 0 );
|
|
377
|
+ siginterrupt ( SIGINT, 0 );
|
|
378
|
+ siginterrupt ( SIGHUP, 0 );
|
369
|
379
|
if ( fd < 0 ) {
|
370
|
|
- logmsg ( LOG_ERR, "accept failed: %s\n",
|
371
|
|
- strerror ( errno ) );
|
372
|
|
- goto err;
|
|
380
|
+ if ( errno == EINTR ) {
|
|
381
|
+ continue;
|
|
382
|
+ } else {
|
|
383
|
+ logmsg ( LOG_ERR, "accept failed: %s\n",
|
|
384
|
+ strerror ( errno ) );
|
|
385
|
+ goto err;
|
|
386
|
+ }
|
373
|
387
|
}
|
374
|
388
|
|
375
|
389
|
/* Fork child process */
|
|
@@ -389,6 +403,8 @@ static int listen_for_hijackers ( struct hijack_listener *listener,
|
389
|
403
|
close ( fd );
|
390
|
404
|
}
|
391
|
405
|
|
|
406
|
+ logmsg ( LOG_INFO, "Stopped listening on %s\n",
|
|
407
|
+ listener->sun.sun_path );
|
392
|
408
|
return 0;
|
393
|
409
|
|
394
|
410
|
err:
|
|
@@ -527,7 +543,7 @@ static int daemonise ( const char *interface ) {
|
527
|
543
|
int main ( int argc, char **argv ) {
|
528
|
544
|
struct hijack_options options;
|
529
|
545
|
struct hijack_listener listener;
|
530
|
|
- struct sigaction sigchld;
|
|
546
|
+ struct sigaction sa;
|
531
|
547
|
|
532
|
548
|
/* Parse command-line options */
|
533
|
549
|
if ( parse_options ( argc, argv, &options ) < 0 )
|
|
@@ -547,11 +563,25 @@ int main ( int argc, char **argv ) {
|
547
|
563
|
}
|
548
|
564
|
|
549
|
565
|
/* Avoid creating zombies */
|
550
|
|
- memset ( &sigchld, 0, sizeof ( sigchld ) );
|
551
|
|
- sigchld.sa_handler = SIG_IGN;
|
552
|
|
- sigchld.sa_flags = SA_NOCLDWAIT;
|
553
|
|
- if ( sigaction ( SIGCHLD, &sigchld, NULL ) < 0 ) {
|
554
|
|
- logmsg ( LOG_ERR, "Could not set signal handler: %s",
|
|
566
|
+ memset ( &sa, 0, sizeof ( sa ) );
|
|
567
|
+ sa.sa_handler = SIG_IGN;
|
|
568
|
+ sa.sa_flags = SA_RESTART | SA_NOCLDWAIT;
|
|
569
|
+ if ( sigaction ( SIGCHLD, &sa, NULL ) < 0 ) {
|
|
570
|
+ logmsg ( LOG_ERR, "Could not set SIGCHLD handler: %s",
|
|
571
|
+ strerror ( errno ) );
|
|
572
|
+ exit ( 1 );
|
|
573
|
+ }
|
|
574
|
+
|
|
575
|
+ /* Set 'signalled' flag on SIGINT or SIGHUP */
|
|
576
|
+ sa.sa_handler = flag_signalled;
|
|
577
|
+ sa.sa_flags = SA_RESTART | SA_RESETHAND;
|
|
578
|
+ if ( sigaction ( SIGINT, &sa, NULL ) < 0 ) {
|
|
579
|
+ logmsg ( LOG_ERR, "Could not set SIGINT handler: %s",
|
|
580
|
+ strerror ( errno ) );
|
|
581
|
+ exit ( 1 );
|
|
582
|
+ }
|
|
583
|
+ if ( sigaction ( SIGHUP, &sa, NULL ) < 0 ) {
|
|
584
|
+ logmsg ( LOG_ERR, "Could not set SIGHUP handler: %s",
|
555
|
585
|
strerror ( errno ) );
|
556
|
586
|
exit ( 1 );
|
557
|
587
|
}
|