w.c revision 1.76 1 1.76 christos /* $NetBSD: w.c,v 1.76 2011/10/21 02:26:09 christos Exp $ */
2 1.18 thorpej
3 1.1 cgd /*-
4 1.11 cgd * Copyright (c) 1980, 1991, 1993, 1994
5 1.11 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.60 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.26 mrg #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.73 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\
35 1.73 lukem The Regents of the University of California. All rights reserved.");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.18 thorpej #if 0
40 1.19 tls static char sccsid[] = "@(#)w.c 8.6 (Berkeley) 6/30/94";
41 1.18 thorpej #else
42 1.76 christos __RCSID("$NetBSD: w.c,v 1.76 2011/10/21 02:26:09 christos Exp $");
43 1.18 thorpej #endif
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.1 cgd /*
47 1.1 cgd * w - print system status (who and what)
48 1.1 cgd *
49 1.1 cgd * This program is similar to the systat command on Tenex/Tops 10/20
50 1.1 cgd *
51 1.1 cgd */
52 1.1 cgd #include <sys/param.h>
53 1.42 jdolecek #include <sys/types.h>
54 1.1 cgd #include <sys/time.h>
55 1.1 cgd #include <sys/stat.h>
56 1.11 cgd #include <sys/sysctl.h>
57 1.1 cgd #include <sys/proc.h>
58 1.1 cgd #include <sys/user.h>
59 1.1 cgd #include <sys/ioctl.h>
60 1.11 cgd #include <sys/socket.h>
61 1.11 cgd
62 1.11 cgd #include <netinet/in.h>
63 1.11 cgd #include <arpa/inet.h>
64 1.11 cgd
65 1.11 cgd #include <ctype.h>
66 1.11 cgd #include <err.h>
67 1.11 cgd #include <errno.h>
68 1.11 cgd #include <fcntl.h>
69 1.11 cgd #include <kvm.h>
70 1.20 explorer #include <limits.h>
71 1.11 cgd #include <netdb.h>
72 1.1 cgd #include <nlist.h>
73 1.1 cgd #include <paths.h>
74 1.11 cgd #include <stdio.h>
75 1.11 cgd #include <stdlib.h>
76 1.1 cgd #include <string.h>
77 1.28 kleink #include <time.h>
78 1.11 cgd #include <tzfile.h>
79 1.11 cgd #include <unistd.h>
80 1.48 christos #ifdef SUPPORT_UTMP
81 1.11 cgd #include <utmp.h>
82 1.48 christos #endif
83 1.48 christos #ifdef SUPPORT_UTMPX
84 1.48 christos #include <utmpx.h>
85 1.48 christos #endif
86 1.7 cgd #include <vis.h>
87 1.1 cgd
88 1.11 cgd #include "extern.h"
89 1.1 cgd
90 1.11 cgd struct timeval boottime;
91 1.11 cgd struct winsize ws;
92 1.11 cgd kvm_t *kd;
93 1.11 cgd time_t now; /* the current time of day */
94 1.11 cgd int ttywidth; /* width of tty */
95 1.39 enami int argwidth; /* width of tty left to print process args */
96 1.11 cgd int header = 1; /* true if -h flag: don't print heading */
97 1.11 cgd int nflag; /* true if -n flag: don't convert addrs */
98 1.65 christos int wflag; /* true if -w flag: wide printout */
99 1.11 cgd int sortidle; /* sort bu idle time */
100 1.11 cgd char *sel_user; /* login of particular user selected */
101 1.29 mrg char domain[MAXHOSTNAMELEN + 1];
102 1.48 christos int maxname = 8, maxline = 3, maxhost = 16;
103 1.1 cgd
104 1.1 cgd /*
105 1.11 cgd * One of these per active utmp entry.
106 1.1 cgd */
107 1.1 cgd struct entry {
108 1.1 cgd struct entry *next;
109 1.72 christos char name[UTX_USERSIZE + 1];
110 1.72 christos char line[UTX_LINESIZE + 1];
111 1.72 christos char host[UTX_HOSTSIZE + 1];
112 1.56 christos char type[2];
113 1.48 christos struct timeval tv;
114 1.35 simonb dev_t tdev; /* dev_t of terminal */
115 1.35 simonb time_t idle; /* idle time of terminal in seconds */
116 1.58 christos struct kinfo_proc2 *tp; /* `most interesting' tty proc */
117 1.58 christos struct kinfo_proc2 *pp; /* pid proc */
118 1.55 christos pid_t pid; /* pid or ~0 if not known */
119 1.74 lukem } *ehead = NULL, **nextp = &ehead;
120 1.1 cgd
121 1.64 christos static void pr_args(struct kinfo_proc2 *);
122 1.64 christos static void pr_header(time_t *, int);
123 1.76 christos static int proc_compare_wrapper(const struct kinfo_proc2 *,
124 1.76 christos const struct kinfo_proc2 *);
125 1.48 christos #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
126 1.64 christos static int ttystat(const char *, struct stat *);
127 1.48 christos static void process(struct entry *);
128 1.48 christos #endif
129 1.75 joerg __dead static void usage(int);
130 1.1 cgd
131 1.11 cgd int
132 1.45 mjl main(int argc, char **argv)
133 1.1 cgd {
134 1.35 simonb struct kinfo_proc2 *kp;
135 1.11 cgd struct hostent *hp;
136 1.24 mycroft struct in_addr l;
137 1.74 lukem struct entry *ep;
138 1.71 rpaulo int ch, i, nentries, nusers, wcmd, curtain, use_sysctl;
139 1.71 rpaulo char *memf, *nlistf, *p, *x, *usrnp;
140 1.74 lukem const char *options;
141 1.48 christos time_t then;
142 1.71 rpaulo size_t len;
143 1.48 christos #ifdef SUPPORT_UTMP
144 1.48 christos struct utmp *ut;
145 1.48 christos #endif
146 1.48 christos #ifdef SUPPORT_UTMPX
147 1.48 christos struct utmpx *utx;
148 1.48 christos #endif
149 1.46 cgd const char *progname;
150 1.20 explorer char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
151 1.11 cgd
152 1.66 christos setprogname(argv[0]);
153 1.66 christos
154 1.11 cgd /* Are we w(1) or uptime(1)? */
155 1.46 cgd progname = getprogname();
156 1.46 cgd if (*progname == '-')
157 1.46 cgd progname++;
158 1.46 cgd if (*progname == 'u') {
159 1.1 cgd wcmd = 0;
160 1.74 lukem options = "";
161 1.11 cgd } else {
162 1.11 cgd wcmd = 1;
163 1.74 lukem options = "hiM:N:nw";
164 1.11 cgd }
165 1.1 cgd
166 1.11 cgd memf = nlistf = NULL;
167 1.74 lukem while ((ch = getopt(argc, argv, options)) != -1)
168 1.11 cgd switch (ch) {
169 1.1 cgd case 'h':
170 1.1 cgd header = 0;
171 1.1 cgd break;
172 1.1 cgd case 'i':
173 1.11 cgd sortidle = 1;
174 1.11 cgd break;
175 1.11 cgd case 'M':
176 1.11 cgd header = 0;
177 1.11 cgd memf = optarg;
178 1.11 cgd break;
179 1.11 cgd case 'N':
180 1.11 cgd nlistf = optarg;
181 1.11 cgd break;
182 1.11 cgd case 'n':
183 1.11 cgd nflag = 1;
184 1.1 cgd break;
185 1.65 christos case 'w':
186 1.65 christos wflag = 1;
187 1.65 christos break;
188 1.1 cgd case '?':
189 1.1 cgd default:
190 1.11 cgd usage(wcmd);
191 1.1 cgd }
192 1.1 cgd argc -= optind;
193 1.1 cgd argv += optind;
194 1.18 thorpej
195 1.71 rpaulo use_sysctl = (memf == NULL && nlistf == NULL);
196 1.71 rpaulo
197 1.70 elad if ((kd = kvm_openfiles(nlistf, memf, NULL,
198 1.70 elad memf == NULL ? KVM_NO_FILES : O_RDONLY, errbuf)) == NULL)
199 1.70 elad errx(1, "%s", errbuf);
200 1.30 mrg
201 1.11 cgd (void)time(&now);
202 1.51 mycroft
203 1.71 rpaulo if (use_sysctl) {
204 1.71 rpaulo len = sizeof(curtain);
205 1.71 rpaulo if (sysctlbyname("security.curtain", &curtain, &len,
206 1.71 rpaulo NULL, 0) == -1)
207 1.71 rpaulo curtain = 0;
208 1.71 rpaulo }
209 1.71 rpaulo
210 1.51 mycroft #ifdef SUPPORT_UTMPX
211 1.51 mycroft setutxent();
212 1.48 christos #endif
213 1.48 christos #ifdef SUPPORT_UTMP
214 1.51 mycroft setutent();
215 1.48 christos #endif
216 1.11 cgd
217 1.1 cgd if (*argv)
218 1.1 cgd sel_user = *argv;
219 1.1 cgd
220 1.48 christos nusers = 0;
221 1.48 christos #ifdef SUPPORT_UTMPX
222 1.48 christos while ((utx = getutxent()) != NULL) {
223 1.48 christos if (utx->ut_type != USER_PROCESS)
224 1.1 cgd continue;
225 1.11 cgd ++nusers;
226 1.52 christos if (sel_user &&
227 1.63 christos strncmp(utx->ut_name, sel_user, sizeof(utx->ut_name)) != 0)
228 1.1 cgd continue;
229 1.11 cgd if ((ep = calloc(1, sizeof(struct entry))) == NULL)
230 1.33 drochner err(1, NULL);
231 1.48 christos (void)memcpy(ep->name, utx->ut_name, sizeof(utx->ut_name));
232 1.48 christos (void)memcpy(ep->line, utx->ut_line, sizeof(utx->ut_line));
233 1.48 christos ep->name[sizeof(utx->ut_name)] = '\0';
234 1.48 christos ep->line[sizeof(utx->ut_line)] = '\0';
235 1.53 enami if (!nflag || getnameinfo((struct sockaddr *)&utx->ut_ss,
236 1.53 enami utx->ut_ss.ss_len, ep->host, sizeof(ep->host), NULL, 0,
237 1.53 enami NI_NUMERICHOST) != 0) {
238 1.53 enami (void)memcpy(ep->host, utx->ut_host,
239 1.53 enami sizeof(utx->ut_host));
240 1.53 enami ep->host[sizeof(utx->ut_host)] = '\0';
241 1.53 enami }
242 1.56 christos ep->type[0] = 'x';
243 1.48 christos ep->tv = utx->ut_tv;
244 1.55 christos ep->pid = utx->ut_pid;
245 1.1 cgd *nextp = ep;
246 1.1 cgd nextp = &(ep->next);
247 1.52 christos if (wcmd != 0)
248 1.52 christos process(ep);
249 1.48 christos }
250 1.48 christos #endif
251 1.48 christos
252 1.48 christos #ifdef SUPPORT_UTMP
253 1.48 christos while ((ut = getutent()) != NULL) {
254 1.49 christos if (ut->ut_name[0] == '\0')
255 1.49 christos continue;
256 1.52 christos
257 1.52 christos if (sel_user &&
258 1.63 christos strncmp(ut->ut_name, sel_user, sizeof(ut->ut_name)) != 0)
259 1.13 cgd continue;
260 1.48 christos
261 1.48 christos /* Don't process entries that we have utmpx for */
262 1.48 christos for (ep = ehead; ep != NULL; ep = ep->next) {
263 1.48 christos if (strncmp(ep->line, ut->ut_line,
264 1.48 christos sizeof(ut->ut_line)) == 0)
265 1.48 christos break;
266 1.42 jdolecek }
267 1.48 christos if (ep != NULL)
268 1.48 christos continue;
269 1.44 mjl
270 1.50 christos ++nusers;
271 1.48 christos if ((ep = calloc(1, sizeof(struct entry))) == NULL)
272 1.48 christos err(1, NULL);
273 1.48 christos (void)memcpy(ep->name, ut->ut_name, sizeof(ut->ut_name));
274 1.48 christos (void)memcpy(ep->line, ut->ut_line, sizeof(ut->ut_line));
275 1.48 christos (void)memcpy(ep->host, ut->ut_host, sizeof(ut->ut_host));
276 1.48 christos ep->name[sizeof(ut->ut_name)] = '\0';
277 1.48 christos ep->line[sizeof(ut->ut_line)] = '\0';
278 1.48 christos ep->host[sizeof(ut->ut_host)] = '\0';
279 1.48 christos ep->tv.tv_sec = ut->ut_time;
280 1.48 christos *nextp = ep;
281 1.48 christos nextp = &(ep->next);
282 1.52 christos if (wcmd != 0)
283 1.52 christos process(ep);
284 1.1 cgd }
285 1.48 christos #endif
286 1.48 christos
287 1.48 christos #ifdef SUPPORT_UTMPX
288 1.51 mycroft endutxent();
289 1.48 christos #endif
290 1.48 christos #ifdef SUPPORT_UTMP
291 1.51 mycroft endutent();
292 1.48 christos #endif
293 1.1 cgd
294 1.1 cgd if (header || wcmd == 0) {
295 1.11 cgd pr_header(&now, nusers);
296 1.11 cgd if (wcmd == 0)
297 1.11 cgd exit (0);
298 1.11 cgd }
299 1.1 cgd
300 1.70 elad if ((kp = kvm_getproc2(kd, KERN_PROC_ALL, 0,
301 1.70 elad sizeof(struct kinfo_proc2), &nentries)) == NULL)
302 1.70 elad errx(1, "%s", kvm_geterr(kd));
303 1.40 simonb
304 1.40 simonb /* Include trailing space because TTY header starts one column early. */
305 1.11 cgd for (i = 0; i < nentries; i++, kp++) {
306 1.1 cgd
307 1.1 cgd for (ep = ehead; ep != NULL; ep = ep->next) {
308 1.58 christos if (ep->tdev != 0 && ep->tdev == kp->p_tdev &&
309 1.58 christos kp->p__pgid == kp->p_tpgid) {
310 1.58 christos /*
311 1.58 christos * Proc is in foreground of this
312 1.58 christos * terminal
313 1.58 christos */
314 1.76 christos if (proc_compare_wrapper(ep->tp, kp))
315 1.58 christos ep->tp = kp;
316 1.58 christos break;
317 1.58 christos }
318 1.58 christos if (ep->pid != 0 && ep->pid == kp->p_pid) {
319 1.58 christos ep->pp = kp;
320 1.1 cgd break;
321 1.1 cgd }
322 1.1 cgd }
323 1.1 cgd }
324 1.37 simonb
325 1.11 cgd if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
326 1.54 enami ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
327 1.54 enami ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
328 1.47 enami ttywidth = 79;
329 1.47 enami else
330 1.47 enami ttywidth = ws.ws_col - 1;
331 1.67 kim
332 1.68 kim if (!wflag && maxhost > (ttywidth / 3))
333 1.68 kim maxhost = ttywidth / 3;
334 1.67 kim
335 1.67 kim argwidth = printf("%-*s TTY %-*s %*s IDLE WHAT\n",
336 1.67 kim maxname, "USER", maxhost, "FROM",
337 1.67 kim 7 /* "dddhhXm" */, "LOGIN@");
338 1.67 kim argwidth -= sizeof("WHAT\n") - 1 /* NUL */;
339 1.37 simonb argwidth = ttywidth - argwidth;
340 1.1 cgd if (argwidth < 4)
341 1.1 cgd argwidth = 8;
342 1.67 kim if (wflag)
343 1.67 kim argwidth = -1;
344 1.67 kim
345 1.1 cgd /* sort by idle time */
346 1.1 cgd if (sortidle && ehead != NULL) {
347 1.1 cgd struct entry *from = ehead, *save;
348 1.54 enami
349 1.1 cgd ehead = NULL;
350 1.1 cgd while (from != NULL) {
351 1.11 cgd for (nextp = &ehead;
352 1.1 cgd (*nextp) && from->idle >= (*nextp)->idle;
353 1.1 cgd nextp = &(*nextp)->next)
354 1.11 cgd continue;
355 1.1 cgd save = from;
356 1.1 cgd from = from->next;
357 1.1 cgd save->next = *nextp;
358 1.1 cgd *nextp = save;
359 1.1 cgd }
360 1.1 cgd }
361 1.48 christos #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
362 1.48 christos else if (ehead != NULL) {
363 1.48 christos struct entry *from = ehead, *save;
364 1.54 enami
365 1.48 christos ehead = NULL;
366 1.48 christos while (from != NULL) {
367 1.48 christos for (nextp = &ehead;
368 1.48 christos (*nextp) && strcmp(from->line, (*nextp)->line) > 0;
369 1.48 christos nextp = &(*nextp)->next)
370 1.48 christos continue;
371 1.48 christos save = from;
372 1.48 christos from = from->next;
373 1.48 christos save->next = *nextp;
374 1.48 christos *nextp = save;
375 1.48 christos }
376 1.48 christos }
377 1.48 christos #endif
378 1.54 enami
379 1.29 mrg if (!nflag) {
380 1.29 mrg int rv;
381 1.29 mrg
382 1.29 mrg rv = gethostname(domain, sizeof(domain));
383 1.29 mrg domain[sizeof(domain) - 1] = '\0';
384 1.29 mrg if (rv < 0 || (p = strchr(domain, '.')) == 0)
385 1.11 cgd domain[0] = '\0';
386 1.29 mrg else
387 1.11 cgd memmove(domain, p, strlen(p) + 1);
388 1.29 mrg }
389 1.11 cgd
390 1.1 cgd for (ep = ehead; ep != NULL; ep = ep->next) {
391 1.48 christos char host_buf[MAXHOSTNAMELEN + 1];
392 1.44 mjl
393 1.74 lukem strlcpy(host_buf, *ep->host ? ep->host : "-", sizeof(host_buf));
394 1.74 lukem p = host_buf;
395 1.44 mjl
396 1.48 christos for (x = p; x < p + MAXHOSTNAMELEN; x++)
397 1.22 christos if (*x == '\0' || *x == ':')
398 1.21 christos break;
399 1.48 christos if (x == p + MAXHOSTNAMELEN || *x != ':')
400 1.21 christos x = NULL;
401 1.22 christos else
402 1.22 christos *x++ = '\0';
403 1.22 christos
404 1.24 mycroft if (!nflag && inet_aton(p, &l) &&
405 1.11 cgd (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) {
406 1.11 cgd if (domain[0] != '\0') {
407 1.11 cgd p = hp->h_name;
408 1.11 cgd p += strlen(hp->h_name);
409 1.11 cgd p -= strlen(domain);
410 1.47 enami if (p > hp->h_name &&
411 1.47 enami strcasecmp(p, domain) == 0)
412 1.11 cgd *p = '\0';
413 1.11 cgd }
414 1.11 cgd p = hp->h_name;
415 1.11 cgd }
416 1.11 cgd if (x) {
417 1.44 mjl (void)snprintf(buf, sizeof(buf), "%s:%s", p, x);
418 1.11 cgd p = buf;
419 1.41 christos }
420 1.71 rpaulo
421 1.58 christos if (ep->tp != NULL)
422 1.58 christos kp = ep->tp;
423 1.58 christos else if (ep->pp != NULL)
424 1.58 christos kp = ep->pp;
425 1.72 christos else if (ep->pid != 0) {
426 1.71 rpaulo if (curtain)
427 1.71 rpaulo kp = NULL;
428 1.71 rpaulo else {
429 1.71 rpaulo warnx("Stale utmp%s entry: %s %s %s",
430 1.71 rpaulo ep->type, ep->name, ep->line, ep->host);
431 1.71 rpaulo continue;
432 1.71 rpaulo }
433 1.11 cgd }
434 1.71 rpaulo usrnp = (kp == NULL) ? ep->name : kp->p_login;
435 1.62 christos (void)printf("%-*s %-7.7s %-*.*s ",
436 1.71 rpaulo maxname, usrnp, ep->line,
437 1.67 kim maxhost, maxhost, *p ? p : "-");
438 1.48 christos then = (time_t)ep->tv.tv_sec;
439 1.48 christos pr_attime(&then, &now);
440 1.11 cgd pr_idle(ep->idle);
441 1.58 christos pr_args(kp);
442 1.37 simonb (void)printf("\n");
443 1.15 mycroft }
444 1.15 mycroft exit(0);
445 1.15 mycroft }
446 1.15 mycroft
447 1.15 mycroft static void
448 1.45 mjl pr_args(struct kinfo_proc2 *kp)
449 1.15 mycroft {
450 1.15 mycroft char **argv;
451 1.15 mycroft int left;
452 1.15 mycroft
453 1.16 mycroft if (kp == 0)
454 1.16 mycroft goto nothing;
455 1.15 mycroft left = argwidth;
456 1.70 elad argv = kvm_getargv2(kd, kp, (argwidth < 0) ? 0 : argwidth);
457 1.57 fredb if (argv == 0) {
458 1.57 fredb if (kp->p_comm == 0) {
459 1.57 fredb goto nothing;
460 1.57 fredb } else {
461 1.57 fredb fmt_putc('(', &left);
462 1.57 fredb fmt_puts((char *)kp->p_comm, &left);
463 1.57 fredb fmt_putc(')', &left);
464 1.57 fredb return;
465 1.57 fredb }
466 1.57 fredb }
467 1.70 elad while (*argv) {
468 1.70 elad fmt_puts(*argv, &left);
469 1.70 elad argv++;
470 1.70 elad fmt_putc(' ', &left);
471 1.1 cgd }
472 1.16 mycroft return;
473 1.16 mycroft nothing:
474 1.16 mycroft putchar('-');
475 1.1 cgd }
476 1.1 cgd
477 1.11 cgd static void
478 1.45 mjl pr_header(time_t *nowp, int nusers)
479 1.1 cgd {
480 1.11 cgd double avenrun[3];
481 1.11 cgd time_t uptime;
482 1.74 lukem int days, hrs, mins;
483 1.11 cgd int mib[2];
484 1.74 lukem size_t size, i;
485 1.11 cgd char buf[256];
486 1.1 cgd
487 1.11 cgd /*
488 1.11 cgd * Print time of day.
489 1.11 cgd *
490 1.11 cgd * SCCS forces the string manipulation below, as it replaces
491 1.11 cgd * %, M, and % in a character string with the file name.
492 1.11 cgd */
493 1.43 cgd (void)strftime(buf, sizeof(buf), "%l:%" "M%p", localtime(nowp));
494 1.23 mrg buf[sizeof(buf) - 1] = '\0';
495 1.11 cgd (void)printf("%s ", buf);
496 1.1 cgd
497 1.11 cgd /*
498 1.11 cgd * Print how long system has been up.
499 1.11 cgd * (Found by looking getting "boottime" from the kernel)
500 1.11 cgd */
501 1.11 cgd mib[0] = CTL_KERN;
502 1.11 cgd mib[1] = KERN_BOOTTIME;
503 1.11 cgd size = sizeof(boottime);
504 1.11 cgd if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
505 1.11 cgd boottime.tv_sec != 0) {
506 1.11 cgd uptime = now - boottime.tv_sec;
507 1.11 cgd uptime += 30;
508 1.25 phil if (uptime > SECSPERMIN) {
509 1.25 phil days = uptime / SECSPERDAY;
510 1.25 phil uptime %= SECSPERDAY;
511 1.25 phil hrs = uptime / SECSPERHOUR;
512 1.25 phil uptime %= SECSPERHOUR;
513 1.25 phil mins = uptime / SECSPERMIN;
514 1.25 phil (void)printf(" up");
515 1.25 phil if (days > 0)
516 1.25 phil (void)printf(" %d day%s,", days,
517 1.25 phil days > 1 ? "s" : "");
518 1.25 phil if (hrs > 0 && mins > 0)
519 1.25 phil (void)printf(" %2d:%02d,", hrs, mins);
520 1.25 phil else {
521 1.25 phil if (hrs > 0)
522 1.25 phil (void)printf(" %d hr%s,",
523 1.25 phil hrs, hrs > 1 ? "s" : "");
524 1.25 phil if (mins > 0)
525 1.25 phil (void)printf(" %d min%s,",
526 1.25 phil mins, mins > 1 ? "s" : "");
527 1.25 phil }
528 1.11 cgd }
529 1.11 cgd }
530 1.11 cgd
531 1.11 cgd /* Print number of users logged in to system */
532 1.14 cgd (void)printf(" %d user%s", nusers, nusers != 1 ? "s" : "");
533 1.11 cgd
534 1.11 cgd /*
535 1.11 cgd * Print 1, 5, and 15 minute load averages.
536 1.11 cgd */
537 1.11 cgd if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
538 1.11 cgd (void)printf(", no load average information available\n");
539 1.11 cgd else {
540 1.11 cgd (void)printf(", load averages:");
541 1.11 cgd for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
542 1.11 cgd if (i > 0)
543 1.11 cgd (void)printf(",");
544 1.11 cgd (void)printf(" %.2f", avenrun[i]);
545 1.11 cgd }
546 1.11 cgd (void)printf("\n");
547 1.11 cgd }
548 1.1 cgd }
549 1.1 cgd
550 1.48 christos #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
551 1.64 christos static int
552 1.64 christos ttystat(const char *line, struct stat *st)
553 1.1 cgd {
554 1.11 cgd char ttybuf[MAXPATHLEN];
555 1.1 cgd
556 1.48 christos (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
557 1.64 christos return stat(ttybuf, st);
558 1.1 cgd }
559 1.48 christos
560 1.48 christos static void
561 1.48 christos process(struct entry *ep)
562 1.48 christos {
563 1.64 christos struct stat st;
564 1.48 christos time_t touched;
565 1.48 christos int max;
566 1.48 christos
567 1.48 christos if ((max = strlen(ep->name)) > maxname)
568 1.48 christos maxname = max;
569 1.48 christos if ((max = strlen(ep->line)) > maxline)
570 1.48 christos maxline = max;
571 1.48 christos if ((max = strlen(ep->host)) > maxhost)
572 1.48 christos maxhost = max;
573 1.48 christos
574 1.64 christos ep->tdev = 0;
575 1.64 christos ep->idle = (time_t)-1;
576 1.48 christos
577 1.55 christos #ifdef SUPPORT_UTMP
578 1.48 christos /*
579 1.48 christos * Hack to recognize and correctly parse
580 1.48 christos * ut entry made by ftpd. The "tty" used
581 1.48 christos * by ftpd is not a real tty, just identifier in
582 1.64 christos * form ftpPID. Pid parsed from the "tty name"
583 1.48 christos * is used later to match corresponding process.
584 1.55 christos * NB: This is only used for utmp entries. For utmpx,
585 1.55 christos * we already have the pid.
586 1.48 christos */
587 1.55 christos if (ep->pid == 0 && strncmp(ep->line, "ftp", 3) == 0) {
588 1.55 christos ep->pid = strtol(ep->line + 3, NULL, 10);
589 1.48 christos return;
590 1.48 christos }
591 1.55 christos #endif
592 1.64 christos if (ttystat(ep->line, &st) == -1)
593 1.55 christos return;
594 1.48 christos
595 1.64 christos ep->tdev = st.st_rdev;
596 1.48 christos /*
597 1.48 christos * If this is the console device, attempt to ascertain
598 1.48 christos * the true console device dev_t.
599 1.48 christos */
600 1.48 christos if (ep->tdev == 0) {
601 1.48 christos int mib[2];
602 1.48 christos size_t size;
603 1.48 christos
604 1.48 christos mib[0] = CTL_KERN;
605 1.48 christos mib[1] = KERN_CONSDEV;
606 1.48 christos size = sizeof(dev_t);
607 1.48 christos (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
608 1.48 christos }
609 1.48 christos
610 1.64 christos touched = st.st_atime;
611 1.48 christos if (touched < ep->tv.tv_sec) {
612 1.48 christos /* tty untouched since before login */
613 1.48 christos touched = ep->tv.tv_sec;
614 1.48 christos }
615 1.48 christos if ((ep->idle = now - touched) < 0)
616 1.48 christos ep->idle = 0;
617 1.48 christos }
618 1.48 christos #endif
619 1.1 cgd
620 1.76 christos static int
621 1.76 christos proc_compare_wrapper(const struct kinfo_proc2 *p1,
622 1.76 christos const struct kinfo_proc2 *p2)
623 1.76 christos {
624 1.76 christos struct kinfo_lwp *l1, *l2;
625 1.76 christos int cnt;
626 1.76 christos
627 1.76 christos if (p1 == NULL)
628 1.76 christos return 1;
629 1.76 christos
630 1.76 christos l1 = kvm_getlwps(kd, p1->p_pid, 0, sizeof(*l1), &cnt);
631 1.76 christos if (l1 == NULL || cnt == 0)
632 1.76 christos return 1;
633 1.76 christos
634 1.76 christos l2 = kvm_getlwps(kd, p2->p_pid, 0, sizeof(*l1), &cnt);
635 1.76 christos if (l2 == NULL || cnt == 0)
636 1.76 christos return 0;
637 1.76 christos
638 1.76 christos return proc_compare(p1, l1, p2, l2);
639 1.76 christos }
640 1.76 christos
641 1.11 cgd static void
642 1.45 mjl usage(int wcmd)
643 1.1 cgd {
644 1.47 enami
645 1.11 cgd if (wcmd)
646 1.11 cgd (void)fprintf(stderr,
647 1.66 christos "Usage: %s [-hinw] [-M core] [-N system] [user]\n",
648 1.66 christos getprogname());
649 1.11 cgd else
650 1.66 christos (void)fprintf(stderr, "Usage: %s\n", getprogname());
651 1.29 mrg exit(1);
652 1.1 cgd }
653