]> git.proxmox.com Git - mirror_frr.git/blob - tests/helpers/c/main.c
*: reindent
[mirror_frr.git] / tests / helpers / c / main.c
1 /*
2 * $Id: main.c,v 1.1 2005/04/25 16:42:24 paul Exp $
3 *
4 * This file is part of Quagga.
5 *
6 * Quagga is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * Quagga is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include <lib/version.h>
24 #include "getopt.h"
25 #include "thread.h"
26 #include "vty.h"
27 #include "command.h"
28 #include "memory.h"
29 #include "memory_vty.h"
30
31 extern void test_init();
32
33 struct thread_master *master;
34
35 struct option longopts[] = {{"daemon", no_argument, NULL, 'd'},
36 {"config_file", required_argument, NULL, 'f'},
37 {"help", no_argument, NULL, 'h'},
38 {"vty_addr", required_argument, NULL, 'A'},
39 {"vty_port", required_argument, NULL, 'P'},
40 {"version", no_argument, NULL, 'v'},
41 {0}};
42
43 DEFUN (daemon_exit,
44 daemon_exit_cmd,
45 "daemon-exit",
46 "Make the daemon exit\n")
47 {
48 exit(0);
49 }
50
51 static int timer_count;
52 static int test_timer(struct thread *thread)
53 {
54 int *count = THREAD_ARG(thread);
55
56 printf("run %d of timer\n", (*count)++);
57 thread_add_timer(master, test_timer, count, 5, NULL);
58 return 0;
59 }
60
61 static void test_timer_init()
62 {
63 thread_add_timer(master, test_timer, &timer_count, 10, NULL);
64 }
65
66 static void test_vty_init()
67 {
68 install_element(VIEW_NODE, &daemon_exit_cmd);
69 }
70
71 /* Help information display. */
72 static void usage(char *progname, int status)
73 {
74 if (status != 0)
75 fprintf(stderr, "Try `%s --help' for more information.\n",
76 progname);
77 else {
78 printf("Usage : %s [OPTION...]\n\
79 Daemon which does 'slow' things.\n\n\
80 -d, --daemon Runs in daemon mode\n\
81 -f, --config_file Set configuration file name\n\
82 -A, --vty_addr Set vty's bind address\n\
83 -P, --vty_port Set vty's port number\n\
84 -v, --version Print program version\n\
85 -h, --help Display this help and exit\n\
86 \n\
87 Report bugs to %s\n",
88 progname, FRR_BUG_ADDRESS);
89 }
90 exit(status);
91 }
92
93
94 /* main routine. */
95 int main(int argc, char **argv)
96 {
97 char *p;
98 char *vty_addr = NULL;
99 int vty_port = 4000;
100 int daemon_mode = 0;
101 char *progname;
102 struct thread thread;
103 char *config_file = NULL;
104
105 /* Set umask before anything for security */
106 umask(0027);
107
108 /* get program name */
109 progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]);
110
111 /* master init. */
112 master = thread_master_create(NULL);
113
114 while (1) {
115 int opt;
116
117 opt = getopt_long(argc, argv, "dhf:A:P:v", longopts, 0);
118
119 if (opt == EOF)
120 break;
121
122 switch (opt) {
123 case 0:
124 break;
125 case 'f':
126 config_file = optarg;
127 break;
128 case 'd':
129 daemon_mode = 1;
130 break;
131 case 'A':
132 vty_addr = optarg;
133 break;
134 case 'P':
135 /* Deal with atoi() returning 0 on failure */
136 if (strcmp(optarg, "0") == 0) {
137 vty_port = 0;
138 break;
139 }
140 vty_port = atoi(optarg);
141 vty_port = (vty_port ? vty_port : 4000);
142 break;
143 case 'v':
144 print_version(progname);
145 exit(0);
146 break;
147 case 'h':
148 usage(progname, 0);
149 break;
150 default:
151 usage(progname, 1);
152 break;
153 }
154 }
155
156 /* Library inits. */
157 cmd_init(1);
158 vty_init(master);
159 memory_init();
160
161 /* OSPF vty inits. */
162 test_vty_init();
163
164 /* Change to the daemon program. */
165 if (daemon_mode && daemon(0, 0) < 0) {
166 fprintf(stderr, "daemon failed: %s", strerror(errno));
167 exit(1);
168 }
169
170 /* Create VTY socket */
171 vty_serv_sock(vty_addr, vty_port, "/tmp/.heavy.sock");
172
173 /* Configuration file read*/
174 if (!config_file)
175 usage(progname, 1);
176 vty_read_config(config_file, NULL);
177
178 test_timer_init();
179
180 test_init();
181
182 /* Fetch next active thread. */
183 while (thread_fetch(master, &thread))
184 thread_call(&thread);
185
186 /* Not reached. */
187 exit(0);
188 }