Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / libntp / machines.c
blobb596ec9f830b5e094bf15830f0b6052f977ee9f8
1 /* $NetBSD$ */
3 /* machines.c - provide special support for peculiar architectures
5 * Real bummers unite !
7 */
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
13 #include "ntp_machine.h"
14 #include "ntp_syslog.h"
15 #include "ntp_stdlib.h"
16 #include "ntp_unixtime.h"
17 #include "lib_strbuf.h"
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
23 #ifdef SYS_WINNT
24 int _getch(void); /* Declare the one function rather than include conio.h */
25 #else
27 #ifdef SYS_VXWORKS
28 #include "taskLib.h"
29 #include "sysLib.h"
30 #include "time.h"
31 #include "ntp_syslog.h"
33 /* some translations to the world of vxWorkings -casey */
34 /* first some netdb type things */
35 #include "ioLib.h"
36 #include <socket.h>
37 int h_errno;
39 struct hostent *gethostbyname(char *name)
41 struct hostent *host1;
42 h_errno = 0; /* we are always successful!!! */
43 host1 = (struct hostent *) malloc (sizeof(struct hostent));
44 host1->h_name = name;
45 host1->h_addrtype = AF_INET;
46 host1->h_aliases = name;
47 host1->h_length = 4;
48 host1->h_addr_list[0] = (char *)hostGetByName (name);
49 host1->h_addr_list[1] = NULL;
50 return host1;
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 *) malloc (sizeof(struct hostent));
58 host1->h_name = name;
59 host1->h_addrtype = AF_INET;
60 host1->h_aliases = name;
61 host1->h_length = 4;
62 host1->h_addr_list = NULL;
63 return host1;
66 struct servent *getservbyname (char *name, char *type)
68 struct servent *serv1;
69 serv1 = (struct servent *) malloc (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 */
74 return serv1;
77 /* second
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)
94 /* emulate unix sleep
95 * casey
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.
104 * casey
106 void alarm (int seconds)
108 sleep(seconds);
111 #endif /* SYS_VXWORKS */
113 #ifdef SYS_PTX /* Does PTX still need this? */
114 /*#include <sys/types.h> */
115 #include <sys/procstats.h>
118 gettimeofday(
119 struct timeval *tvp
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));
129 #endif /* SYS_PTX */
131 #ifdef MPE
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. */
135 #undef bind
136 #include <sys/types.h>
137 #include <sys/socket.h>
138 #include <netinet/in.h>
139 #include <sys/un.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) {
147 int priv = 0;
148 int result;
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) {
153 priv = 1;
154 GETPRIVMODE();
156 /* ((struct sockaddr_in *)addr)->sin_addr.s_addr = 0; */
157 result = bind(s,addr,addrlen);
158 if (priv == 1) GETUSERMODE();
159 } else /* AF_UNIX */
160 result = bind(s,addr,addrlen);
162 return result;
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
168 * function.
171 #undef fcntl
172 #include <errno.h>
173 #include <fcntl.h>
175 int __ntp_mpe_fcntl(int fd, int cmd, int arg);
177 int __ntp_mpe_fcntl(int fd, int cmd, int arg) {
178 int len;
179 struct sockaddr sa;
181 extern int sfcntl(int, int, int);
183 len = sizeof sa;
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.
206 #include <limits.h>
207 #include <signal.h>
210 * Some global data that needs to be shared between setitimer() and
211 * setitimer_mpe_handler().
214 struct {
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);
245 } else {
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;
278 else
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;
289 else
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);
304 return -1;
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);
315 return -1;
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)
340 return (0);
341 else
342 return (-1);
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;
364 GETPRIVMODE();
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 */
368 GETUSERMODE();
370 mpetime = mpetime - offset_usec; /* Convert from local time to UTC */
371 tvp->tv_sec = mpetime / 1000000LL;
372 tvp->tv_usec = mpetime % 1000000LL;
374 return 0;
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 */
403 GETPRIVMODE();
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);
409 GETUSERMODE();
411 return 0;
413 #endif /* MPE */
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[] = {
421 "undetermined",
422 "clock_settime",
423 "settimeofday",
424 "stime"
427 pset_tod_using set_tod_using = NULL;
431 ntp_set_tod(
432 struct timeval *tvp,
433 void *tzp
436 static int tod;
437 int rc = -1;
438 int saved_errno = 0;
440 #ifdef DEBUG
441 if (debug)
442 printf("In ntp_set_tod\n");
443 #endif
445 #ifdef HAVE_CLOCK_SETTIME
446 if (rc && (SET_TOD_CLOCK_SETTIME == tod || !tod)) {
447 struct timespec ts;
449 /* Convert timeval to timespec */
450 ts.tv_sec = tvp->tv_sec;
451 ts.tv_nsec = 1000 * tvp->tv_usec;
453 errno = 0;
454 rc = clock_settime(CLOCK_REALTIME, &ts);
455 saved_errno = errno;
456 #ifdef DEBUG
457 if (debug) {
458 printf("ntp_set_tod: clock_settime: %d: %s\n",
459 rc, strerror(saved_errno));
461 #endif
462 if (!tod && !rc)
463 tod = SET_TOD_CLOCK_SETTIME;
466 #endif /* HAVE_CLOCK_SETTIME */
467 #ifdef HAVE_SETTIMEOFDAY
468 if (rc && (SET_TOD_SETTIMEOFDAY == tod || !tod)) {
469 struct timeval adjtv;
472 * Some broken systems don't reset adjtime() when the
473 * clock is stepped.
475 adjtv.tv_sec = adjtv.tv_usec = 0;
476 adjtime(&adjtv, NULL);
477 errno = 0;
478 rc = SETTIMEOFDAY(tvp, tzp);
479 saved_errno = errno;
480 #ifdef DEBUG
481 if (debug) {
482 printf("ntp_set_tod: settimeofday: %d: %s\n",
483 rc, strerror(saved_errno));
485 #endif
486 if (!tod && !rc)
487 tod = SET_TOD_SETTIMEOFDAY;
489 #endif /* HAVE_SETTIMEOFDAY */
490 #ifdef HAVE_STIME
491 if (rc && (SET_TOD_STIME == tod || !tod)) {
492 long tp = tvp->tv_sec;
494 errno = 0;
495 rc = stime(&tp); /* lie as bad as SysVR4 */
496 saved_errno = errno;
497 #ifdef DEBUG
498 if (debug) {
499 printf("ntp_set_tod: stime: %d: %s\n",
500 rc, strerror(saved_errno));
502 #endif
503 if (!tod && !rc)
504 tod = SET_TOD_STIME;
506 #endif /* HAVE_STIME */
508 #ifdef DEBUG
509 if (debug) {
510 printf("ntp_set_tod: Final result: %s: %d: %s\n",
511 set_tod_used[tod], rc, strerror(saved_errno));
513 #endif
515 * Say how we're setting the time of day
517 if (!rc && NULL != set_tod_using) {
518 (*set_tod_using)(set_tod_used[tod]);
519 set_tod_using = NULL;
522 if (rc)
523 errno = saved_errno;
525 return rc;
528 #endif /* not SYS_WINNT */
530 #if defined (SYS_WINNT) || defined (SYS_VXWORKS) || defined(MPE)
531 /* getpass is used in ntpq.c and ntpdc.c */
533 char *
534 getpass(const char * prompt)
536 int c, i;
537 static char password[32];
539 fprintf(stderr, "%s", prompt);
540 fflush(stderr);
542 for (i=0; i<sizeof(password)-1 && ((c=_getch())!='\n' && c!='\r'); i++) {
543 password[i] = (char) c;
545 password[i] = '\0';
547 fputc('\n', stderr);
548 fflush(stderr);
550 return password;
552 #endif /* SYS_WINNT */
554 #if !defined(HAVE_MEMSET)
555 void
556 ntp_memset(
557 char *a,
558 int x,
559 int c
562 while (c-- > 0)
563 *a++ = (char) x;
565 #endif /*POSIX*/