summaryrefslogtreecommitdiff
path: root/ecp/server/sig.c
blob: 397fef19013240c100733a39f2e6b1ba3b732a40 (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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

#include <ecp/core.h>
#include <ecp/dir/dir.h>

#include "server.h"
#include "dir.h"
#include "acl.h"

#include "sig.h"

static pthread_t sig_handler_thd;
static sigset_t sig_set;

static void * _sig_handler(void *arg) {
    ECPSocket *sock = arg;
    int rv, sig;

    while (1) {
        rv = sigwait(&sig_set, &sig);
        if (rv) {
            LOG(LOG_ERR, "sig_handler: sigwait error\n");
            continue;
        }
        switch (sig) {
            case SIGUSR1: {
                rv = acl_load();
                if (rv) {
                    LOG(LOG_ERR, "sig_handler: acl load err:%d\n", rv);
                    continue;
                }
                LOG(LOG_DEBUG, "sig_handler: acl reloaded - total keys:%u dir keys:%u\n", acl_count(), acl_dir_count());
                break;
            }

            case SIGUSR2: {
                ecp_sock_expire(sock, conn_expired);
                LOG(LOG_DEBUG, "sig_handler: gc done - inbound connection count:%u\n", ecp_sock_gct_count(sock));
                break;
            }

            default: {
                LOG(LOG_DEBUG, "sig_handler: unknown signal:%d\n", sig);
                break;
            }
        }
    }

    return NULL;
}

int sig_start_handler(ECPSocket *sock) {
    int rv;

    rv = pthread_create(&sig_handler_thd, NULL, &_sig_handler, sock);
    if (rv) return ECP_ERR;
    return ECP_OK;

}

int sig_init(void) {
    int rv;

    sigemptyset(&sig_set);
    sigaddset(&sig_set, SIGUSR1);
    sigaddset(&sig_set, SIGUSR2);
    sigaddset(&sig_set, SIGHUP);

    rv = pthread_sigmask(SIG_BLOCK, &sig_set, NULL);
    if (rv) return ECP_ERR;

    return ECP_OK;
}