Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / ntpd / ntpd.c
blobccd6d813ff5d9a0ed22a73c4d9990153b09feb8c
1 /* $NetBSD: ntpd.c,v 1.14 2009/02/03 12:51:13 drochner Exp $ */
3 /*
4 * ntpd.c - main program for the fixed point NTP daemon
5 */
7 #ifdef HAVE_CONFIG_H
8 # include <config.h>
9 #endif
11 #include "ntp_machine.h"
12 #include "ntpd.h"
13 #include "ntp_io.h"
14 #include "ntp_stdlib.h"
15 #include <ntp_random.h>
17 #ifdef SIM
18 # include "ntpsim.h"
19 # include "ntpdsim-opts.h"
20 #else
21 # include "ntpd-opts.h"
22 #endif
24 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27 #ifdef HAVE_SYS_STAT_H
28 # include <sys/stat.h>
29 #endif
30 #include <stdio.h>
31 #if !defined(VMS) /*wjm*/
32 # ifdef HAVE_SYS_PARAM_H
33 # include <sys/param.h>
34 # endif
35 #endif /* VMS */
36 #ifdef HAVE_SYS_SIGNAL_H
37 # include <sys/signal.h>
38 #else
39 # include <signal.h>
40 #endif
41 #ifdef HAVE_SYS_IOCTL_H
42 # include <sys/ioctl.h>
43 #endif /* HAVE_SYS_IOCTL_H */
44 #ifdef HAVE_SYS_RESOURCE_H
45 # include <sys/resource.h>
46 #endif /* HAVE_SYS_RESOURCE_H */
47 #if defined(HAVE_RTPRIO)
48 # ifdef HAVE_SYS_RESOURCE_H
49 # include <sys/resource.h>
50 # endif
51 # ifdef HAVE_SYS_LOCK_H
52 # include <sys/lock.h>
53 # endif
54 # include <sys/rtprio.h>
55 #else
56 # ifdef HAVE_PLOCK
57 # ifdef HAVE_SYS_LOCK_H
58 # include <sys/lock.h>
59 # endif
60 # endif
61 #endif
62 #if defined(HAVE_SCHED_SETSCHEDULER)
63 # ifdef HAVE_SCHED_H
64 # include <sched.h>
65 # else
66 # ifdef HAVE_SYS_SCHED_H
67 # include <sys/sched.h>
68 # endif
69 # endif
70 #endif
71 #if defined(HAVE_SYS_MMAN_H)
72 # include <sys/mman.h>
73 #endif
75 #ifdef HAVE_TERMIOS_H
76 # include <termios.h>
77 #endif
79 #ifdef SYS_DOMAINOS
80 # include <apollo/base.h>
81 #endif /* SYS_DOMAINOS */
83 #include "recvbuff.h"
84 #include "ntp_cmdargs.h"
86 #if 0 /* HMS: I don't think we need this. 961223 */
87 #ifdef LOCK_PROCESS
88 # ifdef SYS_SOLARIS
89 # include <sys/mman.h>
90 # else
91 # include <sys/lock.h>
92 # endif
93 #endif
94 #endif
96 #ifdef _AIX
97 # include <ulimit.h>
98 #endif /* _AIX */
100 #ifdef SCO5_CLOCK
101 # include <sys/ci/ciioctl.h>
102 #endif
104 #ifdef HAVE_DROPROOT
105 # include <ctype.h>
106 # include <grp.h>
107 # include <pwd.h>
108 #ifdef HAVE_LINUX_CAPABILITIES
109 # include <sys/capability.h>
110 # include <sys/prctl.h>
111 #endif
112 #endif
115 * Signals we catch for debugging. If not debugging we ignore them.
117 #define MOREDEBUGSIG SIGUSR1
118 #define LESSDEBUGSIG SIGUSR2
121 * Signals which terminate us gracefully.
123 #ifndef SYS_WINNT
124 # define SIGDIE1 SIGHUP
125 # define SIGDIE3 SIGQUIT
126 # define SIGDIE2 SIGINT
127 # define SIGDIE4 SIGTERM
128 #endif /* SYS_WINNT */
130 #ifdef HAVE_DNSREGISTRATION
131 #include <dns_sd.h>
132 DNSServiceRef mdns;
133 #endif
136 * Scheduling priority we run at
138 #define NTPD_PRIO (-12)
140 int priority_done = 2; /* 0 - Set priority */
141 /* 1 - priority is OK where it is */
142 /* 2 - Don't set priority */
143 /* 1 and 2 are pretty much the same */
145 #ifdef DEBUG
147 * Debugging flag
149 volatile int debug = 0; /* No debugging by default */
150 #endif
152 int listen_to_virtual_ips = 1;
153 const char *specific_interface = NULL; /* interface name or IP address to bind to */
156 * No-fork flag. If set, we do not become a background daemon.
158 int nofork = 0; /* Fork by default */
160 #ifdef HAVE_DROPROOT
161 int droproot = 0;
162 char *user = NULL; /* User to switch to */
163 char *group = NULL; /* group to switch to */
164 char *chrootdir = NULL; /* directory to chroot to */
165 uid_t sw_uid;
166 gid_t sw_gid;
167 char *endp;
168 struct group *gr;
169 struct passwd *pw;
170 #endif /* HAVE_DROPROOT */
173 * Initializing flag. All async routines watch this and only do their
174 * thing when it is clear.
176 int initializing;
179 * Version declaration
181 extern const char *Version;
183 char const *progname;
185 int was_alarmed;
187 #ifdef DECL_SYSCALL
189 * We put this here, since the argument profile is syscall-specific
191 extern int syscall P((int, ...));
192 #endif /* DECL_SYSCALL */
195 #ifdef SIGDIE2
196 static RETSIGTYPE finish P((int));
197 #endif /* SIGDIE2 */
199 #ifdef DEBUG
200 #ifndef SYS_WINNT
201 static RETSIGTYPE moredebug P((int));
202 static RETSIGTYPE lessdebug P((int));
203 #endif
204 #else /* not DEBUG */
205 static RETSIGTYPE no_debug P((int));
206 #endif /* not DEBUG */
208 int ntpdmain P((int, char **));
209 static void set_process_priority P((void));
210 static void init_logging P((char const *));
211 static void setup_logfile P((void));
214 * Initialize the logging
216 void
217 init_logging(char const *name)
219 const char *cp;
222 * Logging. This may actually work on the gizmo board. Find a name
223 * to log with by using the basename
225 cp = strrchr(name, '/');
226 if (cp == 0)
227 cp = name;
228 else
229 cp++;
231 #if !defined(VMS)
233 # ifndef LOG_DAEMON
234 openlog(cp, LOG_PID);
235 # else /* LOG_DAEMON */
237 # ifndef LOG_NTP
238 # define LOG_NTP LOG_DAEMON
239 # endif
240 openlog(cp, LOG_PID | LOG_NDELAY, LOG_NTP);
241 # ifdef DEBUG
242 if (debug)
243 setlogmask(LOG_UPTO(LOG_DEBUG));
244 else
245 # endif /* DEBUG */
246 setlogmask(LOG_UPTO(LOG_DEBUG)); /* @@@ was INFO */
247 # endif /* LOG_DAEMON */
248 #endif /* !SYS_WINNT && !VMS */
250 NLOG(NLOG_SYSINFO) /* conditional if clause for conditional syslog */
251 msyslog(LOG_NOTICE, "%s", Version);
256 * See if we should redirect the logfile
259 void
260 setup_logfile(
261 void
264 if (HAVE_OPT( LOGFILE )) {
265 const char *my_optarg = OPT_ARG( LOGFILE );
266 FILE *new_file;
268 if(strcmp(my_optarg, "stderr") == 0)
269 new_file = stderr;
270 else if(strcmp(my_optarg, "stdout") == 0)
271 new_file = stdout;
272 else
273 new_file = fopen(my_optarg, "a");
274 if (new_file != NULL) {
275 NLOG(NLOG_SYSINFO)
276 msyslog(LOG_NOTICE, "logging to file %s", my_optarg);
277 if (syslog_file != NULL &&
278 fileno(syslog_file) != fileno(new_file))
279 (void)fclose(syslog_file);
281 syslog_file = new_file;
282 syslogit = 0;
284 else
285 msyslog(LOG_ERR,
286 "Cannot open log file %s",
287 my_optarg);
291 #ifdef SIM
293 main(
294 int argc,
295 char *argv[]
298 return ntpsim(argc, argv);
300 #else /* SIM */
301 #ifdef NO_MAIN_ALLOWED
302 CALL(ntpd,"ntpd",ntpdmain);
303 #else
304 #ifndef SYS_WINNT
306 main(
307 int argc,
308 char *argv[]
311 return ntpdmain(argc, argv);
313 #endif /* SYS_WINNT */
314 #endif /* NO_MAIN_ALLOWED */
315 #endif /* SIM */
317 #ifdef _AIX
319 * OK. AIX is different than solaris in how it implements plock().
320 * If you do NOT adjust the stack limit, you will get the MAXIMUM
321 * stack size allocated and PINNED with you program. To check the
322 * value, use ulimit -a.
324 * To fix this, we create an automatic variable and set our stack limit
325 * to that PLUS 32KB of extra space (we need some headroom).
327 * This subroutine gets the stack address.
329 * Grover Davidson and Matt Ladendorf
332 static char *
333 get_aix_stack(void)
335 char ch;
336 return (&ch);
340 * Signal handler for SIGDANGER.
342 static void
343 catch_danger(int signo)
345 msyslog(LOG_INFO, "ntpd: setpgid(): %m");
346 /* Make the system believe we'll free something, but don't do it! */
347 return;
349 #endif /* _AIX */
352 * Set the process priority
354 static void
355 set_process_priority(void)
358 #ifdef DEBUG
359 if (debug > 1)
360 msyslog(LOG_DEBUG, "set_process_priority: %s: priority_done is <%d>",
361 ((priority_done)
362 ? "Leave priority alone"
363 : "Attempt to set priority"
365 priority_done);
366 #endif /* DEBUG */
368 #ifdef SYS_WINNT
369 priority_done += NT_set_process_priority();
370 #endif
372 #if defined(HAVE_SCHED_SETSCHEDULER)
373 if (!priority_done) {
374 extern int config_priority_override, config_priority;
375 int pmax, pmin;
376 struct sched_param sched;
378 pmax = sched_get_priority_max(SCHED_FIFO);
379 sched.sched_priority = pmax;
380 if ( config_priority_override ) {
381 pmin = sched_get_priority_min(SCHED_FIFO);
382 if ( config_priority > pmax )
383 sched.sched_priority = pmax;
384 else if ( config_priority < pmin )
385 sched.sched_priority = pmin;
386 else
387 sched.sched_priority = config_priority;
389 if ( sched_setscheduler(0, SCHED_FIFO, &sched) == -1 )
390 msyslog(LOG_ERR, "sched_setscheduler(): %m");
391 else
392 ++priority_done;
394 #endif /* HAVE_SCHED_SETSCHEDULER */
395 #if defined(HAVE_RTPRIO)
396 # ifdef RTP_SET
397 if (!priority_done) {
398 struct rtprio srtp;
400 srtp.type = RTP_PRIO_REALTIME; /* was: RTP_PRIO_NORMAL */
401 srtp.prio = 0; /* 0 (hi) -> RTP_PRIO_MAX (31,lo) */
403 if (rtprio(RTP_SET, getpid(), &srtp) < 0)
404 msyslog(LOG_ERR, "rtprio() error: %m");
405 else
406 ++priority_done;
408 # else /* not RTP_SET */
409 if (!priority_done) {
410 if (rtprio(0, 120) < 0)
411 msyslog(LOG_ERR, "rtprio() error: %m");
412 else
413 ++priority_done;
415 # endif /* not RTP_SET */
416 #endif /* HAVE_RTPRIO */
417 #if defined(NTPD_PRIO) && NTPD_PRIO != 0
418 # ifdef HAVE_ATT_NICE
419 if (!priority_done) {
420 errno = 0;
421 if (-1 == nice (NTPD_PRIO) && errno != 0)
422 msyslog(LOG_ERR, "nice() error: %m");
423 else
424 ++priority_done;
426 # endif /* HAVE_ATT_NICE */
427 # ifdef HAVE_BSD_NICE
428 if (!priority_done) {
429 if (-1 == setpriority(PRIO_PROCESS, 0, NTPD_PRIO))
430 msyslog(LOG_ERR, "setpriority() error: %m");
431 else
432 ++priority_done;
434 # endif /* HAVE_BSD_NICE */
435 #endif /* NTPD_PRIO && NTPD_PRIO != 0 */
436 if (!priority_done)
437 msyslog(LOG_ERR, "set_process_priority: No way found to improve our priority");
442 * Main program. Initialize us, disconnect us from the tty if necessary,
443 * and loop waiting for I/O and/or timer expiries.
446 ntpdmain(
447 int argc,
448 char *argv[]
451 l_fp now;
452 struct recvbuf *rbuf;
453 #ifdef _AIX /* HMS: ifdef SIGDANGER? */
454 struct sigaction sa;
455 #endif
457 progname = argv[0];
459 initializing = 1; /* mark that we are initializing */
462 int optct = optionProcess(
463 #ifdef SIM
464 &ntpdsimOptions
465 #else
466 &ntpdOptions
467 #endif
468 , argc, argv);
469 argc -= optct;
470 argv += optct;
473 /* HMS: is this lame? Should we process -l first? */
475 init_logging(progname); /* Open the log file */
477 #ifdef HAVE_UMASK
479 mode_t uv;
481 uv = umask(0);
482 if(uv)
483 (void) umask(uv);
484 else
485 (void) umask(022);
487 #endif
489 #if defined(HAVE_GETUID) && !defined(MPE) /* MPE lacks the concept of root */
491 uid_t uid;
493 uid = getuid();
494 if (uid)
496 msyslog(LOG_ERR, "ntpd: must be run as root, not uid %ld", (long)uid);
497 printf("must be run as root, not uid %ld", (long)uid);
498 exit(1);
501 #endif
503 #ifdef OPENSSL
504 if ((SSLeay() ^ OPENSSL_VERSION_NUMBER) & ~0xff0L) {
505 msyslog(LOG_ERR,
506 "ntpd: OpenSSL version mismatch. Built against %lx, you have %lx\n",
507 (long) OPENSSL_VERSION_NUMBER, SSLeay());
508 exit(1);
510 #endif
512 /* getstartup(argc, argv); / * startup configuration, may set debug */
514 #ifdef DEBUG
515 debug = DESC(DEBUG_LEVEL).optOccCt;
516 if (debug)
517 printf("%s\n", Version);
518 #endif
521 * Enable the Multi-Media Timer for Windows?
523 #ifdef SYS_WINNT
524 if (HAVE_OPT( MODIFYMMTIMER ))
525 set_mm_timer(MM_TIMER_HIRES);
526 #endif
528 if (HAVE_OPT( NOFORK ) || HAVE_OPT( QUIT ))
529 nofork = 1;
531 if (HAVE_OPT( NOVIRTUALIPS ))
532 listen_to_virtual_ips = 0;
534 if (HAVE_OPT( INTERFACE )) {
535 #if 0
536 int ifacect = STACKCT_OPT( INTERFACE );
537 char** ifaces = STACKLST_OPT( INTERFACE );
539 /* malloc space for the array of names */
540 while (ifacect-- > 0) {
541 next_iface = *ifaces++;
543 #else
544 specific_interface = OPT_ARG( INTERFACE );
545 #endif
548 if (HAVE_OPT( NICE ))
549 priority_done = 0;
551 #if defined(HAVE_SCHED_SETSCHEDULER)
552 if (HAVE_OPT( PRIORITY )) {
553 config_priority = OPT_VALUE_PRIORITY;
554 config_priority_override = 1;
555 priority_done = 0;
557 #endif
559 #ifdef SYS_WINNT
561 * Initialize the time structures and variables
563 init_winnt_time();
564 #endif
566 setup_logfile();
569 * Initialize random generator and public key pair
571 get_systime(&now);
573 ntp_srandom((int)(now.l_i * now.l_uf));
575 #ifdef HAVE_DNSREGISTRATION
576 /* HMS: does this have to happen this early? */
577 msyslog(LOG_INFO, "Attemping to register mDNS");
578 if ( DNSServiceRegister (&mdns, 0, 0, NULL, "_ntp._udp", NULL, NULL, htons(NTP_PORT), 0, NULL, NULL, NULL) != kDNSServiceErr_NoError ) {
579 msyslog(LOG_ERR, "Unable to register mDNS");
581 #endif
583 #if !defined(VMS)
584 # ifndef NODETACH
586 * Detach us from the terminal. May need an #ifndef GIZMO.
588 if (
589 # ifdef DEBUG
590 !debug &&
591 # endif /* DEBUG */
592 !nofork)
594 # ifndef SYS_WINNT
595 # ifdef HAVE_DAEMON
596 daemon(0, 0);
597 # else /* not HAVE_DAEMON */
598 if (fork()) /* HMS: What about a -1? */
599 exit(0);
602 #if !defined(F_CLOSEM)
603 u_long s;
604 int max_fd;
605 #endif /* not F_CLOSEM */
607 #if defined(F_CLOSEM)
609 * From 'Writing Reliable AIX Daemons,' SG24-4946-00,
610 * by Eric Agar (saves us from doing 32767 system
611 * calls)
613 if (fcntl(0, F_CLOSEM, 0) == -1)
614 msyslog(LOG_ERR, "ntpd: failed to close open files(): %m");
615 #else /* not F_CLOSEM */
617 # if defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX)
618 max_fd = sysconf(_SC_OPEN_MAX);
619 # else /* HAVE_SYSCONF && _SC_OPEN_MAX */
620 max_fd = getdtablesize();
621 # endif /* HAVE_SYSCONF && _SC_OPEN_MAX */
622 for (s = 0; s < max_fd; s++)
623 (void) close((int)s);
624 #endif /* not F_CLOSEM */
625 (void) open("/", 0);
626 (void) dup2(0, 1);
627 (void) dup2(0, 2);
628 #ifdef SYS_DOMAINOS
630 uid_$t puid;
631 status_$t st;
633 proc2_$who_am_i(&puid);
634 proc2_$make_server(&puid, &st);
636 #endif /* SYS_DOMAINOS */
637 #if defined(HAVE_SETPGID) || defined(HAVE_SETSID)
638 # ifdef HAVE_SETSID
639 if (setsid() == (pid_t)-1)
640 msyslog(LOG_ERR, "ntpd: setsid(): %m");
641 # else
642 if (setpgid(0, 0) == -1)
643 msyslog(LOG_ERR, "ntpd: setpgid(): %m");
644 # endif
645 #else /* HAVE_SETPGID || HAVE_SETSID */
647 # if defined(TIOCNOTTY)
648 int fid;
650 fid = open("/dev/tty", 2);
651 if (fid >= 0)
653 (void) ioctl(fid, (u_long) TIOCNOTTY, (char *) 0);
654 (void) close(fid);
656 # endif /* defined(TIOCNOTTY) */
657 # ifdef HAVE_SETPGRP_0
658 (void) setpgrp();
659 # else /* HAVE_SETPGRP_0 */
660 (void) setpgrp(0, getpid());
661 # endif /* HAVE_SETPGRP_0 */
663 #endif /* HAVE_SETPGID || HAVE_SETSID */
664 #ifdef _AIX
665 /* Don't get killed by low-on-memory signal. */
666 sa.sa_handler = catch_danger;
667 sigemptyset(&sa.sa_mask);
668 sa.sa_flags = SA_RESTART;
670 (void) sigaction(SIGDANGER, &sa, NULL);
671 #endif /* _AIX */
673 # endif /* not HAVE_DAEMON */
674 # endif /* SYS_WINNT */
676 # endif /* NODETACH */
677 #endif /* VMS */
679 setup_logfile(); /* We lost any redirect when we daemonized */
681 #ifdef SCO5_CLOCK
683 * SCO OpenServer's system clock offers much more precise timekeeping
684 * on the base CPU than the other CPUs (for multiprocessor systems),
685 * so we must lock to the base CPU.
688 int fd = open("/dev/at1", O_RDONLY);
689 if (fd >= 0) {
690 int zero = 0;
691 if (ioctl(fd, ACPU_LOCK, &zero) < 0)
692 msyslog(LOG_ERR, "cannot lock to base CPU: %m");
693 close( fd );
694 } /* else ...
695 * If we can't open the device, this probably just isn't
696 * a multiprocessor system, so we're A-OK.
699 #endif
701 #if defined(HAVE_MLOCKALL) && defined(MCL_CURRENT) && defined(MCL_FUTURE)
702 # ifdef HAVE_SETRLIMIT
704 * Set the stack limit to something smaller, so that we don't lock a lot
705 * of unused stack memory.
708 struct rlimit rl;
710 /* HMS: must make the rlim_cur amount configurable */
711 if (getrlimit(RLIMIT_STACK, &rl) != -1
712 && (rl.rlim_cur = 50 * 4096) < rl.rlim_max)
714 if (setrlimit(RLIMIT_STACK, &rl) == -1)
716 msyslog(LOG_ERR,
717 "Cannot adjust stack limit for mlockall: %m");
720 # ifdef RLIMIT_MEMLOCK
722 * The default RLIMIT_MEMLOCK is very low on Linux systems.
723 * Unless we increase this limit malloc calls are likely to
724 * fail if we drop root privlege. To be useful the value
725 * has to be larger than the largest ntpd resident set size.
727 rl.rlim_cur = rl.rlim_max = 32*1024*1024;
728 if (setrlimit(RLIMIT_MEMLOCK, &rl) == -1) {
729 msyslog(LOG_ERR, "Cannot set RLIMIT_MEMLOCK: %m");
731 # endif /* RLIMIT_MEMLOCK */
733 # endif /* HAVE_SETRLIMIT */
735 * lock the process into memory
737 if (mlockall(MCL_CURRENT|MCL_FUTURE) < 0)
738 msyslog(LOG_ERR, "mlockall(): %m");
739 #else /* not (HAVE_MLOCKALL && MCL_CURRENT && MCL_FUTURE) */
740 # ifdef HAVE_PLOCK
741 # ifdef PROCLOCK
742 # ifdef _AIX
744 * set the stack limit for AIX for plock().
745 * see get_aix_stack() for more info.
747 if (ulimit(SET_STACKLIM, (get_aix_stack() - 8*4096)) < 0)
749 msyslog(LOG_ERR,"Cannot adjust stack limit for plock on AIX: %m");
751 # endif /* _AIX */
753 * lock the process into memory
755 if (plock(PROCLOCK) < 0)
756 msyslog(LOG_ERR, "plock(PROCLOCK): %m");
757 # else /* not PROCLOCK */
758 # ifdef TXTLOCK
760 * Lock text into ram
762 if (plock(TXTLOCK) < 0)
763 msyslog(LOG_ERR, "plock(TXTLOCK) error: %m");
764 # else /* not TXTLOCK */
765 msyslog(LOG_ERR, "plock() - don't know what to lock!");
766 # endif /* not TXTLOCK */
767 # endif /* not PROCLOCK */
768 # endif /* HAVE_PLOCK */
769 #endif /* not (HAVE_MLOCKALL && MCL_CURRENT && MCL_FUTURE) */
772 * Set up signals we pay attention to locally.
774 #ifdef SIGDIE1
775 (void) signal_no_reset(SIGDIE1, finish);
776 #endif /* SIGDIE1 */
777 #ifdef SIGDIE2
778 (void) signal_no_reset(SIGDIE2, finish);
779 #endif /* SIGDIE2 */
780 #ifdef SIGDIE3
781 (void) signal_no_reset(SIGDIE3, finish);
782 #endif /* SIGDIE3 */
783 #ifdef SIGDIE4
784 (void) signal_no_reset(SIGDIE4, finish);
785 #endif /* SIGDIE4 */
787 #ifdef SIGBUS
788 (void) signal_no_reset(SIGBUS, finish);
789 #endif /* SIGBUS */
791 #if !defined(SYS_WINNT) && !defined(VMS)
792 # ifdef DEBUG
793 (void) signal_no_reset(MOREDEBUGSIG, moredebug);
794 (void) signal_no_reset(LESSDEBUGSIG, lessdebug);
795 # else
796 (void) signal_no_reset(MOREDEBUGSIG, no_debug);
797 (void) signal_no_reset(LESSDEBUGSIG, no_debug);
798 # endif /* DEBUG */
799 #endif /* !SYS_WINNT && !VMS */
802 * Set up signals we should never pay attention to.
804 #if defined SIGPIPE
805 (void) signal_no_reset(SIGPIPE, SIG_IGN);
806 #endif /* SIGPIPE */
809 * Call the init_ routines to initialize the data structures.
811 * Exactly what command-line options are we expecting here?
813 init_auth();
814 init_util();
815 init_restrict();
816 init_mon();
817 init_timer();
818 init_lib();
819 init_request();
820 init_control();
821 init_peer();
822 #ifdef REFCLOCK
823 init_refclock();
824 #endif
825 set_process_priority();
826 init_proto(); /* Call at high priority */
827 init_io();
828 init_loopfilter();
829 mon_start(MON_ON); /* monitor on by default now */
830 /* turn off in config if unwanted */
833 * Get the configuration. This is done in a separate module
834 * since this will definitely be different for the gizmo board.
837 getconfig(argc, argv);
839 loop_config(LOOP_DRIFTCOMP, old_drift / 1e6);
840 #ifdef OPENSSL
841 crypto_setup();
842 #endif /* OPENSSL */
843 initializing = 0;
845 #ifdef HAVE_DROPROOT
846 if( droproot ) {
847 /* Drop super-user privileges and chroot now if the OS supports this */
849 #ifdef HAVE_LINUX_CAPABILITIES
850 /* set flag: keep privileges accross setuid() call (we only really need cap_sys_time): */
851 if( prctl( PR_SET_KEEPCAPS, 1L, 0L, 0L, 0L ) == -1 ) {
852 msyslog( LOG_ERR, "prctl( PR_SET_KEEPCAPS, 1L ) failed: %m" );
853 exit(-1);
855 #else
856 /* we need a user to switch to */
857 if( user == NULL ) {
858 msyslog(LOG_ERR, "Need user name to drop root privileges (see -u flag!)" );
859 exit(-1);
861 #endif /* HAVE_LINUX_CAPABILITIES */
863 if (user != NULL) {
864 if (isdigit((unsigned char)*user)) {
865 sw_uid = (uid_t)strtoul(user, &endp, 0);
866 if (*endp != '\0')
867 goto getuser;
868 } else {
869 getuser:
870 if ((pw = getpwnam(user)) != NULL) {
871 sw_uid = pw->pw_uid;
872 } else {
873 errno = 0;
874 msyslog(LOG_ERR, "Cannot find user `%s'", user);
875 exit (-1);
879 if (group != NULL) {
880 if (isdigit((unsigned char)*group)) {
881 sw_gid = (gid_t)strtoul(group, &endp, 0);
882 if (*endp != '\0')
883 goto getgroup;
884 } else {
885 getgroup:
886 if ((gr = getgrnam(group)) != NULL) {
887 sw_gid = gr->gr_gid;
888 } else {
889 errno = 0;
890 msyslog(LOG_ERR, "Cannot find group `%s'", group);
891 exit (-1);
896 if( chrootdir ) {
897 /* make sure cwd is inside the jail: */
898 if( chdir(chrootdir) ) {
899 msyslog(LOG_ERR, "Cannot chdir() to `%s': %m", chrootdir);
900 exit (-1);
902 if( chroot(chrootdir) ) {
903 msyslog(LOG_ERR, "Cannot chroot() to `%s': %m", chrootdir);
904 exit (-1);
907 if (group && setgid(sw_gid)) {
908 msyslog(LOG_ERR, "Cannot setgid() to group `%s': %m", group);
909 exit (-1);
911 if (group && setegid(sw_gid)) {
912 msyslog(LOG_ERR, "Cannot setegid() to group `%s': %m", group);
913 exit (-1);
915 if (group)
916 setgroups(1, &sw_gid);
917 else
918 initgroups(pw->pw_name, pw->pw_gid);
919 if (user && setuid(sw_uid)) {
920 msyslog(LOG_ERR, "Cannot setuid() to user `%s': %m", user);
921 exit (-1);
923 if (user && seteuid(sw_uid)) {
924 msyslog(LOG_ERR, "Cannot seteuid() to user `%s': %m", user);
925 exit (-1);
928 #ifndef HAVE_LINUX_CAPABILITIES
930 * for now assume that the privilege to bind to privileged ports
931 * is associated with running with uid 0 - should be refined on
932 * ports that allow binding to NTP_PORT with uid != 0
934 disable_dynamic_updates |= (sw_uid != 0); /* also notifies routing message listener */
935 #endif
937 if (disable_dynamic_updates && interface_interval) {
938 interface_interval = 0;
939 msyslog(LOG_INFO, "running in unprivileged mode disables dynamic interface tracking");
942 #ifdef HAVE_LINUX_CAPABILITIES
943 do {
945 * We may be running under non-root uid now, but we still hold full root privileges!
946 * We drop all of them, except for the crucial one or two: cap_sys_time and
947 * cap_net_bind_service if doing dynamic interface tracking.
949 cap_t caps;
950 char *captext = interface_interval ?
951 "cap_sys_time,cap_net_bind_service=ipe" :
952 "cap_sys_time=ipe";
953 if( ! ( caps = cap_from_text( captext ) ) ) {
954 msyslog( LOG_ERR, "cap_from_text() failed: %m" );
955 exit(-1);
957 if( cap_set_proc( caps ) == -1 ) {
958 msyslog( LOG_ERR, "cap_set_proc() failed to drop root privileges: %m" );
959 exit(-1);
961 cap_free( caps );
962 } while(0);
963 #endif /* HAVE_LINUX_CAPABILITIES */
965 } /* if( droproot ) */
966 #endif /* HAVE_DROPROOT */
969 * Report that we're up to any trappers
971 report_event(EVNT_SYSRESTART, (struct peer *)0);
974 * Use select() on all on all input fd's for unlimited
975 * time. select() will terminate on SIGALARM or on the
976 * reception of input. Using select() means we can't do
977 * robust signal handling and we get a potential race
978 * between checking for alarms and doing the select().
979 * Mostly harmless, I think.
981 /* On VMS, I suspect that select() can't be interrupted
982 * by a "signal" either, so I take the easy way out and
983 * have select() time out after one second.
984 * System clock updates really aren't time-critical,
985 * and - lacking a hardware reference clock - I have
986 * yet to learn about anything else that is.
988 #if defined(HAVE_IO_COMPLETION_PORT)
990 for (;;) {
991 GetReceivedBuffers();
992 #else /* normal I/O */
994 BLOCK_IO_AND_ALARM();
995 was_alarmed = 0;
996 for (;;)
998 # if !defined(HAVE_SIGNALED_IO)
999 extern fd_set activefds;
1000 extern int maxactivefd;
1002 fd_set rdfdes;
1003 int nfound;
1004 # endif
1006 if (alarm_flag) /* alarmed? */
1008 was_alarmed = 1;
1009 alarm_flag = 0;
1012 if (!was_alarmed && has_full_recv_buffer() == ISC_FALSE)
1015 * Nothing to do. Wait for something.
1017 # ifndef HAVE_SIGNALED_IO
1018 rdfdes = activefds;
1019 # if defined(VMS) || defined(SYS_VXWORKS)
1020 /* make select() wake up after one second */
1022 struct timeval t1;
1024 t1.tv_sec = 1; t1.tv_usec = 0;
1025 nfound = select(maxactivefd+1, &rdfdes, (fd_set *)0,
1026 (fd_set *)0, &t1);
1028 # else
1029 nfound = select(maxactivefd+1, &rdfdes, (fd_set *)0,
1030 (fd_set *)0, (struct timeval *)0);
1031 # endif /* VMS */
1032 if (nfound > 0)
1034 l_fp ts;
1036 get_systime(&ts);
1038 (void)input_handler(&ts);
1040 else if (nfound == -1 && errno != EINTR)
1041 netsyslog(LOG_ERR, "select() error: %m");
1042 # ifdef DEBUG
1043 else if (debug > 5)
1044 netsyslog(LOG_DEBUG, "select(): nfound=%d, error: %m", nfound);
1045 # endif /* DEBUG */
1046 # else /* HAVE_SIGNALED_IO */
1048 wait_for_signal();
1049 # endif /* HAVE_SIGNALED_IO */
1050 if (alarm_flag) /* alarmed? */
1052 was_alarmed = 1;
1053 alarm_flag = 0;
1057 if (was_alarmed)
1059 UNBLOCK_IO_AND_ALARM();
1061 * Out here, signals are unblocked. Call timer routine
1062 * to process expiry.
1064 timer();
1065 was_alarmed = 0;
1066 BLOCK_IO_AND_ALARM();
1069 #endif /* HAVE_IO_COMPLETION_PORT */
1071 #ifdef DEBUG_TIMING
1073 l_fp pts;
1074 l_fp tsa, tsb;
1075 int bufcount = 0;
1077 get_systime(&pts);
1078 tsa = pts;
1079 #endif
1080 rbuf = get_full_recv_buffer();
1081 while (rbuf != NULL)
1083 if (alarm_flag)
1085 was_alarmed = 1;
1086 alarm_flag = 0;
1088 UNBLOCK_IO_AND_ALARM();
1090 if (was_alarmed)
1091 { /* avoid timer starvation during lengthy I/O handling */
1092 timer();
1093 was_alarmed = 0;
1097 * Call the data procedure to handle each received
1098 * packet.
1100 if (rbuf->receiver != NULL) /* This should always be true */
1102 #ifdef DEBUG_TIMING
1103 l_fp dts = pts;
1105 L_SUB(&dts, &rbuf->recv_time);
1106 DPRINTF(2, ("processing timestamp delta %s (with prec. fuzz)\n", lfptoa(&dts, 9)));
1107 collect_timing(rbuf, "buffer processing delay", 1, &dts);
1108 bufcount++;
1109 #endif
1110 (rbuf->receiver)(rbuf);
1111 } else {
1112 msyslog(LOG_ERR, "receive buffer corruption - receiver found to be NULL - ABORTING");
1113 abort();
1116 BLOCK_IO_AND_ALARM();
1117 freerecvbuf(rbuf);
1118 rbuf = get_full_recv_buffer();
1120 #ifdef DEBUG_TIMING
1121 get_systime(&tsb);
1122 L_SUB(&tsb, &tsa);
1123 if (bufcount) {
1124 collect_timing(NULL, "processing", bufcount, &tsb);
1125 DPRINTF(2, ("processing time for %d buffers %s\n", bufcount, lfptoa(&tsb, 9)));
1128 #endif
1131 * Go around again
1134 UNBLOCK_IO_AND_ALARM();
1135 return 1;
1139 #ifdef SIGDIE2
1141 * finish - exit gracefully
1143 static RETSIGTYPE
1144 finish(
1145 int sig
1149 msyslog(LOG_NOTICE, "ntpd exiting on signal %d", sig);
1150 write_stats();
1151 #ifdef HAVE_DNSREGISTRATION
1152 if (mdns != NULL)
1153 DNSServiceRefDeallocate(mdns);
1154 #endif
1156 switch (sig)
1158 # ifdef SIGBUS
1159 case SIGBUS:
1160 printf("\nfinish(SIGBUS)\n");
1161 exit(0);
1162 # endif
1163 case 0: /* Should never happen... */
1164 return;
1165 default:
1166 exit(0);
1169 #endif /* SIGDIE2 */
1172 #ifdef DEBUG
1173 #ifndef SYS_WINNT
1175 * moredebug - increase debugging verbosity
1177 static RETSIGTYPE
1178 moredebug(
1179 int sig
1182 int saved_errno = errno;
1184 if (debug < 255)
1186 debug++;
1187 msyslog(LOG_DEBUG, "debug raised to %d", debug);
1189 errno = saved_errno;
1193 * lessdebug - decrease debugging verbosity
1195 static RETSIGTYPE
1196 lessdebug(
1197 int sig
1200 int saved_errno = errno;
1202 if (debug > 0)
1204 debug--;
1205 msyslog(LOG_DEBUG, "debug lowered to %d", debug);
1207 errno = saved_errno;
1209 #endif
1210 #else /* not DEBUG */
1211 #ifndef SYS_WINNT
1213 * no_debug - We don't do the debug here.
1215 static RETSIGTYPE
1216 no_debug(
1217 int sig
1220 int saved_errno = errno;
1222 msyslog(LOG_DEBUG, "ntpd not compiled for debugging (signal %d)", sig);
1223 errno = saved_errno;
1225 #endif /* not SYS_WINNT */
1226 #endif /* not DEBUG */