2 * ntpd.c - main program for the fixed point NTP daemon
9 #include "ntp_machine.h"
12 #include "ntp_stdlib.h"
13 #include <ntp_random.h>
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"
27 #include "ntp_libopts.h"
28 #include "ntpd-opts.h"
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
37 #ifdef HAVE_SYS_PARAM_H
38 # include <sys/param.h>
40 #ifdef HAVE_SYS_SIGNAL_H
41 # include <sys/signal.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 # include <sys/ioctl.h>
47 #endif /* HAVE_SYS_IOCTL_H */
48 #if defined(HAVE_RTPRIO)
49 # ifdef HAVE_SYS_LOCK_H
50 # include <sys/lock.h>
52 # include <sys/rtprio.h>
55 # ifdef HAVE_SYS_LOCK_H
56 # include <sys/lock.h>
60 #if defined(HAVE_SCHED_SETSCHEDULER)
64 # ifdef HAVE_SYS_SCHED_H
65 # include <sys/sched.h>
69 #if defined(HAVE_SYS_MMAN_H)
70 # include <sys/mman.h>
78 # include <apollo/base.h>
79 #endif /* SYS_DOMAINOS */
83 #include "ntp_cmdargs.h"
85 #if 0 /* HMS: I don't think we need this. 961223 */
88 # include <sys/mman.h>
90 # include <sys/lock.h>
100 # include <sys/ci/ciioctl.h>
107 #ifdef HAVE_LINUX_CAPABILITIES
108 # include <sys/capability.h>
109 # include <sys/prctl.h>
110 #endif /* HAVE_LINUX_CAPABILITIES */
111 #if defined(HAVE_PRIV_H) && defined(HAVE_SOLARIS_PRIVS)
113 #endif /* HAVE_PRIV_H */
114 #endif /* HAVE_DROPROOT */
116 #if defined (LIBSECCOMP) && (KERN_SECCOMP)
117 /* # include <sys/types.h> */
118 # include <sys/resource.h>
119 # include <seccomp.h>
120 #endif /* LIBSECCOMP and KERN_SECCOMP */
122 #ifdef HAVE_DNSREGISTRATION
127 #ifdef HAVE_SETPGRP_0
128 # define ntp_setpgrp(x, y) setpgrp()
130 # define ntp_setpgrp(x, y) setpgrp(x, y)
133 #ifdef HAVE_SOLARIS_PRIVS
134 # define LOWPRIVS "basic,sys_time,net_privaddr,proc_setid,!proc_info,!proc_session,!proc_exec"
135 static priv_set_t *lowprivs = NULL;
136 static priv_set_t *highprivs = NULL;
137 #endif /* HAVE_SOLARIS_PRIVS */
139 * Scheduling priority we run at
141 #define NTPD_PRIO (-12)
143 int priority_done = 2; /* 0 - Set priority */
144 /* 1 - priority is OK where it is */
145 /* 2 - Don't set priority */
146 /* 1 and 2 are pretty much the same */
148 int listen_to_virtual_ips = TRUE;
151 * No-fork flag. If set, we do not become a background daemon.
153 int nofork; /* Fork by default */
155 #ifdef HAVE_DNSREGISTRATION
157 * mDNS registration flag. If set, we attempt to register with the mDNS system, but only
158 * after we have synched the first time. If the attempt fails, then try again once per
159 * minute for up to 5 times. After all, we may be starting before mDNS.
163 #endif /* HAVE_DNSREGISTRATION */
168 char *user; /* User to switch to */
169 char *group; /* group to switch to */
170 const char *chrootdir; /* directory to chroot to */
176 #endif /* HAVE_DROPROOT */
178 #ifdef HAVE_WORKING_FORK
179 int waitsync_fd_to_close = -1; /* -w/--wait-sync */
183 * Initializing flag. All async routines watch this and only do their
184 * thing when it is clear.
189 * Version declaration
191 extern const char *Version;
193 char const *progname;
199 * We put this here, since the argument profile is syscall-specific
201 extern int syscall (int, ...);
202 #endif /* DECL_SYSCALL */
205 #if !defined(SIM) && defined(SIGDIE1)
206 static RETSIGTYPE finish (int);
209 #if !defined(SIM) && defined(HAVE_WORKING_FORK)
210 static int wait_child_sync_if (int, long);
213 #if !defined(SIM) && !defined(SYS_WINNT)
215 static RETSIGTYPE moredebug (int);
216 static RETSIGTYPE lessdebug (int);
217 # else /* !DEBUG follows */
218 static RETSIGTYPE no_debug (int);
220 #endif /* !SIM && !SYS_WINNT */
226 int ntpdmain (int, char **);
227 static void set_process_priority (void);
228 static void assertion_failed (const char *, int,
231 __attribute__ ((__noreturn__));
232 static void library_fatal_error (const char *, int,
233 const char *, va_list)
234 ISC_FORMAT_PRINTF(3, 0);
235 static void library_unexpected_error(const char *, int,
236 const char *, va_list)
237 ISC_FORMAT_PRINTF(3, 0);
253 optct = ntpOptionProcess(&ntpdOptions, *pargc, *pargv);
270 parse_cmdline_opts(&argc, &argv);
272 debug = OPT_VALUE_SET_DEBUG_LEVEL;
273 DPRINTF(1, ("%s\n", Version));
276 return ntpsim(argc, argv);
278 #else /* !SIM follows */
279 #ifdef NO_MAIN_ALLOWED
280 CALL(ntpd,"ntpd",ntpdmain);
281 #else /* !NO_MAIN_ALLOWED follows */
289 return ntpdmain(argc, argv);
291 #endif /* !SYS_WINNT */
292 #endif /* !NO_MAIN_ALLOWED */
297 * OK. AIX is different than solaris in how it implements plock().
298 * If you do NOT adjust the stack limit, you will get the MAXIMUM
299 * stack size allocated and PINNED with you program. To check the
300 * value, use ulimit -a.
302 * To fix this, we create an automatic variable and set our stack limit
303 * to that PLUS 32KB of extra space (we need some headroom).
305 * This subroutine gets the stack address.
307 * Grover Davidson and Matt Ladendorf
318 * Signal handler for SIGDANGER.
321 catch_danger(int signo)
323 msyslog(LOG_INFO, "ntpd: setpgid(): %m");
324 /* Make the system believe we'll free something, but don't do it! */
330 * Set the process priority
334 set_process_priority(void)
339 msyslog(LOG_DEBUG, "set_process_priority: %s: priority_done is <%d>",
341 ? "Leave priority alone"
342 : "Attempt to set priority"
347 # if defined(HAVE_SCHED_SETSCHEDULER)
348 if (!priority_done) {
349 extern int config_priority_override, config_priority;
351 struct sched_param sched;
353 pmax = sched_get_priority_max(SCHED_FIFO);
354 sched.sched_priority = pmax;
355 if ( config_priority_override ) {
356 pmin = sched_get_priority_min(SCHED_FIFO);
357 if ( config_priority > pmax )
358 sched.sched_priority = pmax;
359 else if ( config_priority < pmin )
360 sched.sched_priority = pmin;
362 sched.sched_priority = config_priority;
364 if ( sched_setscheduler(0, SCHED_FIFO, &sched) == -1 )
365 msyslog(LOG_ERR, "sched_setscheduler(): %m");
369 # endif /* HAVE_SCHED_SETSCHEDULER */
372 if (!priority_done) {
375 srtp.type = RTP_PRIO_REALTIME; /* was: RTP_PRIO_NORMAL */
376 srtp.prio = 0; /* 0 (hi) -> RTP_PRIO_MAX (31,lo) */
378 if (rtprio(RTP_SET, getpid(), &srtp) < 0)
379 msyslog(LOG_ERR, "rtprio() error: %m");
383 # else /* !RTP_SET follows */
384 if (!priority_done) {
385 if (rtprio(0, 120) < 0)
386 msyslog(LOG_ERR, "rtprio() error: %m");
390 # endif /* !RTP_SET */
391 # endif /* HAVE_RTPRIO */
392 # if defined(NTPD_PRIO) && NTPD_PRIO != 0
393 # ifdef HAVE_ATT_NICE
394 if (!priority_done) {
396 if (-1 == nice (NTPD_PRIO) && errno != 0)
397 msyslog(LOG_ERR, "nice() error: %m");
401 # endif /* HAVE_ATT_NICE */
402 # ifdef HAVE_BSD_NICE
403 if (!priority_done) {
404 if (-1 == setpriority(PRIO_PROCESS, 0, NTPD_PRIO))
405 msyslog(LOG_ERR, "setpriority() error: %m");
409 # endif /* HAVE_BSD_NICE */
410 # endif /* NTPD_PRIO && NTPD_PRIO != 0 */
412 msyslog(LOG_ERR, "set_process_priority: No way found to improve our priority");
418 * Main program. Initialize us, disconnect us from the tty if necessary,
419 * and loop waiting for I/O and/or timer expiries.
429 struct recvbuf *rbuf;
430 const char * logfilename;
434 # if defined(HAVE_GETUID) && !defined(MPE) /* MPE lacks the concept of root */
437 # if defined(HAVE_WORKING_FORK)
445 # if !defined(HAVE_SETSID) && !defined (HAVE_SETPGID) && defined(TIOCNOTTY)
448 # endif /* HAVE_WORKING_FORK*/
464 initializing = TRUE; /* mark that we are initializing */
465 parse_cmdline_opts(&argc, &argv);
467 debug = OPT_VALUE_SET_DEBUG_LEVEL;
468 # ifdef HAVE_SETLINEBUF
473 if (HAVE_OPT(NOFORK) || HAVE_OPT(QUIT)
477 || HAVE_OPT(SAVECONFIGQUIT))
480 init_logging(progname, NLOG_SYNCMASK, TRUE);
481 /* honor -l/--logfile option to log to a file */
482 if (HAVE_OPT(LOGFILE)) {
483 logfilename = OPT_ARG(LOGFILE);
485 change_logfile(logfilename, FALSE);
490 if (HAVE_OPT(SAVECONFIGQUIT))
493 msyslog(LOG_NOTICE, "%s: Starting", Version);
497 char buf[1024]; /* Secret knowledge of msyslog buf length */
500 /* Note that every arg has an initial space character */
501 snprintf(cp, sizeof(buf), "Command line:");
504 for (i = 0; i < saved_argc ; ++i) {
505 snprintf(cp, sizeof(buf) - (cp - buf),
506 " %s", saved_argv[i]);
509 msyslog(LOG_INFO, "%s", buf);
513 * Install trap handlers to log errors and assertion failures.
514 * Default handlers print to stderr which doesn't work if detached.
516 isc_assertion_setcallback(assertion_failed);
517 isc_error_setfatal(library_fatal_error);
518 isc_error_setunexpected(library_unexpected_error);
520 /* MPE lacks the concept of root */
521 # if defined(HAVE_GETUID) && !defined(MPE)
523 if (uid && !HAVE_OPT( SAVECONFIGQUIT )) {
526 "must be run as root, not uid %ld", (long)uid);
532 * Enable the Multi-Media Timer for Windows?
535 if (HAVE_OPT( MODIFYMMTIMER ))
536 set_mm_timer(MM_TIMER_HIRES);
539 #ifdef HAVE_DNSREGISTRATION
541 * Enable mDNS registrations?
543 if (HAVE_OPT( MDNS )) {
546 #endif /* HAVE_DNSREGISTRATION */
548 if (HAVE_OPT( NOVIRTUALIPS ))
549 listen_to_virtual_ips = 0;
552 * --interface, listen on specified interfaces
554 if (HAVE_OPT( INTERFACE )) {
555 int ifacect = STACKCT_OPT( INTERFACE );
556 const char** ifaces = STACKLST_OPT( INTERFACE );
559 while (ifacect-- > 0) {
561 is_ip_address(*ifaces, AF_UNSPEC, &addr)
564 *ifaces, -1, ACTION_LISTEN);
569 if (HAVE_OPT( NICE ))
572 # ifdef HAVE_SCHED_SETSCHEDULER
573 if (HAVE_OPT( PRIORITY )) {
574 config_priority = OPT_VALUE_PRIORITY;
575 config_priority_override = 1;
580 # ifdef HAVE_WORKING_FORK
581 do { /* 'loop' once */
582 if (!HAVE_OPT( WAIT_SYNC ))
584 wait_sync = OPT_VALUE_WAIT_SYNC;
585 if (wait_sync <= 0) {
589 /* -w requires a fork() even with debug > 0 */
591 if (pipe(pipe_fds)) {
592 exit_code = (errno) ? errno : -1;
594 "Pipe creation failed for --wait-sync: %m");
597 waitsync_fd_to_close = pipe_fds[1];
598 } while (0); /* 'loop' once */
599 # endif /* HAVE_WORKING_FORK */
604 * Start interpolation thread, must occur before first
610 * Initialize random generator and public key pair
614 ntp_srandom((int)(now.l_i * now.l_uf));
617 * Detach us from the terminal. May need an #ifndef GIZMO.
621 # ifdef HAVE_WORKING_FORK
624 exit_code = (errno) ? errno : -1;
625 msyslog(LOG_ERR, "fork: %m");
630 exit_code = wait_child_sync_if(pipe_fds[0],
637 * close all open files excepting waitsync_fd_to_close.
638 * msyslog() unreliable until after init_logging().
641 if (syslog_file != NULL) {
646 close_all_except(waitsync_fd_to_close);
647 INSIST(0 == open("/dev/null", 0) && 1 == dup2(0, 1) \
650 init_logging(progname, 0, TRUE);
651 /* we lost our logfile (if any) daemonizing */
652 setup_logfile(logfilename);
659 proc2_$who_am_i(&puid);
660 proc2_$make_server(&puid, &st);
662 # endif /* SYS_DOMAINOS */
664 if (setsid() == (pid_t)-1)
665 msyslog(LOG_ERR, "setsid(): %m");
666 # elif defined(HAVE_SETPGID)
667 if (setpgid(0, 0) == -1)
668 msyslog(LOG_ERR, "setpgid(): %m");
669 # else /* !HAVE_SETSID && !HAVE_SETPGID follows */
671 fid = open("/dev/tty", 2);
673 ioctl(fid, (u_long)TIOCNOTTY, NULL);
676 # endif /* TIOCNOTTY */
677 ntp_setpgrp(0, getpid());
678 # endif /* !HAVE_SETSID && !HAVE_SETPGID */
680 /* Don't get killed by low-on-memory signal. */
681 sa.sa_handler = catch_danger;
682 sigemptyset(&sa.sa_mask);
683 sa.sa_flags = SA_RESTART;
684 sigaction(SIGDANGER, &sa, NULL);
686 # endif /* HAVE_WORKING_FORK */
691 * SCO OpenServer's system clock offers much more precise timekeeping
692 * on the base CPU than the other CPUs (for multiprocessor systems),
693 * so we must lock to the base CPU.
695 fd = open("/dev/at1", O_RDONLY);
698 if (ioctl(fd, ACPU_LOCK, &zero) < 0)
699 msyslog(LOG_ERR, "cannot lock to base CPU: %m");
704 /* Setup stack size in preparation for locking pages in memory. */
705 # if defined(HAVE_MLOCKALL)
706 # ifdef HAVE_SETRLIMIT
707 ntp_rlimit(RLIMIT_STACK, DFLT_RLIMIT_STACK * 4096, 4096, "4k");
708 # ifdef RLIMIT_MEMLOCK
710 * The default RLIMIT_MEMLOCK is very low on Linux systems.
711 * Unless we increase this limit malloc calls are likely to
712 * fail if we drop root privilege. To be useful the value
713 * has to be larger than the largest ntpd resident set size.
715 ntp_rlimit(RLIMIT_MEMLOCK, DFLT_RLIMIT_MEMLOCK * 1024 * 1024, 1024 * 1024, "MB");
716 # endif /* RLIMIT_MEMLOCK */
717 # endif /* HAVE_SETRLIMIT */
718 # else /* !HAVE_MLOCKALL follows */
723 * set the stack limit for AIX for plock().
724 * see get_aix_stack() for more info.
726 if (ulimit(SET_STACKLIM, (get_aix_stack() - 8 * 4096)) < 0)
728 "Cannot adjust stack limit for plock: %m");
730 # endif /* PROCLOCK */
731 # endif /* HAVE_PLOCK */
732 # endif /* !HAVE_MLOCKALL */
735 * Set up signals we pay attention to locally.
738 signal_no_reset(SIGDIE1, finish);
739 signal_no_reset(SIGDIE2, finish);
740 signal_no_reset(SIGDIE3, finish);
741 signal_no_reset(SIGDIE4, finish);
744 signal_no_reset(SIGBUS, finish);
747 # if !defined(SYS_WINNT) && !defined(VMS)
749 (void) signal_no_reset(MOREDEBUGSIG, moredebug);
750 (void) signal_no_reset(LESSDEBUGSIG, lessdebug);
752 (void) signal_no_reset(MOREDEBUGSIG, no_debug);
753 (void) signal_no_reset(LESSDEBUGSIG, no_debug);
755 # endif /* !SYS_WINNT && !VMS */
758 * Set up signals we should never pay attention to.
761 signal_no_reset(SIGPIPE, SIG_IGN);
765 * Call the init_ routines to initialize the data structures.
767 * Exactly what command-line options are we expecting here?
781 set_process_priority();
782 init_proto(); /* Call at high priority */
785 mon_start(MON_ON); /* monitor on by default now */
786 /* turn off in config if unwanted */
789 * Get the configuration. This is done in a separate module
790 * since this will definitely be different for the gizmo board.
792 getconfig(argc, argv);
795 # if defined(HAVE_MLOCKALL)
797 * lock the process into memory
799 if (!HAVE_OPT(SAVECONFIGQUIT) &&
800 0 != mlockall(MCL_CURRENT|MCL_FUTURE))
801 msyslog(LOG_ERR, "mlockall(): %m");
802 # else /* !HAVE_MLOCKALL follows */
806 * lock the process into memory
808 if (!HAVE_OPT(SAVECONFIGQUIT) && 0 != plock(PROCLOCK))
809 msyslog(LOG_ERR, "plock(PROCLOCK): %m");
810 # else /* !PROCLOCK follows */
815 if (!HAVE_OPT(SAVECONFIGQUIT) && 0 != plock(TXTLOCK))
816 msyslog(LOG_ERR, "plock(TXTLOCK) error: %m");
817 # else /* !TXTLOCK follows */
818 msyslog(LOG_ERR, "plock() - don't know what to lock!");
819 # endif /* !TXTLOCK */
820 # endif /* !PROCLOCK */
821 # endif /* HAVE_PLOCK */
822 # endif /* !HAVE_MLOCKALL */
825 loop_config(LOOP_DRIFTINIT, 0);
826 report_event(EVNT_SYSRESTART, NULL, NULL);
827 initializing = FALSE;
829 # ifdef HAVE_DROPROOT
831 /* Drop super-user privileges and chroot now if the OS supports this */
833 # ifdef HAVE_LINUX_CAPABILITIES
834 /* set flag: keep privileges accross setuid() call (we only really need cap_sys_time): */
835 if (prctl( PR_SET_KEEPCAPS, 1L, 0L, 0L, 0L ) == -1) {
836 msyslog( LOG_ERR, "prctl( PR_SET_KEEPCAPS, 1L ) failed: %m" );
839 # elif HAVE_SOLARIS_PRIVS
840 /* Nothing to do here */
842 /* we need a user to switch to */
844 msyslog(LOG_ERR, "Need user name to drop root privileges (see -u flag!)" );
847 # endif /* HAVE_LINUX_CAPABILITIES || HAVE_SOLARIS_PRIVS */
850 if (isdigit((unsigned char)*user)) {
851 sw_uid = (uid_t)strtoul(user, &endp, 0);
855 if ((pw = getpwuid(sw_uid)) != NULL) {
857 user = estrdup(pw->pw_name);
861 msyslog(LOG_ERR, "Cannot find user ID %s", user);
868 if ((pw = getpwnam(user)) != NULL) {
873 msyslog(LOG_ERR, "getpwnam(%s) failed: %m", user);
875 msyslog(LOG_ERR, "Cannot find user `%s'", user);
881 if (isdigit((unsigned char)*group)) {
882 sw_gid = (gid_t)strtoul(group, &endp, 0);
887 if ((gr = getgrnam(group)) != NULL) {
891 msyslog(LOG_ERR, "Cannot find group `%s'", group);
898 /* make sure cwd is inside the jail: */
899 if (chdir(chrootdir)) {
900 msyslog(LOG_ERR, "Cannot chdir() to `%s': %m", chrootdir);
903 if (chroot(chrootdir)) {
904 msyslog(LOG_ERR, "Cannot chroot() to `%s': %m", chrootdir);
908 msyslog(LOG_ERR, "Cannot chdir() to`root after chroot(): %m");
912 # ifdef HAVE_SOLARIS_PRIVS
913 if ((lowprivs = priv_str_to_set(LOWPRIVS, ",", NULL)) == NULL) {
914 msyslog(LOG_ERR, "priv_str_to_set() failed:%m");
917 if ((highprivs = priv_allocset()) == NULL) {
918 msyslog(LOG_ERR, "priv_allocset() failed:%m");
921 (void) getppriv(PRIV_PERMITTED, highprivs);
922 (void) priv_intersect(highprivs, lowprivs);
923 if (setppriv(PRIV_SET, PRIV_PERMITTED, lowprivs) == -1) {
924 msyslog(LOG_ERR, "setppriv() failed:%m");
927 # endif /* HAVE_SOLARIS_PRIVS */
928 if (user && initgroups(user, sw_gid)) {
929 msyslog(LOG_ERR, "Cannot initgroups() to user `%s': %m", user);
932 if (group && setgid(sw_gid)) {
933 msyslog(LOG_ERR, "Cannot setgid() to group `%s': %m", group);
936 if (group && setegid(sw_gid)) {
937 msyslog(LOG_ERR, "Cannot setegid() to group `%s': %m", group);
941 setgroups(1, &sw_gid);
943 initgroups(pw->pw_name, pw->pw_gid);
944 if (user && setuid(sw_uid)) {
945 msyslog(LOG_ERR, "Cannot setuid() to user `%s': %m", user);
948 if (user && seteuid(sw_uid)) {
949 msyslog(LOG_ERR, "Cannot seteuid() to user `%s': %m", user);
953 # if !defined(HAVE_LINUX_CAPABILITIES) && !defined(HAVE_SOLARIS_PRIVS)
955 * for now assume that the privilege to bind to privileged ports
956 * is associated with running with uid 0 - should be refined on
957 * ports that allow binding to NTP_PORT with uid != 0
959 disable_dynamic_updates |= (sw_uid != 0); /* also notifies routing message listener */
960 # endif /* !HAVE_LINUX_CAPABILITIES && !HAVE_SOLARIS_PRIVS */
962 if (disable_dynamic_updates && interface_interval) {
963 interface_interval = 0;
964 msyslog(LOG_INFO, "running as non-root disables dynamic interface tracking");
967 # ifdef HAVE_LINUX_CAPABILITIES
970 * We may be running under non-root uid now, but we still hold full root privileges!
971 * We drop all of them, except for the crucial one or two: cap_sys_time and
972 * cap_net_bind_service if doing dynamic interface tracking.
977 captext = (0 != interface_interval)
978 ? "cap_sys_time,cap_net_bind_service=pe"
980 caps = cap_from_text(captext);
983 "cap_from_text(%s) failed: %m",
987 if (-1 == cap_set_proc(caps)) {
989 "cap_set_proc() failed to drop root privs: %m");
994 # endif /* HAVE_LINUX_CAPABILITIES */
995 # ifdef HAVE_SOLARIS_PRIVS
996 if (priv_delset(lowprivs, "proc_setid") == -1) {
997 msyslog(LOG_ERR, "priv_delset() failed:%m");
1000 if (setppriv(PRIV_SET, PRIV_PERMITTED, lowprivs) == -1) {
1001 msyslog(LOG_ERR, "setppriv() failed:%m");
1004 priv_freeset(lowprivs);
1005 priv_freeset(highprivs);
1006 # endif /* HAVE_SOLARIS_PRIVS */
1007 root_dropped = TRUE;
1008 fork_deferred_worker();
1009 } /* if (droproot) */
1010 # endif /* HAVE_DROPROOT */
1012 /* libssecomp sandboxing */
1013 #if defined (LIBSECCOMP) && (KERN_SECCOMP)
1014 scmp_filter_ctx ctx;
1016 if ((ctx = seccomp_init(SCMP_ACT_KILL)) < 0)
1017 msyslog(LOG_ERR, "%s: seccomp_init(SCMP_ACT_KILL) failed: %m", __func__);
1019 msyslog(LOG_DEBUG, "%s: seccomp_init(SCMP_ACT_KILL) succeeded", __func__);
1028 SCMP_SYS(clock_gettime),
1029 SCMP_SYS(clock_settime),
1032 SCMP_SYS(exit_group),
1036 SCMP_SYS(getitimer),
1037 SCMP_SYS(getsockname),
1048 SCMP_SYS(rt_sigaction),
1049 SCMP_SYS(rt_sigprocmask),
1050 SCMP_SYS(rt_sigreturn),
1053 SCMP_SYS(setitimer),
1063 SCMP_SYS(_newselect),
1067 SCMP_SYS(clock_gettime),
1068 SCMP_SYS(clock_settime),
1070 SCMP_SYS(exit_group),
1073 SCMP_SYS(getitimer),
1082 SCMP_SYS(rt_sigaction),
1083 SCMP_SYS(rt_sigprocmask),
1085 SCMP_SYS(setitimer),
1087 SCMP_SYS(sigprocmask),
1088 SCMP_SYS(sigreturn),
1089 SCMP_SYS(socketcall),
1098 for (i = 0; i < COUNTOF(scmp_sc); i++) {
1099 if (seccomp_rule_add(ctx,
1100 SCMP_ACT_ALLOW, scmp_sc[i], 0) < 0) {
1102 "%s: seccomp_rule_add() failed: %m",
1108 if (seccomp_load(ctx) < 0)
1109 msyslog(LOG_ERR, "%s: seccomp_load() failed: %m",
1112 msyslog(LOG_DEBUG, "%s: seccomp_load() succeeded", __func__);
1114 #endif /* LIBSECCOMP and KERN_SECCOMP */
1116 # ifdef HAVE_IO_COMPLETION_PORT
1119 GetReceivedBuffers();
1120 # else /* normal I/O */
1122 BLOCK_IO_AND_ALARM();
1123 was_alarmed = FALSE;
1126 if (alarm_flag) { /* alarmed? */
1131 if (!was_alarmed && !has_full_recv_buffer()) {
1133 * Nothing to do. Wait for something.
1138 if (alarm_flag) { /* alarmed? */
1144 UNBLOCK_IO_AND_ALARM();
1146 * Out here, signals are unblocked. Call timer routine
1147 * to process expiry.
1150 was_alarmed = FALSE;
1151 BLOCK_IO_AND_ALARM();
1154 # endif /* !HAVE_IO_COMPLETION_PORT */
1156 # ifdef DEBUG_TIMING
1165 rbuf = get_full_recv_buffer();
1166 while (rbuf != NULL) {
1171 UNBLOCK_IO_AND_ALARM();
1174 /* avoid timer starvation during lengthy I/O handling */
1176 was_alarmed = FALSE;
1180 * Call the data procedure to handle each received
1183 if (rbuf->receiver != NULL) {
1184 # ifdef DEBUG_TIMING
1187 L_SUB(&dts, &rbuf->recv_time);
1188 DPRINTF(2, ("processing timestamp delta %s (with prec. fuzz)\n", lfptoa(&dts, 9)));
1189 collect_timing(rbuf, "buffer processing delay", 1, &dts);
1192 (*rbuf->receiver)(rbuf);
1194 msyslog(LOG_ERR, "fatal: receive buffer callback NULL");
1198 BLOCK_IO_AND_ALARM();
1200 rbuf = get_full_recv_buffer();
1202 # ifdef DEBUG_TIMING
1206 collect_timing(NULL, "processing", bufcount, &tsb);
1207 DPRINTF(2, ("processing time for %d buffers %s\n", bufcount, lfptoa(&tsb, 9)));
1216 # ifdef HAVE_DNSREGISTRATION
1217 if (mdnsreg && (current_time - mdnsreg ) > 60 && mdnstries && sys_leap != LEAP_NOTINSYNC) {
1218 mdnsreg = current_time;
1219 msyslog(LOG_INFO, "Attempting to register mDNS");
1220 if ( DNSServiceRegister (&mdns, 0, 0, NULL, "_ntp._udp", NULL, NULL,
1221 htons(NTP_PORT), 0, NULL, NULL, NULL) != kDNSServiceErr_NoError ) {
1223 msyslog(LOG_ERR, "Unable to register mDNS, giving up.");
1225 msyslog(LOG_INFO, "Unable to register mDNS, will try later.");
1228 msyslog(LOG_INFO, "mDNS service registered.");
1232 # endif /* HAVE_DNSREGISTRATION */
1235 UNBLOCK_IO_AND_ALARM();
1241 #if !defined(SIM) && defined(SIGDIE1)
1243 * finish - exit gracefully
1250 const char *sig_desc;
1253 #ifdef HAVE_STRSIGNAL
1254 sig_desc = strsignal(sig);
1256 if (sig_desc == NULL)
1258 msyslog(LOG_NOTICE, "%s exiting on signal %d (%s)", progname,
1260 /* See Bug 2513 and Bug 2522 re the unlink of PIDFILE */
1261 # ifdef HAVE_DNSREGISTRATION
1263 DNSServiceRefDeallocate(mdns);
1268 #endif /* !SIM && SIGDIE1 */
1273 * wait_child_sync_if - implements parent side of -w/--wait-sync
1275 # ifdef HAVE_WORKING_FORK
1284 time_t wait_end_time;
1288 struct timeval wtimeout;
1293 /* waitsync_fd_to_close used solely by child */
1294 close(waitsync_fd_to_close);
1295 wait_end_time = time(NULL) + wait_sync;
1297 cur_time = time(NULL);
1298 wait_rem = (wait_end_time > cur_time)
1299 ? (wait_end_time - cur_time)
1301 wtimeout.tv_sec = wait_rem;
1302 wtimeout.tv_usec = 0;
1304 FD_SET(pipe_read_fd, &readset);
1305 rc = select(pipe_read_fd + 1, &readset, NULL, NULL,
1310 exit_code = (errno) ? errno : -1;
1312 "--wait-sync select failed: %m");
1317 * select() indicated a timeout, but in case
1318 * its timeouts are affected by a step of the
1319 * system clock, select() again with a zero
1320 * timeout to confirm.
1323 FD_SET(pipe_read_fd, &readset);
1324 wtimeout.tv_sec = 0;
1325 wtimeout.tv_usec = 0;
1326 rc = select(pipe_read_fd + 1, &readset, NULL,
1328 if (0 == rc) /* select() timeout */
1332 } else /* readable */
1334 } while (wait_rem > 0);
1336 fprintf(stderr, "%s: -w/--wait-sync %ld timed out.\n",
1337 progname, wait_sync);
1340 # endif /* HAVE_WORKING_FORK */
1344 * assertion_failed - Redirect assertion failures to msyslog().
1350 isc_assertiontype_t type,
1354 isc_assertion_setcallback(NULL); /* Avoid recursion */
1356 msyslog(LOG_ERR, "%s:%d: %s(%s) failed",
1357 file, line, isc_assertion_typetotext(type), cond);
1358 msyslog(LOG_ERR, "exiting (due to assertion failure)");
1360 #if defined(DEBUG) && defined(SYS_WINNT)
1370 * library_fatal_error - Handle fatal errors from our libraries.
1373 library_fatal_error(
1382 isc_error_setfatal(NULL); /* Avoid recursion */
1384 msyslog(LOG_ERR, "%s:%d: fatal error:", file, line);
1385 vsnprintf(errbuf, sizeof(errbuf), format, args);
1386 msyslog(LOG_ERR, "%s", errbuf);
1387 msyslog(LOG_ERR, "exiting (due to fatal error in library)");
1389 #if defined(DEBUG) && defined(SYS_WINNT)
1399 * library_unexpected_error - Handle non fatal errors from our libraries.
1401 # define MAX_UNEXPECTED_ERRORS 100
1402 int unexpected_error_cnt = 0;
1404 library_unexpected_error(
1413 if (unexpected_error_cnt >= MAX_UNEXPECTED_ERRORS)
1414 return; /* avoid clutter in log */
1416 msyslog(LOG_ERR, "%s:%d: unexpected error:", file, line);
1417 vsnprintf(errbuf, sizeof(errbuf), format, args);
1418 msyslog(LOG_ERR, "%s", errbuf);
1420 if (++unexpected_error_cnt == MAX_UNEXPECTED_ERRORS)
1421 msyslog(LOG_ERR, "Too many errors. Shutting up.");
1426 #if !defined(SIM) && !defined(SYS_WINNT)
1430 * moredebug - increase debugging verbosity
1437 int saved_errno = errno;
1442 msyslog(LOG_DEBUG, "debug raised to %d", debug);
1444 errno = saved_errno;
1449 * lessdebug - decrease debugging verbosity
1456 int saved_errno = errno;
1461 msyslog(LOG_DEBUG, "debug lowered to %d", debug);
1463 errno = saved_errno;
1466 # else /* !DEBUG follows */
1470 * no_debug - We don't do the debug here.
1477 int saved_errno = errno;
1479 msyslog(LOG_DEBUG, "ntpd not compiled for debugging (signal %d)", sig);
1480 errno = saved_errno;
1482 # endif /* !DEBUG */
1483 #endif /* !SIM && !SYS_WINNT */