summaryrefslogtreecommitdiffstats
path: root/libdaemon/dsignal.c
blob: 9cf5c44cae9f62b6c3a4b85ba4ed7e09a9280c47 (plain)
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
/* $Id$ */

/*
 * This file is part of libdaemon.
 *
 * libdaemon 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.
 *
 * libdaemon 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 libdaemon; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>

#include "dsignal.h"
#include "dlog.h"
#include "dnonblock.h"

static int _signal_pipe[2] = { -1, -1 };

static void _sigfunc(int s) {
    write(_signal_pipe[1], &s, sizeof(s));
}

static int _init(void) {

    if (_signal_pipe[0] < 0 || _signal_pipe[1] < 0) {
        if (pipe(_signal_pipe) < 0) {
            daemon_log(LOG_ERR, "pipe(): %s", strerror(errno));
            return -1;
        }

        if (daemon_nonblock(_signal_pipe[0], 1) < 0 || daemon_nonblock(_signal_pipe[1], 1) < 0)
            return -1;

    }

    return 0;
}

int daemon_signal_install(int s){
    sigset_t ss;
    struct sigaction sa;

    if (_init() < 0)
        return -1;
    
    if (sigemptyset(&ss) < 0) {
        daemon_log(LOG_ERR, "sigemptyset(): %s", strerror(errno));
        return -1;
    }

    if (sigaddset(&ss, s) < 0) {
        daemon_log(LOG_ERR, "sigaddyset(): %s", strerror(errno));
        return -1;
    }

    if (sigprocmask(SIG_UNBLOCK, &ss, NULL) < 0) {
        daemon_log(LOG_ERR, "sigprocmask(): %s", strerror(errno));
        return -1;
    }

    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = _sigfunc;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
        
    if (sigaction(s, &sa, NULL) < 0) {
        daemon_log(LOG_ERR, "sigaction(%s, ...) failed: %s", strsignal(s), strerror(errno));
        return -1;
    }

    return 0;
}

int daemon_signal_init(int s, ...) {
    int sig, r = 0;
    
    va_list ap;
    va_start(ap, s);

    if (_init() < 0)
        return -1;

    sig = s;
    while (sig > 0) {
        if (daemon_signal_install(sig) < 0) {
            r = -1;
            break;
        }
        
        sig = va_arg(ap, int);
    }
            
    va_end(ap);


    return r;
}

void daemon_signal_done(void) {
    if (_signal_pipe[0] != -1)
        close(_signal_pipe[0]);
    
    if (_signal_pipe[1] != -1)
        close(_signal_pipe[1]);

    _signal_pipe[0] = _signal_pipe[1] = -1;
}

int daemon_signal_next(void) {
    int s;
    ssize_t r;

    if ((r = read(_signal_pipe[0], &s, sizeof(s))) == sizeof(s))
        return s;


    if (r < 0) {

        if (errno == EAGAIN)
            return 0;
        else {
            daemon_log(LOG_ERR, "read(signal_pipe[0], ...): %s", strerror(errno));
            return -1;
        }
    }
    
    daemon_log(LOG_ERR, "Short read() on signal pipe.");
    return -1;
}

int daemon_signal_fd(void) {
    return _signal_pipe[0];
}