-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
129 lines (107 loc) · 2.6 KB
/
main.c
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
/*
* ovsd - Open vSwitch integration into LEDE's netifd
* Copyright (C) 2016 Arne Kappen <akappen@inet.tu-berlin.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation
*
* 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.
*/
#include <getopt.h>
#include <syslog.h>
#include <signal.h>
#include "ovsd.h"
#include "ubus.h"
#define DEFAULT_LOG_LVL LOG_NOTICE
static bool use_syslog = true;
static int log_level = DEFAULT_LOG_LVL;
static const int log_class[] = {
[L_CRIT] = LOG_CRIT,
[L_WARNING] = LOG_WARNING,
[L_NOTICE] = LOG_NOTICE,
[L_INFO] = LOG_INFO,
[L_DEBUG] = LOG_DEBUG
};
void
ovsd_log_msg(int log_lvl, const char *format, ...)
{
va_list vl;
if (log_lvl > log_level)
return;
va_start(vl, format);
if (use_syslog)
vsyslog(log_class[log_lvl], format, vl);
else
vfprintf(stderr, format, vl);
va_end(vl);
}
static int
usage(const char *progname)
{
fprintf(stderr, "Usage: %s [options]\n"
"Options:\n"
" -s <path>: Path to the ubus socket\n"
" -l <level>: Log output level (default: %d)\n"
" -S: Use stderr instead of syslog for log messages\n"
"\n", progname, DEFAULT_LOG_LVL);
return 1;
}
static void
ovsd_handle_signal(int signo)
{
ovsd_log_msg(L_NOTICE, "signal %d caught, shutting down...\n", signo);
uloop_end();
}
/* exit on signals SIGINT, SIGTERM, SIGUSR1, SIGUSR2 */
static void
ovsd_setup_signals(void)
{
struct sigaction s;
memset(&s, 0, sizeof(s));
s.sa_handler = ovsd_handle_signal;
s.sa_flags = 0;
sigaction(SIGINT, &s, NULL);
sigaction(SIGTERM, &s, NULL);
sigaction(SIGUSR1, &s, NULL);
sigaction(SIGUSR2, &s, NULL);
s.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &s, NULL);
}
int main(int argc, char **argv)
{
const char *socket = NULL;
int ch;
//global_argv = argv;
while ((ch = getopt(argc, argv, "d:s:p:c:h:r:l:S")) != -1) {
switch(ch) {
case 's':
socket = optarg;
break;
case 'l':
log_level = atoi(optarg);
if (log_level >= ARRAY_SIZE(log_class))
log_level = ARRAY_SIZE(log_class) -1;
break;
case 'S':
use_syslog = false;
break;
default:
return usage(argv[0]);
}
}
if (use_syslog)
openlog("ovsd", 0, LOG_DAEMON);
ovsd_setup_signals();
if (ovsd_ubus_init(socket) < 0) {
fprintf(stderr, "Failed to connect to ubus\n");
return 1;
}
uloop_run();
if (use_syslog)
closelog();
return 0;
}