148327Sbostic /*-
266700Spendry * Copyright (c) 1980, 1991, 1993, 1994
362435Sbostic * The Regents of the University of California. All rights reserved.
448327Sbostic *
548581Sbostic * %sccs.include.redist.c%
621587Sdist */
721587Sdist
812666Ssam #ifndef lint
962435Sbostic static char copyright[] =
1066700Spendry "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
1162435Sbostic The Regents of the University of California. All rights reserved.\n";
1248327Sbostic #endif /* not lint */
1321587Sdist
1421587Sdist #ifndef lint
15*67462Smckusick static char sccsid[] = "@(#)w.c 8.6 (Berkeley) 06/30/94";
1648327Sbostic #endif /* not lint */
1721587Sdist
181157Sbill /*
191157Sbill * w - print system status (who and what)
201157Sbill *
211157Sbill * This program is similar to the systat command on Tenex/Tops 10/20
2240638Smarc *
231157Sbill */
241157Sbill #include <sys/param.h>
2547629Skarels #include <sys/time.h>
261157Sbill #include <sys/stat.h>
2758900Smckusick #include <sys/sysctl.h>
2847629Skarels #include <sys/proc.h>
291157Sbill #include <sys/user.h>
3030334Skarels #include <sys/ioctl.h>
3159364Sbostic #include <sys/socket.h>
3232386Smarc #include <sys/tty.h>
3359364Sbostic
3460203Smckusick #include <machine/cpu.h>
3559364Sbostic #include <netinet/in.h>
3659364Sbostic #include <arpa/inet.h>
3759364Sbostic
3853126Smckusick #include <ctype.h>
3959364Sbostic #include <err.h>
4053635Sbostic #include <errno.h>
4159364Sbostic #include <fcntl.h>
4253126Smckusick #include <kvm.h>
4359364Sbostic #include <netdb.h>
4440638Smarc #include <nlist.h>
4559364Sbostic #include <paths.h>
4653126Smckusick #include <stdio.h>
4753126Smckusick #include <stdlib.h>
4853126Smckusick #include <string.h>
4959364Sbostic #include <tzfile.h>
5059364Sbostic #include <unistd.h>
5153126Smckusick #include <utmp.h>
5266841Sbostic #include <vis.h>
531157Sbill
5459364Sbostic #include "extern.h"
5547629Skarels
5666700Spendry struct timeval boottime;
5766700Spendry struct utmp utmp;
5866700Spendry struct winsize ws;
5966700Spendry kvm_t *kd;
6066700Spendry time_t now; /* the current time of day */
6166700Spendry time_t uptime; /* time of last reboot & elapsed time since */
6266700Spendry int ttywidth; /* width of tty */
6366700Spendry int argwidth; /* width of tty */
6466700Spendry int header = 1; /* true if -h flag: don't print heading */
6566700Spendry int nflag; /* true if -n flag: don't convert addrs */
6666700Spendry int sortidle; /* sort bu idle time */
6766700Spendry char *sel_user; /* login of particular user selected */
6866700Spendry char domain[MAXHOSTNAMELEN];
691157Sbill
7040638Smarc /*
7153126Smckusick * One of these per active utmp entry.
7240638Smarc */
7340638Smarc struct entry {
7440638Smarc struct entry *next;
7540638Smarc struct utmp utmp;
7640638Smarc dev_t tdev; /* dev_t of terminal */
7759364Sbostic time_t idle; /* idle time of terminal in seconds */
7853126Smckusick struct kinfo_proc *kp; /* `most interesting' proc */
7940638Smarc char *args; /* arg list of interesting process */
8040638Smarc } *ep, *ehead = NULL, **nextp = &ehead;
8140638Smarc
8260203Smckusick static void pr_header __P((time_t *, int));
8359364Sbostic static struct stat
8459364Sbostic *ttystat __P((char *));
8559364Sbostic static void usage __P((int));
8640638Smarc
8766700Spendry char *fmt_argv __P((char **, char *, int)); /* ../../bin/ps/fmt.c */
8859376Sbostic
8959364Sbostic int
main(argc,argv)901157Sbill main(argc, argv)
9148581Sbostic int argc;
921157Sbill char **argv;
931157Sbill {
9466700Spendry extern char *__progname;
9559364Sbostic struct kinfo_proc *kp;
9659364Sbostic struct hostent *hp;
9759364Sbostic struct stat *stp;
9859364Sbostic FILE *ut;
9959364Sbostic u_long l;
10066841Sbostic size_t arglen;
10159364Sbostic int ch, i, nentries, nusers, wcmd;
10266841Sbostic char *memf, *nlistf, *p, *vis_args, *x;
10359364Sbostic char buf[MAXHOSTNAMELEN], errbuf[256];
1041157Sbill
10559364Sbostic /* Are we w(1) or uptime(1)? */
10666700Spendry p = __progname;
10766700Spendry if (*p == '-')
10859364Sbostic p++;
10959364Sbostic if (*p == 'u') {
11040638Smarc wcmd = 0;
11159364Sbostic p = "";
11259364Sbostic } else {
11359364Sbostic wcmd = 1;
11459364Sbostic p = "hiflM:N:nsuw";
11559364Sbostic }
1161157Sbill
11759364Sbostic memf = nlistf = NULL;
11859364Sbostic while ((ch = getopt(argc, argv, p)) != EOF)
11959364Sbostic switch (ch) {
12040638Smarc case 'h':
12140638Smarc header = 0;
12240638Smarc break;
12345001Smarc case 'i':
12459364Sbostic sortidle = 1;
12540638Smarc break;
12659364Sbostic case 'M':
12760203Smckusick header = 0;
12859364Sbostic memf = optarg;
12959364Sbostic break;
13059364Sbostic case 'N':
13159364Sbostic nlistf = optarg;
13259364Sbostic break;
13353126Smckusick case 'n':
13459364Sbostic nflag = 1;
13553126Smckusick break;
13640638Smarc case 'f': case 'l': case 's': case 'u': case 'w':
13759364Sbostic warnx("[-flsuw] no longer supported");
13859364Sbostic /* FALLTHROUGH */
13940638Smarc case '?':
14040638Smarc default:
14159364Sbostic usage(wcmd);
1421157Sbill }
14340638Smarc argc -= optind;
14440638Smarc argv += optind;
1451157Sbill
14659364Sbostic if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
14766700Spendry errx(1, "%s", errbuf);
14859364Sbostic
14959364Sbostic (void)time(&now);
15059364Sbostic if ((ut = fopen(_PATH_UTMP, "r")) == NULL)
15159364Sbostic err(1, "%s", _PATH_UTMP);
15259364Sbostic
15348581Sbostic if (*argv)
15448581Sbostic sel_user = *argv;
15548581Sbostic
15659364Sbostic for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
15740638Smarc if (utmp.ut_name[0] == '\0')
15840638Smarc continue;
15959364Sbostic ++nusers;
16053126Smckusick if (wcmd == 0 || (sel_user &&
16140638Smarc strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0))
16240638Smarc continue;
16359364Sbostic if ((ep = calloc(1, sizeof(struct entry))) == NULL)
16459364Sbostic err(1, NULL);
16540638Smarc *nextp = ep;
16640638Smarc nextp = &(ep->next);
16766700Spendry memmove(&(ep->utmp), &utmp, sizeof(struct utmp));
168*67462Smckusick if (!(stp = ttystat(ep->utmp.ut_line)))
169*67462Smckusick continue;
17040638Smarc ep->tdev = stp->st_rdev;
17160203Smckusick #ifdef CPU_CONSDEV
17243676Sbostic /*
17359364Sbostic * If this is the console device, attempt to ascertain
17443676Sbostic * the true console device dev_t.
17543676Sbostic */
17643676Sbostic if (ep->tdev == 0) {
17760465Storek int mib[2];
17860465Storek size_t size;
17943676Sbostic
18060203Smckusick mib[0] = CTL_MACHDEP;
18160203Smckusick mib[1] = CPU_CONSDEV;
18260203Smckusick size = sizeof(dev_t);
18360203Smckusick (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
18443676Sbostic }
18543676Sbostic #endif
18659364Sbostic if ((ep->idle = now - stp->st_atime) < 0)
18740638Smarc ep->idle = 0;
18840638Smarc }
18959364Sbostic (void)fclose(ut);
1901157Sbill
19140638Smarc if (header || wcmd == 0) {
19260203Smckusick pr_header(&now, nusers);
19359364Sbostic if (wcmd == 0)
19459364Sbostic exit (0);
19559364Sbostic }
19640638Smarc
19740638Smarc #define HEADER "USER TTY FROM LOGIN@ IDLE WHAT\n"
19840638Smarc #define WUSED (sizeof (HEADER) - sizeof ("WHAT\n"))
19959364Sbostic (void)printf(HEADER);
2001157Sbill
20158900Smckusick if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
20259364Sbostic err(1, "%s", kvm_geterr(kd));
20353126Smckusick for (i = 0; i < nentries; i++, kp++) {
20466700Spendry struct proc *p = &kp->kp_proc;
20566700Spendry struct eproc *e;
20653126Smckusick
20753126Smckusick if (p->p_stat == SIDL || p->p_stat == SZOMB)
20840638Smarc continue;
20953126Smckusick e = &kp->kp_eproc;
21040638Smarc for (ep = ehead; ep != NULL; ep = ep->next) {
21140638Smarc if (ep->tdev == e->e_tdev && e->e_pgid == e->e_tpgid) {
21240638Smarc /*
21340638Smarc * Proc is in foreground of this terminal
21440638Smarc */
21553126Smckusick if (proc_compare(&ep->kp->kp_proc, p))
21653126Smckusick ep->kp = kp;
21740638Smarc break;
2181157Sbill }
2191157Sbill }
2201157Sbill }
22159364Sbostic if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
22259364Sbostic ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
22359364Sbostic ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
22440638Smarc ttywidth = 79;
22540638Smarc else
22640638Smarc ttywidth = ws.ws_col - 1;
22740638Smarc argwidth = ttywidth - WUSED;
22840638Smarc if (argwidth < 4)
22940638Smarc argwidth = 8;
23040638Smarc for (ep = ehead; ep != NULL; ep = ep->next) {
23153126Smckusick if (ep->kp == NULL) {
23251518Sleres ep->args = "-";
23351518Sleres continue;
23451518Sleres }
23553126Smckusick ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth),
23653126Smckusick ep->kp->kp_proc.p_comm, MAXCOMLEN);
23759364Sbostic if (ep->args == NULL)
23859364Sbostic err(1, NULL);
23940638Smarc }
24040638Smarc /* sort by idle time */
24140638Smarc if (sortidle && ehead != NULL) {
24240638Smarc struct entry *from = ehead, *save;
24340638Smarc
24440638Smarc ehead = NULL;
24540638Smarc while (from != NULL) {
24653126Smckusick for (nextp = &ehead;
24740638Smarc (*nextp) && from->idle >= (*nextp)->idle;
24840638Smarc nextp = &(*nextp)->next)
24953126Smckusick continue;
25040638Smarc save = from;
25140638Smarc from = from->next;
25240638Smarc save->next = *nextp;
25340638Smarc *nextp = save;
25440638Smarc }
25540638Smarc }
25640638Smarc
25759364Sbostic if (!nflag)
25859364Sbostic if (gethostname(domain, sizeof(domain) - 1) < 0 ||
25966700Spendry (p = strchr(domain, '.')) == 0)
26059364Sbostic domain[0] = '\0';
26159364Sbostic else {
26259364Sbostic domain[sizeof(domain) - 1] = '\0';
26366700Spendry memmove(domain, p, strlen(p) + 1);
26459364Sbostic }
26559364Sbostic
26666841Sbostic if ((vis_args = malloc(argwidth * 4 + 1)) == NULL)
26766841Sbostic err(1, NULL);
26840638Smarc for (ep = ehead; ep != NULL; ep = ep->next) {
26959364Sbostic p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
27066841Sbostic if ((x = strchr(p, ':')) != NULL)
27153126Smckusick *x++ = '\0';
27259364Sbostic if (!nflag && isdigit(*p) &&
27359364Sbostic (long)(l = inet_addr(p)) != -1 &&
27459364Sbostic (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) {
27559364Sbostic if (domain[0] != '\0') {
27659364Sbostic p = hp->h_name;
27759364Sbostic p += strlen(hp->h_name);
27859364Sbostic p -= strlen(domain);
27966700Spendry if (p > hp->h_name && strcmp(p, domain) == 0)
28059364Sbostic *p = '\0';
28159364Sbostic }
28259364Sbostic p = hp->h_name;
28353126Smckusick }
28453126Smckusick if (x) {
28567461Smckusick (void)snprintf(buf, sizeof(buf), "%s:%.*s", p,
28667461Smckusick ep->utmp.ut_host + UT_HOSTSIZE - x, x);
28759364Sbostic p = buf;
28853126Smckusick }
28959364Sbostic (void)printf("%-*.*s %-2.2s %-*.*s ",
29059364Sbostic UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
29159364Sbostic strncmp(ep->utmp.ut_line, "tty", 3) ?
29259364Sbostic ep->utmp.ut_line : ep->utmp.ut_line + 3,
29359364Sbostic UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-");
29459364Sbostic pr_attime(&ep->utmp.ut_time, &now);
29559364Sbostic pr_idle(ep->idle);
29666841Sbostic if (ep->args != NULL) {
29766841Sbostic arglen = strlen(ep->args);
29866841Sbostic strvisx(vis_args, ep->args,
29966841Sbostic arglen > argwidth ? argwidth : arglen,
30066841Sbostic VIS_TAB | VIS_NL | VIS_NOSLASH);
30166841Sbostic }
30259364Sbostic (void)printf("%.*s\n", argwidth, ep->args);
30340638Smarc }
30447436Sbostic exit(0);
3051157Sbill }
3061157Sbill
30759364Sbostic static void
pr_header(nowp,nusers)30860203Smckusick pr_header(nowp, nusers)
30959364Sbostic time_t *nowp;
31059364Sbostic int nusers;
31140638Smarc {
31259364Sbostic double avenrun[3];
31359364Sbostic time_t uptime;
31459364Sbostic int days, hrs, i, mins;
31560465Storek int mib[2];
31660465Storek size_t size;
31766700Spendry char buf[256];
3181157Sbill
31959376Sbostic /*
32059376Sbostic * Print time of day.
32159376Sbostic *
32266702Sbostic * SCCS forces the string manipulation below, as it replaces
32366702Sbostic * %, M, and % in a character string with the file name.
32459376Sbostic */
32566702Sbostic (void)strftime(buf, sizeof(buf),
32666702Sbostic __CONCAT("%l:%","M%p"), localtime(nowp));
32759364Sbostic (void)printf("%s ", buf);
3281157Sbill
32959364Sbostic /*
33059364Sbostic * Print how long system has been up.
33160203Smckusick * (Found by looking getting "boottime" from the kernel)
33259364Sbostic */
33360203Smckusick mib[0] = CTL_KERN;
33460203Smckusick mib[1] = KERN_BOOTTIME;
33560203Smckusick size = sizeof(boottime);
33660430Smckusick if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
33760430Smckusick boottime.tv_sec != 0) {
33860430Smckusick uptime = now - boottime.tv_sec;
33960430Smckusick uptime += 30;
34060430Smckusick days = uptime / SECSPERDAY;
34160430Smckusick uptime %= SECSPERDAY;
34260430Smckusick hrs = uptime / SECSPERHOUR;
34360430Smckusick uptime %= SECSPERHOUR;
34460430Smckusick mins = uptime / SECSPERMIN;
34560430Smckusick (void)printf(" up");
34660430Smckusick if (days > 0)
34760430Smckusick (void)printf(" %d day%s,", days, days > 1 ? "s" : "");
34860430Smckusick if (hrs > 0 && mins > 0)
34960430Smckusick (void)printf(" %2d:%02d,", hrs, mins);
35060430Smckusick else {
35160430Smckusick if (hrs > 0)
35260430Smckusick (void)printf(" %d hr%s,",
35360430Smckusick hrs, hrs > 1 ? "s" : "");
35460430Smckusick if (mins > 0)
35560430Smckusick (void)printf(" %d min%s,",
35660430Smckusick mins, mins > 1 ? "s" : "");
35760430Smckusick }
35859364Sbostic }
35959364Sbostic
36059364Sbostic /* Print number of users logged in to system */
36159364Sbostic (void)printf(" %d user%s", nusers, nusers > 1 ? "s" : "");
36259364Sbostic
36359364Sbostic /*
36459364Sbostic * Print 1, 5, and 15 minute load averages.
36559364Sbostic */
36660203Smckusick if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
36759364Sbostic (void)printf(", no load average information available\n");
36859364Sbostic else {
36959364Sbostic (void)printf(", load averages:");
37059364Sbostic for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
37159364Sbostic if (i > 0)
37259364Sbostic (void)printf(",");
37359364Sbostic (void)printf(" %.2f", avenrun[i]);
37459364Sbostic }
37559364Sbostic (void)printf("\n");
37659364Sbostic }
3771157Sbill }
3781157Sbill
37959364Sbostic static struct stat *
ttystat(line)38059364Sbostic ttystat(line)
38159364Sbostic char *line;
3821157Sbill {
38359364Sbostic static struct stat sb;
38459364Sbostic char ttybuf[MAXPATHLEN];
3851157Sbill
38659364Sbostic (void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line);
38759364Sbostic if (stat(ttybuf, &sb))
388*67462Smckusick return (NULL);
38959364Sbostic return (&sb);
3901157Sbill }
3911157Sbill
39259364Sbostic static void
usage(wcmd)39359364Sbostic usage(wcmd)
39459364Sbostic int wcmd;
3951157Sbill {
39659364Sbostic if (wcmd)
39759364Sbostic (void)fprintf(stderr,
39859364Sbostic "usage: w: [-hin] [-M core] [-N system] [user]\n");
39959364Sbostic else
40059364Sbostic (void)fprintf(stderr, "uptime\n");
40159364Sbostic exit (1);
4021157Sbill }
403