rwhod.c (146940) | rwhod.c (201060) |
---|---|
1/* 2 * Copyright (c) 1983, 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 --- 26 unchanged lines hidden (view full) --- 35 36#if 0 37#ifndef lint 38static char sccsid[] = "@(#)rwhod.c 8.1 (Berkeley) 6/6/93"; 39#endif /* not lint */ 40#endif 41 42#include <sys/cdefs.h> | 1/* 2 * Copyright (c) 1983, 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 --- 26 unchanged lines hidden (view full) --- 35 36#if 0 37#ifndef lint 38static char sccsid[] = "@(#)rwhod.c 8.1 (Berkeley) 6/6/93"; 39#endif /* not lint */ 40#endif 41 42#include <sys/cdefs.h> |
43__FBSDID("$FreeBSD: head/usr.sbin/rwhod/rwhod.c 146940 2005-06-03 17:38:33Z ssouhlal $"); | 43__FBSDID("$FreeBSD: head/usr.sbin/rwhod/rwhod.c 201060 2009-12-27 21:14:55Z ed $"); |
44 45#include <sys/param.h> 46#include <sys/socket.h> 47#include <sys/stat.h> 48#include <sys/signal.h> 49#include <sys/ioctl.h> 50#include <sys/sysctl.h> 51 --- 10 unchanged lines hidden (view full) --- 62#include <fcntl.h> 63#include <netdb.h> 64#include <paths.h> 65#include <stdio.h> 66#include <stdlib.h> 67#include <string.h> 68#include <syslog.h> 69#include <timeconv.h> | 44 45#include <sys/param.h> 46#include <sys/socket.h> 47#include <sys/stat.h> 48#include <sys/signal.h> 49#include <sys/ioctl.h> 50#include <sys/sysctl.h> 51 --- 10 unchanged lines hidden (view full) --- 62#include <fcntl.h> 63#include <netdb.h> 64#include <paths.h> 65#include <stdio.h> 66#include <stdlib.h> 67#include <string.h> 68#include <syslog.h> 69#include <timeconv.h> |
70#define _ULOG_POSIX_NAMES 71#include <ulog.h> |
|
70#include <unistd.h> | 72#include <unistd.h> |
71#include <utmp.h> | |
72#include <pwd.h> 73#include <grp.h> 74 75/* 76 * This version of Berkeley's rwhod has been modified to use IP multicast 77 * datagrams, under control of a new command-line option: 78 * 79 * rwhod -m causes rwhod to use IP multicast (instead of --- 61 unchanged lines hidden (view full) --- 141 struct sockaddr *n_addr; /* who to send to */ 142 int n_addrlen; /* size of address */ 143 int n_flags; /* should forward?, interface flags */ 144}; 145 146struct neighbor *neighbors; 147struct whod mywd; 148struct servent *sp; | 73#include <pwd.h> 74#include <grp.h> 75 76/* 77 * This version of Berkeley's rwhod has been modified to use IP multicast 78 * datagrams, under control of a new command-line option: 79 * 80 * rwhod -m causes rwhod to use IP multicast (instead of --- 61 unchanged lines hidden (view full) --- 142 struct sockaddr *n_addr; /* who to send to */ 143 int n_addrlen; /* size of address */ 144 int n_flags; /* should forward?, interface flags */ 145}; 146 147struct neighbor *neighbors; 148struct whod mywd; 149struct servent *sp; |
149int s, utmpf; | 150int s; |
150 151#define WHDRSIZE (int)(sizeof(mywd) - sizeof(mywd.wd_we)) 152 153void run_as(uid_t *, gid_t *); 154int configure(int); 155void getboottime(int); 156void onalrm(int); 157void quit(const char *); --- 68 unchanged lines hidden (view full) --- 226 if (gethostname(myname, sizeof(myname) - 1) < 0) { 227 syslog(LOG_ERR, "gethostname: %m"); 228 exit(1); 229 } 230 if ((cp = index(myname, '.')) != NULL) 231 *cp = '\0'; 232 strncpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname) - 1); 233 mywd.wd_hostname[sizeof(mywd.wd_hostname) - 1] = '\0'; | 151 152#define WHDRSIZE (int)(sizeof(mywd) - sizeof(mywd.wd_we)) 153 154void run_as(uid_t *, gid_t *); 155int configure(int); 156void getboottime(int); 157void onalrm(int); 158void quit(const char *); --- 68 unchanged lines hidden (view full) --- 227 if (gethostname(myname, sizeof(myname) - 1) < 0) { 228 syslog(LOG_ERR, "gethostname: %m"); 229 exit(1); 230 } 231 if ((cp = index(myname, '.')) != NULL) 232 *cp = '\0'; 233 strncpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname) - 1); 234 mywd.wd_hostname[sizeof(mywd.wd_hostname) - 1] = '\0'; |
234 utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644); 235 if (utmpf < 0) { 236 syslog(LOG_ERR, "%s: %m", _PATH_UTMP); 237 exit(1); 238 } | |
239 getboottime(0); 240 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 241 syslog(LOG_ERR, "socket: %m"); 242 exit(1); 243 } 244 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) { 245 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 246 exit(1); --- 131 unchanged lines hidden (view full) --- 378 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 379 return (0); 380 name++, size++; 381 } 382 *name = '\0'; 383 return (size > 0); 384} 385 | 235 getboottime(0); 236 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 237 syslog(LOG_ERR, "socket: %m"); 238 exit(1); 239 } 240 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) { 241 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 242 exit(1); --- 131 unchanged lines hidden (view full) --- 374 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 375 return (0); 376 name++, size++; 377 } 378 *name = '\0'; 379 return (size > 0); 380} 381 |
386int utmptime; 387int utmpent; 388int utmpsize = 0; 389struct utmp *utmp; 390int alarmcount; 391 | |
392void | 382void |
393onalrm(signo) 394 int signo __unused; | 383onalrm(int signo __unused) |
395{ | 384{ |
396 register struct neighbor *np; 397 register struct whoent *we = mywd.wd_we, *wlast; 398 register int i; | 385 struct neighbor *np; 386 struct whoent *we = mywd.wd_we, *wend; |
399 struct stat stb; | 387 struct stat stb; |
388 struct utmpx *ut; 389 static int alarmcount = 0; |
|
400 double avenrun[3]; 401 time_t now; | 390 double avenrun[3]; 391 time_t now; |
402 int cc; | 392 int i, cc; |
403 404 now = time(NULL); 405 if (alarmcount % 10 == 0) 406 getboottime(0); 407 alarmcount++; | 393 394 now = time(NULL); 395 if (alarmcount % 10 == 0) 396 getboottime(0); 397 alarmcount++; |
408 (void) fstat(utmpf, &stb); 409 if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) { 410 utmptime = stb.st_mtime; 411 if (stb.st_size > utmpsize) { 412 utmpsize = stb.st_size + 10 * sizeof(struct utmp); 413 utmp = (struct utmp *)reallocf(utmp, utmpsize); 414 if (utmp == NULL) { 415 syslog(LOG_WARNING, "malloc failed"); 416 utmpsize = 0; 417 goto done; 418 } 419 } 420 (void) lseek(utmpf, (off_t)0, L_SET); 421 cc = read(utmpf, (char *)utmp, stb.st_size); 422 if (cc < 0) { 423 syslog(LOG_ERR, "read(%s): %m", _PATH_UTMP); 424 goto done; 425 } 426 wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1]; 427 utmpent = cc / sizeof(struct utmp); 428 for (i = 0; i < utmpent; i++) 429 if (utmp[i].ut_name[0]) { 430 memcpy(we->we_utmp.out_line, utmp[i].ut_line, 431 sizeof(utmp[i].ut_line)); 432 memcpy(we->we_utmp.out_name, utmp[i].ut_name, 433 sizeof(utmp[i].ut_name)); 434 we->we_utmp.out_time = htonl(utmp[i].ut_time); 435 if (we >= wlast) 436 break; 437 we++; 438 } 439 utmpent = we - mywd.wd_we; | 398 wend = &mywd.wd_we[1024 / sizeof(struct whoent)]; 399 setutxent(); 400 while ((ut = getutxent()) != NULL && we < wend) { 401 if (ut->ut_type != USER_PROCESS) 402 continue; 403 strncpy(we->we_utmp.out_line, ut->ut_line, 404 sizeof(we->we_utmp.out_line)); 405 strncpy(we->we_utmp.out_name, ut->ut_user, 406 sizeof(we->we_utmp.out_name)); 407 we->we_utmp.out_time = 408 htonl(_time_to_time32(ut->ut_tv.tv_sec)); 409 we++; |
440 } | 410 } |
411 endutxent(); |
|
441 | 412 |
442 /* 443 * The test on utmpent looks silly---after all, if no one is 444 * logged on, why worry about efficiency?---but is useful on 445 * (e.g.) compute servers. 446 */ 447 if (utmpent && chdir(_PATH_DEV)) { | 413 if (chdir(_PATH_DEV)) { |
448 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV); 449 exit(1); 450 } | 414 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV); 415 exit(1); 416 } |
451 we = mywd.wd_we; 452 for (i = 0; i < utmpent; i++) { | 417 wend = we; 418 for (we = mywd.wd_we; we < wend; we++) { |
453 if (stat(we->we_utmp.out_line, &stb) >= 0) 454 we->we_idle = htonl(now - stb.st_atime); 455 we++; 456 } 457 (void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 458 for (i = 0; i < 3; i++) 459 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); | 419 if (stat(we->we_utmp.out_line, &stb) >= 0) 420 we->we_idle = htonl(now - stb.st_atime); 421 we++; 422 } 423 (void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 424 for (i = 0; i < 3; i++) 425 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); |
460 cc = (char *)we - (char *)&mywd; | 426 cc = (char *)wend - (char *)&mywd; |
461 mywd.wd_sendtime = htonl(_time_to_time32(time(NULL))); 462 mywd.wd_vers = WHODVERSION; 463 mywd.wd_type = WHODTYPE_STATUS; 464 if (multicast_mode == SCOPED_MULTICAST) { 465 (void) sendto(s, (char *)&mywd, cc, 0, 466 (struct sockaddr *)&multicast_addr, 467 sizeof(multicast_addr)); 468 } --- 11 unchanged lines hidden (view full) --- 480 exit(1); 481 } 482 (void) sendto(s, (char *)&mywd, cc, 0, 483 (struct sockaddr *)&multicast_addr, 484 sizeof(multicast_addr)); 485 } else (void) sendto(s, (char *)&mywd, cc, 0, 486 np->n_addr, np->n_addrlen); 487 } | 427 mywd.wd_sendtime = htonl(_time_to_time32(time(NULL))); 428 mywd.wd_vers = WHODVERSION; 429 mywd.wd_type = WHODTYPE_STATUS; 430 if (multicast_mode == SCOPED_MULTICAST) { 431 (void) sendto(s, (char *)&mywd, cc, 0, 432 (struct sockaddr *)&multicast_addr, 433 sizeof(multicast_addr)); 434 } --- 11 unchanged lines hidden (view full) --- 446 exit(1); 447 } 448 (void) sendto(s, (char *)&mywd, cc, 0, 449 (struct sockaddr *)&multicast_addr, 450 sizeof(multicast_addr)); 451 } else (void) sendto(s, (char *)&mywd, cc, 0, 452 np->n_addr, np->n_addrlen); 453 } |
488 if (utmpent && chdir(_PATH_RWHODIR)) { | 454 if (chdir(_PATH_RWHODIR)) { |
489 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR); 490 exit(1); 491 } | 455 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR); 456 exit(1); 457 } |
492done: | |
493 (void) alarm(AL_INTERVAL); 494} 495 496void 497getboottime(signo) 498 int signo __unused; 499{ 500 int mib[2]; --- 237 unchanged lines hidden --- | 458 (void) alarm(AL_INTERVAL); 459} 460 461void 462getboottime(signo) 463 int signo __unused; 464{ 465 int mib[2]; --- 237 unchanged lines hidden --- |