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