main.c 4.2 KB

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