File: sig_handler.c

package info (click to toggle)
fwknop 2.0.0rc2-2%2Bdeb7u2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,656 kB
  • sloc: ansic: 12,916; sh: 10,743; perl: 545; makefile: 266
file content (151 lines) | stat: -rw-r--r-- 3,877 bytes parent folder | download
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
/* $Id: sig_handler.c 265 2010-07-16 14:28:32Z dstuart $
 *****************************************************************************
 *
 * File:    sig_handler.c
 *
 * Author:  Damien S. Stuart
 *
 * Purpose: Signal handling dta and routines for fwknopd.
 *
 * Copyright 2010 Damien Stuart (dstuart@dstuart.org)
 *
 *  License (GNU Public License):
 *
 *  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 2
 *  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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *  USA
 *
 *****************************************************************************
*/
#include "fwknopd_common.h"
#include "log_msg.h"
#include "sig_handler.h"

#if HAVE_SYS_WAIT_H
  #include <sys/wait.h>
#endif

sig_atomic_t got_signal     = 0;    /* General signal flag (break capture) */

sig_atomic_t got_sighup     = 0;    /* SIGHUP flag  */
sig_atomic_t got_sigint     = 0;    /* SIGINT flag  */
sig_atomic_t got_sigterm    = 0;    /* SIGTERM flag */
sig_atomic_t got_sigusr1    = 0;    /* SIGUSR1 flag */
sig_atomic_t got_sigusr2    = 0;    /* SIGUSR2 flag */
sig_atomic_t got_sigchld    = 0;    /* SIGCHLD flag */

sigset_t    *csmask;

/* SIGHUP Handler
*/
void
sig_handler(int sig)
{
    int o_errno;
    got_signal = sig;

    switch(sig) {
        case SIGHUP:
            got_sighup = 1;
            return;
        case SIGINT:
            got_sigint = 1;
            return;
        case SIGTERM:
            got_sigterm = 1;
            return;
        case SIGUSR1:
            got_sigusr1 = 1;
            return;
        case SIGUSR2:
            got_sigusr2 = 1;
            return;
        case SIGCHLD:
            o_errno = errno; /* Save errno */
            got_sigchld = 1;
            waitpid(-1, NULL, WNOHANG);
            errno = o_errno; /* restore errno (in case reset by waitpid) */
            return;
    }
}

/* Setup signal handlers
*/
int
set_sig_handlers(void)
{
    int                 err = 0;
    struct sigaction    act;

    /* Clear the signal flags.
    */
    got_signal     = 0;
    got_sighup     = 0;
    got_sigint     = 0;
    got_sigterm    = 0;
    got_sigusr1    = 0;
    got_sigusr2    = 0;

    /* Setup the handlers
    */
    act.sa_handler = sig_handler;
    sigemptyset(&act.sa_mask);
    act.sa_flags = SA_RESTART;

    if(sigaction(SIGHUP, &act, NULL) < 0)
    {
        log_msg(LOG_ERR, "* Error setting SIGHUP handler: %s",
            strerror(errno));
        err++;
    }

    if(sigaction(SIGINT, &act, NULL) < 0)
    {
        log_msg(LOG_ERR, "* Error setting SIGINT handler: %s",
            strerror(errno));
        err++;
    }

    if(sigaction(SIGTERM, &act, NULL) < 0)
    {
        log_msg(LOG_ERR, "* Error setting SIGTERM handler: %s",
            strerror(errno));
        err++;
    }

    if(sigaction(SIGUSR1, &act, NULL) < 0)
    {
        log_msg(LOG_ERR, "* Error setting SIGUSR1 handler: %s",
            strerror(errno));
        err++;
    }

    if(sigaction(SIGUSR2, &act, NULL) < 0)
    {
        log_msg(LOG_ERR, "* Error setting SIGUSR2 handler: %s",
            strerror(errno));
        err++;
    }

    if(sigaction(SIGCHLD, &act, NULL) < 0)
    {
        log_msg(LOG_ERR, "* Error setting SIGCHLD handler: %s",
            strerror(errno));
        err++;
    }

    return(err);
}

/***EOF***/