netif_utils: correctly close fd on read error
[project/ustp.git] / main.c
1 /*****************************************************************************
2 Copyright (c) 2006 EMC Corporation.
3 Copyright (c) 2011 Factor-SPE
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2 of the License, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc., 59
17 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 The full GNU General Public License is included in this distribution in the
20 file called LICENSE.
21
22 Authors: Srinivas Aji <Aji_Srinivas@emc.com>
23 Authors: Vitalii Demianets <dvitasgs@gmail.com>
24
25 ******************************************************************************/
26
27 /* #define MISC_TEST_FUNCS */
28
29 #include <config.h>
30
31 #include <unistd.h>
32 #include <syslog.h>
33 #include <signal.h>
34 #include <stdbool.h>
35 #include <string.h>
36 #include <sys/types.h>
37 #include <libubox/uloop.h>
38
39 #include "bridge_ctl.h"
40 #include "netif_utils.h"
41 #include "packet.h"
42 #include "log.h"
43 #include "mstp.h"
44 #include "driver.h"
45 #include "bridge_track.h"
46 #include "worker.h"
47 #include "ubus.h"
48
49 #define APP_NAME "ustpd"
50
51 static int print_to_syslog = 1;
52 int log_level = LOG_LEVEL_DEFAULT;
53
54
55 int main(int argc, char *argv[])
56 {
57 int c;
58
59 while((c = getopt(argc, argv, "sv:")) != -1) {
60 switch (c) {
61 case 's':
62 print_to_syslog = 0;
63 break;
64 case 'v': {
65 char *end;
66 long l;
67 l = strtoul(optarg, &end, 0);
68 if(*optarg == 0 || *end != 0 || l > LOG_LEVEL_MAX) {
69 ERROR("Invalid loglevel %s", optarg);
70 exit(1);
71 }
72 log_level = l;
73 break;
74 }
75 default:
76 return -1;
77 }
78 }
79
80 if (print_to_syslog)
81 openlog(APP_NAME, LOG_PID, LOG_DAEMON);
82
83 uloop_init();
84
85 TST(worker_init() == 0, -1);
86 TST(packet_sock_init() == 0, -1);
87 TST(netsock_init() == 0, -1);
88 TST(init_bridge_ops() == 0, -1);
89 ustp_ubus_init();
90
91 uloop_run();
92 bridge_track_fini();
93 worker_cleanup();
94 ustp_ubus_exit();
95 uloop_done();
96
97 return 0;
98 }
99
100 /*********************** Logging *********************/
101
102 #include <stdarg.h>
103 #include <time.h>
104
105 static void vDprintf(int level, const char *fmt, va_list ap)
106 {
107 if(level > log_level)
108 return;
109
110 if(!print_to_syslog)
111 {
112 char logbuf[256];
113 logbuf[255] = 0;
114 time_t clock;
115 struct tm *local_tm;
116 time(&clock);
117 local_tm = localtime(&clock);
118 int l = strftime(logbuf, sizeof(logbuf) - 1, "%F %T ", local_tm);
119 vsnprintf(logbuf + l, sizeof(logbuf) - l - 1, fmt, ap);
120 printf("%s\n", logbuf);
121 fflush(stdout);
122 }
123 else
124 {
125 vsyslog((level <= LOG_LEVEL_INFO) ? LOG_INFO : LOG_DEBUG, fmt, ap);
126 }
127 }
128
129 void Dprintf(int level, const char *fmt, ...)
130 {
131 va_list ap;
132 va_start(ap, fmt);
133 vDprintf(level, fmt, ap);
134 va_end(ap);
135 }