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