#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "ntp_machine.h"
#include "ntpd.h"
#include "ntp_io.h"
#include "ntp_stdlib.h"
#include <ntp_random.h>
#ifdef SIM
#include "ntpsim.h"
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#include <stdio.h>
#ifndef SYS_WINNT
# if !defined(VMS)
# ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
# endif
# endif
# ifdef HAVE_SYS_SIGNAL_H
# include <sys/signal.h>
# else
# include <signal.h>
# endif
# ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
# endif
# ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
# endif
#else
# include <signal.h>
# include <process.h>
# include <io.h>
# include <clockstuff.h>
#include "ntp_iocompletionport.h"
#endif
#if defined(HAVE_RTPRIO)
# ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
# endif
# ifdef HAVE_SYS_LOCK_H
# include <sys/lock.h>
# endif
# include <sys/rtprio.h>
#else
# ifdef HAVE_PLOCK
# ifdef HAVE_SYS_LOCK_H
# include <sys/lock.h>
# endif
# endif
#endif
#if defined(HAVE_SCHED_SETSCHEDULER)
# ifdef HAVE_SCHED_H
# include <sched.h>
# else
# ifdef HAVE_SYS_SCHED_H
# include <sys/sched.h>
# endif
# endif
#endif
#if defined(HAVE_SYS_MMAN_H)
# include <sys/mman.h>
#endif
#ifdef HAVE_TERMIOS_H
# include <termios.h>
#endif
#ifdef SYS_DOMAINOS
# include <apollo/base.h>
#endif
#include "recvbuff.h"
#include "ntp_cmdargs.h"
#if 0
#ifdef LOCK_PROCESS
# ifdef SYS_SOLARIS
# include <sys/mman.h>
# else
# include <sys/lock.h>
# endif
#endif
#endif
#ifdef _AIX
# include <ulimit.h>
#endif
#ifdef SCO5_CLOCK
# include <sys/ci/ciioctl.h>
#endif
#ifdef HAVE_DROPROOT
# include <ctype.h>
# include <grp.h>
# include <pwd.h>
#ifdef HAVE_LINUX_CAPABILITIES
# include <sys/capability.h>
# include <sys/prctl.h>
#endif
#endif
#ifdef __APPLE__
#include <notify.h>
#endif
#define MOREDEBUGSIG SIGUSR1
#define LESSDEBUGSIG SIGUSR2
#ifndef SYS_WINNT
# define SIGDIE1 SIGHUP
# define SIGDIE3 SIGQUIT
# define SIGDIE2 SIGINT
# define SIGDIE4 SIGTERM
#endif
#ifdef HAVE_DNSREGISTRATION
#include <dns_sd.h>
DNSServiceRef mdns;
#endif
#define NTPD_PRIO (-12)
int priority_done = 2;
volatile int debug;
volatile int info_flag;
int forground_process = FALSE;
int nofork;
#ifdef HAVE_DROPROOT
int droproot = 0;
char *user = NULL;
char *group = NULL;
char *chrootdir = NULL;
int sw_uid;
int sw_gid;
char *endp;
struct group *gr;
struct passwd *pw;
#endif
int initializing;
extern const char *Version;
int was_alarmed;
#ifdef DECL_SYSCALL
extern int syscall P((int, ...));
#endif
#ifdef SIGDIE2
static RETSIGTYPE finish P((int));
#endif
#ifdef DEBUG
#ifndef SYS_WINNT
static RETSIGTYPE moredebug P((int));
static RETSIGTYPE lessdebug P((int));
#endif
#else
static RETSIGTYPE no_debug P((int));
#endif
static RETSIGTYPE info P((int));
int ntpdmain P((int, char **));
static void set_process_priority P((void));
static void init_logging P((char *));
void
init_logging(char *name)
{
char *cp;
cp = strrchr(name, '/');
if (cp == 0)
cp = name;
else
cp++;
#if !defined(VMS)
# ifndef LOG_DAEMON
openlog(cp, LOG_PID);
# else
# ifndef LOG_NTP
# define LOG_NTP LOG_DAEMON
# endif
openlog(cp, LOG_PID | LOG_NDELAY, LOG_NTP);
# ifdef DEBUG
if (debug)
setlogmask(LOG_UPTO(LOG_DEBUG));
else
# endif
setlogmask(LOG_UPTO(LOG_DEBUG));
# endif
#endif
NLOG(NLOG_SYSINFO)
msyslog(LOG_INFO, "%s", Version);
}
#ifdef SIM
int
main(
int argc,
char *argv[]
)
{
return ntpsim(argc, argv);
}
#else
#ifdef NO_MAIN_ALLOWED
CALL(ntpd,"ntpd",ntpdmain);
#else
#ifndef SYS_WINNT
int
main(
int argc,
char *argv[]
)
{
return ntpdmain(argc, argv);
}
#endif
#endif
#endif
#ifdef _AIX
static char *
get_aix_stack(void)
{
char ch;
return (&ch);
}
static void
catch_danger(int signo)
{
msyslog(LOG_INFO, "ntpd: setpgid(): %m");
return;
}
#endif
static void
set_process_priority(void)
{
#ifdef DEBUG
if (debug > 1)
msyslog(LOG_DEBUG, "set_process_priority: %s: priority_done is <%d>",
((priority_done)
? "Leave priority alone"
: "Attempt to set priority"
),
priority_done);
#endif
#ifdef SYS_WINNT
priority_done += NT_set_process_priority();
#endif
#if defined(HAVE_SCHED_SETSCHEDULER)
if (!priority_done) {
extern int config_priority_override, config_priority;
int pmax, pmin;
struct sched_param sched;
pmax = sched_get_priority_max(SCHED_FIFO);
sched.sched_priority = pmax;
if ( config_priority_override ) {
pmin = sched_get_priority_min(SCHED_FIFO);
if ( config_priority > pmax )
sched.sched_priority = pmax;
else if ( config_priority < pmin )
sched.sched_priority = pmin;
else
sched.sched_priority = config_priority;
}
if ( sched_setscheduler(0, SCHED_FIFO, &sched) == -1 )
msyslog(LOG_ERR, "sched_setscheduler(): %m");
else
++priority_done;
}
#endif
#if defined(HAVE_RTPRIO)
# ifdef RTP_SET
if (!priority_done) {
struct rtprio srtp;
srtp.type = RTP_PRIO_REALTIME;
srtp.prio = 0;
if (rtprio(RTP_SET, getpid(), &srtp) < 0)
msyslog(LOG_ERR, "rtprio() error: %m");
else
++priority_done;
}
# else
if (!priority_done) {
if (rtprio(0, 120) < 0)
msyslog(LOG_ERR, "rtprio() error: %m");
else
++priority_done;
}
# endif
#endif
#if defined(NTPD_PRIO) && NTPD_PRIO != 0
# ifdef HAVE_ATT_NICE
if (!priority_done) {
errno = 0;
if (-1 == nice (NTPD_PRIO) && errno != 0)
msyslog(LOG_ERR, "nice() error: %m");
else
++priority_done;
}
# endif
# ifdef HAVE_BSD_NICE
if (!priority_done) {
if (-1 == setpriority(PRIO_PROCESS, 0, NTPD_PRIO))
msyslog(LOG_ERR, "setpriority() error: %m");
else
++priority_done;
}
# endif
#endif
if (!priority_done)
msyslog(LOG_ERR, "set_process_priority: No way found to improve our priority");
}
int
ntpdmain(
int argc,
char *argv[]
)
{
l_fp now;
struct recvbuf *rbuf;
#ifdef _AIX
struct sigaction sa;
#endif
initializing = 1;
debug = 0;
nofork = 0;
init_logging(argv[0]);
#ifdef HAVE_UMASK
{
mode_t uv;
uv = umask(0);
if(uv)
(void) umask(uv);
else
(void) umask(022);
}
#endif
#if defined(HAVE_GETUID) && !defined(MPE)
{
uid_t uid;
uid = getuid();
if (uid)
{
msyslog(LOG_ERR, "ntpd: must be run as root, not uid %ld", (long)uid);
exit(1);
}
}
#endif
#ifdef OPENSSL
if ((SSLeay() ^ OPENSSL_VERSION_NUMBER) & ~0xff0L) {
msyslog(LOG_ERR,
"ntpd: OpenSSL version mismatch. Built against %lx, you have %lx\n",
OPENSSL_VERSION_NUMBER, SSLeay());
exit(1);
}
#endif
#ifdef SYS_WINNT
init_winnt_time();
#endif
getstartup(argc, argv);
if (debug)
printf("%s\n", Version);
get_systime(&now);
ntp_srandom((int)(now.l_i * now.l_uf));
#ifdef HAVE_DNSREGISTRATION
msyslog(LOG_INFO, "Attemping to register mDNS\n");
if ( DNSServiceRegister (&mdns, 0, 0, NULL, "_ntp._udp", NULL, NULL, htons(NTP_PORT), 0, NULL, NULL, NULL) != kDNSServiceErr_NoError ) {
msyslog(LOG_ERR, "Unable to register mDNS\n");
}
#endif
#if !defined(VMS)
# ifndef NODETACH
# ifdef DEBUG
if (!debug && !nofork)
# else
if (!nofork)
# endif
{
# ifndef SYS_WINNT
# ifdef HAVE_DAEMON
daemon(0, 0);
# else
if (fork())
exit(0);
{
#if !defined(F_CLOSEM)
u_long s;
int max_fd;
#endif
#if defined(F_CLOSEM)
if (fcntl(0, F_CLOSEM, 0) == -1)
msyslog(LOG_ERR, "ntpd: failed to close open files(): %m");
#else
# if defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX)
max_fd = sysconf(_SC_OPEN_MAX);
# else
max_fd = getdtablesize();
# endif
for (s = 0; s < max_fd; s++)
(void) close((int)s);
#endif
(void) open("/", 0);
(void) dup2(0, 1);
(void) dup2(0, 2);
#ifdef SYS_DOMAINOS
{
uid_$t puid;
status_$t st;
proc2_$who_am_i(&puid);
proc2_$make_server(&puid, &st);
}
#endif
#if defined(HAVE_SETPGID) || defined(HAVE_SETSID)
# ifdef HAVE_SETSID
if (setsid() == (pid_t)-1)
msyslog(LOG_ERR, "ntpd: setsid(): %m");
# else
if (setpgid(0, 0) == -1)
msyslog(LOG_ERR, "ntpd: setpgid(): %m");
# endif
#else
{
# if defined(TIOCNOTTY)
int fid;
fid = open("/dev/tty", 2);
if (fid >= 0)
{
(void) ioctl(fid, (u_long) TIOCNOTTY, (char *) 0);
(void) close(fid);
}
# endif
# ifdef HAVE_SETPGRP_0
(void) setpgrp();
# else
(void) setpgrp(0, getpid());
# endif
}
#endif
#ifdef _AIX
sa.sa_handler = catch_danger;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
(void) sigaction(SIGDANGER, &sa, NULL);
#endif
}
# endif
# endif
}
# endif
#endif
debug = 0;
getstartup(argc, argv);
#ifdef SCO5_CLOCK
{
int fd = open("/dev/at1", O_RDONLY);
if (fd >= 0) {
int zero = 0;
if (ioctl(fd, ACPU_LOCK, &zero) < 0)
msyslog(LOG_ERR, "cannot lock to base CPU: %m\n");
close( fd );
}
}
#endif
#if defined(HAVE_MLOCKALL) && defined(MCL_CURRENT) && defined(MCL_FUTURE)
# ifdef HAVE_SETRLIMIT
{
struct rlimit rl;
if (getrlimit(RLIMIT_STACK, &rl) != -1
&& (rl.rlim_cur = 50 * 4096) < rl.rlim_max)
{
if (setrlimit(RLIMIT_STACK, &rl) == -1)
{
msyslog(LOG_ERR,
"Cannot adjust stack limit for mlockall: %m");
}
}
# ifdef RLIMIT_MEMLOCK
rl.rlim_cur = rl.rlim_max = 32*1024*1024;
if (setrlimit(RLIMIT_MEMLOCK, &rl) == -1) {
msyslog(LOG_ERR, "Cannot set RLIMIT_MEMLOCK: %m");
}
# endif
}
# endif
# ifndef __APPLE__
if (mlockall(MCL_CURRENT|MCL_FUTURE) < 0)
msyslog(LOG_ERR, "mlockall(): %m");
# endif
#else
# ifdef HAVE_PLOCK
# ifdef PROCLOCK
# ifdef _AIX
if (ulimit(SET_STACKLIM, (get_aix_stack() - 8*4096)) < 0)
{
msyslog(LOG_ERR,"Cannot adjust stack limit for plock on AIX: %m");
}
# endif
if (plock(PROCLOCK) < 0)
msyslog(LOG_ERR, "plock(PROCLOCK): %m");
# else
# ifdef TXTLOCK
if (plock(TXTLOCK) < 0)
msyslog(LOG_ERR, "plock(TXTLOCK) error: %m");
# else
msyslog(LOG_ERR, "plock() - don't know what to lock!");
# endif
# endif
# endif
#endif
#ifdef SIGDIE1
(void) signal_no_reset(SIGDIE1, finish);
#endif
#ifdef SIGDIE2
(void) signal_no_reset(SIGDIE2, finish);
#endif
#ifdef SIGDIE3
(void) signal_no_reset(SIGDIE3, finish);
#endif
#ifdef SIGDIE4
(void) signal_no_reset(SIGDIE4, finish);
#endif
#ifdef SIGBUS
(void) signal_no_reset(SIGBUS, finish);
#endif
#if !defined(SYS_WINNT) && !defined(VMS)
# ifdef DEBUG
(void) signal_no_reset(MOREDEBUGSIG, moredebug);
(void) signal_no_reset(LESSDEBUGSIG, lessdebug);
# else
(void) signal_no_reset(MOREDEBUGSIG, no_debug);
(void) signal_no_reset(LESSDEBUGSIG, no_debug);
# endif
#endif
#if defined SIGPIPE
(void) signal_no_reset(SIGPIPE, SIG_IGN);
#endif
#ifdef __APPLE__
int token;
(void) signal_no_reset(SIGINFO, info);
notify_register_signal("com.apple.system.config.network_change", SIGINFO, &token);
#endif
init_auth();
init_util();
init_restrict();
init_mon();
init_timer();
#if defined (HAVE_IO_COMPLETION_PORT)
init_io_completion_port();
#endif
init_lib();
init_request();
init_control();
init_peer();
#ifdef REFCLOCK
init_refclock();
#endif
set_process_priority();
init_proto();
init_io();
init_loopfilter();
mon_start(MON_ON);
#ifdef DEBUG
debug = 0;
#endif
getconfig(argc, argv);
loop_config(LOOP_DRIFTCOMP, old_drift / 1e6);
#ifdef OPENSSL
crypto_setup();
#endif
initializing = 0;
#ifdef HAVE_DROPROOT
if( droproot ) {
#ifdef HAVE_LINUX_CAPABILITIES
if( prctl( PR_SET_KEEPCAPS, 1L, 0L, 0L, 0L ) == -1 ) {
msyslog( LOG_ERR, "prctl( PR_SET_KEEPCAPS, 1L ) failed: %m" );
exit(-1);
}
#else
if( user == NULL ) {
msyslog(LOG_ERR, "Need user name to drop root privileges (see -u flag!)" );
exit(-1);
}
#endif
if (user != NULL) {
if (isdigit((unsigned char)*user)) {
sw_uid = (uid_t)strtoul(user, &endp, 0);
if (*endp != '\0')
goto getuser;
} else {
getuser:
if ((pw = getpwnam(user)) != NULL) {
sw_uid = pw->pw_uid;
} else {
errno = 0;
msyslog(LOG_ERR, "Cannot find user `%s'", user);
exit (-1);
}
}
}
if (group != NULL) {
if (isdigit((unsigned char)*group)) {
sw_gid = (gid_t)strtoul(group, &endp, 0);
if (*endp != '\0')
goto getgroup;
} else {
getgroup:
if ((gr = getgrnam(group)) != NULL) {
sw_gid = gr->gr_gid;
} else {
errno = 0;
msyslog(LOG_ERR, "Cannot find group `%s'", group);
exit (-1);
}
}
}
if( chrootdir ) {
if( chdir(chrootdir) ) {
msyslog(LOG_ERR, "Cannot chdir() to `%s': %m", chrootdir);
exit (-1);
}
if( chroot(chrootdir) ) {
msyslog(LOG_ERR, "Cannot chroot() to `%s': %m", chrootdir);
exit (-1);
}
}
if (group && setgid(sw_gid)) {
msyslog(LOG_ERR, "Cannot setgid() to group `%s': %m", group);
exit (-1);
}
if (group && setegid(sw_gid)) {
msyslog(LOG_ERR, "Cannot setegid() to group `%s': %m", group);
exit (-1);
}
if (user && setuid(sw_uid)) {
msyslog(LOG_ERR, "Cannot setuid() to user `%s': %m", user);
exit (-1);
}
if (user && seteuid(sw_uid)) {
msyslog(LOG_ERR, "Cannot seteuid() to user `%s': %m", user);
exit (-1);
}
#ifdef HAVE_LINUX_CAPABILITIES
do {
cap_t caps;
if( ! ( caps = cap_from_text( "cap_sys_time=ipe" ) ) ) {
msyslog( LOG_ERR, "cap_from_text() failed: %m" );
exit(-1);
}
if( cap_set_proc( caps ) == -1 ) {
msyslog( LOG_ERR, "cap_set_proc() failed to drop root privileges: %m" );
exit(-1);
}
cap_free( caps );
} while(0);
#endif
}
#endif
report_event(EVNT_SYSRESTART, (struct peer *)0);
#if defined(HAVE_IO_COMPLETION_PORT)
for (;;) {
int tot_full_recvbufs = GetReceivedBuffers();
#else
#if defined(HAVE_SIGNALED_IO)
block_io_and_alarm();
# endif
was_alarmed = 0;
for (;;)
{
# if !defined(HAVE_SIGNALED_IO)
extern fd_set activefds;
extern int maxactivefd;
fd_set rdfdes;
int nfound;
# endif
if (alarm_flag)
{
was_alarmed = 1;
alarm_flag = 0;
}
if (info_flag) {
info_flag = 0;
rebind_timer = current_time + 10;
}
if (!was_alarmed && has_full_recv_buffer() == ISC_FALSE)
{
# ifndef HAVE_SIGNALED_IO
rdfdes = activefds;
# if defined(VMS) || defined(SYS_VXWORKS)
{
struct timeval t1;
t1.tv_sec = 1; t1.tv_usec = 0;
nfound = select(maxactivefd+1, &rdfdes, (fd_set *)0,
(fd_set *)0, &t1);
}
# else
nfound = select(maxactivefd+1, &rdfdes, (fd_set *)0,
(fd_set *)0, (struct timeval *)0);
# endif
if (nfound > 0)
{
l_fp ts;
get_systime(&ts);
(void)input_handler(&ts);
}
else if (nfound == -1 && errno != EINTR)
netsyslog(LOG_ERR, "select() error: %m");
# ifdef DEBUG
else if (debug > 5)
netsyslog(LOG_DEBUG, "select(): nfound=%d, error: %m", nfound);
# endif
# else
wait_for_signal();
# endif
if (alarm_flag)
{
was_alarmed = 1;
alarm_flag = 0;
}
}
if (was_alarmed)
{
# ifdef HAVE_SIGNALED_IO
unblock_io_and_alarm();
# endif
timer();
was_alarmed = 0;
# ifdef HAVE_SIGNALED_IO
block_io_and_alarm();
# endif
}
#endif
rbuf = get_full_recv_buffer();
while (rbuf != NULL)
{
# ifdef HAVE_SIGNALED_IO
unblock_io_and_alarm();
# endif
if (rbuf->receiver != NULL)
{
(rbuf->receiver)(rbuf);
} else {
msyslog(LOG_ERR, "receive buffer corruption - receiver found to be NULL - ABORTING");
abort();
}
# ifdef HAVE_SIGNALED_IO
block_io_and_alarm();
# endif
freerecvbuf(rbuf);
rbuf = get_full_recv_buffer();
}
}
# ifdef HAVE_SIGNALED_IO
unblock_io_and_alarm();
# endif
return 1;
}
#ifdef SIGDIE2
static RETSIGTYPE
finish(
int sig
)
{
msyslog(LOG_INFO, "ntpd exiting on signal %d", sig);
write_stats();
#ifdef HAVE_DNSREGISTRATION
if (mdns != NULL)
DNSServiceRefDeallocate(mdns);
#endif
switch (sig)
{
# ifdef SIGBUS
case SIGBUS:
printf("\nfinish(SIGBUS)\n");
exit(0);
# endif
case 0:
return;
# if defined(SIGHUP) || defined(SIGINT) || defined(SIGTERM)
# ifdef SIGHUP
case SIGHUP:
# endif
# ifdef SIGINT
case SIGINT:
# endif
# ifdef SIGTERM
case SIGTERM:
# endif
save_drift_file(1);
# endif
default:
exit(0);
}
}
#endif
#ifdef DEBUG
#ifndef SYS_WINNT
static RETSIGTYPE
moredebug(
int sig
)
{
int saved_errno = errno;
if (debug < 255)
{
debug++;
msyslog(LOG_DEBUG, "debug raised to %d", debug);
}
errno = saved_errno;
}
static RETSIGTYPE
lessdebug(
int sig
)
{
int saved_errno = errno;
if (debug > 0)
{
debug--;
msyslog(LOG_DEBUG, "debug lowered to %d", debug);
}
errno = saved_errno;
}
#endif
#else
#ifndef SYS_WINNT
static RETSIGTYPE
no_debug(
int sig
)
{
int saved_errno = errno;
msyslog(LOG_DEBUG, "ntpd not compiled for debugging (signal %d)", sig);
errno = saved_errno;
}
#endif
#endif
static RETSIGTYPE
info(
int sig
)
{
info_flag = 1;
}