paul | 9f3f7a1 | 2005-04-25 16:42:24 +0000 | [diff] [blame] | 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 | 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 | 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 | |
| 174 | sort_node (); |
| 175 | |
| 176 | /* Change to the daemon program. */ |
| 177 | if (daemon_mode) |
| 178 | daemon (0, 0); |
| 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 | while (thread_fetch (master, &thread)) |
| 194 | thread_call (&thread); |
| 195 | |
| 196 | /* Not reached. */ |
| 197 | exit (0); |
| 198 | } |
| 199 | |