xref: /freebsd/contrib/ntp/libntp/machines.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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_SYS_TIME_H
17 #include <sys/time.h>
18 #endif
19 
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
23 
24 #ifdef SYS_WINNT
25 # 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>
38 int h_errno;
39 
40 struct 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 
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 *) 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 
67 struct 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  */
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  */
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
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 const char *set_tod_using = "UNKNOWN";
133 
134 int
135 ntp_set_tod(
136 	struct timeval *tvp,
137 	void *tzp
138 	)
139 {
140 	int rc;
141 
142 #ifdef HAVE_CLOCK_SETTIME
143 	{
144 		struct timespec ts;
145 
146 		/* Convert timeval to timespec */
147 		ts.tv_sec = tvp->tv_sec;
148 		ts.tv_nsec = 1000 *  tvp->tv_usec;
149 
150 		rc = clock_settime(CLOCK_REALTIME, &ts);
151 		if (!rc)
152 		{
153 			set_tod_using = "clock_settime";
154 			return rc;
155 		}
156 	}
157 #endif /* HAVE_CLOCK_SETTIME */
158 #ifdef HAVE_SETTIMEOFDAY
159 	{
160 		rc = SETTIMEOFDAY(tvp, tzp);
161 		if (!rc)
162 		{
163 			set_tod_using = "settimeofday";
164 			return rc;
165 		}
166 	}
167 #endif /* HAVE_SETTIMEOFDAY */
168 #ifdef HAVE_STIME
169 	{
170 		long tp = tvp->tv_sec;
171 
172 		rc = stime(&tp); /* lie as bad as SysVR4 */
173 		if (!rc)
174 		{
175 			set_tod_using = "stime";
176 			return rc;
177 		}
178 	}
179 #endif /* HAVE_STIME */
180 	set_tod_using = "Failed!";
181 	return -1;
182 }
183 
184 #endif /* not SYS_WINNT */
185 
186 #if defined (SYS_WINNT) || defined (SYS_VXWORKS)
187 /* getpass is used in ntpq.c and ntpdc.c */
188 
189 char *
190 getpass(const char * prompt)
191 {
192 	int c, i;
193 	static char password[32];
194 #ifdef DEBUG
195 	fprintf(stderr, "%s", prompt);
196 	fflush(stderr);
197 #endif
198 	for (i=0; i<sizeof(password)-1 && ((c=_getch())!='\n'); i++) {
199 		password[i] = (char) c;
200 	}
201 	password[i] = '\0';
202 
203 	return password;
204 }
205 #endif /* SYS_WINNT */
206 
207 #if !defined(HAVE_MEMSET)
208 void
209 ntp_memset(
210 	char *a,
211 	int x,
212 	int c
213 	)
214 {
215 	while (c-- > 0)
216 		*a++ = (char) x;
217 }
218 #endif /*POSIX*/
219