New upstream version 1.2.3
[quagga-debian.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
31 extern void test_init();
32
33 struct thread_master *master;
34
35 struct 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
46 DEFUN (daemon_exit,
47        daemon_exit_cmd,
48        "daemon-exit",
49        "Make the daemon exit\n")
50 {
51   exit(0);
52 }
53
54 static int timer_count;
55 static int
56 test_timer (struct thread *thread)
57 {
58   int *count = THREAD_ARG(thread);
59   
60   printf ("run %d of timer\n", (*count)++);
61   thread_add_timer (master, test_timer, count, 5);
62   return 0;
63 }
64
65 static void
66 test_timer_init()
67 {
68   thread_add_timer (master, test_timer, &timer_count, 10);
69 }
70
71 static void
72 test_vty_init()
73 {
74   install_element (VIEW_NODE, &daemon_exit_cmd);
75 }
76
77 /* Help information display. */
78 static void
79 usage (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\
86 Daemon 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\
94 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
95     }
96   exit (status);
97 }
98
99
100 /* main routine. */
101 int
102 main (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   char *config_file = NULL;
110   
111   /* Set umask before anything for security */
112   umask (0027);
113
114   /* get program name */
115   progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
116
117   /* master init. */
118   master = thread_master_create ();
119
120   while (1) 
121     {
122       int opt;
123
124       opt = getopt_long (argc, argv, "dhf:A:P:v", longopts, 0);
125     
126       if (opt == EOF)
127         break;
128
129       switch (opt) 
130         {
131         case 0:
132           break;
133         case 'f':
134           config_file = optarg;
135           break;
136         case 'd':
137           daemon_mode = 1;
138           break;
139         case 'A':
140           vty_addr = optarg;
141           break;
142         case 'P':
143           /* Deal with atoi() returning 0 on failure */
144           if (strcmp(optarg, "0") == 0)
145             {
146               vty_port = 0;
147               break;
148             } 
149           vty_port = atoi (optarg);
150           vty_port = (vty_port ? vty_port : 4000);
151           break;
152         case 'v':
153           print_version (progname);
154           exit (0);
155           break;
156         case 'h':
157           usage (progname, 0);
158           break;
159         default:
160           usage (progname, 1);
161           break;
162         }
163     }
164
165   /* Library inits. */
166   cmd_init (1);
167   vty_init (master);
168   memory_init ();
169
170   /* OSPF vty inits. */
171   test_vty_init ();
172
173   /* Change to the daemon program. */
174   if (daemon_mode && daemon (0, 0) < 0)
175     {
176       fprintf(stderr, "daemon failed: %s", strerror(errno));
177       exit (1);
178     }
179
180   /* Create VTY socket */
181   vty_serv_sock (vty_addr, vty_port, "/tmp/.heavy.sock");
182   
183   /* Configuration file read*/
184   if (!config_file)
185     usage (progname, 1);
186   vty_read_config (config_file, NULL);
187   
188   test_timer_init();
189   
190   test_init();  
191   
192   /* Fetch next active thread. */
193   thread_main (master);
194
195   /* Not reached. */
196   exit (0);
197 }
198