aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 9df0b921b1415ca0b022e3d5a3370902a8f1160b (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
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
/* ==========================================================================
    Licensed under BSD 2clause license. See LICENSE file for more information
    Author: Michał Łyszczek <michal.lyszczek@bofc.pl>
   ==========================================================================
         ------------------------------------------------------------
        / This is entry point of server, here things are initialized \
        \ and main loop is started                                   /
         ------------------------------------------------------------
          \
           \ \_\_    _/_/
            \    \__/
                 (oo)\_______
                 (__)\       )\/\
                     ||----w |
                     ||     ||
   ==========================================================================
      _               __            __           __   ____ _  __
     (_)____   _____ / /__  __ ____/ /___   ____/ /  / __/(_)/ /___   _____
    / // __ \ / ___// // / / // __  // _ \ / __  /  / /_ / // // _ \ / ___/
   / // / / // /__ / // /_/ // /_/ //  __// /_/ /  / __// // //  __/(__  )
  /_//_/ /_/ \___//_/ \__,_/ \__,_/ \___/ \__,_/  /_/  /_//_/ \___//____/

   ========================================================================== */


#include "feature.h"

#include <errno.h>
#include <string.h>
#include <signal.h>
#include <embedlog.h>

#include "config.h"
#include "bnwlist.h"
#include "globals.h"
#include "server.h"
#include "daemonize.h"


/* ==========================================================================
                  _                __           ____
    ____   _____ (_)_   __ ____ _ / /_ ___     / __/__  __ ____   _____ _____
   / __ \ / ___// /| | / // __ `// __// _ \   / /_ / / / // __ \ / ___// ___/
  / /_/ // /   / / | |/ // /_/ // /_ /  __/  / __// /_/ // / / // /__ (__  )
 / .___//_/   /_/  |___/ \__,_/ \__/ \___/  /_/   \__,_//_/ /_/ \___//____/
/_/
   ========================================================================== */


/* ==========================================================================
    handles SIGINT and SIGTERM signals, it basicly sets global variable
    g_shutdown to 1, to inform all module to exit. Also accept() from main
    loop will receive EINTR so it doesn't stuck
   ========================================================================== */


static void sigint_handler(int signo)
{

    if (signo == SIGTERM || signo == SIGINT)
    {
        if (g_shutdown)
        {
            /* someone hit ctrl-c second time, impatient fella */

            el_print(ELN, "Double SIGTERM received, quiting immediately");
            g_stfu = 1;
            return;
        }

        el_print(ELN, "SIGTERM received, waiting for connection to finish");
        g_shutdown = 1;
    }

    if (signo == SIGALRM)
        g_sigalrm = 1;
}


/* ==========================================================================
                       __     __ _          ____
        ____   __  __ / /_   / /(_)_____   / __/__  __ ____   _____ _____
       / __ \ / / / // __ \ / // // ___/  / /_ / / / // __ \ / ___// ___/
      / /_/ // /_/ // /_/ // // // /__   / __// /_/ // / / // /__ (__  )
     / .___/ \__,_//_.___//_//_/ \___/  /_/   \__,_//_/ /_/ \___//____/
    /_/
   ========================================================================== */


int main(int argc, char *argv[])
{
    int               rv;
    struct sigaction  sa;
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


    if (config_init(argc, argv) != 0)
    {
        fprintf(stderr, "fatal error parsing configuration, cannot continue\n");
        return 1;
    }

    /* configure logger for diagnostic logs */

    el_init();
    el_option(EL_THREAD_SAFE, 0);
    el_option(EL_LEVEL, g_config.log_level);
    el_option(EL_OUT, EL_OUT_FILE);
    el_option(EL_TS, EL_TS_LONG);
    el_option(EL_TS_TM, EL_TS_TM_REALTIME);
    el_option(EL_TS_FRACT, EL_TS_FRACT_MS);
    el_option(EL_FUNCINFO, 1);
    el_option(EL_FINFO, 1);
    el_option(EL_COLORS, g_config.colorful_output);
    el_option(EL_FSYNC_EVERY, 0);

    if (el_option(EL_FPATH, g_config.program_log) != 0)
    {
        fprintf(stderr, "WARNING couldn't open program log file %s: %s "
            "logs will be printed to stderr\n",
            g_config.program_log,  strerror(errno));
        el_option(EL_OUT, EL_OUT_STDERR);
    }

    /* configure logger to log queries */

    el_oinit(&g_qlog);
    el_ooption(&g_qlog, EL_THREAD_SAFE, 0);
    el_ooption(&g_qlog, EL_LEVEL, EL_INFO);
    el_ooption(&g_qlog, EL_OUT, EL_OUT_FILE);
    el_ooption(&g_qlog, EL_TS, EL_TS_LONG);
    el_ooption(&g_qlog, EL_TS_TM, EL_TS_TM_REALTIME);
    el_ooption(&g_qlog, EL_PRINT_LEVEL, 0);
    el_ooption(&g_qlog, EL_FSYNC_EVERY, 0);

    if (el_ooption(&g_qlog, EL_FPATH, g_config.query_log) != 0)
    {
        fprintf(stderr, "WARNING couldn't open query log file %s: %s "
            "query logs will be printed to stdout\n",
            g_config.query_log, strerror(errno));
        el_ooption(&g_qlog, EL_OUT, EL_OUT_STDOUT);

        /* turn off stdout buffering, forcing query logs to appear
         * immediately
         */

        setvbuf(stdout, NULL, _IONBF, 0);
    }

    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = sigint_handler;
    sigaction(SIGINT, &sa, NULL);
    sigaction(SIGTERM, &sa, NULL);
    sigaction(SIGALRM, &sa, NULL);

    config_print();

    if (config_validate() != 0)
    {
        rv = 1;
        goto config_validate_error;
    }

    if (bnw_init(g_config.list_file, g_config.list_type) != 0)
    {
        rv = 1;
        el_print(ELF, "couldn't initialize list from file %s",
            g_config.list_file);
        goto bnw_error;
    }

    if (server_init() != 0)
    {
        rv = 1;
        el_print(ELF, "couldn't start server, aborting");
        goto server_error;
    }

    if (g_config.daemonize)
    {
        daemonize(g_config.pid_file, g_config.user, g_config.group);
    }

    server_loop_forever();
    server_destroy();
    rv = 0;

server_error:
    bnw_destroy();

bnw_error:
    if (g_config.daemonize)
    {
        daemonize_cleanup(g_config.pid_file);
    }

config_validate_error:
    return rv;
}