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