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