Deleted Added
full compact
ntpd.c (106163) ntpd.c (132451)
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
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
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"
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>
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
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
102#ifdef PUBKEY
103#include "ntp_crypto.h"
104#endif /* PUBKEY */
107#ifdef HAVE_CLOCKCTL
108# include <ctype.h>
109# include <grp.h>
110# include <pwd.h>
111#endif
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 */
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;
126SERVICE_STATUS ssStatus;
127SERVICE_STATUS_HANDLE sshStatusHandle;
128HANDLE WaitHandles[3] = { NULL, NULL, NULL };
129char szMsgPath[255];
130static BOOL WINAPI OnConsoleEvent(DWORD dwCtrlType);
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();
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/*
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/*
149 * No-fork flag. If set, we do not become a background daemon.
150 */
151int nofork;
152
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
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
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
179static RETSIGTYPE moredebug P((int));
180static RETSIGTYPE lessdebug P((int));
205static RETSIGTYPE moredebug P((int));
206static RETSIGTYPE lessdebug P((int));
207#endif
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
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
188
215#ifdef SIM
216int
217main(
218 int argc,
219 char *argv[]
220 )
221{
222 return ntpsim(argc, argv);
223}
224#else /* SIM */
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
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 */
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;
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;
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
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
387 /*
388 * Initialize random generator and public key pair
389 */
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
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
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)
417 /*
418 * From 'Writing Reliable AIX Daemons,' SG24-4946-00,
419 * by Eric Agar (saves us from doing 32767 system
420 * calls)
421 */
459 /*
460 * From 'Writing Reliable AIX Daemons,' SG24-4946-00,
461 * by Eric Agar (saves us from doing 32767 system
462 * calls)
463 */
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 {
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 {
487 SERVICE_TABLE_ENTRY dispatchTable[] = {
528 if (NoWinService == FALSE) {
529 SERVICE_TABLE_ENTRY dispatchTable[] = {
488 { TEXT("NetworkTimeProtocol"), (LPSERVICE_MAIN_FUNCTION)service_main },
489 { NULL, NULL }
530 { TEXT("NetworkTimeProtocol"), (LPSERVICE_MAIN_FUNCTION)service_main },
531 { NULL, NULL }
490 };
532 };
491
533
492 /* daemonize */
493 if (!StartServiceCtrlDispatcher(dispatchTable))
494 {
495 msyslog(LOG_ERR, "StartServiceCtrlDispatcher: %m");
496 ExitProcess(2);
534 /* daemonize */
535 if (!StartServiceCtrlDispatcher(dispatchTable))
536 {
537 msyslog(LOG_ERR, "StartServiceCtrlDispatcher: %m");
538 ExitProcess(2);
539 }
497 }
540 }
541 else {
542 service_main(argc, argv);
543 return 0;
544 }
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;
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;
523#ifdef AUTOKEY
524 u_int n;
525 char hostname[MAXFILENAME];
526#endif /* AUTOKEY */
527 if(!debug)
570
571 if(!debug && NoWinService == FALSE)
528 {
529 /* register our service control handler */
572 {
573 /* register our service control handler */
530 if (!(sshStatusHandle = RegisterServiceCtrlHandler( TEXT("NetworkTimeProtocol"),
531 (LPHANDLER_FUNCTION)service_ctrl)))
574 sshStatusHandle = RegisterServiceCtrlHandler( TEXT("NetworkTimeProtocol"),
575 (LPHANDLER_FUNCTION)service_ctrl);
576 if(sshStatusHandle == 0)
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
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
570#if !defined(SYS_WINNT) && !defined(VMS)
615#if !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)
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
627 /*
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 /*
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);
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);
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
808#ifdef OPENSSL
750 crypto_setup();
809 crypto_setup();
751#endif /* PUBKEY */
752#endif /* AUTOKEY */
810#endif /* OPENSSL */
753 initializing = 0;
754
755#if defined(SYS_WINNT) && !defined(NODETACH)
756# if defined(DEBUG)
757 if(!debug)
758 {
759# endif
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) {
760 /* report to the service control manager that the service is running */
819 /* 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;
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 }
771 }
772# if defined(DEBUG)
773 }
774# endif
775#endif
776
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
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();
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();
800 WaitHandles[2] = get_io_event();
918 WaitHandles[2] = get_io_event();
801
802 for (;;) {
919
920 for (;;) {
803 DWORD Index = WaitForMultipleObjectsEx(sizeof(WaitHandles)/sizeof(WaitHandles[0]), WaitHandles, FALSE, 1000, MWMO_ALERTABLE);
921 DWORD Index = WaitForMultipleObjectsEx(sizeof(WaitHandles)/sizeof(WaitHandles[0]), WaitHandles, FALSE, 1000, TRUE);
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
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
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;
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;
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");
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");
903 else if (debug > 2) {
1007# ifdef DEBUG
1008 else if (debug > 2)
904 msyslog(LOG_DEBUG, "select(): nfound=%d, error: %m", nfound);
1009 msyslog(LOG_DEBUG, "select(): nfound=%d, error: %m", nfound);
905 }
1010# endif /* DEBUG */
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 }
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
953 exit(1); /* unreachable */
1059 exit(1); /* unreachable */
1060#endif
1061#ifndef SYS_WINNT
954 return 1; /* DEC OSF cc braindamage */
1062 return 1; /* DEC OSF cc braindamage */
1063#endif
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
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
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}
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
1022#else /* not DEBUG */
1133#else /* not DEBUG */
1023/*
1134#ifndef SYS_WINNT/*
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}
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 */
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 ---
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 ---