~ubuntu-branches/ubuntu/breezy/quagga/breezy-security

« back to all changes in this revision

Viewing changes to isisd/isis_main.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Mueller
  • Date: 2005-05-20 13:16:12 UTC
  • Revision ID: james.westby@ubuntu.com-20050520131612-pr6paalox60o3x3n
Tags: upstream-0.99.1
ImportĀ upstreamĀ versionĀ 0.99.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * IS-IS Rout(e)ing protocol - isis_main.c
 
3
 *
 
4
 * Copyright (C) 2001,2002   Sampo Saaristo
 
5
 *                           Tampere University of Technology      
 
6
 *                           Institute of Communications Engineering
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify it 
 
9
 * under the terms of the GNU General Public Licenseas published by the Free 
 
10
 * Software Foundation; either version 2 of the License, or (at your option) 
 
11
 * any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,but WITHOUT 
 
14
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 
15
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
 
16
 * more details.
 
17
 
 
18
 * You should have received a copy of the GNU General Public License along 
 
19
 * with this program; if not, write to the Free Software Foundation, Inc., 
 
20
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
21
 */
 
22
 
 
23
#include <stdio.h>
 
24
#include <zebra.h>
 
25
 
 
26
#include "getopt.h"
 
27
#include "thread.h"
 
28
#include "log.h"
 
29
#include <lib/version.h>
 
30
#include "command.h"
 
31
#include "vty.h"
 
32
#include "memory.h"
 
33
#include "stream.h"
 
34
#include "if.h"
 
35
#include "privs.h"
 
36
#include "sigevent.h"
 
37
#include "filter.h"
 
38
 
 
39
#include "isisd/dict.h"
 
40
#include "include-netbsd/iso.h"
 
41
#include "isisd/isis_constants.h"
 
42
#include "isisd/isis_common.h"
 
43
#include "isisd/isis_flags.h"
 
44
#include "isisd/isis_circuit.h"
 
45
#include "isisd/isisd.h"
 
46
#include "isisd/isis_dynhn.h"
 
47
 
 
48
/* Default configuration file name */
 
49
#define ISISD_DEFAULT_CONFIG "isisd.conf"
 
50
/* Default vty port */
 
51
#define ISISD_VTY_PORT       2608
 
52
 
 
53
/* isisd privileges */
 
54
zebra_capabilities_t _caps_p[] = {
 
55
  ZCAP_RAW,
 
56
  ZCAP_BIND
 
57
};
 
58
 
 
59
struct zebra_privs_t isisd_privs = {
 
60
#if defined(QUAGGA_USER)
 
61
  .user = QUAGGA_USER,
 
62
#endif
 
63
#if defined QUAGGA_GROUP
 
64
  .group = QUAGGA_GROUP,
 
65
#endif
 
66
#ifdef VTY_GROUP
 
67
  .vty_group = VTY_GROUP,
 
68
#endif
 
69
  .caps_p = _caps_p,
 
70
  .cap_num_p = 2,
 
71
  .cap_num_i = 0
 
72
};
 
73
 
 
74
/* isisd options */
 
75
struct option longopts[] = {
 
76
  {"daemon", no_argument, NULL, 'd'},
 
77
  {"config_file", required_argument, NULL, 'f'},
 
78
  {"pid_file", required_argument, NULL, 'i'},
 
79
  {"vty_addr", required_argument, NULL, 'A'},
 
80
  {"vty_port", required_argument, NULL, 'P'},
 
81
  {"user", required_argument, NULL, 'u'},
 
82
  {"group", required_argument, NULL, 'g'},
 
83
  {"version", no_argument, NULL, 'v'},
 
84
  {"help", no_argument, NULL, 'h'},
 
85
  {0}
 
86
};
 
87
 
 
88
/* Configuration file and directory. */
 
89
char config_default[] = SYSCONFDIR ISISD_DEFAULT_CONFIG;
 
90
char *config_file = NULL;
 
91
 
 
92
/* isisd program name. */
 
93
char *progname;
 
94
 
 
95
int daemon_mode = 0;
 
96
 
 
97
/* Master of threads. */
 
98
struct thread_master *master;
 
99
 
 
100
/* Process ID saved for use by init system */
 
101
const char *pid_file = PATH_ISISD_PID;
 
102
 
 
103
/* for reload */
 
104
char _cwd[MAXPATHLEN];
 
105
char _progpath[MAXPATHLEN];
 
106
int _argc;
 
107
char **_argv;
 
108
char **_envp;
 
109
 
 
110
/* Help information display. */
 
111
static void
 
112
usage (int status)
 
113
{
 
114
  if (status != 0)
 
115
    fprintf (stderr, "Try `%s --help' for more information.\n", progname);
 
116
  else
 
117
    {
 
118
      printf ("Usage : %s [OPTION...]\n\n\
 
119
Daemon which manages IS-IS routing\n\n\
 
120
-d, --daemon       Runs in daemon mode\n\
 
121
-f, --config_file  Set configuration file name\n\
 
122
-i, --pid_file     Set process identifier file name\n\
 
123
-A, --vty_addr     Set vty's bind address\n\
 
124
-P, --vty_port     Set vty's port number\n\
 
125
-u, --user         User to run as\n\
 
126
-g, --group        Group to run as\n\
 
127
-v, --version      Print program version\n\
 
128
-h, --help         Display this help and exit\n\
 
129
\n\
 
130
Report bugs to http://bugzilla.quagga.net\n", progname);
 
131
    }
 
132
 
 
133
  exit (status);
 
134
}
 
135
 
 
136
 
 
137
void
 
138
reload ()
 
139
{
 
140
  zlog_debug ("Reload");
 
141
  /* FIXME: Clean up func call here */
 
142
  vty_reset ();
 
143
  execve (_progpath, _argv, _envp);
 
144
}
 
145
 
 
146
static void
 
147
terminate (int i)
 
148
{
 
149
  exit (i);
 
150
}
 
151
 
 
152
/*
 
153
 * Signal handlers
 
154
 */
 
155
 
 
156
void
 
157
sighup (void)
 
158
{
 
159
  zlog_debug ("SIGHUP received");
 
160
  reload ();
 
161
 
 
162
  return;
 
163
}
 
164
 
 
165
void
 
166
sigint (void)
 
167
{
 
168
  zlog_notice ("Terminating on signal SIGINT");
 
169
  terminate (0);
 
170
}
 
171
 
 
172
void
 
173
sigterm (void)
 
174
{
 
175
  zlog_notice ("Terminating on signal SIGTERM");
 
176
  terminate (0);
 
177
}
 
178
 
 
179
void
 
180
sigusr1 (void)
 
181
{
 
182
  zlog_debug ("SIGUSR1 received");
 
183
  zlog_rotate (NULL);
 
184
}
 
185
 
 
186
struct quagga_signal_t isisd_signals[] =
 
187
{
 
188
  {
 
189
   .signal = SIGHUP,
 
190
   .handler = &sighup,
 
191
   },
 
192
  {
 
193
   .signal = SIGUSR1,
 
194
   .handler = &sigusr1,
 
195
   },
 
196
  {
 
197
   .signal = SIGINT,
 
198
   .handler = &sigint,
 
199
   },
 
200
  {
 
201
   .signal = SIGTERM,
 
202
   .handler = &sigterm,
 
203
   },
 
204
};
 
205
 
 
206
/*
 
207
 * Main routine of isisd. Parse arguments and handle IS-IS state machine.
 
208
 */
 
209
int
 
210
main (int argc, char **argv, char **envp)
 
211
{
 
212
  char *p;
 
213
  int opt, vty_port = ISISD_VTY_PORT;
 
214
  struct thread thread;
 
215
  char *config_file = NULL;
 
216
  char *vty_addr = NULL;
 
217
 
 
218
  /* Get the programname without the preceding path. */
 
219
  progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
 
220
 
 
221
  zlog_default = openzlog (progname, ZLOG_ISIS,
 
222
                           LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);
 
223
 
 
224
  /* for reload */
 
225
  _argc = argc;
 
226
  _argv = argv;
 
227
  _envp = envp;
 
228
  getcwd (_cwd, sizeof (_cwd));
 
229
  if (*argv[0] == '.')
 
230
    snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]);
 
231
  else
 
232
    snprintf (_progpath, sizeof (_progpath), "%s", argv[0]);
 
233
 
 
234
  /* Command line argument treatment. */
 
235
  while (1)
 
236
    {
 
237
      opt = getopt_long (argc, argv, "df:i:hA:p:P:u:g:v", longopts, 0);
 
238
 
 
239
      if (opt == EOF)
 
240
        break;
 
241
 
 
242
      switch (opt)
 
243
        {
 
244
        case 0:
 
245
          break;
 
246
        case 'd':
 
247
          daemon_mode = 1;
 
248
          break;
 
249
        case 'f':
 
250
          config_file = optarg;
 
251
          break;
 
252
        case 'i':
 
253
          pid_file = optarg;
 
254
          break;
 
255
        case 'A':
 
256
          vty_addr = optarg;
 
257
          break;
 
258
        case 'P':
 
259
          /* Deal with atoi() returning 0 on failure, and isisd not
 
260
             listening on isisd port... */
 
261
          if (strcmp (optarg, "0") == 0)
 
262
            {
 
263
              vty_port = 0;
 
264
              break;
 
265
            }
 
266
          vty_port = atoi (optarg);
 
267
          vty_port = (vty_port ? vty_port : ISISD_VTY_PORT);
 
268
          break;
 
269
        case 'u':
 
270
          isisd_privs.user = optarg;
 
271
          break;
 
272
        case 'g':
 
273
          isisd_privs.group = optarg;
 
274
          break;
 
275
        case 'v':
 
276
          printf ("ISISd version %s\n", ISISD_VERSION);
 
277
          printf ("Copyright (c) 2001-2002 Sampo Saaristo,"
 
278
                  " Ofer Wald and Hannes Gredler\n");
 
279
          print_version ("Zebra");
 
280
          exit (0);
 
281
          break;
 
282
        case 'h':
 
283
          usage (0);
 
284
          break;
 
285
        default:
 
286
          usage (1);
 
287
          break;
 
288
        }
 
289
    }
 
290
 
 
291
  /* thread master */
 
292
  master = thread_master_create ();
 
293
 
 
294
  /* random seed from time */
 
295
  srand (time (NULL));
 
296
 
 
297
  /*
 
298
   *  initializations
 
299
   */
 
300
  zprivs_init (&isisd_privs);
 
301
  signal_init (master, Q_SIGC (isisd_signals), isisd_signals);
 
302
  cmd_init (1);
 
303
  vty_init (master);
 
304
  memory_init ();
 
305
  access_list_init();
 
306
  isis_init ();
 
307
  dyn_cache_init ();
 
308
  sort_node ();
 
309
 
 
310
  /* parse config file */
 
311
  /* this is needed three times! because we have interfaces before the areas */
 
312
  vty_read_config (config_file, config_default);
 
313
  vty_read_config (config_file, config_default);
 
314
  vty_read_config (config_file, config_default);
 
315
 
 
316
  /* demonize */
 
317
  if (daemon_mode)
 
318
    daemon (0, 0);
 
319
 
 
320
  /* Process ID file creation. */
 
321
  pid_output (pid_file);
 
322
 
 
323
  /* Make isis vty socket. */
 
324
  vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH);
 
325
 
 
326
  /* Print banner. */
 
327
  zlog_notice ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
 
328
#ifdef HAVE_IPV6
 
329
  zlog_debug ("IPv6 enabled");
 
330
#endif
 
331
  /* Start finite state machine. */
 
332
  while (thread_fetch (master, &thread))
 
333
    thread_call (&thread);
 
334
 
 
335
  /* Not reached. */
 
336
  exit (0);
 
337
}