Deleted Added
sdiff udiff text old ( 106163 ) new ( 132451 )
full compact
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
14#ifdef SIM
15#include "ntpsim.h"
16#endif
17
18#ifdef HAVE_UNISTD_H
19# include <unistd.h>
20#endif
21#ifdef HAVE_SYS_STAT_H
22# include <sys/stat.h>
23#endif
24#include <stdio.h>
25#ifndef SYS_WINNT

--- 13 unchanged lines hidden (view full) ---

39# ifdef HAVE_SYS_RESOURCE_H
40# include <sys/resource.h>
41# endif /* HAVE_SYS_RESOURCE_H */
42#else
43# include <signal.h>
44# include <process.h>
45# include <io.h>
46# include "../libntp/log.h"
47# include <clockstuff.h>
48# include <crtdbg.h>
49#endif /* SYS_WINNT */
50#if defined(HAVE_RTPRIO)
51# ifdef HAVE_SYS_RESOURCE_H
52# include <sys/resource.h>
53# endif
54# ifdef HAVE_SYS_LOCK_H
55# include <sys/lock.h>

--- 43 unchanged lines hidden (view full) ---

99#ifdef _AIX
100# include <ulimit.h>
101#endif /* _AIX */
102
103#ifdef SCO5_CLOCK
104# include <sys/ci/ciioctl.h>
105#endif
106
107#ifdef HAVE_CLOCKCTL
108# include <ctype.h>
109# include <grp.h>
110# include <pwd.h>
111#endif
112
113/*
114 * Signals we catch for debugging. If not debugging we ignore them.
115 */
116#define MOREDEBUGSIG SIGUSR1
117#define LESSDEBUGSIG SIGUSR2
118
119/*

--- 5 unchanged lines hidden (view full) ---

125# define SIGDIE2 SIGINT
126# define SIGDIE4 SIGTERM
127#endif /* SYS_WINNT */
128
129#if defined SYS_WINNT
130/* handles for various threads, process, and objects */
131HANDLE ResolverThreadHandle = NULL;
132/* variables used to inform the Service Control Manager of our current state */
133BOOL NoWinService = FALSE;
134SERVICE_STATUS ssStatus;
135SERVICE_STATUS_HANDLE sshStatusHandle;
136HANDLE WaitHandles[3] = { NULL, NULL, NULL };
137char szMsgPath[255];
138static BOOL WINAPI OnConsoleEvent(DWORD dwCtrlType);
139BOOL init_randfile();
140#endif /* SYS_WINNT */
141
142/*
143 * Scheduling priority we run at
144 */
145#define NTPD_PRIO (-12)
146
147int priority_done = 2; /* 0 - Set priority */
148 /* 1 - priority is OK where it is */
149 /* 2 - Don't set priority */
150 /* 1 and 2 are pretty much the same */
151
152/*
153 * Debugging flag
154 */
155volatile int debug;
156
157/*
158 * Set the processing not to be in the forground
159 */
160int forground_process = FALSE;
161
162/*
163 * No-fork flag. If set, we do not become a background daemon.
164 */
165int nofork;
166
167#ifdef HAVE_CLOCKCTL
168char *user = NULL; /* User to switch to */
169char *group = NULL; /* group to switch to */
170char *chrootdir = NULL; /* directory to chroot to */
171int sw_uid;
172int sw_gid;
173char *endp;
174struct group *gr;
175struct passwd *pw;
176#endif /* HAVE_CLOCKCTL */
177
178/*
179 * Initializing flag. All async routines watch this and only do their
180 * thing when it is clear.
181 */
182int initializing;
183
184/*
185 * Version declaration

--- 10 unchanged lines hidden (view full) ---

196#endif /* DECL_SYSCALL */
197
198
199#ifdef SIGDIE2
200static RETSIGTYPE finish P((int));
201#endif /* SIGDIE2 */
202
203#ifdef DEBUG
204#ifndef SYS_WINNT
205static RETSIGTYPE moredebug P((int));
206static RETSIGTYPE lessdebug P((int));
207#endif
208#else /* not DEBUG */
209static RETSIGTYPE no_debug P((int));
210#endif /* not DEBUG */
211
212int ntpdmain P((int, char **));
213static void set_process_priority P((void));
214
215#ifdef SIM
216int
217main(
218 int argc,
219 char *argv[]
220 )
221{
222 return ntpsim(argc, argv);
223}
224#else /* SIM */
225#ifdef NO_MAIN_ALLOWED
226CALL(ntpd,"ntpd",ntpdmain);
227#else
228int
229main(
230 int argc,
231 char *argv[]
232 )
233{
234 return ntpdmain(argc, argv);
235}
236#endif
237#endif /* SIM */
238
239#ifdef _AIX
240/*
241 * OK. AIX is different than solaris in how it implements plock().
242 * If you do NOT adjust the stack limit, you will get the MAXIMUM
243 * stack size allocated and PINNED with you program. To check the
244 * value, use ulimit -a.
245 *

--- 121 unchanged lines hidden (view full) ---

367int
368ntpdmain(
369 int argc,
370 char *argv[]
371 )
372{
373 l_fp now;
374 char *cp;
375 struct recvbuf *rbuflist;
376 struct recvbuf *rbuf;
377#ifdef _AIX /* HMS: ifdef SIGDANGER? */
378 struct sigaction sa;
379#endif
380
381 initializing = 1; /* mark that we are initializing */
382 debug = 0; /* no debugging by default */

--- 29 unchanged lines hidden (view full) ---

412 if (!GetModuleFileName(NULL, szMsgPath, sizeof(szMsgPath))) {
413 msyslog(LOG_ERR, "GetModuleFileName(PGM_EXE_FILE) failed: %m\n");
414 exit(1);
415 }
416 addSourceToRegistry("NTP", szMsgPath);
417#endif
418 getstartup(argc, argv); /* startup configuration, may set debug */
419
420 if (debug)
421 printf("%s\n", Version);
422
423 /*
424 * Initialize random generator and public key pair
425 */
426#ifdef SYS_WINNT
427 /* Initialize random file before OpenSSL checks */
428 if(!init_randfile())
429 msyslog(LOG_ERR, "Unable to initialize .rnd file\n");
430#endif
431 get_systime(&now);
432 SRANDOM((int)(now.l_i * now.l_uf));
433
434#if !defined(VMS)
435# ifndef NODETACH
436 /*
437 * Detach us from the terminal. May need an #ifndef GIZMO.
438 */

--- 11 unchanged lines hidden (view full) ---

450 exit(0);
451
452 {
453#if !defined(F_CLOSEM)
454 u_long s;
455 int max_fd;
456#endif /* not F_CLOSEM */
457
458#if defined(F_CLOSEM)
459 /*
460 * From 'Writing Reliable AIX Daemons,' SG24-4946-00,
461 * by Eric Agar (saves us from doing 32767 system
462 * calls)
463 */
464 if (fcntl(0, F_CLOSEM, 0) == -1)
465 msyslog(LOG_ERR, "ntpd: failed to close open files(): %m");
466#else /* not F_CLOSEM */
467
468# if defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX)
469 max_fd = sysconf(_SC_OPEN_MAX);
470# else /* HAVE_SYSCONF && _SC_OPEN_MAX */
471 max_fd = getdtablesize();

--- 48 unchanged lines hidden (view full) ---

520
521 (void) sigaction(SIGDANGER, &sa, NULL);
522#endif /* _AIX */
523 }
524# endif /* not HAVE_DAEMON */
525# else /* SYS_WINNT */
526
527 {
528 if (NoWinService == FALSE) {
529 SERVICE_TABLE_ENTRY dispatchTable[] = {
530 { TEXT("NetworkTimeProtocol"), (LPSERVICE_MAIN_FUNCTION)service_main },
531 { NULL, NULL }
532 };
533
534 /* daemonize */
535 if (!StartServiceCtrlDispatcher(dispatchTable))
536 {
537 msyslog(LOG_ERR, "StartServiceCtrlDispatcher: %m");
538 ExitProcess(2);
539 }
540 }
541 else {
542 service_main(argc, argv);
543 return 0;
544 }
545 }
546# endif /* SYS_WINNT */
547 }
548# endif /* NODETACH */
549# if defined(SYS_WINNT) && !defined(NODETACH)
550 else
551 service_main(argc, argv);
552 return 0; /* must return a value */

--- 9 unchanged lines hidden (view full) ---

562service_main(
563 DWORD argc,
564 LPTSTR *argv
565 )
566{
567 char *cp;
568 struct recvbuf *rbuflist;
569 struct recvbuf *rbuf;
570
571 if(!debug && NoWinService == FALSE)
572 {
573 /* register our service control handler */
574 sshStatusHandle = RegisterServiceCtrlHandler( TEXT("NetworkTimeProtocol"),
575 (LPHANDLER_FUNCTION)service_ctrl);
576 if(sshStatusHandle == 0)
577 {
578 msyslog(LOG_ERR, "RegisterServiceCtrlHandler failed: %m");
579 return;
580 }
581
582 /* report pending status to Service Control Manager */
583 ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
584 ssStatus.dwCurrentState = SERVICE_START_PENDING;

--- 22 unchanged lines hidden (view full) ---

607 if (cp == 0)
608 cp = argv[0];
609 else
610 cp++;
611
612 debug = 0; /* will be immediately re-initialized 8-( */
613 getstartup(argc, argv); /* startup configuration, catch logfile this time */
614
615#if !defined(VMS)
616
617# ifndef LOG_DAEMON
618 openlog(cp, LOG_PID);
619# else /* LOG_DAEMON */
620
621# ifndef LOG_NTP
622# define LOG_NTP LOG_DAEMON
623# endif

--- 40 unchanged lines hidden (view full) ---

664 } /* else ...
665 * If we can't open the device, this probably just isn't
666 * a multiprocessor system, so we're A-OK.
667 */
668 }
669#endif
670
671#if defined(HAVE_MLOCKALL) && defined(MCL_CURRENT) && defined(MCL_FUTURE)
672# ifdef HAVE_SETRLIMIT
673 /*
674 * Set the stack limit to something smaller, so that we don't lock a lot
675 * of unused stack memory.
676 */
677 {
678 struct rlimit rl;
679
680 if (getrlimit(RLIMIT_STACK, &rl) != -1
681 && (rl.rlim_cur = 20 * 4096) < rl.rlim_max)
682 {
683 if (setrlimit(RLIMIT_STACK, &rl) == -1)
684 {
685 msyslog(LOG_ERR,
686 "Cannot adjust stack limit for mlockall: %m");
687 }
688 }
689 }
690# endif /* HAVE_SETRLIMIT */
691 /*
692 * lock the process into memory
693 */
694 if (mlockall(MCL_CURRENT|MCL_FUTURE) < 0)
695 msyslog(LOG_ERR, "mlockall(): %m");
696#else /* not (HAVE_MLOCKALL && MCL_CURRENT && MCL_FUTURE) */
697# ifdef HAVE_PLOCK
698# ifdef PROCLOCK
699# ifdef _AIX

--- 100 unchanged lines hidden (view full) ---

800 * done in a separate module since this will definitely be different
801 * for the gizmo board. While at it, save the host name for later
802 * along with the length. The crypto needs this.
803 */
804#ifdef DEBUG
805 debug = 0;
806#endif
807 getconfig(argc, argv);
808#ifdef OPENSSL
809 crypto_setup();
810#endif /* OPENSSL */
811 initializing = 0;
812
813#if defined(SYS_WINNT) && !defined(NODETACH)
814# if defined(DEBUG)
815 if(!debug)
816 {
817# endif
818 if (NoWinService == FALSE) {
819 /* report to the service control manager that the service is running */
820 ssStatus.dwCurrentState = SERVICE_RUNNING;
821 ssStatus.dwWin32ExitCode = NO_ERROR;
822 if (!SetServiceStatus(sshStatusHandle, &ssStatus))
823 {
824 msyslog(LOG_ERR, "SetServiceStatus: %m");
825 if (ResolverThreadHandle != NULL)
826 CloseHandle(ResolverThreadHandle);
827 ssStatus.dwCurrentState = SERVICE_STOPPED;
828 SetServiceStatus(sshStatusHandle, &ssStatus);
829 return;
830 }
831 }
832# if defined(DEBUG)
833 }
834# endif
835#endif
836
837#ifdef HAVE_CLOCKCTL
838 /*
839 * Drop super-user privileges and chroot now if the OS supports
840 * non root clock control (only NetBSD for now).
841 */
842 if (user != NULL) {
843 if (isdigit((unsigned char)*user)) {
844 sw_uid = (uid_t)strtoul(user, &endp, 0);
845 if (*endp != '\0')
846 goto getuser;
847 } else {
848getuser:
849 if ((pw = getpwnam(user)) != NULL) {
850 sw_uid = pw->pw_uid;
851 } else {
852 errno = 0;
853 msyslog(LOG_ERR, "Cannot find user `%s'", user);
854 exit (-1);
855 }
856 }
857 }
858 if (group != NULL) {
859 if (isdigit((unsigned char)*group)) {
860 sw_gid = (gid_t)strtoul(group, &endp, 0);
861 if (*endp != '\0')
862 goto getgroup;
863 } else {
864getgroup:
865 if ((gr = getgrnam(group)) != NULL) {
866 sw_gid = pw->pw_gid;
867 } else {
868 errno = 0;
869 msyslog(LOG_ERR, "Cannot find group `%s'", group);
870 exit (-1);
871 }
872 }
873 }
874 if (chrootdir && chroot(chrootdir)) {
875 msyslog(LOG_ERR, "Cannot chroot to `%s': %m", chrootdir);
876 exit (-1);
877 }
878 if (group && setgid(sw_gid)) {
879 msyslog(LOG_ERR, "Cannot setgid() to group `%s': %m", group);
880 exit (-1);
881 }
882 if (group && setegid(sw_gid)) {
883 msyslog(LOG_ERR, "Cannot setegid() to group `%s': %m", group);
884 exit (-1);
885 }
886 if (user && setuid(sw_uid)) {
887 msyslog(LOG_ERR, "Cannot setuid() to user `%s': %m", user);
888 exit (-1);
889 }
890 if (user && seteuid(sw_uid)) {
891 msyslog(LOG_ERR, "Cannot seteuid() to user `%s': %m", user);
892 exit (-1);
893 }
894#endif
895 /*
896 * Report that we're up to any trappers
897 */
898 report_event(EVNT_SYSRESTART, (struct peer *)0);
899
900 /*
901 * Use select() on all on all input fd's for unlimited
902 * time. select() will terminate on SIGALARM or on the

--- 7 unchanged lines hidden (view full) ---

910 * have select() time out after one second.
911 * System clock updates really aren't time-critical,
912 * and - lacking a hardware reference clock - I have
913 * yet to learn about anything else that is.
914 */
915#if defined(HAVE_IO_COMPLETION_PORT)
916 WaitHandles[0] = CreateEvent(NULL, FALSE, FALSE, NULL); /* exit reques */
917 WaitHandles[1] = get_timer_handle();
918 WaitHandles[2] = get_io_event();
919
920 for (;;) {
921 DWORD Index = WaitForMultipleObjectsEx(sizeof(WaitHandles)/sizeof(WaitHandles[0]), WaitHandles, FALSE, 1000, TRUE);
922 switch (Index) {
923 case WAIT_OBJECT_0 + 0 : /* exit request */
924 exit(0);
925 break;
926
927 case WAIT_OBJECT_0 + 1 : /* timer */
928 timer();
929 break;
930
931 case WAIT_OBJECT_0 + 2 : /* Io event */
932# ifdef DEBUG
933 if ( debug > 3 )
934 {
935 printf( "IoEvent occurred\n" );
936 }
937# endif
938 break;
939
940 case WAIT_IO_COMPLETION : /* loop */
941 case WAIT_TIMEOUT :
942 break;
943 case WAIT_FAILED:
944 msyslog(LOG_ERR, "ntpdc: WaitForMultipleObjectsEx Failed: Error: %m");
945 break;
946
947 /* For now do nothing if not expected */
948 default:
949 break;
950
951 } /* switch */
952 rbuflist = getrecvbufs(); /* get received buffers */
953
954#else /* normal I/O */
955
956 was_alarmed = 0;
957 rbuflist = (struct recvbuf *)0;

--- 41 unchanged lines hidden (view full) ---

999 l_fp ts;
1000
1001 get_systime(&ts);
1002
1003 (void)input_handler(&ts);
1004 }
1005 else if (nfound == -1 && errno != EINTR)
1006 msyslog(LOG_ERR, "select() error: %m");
1007# ifdef DEBUG
1008 else if (debug > 2)
1009 msyslog(LOG_DEBUG, "select(): nfound=%d, error: %m", nfound);
1010# endif /* DEBUG */
1011# else /* HAVE_SIGNALED_IO */
1012
1013 wait_for_signal();
1014# endif /* HAVE_SIGNALED_IO */
1015 if (alarm_flag) /* alarmed? */
1016 {
1017 was_alarmed = 1;
1018 alarm_flag = 0;

--- 31 unchanged lines hidden (view full) ---

1050 printf("getrecvbufs: %ld handler interrupts, %ld frames\n",
1051 handler_calls, handler_pkts);
1052#endif
1053
1054 /*
1055 * Go around again
1056 */
1057 }
1058#ifndef SYS_WINNT
1059 exit(1); /* unreachable */
1060#endif
1061#ifndef SYS_WINNT
1062 return 1; /* DEC OSF cc braindamage */
1063#endif
1064}
1065
1066
1067#ifdef SIGDIE2
1068/*
1069 * finish - exit gracefully
1070 */
1071static RETSIGTYPE

--- 16 unchanged lines hidden (view full) ---

1088 default:
1089 exit(0);
1090 }
1091}
1092#endif /* SIGDIE2 */
1093
1094
1095#ifdef DEBUG
1096#ifndef SYS_WINNT
1097/*
1098 * moredebug - increase debugging verbosity
1099 */
1100static RETSIGTYPE
1101moredebug(
1102 int sig
1103 )
1104{

--- 19 unchanged lines hidden (view full) ---

1124
1125 if (debug > 0)
1126 {
1127 debug--;
1128 msyslog(LOG_DEBUG, "debug lowered to %d", debug);
1129 }
1130 errno = saved_errno;
1131}
1132#endif
1133#else /* not DEBUG */
1134#ifndef SYS_WINNT/*
1135 * no_debug - We don't do the debug here.
1136 */
1137static RETSIGTYPE
1138no_debug(
1139 int sig
1140 )
1141{
1142 int saved_errno = errno;
1143
1144 msyslog(LOG_DEBUG, "ntpd not compiled for debugging (signal %d)", sig);
1145 errno = saved_errno;
1146}
1147#endif /* not SYS_WINNT */
1148#endif /* not DEBUG */
1149
1150#ifdef SYS_WINNT
1151/* service_ctrl - control handler for NTP service
1152 * signals the service_main routine of start/stop requests
1153 * from the control panel or other applications making
1154 * win32API calls
1155 */

--- 119 unchanged lines hidden ---