drm/linux: Add io_schedule_timeout()
[dragonfly.git] / usr.sbin / timed / timedc / cmds.c
1 /*-
2  * Copyright (c) 1985, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)cmds.c   8.1 (Berkeley) 6/6/93
30  * $FreeBSD: src/usr.sbin/timed/timedc/cmds.c,v 1.6.2.2 2001/08/31 08:02:06 kris Exp $
31  */
32
33 #include "timedc.h"
34 #include <sys/file.h>
35
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/ip_icmp.h>
39
40 #include <err.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 #define TSPTYPES
46 #include <protocols/timed.h>
47
48 #ifdef sgi
49 #include <bstring.h>
50 #include <sys/clock.h>
51 #else
52 #define SECHR   (60*60)
53 #define SECDAY  (24*SECHR)
54 #endif /* sgi */
55
56 # define DATE_PROTO "udp"
57 # define DATE_PORT "time"
58
59
60 int sock;
61 int sock_raw;
62 char myname[MAXHOSTNAMELEN];
63 struct hostent *hp;
64 struct sockaddr_in server;
65 struct sockaddr_in dayaddr;
66 extern int measure_delta;
67
68 void bytenetorder(struct tsp *);
69 void bytehostorder(struct tsp *);
70
71
72 #define BU (2208988800UL)       /* seconds before UNIX epoch */
73
74
75 /* compute the difference between our date and another machine
76  */
77 static int                              /* difference in days from our time */
78 daydiff(char *hostname)
79 {
80         int i;
81         int trials;
82         struct timeval tout, now;
83         fd_set ready;
84         struct sockaddr from;
85         int fromlen;
86         unsigned long sec;
87
88         /* wait 2 seconds between 10 tries */
89         tout.tv_sec = 2;
90         tout.tv_usec = 0;
91         for (trials = 0; trials < 10; trials++) {
92                 /* ask for the time */
93                 sec = 0;
94                 if (sendto(sock, &sec, sizeof(sec), 0,
95                            (struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
96                         warn("sendto(sock)");
97                         return 0;
98                 }
99
100                 for (;;) {
101                         FD_ZERO(&ready);
102                         FD_SET(sock, &ready);
103                         i = select(sock+1, &ready, NULL, NULL, &tout);
104                         if (i < 0) {
105                                 if (errno == EINTR)
106                                         continue;
107                                 warn("select(date read)");
108                                 return 0;
109                         }
110                         if (0 == i)
111                                 break;
112
113                         fromlen = sizeof(from);
114                         if (recvfrom(sock,&sec,sizeof(sec),0,
115                                      &from,&fromlen) < 0) {
116                                 warn("recvfrom(date read)");
117                                 return 0;
118                         }
119
120                         sec = ntohl(sec);
121                         if (sec < BU) {
122                                 warnx("%s says it is before 1970: %lu",
123                                         hostname, sec);
124                                 return 0;
125                         }
126                         sec -= BU;
127
128                         gettimeofday(&now, NULL);
129                         return (sec - now.tv_sec);
130                 }
131         }
132
133         /* if we get here, we tried too many times */
134         warnx("%s will not tell us the date", hostname);
135         return 0;
136 }
137
138
139 /*
140  * Clockdiff computes the difference between the time of the machine on
141  * which it is called and the time of the machines given as argument.
142  * The time differences measured by clockdiff are obtained using a sequence
143  * of ICMP TSTAMP messages which are returned to the sender by the IP module
144  * in the remote machine.
145  * In order to compare clocks of machines in different time zones, the time
146  * is transmitted (as a 32-bit value) in milliseconds since midnight UT.
147  * If a hosts uses a different time format, it should set the high order
148  * bit of the 32-bit quantity it transmits.
149  * However, VMS apparently transmits the time in milliseconds since midnight
150  * local time (rather than GMT) without setting the high order bit.
151  * Furthermore, it does not understand daylight-saving time.  This makes
152  * clockdiff behaving inconsistently with hosts running VMS.
153  *
154  * In order to reduce the sensitivity to the variance of message transmission
155  * time, clockdiff sends a sequence of messages.  Yet, measures between
156  * two `distant' hosts can be affected by a small error. The error can,
157  * however, be reduced by increasing the number of messages sent in each
158  * measurement.
159  */
160 void
161 clockdiff(int argc, char *argv[])
162 {
163         int measure_status;
164         extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
165         int avg_cnt;
166         long avg;
167         struct servent *sp;
168
169         if (argc < 2)  {
170                 printf("usage: timedc clockdiff host ...\n");
171                 return;
172         }
173
174         if (gethostname(myname, sizeof(myname) - 1) < 0)
175                 err(1, "gethostname");
176
177         /* get the address for the date ready */
178         sp = getservbyname(DATE_PORT, DATE_PROTO);
179         if (!sp) {
180                 warnx("%s/%s is an unknown service", DATE_PORT, DATE_PROTO);
181                 dayaddr.sin_port = 0;
182         } else {
183                 dayaddr.sin_port = sp->s_port;
184         }
185
186         while (argc > 1) {
187                 argc--; argv++;
188                 hp = gethostbyname(*argv);
189                 if (hp == NULL) {
190                         warnx("%s: %s", *argv, hstrerror(h_errno));
191                         continue;
192                 }
193
194                 server.sin_family = hp->h_addrtype;
195                 bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
196                 for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
197                         measure_status = measure(10000,100, *argv, &server, 1);
198                         if (measure_status != GOOD)
199                                 break;
200                         avg += measure_delta;
201                 }
202                 if (measure_status == GOOD)
203                         measure_delta = avg/avg_cnt;
204
205                 switch (measure_status) {
206                 case HOSTDOWN:
207                         printf("%s is down\n", hp->h_name);
208                         continue;
209                 case NONSTDTIME:
210                         printf("%s transmitts a non-standard time format\n",
211                                hp->h_name);
212                         continue;
213                 case UNREACHABLE:
214                         printf("%s is unreachable\n", hp->h_name);
215                         continue;
216                 }
217
218                 /*
219                  * Try to get the date only after using ICMP timestamps to
220                  * get the time.  This is because the date protocol
221                  * is optional.
222                  */
223                 if (dayaddr.sin_port != 0) {
224                         dayaddr.sin_family = hp->h_addrtype;
225                         bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr,
226                               hp->h_length);
227                         avg = daydiff(*argv);
228                         if (avg > SECDAY) {
229                                 printf("time on %s is %ld days ahead %s\n",
230                                        hp->h_name, avg/SECDAY, myname);
231                                 continue;
232                         } else if (avg < -SECDAY) {
233                                 printf("time on %s is %ld days behind %s\n",
234                                        hp->h_name, -avg/SECDAY, myname);
235                                 continue;
236                         }
237                 }
238
239                 if (measure_delta > 0) {
240                         printf("time on %s is %d ms. ahead of time on %s\n",
241                                hp->h_name, measure_delta, myname);
242                 } else if (measure_delta == 0) {
243                         printf("%s and %s have the same time\n",
244                                hp->h_name, myname);
245                 } else {
246                         printf("time on %s is %d ms. behind time on %s\n",
247                                hp->h_name, -measure_delta, myname);
248                 }
249         }
250         return;
251 }
252
253
254 /*
255  * finds location of master timedaemon
256  */
257 void
258 msite(int argc, char *argv[])
259 {
260         int cc;
261         fd_set ready;
262         struct sockaddr_in dest;
263         int i, length;
264         struct sockaddr_in from;
265         struct timeval tout;
266         struct tsp msg;
267         struct servent *srvp;
268         char *tgtname;
269
270         if (argc < 1) {
271                 printf("usage: timedc msite [host ...]\n");
272                 return;
273         }
274
275         srvp = getservbyname("timed", "udp");
276         if (srvp == NULL) {
277                 warnx("udp/timed: unknown service");
278                 return;
279         }
280         dest.sin_port = srvp->s_port;
281         dest.sin_family = AF_INET;
282
283         if (gethostname(myname, sizeof(myname) - 1) < 0)
284                 err(1, "gethostname");
285         i = 1;
286         do {
287                 tgtname = (i >= argc) ? myname : argv[i];
288                 hp = gethostbyname(tgtname);
289                 if (hp == NULL) {
290                         warnx("%s: %s", tgtname, hstrerror(h_errno));
291                         continue;
292                 }
293                 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
294
295                 strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
296                 msg.tsp_type = TSP_MSITE;
297                 msg.tsp_vers = TSPVERSION;
298                 bytenetorder(&msg);
299                 if (sendto(sock, &msg, sizeof(struct tsp), 0,
300                            (struct sockaddr*)&dest,
301                            sizeof(struct sockaddr)) < 0) {
302                         warn("sendto");
303                         continue;
304                 }
305
306                 tout.tv_sec = 15;
307                 tout.tv_usec = 0;
308                 FD_ZERO(&ready);
309                 FD_SET(sock, &ready);
310                 if (select(FD_SETSIZE, &ready, NULL, NULL, &tout)) {
311                         length = sizeof(from);
312                         cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
313                                       (struct sockaddr *)&from, &length);
314                         if (cc < 0) {
315                                 warn("recvfrom");
316                                 continue;
317                         }
318                         /*
319                          * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
320                          * this is still OS-dependent.  Demand that the packet is at
321                          * least long enough to hold a 4.3BSD packet.
322                          */
323                         if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
324                                 fprintf(stderr, 
325                                    "short packet (%d/%zu bytes) from %s\n",
326                                    cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
327                                    inet_ntoa(from.sin_addr));
328                                 continue;
329                         }
330                         bytehostorder(&msg);
331                         if (msg.tsp_type == TSP_ACK) {
332                                 printf("master timedaemon at %s is %s\n",
333                                        tgtname, msg.tsp_name);
334                         } else {
335                                 if (msg.tsp_type >= TSPTYPENUMBER)
336                                         printf("unknown ack received: %u\n",
337                                                 msg.tsp_type);
338                                 else    
339                                         printf("wrong ack received: %s\n",
340                                                 tsptype[msg.tsp_type]);
341                         }
342                 } else {
343                         printf("communication error with %s\n", tgtname);
344                 }
345         } while (++i < argc);
346 }
347
348 /*
349  * quits timedc
350  */
351 void
352 quit(void)
353 {
354
355         exit(0);
356 }
357
358
359 /*
360  * Causes the election timer to expire on the selected hosts
361  * It sends just one udp message per machine, relying on
362  * reliability of communication channel.
363  */
364 void
365 testing(int argc, char *argv[])
366 {
367         struct servent *srvp;
368         struct sockaddr_in sin;
369         struct tsp msg;
370
371         if (argc < 2)  {
372                 printf("usage: timedc election host1 [host2 ...]\n");
373                 return;
374         }
375
376         srvp = getservbyname("timed", "udp");
377         if (srvp == NULL) {
378                 warnx("udp/timed: unknown service");
379                 return;
380         }
381
382         while (argc > 1) {
383                 argc--; argv++;
384                 hp = gethostbyname(*argv);
385                 if (hp == NULL) {
386                         warnx("%s: %s", *argv, hstrerror(h_errno));
387                         argc--; argv++;
388                         continue;
389                 }
390                 sin.sin_port = srvp->s_port;
391                 sin.sin_family = hp->h_addrtype;
392                 bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length);
393
394                 msg.tsp_type = TSP_TEST;
395                 msg.tsp_vers = TSPVERSION;
396                 if (gethostname(myname, sizeof(myname) - 1) < 0)
397                         err(1, "gethostname");
398                 strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
399                 bytenetorder(&msg);
400                 if (sendto(sock, &msg, sizeof(struct tsp), 0,
401                            (struct sockaddr*)&sin,
402                            sizeof(struct sockaddr)) < 0) {
403                         warn("sendto");
404                 }
405         }
406 }
407
408
409 /*
410  * Enables or disables tracing on local timedaemon
411  */
412 void
413 tracing(int argc, char *argv[])
414 {
415         int onflag;
416         int length;
417         int cc;
418         fd_set ready;
419         struct sockaddr_in dest;
420         struct sockaddr_in from;
421         struct timeval tout;
422         struct tsp msg;
423         struct servent *srvp;
424
425         if (argc != 2) {
426                 printf("usage: timedc trace { on | off }\n");
427                 return;
428         }
429
430         srvp = getservbyname("timed", "udp");
431         if (srvp == NULL) {
432                 warnx("udp/timed: unknown service");
433                 return;
434         }
435         dest.sin_port = srvp->s_port;
436         dest.sin_family = AF_INET;
437
438         if (gethostname(myname, sizeof(myname) - 1) < 0)
439                 err(1, "gethostname");
440         hp = gethostbyname(myname);
441         bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
442
443         if (strcmp(argv[1], "on") == 0) {
444                 msg.tsp_type = TSP_TRACEON;
445                 onflag = ON;
446         } else {
447                 msg.tsp_type = TSP_TRACEOFF;
448                 onflag = OFF;
449         }
450
451         strcpy(msg.tsp_name, myname);
452         msg.tsp_vers = TSPVERSION;
453         bytenetorder(&msg);
454         if (sendto(sock, &msg, sizeof(struct tsp), 0,
455                    (struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) {
456                 warn("sendto");
457                 return;
458         }
459
460         tout.tv_sec = 5;
461         tout.tv_usec = 0;
462         FD_ZERO(&ready);
463         FD_SET(sock, &ready);
464         if (select(FD_SETSIZE, &ready, NULL, NULL, &tout)) {
465                 length = sizeof(from);
466                 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
467                               (struct sockaddr *)&from, &length);
468                 if (cc < 0) {
469                         warn("recvfrom");
470                         return;
471                 }
472                 /*
473                  * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
474                  * this is still OS-dependent.  Demand that the packet is at
475                  * least long enough to hold a 4.3BSD packet.
476                  */
477                 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
478                         fprintf(stderr, "short packet (%d/%zu bytes) from %s\n",
479                             cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
480                             inet_ntoa(from.sin_addr));
481                         return;
482                 }
483                 bytehostorder(&msg);
484                 if (msg.tsp_type == TSP_ACK)
485                         if (onflag)
486                                 printf("timed tracing enabled\n");
487                         else
488                                 printf("timed tracing disabled\n");
489                 else {
490                         if (msg.tsp_type >= TSPTYPENUMBER)
491                                 printf("unknown ack received: %u\n",
492                                         msg.tsp_type);
493                         else    
494                                 printf("wrong ack received: %s\n",
495                                                 tsptype[msg.tsp_type]);
496                 }
497         } else
498                 printf("communication error\n");
499 }
500
501 int
502 priv_resources(void)
503 {
504         int port;
505         struct sockaddr_in sin;
506
507         sock = socket(AF_INET, SOCK_DGRAM, 0);
508         if (sock < 0) {
509                 warn("opening socket");
510                 return(-1);
511         }
512
513         sin.sin_family = AF_INET;
514         sin.sin_addr.s_addr = 0;
515         for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
516                 sin.sin_port = htons((u_short)port);
517                 if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
518                         break;
519                 if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
520                         warn("bind");
521                         close(sock);
522                         return(-1);
523                 }
524         }
525         if (port == IPPORT_RESERVED / 2) {
526                 warnx("all reserved ports in use");
527                 close(sock);
528                 return(-1);
529         }
530
531         sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
532         if (sock_raw < 0)  {
533                 warn("opening raw socket");
534                 close(sock);
535                 return(-1);
536         }
537         return(1);
538 }