1 /* machines.c - provide special support for peculiar architectures
12 #include "ntp_machine.h"
13 #include "ntp_syslog.h"
14 #include "ntp_stdlib.h"
15 #include "ntp_unixtime.h"
16 #include "lib_strbuf.h"
17 #include "ntp_debug.h"
24 int _getch(void); /* Declare the one function rather than include conio.h */
31 #include "ntp_syslog.h"
33 /* some translations to the world of vxWorkings -casey */
34 /* first some netdb type things */
39 struct hostent *gethostbyname(char *name)
41 struct hostent *host1;
42 h_errno = 0; /* we are always successful!!! */
43 host1 = (struct hostent *) emalloc (sizeof(struct hostent));
45 host1->h_addrtype = AF_INET;
46 host1->h_aliases = name;
48 host1->h_addr_list[0] = (char *)hostGetByName (name);
49 host1->h_addr_list[1] = NULL;
53 struct hostent *gethostbyaddr(char *name, int size, int addr_type)
55 struct hostent *host1;
56 h_errno = 0; /* we are always successful!!! */
57 host1 = (struct hostent *) emalloc (sizeof(struct hostent));
59 host1->h_addrtype = AF_INET;
60 host1->h_aliases = name;
62 host1->h_addr_list = NULL;
66 struct servent *getservbyname (char *name, char *type)
68 struct servent *serv1;
69 serv1 = (struct servent *) emalloc (sizeof(struct servent));
70 serv1->s_name = "ntp"; /* official service name */
71 serv1->s_aliases = NULL; /* alias list */
72 serv1->s_port = 123; /* port # */
73 serv1->s_proto = "udp"; /* protocol to use */
78 * vxworks thinks it has insomnia
79 * we have to sleep for number of seconds
82 #define CLKRATE sysClkRateGet()
84 /* I am not sure how valid the granularity is - it is from G. Eger's port */
85 #define CLK_GRANULARITY 1 /* Granularity of system clock in usec */
86 /* Used to round down # usecs/tick */
87 /* On a VCOM-100, PIT gets 8 MHz clk, */
88 /* & it prescales by 32, thus 4 usec */
89 /* on mv167, granularity is 1usec anyway*/
90 /* To defeat rounding, set to 1 */
91 #define USECS_PER_SEC MILLION /* Microseconds per second */
92 #define TICK (((USECS_PER_SEC / CLKRATE) / CLK_GRANULARITY) * CLK_GRANULARITY)
97 void sleep(int seconds)
99 taskDelay(seconds*TICK);
101 /* emulate unix alarm
102 * that pauses and calls SIGALRM after the seconds are up...
103 * so ... taskDelay() fudged for seconds should amount to the same thing.
106 void alarm (int seconds)
111 #endif /* SYS_VXWORKS */
113 #ifdef SYS_PTX /* Does PTX still need this? */
114 /*#include <sys/types.h> */
115 #include <sys/procstats.h>
123 * hi, this is Sequents sneak path to get to a clock
124 * this is also the most logical syscall for such a function
126 return (get_process_stats(tvp, PS_SELF, (struct procstats *) 0,
127 (struct procstats *) 0));
132 /* This is a substitute for bind() that if called for an AF_INET socket
133 port less than 1024, GETPRIVMODE() and GETUSERMODE() calls will be done. */
136 #include <sys/types.h>
137 #include <sys/socket.h>
138 #include <netinet/in.h>
141 extern void GETPRIVMODE(void);
142 extern void GETUSERMODE(void);
144 int __ntp_mpe_bind(int s, void *addr, int addrlen);
146 int __ntp_mpe_bind(int s, void *addr, int addrlen) {
150 if (addrlen == sizeof(struct sockaddr_in)) { /* AF_INET */
151 if (((struct sockaddr_in *)addr)->sin_port > 0 &&
152 ((struct sockaddr_in *)addr)->sin_port < 1024) {
156 /* ((struct sockaddr_in *)addr)->sin_addr.s_addr = 0; */
157 result = bind(s,addr,addrlen);
158 if (priv == 1) GETUSERMODE();
160 result = bind(s,addr,addrlen);
166 * MPE stupidly requires sfcntl() to be used on sockets instead of fcntl(),
167 * so we define a wrapper to analyze the file descriptor and call the correct
175 int __ntp_mpe_fcntl(int fd, int cmd, int arg);
177 int __ntp_mpe_fcntl(int fd, int cmd, int arg) {
181 extern int sfcntl(int, int, int);
184 if (getsockname(fd, &sa, &len) == -1) {
185 if (errno == EAFNOSUPPORT) /* AF_UNIX socket */
186 return sfcntl(fd, cmd, arg);
187 if (errno == ENOTSOCK) /* file or pipe */
188 return fcntl(fd, cmd, arg);
189 return (-1); /* unknown getsockname() failure */
190 } else /* AF_INET socket */
191 return sfcntl(fd, cmd, arg);
195 * Setitimer emulation support. Note that we implement this using alarm(),
196 * and since alarm() only delivers one signal, we must re-enable the alarm
197 * by enabling our own SIGALRM setitimer_mpe_handler routine to be called
198 * before the real handler routine and re-enable the alarm at that time.
200 * Note that this solution assumes that sigaction(SIGALRM) is called before
201 * calling setitimer(). If it should ever to become necessary to support
202 * sigaction(SIGALRM) after calling setitimer(), it will be necessary to trap
203 * those sigaction() calls.
210 * Some global data that needs to be shared between setitimer() and
211 * setitimer_mpe_handler().
215 unsigned long current_msec; /* current alarm() value in effect */
216 unsigned long interval_msec; /* next alarm() value from setitimer */
217 unsigned long value_msec; /* first alarm() value from setitimer */
218 struct itimerval current_itimerval; /* current itimerval in effect */
219 struct sigaction oldact; /* SIGALRM state saved by setitimer */
220 } setitimer_mpe_ctx = { 0, 0, 0 };
223 * Undocumented, unsupported function to do alarm() in milliseconds.
226 extern unsigned int px_alarm(unsigned long, int *);
229 * The SIGALRM handler routine enabled by setitimer(). Re-enable the alarm or
230 * restore the original SIGALRM setting if no more alarms are needed. Then
231 * call the original SIGALRM handler (if any).
234 static RETSIGTYPE setitimer_mpe_handler(int sig)
236 int alarm_hpe_status;
238 /* Update the new current alarm value */
240 setitimer_mpe_ctx.current_msec = setitimer_mpe_ctx.interval_msec;
242 if (setitimer_mpe_ctx.interval_msec > 0) {
243 /* Additional intervals needed; re-arm the alarm timer */
244 px_alarm(setitimer_mpe_ctx.interval_msec,&alarm_hpe_status);
246 /* No more intervals, so restore previous original SIGALRM handler */
247 sigaction(SIGALRM, &setitimer_mpe_ctx.oldact, NULL);
250 /* Call the original SIGALRM handler if it is a function and not just a flag */
252 if (setitimer_mpe_ctx.oldact.sa_handler != SIG_DFL &&
253 setitimer_mpe_ctx.oldact.sa_handler != SIG_ERR &&
254 setitimer_mpe_ctx.oldact.sa_handler != SIG_IGN)
255 (*setitimer_mpe_ctx.oldact.sa_handler)(SIGALRM);
260 * Our implementation of setitimer().
264 setitimer(int which, struct itimerval *value,
265 struct itimerval *ovalue)
268 int alarm_hpe_status;
269 unsigned long remaining_msec, value_msec, interval_msec;
270 struct sigaction newact;
273 * Convert the initial interval to milliseconds
276 if (value->it_value.tv_sec > (UINT_MAX / 1000))
277 value_msec = UINT_MAX;
279 value_msec = value->it_value.tv_sec * 1000;
281 value_msec += value->it_value.tv_usec / 1000;
284 * Convert the reset interval to milliseconds
287 if (value->it_interval.tv_sec > (UINT_MAX / 1000))
288 interval_msec = UINT_MAX;
290 interval_msec = value->it_interval.tv_sec * 1000;
292 interval_msec += value->it_interval.tv_usec / 1000;
294 if (value_msec > 0 && interval_msec > 0) {
296 * We'll be starting an interval timer that will be repeating, so we need to
297 * insert our own SIGALRM signal handler to schedule the repeats.
300 /* Read the current SIGALRM action */
302 if (sigaction(SIGALRM, NULL, &setitimer_mpe_ctx.oldact) < 0) {
303 fprintf(stderr,"MPE setitimer old handler failed, errno=%d\n",errno);
307 /* Initialize the new action to call our SIGALRM handler instead */
309 newact.sa_handler = &setitimer_mpe_handler;
310 newact.sa_mask = setitimer_mpe_ctx.oldact.sa_mask;
311 newact.sa_flags = setitimer_mpe_ctx.oldact.sa_flags;
313 if (sigaction(SIGALRM, &newact, NULL) < 0) {
314 fprintf(stderr,"MPE setitimer new handler failed, errno=%d\n",errno);
320 * Return previous itimerval if desired
323 if (ovalue != NULL) *ovalue = setitimer_mpe_ctx.current_itimerval;
326 * Save current parameters for later usage
329 setitimer_mpe_ctx.current_itimerval = *value;
330 setitimer_mpe_ctx.current_msec = value_msec;
331 setitimer_mpe_ctx.value_msec = value_msec;
332 setitimer_mpe_ctx.interval_msec = interval_msec;
335 * Schedule the first alarm
338 remaining_msec = px_alarm(value_msec, &alarm_hpe_status);
339 if (alarm_hpe_status == 0)
346 * MPE lacks gettimeofday(), so we define our own.
349 int gettimeofday(struct timeval *tvp)
352 /* Documented, supported MPE functions. */
353 extern void GETPRIVMODE(void);
354 extern void GETUSERMODE(void);
356 /* Undocumented, unsupported MPE functions. */
357 extern long long get_time(void);
358 extern void get_time_change_info(long long *, char *, char *);
359 extern long long ticks_to_micro(long long);
361 char pwf_since_boot, recover_pwf_time;
362 long long mpetime, offset_ticks, offset_usec;
365 mpetime = get_time(); /* MPE local time usecs since Jan 1 1970 */
366 get_time_change_info(&offset_ticks, &pwf_since_boot, &recover_pwf_time);
367 offset_usec = ticks_to_micro(offset_ticks); /* UTC offset usecs */
370 mpetime = mpetime - offset_usec; /* Convert from local time to UTC */
371 tvp->tv_sec = mpetime / 1000000LL;
372 tvp->tv_usec = mpetime % 1000000LL;
378 * MPE lacks settimeofday(), so we define our own.
381 #define HAVE_SETTIMEOFDAY
383 int settimeofday(struct timeval *tvp)
386 /* Documented, supported MPE functions. */
387 extern void GETPRIVMODE(void);
388 extern void GETUSERMODE(void);
390 /* Undocumented, unsupported MPE functions. */
391 extern void get_time_change_info(long long *, char *, char *);
392 extern void initialize_system_time(long long, int);
393 extern void set_time_correction(long long, int, int);
394 extern long long ticks_to_micro(long long);
396 char pwf_since_boot, recover_pwf_time;
397 long long big_sec, big_usec, mpetime, offset_ticks, offset_usec;
399 big_sec = tvp->tv_sec;
400 big_usec = tvp->tv_usec;
401 mpetime = (big_sec * 1000000LL) + big_usec; /* Desired UTC microseconds */
404 set_time_correction(0LL,0,0); /* Cancel previous time correction, if any */
405 get_time_change_info(&offset_ticks, &pwf_since_boot, &recover_pwf_time);
406 offset_usec = ticks_to_micro(offset_ticks); /* UTC offset microseconds */
407 mpetime = mpetime + offset_usec; /* Convert from UTC to local time */
408 initialize_system_time(mpetime,1);
415 #define SET_TOD_UNDETERMINED 0
416 #define SET_TOD_CLOCK_SETTIME 1
417 #define SET_TOD_SETTIMEOFDAY 2
418 #define SET_TOD_STIME 3
420 const char * const set_tod_used[] = {
427 pset_tod_using set_tod_using = NULL;
440 TRACE(1, ("In ntp_set_tod\n"));
444 #ifdef HAVE_CLOCK_SETTIME
445 if (rc && (SET_TOD_CLOCK_SETTIME == tod || !tod)) {
448 /* Convert timeval to timespec */
449 ts.tv_sec = tvp->tv_sec;
450 ts.tv_nsec = 1000 * tvp->tv_usec;
453 rc = clock_settime(CLOCK_REALTIME, &ts);
455 TRACE(1, ("ntp_set_tod: clock_settime: %d %m\n", rc));
457 tod = SET_TOD_CLOCK_SETTIME;
460 #endif /* HAVE_CLOCK_SETTIME */
461 #ifdef HAVE_SETTIMEOFDAY
462 if (rc && (SET_TOD_SETTIMEOFDAY == tod || !tod)) {
463 struct timeval adjtv;
466 * Some broken systems don't reset adjtime() when the
469 adjtv.tv_sec = adjtv.tv_usec = 0;
470 adjtime(&adjtv, NULL);
472 rc = SETTIMEOFDAY(tvp, tzp);
474 TRACE(1, ("ntp_set_tod: settimeofday: %d %m\n", rc));
476 tod = SET_TOD_SETTIMEOFDAY;
478 #endif /* HAVE_SETTIMEOFDAY */
480 if (rc && (SET_TOD_STIME == tod || !tod)) {
481 long tp = tvp->tv_sec;
484 rc = stime(&tp); /* lie as bad as SysVR4 */
486 TRACE(1, ("ntp_set_tod: stime: %d %m\n", rc));
490 #endif /* HAVE_STIME */
492 errno = saved_errno; /* for %m below */
493 TRACE(1, ("ntp_set_tod: Final result: %s: %d %m\n",
494 set_tod_used[tod], rc));
496 * Say how we're setting the time of day
498 if (!rc && NULL != set_tod_using) {
499 (*set_tod_using)(set_tod_used[tod]);
500 set_tod_using = NULL;
509 #endif /* not SYS_WINNT */
511 #if defined (SYS_WINNT) || defined (SYS_VXWORKS) || defined(MPE)
512 /* getpass is used in ntpq.c and ntpdc.c */
515 getpass(const char * prompt)
518 static char password[32];
520 fprintf(stderr, "%s", prompt);
523 for (i=0; i<sizeof(password)-1 && ((c=_getch())!='\n' && c!='\r'); i++) {
524 password[i] = (char) c;
533 #endif /* SYS_WINNT */