w.c revision 1.1 1 1.1 cgd /*-
2 1.1 cgd * Copyright (c) 1980, 1991 The Regents of the University of California.
3 1.1 cgd * 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.1 cgd char copyright[] =
36 1.1 cgd "@(#) Copyright (c) 1980, 1991 The Regents of the University of California.\n\
37 1.1 cgd All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.1 cgd static char sccsid[] = "@(#)w.c 5.29 (Berkeley) 4/23/91";
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 <utmp.h>
52 1.1 cgd #include <sys/time.h>
53 1.1 cgd #include <sys/stat.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.1 cgd #include <sys/tty.h>
58 1.1 cgd #include <nlist.h>
59 1.1 cgd #include <kvm.h>
60 1.1 cgd #include <ctype.h>
61 1.1 cgd #include <paths.h>
62 1.1 cgd #include <string.h>
63 1.1 cgd #include <stdio.h>
64 1.1 cgd
65 1.1 cgd #ifdef SPPWAIT
66 1.1 cgd #define NEWVM
67 1.1 cgd #endif
68 1.1 cgd #ifndef NEWVM
69 1.1 cgd #include <machine/pte.h>
70 1.1 cgd #include <sys/vm.h>
71 1.1 cgd #endif
72 1.1 cgd
73 1.1 cgd char *program;
74 1.1 cgd int ttywidth; /* width of tty */
75 1.1 cgd int argwidth; /* width of tty */
76 1.1 cgd int header = 1; /* true if -h flag: don't print heading */
77 1.1 cgd int wcmd = 1; /* true if this is w(1), and not uptime(1) */
78 1.1 cgd int nusers; /* number of users logged in now */
79 1.1 cgd char * sel_user; /* login of particular user selected */
80 1.1 cgd time_t now; /* the current time of day */
81 1.1 cgd struct timeval boottime;
82 1.1 cgd time_t uptime; /* time of last reboot & elapsed time since */
83 1.1 cgd struct utmp utmp;
84 1.1 cgd struct winsize ws;
85 1.1 cgd int sortidle; /* sort bu idle time */
86 1.1 cgd
87 1.1 cgd
88 1.1 cgd /*
89 1.1 cgd * One of these per active utmp entry.
90 1.1 cgd */
91 1.1 cgd struct entry {
92 1.1 cgd struct entry *next;
93 1.1 cgd struct utmp utmp;
94 1.1 cgd dev_t tdev; /* dev_t of terminal */
95 1.1 cgd int idle; /* idle time of terminal in minutes */
96 1.1 cgd struct proc *proc; /* list of procs in foreground */
97 1.1 cgd char *args; /* arg list of interesting process */
98 1.1 cgd } *ep, *ehead = NULL, **nextp = &ehead;
99 1.1 cgd
100 1.1 cgd struct nlist nl[] = {
101 1.1 cgd { "_boottime" },
102 1.1 cgd #define X_BOOTTIME 0
103 1.1 cgd #if defined(hp300) || defined(i386)
104 1.1 cgd { "_cn_tty" },
105 1.1 cgd #define X_CNTTY 1
106 1.1 cgd #endif
107 1.1 cgd { "" },
108 1.1 cgd };
109 1.1 cgd
110 1.1 cgd #define USAGE "[ -hi ] [ user ]"
111 1.1 cgd #define usage() fprintf(stderr, "usage: %s: %s\n", program, USAGE)
112 1.1 cgd
113 1.1 cgd main(argc, argv)
114 1.1 cgd int argc;
115 1.1 cgd char **argv;
116 1.1 cgd {
117 1.1 cgd register int i;
118 1.1 cgd struct winsize win;
119 1.1 cgd register struct proc *p;
120 1.1 cgd struct eproc *e;
121 1.1 cgd struct stat *stp, *ttystat();
122 1.1 cgd FILE *ut;
123 1.1 cgd char *cp;
124 1.1 cgd int ch;
125 1.1 cgd extern char *optarg;
126 1.1 cgd extern int optind;
127 1.1 cgd char *attime();
128 1.1 cgd
129 1.1 cgd program = argv[0];
130 1.1 cgd /*
131 1.1 cgd * are we w(1) or uptime(1)
132 1.1 cgd */
133 1.1 cgd if ((cp = rindex(program, '/')) || *(cp = program) == '-')
134 1.1 cgd cp++;
135 1.1 cgd if (*cp == 'u')
136 1.1 cgd wcmd = 0;
137 1.1 cgd
138 1.1 cgd while ((ch = getopt(argc, argv, "hiflsuw")) != EOF)
139 1.1 cgd switch((char)ch) {
140 1.1 cgd case 'h':
141 1.1 cgd header = 0;
142 1.1 cgd break;
143 1.1 cgd case 'i':
144 1.1 cgd sortidle++;
145 1.1 cgd break;
146 1.1 cgd case 'f': case 'l': case 's': case 'u': case 'w':
147 1.1 cgd error("[-flsuw] no longer supported");
148 1.1 cgd usage();
149 1.1 cgd exit(1);
150 1.1 cgd case '?':
151 1.1 cgd default:
152 1.1 cgd usage();
153 1.1 cgd exit(1);
154 1.1 cgd }
155 1.1 cgd argc -= optind;
156 1.1 cgd argv += optind;
157 1.1 cgd
158 1.1 cgd if (*argv)
159 1.1 cgd sel_user = *argv;
160 1.1 cgd
161 1.1 cgd if (header && kvm_nlist(nl) != 0) {
162 1.1 cgd error("can't get namelist");
163 1.1 cgd exit (1);
164 1.1 cgd }
165 1.1 cgd time(&now);
166 1.1 cgd ut = fopen(_PATH_UTMP, "r");
167 1.1 cgd while (fread(&utmp, sizeof(utmp), 1, ut)) {
168 1.1 cgd if (utmp.ut_name[0] == '\0')
169 1.1 cgd continue;
170 1.1 cgd nusers++;
171 1.1 cgd if (wcmd == 0 || (sel_user &&
172 1.1 cgd strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0))
173 1.1 cgd continue;
174 1.1 cgd if ((ep = (struct entry *)
175 1.1 cgd calloc(1, sizeof (struct entry))) == NULL) {
176 1.1 cgd error("out of memory");
177 1.1 cgd exit(1);
178 1.1 cgd }
179 1.1 cgd *nextp = ep;
180 1.1 cgd nextp = &(ep->next);
181 1.1 cgd bcopy(&utmp, &(ep->utmp), sizeof (struct utmp));
182 1.1 cgd stp = ttystat(ep->utmp.ut_line);
183 1.1 cgd ep->tdev = stp->st_rdev;
184 1.1 cgd #if defined(hp300) || defined(i386)
185 1.1 cgd /*
186 1.1 cgd * XXX If this is the console device, attempt to ascertain
187 1.1 cgd * the true console device dev_t.
188 1.1 cgd */
189 1.1 cgd if (ep->tdev == 0) {
190 1.1 cgd static dev_t cn_dev;
191 1.1 cgd
192 1.1 cgd if (nl[X_CNTTY].n_value) {
193 1.1 cgd struct tty cn_tty, *cn_ttyp;
194 1.1 cgd
195 1.1 cgd if (kvm_read((void *)nl[X_CNTTY].n_value,
196 1.1 cgd &cn_ttyp, sizeof(cn_ttyp)) > 0) {
197 1.1 cgd (void)kvm_read(cn_ttyp, &cn_tty,
198 1.1 cgd sizeof (cn_tty));
199 1.1 cgd cn_dev = cn_tty.t_dev;
200 1.1 cgd }
201 1.1 cgd nl[X_CNTTY].n_value = 0;
202 1.1 cgd }
203 1.1 cgd ep->tdev = cn_dev;
204 1.1 cgd }
205 1.1 cgd #endif
206 1.1 cgd ep->idle = ((now - stp->st_atime) + 30) / 60; /* secs->mins */
207 1.1 cgd if (ep->idle < 0)
208 1.1 cgd ep->idle = 0;
209 1.1 cgd }
210 1.1 cgd fclose(ut);
211 1.1 cgd
212 1.1 cgd if (header || wcmd == 0) {
213 1.1 cgd double avenrun[3];
214 1.1 cgd int days, hrs, mins;
215 1.1 cgd
216 1.1 cgd /*
217 1.1 cgd * Print time of day
218 1.1 cgd */
219 1.1 cgd fputs(attime(&now), stdout);
220 1.1 cgd /*
221 1.1 cgd * Print how long system has been up.
222 1.1 cgd * (Found by looking for "boottime" in kernel)
223 1.1 cgd */
224 1.1 cgd (void)kvm_read((void *)nl[X_BOOTTIME].n_value, &boottime,
225 1.1 cgd sizeof (boottime));
226 1.1 cgd uptime = now - boottime.tv_sec;
227 1.1 cgd uptime += 30;
228 1.1 cgd days = uptime / (60*60*24);
229 1.1 cgd uptime %= (60*60*24);
230 1.1 cgd hrs = uptime / (60*60);
231 1.1 cgd uptime %= (60*60);
232 1.1 cgd mins = uptime / 60;
233 1.1 cgd
234 1.1 cgd printf(" up");
235 1.1 cgd if (days > 0)
236 1.1 cgd printf(" %d day%s,", days, days>1?"s":"");
237 1.1 cgd if (hrs > 0 && mins > 0) {
238 1.1 cgd printf(" %2d:%02d,", hrs, mins);
239 1.1 cgd } else {
240 1.1 cgd if (hrs > 0)
241 1.1 cgd printf(" %d hr%s,", hrs, hrs>1?"s":"");
242 1.1 cgd if (mins > 0)
243 1.1 cgd printf(" %d min%s,", mins, mins>1?"s":"");
244 1.1 cgd }
245 1.1 cgd
246 1.1 cgd /* Print number of users logged in to system */
247 1.1 cgd printf(" %d user%s", nusers, nusers>1?"s":"");
248 1.1 cgd
249 1.1 cgd /*
250 1.1 cgd * Print 1, 5, and 15 minute load averages.
251 1.1 cgd */
252 1.1 cgd printf(", load average:");
253 1.1 cgd (void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
254 1.1 cgd for (i = 0; i < (sizeof(avenrun)/sizeof(avenrun[0])); i++) {
255 1.1 cgd if (i > 0)
256 1.1 cgd printf(",");
257 1.1 cgd printf(" %.2f", avenrun[i]);
258 1.1 cgd }
259 1.1 cgd printf("\n");
260 1.1 cgd if (wcmd == 0) /* if uptime(1) then done */
261 1.1 cgd exit(0);
262 1.1 cgd #define HEADER "USER TTY FROM LOGIN@ IDLE WHAT\n"
263 1.1 cgd #define WUSED (sizeof (HEADER) - sizeof ("WHAT\n"))
264 1.1 cgd printf(HEADER);
265 1.1 cgd }
266 1.1 cgd
267 1.1 cgd while ((p = kvm_nextproc()) != NULL) {
268 1.1 cgd if (p->p_stat == SZOMB || (p->p_flag & SCTTY) == 0)
269 1.1 cgd continue;
270 1.1 cgd e = kvm_geteproc(p);
271 1.1 cgd for (ep = ehead; ep != NULL; ep = ep->next) {
272 1.1 cgd if (ep->tdev == e->e_tdev && e->e_pgid == e->e_tpgid) {
273 1.1 cgd /*
274 1.1 cgd * Proc is in foreground of this terminal
275 1.1 cgd */
276 1.1 cgd if (proc_compare(ep->proc, p))
277 1.1 cgd ep->proc = p;
278 1.1 cgd break;
279 1.1 cgd }
280 1.1 cgd }
281 1.1 cgd }
282 1.1 cgd if ((ioctl(1, TIOCGWINSZ, &ws) == -1 &&
283 1.1 cgd ioctl(2, TIOCGWINSZ, &ws) == -1 &&
284 1.1 cgd ioctl(0, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
285 1.1 cgd ttywidth = 79;
286 1.1 cgd else
287 1.1 cgd ttywidth = ws.ws_col - 1;
288 1.1 cgd argwidth = ttywidth - WUSED;
289 1.1 cgd if (argwidth < 4)
290 1.1 cgd argwidth = 8;
291 1.1 cgd for (ep = ehead; ep != NULL; ep = ep->next) {
292 1.1 cgd ep->args = strdup(kvm_getargs(ep->proc, kvm_getu(ep->proc)));
293 1.1 cgd if (ep->args == NULL) {
294 1.1 cgd error("out of memory");
295 1.1 cgd exit(1);
296 1.1 cgd }
297 1.1 cgd }
298 1.1 cgd /* sort by idle time */
299 1.1 cgd if (sortidle && ehead != NULL) {
300 1.1 cgd struct entry *from = ehead, *save;
301 1.1 cgd
302 1.1 cgd ehead = NULL;
303 1.1 cgd while (from != NULL) {
304 1.1 cgd for (nextp = &ehead;
305 1.1 cgd (*nextp) && from->idle >= (*nextp)->idle;
306 1.1 cgd nextp = &(*nextp)->next)
307 1.1 cgd ;
308 1.1 cgd save = from;
309 1.1 cgd from = from->next;
310 1.1 cgd save->next = *nextp;
311 1.1 cgd *nextp = save;
312 1.1 cgd }
313 1.1 cgd }
314 1.1 cgd
315 1.1 cgd for (ep = ehead; ep != NULL; ep = ep->next) {
316 1.1 cgd printf("%-*.*s %-2.2s %-*.*s %s",
317 1.1 cgd UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
318 1.1 cgd strncmp(ep->utmp.ut_line, "tty", 3) == 0 ?
319 1.1 cgd ep->utmp.ut_line+3 : ep->utmp.ut_line,
320 1.1 cgd UT_HOSTSIZE, UT_HOSTSIZE, *ep->utmp.ut_host ?
321 1.1 cgd ep->utmp.ut_host : "-",
322 1.1 cgd attime(&ep->utmp.ut_time));
323 1.1 cgd if (ep->idle >= 36 * 60)
324 1.1 cgd printf(" %ddays ", (ep->idle + 12 * 60) / (24 * 60));
325 1.1 cgd else
326 1.1 cgd prttime(ep->idle, " ");
327 1.1 cgd printf("%.*s\n", argwidth, ep->args);
328 1.1 cgd }
329 1.1 cgd exit(0);
330 1.1 cgd }
331 1.1 cgd
332 1.1 cgd struct stat *
333 1.1 cgd ttystat(line)
334 1.1 cgd {
335 1.1 cgd static struct stat statbuf;
336 1.1 cgd char ttybuf[sizeof (_PATH_DEV) + UT_LINESIZE + 1];
337 1.1 cgd
338 1.1 cgd sprintf(ttybuf, "%s/%.*s", _PATH_DEV, UT_LINESIZE, line);
339 1.1 cgd (void) stat(ttybuf, &statbuf);
340 1.1 cgd
341 1.1 cgd return (&statbuf);
342 1.1 cgd }
343 1.1 cgd
344 1.1 cgd /*
345 1.1 cgd * prttime prints a time in hours and minutes or minutes and seconds.
346 1.1 cgd * The character string tail is printed at the end, obvious
347 1.1 cgd * strings to pass are "", " ", or "am".
348 1.1 cgd */
349 1.1 cgd prttime(tim, tail)
350 1.1 cgd time_t tim;
351 1.1 cgd char *tail;
352 1.1 cgd {
353 1.1 cgd
354 1.1 cgd if (tim >= 60) {
355 1.1 cgd printf(" %2d:", tim/60);
356 1.1 cgd tim %= 60;
357 1.1 cgd printf("%02d", tim);
358 1.1 cgd } else if (tim >= 0)
359 1.1 cgd printf(" %2d", tim);
360 1.1 cgd printf("%s", tail);
361 1.1 cgd }
362 1.1 cgd
363 1.1 cgd #include <varargs.h>
364 1.1 cgd
365 1.1 cgd error(va_alist)
366 1.1 cgd va_dcl
367 1.1 cgd {
368 1.1 cgd char *fmt;
369 1.1 cgd va_list ap;
370 1.1 cgd
371 1.1 cgd fprintf(stderr, "%s: ", program);
372 1.1 cgd va_start(ap);
373 1.1 cgd fmt = va_arg(ap, char *);
374 1.1 cgd (void) vfprintf(stderr, fmt, ap);
375 1.1 cgd va_end(ap);
376 1.1 cgd fprintf(stderr, "\n");
377 1.1 cgd }
378