]> git.ipfire.org Git - people/ms/rstp.git/blame - main.c
Initial commit
[people/ms/rstp.git] / main.c
CommitLineData
ad02a0eb
SH
1/*****************************************************************************
2 Copyright (c) 2006 EMC Corporation.
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2 of the License, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc., 59
16 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 The full GNU General Public License is included in this distribution in the
19 file called LICENSE.
20
21 Authors: Srinivas Aji <Aji_Srinivas@emc.com>
22
23******************************************************************************/
24
25#include "epoll_loop.h"
26#include "bridge_ctl.h"
27#include "ctl_socket_server.h"
28#include "netif_utils.h"
29#include "log.h"
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <unistd.h>
34#include <getopt.h>
35#include <syslog.h>
36
37static int become_daemon = 1;
38static int is_daemon = 0;
39int log_level = LOG_LEVEL_DEFAULT;
40
41int main(int argc, char *argv[])
42{
43 int c;
44 while ((c = getopt(argc, argv, "dv:")) != -1) {
45 switch (c) {
46 case 'd':
47 become_daemon = 0; break;
48 case 'v':
49 {
50 char *end;
51 long l;
52 l = strtoul(optarg, &end, 0);
53 if (*optarg == 0 || *end != 0 || l > LOG_LEVEL_MAX) {
54 ERROR("Invalid loglevel %s", optarg);
55 exit(1);
56 }
57 log_level = l;
58 }
59 break;
60 default:
61 return -1;
62 }
63 }
64
65 TST(init_epoll() == 0, -1);
66 TST(ctl_socket_init() == 0, -1);
67
68 TST(netsock_init() == 0, -1);
69 TST(init_bridge_ops() == 0, -1);
70 if (become_daemon) {
71 openlog("rstpd", 0, LOG_DAEMON);
72 daemon(0,0);
73 is_daemon = 1;
74 }
75 return epoll_main_loop();
76}
77
78/*********************** Logging *********************/
79
80#include <stdarg.h>
81#include <time.h>
82
83void vDprintf(int level, const char* fmt, va_list ap)
84{
85 if (level > log_level)
86 return;
87
88 if (!is_daemon) {
89 char logbuf[256];
90 logbuf[255] = 0;
91 time_t clock;
92 struct tm *local_tm;
93 time(&clock);
94 local_tm = localtime (&clock);
95 int l = strftime(logbuf, sizeof(logbuf)-1, "%F %T ", local_tm);
96 vsnprintf(logbuf + l, sizeof(logbuf) - l - 1, fmt, ap);
97 printf("%s\n", logbuf);
98 }
99 else {
100 vsyslog((level <= LOG_LEVEL_INFO) ? LOG_INFO : LOG_DEBUG, fmt, ap);
101 }
102}
103
104void Dprintf(int level, const char *fmt, ...)
105{
106 va_list ap;
107 va_start(ap, fmt);
108 vDprintf(level, fmt, ap);
109 va_end(ap);
110}