w.c revision 1.11 1 1.1 cgd /*-
2 1.11 cgd * Copyright (c) 1980, 1991, 1993, 1994
3 1.11 cgd * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.11 cgd static char copyright[] =
36 1.11 cgd "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
37 1.11 cgd The Regents of the University of California. All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.11 cgd static char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94";
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd /*
45 1.1 cgd * w - print system status (who and what)
46 1.1 cgd *
47 1.1 cgd * This program is similar to the systat command on Tenex/Tops 10/20
48 1.1 cgd *
49 1.1 cgd */
50 1.1 cgd #include <sys/param.h>
51 1.1 cgd #include <sys/time.h>
52 1.1 cgd #include <sys/stat.h>
53 1.11 cgd #include <sys/sysctl.h>
54 1.1 cgd #include <sys/proc.h>
55 1.1 cgd #include <sys/user.h>
56 1.1 cgd #include <sys/ioctl.h>
57 1.11 cgd #include <sys/socket.h>
58 1.1 cgd #include <sys/tty.h>
59 1.11 cgd
60 1.11 cgd #include <machine/cpu.h>
61 1.11 cgd #include <netinet/in.h>
62 1.11 cgd #include <arpa/inet.h>
63 1.11 cgd
64 1.11 cgd #include <ctype.h>
65 1.11 cgd #include <err.h>
66 1.11 cgd #include <errno.h>
67 1.11 cgd #include <fcntl.h>
68 1.11 cgd #include <kvm.h>
69 1.11 cgd #include <netdb.h>
70 1.1 cgd #include <nlist.h>
71 1.1 cgd #include <paths.h>
72 1.11 cgd #include <stdio.h>
73 1.11 cgd #include <stdlib.h>
74 1.1 cgd #include <string.h>
75 1.11 cgd #include <tzfile.h>
76 1.11 cgd #include <unistd.h>
77 1.11 cgd #include <utmp.h>
78 1.7 cgd #include <vis.h>
79 1.1 cgd
80 1.11 cgd #include "extern.h"
81 1.1 cgd
82 1.11 cgd struct timeval boottime;
83 1.11 cgd struct utmp utmp;
84 1.11 cgd struct winsize ws;
85 1.11 cgd kvm_t *kd;
86 1.11 cgd time_t now; /* the current time of day */
87 1.11 cgd time_t uptime; /* time of last reboot & elapsed time since */
88 1.11 cgd int ttywidth; /* width of tty */
89 1.11 cgd int argwidth; /* width of tty */
90 1.11 cgd int header = 1; /* true if -h flag: don't print heading */
91 1.11 cgd int nflag; /* true if -n flag: don't convert addrs */
92 1.11 cgd int sortidle; /* sort bu idle time */
93 1.11 cgd char *sel_user; /* login of particular user selected */
94 1.11 cgd char domain[MAXHOSTNAMELEN];
95 1.1 cgd
96 1.1 cgd /*
97 1.11 cgd * One of these per active utmp entry.
98 1.1 cgd */
99 1.1 cgd struct entry {
100 1.1 cgd struct entry *next;
101 1.1 cgd struct utmp utmp;
102 1.1 cgd dev_t tdev; /* dev_t of terminal */
103 1.11 cgd time_t idle; /* idle time of terminal in seconds */
104 1.11 cgd struct kinfo_proc *kp; /* `most interesting' proc */
105 1.1 cgd char *args; /* arg list of interesting process */
106 1.1 cgd } *ep, *ehead = NULL, **nextp = &ehead;
107 1.1 cgd
108 1.11 cgd static void pr_header __P((time_t *, int));
109 1.11 cgd static struct stat
110 1.11 cgd *ttystat __P((char *));
111 1.11 cgd static void usage __P((int));
112 1.1 cgd
113 1.11 cgd char *fmt_argv __P((char **, char *, int)); /* ../../bin/ps/fmt.c */
114 1.1 cgd
115 1.11 cgd int
116 1.1 cgd main(argc, argv)
117 1.1 cgd int argc;
118 1.1 cgd char **argv;
119 1.1 cgd {
120 1.11 cgd extern char *__progname;
121 1.11 cgd struct kinfo_proc *kp;
122 1.11 cgd struct hostent *hp;
123 1.11 cgd struct stat *stp;
124 1.1 cgd FILE *ut;
125 1.11 cgd u_long l;
126 1.11 cgd size_t arglen;
127 1.11 cgd int ch, i, nentries, nusers, wcmd;
128 1.11 cgd char *memf, *nlistf, *p, *vis_args, *x;
129 1.11 cgd char buf[MAXHOSTNAMELEN], errbuf[256];
130 1.11 cgd
131 1.11 cgd /* Are we w(1) or uptime(1)? */
132 1.11 cgd p = __progname;
133 1.11 cgd if (*p == '-')
134 1.11 cgd p++;
135 1.11 cgd if (*p == 'u') {
136 1.1 cgd wcmd = 0;
137 1.11 cgd p = "";
138 1.11 cgd } else {
139 1.11 cgd wcmd = 1;
140 1.11 cgd p = "hiflM:N:nsuw";
141 1.11 cgd }
142 1.1 cgd
143 1.11 cgd memf = nlistf = NULL;
144 1.11 cgd while ((ch = getopt(argc, argv, p)) != EOF)
145 1.11 cgd switch (ch) {
146 1.1 cgd case 'h':
147 1.1 cgd header = 0;
148 1.1 cgd break;
149 1.1 cgd case 'i':
150 1.11 cgd sortidle = 1;
151 1.11 cgd break;
152 1.11 cgd case 'M':
153 1.11 cgd header = 0;
154 1.11 cgd memf = optarg;
155 1.11 cgd break;
156 1.11 cgd case 'N':
157 1.11 cgd nlistf = optarg;
158 1.11 cgd break;
159 1.11 cgd case 'n':
160 1.11 cgd nflag = 1;
161 1.1 cgd break;
162 1.1 cgd case 'f': case 'l': case 's': case 'u': case 'w':
163 1.11 cgd warnx("[-flsuw] no longer supported");
164 1.11 cgd /* FALLTHROUGH */
165 1.1 cgd case '?':
166 1.1 cgd default:
167 1.11 cgd usage(wcmd);
168 1.1 cgd }
169 1.1 cgd argc -= optind;
170 1.1 cgd argv += optind;
171 1.1 cgd
172 1.11 cgd if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
173 1.11 cgd errx(1, "%s", errbuf);
174 1.11 cgd
175 1.11 cgd (void)time(&now);
176 1.11 cgd if ((ut = fopen(_PATH_UTMP, "r")) == NULL)
177 1.11 cgd err(1, "%s", _PATH_UTMP);
178 1.11 cgd
179 1.1 cgd if (*argv)
180 1.1 cgd sel_user = *argv;
181 1.1 cgd
182 1.11 cgd for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
183 1.1 cgd if (utmp.ut_name[0] == '\0')
184 1.1 cgd continue;
185 1.11 cgd ++nusers;
186 1.11 cgd if (wcmd == 0 || (sel_user &&
187 1.1 cgd strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0))
188 1.1 cgd continue;
189 1.11 cgd if ((ep = calloc(1, sizeof(struct entry))) == NULL)
190 1.11 cgd err(1, NULL);
191 1.1 cgd *nextp = ep;
192 1.1 cgd nextp = &(ep->next);
193 1.11 cgd memmove(&(ep->utmp), &utmp, sizeof(struct utmp));
194 1.1 cgd stp = ttystat(ep->utmp.ut_line);
195 1.1 cgd ep->tdev = stp->st_rdev;
196 1.11 cgd #ifdef CPU_CONSDEV
197 1.11 cgd /*
198 1.11 cgd * If this is the console device, attempt to ascertain
199 1.11 cgd * the true console device dev_t.
200 1.11 cgd */
201 1.11 cgd if (ep->tdev == 0) {
202 1.11 cgd int mib[2];
203 1.11 cgd size_t size;
204 1.11 cgd
205 1.11 cgd mib[0] = CTL_MACHDEP;
206 1.11 cgd mib[1] = CPU_CONSDEV;
207 1.11 cgd size = sizeof(dev_t);
208 1.11 cgd (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
209 1.11 cgd }
210 1.11 cgd #endif
211 1.11 cgd if ((ep->idle = now - stp->st_atime) < 0)
212 1.1 cgd ep->idle = 0;
213 1.1 cgd }
214 1.11 cgd (void)fclose(ut);
215 1.1 cgd
216 1.1 cgd if (header || wcmd == 0) {
217 1.11 cgd pr_header(&now, nusers);
218 1.11 cgd if (wcmd == 0)
219 1.11 cgd exit (0);
220 1.11 cgd }
221 1.1 cgd
222 1.1 cgd #define HEADER "USER TTY FROM LOGIN@ IDLE WHAT\n"
223 1.1 cgd #define WUSED (sizeof (HEADER) - sizeof ("WHAT\n"))
224 1.11 cgd (void)printf(HEADER);
225 1.11 cgd
226 1.11 cgd if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
227 1.11 cgd err(1, "%s", kvm_geterr(kd));
228 1.11 cgd for (i = 0; i < nentries; i++, kp++) {
229 1.11 cgd struct proc *p = &kp->kp_proc;
230 1.11 cgd struct eproc *e;
231 1.1 cgd
232 1.11 cgd if (p->p_stat == SIDL || p->p_stat == SZOMB)
233 1.1 cgd continue;
234 1.11 cgd e = &kp->kp_eproc;
235 1.1 cgd for (ep = ehead; ep != NULL; ep = ep->next) {
236 1.1 cgd if (ep->tdev == e->e_tdev && e->e_pgid == e->e_tpgid) {
237 1.1 cgd /*
238 1.1 cgd * Proc is in foreground of this terminal
239 1.1 cgd */
240 1.11 cgd if (proc_compare(&ep->kp->kp_proc, p))
241 1.11 cgd ep->kp = kp;
242 1.1 cgd break;
243 1.1 cgd }
244 1.1 cgd }
245 1.1 cgd }
246 1.11 cgd if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
247 1.11 cgd ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
248 1.11 cgd ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
249 1.1 cgd ttywidth = 79;
250 1.1 cgd else
251 1.1 cgd ttywidth = ws.ws_col - 1;
252 1.1 cgd argwidth = ttywidth - WUSED;
253 1.1 cgd if (argwidth < 4)
254 1.1 cgd argwidth = 8;
255 1.1 cgd for (ep = ehead; ep != NULL; ep = ep->next) {
256 1.11 cgd if (ep->kp == NULL) {
257 1.11 cgd ep->args = "-";
258 1.3 mycroft continue;
259 1.3 mycroft }
260 1.11 cgd ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth),
261 1.11 cgd ep->kp->kp_proc.p_comm, MAXCOMLEN);
262 1.11 cgd if (ep->args == NULL)
263 1.11 cgd err(1, NULL);
264 1.1 cgd }
265 1.1 cgd /* sort by idle time */
266 1.1 cgd if (sortidle && ehead != NULL) {
267 1.1 cgd struct entry *from = ehead, *save;
268 1.1 cgd
269 1.1 cgd ehead = NULL;
270 1.1 cgd while (from != NULL) {
271 1.11 cgd for (nextp = &ehead;
272 1.1 cgd (*nextp) && from->idle >= (*nextp)->idle;
273 1.1 cgd nextp = &(*nextp)->next)
274 1.11 cgd continue;
275 1.1 cgd save = from;
276 1.1 cgd from = from->next;
277 1.1 cgd save->next = *nextp;
278 1.1 cgd *nextp = save;
279 1.1 cgd }
280 1.1 cgd }
281 1.11 cgd
282 1.11 cgd if (!nflag)
283 1.11 cgd if (gethostname(domain, sizeof(domain) - 1) < 0 ||
284 1.11 cgd (p = strchr(domain, '.')) == 0)
285 1.11 cgd domain[0] = '\0';
286 1.11 cgd else {
287 1.11 cgd domain[sizeof(domain) - 1] = '\0';
288 1.11 cgd memmove(domain, p, strlen(p) + 1);
289 1.11 cgd }
290 1.11 cgd
291 1.11 cgd if ((vis_args = malloc(argwidth * 4 + 1)) == NULL)
292 1.11 cgd err(1, NULL);
293 1.1 cgd for (ep = ehead; ep != NULL; ep = ep->next) {
294 1.11 cgd p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
295 1.11 cgd if ((x = strchr(p, ':')) != NULL)
296 1.11 cgd *x++ = '\0';
297 1.11 cgd if (!nflag && isdigit(*p) &&
298 1.11 cgd (long)(l = inet_addr(p)) != -1 &&
299 1.11 cgd (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) {
300 1.11 cgd if (domain[0] != '\0') {
301 1.11 cgd p = hp->h_name;
302 1.11 cgd p += strlen(hp->h_name);
303 1.11 cgd p -= strlen(domain);
304 1.11 cgd if (p > hp->h_name && strcmp(p, domain) == 0)
305 1.11 cgd *p = '\0';
306 1.11 cgd }
307 1.11 cgd p = hp->h_name;
308 1.11 cgd }
309 1.11 cgd if (x) {
310 1.11 cgd (void)snprintf(buf, sizeof(buf), "%s:%s", p, x);
311 1.11 cgd p = buf;
312 1.11 cgd }
313 1.11 cgd (void)printf("%-*.*s %-2.2s %-*.*s ",
314 1.11 cgd UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
315 1.11 cgd strncmp(ep->utmp.ut_line, "tty", 3) ?
316 1.11 cgd ep->utmp.ut_line : ep->utmp.ut_line + 3,
317 1.11 cgd UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-");
318 1.11 cgd pr_attime(&ep->utmp.ut_time, &now);
319 1.11 cgd pr_idle(ep->idle);
320 1.11 cgd if (ep->args != NULL) {
321 1.11 cgd arglen = strlen(ep->args);
322 1.11 cgd strvisx(vis_args, ep->args,
323 1.11 cgd arglen > argwidth ? argwidth : arglen,
324 1.11 cgd VIS_TAB | VIS_NL | VIS_NOSLASH);
325 1.11 cgd }
326 1.11 cgd (void)printf("%.*s\n", argwidth, ep->args);
327 1.1 cgd }
328 1.1 cgd exit(0);
329 1.1 cgd }
330 1.1 cgd
331 1.11 cgd static void
332 1.11 cgd pr_header(nowp, nusers)
333 1.11 cgd time_t *nowp;
334 1.11 cgd int nusers;
335 1.1 cgd {
336 1.11 cgd double avenrun[3];
337 1.11 cgd time_t uptime;
338 1.11 cgd int days, hrs, i, mins;
339 1.11 cgd int mib[2];
340 1.11 cgd size_t size;
341 1.11 cgd char buf[256];
342 1.1 cgd
343 1.11 cgd /*
344 1.11 cgd * Print time of day.
345 1.11 cgd *
346 1.11 cgd * SCCS forces the string manipulation below, as it replaces
347 1.11 cgd * %, M, and % in a character string with the file name.
348 1.11 cgd */
349 1.11 cgd (void)strftime(buf, sizeof(buf),
350 1.11 cgd __CONCAT("%l:%","M%p"), localtime(nowp));
351 1.11 cgd (void)printf("%s ", buf);
352 1.1 cgd
353 1.11 cgd /*
354 1.11 cgd * Print how long system has been up.
355 1.11 cgd * (Found by looking getting "boottime" from the kernel)
356 1.11 cgd */
357 1.11 cgd mib[0] = CTL_KERN;
358 1.11 cgd mib[1] = KERN_BOOTTIME;
359 1.11 cgd size = sizeof(boottime);
360 1.11 cgd if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
361 1.11 cgd boottime.tv_sec != 0) {
362 1.11 cgd uptime = now - boottime.tv_sec;
363 1.11 cgd uptime += 30;
364 1.11 cgd days = uptime / SECSPERDAY;
365 1.11 cgd uptime %= SECSPERDAY;
366 1.11 cgd hrs = uptime / SECSPERHOUR;
367 1.11 cgd uptime %= SECSPERHOUR;
368 1.11 cgd mins = uptime / SECSPERMIN;
369 1.11 cgd (void)printf(" up");
370 1.11 cgd if (days > 0)
371 1.11 cgd (void)printf(" %d day%s,", days, days > 1 ? "s" : "");
372 1.11 cgd if (hrs > 0 && mins > 0)
373 1.11 cgd (void)printf(" %2d:%02d,", hrs, mins);
374 1.11 cgd else {
375 1.11 cgd if (hrs > 0)
376 1.11 cgd (void)printf(" %d hr%s,",
377 1.11 cgd hrs, hrs > 1 ? "s" : "");
378 1.11 cgd if (mins > 0)
379 1.11 cgd (void)printf(" %d min%s,",
380 1.11 cgd mins, mins > 1 ? "s" : "");
381 1.11 cgd }
382 1.11 cgd }
383 1.11 cgd
384 1.11 cgd /* Print number of users logged in to system */
385 1.11 cgd (void)printf(" %d user%s", nusers, nusers > 1 ? "s" : "");
386 1.11 cgd
387 1.11 cgd /*
388 1.11 cgd * Print 1, 5, and 15 minute load averages.
389 1.11 cgd */
390 1.11 cgd if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
391 1.11 cgd (void)printf(", no load average information available\n");
392 1.11 cgd else {
393 1.11 cgd (void)printf(", load averages:");
394 1.11 cgd for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
395 1.11 cgd if (i > 0)
396 1.11 cgd (void)printf(",");
397 1.11 cgd (void)printf(" %.2f", avenrun[i]);
398 1.11 cgd }
399 1.11 cgd (void)printf("\n");
400 1.11 cgd }
401 1.1 cgd }
402 1.1 cgd
403 1.11 cgd static struct stat *
404 1.11 cgd ttystat(line)
405 1.11 cgd char *line;
406 1.1 cgd {
407 1.11 cgd static struct stat sb;
408 1.11 cgd char ttybuf[MAXPATHLEN];
409 1.1 cgd
410 1.11 cgd (void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line);
411 1.11 cgd if (stat(ttybuf, &sb))
412 1.11 cgd err(1, "%s", ttybuf);
413 1.11 cgd return (&sb);
414 1.1 cgd }
415 1.1 cgd
416 1.11 cgd static void
417 1.11 cgd usage(wcmd)
418 1.11 cgd int wcmd;
419 1.1 cgd {
420 1.11 cgd if (wcmd)
421 1.11 cgd (void)fprintf(stderr,
422 1.11 cgd "usage: w: [-hin] [-M core] [-N system] [user]\n");
423 1.11 cgd else
424 1.11 cgd (void)fprintf(stderr, "uptime\n");
425 1.11 cgd exit (1);
426 1.1 cgd }
427