paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * $Id: heavy.c,v 1.1 2005/04/19 21:28:36 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 | /* This programme shows the effects of 'heavy' long-running functions |
| 23 | * on the cooperative threading model. |
| 24 | * |
| 25 | * Run it with a config file containing 'password whatever', telnet to it |
| 26 | * (it defaults to port 4000) and enter the 'clear foo string' command. |
| 27 | * then type whatever and observe that the vty interface is unresponsive |
| 28 | * for quite a period of time, due to the clear_something command |
| 29 | * taking a very long time to complete. |
| 30 | */ |
| 31 | #include <zebra.h> |
| 32 | |
| 33 | #include <lib/version.h> |
| 34 | #include "getopt.h" |
| 35 | #include "thread.h" |
| 36 | #include "vty.h" |
| 37 | #include "command.h" |
| 38 | #include "memory.h" |
| 39 | |
| 40 | struct thread_master *master; |
| 41 | |
| 42 | struct option longopts[] = |
| 43 | { |
| 44 | { "daemon", no_argument, NULL, 'd'}, |
| 45 | { "config_file", required_argument, NULL, 'f'}, |
| 46 | { "help", no_argument, NULL, 'h'}, |
| 47 | { "vty_addr", required_argument, NULL, 'A'}, |
| 48 | { "vty_port", required_argument, NULL, 'P'}, |
| 49 | { "version", no_argument, NULL, 'v'}, |
| 50 | { 0 } |
| 51 | }; |
| 52 | |
| 53 | enum |
| 54 | { |
| 55 | ITERS_FIRST = 0, |
| 56 | ITERS_PRINT = 10, |
| 57 | ITERS_MAX = 1000, |
| 58 | }; |
| 59 | |
| 60 | static void |
| 61 | slow_func (struct vty *vty, char *str, int i) |
| 62 | { |
| 63 | usleep (10000); |
| 64 | if ((i % ITERS_PRINT) == 0) |
| 65 | printf ("%s did %d%s", str, i, VTY_NEWLINE); |
| 66 | } |
| 67 | |
| 68 | static void |
| 69 | clear_something (struct vty *vty, char *str) |
| 70 | { |
| 71 | int i; |
| 72 | |
| 73 | /* this could be like iterating through 150k of route_table |
| 74 | * or worse, iterating through a list of peers, each with 150k routes... |
| 75 | */ |
| 76 | for (i = ITERS_FIRST; i < ITERS_MAX; i++) |
| 77 | slow_func (vty, str, i); |
| 78 | |
| 79 | XFREE (MTYPE_TMP, str); |
| 80 | } |
| 81 | |
| 82 | DEFUN (clear_foo, |
| 83 | clear_foo_cmd, |
| 84 | "clear foo .LINE", |
| 85 | "clear command\n" |
| 86 | "arbitrary string\n") |
| 87 | { |
| 88 | char *str; |
| 89 | if (!argc) |
| 90 | { |
| 91 | vty_out (vty, "%% string argument required%s", VTY_NEWLINE); |
| 92 | return CMD_WARNING; |
| 93 | } |
| 94 | |
| 95 | str = argv_concat (argv, argc, 0); |
| 96 | |
| 97 | clear_something (vty, str); |
| 98 | return CMD_SUCCESS; |
| 99 | } |
| 100 | |
| 101 | static void |
| 102 | slow_vty_init() |
| 103 | { |
| 104 | install_element (VIEW_NODE, &clear_foo_cmd); |
| 105 | } |
| 106 | |
| 107 | /* Help information display. */ |
| 108 | static void |
| 109 | usage (char *progname, int status) |
| 110 | { |
| 111 | if (status != 0) |
| 112 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 113 | else |
| 114 | { |
| 115 | printf ("Usage : %s [OPTION...]\n\ |
| 116 | Daemon which does 'slow' things.\n\n\ |
| 117 | -d, --daemon Runs in daemon mode\n\ |
| 118 | -f, --config_file Set configuration file name\n\ |
| 119 | -A, --vty_addr Set vty's bind address\n\ |
| 120 | -P, --vty_port Set vty's port number\n\ |
| 121 | -v, --version Print program version\n\ |
| 122 | -h, --help Display this help and exit\n\ |
| 123 | \n\ |
| 124 | Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 125 | } |
| 126 | exit (status); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /* main routine. */ |
| 131 | int |
| 132 | main (int argc, char **argv) |
| 133 | { |
| 134 | char *p; |
| 135 | char *vty_addr = NULL; |
| 136 | int vty_port = 4000; |
| 137 | int daemon_mode = 0; |
| 138 | char *progname; |
| 139 | struct thread thread; |
| 140 | char *config_file = NULL; |
| 141 | |
| 142 | /* Set umask before anything for security */ |
| 143 | umask (0027); |
| 144 | |
| 145 | /* get program name */ |
| 146 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 147 | |
| 148 | /* master init. */ |
| 149 | master = thread_master_create (); |
| 150 | |
| 151 | while (1) |
| 152 | { |
| 153 | int opt; |
| 154 | |
| 155 | opt = getopt_long (argc, argv, "dhf:A:P:v", longopts, 0); |
| 156 | |
| 157 | if (opt == EOF) |
| 158 | break; |
| 159 | |
| 160 | switch (opt) |
| 161 | { |
| 162 | case 0: |
| 163 | break; |
| 164 | case 'f': |
| 165 | config_file = optarg; |
| 166 | break; |
| 167 | case 'd': |
| 168 | daemon_mode = 1; |
| 169 | break; |
| 170 | case 'A': |
| 171 | vty_addr = optarg; |
| 172 | break; |
| 173 | case 'P': |
| 174 | /* Deal with atoi() returning 0 on failure */ |
| 175 | if (strcmp(optarg, "0") == 0) |
| 176 | { |
| 177 | vty_port = 0; |
| 178 | break; |
| 179 | } |
| 180 | vty_port = atoi (optarg); |
| 181 | vty_port = (vty_port ? vty_port : 4000); |
| 182 | break; |
| 183 | case 'v': |
| 184 | print_version (progname); |
| 185 | exit (0); |
| 186 | break; |
| 187 | case 'h': |
| 188 | usage (progname, 0); |
| 189 | break; |
| 190 | default: |
| 191 | usage (progname, 1); |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* Library inits. */ |
| 197 | cmd_init (1); |
| 198 | vty_init (master); |
| 199 | memory_init (); |
| 200 | |
| 201 | /* OSPF vty inits. */ |
| 202 | slow_vty_init (); |
| 203 | |
| 204 | sort_node (); |
| 205 | |
| 206 | /* Change to the daemon program. */ |
| 207 | if (daemon_mode) |
| 208 | daemon (0, 0); |
| 209 | |
| 210 | /* Create VTY socket */ |
| 211 | vty_serv_sock (vty_addr, vty_port, "/tmp/.heavy.sock"); |
| 212 | |
| 213 | /* Configuration file read*/ |
| 214 | if (!config_file) |
| 215 | usage (progname, 1); |
| 216 | vty_read_config (config_file, NULL); |
| 217 | |
| 218 | /* Fetch next active thread. */ |
| 219 | while (thread_fetch (master, &thread)) |
| 220 | thread_call (&thread); |
| 221 | |
| 222 | /* Not reached. */ |
| 223 | exit (0); |
| 224 | } |
| 225 | |