]> git.proxmox.com Git - mirror_frr.git/blame - tests/helpers/c/main.c
lib: mgmtd: add manual vty server start option and use it
[mirror_frr.git] / tests / helpers / c / main.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
9f3f7a11 2/*
9f3f7a11 3 */
4
5#include <zebra.h>
6
7#include <lib/version.h>
8#include "getopt.h"
24a58196 9#include "frrevent.h"
9f3f7a11 10#include "vty.h"
11#include "command.h"
12#include "memory.h"
1c0d8808 13#include "lib_vty.h"
9f3f7a11 14
4d762f26 15extern void test_init(void);
9f3f7a11 16
cd9d0537 17struct event_loop *master;
9f3f7a11 18
d62a17ae 19struct option longopts[] = {{"daemon", no_argument, NULL, 'd'},
20 {"config_file", required_argument, NULL, 'f'},
21 {"help", no_argument, NULL, 'h'},
22 {"vty_addr", required_argument, NULL, 'A'},
23 {"vty_port", required_argument, NULL, 'P'},
24 {"version", no_argument, NULL, 'v'},
25 {0}};
9f3f7a11 26
27DEFUN (daemon_exit,
28 daemon_exit_cmd,
29 "daemon-exit",
30 "Make the daemon exit\n")
31{
d62a17ae 32 exit(0);
9f3f7a11 33}
34
35static int timer_count;
e6685141 36static void test_timer(struct event *thread)
9f3f7a11 37{
e16d030c 38 int *count = EVENT_ARG(thread);
d62a17ae 39
40 printf("run %d of timer\n", (*count)++);
907a2395 41 event_add_timer(master, test_timer, count, 5, NULL);
9f3f7a11 42}
43
4d762f26 44static void test_timer_init(void)
9f3f7a11 45{
907a2395 46 event_add_timer(master, test_timer, &timer_count, 10, NULL);
9f3f7a11 47}
48
4d762f26 49static void test_vty_init(void)
9f3f7a11 50{
d62a17ae 51 install_element(VIEW_NODE, &daemon_exit_cmd);
9f3f7a11 52}
53
54/* Help information display. */
d62a17ae 55static void usage(char *progname, int status)
9f3f7a11 56{
d62a17ae 57 if (status != 0)
58 fprintf(stderr, "Try `%s --help' for more information.\n",
59 progname);
60 else {
61 printf("Usage : %s [OPTION...]\n\
9f3f7a11 62Daemon which does 'slow' things.\n\n\
63-d, --daemon Runs in daemon mode\n\
64-f, --config_file Set configuration file name\n\
65-A, --vty_addr Set vty's bind address\n\
66-P, --vty_port Set vty's port number\n\
67-v, --version Print program version\n\
68-h, --help Display this help and exit\n\
69\n\
d62a17ae 70Report bugs to %s\n",
71 progname, FRR_BUG_ADDRESS);
72 }
73 exit(status);
9f3f7a11 74}
6b0655a2
DL
75
76
9f3f7a11 77/* main routine. */
d62a17ae 78int main(int argc, char **argv)
9f3f7a11 79{
d62a17ae 80 char *p;
81 char *vty_addr = NULL;
82 int vty_port = 4000;
83 int daemon_mode = 0;
84 char *progname;
e6685141 85 struct event thread;
d62a17ae 86 char *config_file = NULL;
87
88 /* Set umask before anything for security */
89 umask(0027);
90
91 /* get program name */
92 progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]);
93
94 /* master init. */
ce50d11c 95 master = event_master_create(NULL);
d62a17ae 96
97 while (1) {
98 int opt;
99
100 opt = getopt_long(argc, argv, "dhf:A:P:v", longopts, 0);
101
102 if (opt == EOF)
103 break;
104
105 switch (opt) {
106 case 0:
107 break;
108 case 'f':
109 config_file = optarg;
110 break;
111 case 'd':
112 daemon_mode = 1;
113 break;
114 case 'A':
115 vty_addr = optarg;
116 break;
117 case 'P':
118 /* Deal with atoi() returning 0 on failure */
119 if (strcmp(optarg, "0") == 0) {
120 vty_port = 0;
121 break;
122 }
123 vty_port = atoi(optarg);
124 vty_port = (vty_port ? vty_port : 4000);
125 break;
126 case 'v':
127 print_version(progname);
128 exit(0);
129 break;
130 case 'h':
131 usage(progname, 0);
132 break;
133 default:
134 usage(progname, 1);
135 break;
136 }
9f3f7a11 137 }
9f3f7a11 138
d62a17ae 139 /* Library inits. */
140 cmd_init(1);
2950f5da 141 vty_init(master, false);
1c0d8808 142 lib_cmd_init();
390a8862 143 nb_init(master, NULL, 0, false);
d62a17ae 144
145 /* OSPF vty inits. */
146 test_vty_init();
147
148 /* Change to the daemon program. */
149 if (daemon_mode && daemon(0, 0) < 0) {
150 fprintf(stderr, "daemon failed: %s", strerror(errno));
151 exit(1);
152 }
153
154 /* Create VTY socket */
5ba56130 155 vty_serv_start(vty_addr, vty_port, "/tmp/.heavy.sock");
d62a17ae 156
157 /* Configuration file read*/
158 if (!config_file)
159 usage(progname, 1);
1c2facd1 160 vty_read_config(NULL, config_file, NULL);
d62a17ae 161
162 test_timer_init();
163
164 test_init();
165
166 /* Fetch next active thread. */
de2754be
DS
167 while (event_fetch(master, &thread))
168 event_call(&thread);
d62a17ae 169
170 /* Not reached. */
171 exit(0);
172}