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