Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / unix / main.c @ 9b9a7143

History | View | Annotate | Download (14.3 KB)

1
/*
2
 *        BIRD Internet Routing Daemon -- Unix Entry Point
3
 *
4
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#undef LOCAL_DEBUG
10

    
11
#define _GNU_SOURCE 1
12

    
13
#include <stdio.h>
14
#include <stdlib.h>
15
#include <fcntl.h>
16
#include <unistd.h>
17
#include <signal.h>
18
#include <pwd.h>
19
#include <grp.h>
20
#include <sys/stat.h>
21
#include <libgen.h>
22

    
23
#include "nest/bird.h"
24
#include "lib/lists.h"
25
#include "lib/resource.h"
26
#include "lib/socket.h"
27
#include "lib/event.h"
28
#include "lib/string.h"
29
#include "nest/route.h"
30
#include "nest/protocol.h"
31
#include "nest/iface.h"
32
#include "nest/cli.h"
33
#include "nest/locks.h"
34
#include "conf/conf.h"
35
#include "filter/filter.h"
36

    
37
#include "unix.h"
38
#include "krt.h"
39

    
40
/*
41
 *        Debugging
42
 */
43

    
44
#ifdef DEBUGGING
45
static int debug_flag = 1;
46
#else
47
static int debug_flag = 0;
48
#endif
49

    
50
void
51
async_dump(void)
52
{
53
  debug("INTERNAL STATE DUMP\n\n");
54

    
55
  rdump(&root_pool);
56
  sk_dump_all();
57
  tm_dump_all();
58
  if_dump_all();
59
  neigh_dump_all();
60
  rta_dump_all();
61
  rt_dump_all();
62
  protos_dump_all();
63

    
64
  debug("\n");
65
}
66

    
67
/*
68
 *        Dropping privileges
69
 */
70

    
71
#ifdef CONFIG_RESTRICTED_PRIVILEGES
72
#include "lib/syspriv.h"
73
#else
74

    
75
static inline void
76
drop_uid(uid_t uid)
77
{
78
  die("Cannot change user on this platform");
79
}
80

    
81
#endif
82

    
83
static inline void
84
drop_gid(gid_t gid)
85
{
86
  if (setgid(gid) < 0)
87
    die("setgid: %m");
88
}
89

    
90
/*
91
 *        Reading the Configuration
92
 */
93

    
94
#ifdef PATH_IPROUTE_DIR
95

    
96
static inline void
97
add_num_const(char *name, int val)
98
{
99
  struct symbol *s = cf_get_symbol(name);
100
  s->class = SYM_CONSTANT | T_INT;
101
  s->def = cfg_allocz(sizeof(struct f_val));
102
  SYM_TYPE(s) = T_INT;
103
  SYM_VAL(s).i = val;
104
}
105

    
106
/* the code of read_iproute_table() is based on
107
   rtnl_tab_initialize() from iproute2 package */
108
static void
109
read_iproute_table(char *file, char *prefix, int max)
110
{
111
  char buf[512], namebuf[512];
112
  char *name;
113
  int val;
114
  FILE *fp;
115

    
116
  strcpy(namebuf, prefix);
117
  name = namebuf + strlen(prefix);
118

    
119
  fp = fopen(file, "r");
120
  if (!fp)
121
    return;
122

    
123
  while (fgets(buf, sizeof(buf), fp))
124
  {
125
    char *p = buf;
126

    
127
    while (*p == ' ' || *p == '\t')
128
      p++;
129

    
130
    if (*p == '#' || *p == '\n' || *p == 0)
131
      continue;
132
   
133
    if (sscanf(p, "0x%x %s\n", &val, name) != 2 &&
134
        sscanf(p, "0x%x %s #", &val, name) != 2 &&
135
        sscanf(p, "%d %s\n", &val, name) != 2 &&
136
        sscanf(p, "%d %s #", &val, name) != 2)
137
      continue;
138

    
139
    if (val < 0 || val > max)
140
      continue;
141

    
142
    for(p = name; *p; p++)
143
      if ((*p < 'a' || *p > 'z') && (*p < '0' || *p > '9') && (*p != '_'))
144
        *p = '_';
145

    
146
    add_num_const(namebuf, val);
147
  }
148

    
149
  fclose(fp);
150
}
151

    
152
#endif // PATH_IPROUTE_DIR
153

    
154

    
155
static char *config_name = PATH_CONFIG_FILE;
156

    
157
static int
158
cf_read(byte *dest, uint len, int fd)
159
{
160
  int l = read(fd, dest, len);
161
  if (l < 0)
162
    cf_error("Read error");
163
  return l;
164
}
165

    
166
void
167
sysdep_preconfig(struct config *c)
168
{
169
  init_list(&c->logfiles);
170

    
171
  c->latency_limit = UNIX_DEFAULT_LATENCY_LIMIT;
172
  c->watchdog_warning = UNIX_DEFAULT_WATCHDOG_WARNING;
173

    
174
#ifdef PATH_IPROUTE_DIR
175
  read_iproute_table(PATH_IPROUTE_DIR "/rt_protos", "ipp_", 256);
176
  read_iproute_table(PATH_IPROUTE_DIR "/rt_realms", "ipr_", 256);
177
  read_iproute_table(PATH_IPROUTE_DIR "/rt_scopes", "ips_", 256);
178
  read_iproute_table(PATH_IPROUTE_DIR "/rt_tables", "ipt_", 256);
179
#endif
180
}
181

    
182
int
183
sysdep_commit(struct config *new, struct config *old UNUSED)
184
{
185
  log_switch(debug_flag, &new->logfiles, new->syslog_name);
186
  return 0;
187
}
188

    
189
static int
190
unix_read_config(struct config **cp, char *name)
191
{
192
  struct config *conf = config_alloc(name);
193
  int ret;
194

    
195
  *cp = conf;
196
  conf->file_fd = open(name, O_RDONLY);
197
  if (conf->file_fd < 0)
198
    return 0;
199
  cf_read_hook = cf_read;
200
  ret = config_parse(conf);
201
  close(conf->file_fd);
202
  return ret;
203
}
204

    
205
static struct config *
206
read_config(void)
207
{
208
  struct config *conf;
209

    
210
  if (!unix_read_config(&conf, config_name))
211
    {
212
      if (conf->err_msg)
213
        die("%s, line %d: %s", conf->err_file_name, conf->err_lino, conf->err_msg);
214
      else
215
        die("Unable to open configuration file %s: %m", config_name);
216
    }
217

    
218
  return conf;
219
}
220

    
221
void
222
async_config(void)
223
{
224
  struct config *conf;
225

    
226
  log(L_INFO "Reconfiguration requested by SIGHUP");
227
  if (!unix_read_config(&conf, config_name))
228
    {
229
      if (conf->err_msg)
230
        log(L_ERR "%s, line %d: %s", conf->err_file_name, conf->err_lino, conf->err_msg);
231
      else
232
        log(L_ERR "Unable to open configuration file %s: %m", config_name);
233
      config_free(conf);
234
    }
235
  else
236
    config_commit(conf, RECONFIG_HARD, 0);
237
}
238

    
239
static struct config *
240
cmd_read_config(char *name)
241
{
242
  struct config *conf;
243

    
244
  if (!name)
245
    name = config_name;
246

    
247
  cli_msg(-2, "Reading configuration from %s", name);
248
  if (!unix_read_config(&conf, name))
249
    {
250
      if (conf->err_msg)
251
        cli_msg(8002, "%s, line %d: %s", conf->err_file_name, conf->err_lino, conf->err_msg);
252
      else
253
        cli_msg(8002, "%s: %m", name);
254
      config_free(conf);
255
      conf = NULL;
256
    }
257

    
258
  return conf;
259
}
260

    
261
void
262
cmd_check_config(char *name)
263
{
264
  struct config *conf = cmd_read_config(name);
265
  if (!conf)
266
    return;
267

    
268
  cli_msg(20, "Configuration OK");
269
  config_free(conf);
270
}
271

    
272
static void
273
cmd_reconfig_msg(int r)
274
{
275
  switch (r)
276
    {
277
    case CONF_DONE:        cli_msg( 3, "Reconfigured"); break;
278
    case CONF_PROGRESS: cli_msg( 4, "Reconfiguration in progress"); break;
279
    case CONF_QUEUED:        cli_msg( 5, "Reconfiguration already in progress, queueing new config"); break;
280
    case CONF_UNQUEUED:        cli_msg(17, "Reconfiguration already in progress, removing queued config"); break;
281
    case CONF_CONFIRM:        cli_msg(18, "Reconfiguration confirmed"); break;
282
    case CONF_SHUTDOWN:        cli_msg( 6, "Reconfiguration ignored, shutting down"); break;
283
    case CONF_NOTHING:        cli_msg(19, "Nothing to do"); break;
284
    default:                break;
285
    }
286
}
287

    
288
/* Hack for scheduled undo notification */
289
cli *cmd_reconfig_stored_cli;
290

    
291
void
292
cmd_reconfig_undo_notify(void)
293
{
294
  if (cmd_reconfig_stored_cli)
295
    {
296
      cli *c = cmd_reconfig_stored_cli;
297
      cli_printf(c, CLI_ASYNC_CODE, "Config timeout expired, starting undo");
298
      cli_write_trigger(c);
299
    }
300
}
301

    
302
void
303
cmd_reconfig(char *name, int type, int timeout)
304
{
305
  if (cli_access_restricted())
306
    return;
307

    
308
  struct config *conf = cmd_read_config(name);
309
  if (!conf)
310
    return;
311

    
312
  int r = config_commit(conf, type, timeout);
313

    
314
  if ((r >= 0) && (timeout > 0))
315
    {
316
      cmd_reconfig_stored_cli = this_cli;
317
      cli_msg(-22, "Undo scheduled in %d s", timeout);
318
    }
319

    
320
  cmd_reconfig_msg(r);
321
}
322

    
323
void
324
cmd_reconfig_confirm(void)
325
{
326
  if (cli_access_restricted())
327
    return;
328

    
329
  int r = config_confirm();
330
  cmd_reconfig_msg(r);
331
}
332

    
333
void
334
cmd_reconfig_undo(void)
335
{
336
  if (cli_access_restricted())
337
    return;
338

    
339
  cli_msg(-21, "Undo requested");
340

    
341
  int r = config_undo();
342
  cmd_reconfig_msg(r);
343
}
344

    
345
/*
346
 *        Command-Line Interface
347
 */
348

    
349
static sock *cli_sk;
350
static char *path_control_socket = PATH_CONTROL_SOCKET;
351

    
352

    
353
static void
354
cli_write(cli *c)
355
{
356
  sock *s = c->priv;
357

    
358
  while (c->tx_pos)
359
    {
360
      struct cli_out *o = c->tx_pos;
361

    
362
      int len = o->wpos - o->outpos;
363
      s->tbuf = o->outpos;
364
      o->outpos = o->wpos;
365

    
366
      if (sk_send(s, len) <= 0)
367
        return;
368

    
369
      c->tx_pos = o->next;
370
    }
371

    
372
  /* Everything is written */
373
  s->tbuf = NULL;
374
  cli_written(c);
375
}
376

    
377
void
378
cli_write_trigger(cli *c)
379
{
380
  sock *s = c->priv;
381

    
382
  if (s->tbuf == NULL)
383
    cli_write(c);
384
}
385

    
386
static void
387
cli_tx(sock *s)
388
{
389
  cli_write(s->data);
390
}
391

    
392
int
393
cli_get_command(cli *c)
394
{
395
  sock *s = c->priv;
396
  byte *t = c->rx_aux ? : s->rbuf;
397
  byte *tend = s->rpos;
398
  byte *d = c->rx_pos;
399
  byte *dend = c->rx_buf + CLI_RX_BUF_SIZE - 2;
400

    
401
  while (t < tend)
402
    {
403
      if (*t == '\r')
404
        t++;
405
      else if (*t == '\n')
406
        {
407
          t++;
408
          c->rx_pos = c->rx_buf;
409
          c->rx_aux = t;
410
          *d = 0;
411
          return (d < dend) ? 1 : -1;
412
        }
413
      else if (d < dend)
414
        *d++ = *t++;
415
    }
416
  c->rx_aux = s->rpos = s->rbuf;
417
  c->rx_pos = d;
418
  return 0;
419
}
420

    
421
static int
422
cli_rx(sock *s, int size UNUSED)
423
{
424
  cli_kick(s->data);
425
  return 0;
426
}
427

    
428
static void
429
cli_err(sock *s, int err)
430
{
431
  if (config->cli_debug)
432
    {
433
      if (err)
434
        log(L_INFO "CLI connection dropped: %s", strerror(err));
435
      else
436
        log(L_INFO "CLI connection closed");
437
    }
438
  cli_free(s->data);
439
}
440

    
441
static int
442
cli_connect(sock *s, int size UNUSED)
443
{
444
  cli *c;
445

    
446
  if (config->cli_debug)
447
    log(L_INFO "CLI connect");
448
  s->rx_hook = cli_rx;
449
  s->tx_hook = cli_tx;
450
  s->err_hook = cli_err;
451
  s->data = c = cli_new(s);
452
  s->pool = c->pool;                /* We need to have all the socket buffers allocated in the cli pool */
453
  c->rx_pos = c->rx_buf;
454
  c->rx_aux = NULL;
455
  rmove(s, c->pool);
456
  return 1;
457
}
458

    
459
static void
460
cli_init_unix(uid_t use_uid, gid_t use_gid)
461
{
462
  sock *s;
463

    
464
  cli_init();
465
  s = cli_sk = sk_new(cli_pool);
466
  s->type = SK_UNIX_PASSIVE;
467
  s->rx_hook = cli_connect;
468
  s->rbsize = 1024;
469

    
470
  /* Return value intentionally ignored */
471
  unlink(path_control_socket);
472

    
473
  if (sk_open_unix(s, path_control_socket) < 0)
474
    die("Cannot create control socket %s: %m", path_control_socket);
475

    
476
  if (use_uid || use_gid)
477
    if (chown(path_control_socket, use_uid, use_gid) < 0)
478
      die("chown: %m");
479

    
480
  if (chmod(path_control_socket, 0660) < 0)
481
    die("chmod: %m");
482
}
483

    
484
/*
485
 *        PID file
486
 */
487

    
488
static char *pid_file;
489
static int pid_fd;
490

    
491
static inline void
492
open_pid_file(void)
493
{
494
  if (!pid_file)
495
    return;
496

    
497
  pid_fd = open(pid_file, O_WRONLY|O_CREAT, 0664);
498
  if (pid_fd < 0)
499
    die("Cannot create PID file %s: %m", pid_file);
500
}
501

    
502
static inline void
503
write_pid_file(void)
504
{
505
  int pl, rv;
506
  char ps[24];
507

    
508
  if (!pid_file)
509
    return;
510

    
511
  /* We don't use PID file for uniqueness, so no need for locking */
512

    
513
  pl = bsnprintf(ps, sizeof(ps), "%ld\n", (long) getpid());
514
  if (pl < 0)
515
    bug("PID buffer too small");
516

    
517
  rv = ftruncate(pid_fd, 0);
518
  if (rv < 0)
519
    die("fruncate: %m");
520
    
521
  rv = write(pid_fd, ps, pl);
522
  if(rv < 0)
523
    die("write: %m");
524

    
525
  close(pid_fd);
526
}
527

    
528
static inline void
529
unlink_pid_file(void)
530
{
531
  if (pid_file)
532
    unlink(pid_file);
533
}
534

    
535

    
536
/*
537
 *        Shutdown
538
 */
539

    
540
void
541
cmd_shutdown(void)
542
{
543
  if (cli_access_restricted())
544
    return;
545

    
546
  cli_msg(7, "Shutdown requested");
547
  order_shutdown();
548
}
549

    
550
void
551
async_shutdown(void)
552
{
553
  DBG("Shutting down...\n");
554
  order_shutdown();
555
}
556

    
557
void
558
sysdep_shutdown_done(void)
559
{
560
  unlink_pid_file();
561
  unlink(path_control_socket);
562
  log_msg(L_FATAL "Shutdown completed");
563
  exit(0);
564
}
565

    
566
/*
567
 *        Signals
568
 */
569

    
570
static void
571
handle_sighup(int sig UNUSED)
572
{
573
  DBG("Caught SIGHUP...\n");
574
  async_config_flag = 1;
575
}
576

    
577
static void
578
handle_sigusr(int sig UNUSED)
579
{
580
  DBG("Caught SIGUSR...\n");
581
  async_dump_flag = 1;
582
}
583

    
584
static void
585
handle_sigterm(int sig UNUSED)
586
{
587
  DBG("Caught SIGTERM...\n");
588
  async_shutdown_flag = 1;
589
}
590

    
591
void watchdog_sigalrm(int sig UNUSED);
592

    
593
static void
594
signal_init(void)
595
{
596
  struct sigaction sa;
597

    
598
  bzero(&sa, sizeof(sa));
599
  sa.sa_handler = handle_sigusr;
600
  sa.sa_flags = SA_RESTART;
601
  sigaction(SIGUSR1, &sa, NULL);
602
  sa.sa_handler = handle_sighup;
603
  sa.sa_flags = SA_RESTART;
604
  sigaction(SIGHUP, &sa, NULL);
605
  sa.sa_handler = handle_sigterm;
606
  sa.sa_flags = SA_RESTART;
607
  sigaction(SIGTERM, &sa, NULL);
608
  sa.sa_handler = watchdog_sigalrm;
609
  sa.sa_flags = 0;
610
  sigaction(SIGALRM, &sa, NULL);
611
  signal(SIGPIPE, SIG_IGN);
612
}
613

    
614
/*
615
 *        Parsing of command-line arguments
616
 */
617

    
618
static char *opt_list = "c:dD:ps:P:u:g:fR";
619
static int parse_and_exit;
620
char *bird_name;
621
static char *use_user;
622
static char *use_group;
623
static int run_in_foreground = 0;
624

    
625
static void
626
usage(void)
627
{
628
  fprintf(stderr, "Usage: %s [-c <config-file>] [-d] [-D <debug-file>] [-p] [-s <control-socket>] [-P <pid-file>] [-u <user>] [-g <group>] [-f] [-R]\n", bird_name);
629
  exit(1);
630
}
631

    
632
static inline char *
633
get_bird_name(char *s, char *def)
634
{
635
  char *t;
636
  if (!s)
637
    return def;
638
  t = strrchr(s, '/');
639
  if (!t)
640
    return s;
641
  if (!t[1])
642
    return def;
643
  return t+1;
644
}
645

    
646
static inline uid_t
647
get_uid(const char *s)
648
{
649
  struct passwd *pw;
650
  char *endptr;
651
  long int rv;
652

    
653
  if (!s)
654
    return 0;
655

    
656
  errno = 0;
657
  rv = strtol(s, &endptr, 10);
658

    
659
  if (!errno && !*endptr)
660
    return rv;
661

    
662
  pw = getpwnam(s);
663
  if (!pw)
664
    die("Cannot find user '%s'", s);
665

    
666
  return pw->pw_uid;
667
}
668

    
669
static inline gid_t
670
get_gid(const char *s)
671
{
672
  struct group *gr;
673
  char *endptr;
674
  long int rv;
675

    
676
  if (!s)
677
    return 0;
678
  
679
  errno = 0;
680
  rv = strtol(s, &endptr, 10);
681

    
682
  if (!errno && !*endptr)
683
    return rv;
684

    
685
  gr = getgrnam(s);
686
  if (!gr)
687
    die("Cannot find group '%s'", s);
688

    
689
  return gr->gr_gid;
690
}
691

    
692
static void
693
parse_args(int argc, char **argv)
694
{
695
  int c;
696

    
697
  bird_name = get_bird_name(argv[0], "bird");
698
  if (argc == 2)
699
    {
700
      if (!strcmp(argv[1], "--version"))
701
        {
702
          fprintf(stderr, "BIRD version " BIRD_VERSION "\n");
703
          exit(0);
704
        }
705
      if (!strcmp(argv[1], "--help"))
706
        usage();
707
    }
708
  while ((c = getopt(argc, argv, opt_list)) >= 0)
709
    switch (c)
710
      {
711
      case 'c':
712
        config_name = optarg;
713
        break;
714
      case 'd':
715
        debug_flag |= 1;
716
        break;
717
      case 'D':
718
        log_init_debug(optarg);
719
        debug_flag |= 2;
720
        break;
721
      case 'p':
722
        parse_and_exit = 1;
723
        break;
724
      case 's':
725
        path_control_socket = optarg;
726
        break;
727
      case 'P':
728
        pid_file = optarg;
729
        break;
730
      case 'u':
731
        use_user = optarg;
732
        break;
733
      case 'g':
734
        use_group = optarg;
735
        break;
736
      case 'f':
737
        run_in_foreground = 1;
738
        break;
739
      case 'R':
740
        graceful_restart_recovery();
741
        break;
742
      default:
743
        usage();
744
      }
745
  if (optind < argc)
746
    usage();
747
}
748

    
749
/*
750
 *        Hic Est main()
751
 */
752

    
753
int
754
main(int argc, char **argv)
755
{
756
#ifdef HAVE_LIBDMALLOC
757
  if (!getenv("DMALLOC_OPTIONS"))
758
    dmalloc_debug(0x2f03d00);
759
#endif
760

    
761
  parse_args(argc, argv);
762
  if (debug_flag == 1)
763
    log_init_debug("");
764
  log_switch(debug_flag, NULL, NULL);
765

    
766
  resource_init();
767
  olock_init();
768
  io_init();
769
  rt_init();
770
  if_init();
771
  roa_init();
772
  config_init();
773

    
774
  uid_t use_uid = get_uid(use_user);
775
  gid_t use_gid = get_gid(use_group);
776

    
777
  if (!parse_and_exit)
778
  {
779
    test_old_bird(path_control_socket);
780
    cli_init_unix(use_uid, use_gid);
781
  }
782

    
783
  if (use_gid)
784
    drop_gid(use_gid);
785

    
786
  if (use_uid)
787
    drop_uid(use_uid);
788

    
789
  if (!parse_and_exit)
790
    open_pid_file();
791

    
792
  protos_build();
793
  proto_build(&proto_unix_kernel);
794
  proto_build(&proto_unix_iface);
795

    
796
  struct config *conf = read_config();
797

    
798
  if (parse_and_exit)
799
    exit(0);
800

    
801
  if (!(debug_flag||run_in_foreground))
802
    {
803
      pid_t pid = fork();
804
      if (pid < 0)
805
        die("fork: %m");
806
      if (pid)
807
        return 0;
808
      setsid();
809
      close(0);
810
      if (open("/dev/null", O_RDWR) < 0)
811
        die("Cannot open /dev/null: %m");
812
      dup2(0, 1);
813
      dup2(0, 2);
814
    }
815

    
816
  main_thread_init();
817

    
818
  write_pid_file();
819

    
820
  signal_init();
821

    
822
  config_commit(conf, RECONFIG_HARD, 0);
823

    
824
  graceful_restart_init();
825

    
826
#ifdef LOCAL_DEBUG
827
  async_dump_flag = 1;
828
#endif
829

    
830
  log(L_INFO "Started");
831
  DBG("Entering I/O loop.\n");
832

    
833
  io_loop();
834
  bug("I/O loop died");
835
}