file reorg, pathnames.h, paths.h
[unix-history] / usr / src / usr.sbin / rwhod / rwhod.c
CommitLineData
3b86b0f6 1/*
86f08f99
KB
2 * Copyright (c) 1983 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
3b86b0f6
DF
16 */
17
18#ifndef lint
19char copyright[] =
86f08f99 20"@(#) Copyright (c) 1983 The Regents of the University of California.\n\
3b86b0f6 21 All rights reserved.\n";
86f08f99 22#endif /* not lint */
3b86b0f6 23
52b4fb0c 24#ifndef lint
7abf8d65 25static char sccsid[] = "@(#)rwhod.c 5.13 (Berkeley) %G%";
86f08f99 26#endif /* not lint */
52b4fb0c 27
52b4fb0c 28#include <sys/types.h>
52b4fb0c 29#include <sys/socket.h>
52b4fb0c 30#include <sys/stat.h>
17ccb086 31#include <sys/signal.h>
52b4fb0c 32#include <sys/ioctl.h>
9bd66dcf 33#include <sys/file.h>
de3b21e8 34
7af17b3e 35#include <net/if.h>
de3b21e8
SL
36#include <netinet/in.h>
37
38#include <nlist.h>
de3b21e8
SL
39#include <errno.h>
40#include <utmp.h>
a0f59ac1
SL
41#include <ctype.h>
42#include <netdb.h>
00cdc205 43#include <syslog.h>
3c13756b 44#include <protocols/rwhod.h>
17ccb086 45#include <stdio.h>
7abf8d65 46#include <paths.h>
de3b21e8 47
83361bb3
RC
48/*
49 * Alarm interval. Don't forget to change the down time check in ruptime
50 * if this is changed.
51 */
00cdc205 52#define AL_INTERVAL (3 * 60)
83361bb3 53
a0f59ac1 54struct sockaddr_in sin = { AF_INET };
52b4fb0c 55
7af17b3e 56char myname[32];
52b4fb0c
BJ
57
58struct nlist nl[] = {
59#define NL_AVENRUN 0
60 { "_avenrun" },
de3b21e8
SL
61#define NL_BOOTTIME 1
62 { "_boottime" },
52b4fb0c
BJ
63 0
64};
65
7af17b3e
SL
66/*
67 * We communicate with each neighbor in
68 * a list constructed at the time we're
69 * started up. Neighbors are currently
70 * directly connected via a hardware interface.
71 */
72struct neighbor {
73 struct neighbor *n_next;
74 char *n_name; /* interface name */
75 char *n_addr; /* who to send to */
76 int n_addrlen; /* size of address */
77 int n_flags; /* should forward?, interface flags */
78};
79
80struct neighbor *neighbors;
52b4fb0c 81struct whod mywd;
7af17b3e 82struct servent *sp;
52b4fb0c
BJ
83int s, utmpf, kmemf = -1;
84
7af17b3e 85#define WHDRSIZE (sizeof (mywd) - sizeof (mywd.wd_we))
fea387f0 86
17ccb086 87extern int errno;
52b4fb0c 88int onalrm();
9bd38ba8 89char *strcpy(), *malloc();
52b4fb0c
BJ
90long lseek();
91int getkmem();
7af17b3e 92struct in_addr inet_makeaddr();
52b4fb0c
BJ
93
94main()
95{
96 struct sockaddr_in from;
0f2d5e68 97 struct stat st;
52b4fb0c 98 char path[64];
221be169 99 int on = 1;
17ccb086 100 char *cp, *index(), *strerror();
52b4fb0c 101
00cdc205
RC
102 if (getuid()) {
103 fprintf(stderr, "rwhod: not super user\n");
104 exit(1);
105 }
a0f59ac1
SL
106 sp = getservbyname("who", "udp");
107 if (sp == 0) {
108 fprintf(stderr, "rwhod: udp/who: unknown service\n");
109 exit(1);
110 }
52b4fb0c
BJ
111#ifndef DEBUG
112 if (fork())
113 exit(0);
114 { int s;
115 for (s = 0; s < 10; s++)
116 (void) close(s);
117 (void) open("/", 0);
118 (void) dup2(0, 1);
119 (void) dup2(0, 2);
7abf8d65 120 s = open(_PATH_TTY, 2);
52b4fb0c
BJ
121 if (s >= 0) {
122 ioctl(s, TIOCNOTTY, 0);
123 (void) close(s);
124 }
125 }
126#endif
17ccb086
KB
127 if (chdir(_PATH_RWHODIR) < 0) {
128 (void)fprintf(stderr, "rwhod: %s: %s\n",
129 _PATH_RWHODIR, strerror(errno));
ac82a1ec
KM
130 exit(1);
131 }
52b4fb0c 132 (void) signal(SIGHUP, getkmem);
076ae92c 133 openlog("rwhod", LOG_PID, LOG_DAEMON);
7af17b3e
SL
134 /*
135 * Establish host name as returned by system.
136 */
137 if (gethostname(myname, sizeof (myname) - 1) < 0) {
00cdc205 138 syslog(LOG_ERR, "gethostname: %m");
52b4fb0c
BJ
139 exit(1);
140 }
d2444544
JB
141 if ((cp = index(myname, '.')) != NULL)
142 *cp = '\0';
7af17b3e 143 strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
17ccb086 144 utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
52b4fb0c 145 if (utmpf < 0) {
17ccb086 146 syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
52b4fb0c
BJ
147 exit(1);
148 }
149 getkmem();
9bd66dcf 150 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00cdc205 151 syslog(LOG_ERR, "socket: %m");
de3b21e8
SL
152 exit(1);
153 }
3de1986a 154 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
b0e1e5f3
MK
155 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
156 exit(1);
157 }
7af17b3e 158 sin.sin_port = sp->s_port;
9bd66dcf 159 if (bind(s, &sin, sizeof (sin)) < 0) {
00cdc205 160 syslog(LOG_ERR, "bind: %m");
de3b21e8 161 exit(1);
52b4fb0c 162 }
7af17b3e
SL
163 if (!configure(s))
164 exit(1);
8a53982e 165 signal(SIGALRM, onalrm);
52b4fb0c
BJ
166 onalrm();
167 for (;;) {
168 struct whod wd;
7af17b3e 169 int cc, whod, len = sizeof (from);
52b4fb0c 170
7af17b3e
SL
171 cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
172 &from, &len);
52b4fb0c
BJ
173 if (cc <= 0) {
174 if (cc < 0 && errno != EINTR)
00cdc205 175 syslog(LOG_WARNING, "recv: %m");
52b4fb0c
BJ
176 continue;
177 }
a0f59ac1 178 if (from.sin_port != sp->s_port) {
00cdc205 179 syslog(LOG_WARNING, "%d: bad from port",
a0f59ac1 180 ntohs(from.sin_port));
52b4fb0c
BJ
181 continue;
182 }
a0f59ac1
SL
183#ifdef notdef
184 if (gethostbyname(wd.wd_hostname) == 0) {
00cdc205 185 syslog(LOG_WARNING, "%s: unknown host",
a0f59ac1
SL
186 wd.wd_hostname);
187 continue;
188 }
189#endif
aaddf8a1
SL
190 if (wd.wd_vers != WHODVERSION)
191 continue;
7af17b3e
SL
192 if (wd.wd_type != WHODTYPE_STATUS)
193 continue;
a0f59ac1 194 if (!verify(wd.wd_hostname)) {
00cdc205 195 syslog(LOG_WARNING, "malformed host name from %x",
a0f59ac1 196 from.sin_addr);
52b4fb0c
BJ
197 continue;
198 }
ac82a1ec 199 (void) sprintf(path, "whod.%s", wd.wd_hostname);
0f2d5e68
MK
200 /*
201 * Rather than truncating and growing the file each time,
202 * use ftruncate if size is less than previous size.
203 */
204 whod = open(path, O_WRONLY | O_CREAT, 0644);
52b4fb0c 205 if (whod < 0) {
00cdc205 206 syslog(LOG_WARNING, "%s: %m", path);
52b4fb0c
BJ
207 continue;
208 }
fea387f0
SL
209#if vax || pdp11
210 {
9bd66dcf 211 int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
fea387f0
SL
212 struct whoent *we;
213
214 /* undo header byte swapping before writing to file */
215 wd.wd_sendtime = ntohl(wd.wd_sendtime);
216 for (i = 0; i < 3; i++)
217 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
218 wd.wd_boottime = ntohl(wd.wd_boottime);
219 we = wd.wd_we;
220 for (i = 0; i < n; i++) {
221 we->we_idle = ntohl(we->we_idle);
1cccbcb3
SL
222 we->we_utmp.out_time =
223 ntohl(we->we_utmp.out_time);
fea387f0
SL
224 we++;
225 }
226 }
227#endif
52b4fb0c
BJ
228 (void) time(&wd.wd_recvtime);
229 (void) write(whod, (char *)&wd, cc);
0f2d5e68
MK
230 if (fstat(whod, &st) < 0 || st.st_size > cc)
231 ftruncate(whod, cc);
52b4fb0c
BJ
232 (void) close(whod);
233 }
234}
235
a0f59ac1
SL
236/*
237 * Check out host name for unprintables
238 * and other funnies before allowing a file
239 * to be created. Sorry, but blanks aren't allowed.
240 */
241verify(name)
242 register char *name;
243{
244 register int size = 0;
245
246 while (*name) {
77650176 247 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
a0f59ac1
SL
248 return (0);
249 name++, size++;
250 }
251 return (size > 0);
252}
253
52b4fb0c
BJ
254int utmptime;
255int utmpent;
4c9b3acd
JB
256int utmpsize = 0;
257struct utmp *utmp;
52b4fb0c
BJ
258int alarmcount;
259
260onalrm()
261{
17ccb086
KB
262 register struct neighbor *np;
263 register struct whoent *we = mywd.wd_we, *wlast;
52b4fb0c
BJ
264 register int i;
265 struct stat stb;
52b4fb0c
BJ
266 int cc;
267 double avenrun[3];
17ccb086
KB
268 time_t now = time((time_t *)NULL);
269 char *strerror();
52b4fb0c
BJ
270
271 if (alarmcount % 10 == 0)
272 getkmem();
273 alarmcount++;
274 (void) fstat(utmpf, &stb);
4c9b3acd 275 if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
02d31241 276 utmptime = stb.st_mtime;
4c9b3acd
JB
277 if (stb.st_size > utmpsize) {
278 utmpsize = stb.st_size + 10 * sizeof(struct utmp);
279 if (utmp)
280 utmp = (struct utmp *)realloc(utmp, utmpsize);
281 else
282 utmp = (struct utmp *)malloc(utmpsize);
283 if (! utmp) {
284 fprintf(stderr, "rwhod: malloc failed\n");
285 utmpsize = 0;
286 goto done;
287 }
288 }
9bd66dcf 289 (void) lseek(utmpf, (long)0, L_SET);
4c9b3acd 290 cc = read(utmpf, (char *)utmp, stb.st_size);
52b4fb0c 291 if (cc < 0) {
17ccb086
KB
292 fprintf(stderr, "rwhod: %s: %s\n",
293 _PATH_UTMP, strerror(errno));
08d10f57 294 goto done;
52b4fb0c 295 }
7af17b3e 296 wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
52b4fb0c
BJ
297 utmpent = cc / sizeof (struct utmp);
298 for (i = 0; i < utmpent; i++)
299 if (utmp[i].ut_name[0]) {
b1456c48
SL
300 bcopy(utmp[i].ut_line, we->we_utmp.out_line,
301 sizeof (utmp[i].ut_line));
302 bcopy(utmp[i].ut_name, we->we_utmp.out_name,
303 sizeof (utmp[i].ut_name));
b1456c48 304 we->we_utmp.out_time = htonl(utmp[i].ut_time);
76886f32
SL
305 if (we >= wlast)
306 break;
52b4fb0c
BJ
307 we++;
308 }
309 utmpent = we - mywd.wd_we;
310 }
221be169
KM
311
312 /*
313 * The test on utmpent looks silly---after all, if no one is
314 * logged on, why worry about efficiency?---but is useful on
315 * (e.g.) compute servers.
316 */
7abf8d65
KB
317 if (utmpent && chdir(_PATH_DEV)) {
318 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
221be169
KM
319 exit(1);
320 }
52b4fb0c
BJ
321 we = mywd.wd_we;
322 for (i = 0; i < utmpent; i++) {
221be169 323 if (stat(we->we_utmp.out_line, &stb) >= 0)
b1456c48 324 we->we_idle = htonl(now - stb.st_atime);
52b4fb0c
BJ
325 we++;
326 }
9bd66dcf 327 (void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET);
52b4fb0c
BJ
328 (void) read(kmemf, (char *)avenrun, sizeof (avenrun));
329 for (i = 0; i < 3; i++)
353c1281 330 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
b1456c48 331 cc = (char *)we - (char *)&mywd;
b1456c48 332 mywd.wd_sendtime = htonl(time(0));
7af17b3e
SL
333 mywd.wd_vers = WHODVERSION;
334 mywd.wd_type = WHODTYPE_STATUS;
335 for (np = neighbors; np != NULL; np = np->n_next)
336 (void) sendto(s, (char *)&mywd, cc, 0,
337 np->n_addr, np->n_addrlen);
17ccb086
KB
338 if (utmpent && chdir(_PATH_RWHODIR)) {
339 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
221be169
KM
340 exit(1);
341 }
08d10f57 342done:
83361bb3 343 (void) alarm(AL_INTERVAL);
52b4fb0c
BJ
344}
345
346getkmem()
347{
07cac863
SL
348 static ino_t vmunixino;
349 static time_t vmunixctime;
350 struct stat sb;
52b4fb0c 351
17ccb086 352 if (stat(_PATH_UNIX, &sb) < 0) {
07cac863
SL
353 if (vmunixctime)
354 return;
355 } else {
356 if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
357 return;
358 vmunixctime = sb.st_ctime;
359 vmunixino= sb.st_ino;
360 }
52b4fb0c
BJ
361 if (kmemf >= 0)
362 (void) close(kmemf);
363loop:
17ccb086
KB
364 if (nlist(_PATH_UNIX, nl)) {
365 syslog(LOG_WARNING, "%s: namelist botch", _PATH_UNIX);
52b4fb0c
BJ
366 sleep(300);
367 goto loop;
368 }
17ccb086 369 kmemf = open(_PATH_KMEM, O_RDONLY, 0);
52b4fb0c 370 if (kmemf < 0) {
17ccb086 371 syslog(LOG_ERR, "%s: %m", _PATH_KMEM);
00cdc205 372 exit(1);
52b4fb0c 373 }
9bd66dcf
SL
374 (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
375 (void) read(kmemf, (char *)&mywd.wd_boottime,
376 sizeof (mywd.wd_boottime));
b1456c48 377 mywd.wd_boottime = htonl(mywd.wd_boottime);
52b4fb0c 378}
7af17b3e
SL
379
380/*
381 * Figure out device configuration and select
382 * networks which deserve status information.
383 */
384configure(s)
385 int s;
386{
387 char buf[BUFSIZ];
388 struct ifconf ifc;
389 struct ifreq ifreq, *ifr;
7af17b3e
SL
390 struct sockaddr_in *sin;
391 register struct neighbor *np;
9bd66dcf 392 int n;
7af17b3e
SL
393
394 ifc.ifc_len = sizeof (buf);
395 ifc.ifc_buf = buf;
396 if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
00cdc205 397 syslog(LOG_ERR, "ioctl (get interface configuration)");
7af17b3e
SL
398 return (0);
399 }
400 ifr = ifc.ifc_req;
401 for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
402 for (np = neighbors; np != NULL; np = np->n_next)
403 if (np->n_name &&
404 strcmp(ifr->ifr_name, np->n_name) == 0)
405 break;
406 if (np != NULL)
407 continue;
408 ifreq = *ifr;
409 np = (struct neighbor *)malloc(sizeof (*np));
410 if (np == NULL)
411 continue;
412 np->n_name = malloc(strlen(ifr->ifr_name) + 1);
413 if (np->n_name == NULL) {
414 free((char *)np);
415 continue;
416 }
417 strcpy(np->n_name, ifr->ifr_name);
418 np->n_addrlen = sizeof (ifr->ifr_addr);
419 np->n_addr = malloc(np->n_addrlen);
420 if (np->n_addr == NULL) {
421 free(np->n_name);
422 free((char *)np);
423 continue;
424 }
425 bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
426 if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
00cdc205 427 syslog(LOG_ERR, "ioctl (get interface flags)");
7af17b3e
SL
428 free((char *)np);
429 continue;
430 }
64b7ee70
MK
431 if ((ifreq.ifr_flags & IFF_UP) == 0 ||
432 (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
7af17b3e
SL
433 free((char *)np);
434 continue;
435 }
436 np->n_flags = ifreq.ifr_flags;
437 if (np->n_flags & IFF_POINTOPOINT) {
438 if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
00cdc205 439 syslog(LOG_ERR, "ioctl (get dstaddr)");
7af17b3e
SL
440 free((char *)np);
441 continue;
442 }
443 /* we assume addresses are all the same size */
444 bcopy((char *)&ifreq.ifr_dstaddr,
445 np->n_addr, np->n_addrlen);
446 }
447 if (np->n_flags & IFF_BROADCAST) {
64b7ee70
MK
448 if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
449 syslog(LOG_ERR, "ioctl (get broadaddr)");
450 free((char *)np);
451 continue;
452 }
7af17b3e 453 /* we assume addresses are all the same size */
64b7ee70
MK
454 bcopy((char *)&ifreq.ifr_broadaddr,
455 np->n_addr, np->n_addrlen);
7af17b3e
SL
456 }
457 /* gag, wish we could get rid of Internet dependencies */
458 sin = (struct sockaddr_in *)np->n_addr;
459 sin->sin_port = sp->s_port;
460 np->n_next = neighbors;
461 neighbors = np;
462 }
463 return (1);
464}
b1456c48
SL
465
466#ifdef DEBUG
467sendto(s, buf, cc, flags, to, tolen)
468 int s;
469 char *buf;
470 int cc, flags;
471 char *to;
472 int tolen;
473{
474 register struct whod *w = (struct whod *)buf;
475 register struct whoent *we;
476 struct sockaddr_in *sin = (struct sockaddr_in *)to;
477 char *interval();
478
479 printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
480 printf("hostname %s %s\n", w->wd_hostname,
9bd66dcf 481 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up"));
b1456c48 482 printf("load %4.2f, %4.2f, %4.2f\n",
9bd66dcf
SL
483 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
484 ntohl(w->wd_loadav[2]) / 100.0);
b1456c48
SL
485 cc -= WHDRSIZE;
486 for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
9bd66dcf 487 time_t t = ntohl(we->we_utmp.out_time);
b1456c48 488 printf("%-8.8s %s:%s %.12s",
9bd66dcf
SL
489 we->we_utmp.out_name,
490 w->wd_hostname, we->we_utmp.out_line,
491 ctime(&t)+4);
492 we->we_idle = ntohl(we->we_idle) / 60;
b1456c48
SL
493 if (we->we_idle) {
494 if (we->we_idle >= 100*60)
495 we->we_idle = 100*60 - 1;
496 if (we->we_idle >= 60)
497 printf(" %2d", we->we_idle / 60);
498 else
499 printf(" ");
500 printf(":%02d", we->we_idle % 60);
501 }
502 printf("\n");
503 }
504}
505
506char *
507interval(time, updown)
508 int time;
509 char *updown;
510{
511 static char resbuf[32];
512 int days, hours, minutes;
513
514 if (time < 0 || time > 3*30*24*60*60) {
515 (void) sprintf(resbuf, " %s ??:??", updown);
516 return (resbuf);
517 }
518 minutes = (time + 59) / 60; /* round to minutes */
519 hours = minutes / 60; minutes %= 60;
520 days = hours / 24; hours %= 24;
521 if (days)
522 (void) sprintf(resbuf, "%s %2d+%02d:%02d",
523 updown, days, hours, minutes);
524 else
525 (void) sprintf(resbuf, "%s %2d:%02d",
526 updown, hours, minutes);
527 return (resbuf);
528}
529#endif