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