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