1 /* $NetBSD: machines.c,v 1.7 2024/08/18 20:47:13 christos Exp $ */
2
3 /* machines.c - provide special support for peculiar architectures
4 *
5 * Real bummers unite !
6 *
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include "ntp.h"
14 #include "ntp_machine.h"
15 #include "ntp_syslog.h"
16 #include "ntp_stdlib.h"
17 #include "ntp_unixtime.h"
18 #include "ntp_debug.h"
19 #include "ntp_tty.h"
20
21 #ifdef HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24
25 #ifdef SYS_WINNT
26 #include <conio.h>
27 #else
28 #ifdef SYS_VXWORKS
29 #include "taskLib.h"
30 #include "sysLib.h"
31 #include "time.h"
32 #include "ntp_syslog.h"
33
34 /* some translations to the world of vxWorkings -casey */
35 /* first some netdb type things */
36 #include "ioLib.h"
37 #include <socket.h>
38 int h_errno;
39
gethostbyname(char * name)40 struct hostent *gethostbyname(char *name)
41 {
42 struct hostent *host1;
43 h_errno = 0; /* we are always successful!!! */
44 host1 = (struct hostent *) emalloc (sizeof(struct hostent));
45 host1->h_name = name;
46 host1->h_addrtype = AF_INET;
47 host1->h_aliases = name;
48 host1->h_length = 4;
49 host1->h_addr_list[0] = (char *)hostGetByName (name);
50 host1->h_addr_list[1] = NULL;
51 return host1;
52 }
53
gethostbyaddr(char * name,int size,int addr_type)54 struct hostent *gethostbyaddr(char *name, int size, int addr_type)
55 {
56 struct hostent *host1;
57 h_errno = 0; /* we are always successful!!! */
58 host1 = (struct hostent *) emalloc (sizeof(struct hostent));
59 host1->h_name = name;
60 host1->h_addrtype = AF_INET;
61 host1->h_aliases = name;
62 host1->h_length = 4;
63 host1->h_addr_list = NULL;
64 return host1;
65 }
66
getservbyname(char * name,char * type)67 struct servent *getservbyname (char *name, char *type)
68 {
69 struct servent *serv1;
70 serv1 = (struct servent *) emalloc (sizeof(struct servent));
71 serv1->s_name = "ntp"; /* official service name */
72 serv1->s_aliases = NULL; /* alias list */
73 serv1->s_port = 123; /* port # */
74 serv1->s_proto = "udp"; /* protocol to use */
75 return serv1;
76 }
77
78 /* second
79 * vxworks thinks it has insomnia
80 * we have to sleep for number of seconds
81 */
82
83 #define CLKRATE sysClkRateGet()
84
85 /* I am not sure how valid the granularity is - it is from G. Eger's port */
86 #define CLK_GRANULARITY 1 /* Granularity of system clock in usec */
87 /* Used to round down # usecs/tick */
88 /* On a VCOM-100, PIT gets 8 MHz clk, */
89 /* & it prescales by 32, thus 4 usec */
90 /* on mv167, granularity is 1usec anyway*/
91 /* To defeat rounding, set to 1 */
92 #define USECS_PER_SEC MILLION /* Microseconds per second */
93 #define TICK (((USECS_PER_SEC / CLKRATE) / CLK_GRANULARITY) * CLK_GRANULARITY)
94
95 /* emulate unix sleep
96 * casey
97 */
sleep(int seconds)98 void sleep(int seconds)
99 {
100 taskDelay(seconds*TICK);
101 }
102 /* emulate unix alarm
103 * that pauses and calls SIGALRM after the seconds are up...
104 * so ... taskDelay() fudged for seconds should amount to the same thing.
105 * casey
106 */
alarm(int seconds)107 void alarm (int seconds)
108 {
109 sleep(seconds);
110 }
111
112 #endif /* SYS_VXWORKS */
113
114 #ifdef SYS_PTX /* Does PTX still need this? */
115 /*#include <sys/types.h> */
116 #include <sys/procstats.h>
117
118 int
gettimeofday(struct timeval * tvp)119 gettimeofday(
120 struct timeval *tvp
121 )
122 {
123 /*
124 * hi, this is Sequents sneak path to get to a clock
125 * this is also the most logical syscall for such a function
126 */
127 return (get_process_stats(tvp, PS_SELF, (struct procstats *) 0,
128 (struct procstats *) 0));
129 }
130 #endif /* SYS_PTX */
131
132 #ifdef MPE
133 /* This is a substitute for bind() that if called for an AF_INET socket
134 port less than 1024, GETPRIVMODE() and GETUSERMODE() calls will be done. */
135
136 #undef bind
137 #include <sys/types.h>
138 #include <sys/socket.h>
139 #include <netinet/in.h>
140 #include <sys/un.h>
141
142 extern void GETPRIVMODE(void);
143 extern void GETUSERMODE(void);
144
145 int __ntp_mpe_bind(int s, void *addr, int addrlen);
146
__ntp_mpe_bind(int s,void * addr,int addrlen)147 int __ntp_mpe_bind(int s, void *addr, int addrlen) {
148 int priv = 0;
149 int result;
150
151 if (addrlen == sizeof(struct sockaddr_in)) { /* AF_INET */
152 if (((struct sockaddr_in *)addr)->sin_port > 0 &&
153 ((struct sockaddr_in *)addr)->sin_port < 1024) {
154 priv = 1;
155 GETPRIVMODE();
156 }
157 /* ((struct sockaddr_in *)addr)->sin_addr.s_addr = 0; */
158 result = bind(s,addr,addrlen);
159 if (priv == 1) GETUSERMODE();
160 } else /* AF_UNIX */
161 result = bind(s,addr,addrlen);
162
163 return result;
164 }
165
166 /*
167 * MPE stupidly requires sfcntl() to be used on sockets instead of fcntl(),
168 * so we define a wrapper to analyze the file descriptor and call the correct
169 * function.
170 */
171
172 #undef fcntl
173 #include <errno.h>
174 #include <fcntl.h>
175
176 int __ntp_mpe_fcntl(int fd, int cmd, int arg);
177
__ntp_mpe_fcntl(int fd,int cmd,int arg)178 int __ntp_mpe_fcntl(int fd, int cmd, int arg) {
179 int len;
180 struct sockaddr sa;
181
182 extern int sfcntl(int, int, int);
183
184 len = sizeof sa;
185 if (getsockname(fd, &sa, &len) == -1) {
186 if (errno == EAFNOSUPPORT) /* AF_UNIX socket */
187 return sfcntl(fd, cmd, arg);
188 if (errno == ENOTSOCK) /* file or pipe */
189 return fcntl(fd, cmd, arg);
190 return (-1); /* unknown getsockname() failure */
191 } else /* AF_INET socket */
192 return sfcntl(fd, cmd, arg);
193 }
194
195 /*
196 * Setitimer emulation support. Note that we implement this using alarm(),
197 * and since alarm() only delivers one signal, we must re-enable the alarm
198 * by enabling our own SIGALRM setitimer_mpe_handler routine to be called
199 * before the real handler routine and re-enable the alarm at that time.
200 *
201 * Note that this solution assumes that sigaction(SIGALRM) is called before
202 * calling setitimer(). If it should ever to become necessary to support
203 * sigaction(SIGALRM) after calling setitimer(), it will be necessary to trap
204 * those sigaction() calls.
205 */
206
207 #include <limits.h>
208 #include <signal.h>
209
210 /*
211 * Some global data that needs to be shared between setitimer() and
212 * setitimer_mpe_handler().
213 */
214
215 struct {
216 unsigned long current_msec; /* current alarm() value in effect */
217 unsigned long interval_msec; /* next alarm() value from setitimer */
218 unsigned long value_msec; /* first alarm() value from setitimer */
219 struct itimerval current_itimerval; /* current itimerval in effect */
220 struct sigaction oldact; /* SIGALRM state saved by setitimer */
221 } setitimer_mpe_ctx = { 0, 0, 0 };
222
223 /*
224 * Undocumented, unsupported function to do alarm() in milliseconds.
225 */
226
227 extern unsigned int px_alarm(unsigned long, int *);
228
229 /*
230 * The SIGALRM handler routine enabled by setitimer(). Re-enable the alarm or
231 * restore the original SIGALRM setting if no more alarms are needed. Then
232 * call the original SIGALRM handler (if any).
233 */
234
setitimer_mpe_handler(int sig)235 static RETSIGTYPE setitimer_mpe_handler(int sig)
236 {
237 int alarm_hpe_status;
238
239 /* Update the new current alarm value */
240
241 setitimer_mpe_ctx.current_msec = setitimer_mpe_ctx.interval_msec;
242
243 if (setitimer_mpe_ctx.interval_msec > 0) {
244 /* Additional intervals needed; re-arm the alarm timer */
245 px_alarm(setitimer_mpe_ctx.interval_msec,&alarm_hpe_status);
246 } else {
247 /* No more intervals, so restore previous original SIGALRM handler */
248 sigaction(SIGALRM, &setitimer_mpe_ctx.oldact, NULL);
249 }
250
251 /* Call the original SIGALRM handler if it is a function and not just a flag */
252
253 if (setitimer_mpe_ctx.oldact.sa_handler != SIG_DFL &&
254 setitimer_mpe_ctx.oldact.sa_handler != SIG_ERR &&
255 setitimer_mpe_ctx.oldact.sa_handler != SIG_IGN)
256 (*setitimer_mpe_ctx.oldact.sa_handler)(SIGALRM);
257
258 }
259
260 /*
261 * Our implementation of setitimer().
262 */
263
264 int
setitimer(int which,struct itimerval * value,struct itimerval * ovalue)265 setitimer(int which, struct itimerval *value,
266 struct itimerval *ovalue)
267 {
268
269 int alarm_hpe_status;
270 unsigned long remaining_msec, value_msec, interval_msec;
271 struct sigaction newact;
272
273 /*
274 * Convert the initial interval to milliseconds
275 */
276
277 if (value->it_value.tv_sec > (UINT_MAX / 1000))
278 value_msec = UINT_MAX;
279 else
280 value_msec = value->it_value.tv_sec * 1000;
281
282 value_msec += value->it_value.tv_usec / 1000;
283
284 /*
285 * Convert the reset interval to milliseconds
286 */
287
288 if (value->it_interval.tv_sec > (UINT_MAX / 1000))
289 interval_msec = UINT_MAX;
290 else
291 interval_msec = value->it_interval.tv_sec * 1000;
292
293 interval_msec += value->it_interval.tv_usec / 1000;
294
295 if (value_msec > 0 && interval_msec > 0) {
296 /*
297 * We'll be starting an interval timer that will be repeating, so we need to
298 * insert our own SIGALRM signal handler to schedule the repeats.
299 */
300
301 /* Read the current SIGALRM action */
302
303 if (sigaction(SIGALRM, NULL, &setitimer_mpe_ctx.oldact) < 0) {
304 fprintf(stderr,"MPE setitimer old handler failed, errno=%d\n",errno);
305 return -1;
306 }
307
308 /* Initialize the new action to call our SIGALRM handler instead */
309
310 newact.sa_handler = &setitimer_mpe_handler;
311 newact.sa_mask = setitimer_mpe_ctx.oldact.sa_mask;
312 newact.sa_flags = setitimer_mpe_ctx.oldact.sa_flags;
313
314 if (sigaction(SIGALRM, &newact, NULL) < 0) {
315 fprintf(stderr,"MPE setitimer new handler failed, errno=%d\n",errno);
316 return -1;
317 }
318 }
319
320 /*
321 * Return previous itimerval if desired
322 */
323
324 if (ovalue != NULL) *ovalue = setitimer_mpe_ctx.current_itimerval;
325
326 /*
327 * Save current parameters for later usage
328 */
329
330 setitimer_mpe_ctx.current_itimerval = *value;
331 setitimer_mpe_ctx.current_msec = value_msec;
332 setitimer_mpe_ctx.value_msec = value_msec;
333 setitimer_mpe_ctx.interval_msec = interval_msec;
334
335 /*
336 * Schedule the first alarm
337 */
338
339 remaining_msec = px_alarm(value_msec, &alarm_hpe_status);
340 if (alarm_hpe_status == 0)
341 return (0);
342 else
343 return (-1);
344 }
345
346 /*
347 * MPE lacks gettimeofday(), so we define our own.
348 */
349
gettimeofday(struct timeval * tvp)350 int gettimeofday(struct timeval *tvp)
351
352 {
353 /* Documented, supported MPE functions. */
354 extern void GETPRIVMODE(void);
355 extern void GETUSERMODE(void);
356
357 /* Undocumented, unsupported MPE functions. */
358 extern long long get_time(void);
359 extern void get_time_change_info(long long *, char *, char *);
360 extern long long ticks_to_micro(long long);
361
362 char pwf_since_boot, recover_pwf_time;
363 long long mpetime, offset_ticks, offset_usec;
364
365 GETPRIVMODE();
366 mpetime = get_time(); /* MPE local time usecs since Jan 1 1970 */
367 get_time_change_info(&offset_ticks, &pwf_since_boot, &recover_pwf_time);
368 offset_usec = ticks_to_micro(offset_ticks); /* UTC offset usecs */
369 GETUSERMODE();
370
371 mpetime = mpetime - offset_usec; /* Convert from local time to UTC */
372 tvp->tv_sec = mpetime / 1000000LL;
373 tvp->tv_usec = mpetime % 1000000LL;
374
375 return 0;
376 }
377
378 /*
379 * MPE lacks settimeofday(), so we define our own.
380 */
381
382 #define HAVE_SETTIMEOFDAY
383
settimeofday(struct timeval * tvp)384 int settimeofday(struct timeval *tvp)
385
386 {
387 /* Documented, supported MPE functions. */
388 extern void GETPRIVMODE(void);
389 extern void GETUSERMODE(void);
390
391 /* Undocumented, unsupported MPE functions. */
392 extern void get_time_change_info(long long *, char *, char *);
393 extern void initialize_system_time(long long, int);
394 extern void set_time_correction(long long, int, int);
395 extern long long ticks_to_micro(long long);
396
397 char pwf_since_boot, recover_pwf_time;
398 long long big_sec, big_usec, mpetime, offset_ticks, offset_usec;
399
400 big_sec = tvp->tv_sec;
401 big_usec = tvp->tv_usec;
402 mpetime = (big_sec * 1000000LL) + big_usec; /* Desired UTC microseconds */
403
404 GETPRIVMODE();
405 set_time_correction(0LL,0,0); /* Cancel previous time correction, if any */
406 get_time_change_info(&offset_ticks, &pwf_since_boot, &recover_pwf_time);
407 offset_usec = ticks_to_micro(offset_ticks); /* UTC offset microseconds */
408 mpetime = mpetime + offset_usec; /* Convert from UTC to local time */
409 initialize_system_time(mpetime,1);
410 GETUSERMODE();
411
412 return 0;
413 }
414 #endif /* MPE */
415
416 #define SET_TOD_UNDETERMINED 0
417 #define SET_TOD_CLOCK_SETTIME 1
418 #define SET_TOD_SETTIMEOFDAY 2
419 #define SET_TOD_STIME 3
420
421 const char * const set_tod_used[] = {
422 "undetermined",
423 "clock_settime",
424 "settimeofday",
425 "stime"
426 };
427
428 pset_tod_using set_tod_using = NULL;
429
430
431 int
ntp_set_tod(struct timeval * tvp,void * tzp)432 ntp_set_tod(
433 struct timeval *tvp,
434 void *tzp
435 )
436 {
437 static int tod;
438 int rc;
439 int saved_errno;
440
441 TRACE(1, ("In ntp_set_tod\n"));
442 rc = -1;
443 saved_errno = 0;
444
445 #ifdef HAVE_CLOCK_SETTIME
446 if (rc && (SET_TOD_CLOCK_SETTIME == tod || !tod)) {
447 struct timespec ts;
448
449 /* Convert timeval to timespec */
450 ts.tv_sec = tvp->tv_sec;
451 ts.tv_nsec = 1000 * tvp->tv_usec;
452
453 errno = 0;
454 rc = clock_settime(CLOCK_REALTIME, &ts);
455 saved_errno = errno;
456 TRACE(1, ("ntp_set_tod: clock_settime: %d %m\n", rc));
457 if (!tod && !rc)
458 tod = SET_TOD_CLOCK_SETTIME;
459
460 }
461 #endif /* HAVE_CLOCK_SETTIME */
462 #ifdef HAVE_SETTIMEOFDAY
463 if (rc && (SET_TOD_SETTIMEOFDAY == tod || !tod)) {
464 struct timeval adjtv;
465
466 /*
467 * Some broken systems don't reset adjtime() when the
468 * clock is stepped.
469 */
470 adjtv.tv_sec = adjtv.tv_usec = 0;
471 adjtime(&adjtv, NULL);
472 errno = 0;
473 rc = SETTIMEOFDAY(tvp, tzp);
474 saved_errno = errno;
475 TRACE(1, ("ntp_set_tod: settimeofday: %d %m\n", rc));
476 if (!tod && !rc)
477 tod = SET_TOD_SETTIMEOFDAY;
478 }
479 #endif /* HAVE_SETTIMEOFDAY */
480 #ifdef HAVE_STIME
481 if (rc && (SET_TOD_STIME == tod || !tod)) {
482 long tp = tvp->tv_sec;
483
484 errno = 0;
485 rc = stime(&tp); /* lie as bad as SysVR4 */
486 saved_errno = errno;
487 TRACE(1, ("ntp_set_tod: stime: %d %m\n", rc));
488 if (!tod && !rc)
489 tod = SET_TOD_STIME;
490 }
491 #endif /* HAVE_STIME */
492
493 errno = saved_errno; /* for %m below */
494 TRACE(1, ("ntp_set_tod: Final result: %s: %d %m\n",
495 set_tod_used[tod], rc));
496 /*
497 * Say how we're setting the time of day
498 */
499 if (!rc && NULL != set_tod_using) {
500 (*set_tod_using)(set_tod_used[tod]);
501 set_tod_using = NULL;
502 }
503
504 if (rc)
505 errno = saved_errno;
506
507 return rc;
508 }
509
510 #endif /* not SYS_WINNT */
511
512 #if defined (SYS_WINNT) || defined (SYS_VXWORKS) || defined(MPE)
513 /* getpass is used in ntpq.c and ntpdc.c */
514
515 char *
getpass(const char * prompt)516 getpass(const char * prompt)
517 {
518 int c, i;
519 static char password[32];
520
521 fprintf(stderr, "%s", prompt);
522 fflush(stderr);
523
524 for (i=0; i<sizeof(password)-1 && ((c=_getch())!='\n' && c!='\r'); i++) {
525 password[i] = (char) c;
526 }
527 password[i] = '\0';
528
529 fputc('\n', stderr);
530 fflush(stderr);
531
532 return password;
533 }
534 #endif /* SYS_WINNT */
535
536
537 static const int baudTable[][2] = {
538 {B0, 0},
539 {B50, 50},
540 {B75, 75},
541 {B110, 110},
542 {B134, 134},
543 {B150, 150},
544 {B200, 200},
545 {B300, 300},
546 {B600, 600},
547 {B1200, 1200},
548 {B1800, 1800},
549 {B2400, 2400},
550 {B4800, 4800},
551 {B9600, 9600},
552 {B19200, 19200},
553 {B38400, 38400},
554 # ifdef B57600
555 {B57600, 57600 },
556 # endif
557 # ifdef B115200
558 {B115200, 115200},
559 # endif
560 {-1, -1}
561 };
562
563
symBaud2numBaud(int symBaud)564 int symBaud2numBaud(int symBaud)
565 {
566 int i;
567
568 for (i = 0; baudTable[i][1] >= 0; ++i) {
569 if (baudTable[i][0] == symBaud) {
570 break;
571 }
572 }
573 return baudTable[i][1];
574 }
575
576
577 #if 0 /* unused */
578 int numBaud2symBaud(int numBaud)
579 {
580 int i;
581
582 for (i = 0; baudTable[i][1] >= 0; ++i) {
583 if (baudTable[i][1] == numBaud) {
584 break;
585 }
586 }
587 return baudTable[i][0];
588 }
589 #endif /* unused fn */
590