1/*	$NetBSD$	*/
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
20#ifdef HAVE_UNISTD_H
21#include <unistd.h>
22#endif
23
24#ifdef SYS_WINNT
25int _getch(void);	/* Declare the one function rather than include conio.h */
26#else
27
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>
38int h_errno;
39
40struct hostent *gethostbyname(char *name)
41	{
42	struct hostent *host1;
43	h_errno = 0;					/* we are always successful!!! */
44	host1 = (struct hostent *) malloc (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
54struct 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 *) malloc (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
67struct servent *getservbyname (char *name, char *type)
68	{
69	struct servent *serv1;
70	serv1 = (struct servent *) malloc (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 */
98void 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 */
107void 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
118int
119gettimeofday(
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
134port 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
142extern void GETPRIVMODE(void);
143extern void GETUSERMODE(void);
144
145int __ntp_mpe_bind(int s, void *addr, int addrlen);
146
147int __ntp_mpe_bind(int s, void *addr, int addrlen) {
148	int priv = 0;
149	int result;
150
151if (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
163return 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
176int __ntp_mpe_fcntl(int fd, int cmd, int arg);
177
178int __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
215struct {
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
227extern 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
235static RETSIGTYPE setitimer_mpe_handler(int sig)
236{
237int alarm_hpe_status;
238
239/* Update the new current alarm value */
240
241setitimer_mpe_ctx.current_msec = setitimer_mpe_ctx.interval_msec;
242
243if (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
253if (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
264int
265setitimer(int which, struct itimerval *value,
266	    struct itimerval *ovalue)
267{
268
269int alarm_hpe_status;
270unsigned long remaining_msec, value_msec, interval_msec;
271struct sigaction newact;
272
273/*
274 * Convert the initial interval to milliseconds
275 */
276
277if (value->it_value.tv_sec > (UINT_MAX / 1000))
278  value_msec = UINT_MAX;
279else
280  value_msec = value->it_value.tv_sec * 1000;
281
282value_msec += value->it_value.tv_usec / 1000;
283
284/*
285 * Convert the reset interval to milliseconds
286 */
287
288if (value->it_interval.tv_sec > (UINT_MAX / 1000))
289  interval_msec = UINT_MAX;
290else
291  interval_msec = value->it_interval.tv_sec * 1000;
292
293interval_msec += value->it_interval.tv_usec / 1000;
294
295if (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
324if (ovalue != NULL) *ovalue = setitimer_mpe_ctx.current_itimerval;
325
326/*
327 * Save current parameters for later usage
328 */
329
330setitimer_mpe_ctx.current_itimerval = *value;
331setitimer_mpe_ctx.current_msec = value_msec;
332setitimer_mpe_ctx.value_msec = value_msec;
333setitimer_mpe_ctx.interval_msec = interval_msec;
334
335/*
336 * Schedule the first alarm
337 */
338
339remaining_msec = px_alarm(value_msec, &alarm_hpe_status);
340if (alarm_hpe_status == 0)
341  return (0);
342else
343  return (-1);
344}
345
346/*
347 * MPE lacks gettimeofday(), so we define our own.
348 */
349
350int gettimeofday(struct timeval *tvp)
351
352{
353/* Documented, supported MPE functions. */
354extern void GETPRIVMODE(void);
355extern void GETUSERMODE(void);
356
357/* Undocumented, unsupported MPE functions. */
358extern long long get_time(void);
359extern void get_time_change_info(long long *, char *, char *);
360extern long long ticks_to_micro(long long);
361
362char pwf_since_boot, recover_pwf_time;
363long long mpetime, offset_ticks, offset_usec;
364
365GETPRIVMODE();
366mpetime = get_time(); /* MPE local time usecs since Jan 1 1970 */
367get_time_change_info(&offset_ticks, &pwf_since_boot, &recover_pwf_time);
368offset_usec = ticks_to_micro(offset_ticks);  /* UTC offset usecs */
369GETUSERMODE();
370
371mpetime = mpetime - offset_usec;  /* Convert from local time to UTC */
372tvp->tv_sec = mpetime / 1000000LL;
373tvp->tv_usec = mpetime % 1000000LL;
374
375return 0;
376}
377
378/*
379 * MPE lacks settimeofday(), so we define our own.
380 */
381
382#define HAVE_SETTIMEOFDAY
383
384int settimeofday(struct timeval *tvp)
385
386{
387/* Documented, supported MPE functions. */
388extern void GETPRIVMODE(void);
389extern void GETUSERMODE(void);
390
391/* Undocumented, unsupported MPE functions. */
392extern void get_time_change_info(long long *, char *, char *);
393extern void initialize_system_time(long long, int);
394extern void set_time_correction(long long, int, int);
395extern long long ticks_to_micro(long long);
396
397char pwf_since_boot, recover_pwf_time;
398long long big_sec, big_usec, mpetime, offset_ticks, offset_usec;
399
400big_sec = tvp->tv_sec;
401big_usec = tvp->tv_usec;
402mpetime = (big_sec * 1000000LL) + big_usec;  /* Desired UTC microseconds */
403
404GETPRIVMODE();
405set_time_correction(0LL,0,0); /* Cancel previous time correction, if any */
406get_time_change_info(&offset_ticks, &pwf_since_boot, &recover_pwf_time);
407offset_usec = ticks_to_micro(offset_ticks); /* UTC offset microseconds */
408mpetime = mpetime + offset_usec; /* Convert from UTC to local time */
409initialize_system_time(mpetime,1);
410GETUSERMODE();
411
412return 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
421const char * const set_tod_used[] = {
422	"undetermined",
423	"clock_settime",
424	"settimeofday",
425	"stime"
426};
427
428pset_tod_using	set_tod_using = NULL;
429
430
431int
432ntp_set_tod(
433	struct timeval *tvp,
434	void *tzp
435	)
436{
437	static int tod;
438	int rc = -1;
439	int saved_errno = 0;
440
441#ifdef DEBUG
442	if (debug)
443	    printf("In ntp_set_tod\n");
444#endif
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#ifdef DEBUG
458		if (debug) {
459			printf("ntp_set_tod: clock_settime: %d: %s\n",
460			       rc, strerror(saved_errno));
461		}
462#endif
463		if (!tod && !rc)
464			tod = SET_TOD_CLOCK_SETTIME;
465
466	}
467#endif /* HAVE_CLOCK_SETTIME */
468#ifdef HAVE_SETTIMEOFDAY
469	if (rc && (SET_TOD_SETTIMEOFDAY == tod || !tod)) {
470		struct timeval adjtv;
471
472		/*
473		 * Some broken systems don't reset adjtime() when the
474		 * clock is stepped.
475		 */
476		adjtv.tv_sec = adjtv.tv_usec = 0;
477		adjtime(&adjtv, NULL);
478		errno = 0;
479		rc = SETTIMEOFDAY(tvp, tzp);
480		saved_errno = errno;
481#ifdef DEBUG
482		if (debug) {
483			printf("ntp_set_tod: settimeofday: %d: %s\n",
484			       rc, strerror(saved_errno));
485		}
486#endif
487		if (!tod && !rc)
488			tod = SET_TOD_SETTIMEOFDAY;
489	}
490#endif /* HAVE_SETTIMEOFDAY */
491#ifdef HAVE_STIME
492	if (rc && (SET_TOD_STIME == tod || !tod)) {
493		long tp = tvp->tv_sec;
494
495		errno = 0;
496		rc = stime(&tp); /* lie as bad as SysVR4 */
497		saved_errno = errno;
498#ifdef DEBUG
499		if (debug) {
500			printf("ntp_set_tod: stime: %d: %s\n",
501			       rc, strerror(saved_errno));
502		}
503#endif
504		if (!tod && !rc)
505			tod = SET_TOD_STIME;
506	}
507#endif /* HAVE_STIME */
508
509#ifdef DEBUG
510	if (debug) {
511		printf("ntp_set_tod: Final result: %s: %d: %s\n",
512			set_tod_used[tod], rc, strerror(saved_errno));
513	}
514#endif
515	/*
516	 * Say how we're setting the time of day
517	 */
518	if (!rc && NULL != set_tod_using) {
519		(*set_tod_using)(set_tod_used[tod]);
520		set_tod_using = NULL;
521	}
522
523	if (rc)
524		errno = saved_errno;
525
526	return rc;
527}
528
529#endif /* not SYS_WINNT */
530
531#if defined (SYS_WINNT) || defined (SYS_VXWORKS) || defined(MPE)
532/* getpass is used in ntpq.c and ntpdc.c */
533
534char *
535getpass(const char * prompt)
536{
537	int c, i;
538	static char password[32];
539
540	fprintf(stderr, "%s", prompt);
541	fflush(stderr);
542
543	for (i=0; i<sizeof(password)-1 && ((c=_getch())!='\n' && c!='\r'); i++) {
544		password[i] = (char) c;
545	}
546	password[i] = '\0';
547
548	fputc('\n', stderr);
549	fflush(stderr);
550
551	return password;
552}
553#endif /* SYS_WINNT */
554
555#if !defined(HAVE_MEMSET)
556void
557ntp_memset(
558	char *a,
559	int x,
560	int c
561	)
562{
563	while (c-- > 0)
564		*a++ = (char) x;
565}
566#endif /*POSIX*/
567