]> git.proxmox.com Git - mirror_frr.git/blame - tests/helpers/c/main.c
Merge pull request #10447 from ton31337/fix/json_with_whitespaces
[mirror_frr.git] / tests / helpers / c / main.c
CommitLineData
9f3f7a11 1/*
9f3f7a11 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 *
896014f4
DL
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
9f3f7a11 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"
1c0d8808 27#include "lib_vty.h"
9f3f7a11 28
4d762f26 29extern void test_init(void);
9f3f7a11 30
31struct thread_master *master;
32
d62a17ae 33struct 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}};
9f3f7a11 40
41DEFUN (daemon_exit,
42 daemon_exit_cmd,
43 "daemon-exit",
44 "Make the daemon exit\n")
45{
d62a17ae 46 exit(0);
9f3f7a11 47}
48
49static int timer_count;
cc9f21da 50static void test_timer(struct thread *thread)
9f3f7a11 51{
d62a17ae 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);
9f3f7a11 56}
57
4d762f26 58static void test_timer_init(void)
9f3f7a11 59{
d62a17ae 60 thread_add_timer(master, test_timer, &timer_count, 10, NULL);
9f3f7a11 61}
62
4d762f26 63static void test_vty_init(void)
9f3f7a11 64{
d62a17ae 65 install_element(VIEW_NODE, &daemon_exit_cmd);
9f3f7a11 66}
67
68/* Help information display. */
d62a17ae 69static void usage(char *progname, int status)
9f3f7a11 70{
d62a17ae 71 if (status != 0)
72 fprintf(stderr, "Try `%s --help' for more information.\n",
73 progname);
74 else {
75 printf("Usage : %s [OPTION...]\n\
9f3f7a11 76Daemon which does 'slow' things.\n\n\
77-d, --daemon Runs in daemon mode\n\
78-f, --config_file Set configuration file name\n\
79-A, --vty_addr Set vty's bind address\n\
80-P, --vty_port Set vty's port number\n\
81-v, --version Print program version\n\
82-h, --help Display this help and exit\n\
83\n\
d62a17ae 84Report bugs to %s\n",
85 progname, FRR_BUG_ADDRESS);
86 }
87 exit(status);
9f3f7a11 88}
6b0655a2
DL
89
90
9f3f7a11 91/* main routine. */
d62a17ae 92int main(int argc, char **argv)
9f3f7a11 93{
d62a17ae 94 char *p;
95 char *vty_addr = NULL;
96 int vty_port = 4000;
97 int daemon_mode = 0;
98 char *progname;
99 struct thread thread;
100 char *config_file = NULL;
101
102 /* Set umask before anything for security */
103 umask(0027);
104
105 /* get program name */
106 progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]);
107
108 /* master init. */
109 master = thread_master_create(NULL);
110
111 while (1) {
112 int opt;
113
114 opt = getopt_long(argc, argv, "dhf:A:P:v", longopts, 0);
115
116 if (opt == EOF)
117 break;
118
119 switch (opt) {
120 case 0:
121 break;
122 case 'f':
123 config_file = optarg;
124 break;
125 case 'd':
126 daemon_mode = 1;
127 break;
128 case 'A':
129 vty_addr = optarg;
130 break;
131 case 'P':
132 /* Deal with atoi() returning 0 on failure */
133 if (strcmp(optarg, "0") == 0) {
134 vty_port = 0;
135 break;
136 }
137 vty_port = atoi(optarg);
138 vty_port = (vty_port ? vty_port : 4000);
139 break;
140 case 'v':
141 print_version(progname);
142 exit(0);
143 break;
144 case 'h':
145 usage(progname, 0);
146 break;
147 default:
148 usage(progname, 1);
149 break;
150 }
9f3f7a11 151 }
9f3f7a11 152
d62a17ae 153 /* Library inits. */
154 cmd_init(1);
2950f5da 155 vty_init(master, false);
1c0d8808 156 lib_cmd_init();
390a8862 157 nb_init(master, NULL, 0, false);
d62a17ae 158
159 /* OSPF vty inits. */
160 test_vty_init();
161
162 /* Change to the daemon program. */
163 if (daemon_mode && daemon(0, 0) < 0) {
164 fprintf(stderr, "daemon failed: %s", strerror(errno));
165 exit(1);
166 }
167
168 /* Create VTY socket */
169 vty_serv_sock(vty_addr, vty_port, "/tmp/.heavy.sock");
170
171 /* Configuration file read*/
172 if (!config_file)
173 usage(progname, 1);
1c2facd1 174 vty_read_config(NULL, config_file, NULL);
d62a17ae 175
176 test_timer_init();
177
178 test_init();
179
180 /* Fetch next active thread. */
181 while (thread_fetch(master, &thread))
182 thread_call(&thread);
183
184 /* Not reached. */
185 exit(0);
186}