Changeset View
Changeset View
Standalone View
Standalone View
head/usr.bin/w/w.c
Show First 20 Lines • Show All 129 Lines • ▼ Show 20 Lines | |||||
int | int | ||||
main(int argc, char *argv[]) | main(int argc, char *argv[]) | ||||
{ | { | ||||
struct kinfo_proc *kp; | struct kinfo_proc *kp; | ||||
struct kinfo_proc *dkp; | struct kinfo_proc *dkp; | ||||
struct stat *stp; | struct stat *stp; | ||||
time_t touched; | time_t touched; | ||||
int ch, i, nentries, nusers, wcmd, longidle, longattime, dropgid; | int ch, i, nentries, nusers, wcmd, longidle, longattime; | ||||
const char *memf, *nlistf, *p, *save_p; | const char *memf, *nlistf, *p, *save_p; | ||||
char *x_suffix; | char *x_suffix; | ||||
char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX]; | char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX]; | ||||
char fn[MAXHOSTNAMELEN]; | char fn[MAXHOSTNAMELEN]; | ||||
char *dot; | char *dot; | ||||
(void)setlocale(LC_ALL, ""); | (void)setlocale(LC_ALL, ""); | ||||
use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0'); | use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0'); | ||||
use_comma = (*nl_langinfo(RADIXCHAR) != ','); | use_comma = (*nl_langinfo(RADIXCHAR) != ','); | ||||
argc = xo_parse_args(argc, argv); | argc = xo_parse_args(argc, argv); | ||||
if (argc < 0) | if (argc < 0) | ||||
exit(1); | exit(1); | ||||
/* Are we w(1) or uptime(1)? */ | /* Are we w(1) or uptime(1)? */ | ||||
if (strcmp(basename(argv[0]), "uptime") == 0) { | if (strcmp(basename(argv[0]), "uptime") == 0) { | ||||
wcmd = 0; | wcmd = 0; | ||||
p = ""; | p = ""; | ||||
} else { | } else { | ||||
wcmd = 1; | wcmd = 1; | ||||
p = "dhiflM:N:nsuw"; | p = "dhiflM:N:nsuw"; | ||||
} | } | ||||
dropgid = 0; | |||||
memf = _PATH_DEVNULL; | memf = _PATH_DEVNULL; | ||||
nlistf = NULL; | nlistf = NULL; | ||||
while ((ch = getopt(argc, argv, p)) != -1) | while ((ch = getopt(argc, argv, p)) != -1) | ||||
switch (ch) { | switch (ch) { | ||||
case 'd': | case 'd': | ||||
dflag = 1; | dflag = 1; | ||||
break; | break; | ||||
case 'h': | case 'h': | ||||
header = 0; | header = 0; | ||||
break; | break; | ||||
case 'i': | case 'i': | ||||
sortidle = 1; | sortidle = 1; | ||||
break; | break; | ||||
case 'M': | case 'M': | ||||
header = 0; | header = 0; | ||||
memf = optarg; | memf = optarg; | ||||
dropgid = 1; | |||||
break; | break; | ||||
case 'N': | case 'N': | ||||
nlistf = optarg; | nlistf = optarg; | ||||
dropgid = 1; | |||||
break; | break; | ||||
case 'n': | case 'n': | ||||
nflag = 1; | nflag = 1; | ||||
break; | break; | ||||
case 'f': case 'l': case 's': case 'u': case 'w': | case 'f': case 'l': case 's': case 'u': case 'w': | ||||
warnx("[-flsuw] no longer supported"); | warnx("[-flsuw] no longer supported"); | ||||
/* FALLTHROUGH */ | /* FALLTHROUGH */ | ||||
case '?': | case '?': | ||||
default: | default: | ||||
usage(wcmd); | usage(wcmd); | ||||
} | } | ||||
argc -= optind; | argc -= optind; | ||||
argv += optind; | argv += optind; | ||||
if (!(_res.options & RES_INIT)) | if (!(_res.options & RES_INIT)) | ||||
res_init(); | res_init(); | ||||
_res.retrans = 2; /* resolver timeout to 2 seconds per try */ | _res.retrans = 2; /* resolver timeout to 2 seconds per try */ | ||||
_res.retry = 1; /* only try once.. */ | _res.retry = 1; /* only try once.. */ | ||||
/* | |||||
* Discard setgid privileges if not the running kernel so that bad | |||||
* guys can't print interesting stuff from kernel memory. | |||||
*/ | |||||
if (dropgid) | |||||
setgid(getgid()); | |||||
if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL) | if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL) | ||||
errx(1, "%s", errbuf); | errx(1, "%s", errbuf); | ||||
(void)time(&now); | (void)time(&now); | ||||
if (*argv) | if (*argv) | ||||
sel_users = argv; | sel_users = argv; | ||||
▲ Show 20 Lines • Show All 353 Lines • Show Last 20 Lines |