]> git.ipfire.org Git - people/ms/rstp.git/blame - main.c
fixes for 4.3.3 GCC warnings/errors
[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"
fcea43ad 29#include "packet.h"
ad02a0eb
SH
30#include "log.h"
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <unistd.h>
35#include <getopt.h>
36#include <syslog.h>
358260ff 37#include <errno.h>
ad02a0eb
SH
38
39static int become_daemon = 1;
40static int is_daemon = 0;
41int log_level = LOG_LEVEL_DEFAULT;
42
43int main(int argc, char *argv[])
44{
c768cf44 45 int c,ret;
11904a35
SH
46 while ((c = getopt(argc, argv, "dv:")) != -1) {
47 switch (c) {
48 case 'd':
49 become_daemon = 0;
50 break;
51 case 'v':
52 {
53 char *end;
54 long l;
55 l = strtoul(optarg, &end, 0);
56 if (*optarg == 0 || *end != 0
57 || l > LOG_LEVEL_MAX) {
58 ERROR("Invalid loglevel %s", optarg);
59 exit(1);
60 }
61 log_level = l;
62 }
63 break;
64 default:
65 return -1;
66 }
67 }
68
69 TST(init_epoll() == 0, -1);
70 TST(ctl_socket_init() == 0, -1);
fcea43ad 71 TST(packet_sock_init() == 0, -1);
11904a35
SH
72 TST(netsock_init() == 0, -1);
73 TST(init_bridge_ops() == 0, -1);
74 if (become_daemon) {
358260ff
SH
75 FILE *f = fopen("/var/run/rstpd.pid", "w");
76 if (!f) {
77 ERROR("can't open /var/run/rstp.pid");
78 return -1;
79 }
11904a35 80 openlog("rstpd", 0, LOG_DAEMON);
c768cf44
DF
81 ret = daemon(0, 0);
82 if (ret) {
83 ERROR("daemon() failed");
84 return -1;
85 }
11904a35 86 is_daemon = 1;
358260ff
SH
87 fprintf(f, "%d", getpid());
88 fclose(f);
11904a35
SH
89 }
90 return epoll_main_loop();
ad02a0eb
SH
91}
92
93/*********************** Logging *********************/
94
95#include <stdarg.h>
96#include <time.h>
97
11904a35 98void vDprintf(int level, const char *fmt, va_list ap)
ad02a0eb 99{
11904a35
SH
100 if (level > log_level)
101 return;
102
103 if (!is_daemon) {
104 char logbuf[256];
105 logbuf[255] = 0;
106 time_t clock;
107 struct tm *local_tm;
108 time(&clock);
109 local_tm = localtime(&clock);
110 int l =
111 strftime(logbuf, sizeof(logbuf) - 1, "%F %T ", local_tm);
112 vsnprintf(logbuf + l, sizeof(logbuf) - l - 1, fmt, ap);
113 printf("%s\n", logbuf);
114 } else {
115 vsyslog((level <= LOG_LEVEL_INFO) ? LOG_INFO : LOG_DEBUG, fmt,
116 ap);
117 }
ad02a0eb
SH
118}
119
11904a35 120void Dprintf(int level, const char *fmt, ...)
ad02a0eb 121{
11904a35
SH
122 va_list ap;
123 va_start(ap, fmt);
124 vDprintf(level, fmt, ap);
125 va_end(ap);
ad02a0eb 126}