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 HAVE_UNISTD_H
15# include <unistd.h>
16#endif
17#ifdef HAVE_SYS_STAT_H
18# include <sys/stat.h>
19#endif
20#include <stdio.h>
21#ifndef SYS_WINNT

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

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

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

94#ifdef _AIX
95# include <ulimit.h>
96#endif /* _AIX */
97
98#ifdef SCO5_CLOCK
99# include <sys/ci/ciioctl.h>
100#endif
101
102#ifdef PUBKEY
103#include "ntp_crypto.h"
104#endif /* PUBKEY */
105
106/*
107 * Signals we catch for debugging. If not debugging we ignore them.
108 */
109#define MOREDEBUGSIG SIGUSR1
110#define LESSDEBUGSIG SIGUSR2
111
112/*

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

118# define SIGDIE2 SIGINT
119# define SIGDIE4 SIGTERM
120#endif /* SYS_WINNT */
121
122#if defined SYS_WINNT
123/* handles for various threads, process, and objects */
124HANDLE ResolverThreadHandle = NULL;
125/* variables used to inform the Service Control Manager of our current state */
126SERVICE_STATUS ssStatus;
127SERVICE_STATUS_HANDLE sshStatusHandle;
128HANDLE WaitHandles[3] = { NULL, NULL, NULL };
129char szMsgPath[255];
130static BOOL WINAPI OnConsoleEvent(DWORD dwCtrlType);
131#endif /* SYS_WINNT */
132
133/*
134 * Scheduling priority we run at
135 */
136#define NTPD_PRIO (-12)
137
138int priority_done = 2; /* 0 - Set priority */
139 /* 1 - priority is OK where it is */
140 /* 2 - Don't set priority */
141 /* 1 and 2 are pretty much the same */
142
143/*
144 * Debugging flag
145 */
146volatile int debug;
147
148/*
149 * No-fork flag. If set, we do not become a background daemon.
150 */
151int nofork;
152
153/*
154 * Initializing flag. All async routines watch this and only do their
155 * thing when it is clear.
156 */
157int initializing;
158
159/*
160 * Version declaration

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

171#endif /* DECL_SYSCALL */
172
173
174#ifdef SIGDIE2
175static RETSIGTYPE finish P((int));
176#endif /* SIGDIE2 */
177
178#ifdef DEBUG
179static RETSIGTYPE moredebug P((int));
180static RETSIGTYPE lessdebug P((int));
181#else /* not DEBUG */
182static RETSIGTYPE no_debug P((int));
183#endif /* not DEBUG */
184
185int ntpdmain P((int, char **));
186static void set_process_priority P((void));
187
188
189#ifdef NO_MAIN_ALLOWED
190CALL(ntpd,"ntpd",ntpdmain);
191#else
192int
193main(
194 int argc,
195 char *argv[]
196 )
197{
198 return ntpdmain(argc, argv);
199}
200#endif
201
202#ifdef _AIX
203/*
204 * OK. AIX is different than solaris in how it implements plock().
205 * If you do NOT adjust the stack limit, you will get the MAXIMUM
206 * stack size allocated and PINNED with you program. To check the
207 * value, use ulimit -a.
208 *

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

330int
331ntpdmain(
332 int argc,
333 char *argv[]
334 )
335{
336 l_fp now;
337 char *cp;
338#ifdef AUTOKEY
339 u_int n;
340 char hostname[MAXFILENAME];
341#endif /* AUTOKEY */
342 struct recvbuf *rbuflist;
343 struct recvbuf *rbuf;
344#ifdef _AIX /* HMS: ifdef SIGDANGER? */
345 struct sigaction sa;
346#endif
347
348 initializing = 1; /* mark that we are initializing */
349 debug = 0; /* no debugging by default */

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

379 if (!GetModuleFileName(NULL, szMsgPath, sizeof(szMsgPath))) {
380 msyslog(LOG_ERR, "GetModuleFileName(PGM_EXE_FILE) failed: %m\n");
381 exit(1);
382 }
383 addSourceToRegistry("NTP", szMsgPath);
384#endif
385 getstartup(argc, argv); /* startup configuration, may set debug */
386
387 /*
388 * Initialize random generator and public key pair
389 */
390 get_systime(&now);
391 SRANDOM((int)(now.l_i * now.l_uf));
392
393#if !defined(VMS)
394# ifndef NODETACH
395 /*
396 * Detach us from the terminal. May need an #ifndef GIZMO.
397 */

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

409 exit(0);
410
411 {
412#if !defined(F_CLOSEM)
413 u_long s;
414 int max_fd;
415#endif /* not F_CLOSEM */
416
417 /*
418 * From 'Writing Reliable AIX Daemons,' SG24-4946-00,
419 * by Eric Agar (saves us from doing 32767 system
420 * calls)
421 */
422#if defined(F_CLOSEM)
423 if (fcntl(0, F_CLOSEM, 0) == -1)
424 msyslog(LOG_ERR, "ntpd: failed to close open files(): %m");
425#else /* not F_CLOSEM */
426
427# if defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX)
428 max_fd = sysconf(_SC_OPEN_MAX);
429# else /* HAVE_SYSCONF && _SC_OPEN_MAX */
430 max_fd = getdtablesize();

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

479
480 (void) sigaction(SIGDANGER, &sa, NULL);
481#endif /* _AIX */
482 }
483# endif /* not HAVE_DAEMON */
484# else /* SYS_WINNT */
485
486 {
487 SERVICE_TABLE_ENTRY dispatchTable[] = {
488 { TEXT("NetworkTimeProtocol"), (LPSERVICE_MAIN_FUNCTION)service_main },
489 { NULL, NULL }
490 };
491
492 /* daemonize */
493 if (!StartServiceCtrlDispatcher(dispatchTable))
494 {
495 msyslog(LOG_ERR, "StartServiceCtrlDispatcher: %m");
496 ExitProcess(2);
497 }
498 }
499# endif /* SYS_WINNT */
500 }
501# endif /* NODETACH */
502# if defined(SYS_WINNT) && !defined(NODETACH)
503 else
504 service_main(argc, argv);
505 return 0; /* must return a value */

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

515service_main(
516 DWORD argc,
517 LPTSTR *argv
518 )
519{
520 char *cp;
521 struct recvbuf *rbuflist;
522 struct recvbuf *rbuf;
523#ifdef AUTOKEY
524 u_int n;
525 char hostname[MAXFILENAME];
526#endif /* AUTOKEY */
527 if(!debug)
528 {
529 /* register our service control handler */
530 if (!(sshStatusHandle = RegisterServiceCtrlHandler( TEXT("NetworkTimeProtocol"),
531 (LPHANDLER_FUNCTION)service_ctrl)))
532 {
533 msyslog(LOG_ERR, "RegisterServiceCtrlHandler failed: %m");
534 return;
535 }
536
537 /* report pending status to Service Control Manager */
538 ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
539 ssStatus.dwCurrentState = SERVICE_START_PENDING;

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

562 if (cp == 0)
563 cp = argv[0];
564 else
565 cp++;
566
567 debug = 0; /* will be immediately re-initialized 8-( */
568 getstartup(argc, argv); /* startup configuration, catch logfile this time */
569
570#if !defined(SYS_WINNT) && !defined(VMS)
571
572# ifndef LOG_DAEMON
573 openlog(cp, LOG_PID);
574# else /* LOG_DAEMON */
575
576# ifndef LOG_NTP
577# define LOG_NTP LOG_DAEMON
578# endif

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

619 } /* else ...
620 * If we can't open the device, this probably just isn't
621 * a multiprocessor system, so we're A-OK.
622 */
623 }
624#endif
625
626#if defined(HAVE_MLOCKALL) && defined(MCL_CURRENT) && defined(MCL_FUTURE)
627 /*
628 * lock the process into memory
629 */
630 if (mlockall(MCL_CURRENT|MCL_FUTURE) < 0)
631 msyslog(LOG_ERR, "mlockall(): %m");
632#else /* not (HAVE_MLOCKALL && MCL_CURRENT && MCL_FUTURE) */
633# ifdef HAVE_PLOCK
634# ifdef PROCLOCK
635# ifdef _AIX

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

736 * done in a separate module since this will definitely be different
737 * for the gizmo board. While at it, save the host name for later
738 * along with the length. The crypto needs this.
739 */
740#ifdef DEBUG
741 debug = 0;
742#endif
743 getconfig(argc, argv);
744#ifdef AUTOKEY
745 gethostname(hostname, MAXFILENAME);
746 n = strlen(hostname) + 1;
747 sys_hostname = emalloc(n);
748 memcpy(sys_hostname, hostname, n);
749#ifdef PUBKEY
750 crypto_setup();
751#endif /* PUBKEY */
752#endif /* AUTOKEY */
753 initializing = 0;
754
755#if defined(SYS_WINNT) && !defined(NODETACH)
756# if defined(DEBUG)
757 if(!debug)
758 {
759# endif
760 /* report to the service control manager that the service is running */
761 ssStatus.dwCurrentState = SERVICE_RUNNING;
762 ssStatus.dwWin32ExitCode = NO_ERROR;
763 if (!SetServiceStatus(sshStatusHandle, &ssStatus))
764 {
765 msyslog(LOG_ERR, "SetServiceStatus: %m");
766 if (ResolverThreadHandle != NULL)
767 CloseHandle(ResolverThreadHandle);
768 ssStatus.dwCurrentState = SERVICE_STOPPED;
769 SetServiceStatus(sshStatusHandle, &ssStatus);
770 return;
771 }
772# if defined(DEBUG)
773 }
774# endif
775#endif
776
777 /*
778 * Report that we're up to any trappers
779 */
780 report_event(EVNT_SYSRESTART, (struct peer *)0);
781
782 /*
783 * Use select() on all on all input fd's for unlimited
784 * time. select() will terminate on SIGALARM or on the

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

792 * have select() time out after one second.
793 * System clock updates really aren't time-critical,
794 * and - lacking a hardware reference clock - I have
795 * yet to learn about anything else that is.
796 */
797#if defined(HAVE_IO_COMPLETION_PORT)
798 WaitHandles[0] = CreateEvent(NULL, FALSE, FALSE, NULL); /* exit reques */
799 WaitHandles[1] = get_timer_handle();
800 WaitHandles[2] = get_io_event();
801
802 for (;;) {
803 DWORD Index = WaitForMultipleObjectsEx(sizeof(WaitHandles)/sizeof(WaitHandles[0]), WaitHandles, FALSE, 1000, MWMO_ALERTABLE);
804 switch (Index) {
805 case WAIT_OBJECT_0 + 0 : /* exit request */
806 exit(0);
807 break;
808
809 case WAIT_OBJECT_0 + 1 : /* timer */
810 timer();
811 break;
812
813 case WAIT_OBJECT_0 + 2 : /* Io event */
814# ifdef DEBUG
815 if ( debug > 3 )
816 {
817 printf( "IoEvent occurred\n" );
818 }
819# endif
820 break;
821
822# if 1
823 /*
824 * FIXME: According to the documentation for WaitForMultipleObjectsEx
825 * this is not possible. This may be a vestigial from when this was
826 * MsgWaitForMultipleObjects, maybe it should be removed?
827 */
828 case WAIT_OBJECT_0 + 3 : /* windows message */
829 {
830 MSG msg;
831 while ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
832 {
833 if ( msg.message == WM_QUIT )
834 {
835 exit( 0 );
836 }
837 DispatchMessage( &msg );
838 }
839 }
840 break;
841# endif
842
843 case WAIT_IO_COMPLETION : /* loop */
844 case WAIT_TIMEOUT :
845 break;
846
847 } /* switch */
848 rbuflist = getrecvbufs(); /* get received buffers */
849
850#else /* normal I/O */
851
852 was_alarmed = 0;
853 rbuflist = (struct recvbuf *)0;

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

895 l_fp ts;
896
897 get_systime(&ts);
898
899 (void)input_handler(&ts);
900 }
901 else if (nfound == -1 && errno != EINTR)
902 msyslog(LOG_ERR, "select() error: %m");
903 else if (debug > 2) {
904 msyslog(LOG_DEBUG, "select(): nfound=%d, error: %m", nfound);
905 }
906# else /* HAVE_SIGNALED_IO */
907
908 wait_for_signal();
909# endif /* HAVE_SIGNALED_IO */
910 if (alarm_flag) /* alarmed? */
911 {
912 was_alarmed = 1;
913 alarm_flag = 0;

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

945 printf("getrecvbufs: %ld handler interrupts, %ld frames\n",
946 handler_calls, handler_pkts);
947#endif
948
949 /*
950 * Go around again
951 */
952 }
953 exit(1); /* unreachable */
954 return 1; /* DEC OSF cc braindamage */
955}
956
957
958#ifdef SIGDIE2
959/*
960 * finish - exit gracefully
961 */
962static RETSIGTYPE

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

979 default:
980 exit(0);
981 }
982}
983#endif /* SIGDIE2 */
984
985
986#ifdef DEBUG
987/*
988 * moredebug - increase debugging verbosity
989 */
990static RETSIGTYPE
991moredebug(
992 int sig
993 )
994{

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

1014
1015 if (debug > 0)
1016 {
1017 debug--;
1018 msyslog(LOG_DEBUG, "debug lowered to %d", debug);
1019 }
1020 errno = saved_errno;
1021}
1022#else /* not DEBUG */
1023/*
1024 * no_debug - We don't do the debug here.
1025 */
1026static RETSIGTYPE
1027no_debug(
1028 int sig
1029 )
1030{
1031 int saved_errno = errno;
1032
1033 msyslog(LOG_DEBUG, "ntpd not compiled for debugging (signal %d)", sig);
1034 errno = saved_errno;
1035}
1036#endif /* not DEBUG */
1037
1038#ifdef SYS_WINNT
1039/* service_ctrl - control handler for NTP service
1040 * signals the service_main routine of start/stop requests
1041 * from the control panel or other applications making
1042 * win32API calls
1043 */

--- 119 unchanged lines hidden ---