ntpd.c revision 330106
1/* 2 * ntpd.c - main program for the fixed point NTP daemon 3 */ 4 5#ifdef HAVE_CONFIG_H 6# include <config.h> 7#endif 8 9#include "ntp_machine.h" 10#include "ntpd.h" 11#include "ntp_io.h" 12#include "ntp_stdlib.h" 13#include <ntp_random.h> 14 15#include "ntp_config.h" 16#include "ntp_syslog.h" 17#include "ntp_assert.h" 18#include "isc/error.h" 19#include "isc/strerror.h" 20#include "isc/formatcheck.h" 21#include "iosignal.h" 22 23#ifdef SIM 24# include "ntpsim.h" 25#endif 26 27#include "ntp_libopts.h" 28#include "ntpd-opts.h" 29 30/* there's a short treatise below what the thread stuff is for. 31 * [Bug 2954] enable the threading warm-up only for Linux. 32 */ 33#if defined(HAVE_PTHREADS) && HAVE_PTHREADS && !defined(NO_THREADS) 34# ifdef HAVE_PTHREAD_H 35# include <pthread.h> 36# endif 37# if defined(linux) 38# define NEED_PTHREAD_WARMUP 39# endif 40#endif 41 42#ifdef HAVE_UNISTD_H 43# include <unistd.h> 44#endif 45#ifdef HAVE_SYS_STAT_H 46# include <sys/stat.h> 47#endif 48#include <stdio.h> 49#ifdef HAVE_SYS_PARAM_H 50# include <sys/param.h> 51#endif 52#ifdef HAVE_SYS_SIGNAL_H 53# include <sys/signal.h> 54#else 55# include <signal.h> 56#endif 57#ifdef HAVE_SYS_IOCTL_H 58# include <sys/ioctl.h> 59#endif /* HAVE_SYS_IOCTL_H */ 60#if defined(HAVE_RTPRIO) 61# ifdef HAVE_SYS_LOCK_H 62# include <sys/lock.h> 63# endif 64# include <sys/rtprio.h> 65#else 66# ifdef HAVE_PLOCK 67# ifdef HAVE_SYS_LOCK_H 68# include <sys/lock.h> 69# endif 70# endif 71#endif 72#if defined(HAVE_SCHED_SETSCHEDULER) 73# ifdef HAVE_SCHED_H 74# include <sched.h> 75# else 76# ifdef HAVE_SYS_SCHED_H 77# include <sys/sched.h> 78# endif 79# endif 80#endif 81#if defined(HAVE_SYS_MMAN_H) 82# include <sys/mman.h> 83#endif 84 85#ifdef HAVE_TERMIOS_H 86# include <termios.h> 87#endif 88 89#ifdef SYS_DOMAINOS 90# include <apollo/base.h> 91#endif /* SYS_DOMAINOS */ 92 93 94#include "recvbuff.h" 95#include "ntp_cmdargs.h" 96 97#if 0 /* HMS: I don't think we need this. 961223 */ 98#ifdef LOCK_PROCESS 99# ifdef SYS_SOLARIS 100# include <sys/mman.h> 101# else 102# include <sys/lock.h> 103# endif 104#endif 105#endif 106 107#ifdef _AIX 108# include <ulimit.h> 109#endif /* _AIX */ 110 111#ifdef SCO5_CLOCK 112# include <sys/ci/ciioctl.h> 113#endif 114 115#ifdef HAVE_DROPROOT 116# include <ctype.h> 117# include <grp.h> 118# include <pwd.h> 119#ifdef HAVE_LINUX_CAPABILITIES 120# include <sys/capability.h> 121# include <sys/prctl.h> 122#endif /* HAVE_LINUX_CAPABILITIES */ 123#if defined(HAVE_PRIV_H) && defined(HAVE_SOLARIS_PRIVS) 124# include <priv.h> 125#endif /* HAVE_PRIV_H */ 126#endif /* HAVE_DROPROOT */ 127 128#if defined (LIBSECCOMP) && (KERN_SECCOMP) 129/* # include <sys/types.h> */ 130# include <sys/resource.h> 131# include <seccomp.h> 132#endif /* LIBSECCOMP and KERN_SECCOMP */ 133 134#ifdef HAVE_DNSREGISTRATION 135# include <dns_sd.h> 136DNSServiceRef mdns; 137#endif 138 139#ifdef HAVE_SETPGRP_0 140# define ntp_setpgrp(x, y) setpgrp() 141#else 142# define ntp_setpgrp(x, y) setpgrp(x, y) 143#endif 144 145#ifdef HAVE_SOLARIS_PRIVS 146# define LOWPRIVS "basic,sys_time,net_privaddr,proc_setid,!proc_info,!proc_session,!proc_exec" 147static priv_set_t *lowprivs = NULL; 148static priv_set_t *highprivs = NULL; 149#endif /* HAVE_SOLARIS_PRIVS */ 150/* 151 * Scheduling priority we run at 152 */ 153#define NTPD_PRIO (-12) 154 155int priority_done = 2; /* 0 - Set priority */ 156 /* 1 - priority is OK where it is */ 157 /* 2 - Don't set priority */ 158 /* 1 and 2 are pretty much the same */ 159 160int listen_to_virtual_ips = TRUE; 161 162/* 163 * No-fork flag. If set, we do not become a background daemon. 164 */ 165int nofork; /* Fork by default */ 166 167#ifdef HAVE_DNSREGISTRATION 168/* 169 * mDNS registration flag. If set, we attempt to register with the mDNS system, but only 170 * after we have synched the first time. If the attempt fails, then try again once per 171 * minute for up to 5 times. After all, we may be starting before mDNS. 172 */ 173int mdnsreg = FALSE; 174int mdnstries = 5; 175#endif /* HAVE_DNSREGISTRATION */ 176 177#ifdef HAVE_DROPROOT 178int droproot; 179int root_dropped; 180char *user; /* User to switch to */ 181char *group; /* group to switch to */ 182const char *chrootdir; /* directory to chroot to */ 183uid_t sw_uid; 184gid_t sw_gid; 185char *endp; 186struct group *gr; 187struct passwd *pw; 188#endif /* HAVE_DROPROOT */ 189 190#ifdef HAVE_WORKING_FORK 191int waitsync_fd_to_close = -1; /* -w/--wait-sync */ 192#endif 193 194/* 195 * Version declaration 196 */ 197extern const char *Version; 198 199char const *progname; 200 201int was_alarmed; 202 203#ifdef DECL_SYSCALL 204/* 205 * We put this here, since the argument profile is syscall-specific 206 */ 207extern int syscall (int, ...); 208#endif /* DECL_SYSCALL */ 209 210 211#if !defined(SIM) && defined(SIGDIE1) 212static volatile int signalled = 0; 213static volatile int signo = 0; 214 215/* In an ideal world, 'finish_safe()' would declared as noreturn... */ 216static void finish_safe (int); 217static RETSIGTYPE finish (int); 218#endif 219 220#if !defined(SIM) && defined(HAVE_WORKING_FORK) 221static int wait_child_sync_if (int, long); 222#endif 223 224#if !defined(SIM) && !defined(SYS_WINNT) 225# ifdef DEBUG 226static RETSIGTYPE moredebug (int); 227static RETSIGTYPE lessdebug (int); 228# else /* !DEBUG follows */ 229static RETSIGTYPE no_debug (int); 230# endif /* !DEBUG */ 231#endif /* !SIM && !SYS_WINNT */ 232 233#ifndef WORK_FORK 234int saved_argc; 235char ** saved_argv; 236#endif 237 238#ifndef SIM 239int ntpdmain (int, char **); 240static void set_process_priority (void); 241static void assertion_failed (const char *, int, 242 isc_assertiontype_t, 243 const char *) 244 __attribute__ ((__noreturn__)); 245static void library_fatal_error (const char *, int, 246 const char *, va_list) 247 ISC_FORMAT_PRINTF(3, 0); 248static void library_unexpected_error(const char *, int, 249 const char *, va_list) 250 ISC_FORMAT_PRINTF(3, 0); 251#endif /* !SIM */ 252 253 254/* Bug2332 unearthed a problem in the interaction of reduced user 255 * privileges, the limits on memory usage and some versions of the 256 * pthread library on Linux systems. The 'pthread_cancel()' function and 257 * likely some others need to track the stack of the thread involved, 258 * and uses a function that comes from GCC (--> libgcc_s.so) to do 259 * this. Unfortunately the developers of glibc decided to load the 260 * library on demand, which speeds up program start but can cause 261 * trouble here: Due to all the things NTPD does to limit its resource 262 * usage, this deferred load of libgcc_s does not always work once the 263 * restrictions are in effect. 264 * 265 * One way out of this was attempting a forced link against libgcc_s 266 * when possible because it makes the library available immediately 267 * without deferred load. (The symbol resolution would still be dynamic 268 * and on demand, but the code would already be in the process image.) 269 * 270 * This is a tricky thing to do, since it's not necessary everywhere, 271 * not possible everywhere, has shown to break the build of other 272 * programs in the NTP suite and is now generally frowned upon. 273 * 274 * So we take a different approach here: We creat a worker thread that does 275 * actually nothing except waiting for cancellation and cancel it. If 276 * this is done before all the limitations are put in place, the 277 * machinery is pre-heated and all the runtime stuff should be in place 278 * and useable when needed. 279 * 280 * This uses only the standard pthread API and should work with all 281 * implementations of pthreads. It is not necessary everywhere, but it's 282 * cheap enough to go on nearly unnoticed. 283 * 284 * Addendum: Bug 2954 showed that the assumption that this should work 285 * with all OS is wrong -- at least FreeBSD bombs heavily. 286 */ 287#ifdef NEED_PTHREAD_WARMUP 288 289/* simple thread function: sleep until cancelled, just to exercise 290 * thread cancellation. 291 */ 292static void* 293my_pthread_warmup_worker( 294 void *thread_args) 295{ 296 (void)thread_args; 297 for (;;) 298 sleep(10); 299 return NULL; 300} 301 302/* pre-heat threading: create a thread and cancel it, just to exercise 303 * thread cancellation. 304 */ 305static void 306my_pthread_warmup(void) 307{ 308 pthread_t thread; 309 pthread_attr_t thr_attr; 310 int rc; 311 312 pthread_attr_init(&thr_attr); 313#if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \ 314 defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) && \ 315 defined(PTHREAD_STACK_MIN) 316 { 317 size_t ssmin = 32*1024; /* 32kB should be minimum */ 318 if (ssmin < PTHREAD_STACK_MIN) 319 ssmin = PTHREAD_STACK_MIN; 320 rc = pthread_attr_setstacksize(&thr_attr, ssmin); 321 if (0 != rc) 322 msyslog(LOG_ERR, 323 "my_pthread_warmup: pthread_attr_setstacksize() -> %s", 324 strerror(rc)); 325 } 326#endif 327 rc = pthread_create( 328 &thread, &thr_attr, my_pthread_warmup_worker, NULL); 329 pthread_attr_destroy(&thr_attr); 330 if (0 != rc) { 331 msyslog(LOG_ERR, 332 "my_pthread_warmup: pthread_create() -> %s", 333 strerror(rc)); 334 } else { 335 pthread_cancel(thread); 336 pthread_join(thread, NULL); 337 } 338} 339 340#endif /*defined(NEED_PTHREAD_WARMUP)*/ 341 342#ifdef NEED_EARLY_FORK 343static void 344dummy_callback(void) { return; } 345 346static void 347fork_nonchroot_worker(void) { 348 getaddrinfo_sometime("localhost", "ntp", NULL, INITIAL_DNS_RETRY, 349 (gai_sometime_callback)&dummy_callback, NULL); 350} 351#endif /* NEED_EARLY_FORK */ 352 353void 354parse_cmdline_opts( 355 int * pargc, 356 char ***pargv 357 ) 358{ 359 static int parsed; 360 static int optct; 361 362 if (!parsed) 363 optct = ntpOptionProcess(&ntpdOptions, *pargc, *pargv); 364 365 parsed = 1; 366 367 *pargc -= optct; 368 *pargv += optct; 369} 370 371 372#ifdef SIM 373int 374main( 375 int argc, 376 char *argv[] 377 ) 378{ 379 progname = argv[0]; 380 parse_cmdline_opts(&argc, &argv); 381#ifdef DEBUG 382 debug = OPT_VALUE_SET_DEBUG_LEVEL; 383 DPRINTF(1, ("%s\n", Version)); 384#endif 385 386 return ntpsim(argc, argv); 387} 388#else /* !SIM follows */ 389#ifdef NO_MAIN_ALLOWED 390CALL(ntpd,"ntpd",ntpdmain); 391#else /* !NO_MAIN_ALLOWED follows */ 392#ifndef SYS_WINNT 393int 394main( 395 int argc, 396 char *argv[] 397 ) 398{ 399 return ntpdmain(argc, argv); 400} 401#endif /* !SYS_WINNT */ 402#endif /* !NO_MAIN_ALLOWED */ 403#endif /* !SIM */ 404 405#ifdef _AIX 406/* 407 * OK. AIX is different than solaris in how it implements plock(). 408 * If you do NOT adjust the stack limit, you will get the MAXIMUM 409 * stack size allocated and PINNED with you program. To check the 410 * value, use ulimit -a. 411 * 412 * To fix this, we create an automatic variable and set our stack limit 413 * to that PLUS 32KB of extra space (we need some headroom). 414 * 415 * This subroutine gets the stack address. 416 * 417 * Grover Davidson and Matt Ladendorf 418 * 419 */ 420static char * 421get_aix_stack(void) 422{ 423 char ch; 424 return (&ch); 425} 426 427/* 428 * Signal handler for SIGDANGER. 429 */ 430static void 431catch_danger(int signo) 432{ 433 msyslog(LOG_INFO, "ntpd: setpgid(): %m"); 434 /* Make the system believe we'll free something, but don't do it! */ 435 return; 436} 437#endif /* _AIX */ 438 439/* 440 * Set the process priority 441 */ 442#ifndef SIM 443static void 444set_process_priority(void) 445{ 446 447# ifdef DEBUG 448 if (debug > 1) 449 msyslog(LOG_DEBUG, "set_process_priority: %s: priority_done is <%d>", 450 ((priority_done) 451 ? "Leave priority alone" 452 : "Attempt to set priority" 453 ), 454 priority_done); 455# endif /* DEBUG */ 456 457# if defined(HAVE_SCHED_SETSCHEDULER) 458 if (!priority_done) { 459 extern int config_priority_override, config_priority; 460 int pmax, pmin; 461 struct sched_param sched; 462 463 pmax = sched_get_priority_max(SCHED_FIFO); 464 sched.sched_priority = pmax; 465 if ( config_priority_override ) { 466 pmin = sched_get_priority_min(SCHED_FIFO); 467 if ( config_priority > pmax ) 468 sched.sched_priority = pmax; 469 else if ( config_priority < pmin ) 470 sched.sched_priority = pmin; 471 else 472 sched.sched_priority = config_priority; 473 } 474 if ( sched_setscheduler(0, SCHED_FIFO, &sched) == -1 ) 475 msyslog(LOG_ERR, "sched_setscheduler(): %m"); 476 else 477 ++priority_done; 478 } 479# endif /* HAVE_SCHED_SETSCHEDULER */ 480# ifdef HAVE_RTPRIO 481# ifdef RTP_SET 482 if (!priority_done) { 483 struct rtprio srtp; 484 485 srtp.type = RTP_PRIO_REALTIME; /* was: RTP_PRIO_NORMAL */ 486 srtp.prio = 0; /* 0 (hi) -> RTP_PRIO_MAX (31,lo) */ 487 488 if (rtprio(RTP_SET, getpid(), &srtp) < 0) 489 msyslog(LOG_ERR, "rtprio() error: %m"); 490 else 491 ++priority_done; 492 } 493# else /* !RTP_SET follows */ 494 if (!priority_done) { 495 if (rtprio(0, 120) < 0) 496 msyslog(LOG_ERR, "rtprio() error: %m"); 497 else 498 ++priority_done; 499 } 500# endif /* !RTP_SET */ 501# endif /* HAVE_RTPRIO */ 502# if defined(NTPD_PRIO) && NTPD_PRIO != 0 503# ifdef HAVE_ATT_NICE 504 if (!priority_done) { 505 errno = 0; 506 if (-1 == nice (NTPD_PRIO) && errno != 0) 507 msyslog(LOG_ERR, "nice() error: %m"); 508 else 509 ++priority_done; 510 } 511# endif /* HAVE_ATT_NICE */ 512# ifdef HAVE_BSD_NICE 513 if (!priority_done) { 514 if (-1 == setpriority(PRIO_PROCESS, 0, NTPD_PRIO)) 515 msyslog(LOG_ERR, "setpriority() error: %m"); 516 else 517 ++priority_done; 518 } 519# endif /* HAVE_BSD_NICE */ 520# endif /* NTPD_PRIO && NTPD_PRIO != 0 */ 521 if (!priority_done) 522 msyslog(LOG_ERR, "set_process_priority: No way found to improve our priority"); 523} 524#endif /* !SIM */ 525 526 527/* 528 * Main program. Initialize us, disconnect us from the tty if necessary, 529 * and loop waiting for I/O and/or timer expiries. 530 */ 531#ifndef SIM 532int 533ntpdmain( 534 int argc, 535 char *argv[] 536 ) 537{ 538 l_fp now; 539 struct recvbuf *rbuf; 540 const char * logfilename; 541# ifdef HAVE_UMASK 542 mode_t uv; 543# endif 544# if defined(HAVE_GETUID) && !defined(MPE) /* MPE lacks the concept of root */ 545 uid_t uid; 546# endif 547# if defined(HAVE_WORKING_FORK) 548 long wait_sync = 0; 549 int pipe_fds[2]; 550 int rc; 551 int exit_code; 552# ifdef _AIX 553 struct sigaction sa; 554# endif 555# if !defined(HAVE_SETSID) && !defined (HAVE_SETPGID) && defined(TIOCNOTTY) 556 int fid; 557# endif 558# endif /* HAVE_WORKING_FORK*/ 559# ifdef SCO5_CLOCK 560 int fd; 561 int zero; 562# endif 563 564# ifdef NEED_PTHREAD_WARMUP 565 my_pthread_warmup(); 566# endif 567 568# ifdef HAVE_UMASK 569 uv = umask(0); 570 if (uv) 571 umask(uv); 572 else 573 umask(022); 574# endif 575 saved_argc = argc; 576 saved_argv = argv; 577 progname = argv[0]; 578 initializing = TRUE; /* mark that we are initializing */ 579 parse_cmdline_opts(&argc, &argv); 580# ifdef DEBUG 581 debug = OPT_VALUE_SET_DEBUG_LEVEL; 582# ifdef HAVE_SETLINEBUF 583 setlinebuf(stdout); 584# endif 585# endif 586 587 if (HAVE_OPT(NOFORK) || HAVE_OPT(QUIT) 588# ifdef DEBUG 589 || debug 590# endif 591 || HAVE_OPT(SAVECONFIGQUIT)) 592 nofork = TRUE; 593 594 init_logging(progname, NLOG_SYNCMASK, TRUE); 595 /* honor -l/--logfile option to log to a file */ 596 if (HAVE_OPT(LOGFILE)) { 597 logfilename = OPT_ARG(LOGFILE); 598 syslogit = FALSE; 599 change_logfile(logfilename, FALSE); 600 } else { 601 logfilename = NULL; 602 if (nofork) 603 msyslog_term = TRUE; 604 if (HAVE_OPT(SAVECONFIGQUIT)) 605 syslogit = FALSE; 606 } 607 msyslog(LOG_NOTICE, "%s: Starting", Version); 608 609 { 610 int i; 611 char buf[1024]; /* Secret knowledge of msyslog buf length */ 612 char *cp = buf; 613 614 /* Note that every arg has an initial space character */ 615 snprintf(cp, sizeof(buf), "Command line:"); 616 cp += strlen(cp); 617 618 for (i = 0; i < saved_argc ; ++i) { 619 snprintf(cp, sizeof(buf) - (cp - buf), 620 " %s", saved_argv[i]); 621 cp += strlen(cp); 622 } 623 msyslog(LOG_INFO, "%s", buf); 624 } 625 626 /* 627 * Install trap handlers to log errors and assertion failures. 628 * Default handlers print to stderr which doesn't work if detached. 629 */ 630 isc_assertion_setcallback(assertion_failed); 631 isc_error_setfatal(library_fatal_error); 632 isc_error_setunexpected(library_unexpected_error); 633 634 /* MPE lacks the concept of root */ 635# if defined(HAVE_GETUID) && !defined(MPE) 636 uid = getuid(); 637 if (uid && !HAVE_OPT( SAVECONFIGQUIT )) { 638 msyslog_term = TRUE; 639 msyslog(LOG_ERR, 640 "must be run as root, not uid %ld", (long)uid); 641 exit(1); 642 } 643# endif 644 645/* 646 * Enable the Multi-Media Timer for Windows? 647 */ 648# ifdef SYS_WINNT 649 if (HAVE_OPT( MODIFYMMTIMER )) 650 set_mm_timer(MM_TIMER_HIRES); 651# endif 652 653#ifdef HAVE_DNSREGISTRATION 654/* 655 * Enable mDNS registrations? 656 */ 657 if (HAVE_OPT( MDNS )) { 658 mdnsreg = TRUE; 659 } 660#endif /* HAVE_DNSREGISTRATION */ 661 662 if (HAVE_OPT( NOVIRTUALIPS )) 663 listen_to_virtual_ips = 0; 664 665 /* 666 * --interface, listen on specified interfaces 667 */ 668 if (HAVE_OPT( INTERFACE )) { 669 int ifacect = STACKCT_OPT( INTERFACE ); 670 const char** ifaces = STACKLST_OPT( INTERFACE ); 671 sockaddr_u addr; 672 673 while (ifacect-- > 0) { 674 add_nic_rule( 675 is_ip_address(*ifaces, AF_UNSPEC, &addr) 676 ? MATCH_IFADDR 677 : MATCH_IFNAME, 678 *ifaces, -1, ACTION_LISTEN); 679 ifaces++; 680 } 681 } 682 683 if (HAVE_OPT( NICE )) 684 priority_done = 0; 685 686# ifdef HAVE_SCHED_SETSCHEDULER 687 if (HAVE_OPT( PRIORITY )) { 688 config_priority = OPT_VALUE_PRIORITY; 689 config_priority_override = 1; 690 priority_done = 0; 691 } 692# endif 693 694# ifdef HAVE_WORKING_FORK 695 /* make sure the FDs are initialised */ 696 pipe_fds[0] = -1; 697 pipe_fds[1] = -1; 698 do { /* 'loop' once */ 699 if (!HAVE_OPT( WAIT_SYNC )) 700 break; 701 wait_sync = OPT_VALUE_WAIT_SYNC; 702 if (wait_sync <= 0) { 703 wait_sync = 0; 704 break; 705 } 706 /* -w requires a fork() even with debug > 0 */ 707 nofork = FALSE; 708 if (pipe(pipe_fds)) { 709 exit_code = (errno) ? errno : -1; 710 msyslog(LOG_ERR, 711 "Pipe creation failed for --wait-sync: %m"); 712 exit(exit_code); 713 } 714 waitsync_fd_to_close = pipe_fds[1]; 715 } while (0); /* 'loop' once */ 716# endif /* HAVE_WORKING_FORK */ 717 718 init_lib(); 719# ifdef SYS_WINNT 720 /* 721 * Start interpolation thread, must occur before first 722 * get_systime() 723 */ 724 init_winnt_time(); 725# endif 726 /* 727 * Initialize random generator and public key pair 728 */ 729 get_systime(&now); 730 731 ntp_srandom((int)(now.l_i * now.l_uf)); 732 733 /* 734 * Detach us from the terminal. May need an #ifndef GIZMO. 735 */ 736 if (!nofork) { 737 738# ifdef HAVE_WORKING_FORK 739 rc = fork(); 740 if (-1 == rc) { 741 exit_code = (errno) ? errno : -1; 742 msyslog(LOG_ERR, "fork: %m"); 743 exit(exit_code); 744 } 745 if (rc > 0) { 746 /* parent */ 747 exit_code = wait_child_sync_if(pipe_fds[0], 748 wait_sync); 749 exit(exit_code); 750 } 751 752 /* 753 * child/daemon 754 * close all open files excepting waitsync_fd_to_close. 755 * msyslog() unreliable until after init_logging(). 756 */ 757 closelog(); 758 if (syslog_file != NULL) { 759 fclose(syslog_file); 760 syslog_file = NULL; 761 syslogit = TRUE; 762 } 763 close_all_except(waitsync_fd_to_close); 764 INSIST(0 == open("/dev/null", 0) && 1 == dup2(0, 1) \ 765 && 2 == dup2(0, 2)); 766 767 init_logging(progname, 0, TRUE); 768 /* we lost our logfile (if any) daemonizing */ 769 setup_logfile(logfilename); 770 771# ifdef SYS_DOMAINOS 772 { 773 uid_$t puid; 774 status_$t st; 775 776 proc2_$who_am_i(&puid); 777 proc2_$make_server(&puid, &st); 778 } 779# endif /* SYS_DOMAINOS */ 780# ifdef HAVE_SETSID 781 if (setsid() == (pid_t)-1) 782 msyslog(LOG_ERR, "setsid(): %m"); 783# elif defined(HAVE_SETPGID) 784 if (setpgid(0, 0) == -1) 785 msyslog(LOG_ERR, "setpgid(): %m"); 786# else /* !HAVE_SETSID && !HAVE_SETPGID follows */ 787# ifdef TIOCNOTTY 788 fid = open("/dev/tty", 2); 789 if (fid >= 0) { 790 ioctl(fid, (u_long)TIOCNOTTY, NULL); 791 close(fid); 792 } 793# endif /* TIOCNOTTY */ 794 ntp_setpgrp(0, getpid()); 795# endif /* !HAVE_SETSID && !HAVE_SETPGID */ 796# ifdef _AIX 797 /* Don't get killed by low-on-memory signal. */ 798 sa.sa_handler = catch_danger; 799 sigemptyset(&sa.sa_mask); 800 sa.sa_flags = SA_RESTART; 801 sigaction(SIGDANGER, &sa, NULL); 802# endif /* _AIX */ 803# endif /* HAVE_WORKING_FORK */ 804 } 805 806# ifdef SCO5_CLOCK 807 /* 808 * SCO OpenServer's system clock offers much more precise timekeeping 809 * on the base CPU than the other CPUs (for multiprocessor systems), 810 * so we must lock to the base CPU. 811 */ 812 fd = open("/dev/at1", O_RDONLY); 813 if (fd >= 0) { 814 zero = 0; 815 if (ioctl(fd, ACPU_LOCK, &zero) < 0) 816 msyslog(LOG_ERR, "cannot lock to base CPU: %m"); 817 close(fd); 818 } 819# endif 820 821 /* Setup stack size in preparation for locking pages in memory. */ 822# if defined(HAVE_MLOCKALL) 823# ifdef HAVE_SETRLIMIT 824 ntp_rlimit(RLIMIT_STACK, DFLT_RLIMIT_STACK * 4096, 4096, "4k"); 825# ifdef RLIMIT_MEMLOCK 826 /* 827 * The default RLIMIT_MEMLOCK is very low on Linux systems. 828 * Unless we increase this limit malloc calls are likely to 829 * fail if we drop root privilege. To be useful the value 830 * has to be larger than the largest ntpd resident set size. 831 */ 832 ntp_rlimit(RLIMIT_MEMLOCK, DFLT_RLIMIT_MEMLOCK * 1024 * 1024, 1024 * 1024, "MB"); 833# endif /* RLIMIT_MEMLOCK */ 834# endif /* HAVE_SETRLIMIT */ 835# else /* !HAVE_MLOCKALL follows */ 836# ifdef HAVE_PLOCK 837# ifdef PROCLOCK 838# ifdef _AIX 839 /* 840 * set the stack limit for AIX for plock(). 841 * see get_aix_stack() for more info. 842 */ 843 if (ulimit(SET_STACKLIM, (get_aix_stack() - 8 * 4096)) < 0) 844 msyslog(LOG_ERR, 845 "Cannot adjust stack limit for plock: %m"); 846# endif /* _AIX */ 847# endif /* PROCLOCK */ 848# endif /* HAVE_PLOCK */ 849# endif /* !HAVE_MLOCKALL */ 850 851 /* 852 * Set up signals we pay attention to locally. 853 */ 854# ifdef SIGDIE1 855 signal_no_reset(SIGDIE1, finish); 856 signal_no_reset(SIGDIE2, finish); 857 signal_no_reset(SIGDIE3, finish); 858 signal_no_reset(SIGDIE4, finish); 859# endif 860# ifdef SIGBUS 861 signal_no_reset(SIGBUS, finish); 862# endif 863 864# if !defined(SYS_WINNT) && !defined(VMS) 865# ifdef DEBUG 866 (void) signal_no_reset(MOREDEBUGSIG, moredebug); 867 (void) signal_no_reset(LESSDEBUGSIG, lessdebug); 868# else 869 (void) signal_no_reset(MOREDEBUGSIG, no_debug); 870 (void) signal_no_reset(LESSDEBUGSIG, no_debug); 871# endif /* DEBUG */ 872# endif /* !SYS_WINNT && !VMS */ 873 874 /* 875 * Set up signals we should never pay attention to. 876 */ 877# ifdef SIGPIPE 878 signal_no_reset(SIGPIPE, SIG_IGN); 879# endif 880 881 /* 882 * Call the init_ routines to initialize the data structures. 883 * 884 * Exactly what command-line options are we expecting here? 885 */ 886 INIT_SSL(); 887 init_auth(); 888 init_util(); 889 init_restrict(); 890 init_mon(); 891 init_timer(); 892 init_request(); 893 init_control(); 894 init_peer(); 895# ifdef REFCLOCK 896 init_refclock(); 897# endif 898 set_process_priority(); 899 init_proto(); /* Call at high priority */ 900 init_io(); 901 init_loopfilter(); 902 mon_start(MON_ON); /* monitor on by default now */ 903 /* turn off in config if unwanted */ 904 905 /* 906 * Get the configuration. This is done in a separate module 907 * since this will definitely be different for the gizmo board. 908 */ 909 getconfig(argc, argv); 910 911 if (-1 == cur_memlock) { 912# if defined(HAVE_MLOCKALL) 913 /* 914 * lock the process into memory 915 */ 916 if ( !HAVE_OPT(SAVECONFIGQUIT) 917# ifdef RLIMIT_MEMLOCK 918 && -1 != DFLT_RLIMIT_MEMLOCK 919# endif 920 && 0 != mlockall(MCL_CURRENT|MCL_FUTURE)) 921 msyslog(LOG_ERR, "mlockall(): %m"); 922# else /* !HAVE_MLOCKALL follows */ 923# ifdef HAVE_PLOCK 924# ifdef PROCLOCK 925 /* 926 * lock the process into memory 927 */ 928 if (!HAVE_OPT(SAVECONFIGQUIT) && 0 != plock(PROCLOCK)) 929 msyslog(LOG_ERR, "plock(PROCLOCK): %m"); 930# else /* !PROCLOCK follows */ 931# ifdef TXTLOCK 932 /* 933 * Lock text into ram 934 */ 935 if (!HAVE_OPT(SAVECONFIGQUIT) && 0 != plock(TXTLOCK)) 936 msyslog(LOG_ERR, "plock(TXTLOCK) error: %m"); 937# else /* !TXTLOCK follows */ 938 msyslog(LOG_ERR, "plock() - don't know what to lock!"); 939# endif /* !TXTLOCK */ 940# endif /* !PROCLOCK */ 941# endif /* HAVE_PLOCK */ 942# endif /* !HAVE_MLOCKALL */ 943 } 944 945 loop_config(LOOP_DRIFTINIT, 0); 946 report_event(EVNT_SYSRESTART, NULL, NULL); 947 initializing = FALSE; 948 949# ifdef HAVE_DROPROOT 950 if (droproot) { 951 952#ifdef NEED_EARLY_FORK 953 fork_nonchroot_worker(); 954#endif 955 956 /* Drop super-user privileges and chroot now if the OS supports this */ 957 958# ifdef HAVE_LINUX_CAPABILITIES 959 /* set flag: keep privileges accross setuid() call (we only really need cap_sys_time): */ 960 if (prctl( PR_SET_KEEPCAPS, 1L, 0L, 0L, 0L ) == -1) { 961 msyslog( LOG_ERR, "prctl( PR_SET_KEEPCAPS, 1L ) failed: %m" ); 962 exit(-1); 963 } 964# elif HAVE_SOLARIS_PRIVS 965 /* Nothing to do here */ 966# else 967 /* we need a user to switch to */ 968 if (user == NULL) { 969 msyslog(LOG_ERR, "Need user name to drop root privileges (see -u flag!)" ); 970 exit(-1); 971 } 972# endif /* HAVE_LINUX_CAPABILITIES || HAVE_SOLARIS_PRIVS */ 973 974 if (user != NULL) { 975 if (isdigit((unsigned char)*user)) { 976 sw_uid = (uid_t)strtoul(user, &endp, 0); 977 if (*endp != '\0') 978 goto getuser; 979 980 if ((pw = getpwuid(sw_uid)) != NULL) { 981 free(user); 982 user = estrdup(pw->pw_name); 983 sw_gid = pw->pw_gid; 984 } else { 985 errno = 0; 986 msyslog(LOG_ERR, "Cannot find user ID %s", user); 987 exit (-1); 988 } 989 990 } else { 991getuser: 992 errno = 0; 993 if ((pw = getpwnam(user)) != NULL) { 994 sw_uid = pw->pw_uid; 995 sw_gid = pw->pw_gid; 996 } else { 997 if (errno) 998 msyslog(LOG_ERR, "getpwnam(%s) failed: %m", user); 999 else 1000 msyslog(LOG_ERR, "Cannot find user `%s'", user); 1001 exit (-1); 1002 } 1003 } 1004 } 1005 if (group != NULL) { 1006 if (isdigit((unsigned char)*group)) { 1007 sw_gid = (gid_t)strtoul(group, &endp, 0); 1008 if (*endp != '\0') 1009 goto getgroup; 1010 } else { 1011getgroup: 1012 if ((gr = getgrnam(group)) != NULL) { 1013 sw_gid = gr->gr_gid; 1014 } else { 1015 errno = 0; 1016 msyslog(LOG_ERR, "Cannot find group `%s'", group); 1017 exit (-1); 1018 } 1019 } 1020 } 1021 1022 if (chrootdir ) { 1023 /* make sure cwd is inside the jail: */ 1024 if (chdir(chrootdir)) { 1025 msyslog(LOG_ERR, "Cannot chdir() to `%s': %m", chrootdir); 1026 exit (-1); 1027 } 1028 if (chroot(chrootdir)) { 1029 msyslog(LOG_ERR, "Cannot chroot() to `%s': %m", chrootdir); 1030 exit (-1); 1031 } 1032 if (chdir("/")) { 1033 msyslog(LOG_ERR, "Cannot chdir() to`root after chroot(): %m"); 1034 exit (-1); 1035 } 1036 } 1037# ifdef HAVE_SOLARIS_PRIVS 1038 if ((lowprivs = priv_str_to_set(LOWPRIVS, ",", NULL)) == NULL) { 1039 msyslog(LOG_ERR, "priv_str_to_set() failed:%m"); 1040 exit(-1); 1041 } 1042 if ((highprivs = priv_allocset()) == NULL) { 1043 msyslog(LOG_ERR, "priv_allocset() failed:%m"); 1044 exit(-1); 1045 } 1046 (void) getppriv(PRIV_PERMITTED, highprivs); 1047 (void) priv_intersect(highprivs, lowprivs); 1048 if (setppriv(PRIV_SET, PRIV_PERMITTED, lowprivs) == -1) { 1049 msyslog(LOG_ERR, "setppriv() failed:%m"); 1050 exit(-1); 1051 } 1052# endif /* HAVE_SOLARIS_PRIVS */ 1053 if (user && initgroups(user, sw_gid)) { 1054 msyslog(LOG_ERR, "Cannot initgroups() to user `%s': %m", user); 1055 exit (-1); 1056 } 1057 if (group && setgid(sw_gid)) { 1058 msyslog(LOG_ERR, "Cannot setgid() to group `%s': %m", group); 1059 exit (-1); 1060 } 1061 if (group && setegid(sw_gid)) { 1062 msyslog(LOG_ERR, "Cannot setegid() to group `%s': %m", group); 1063 exit (-1); 1064 } 1065 if (group) { 1066 if (0 != setgroups(1, &sw_gid)) { 1067 msyslog(LOG_ERR, "setgroups(1, %d) failed: %m", sw_gid); 1068 exit (-1); 1069 } 1070 } 1071 else if (pw) 1072 if (0 != initgroups(pw->pw_name, pw->pw_gid)) { 1073 msyslog(LOG_ERR, "initgroups(<%s>, %d) filed: %m", pw->pw_name, pw->pw_gid); 1074 exit (-1); 1075 } 1076 if (user && setuid(sw_uid)) { 1077 msyslog(LOG_ERR, "Cannot setuid() to user `%s': %m", user); 1078 exit (-1); 1079 } 1080 if (user && seteuid(sw_uid)) { 1081 msyslog(LOG_ERR, "Cannot seteuid() to user `%s': %m", user); 1082 exit (-1); 1083 } 1084 1085# if !defined(HAVE_LINUX_CAPABILITIES) && !defined(HAVE_SOLARIS_PRIVS) 1086 /* 1087 * for now assume that the privilege to bind to privileged ports 1088 * is associated with running with uid 0 - should be refined on 1089 * ports that allow binding to NTP_PORT with uid != 0 1090 */ 1091 disable_dynamic_updates |= (sw_uid != 0); /* also notifies routing message listener */ 1092# endif /* !HAVE_LINUX_CAPABILITIES && !HAVE_SOLARIS_PRIVS */ 1093 1094 if (disable_dynamic_updates && interface_interval) { 1095 interface_interval = 0; 1096 msyslog(LOG_INFO, "running as non-root disables dynamic interface tracking"); 1097 } 1098 1099# ifdef HAVE_LINUX_CAPABILITIES 1100 { 1101 /* 1102 * We may be running under non-root uid now, but we still hold full root privileges! 1103 * We drop all of them, except for the crucial one or two: cap_sys_time and 1104 * cap_net_bind_service if doing dynamic interface tracking. 1105 */ 1106 cap_t caps; 1107 char *captext; 1108 1109 captext = (0 != interface_interval) 1110 ? "cap_sys_time,cap_net_bind_service=pe" 1111 : "cap_sys_time=pe"; 1112 caps = cap_from_text(captext); 1113 if (!caps) { 1114 msyslog(LOG_ERR, 1115 "cap_from_text(%s) failed: %m", 1116 captext); 1117 exit(-1); 1118 } 1119 if (-1 == cap_set_proc(caps)) { 1120 msyslog(LOG_ERR, 1121 "cap_set_proc() failed to drop root privs: %m"); 1122 exit(-1); 1123 } 1124 cap_free(caps); 1125 } 1126# endif /* HAVE_LINUX_CAPABILITIES */ 1127# ifdef HAVE_SOLARIS_PRIVS 1128 if (priv_delset(lowprivs, "proc_setid") == -1) { 1129 msyslog(LOG_ERR, "priv_delset() failed:%m"); 1130 exit(-1); 1131 } 1132 if (setppriv(PRIV_SET, PRIV_PERMITTED, lowprivs) == -1) { 1133 msyslog(LOG_ERR, "setppriv() failed:%m"); 1134 exit(-1); 1135 } 1136 priv_freeset(lowprivs); 1137 priv_freeset(highprivs); 1138# endif /* HAVE_SOLARIS_PRIVS */ 1139 root_dropped = TRUE; 1140 fork_deferred_worker(); 1141 } /* if (droproot) */ 1142# endif /* HAVE_DROPROOT */ 1143 1144/* libssecomp sandboxing */ 1145#if defined (LIBSECCOMP) && (KERN_SECCOMP) 1146 scmp_filter_ctx ctx; 1147 1148 if ((ctx = seccomp_init(SCMP_ACT_KILL)) < 0) 1149 msyslog(LOG_ERR, "%s: seccomp_init(SCMP_ACT_KILL) failed: %m", __func__); 1150 else { 1151 msyslog(LOG_DEBUG, "%s: seccomp_init(SCMP_ACT_KILL) succeeded", __func__); 1152 } 1153 1154#ifdef __x86_64__ 1155int scmp_sc[] = { 1156 SCMP_SYS(adjtimex), 1157 SCMP_SYS(bind), 1158 SCMP_SYS(brk), 1159 SCMP_SYS(chdir), 1160 SCMP_SYS(clock_gettime), 1161 SCMP_SYS(clock_settime), 1162 SCMP_SYS(close), 1163 SCMP_SYS(connect), 1164 SCMP_SYS(exit_group), 1165 SCMP_SYS(fstat), 1166 SCMP_SYS(fsync), 1167 SCMP_SYS(futex), 1168 SCMP_SYS(getitimer), 1169 SCMP_SYS(getsockname), 1170 SCMP_SYS(ioctl), 1171 SCMP_SYS(lseek), 1172 SCMP_SYS(madvise), 1173 SCMP_SYS(mmap), 1174 SCMP_SYS(munmap), 1175 SCMP_SYS(open), 1176 SCMP_SYS(poll), 1177 SCMP_SYS(read), 1178 SCMP_SYS(recvmsg), 1179 SCMP_SYS(rename), 1180 SCMP_SYS(rt_sigaction), 1181 SCMP_SYS(rt_sigprocmask), 1182 SCMP_SYS(rt_sigreturn), 1183 SCMP_SYS(select), 1184 SCMP_SYS(sendto), 1185 SCMP_SYS(setitimer), 1186 SCMP_SYS(setsid), 1187 SCMP_SYS(socket), 1188 SCMP_SYS(stat), 1189 SCMP_SYS(time), 1190 SCMP_SYS(write), 1191}; 1192#endif 1193#ifdef __i386__ 1194int scmp_sc[] = { 1195 SCMP_SYS(_newselect), 1196 SCMP_SYS(adjtimex), 1197 SCMP_SYS(brk), 1198 SCMP_SYS(chdir), 1199 SCMP_SYS(clock_gettime), 1200 SCMP_SYS(clock_settime), 1201 SCMP_SYS(close), 1202 SCMP_SYS(exit_group), 1203 SCMP_SYS(fsync), 1204 SCMP_SYS(futex), 1205 SCMP_SYS(getitimer), 1206 SCMP_SYS(madvise), 1207 SCMP_SYS(mmap), 1208 SCMP_SYS(mmap2), 1209 SCMP_SYS(munmap), 1210 SCMP_SYS(open), 1211 SCMP_SYS(poll), 1212 SCMP_SYS(read), 1213 SCMP_SYS(rename), 1214 SCMP_SYS(rt_sigaction), 1215 SCMP_SYS(rt_sigprocmask), 1216 SCMP_SYS(select), 1217 SCMP_SYS(setitimer), 1218 SCMP_SYS(setsid), 1219 SCMP_SYS(sigprocmask), 1220 SCMP_SYS(sigreturn), 1221 SCMP_SYS(socketcall), 1222 SCMP_SYS(stat64), 1223 SCMP_SYS(time), 1224 SCMP_SYS(write), 1225}; 1226#endif 1227 { 1228 int i; 1229 1230 for (i = 0; i < COUNTOF(scmp_sc); i++) { 1231 if (seccomp_rule_add(ctx, 1232 SCMP_ACT_ALLOW, scmp_sc[i], 0) < 0) { 1233 msyslog(LOG_ERR, 1234 "%s: seccomp_rule_add() failed: %m", 1235 __func__); 1236 } 1237 } 1238 } 1239 1240 if (seccomp_load(ctx) < 0) 1241 msyslog(LOG_ERR, "%s: seccomp_load() failed: %m", 1242 __func__); 1243 else { 1244 msyslog(LOG_DEBUG, "%s: seccomp_load() succeeded", __func__); 1245 } 1246#endif /* LIBSECCOMP and KERN_SECCOMP */ 1247 1248# ifdef HAVE_IO_COMPLETION_PORT 1249 1250 for (;;) { 1251#if !defined(SIM) && defined(SIGDIE1) 1252 if (signalled) 1253 finish_safe(signo); 1254#endif 1255 GetReceivedBuffers(); 1256# else /* normal I/O */ 1257 1258 BLOCK_IO_AND_ALARM(); 1259 was_alarmed = FALSE; 1260 1261 for (;;) { 1262#if !defined(SIM) && defined(SIGDIE1) 1263 if (signalled) 1264 finish_safe(signo); 1265#endif 1266 if (alarm_flag) { /* alarmed? */ 1267 was_alarmed = TRUE; 1268 alarm_flag = FALSE; 1269 } 1270 1271 /* collect async name/addr results */ 1272 if (!was_alarmed) 1273 harvest_blocking_responses(); 1274 1275 if (!was_alarmed && !has_full_recv_buffer()) { 1276 /* 1277 * Nothing to do. Wait for something. 1278 */ 1279 io_handler(); 1280 } 1281 1282 if (alarm_flag) { /* alarmed? */ 1283 was_alarmed = TRUE; 1284 alarm_flag = FALSE; 1285 } 1286 1287 if (was_alarmed) { 1288 UNBLOCK_IO_AND_ALARM(); 1289 /* 1290 * Out here, signals are unblocked. Call timer routine 1291 * to process expiry. 1292 */ 1293 timer(); 1294 was_alarmed = FALSE; 1295 BLOCK_IO_AND_ALARM(); 1296 } 1297 1298# endif /* !HAVE_IO_COMPLETION_PORT */ 1299 1300# ifdef DEBUG_TIMING 1301 { 1302 l_fp pts; 1303 l_fp tsa, tsb; 1304 int bufcount = 0; 1305 1306 get_systime(&pts); 1307 tsa = pts; 1308# endif 1309 rbuf = get_full_recv_buffer(); 1310 while (rbuf != NULL) { 1311 if (alarm_flag) { 1312 was_alarmed = TRUE; 1313 alarm_flag = FALSE; 1314 } 1315 UNBLOCK_IO_AND_ALARM(); 1316 1317 if (was_alarmed) { 1318 /* avoid timer starvation during lengthy I/O handling */ 1319 timer(); 1320 was_alarmed = FALSE; 1321 } 1322 1323 /* 1324 * Call the data procedure to handle each received 1325 * packet. 1326 */ 1327 if (rbuf->receiver != NULL) { 1328# ifdef DEBUG_TIMING 1329 l_fp dts = pts; 1330 1331 L_SUB(&dts, &rbuf->recv_time); 1332 DPRINTF(2, ("processing timestamp delta %s (with prec. fuzz)\n", lfptoa(&dts, 9))); 1333 collect_timing(rbuf, "buffer processing delay", 1, &dts); 1334 bufcount++; 1335# endif 1336 (*rbuf->receiver)(rbuf); 1337 } else { 1338 msyslog(LOG_ERR, "fatal: receive buffer callback NULL"); 1339 abort(); 1340 } 1341 1342 BLOCK_IO_AND_ALARM(); 1343 freerecvbuf(rbuf); 1344 rbuf = get_full_recv_buffer(); 1345 } 1346# ifdef DEBUG_TIMING 1347 get_systime(&tsb); 1348 L_SUB(&tsb, &tsa); 1349 if (bufcount) { 1350 collect_timing(NULL, "processing", bufcount, &tsb); 1351 DPRINTF(2, ("processing time for %d buffers %s\n", bufcount, lfptoa(&tsb, 9))); 1352 } 1353 } 1354# endif 1355 1356 /* 1357 * Go around again 1358 */ 1359 1360# ifdef HAVE_DNSREGISTRATION 1361 if (mdnsreg && (current_time - mdnsreg ) > 60 && mdnstries && sys_leap != LEAP_NOTINSYNC) { 1362 mdnsreg = current_time; 1363 msyslog(LOG_INFO, "Attempting to register mDNS"); 1364 if ( DNSServiceRegister (&mdns, 0, 0, NULL, "_ntp._udp", NULL, NULL, 1365 htons(NTP_PORT), 0, NULL, NULL, NULL) != kDNSServiceErr_NoError ) { 1366 if (!--mdnstries) { 1367 msyslog(LOG_ERR, "Unable to register mDNS, giving up."); 1368 } else { 1369 msyslog(LOG_INFO, "Unable to register mDNS, will try later."); 1370 } 1371 } else { 1372 msyslog(LOG_INFO, "mDNS service registered."); 1373 mdnsreg = FALSE; 1374 } 1375 } 1376# endif /* HAVE_DNSREGISTRATION */ 1377 1378 } 1379 UNBLOCK_IO_AND_ALARM(); 1380 return 1; 1381} 1382#endif /* !SIM */ 1383 1384 1385#if !defined(SIM) && defined(SIGDIE1) 1386/* 1387 * finish - exit gracefully 1388 */ 1389static void 1390finish_safe( 1391 int sig 1392 ) 1393{ 1394 const char *sig_desc; 1395 1396 sig_desc = NULL; 1397#ifdef HAVE_STRSIGNAL 1398 sig_desc = strsignal(sig); 1399#endif 1400 if (sig_desc == NULL) 1401 sig_desc = ""; 1402 msyslog(LOG_NOTICE, "%s exiting on signal %d (%s)", progname, 1403 sig, sig_desc); 1404 /* See Bug 2513 and Bug 2522 re the unlink of PIDFILE */ 1405# ifdef HAVE_DNSREGISTRATION 1406 if (mdns != NULL) 1407 DNSServiceRefDeallocate(mdns); 1408# endif 1409 peer_cleanup(); 1410 exit(0); 1411} 1412 1413static RETSIGTYPE 1414finish( 1415 int sig 1416 ) 1417{ 1418 signalled = 1; 1419 signo = sig; 1420} 1421 1422#endif /* !SIM && SIGDIE1 */ 1423 1424 1425#ifndef SIM 1426/* 1427 * wait_child_sync_if - implements parent side of -w/--wait-sync 1428 */ 1429# ifdef HAVE_WORKING_FORK 1430static int 1431wait_child_sync_if( 1432 int pipe_read_fd, 1433 long wait_sync 1434 ) 1435{ 1436 int rc; 1437 int exit_code; 1438 time_t wait_end_time; 1439 time_t cur_time; 1440 time_t wait_rem; 1441 fd_set readset; 1442 struct timeval wtimeout; 1443 1444 if (0 == wait_sync) 1445 return 0; 1446 1447 /* waitsync_fd_to_close used solely by child */ 1448 close(waitsync_fd_to_close); 1449 wait_end_time = time(NULL) + wait_sync; 1450 do { 1451 cur_time = time(NULL); 1452 wait_rem = (wait_end_time > cur_time) 1453 ? (wait_end_time - cur_time) 1454 : 0; 1455 wtimeout.tv_sec = wait_rem; 1456 wtimeout.tv_usec = 0; 1457 FD_ZERO(&readset); 1458 FD_SET(pipe_read_fd, &readset); 1459 rc = select(pipe_read_fd + 1, &readset, NULL, NULL, 1460 &wtimeout); 1461 if (-1 == rc) { 1462 if (EINTR == errno) 1463 continue; 1464 exit_code = (errno) ? errno : -1; 1465 msyslog(LOG_ERR, 1466 "--wait-sync select failed: %m"); 1467 return exit_code; 1468 } 1469 if (0 == rc) { 1470 /* 1471 * select() indicated a timeout, but in case 1472 * its timeouts are affected by a step of the 1473 * system clock, select() again with a zero 1474 * timeout to confirm. 1475 */ 1476 FD_ZERO(&readset); 1477 FD_SET(pipe_read_fd, &readset); 1478 wtimeout.tv_sec = 0; 1479 wtimeout.tv_usec = 0; 1480 rc = select(pipe_read_fd + 1, &readset, NULL, 1481 NULL, &wtimeout); 1482 if (0 == rc) /* select() timeout */ 1483 break; 1484 else /* readable */ 1485 return 0; 1486 } else /* readable */ 1487 return 0; 1488 } while (wait_rem > 0); 1489 1490 fprintf(stderr, "%s: -w/--wait-sync %ld timed out.\n", 1491 progname, wait_sync); 1492 return ETIMEDOUT; 1493} 1494# endif /* HAVE_WORKING_FORK */ 1495 1496 1497/* 1498 * assertion_failed - Redirect assertion failures to msyslog(). 1499 */ 1500static void 1501assertion_failed( 1502 const char *file, 1503 int line, 1504 isc_assertiontype_t type, 1505 const char *cond 1506 ) 1507{ 1508 isc_assertion_setcallback(NULL); /* Avoid recursion */ 1509 1510 msyslog(LOG_ERR, "%s:%d: %s(%s) failed", 1511 file, line, isc_assertion_typetotext(type), cond); 1512 msyslog(LOG_ERR, "exiting (due to assertion failure)"); 1513 1514#if defined(DEBUG) && defined(SYS_WINNT) 1515 if (debug) 1516 DebugBreak(); 1517#endif 1518 1519 abort(); 1520} 1521 1522 1523/* 1524 * library_fatal_error - Handle fatal errors from our libraries. 1525 */ 1526static void 1527library_fatal_error( 1528 const char *file, 1529 int line, 1530 const char *format, 1531 va_list args 1532 ) 1533{ 1534 char errbuf[256]; 1535 1536 isc_error_setfatal(NULL); /* Avoid recursion */ 1537 1538 msyslog(LOG_ERR, "%s:%d: fatal error:", file, line); 1539 vsnprintf(errbuf, sizeof(errbuf), format, args); 1540 msyslog(LOG_ERR, "%s", errbuf); 1541 msyslog(LOG_ERR, "exiting (due to fatal error in library)"); 1542 1543#if defined(DEBUG) && defined(SYS_WINNT) 1544 if (debug) 1545 DebugBreak(); 1546#endif 1547 1548 abort(); 1549} 1550 1551 1552/* 1553 * library_unexpected_error - Handle non fatal errors from our libraries. 1554 */ 1555# define MAX_UNEXPECTED_ERRORS 100 1556int unexpected_error_cnt = 0; 1557static void 1558library_unexpected_error( 1559 const char *file, 1560 int line, 1561 const char *format, 1562 va_list args 1563 ) 1564{ 1565 char errbuf[256]; 1566 1567 if (unexpected_error_cnt >= MAX_UNEXPECTED_ERRORS) 1568 return; /* avoid clutter in log */ 1569 1570 msyslog(LOG_ERR, "%s:%d: unexpected error:", file, line); 1571 vsnprintf(errbuf, sizeof(errbuf), format, args); 1572 msyslog(LOG_ERR, "%s", errbuf); 1573 1574 if (++unexpected_error_cnt == MAX_UNEXPECTED_ERRORS) 1575 msyslog(LOG_ERR, "Too many errors. Shutting up."); 1576 1577} 1578#endif /* !SIM */ 1579 1580#if !defined(SIM) && !defined(SYS_WINNT) 1581# ifdef DEBUG 1582 1583/* 1584 * moredebug - increase debugging verbosity 1585 */ 1586static RETSIGTYPE 1587moredebug( 1588 int sig 1589 ) 1590{ 1591 int saved_errno = errno; 1592 1593 if (debug < 255) 1594 { 1595 debug++; 1596 msyslog(LOG_DEBUG, "debug raised to %d", debug); 1597 } 1598 errno = saved_errno; 1599} 1600 1601 1602/* 1603 * lessdebug - decrease debugging verbosity 1604 */ 1605static RETSIGTYPE 1606lessdebug( 1607 int sig 1608 ) 1609{ 1610 int saved_errno = errno; 1611 1612 if (debug > 0) 1613 { 1614 debug--; 1615 msyslog(LOG_DEBUG, "debug lowered to %d", debug); 1616 } 1617 errno = saved_errno; 1618} 1619 1620# else /* !DEBUG follows */ 1621 1622 1623/* 1624 * no_debug - We don't do the debug here. 1625 */ 1626static RETSIGTYPE 1627no_debug( 1628 int sig 1629 ) 1630{ 1631 int saved_errno = errno; 1632 1633 msyslog(LOG_DEBUG, "ntpd not compiled for debugging (signal %d)", sig); 1634 errno = saved_errno; 1635} 1636# endif /* !DEBUG */ 1637#endif /* !SIM && !SYS_WINNT */ 1638