]>
Commit | Line | Data |
---|---|---|
44bd5ea7 A |
1 | /*- |
2 | * Copyright (c) 1980, 1991, 1993, 1994 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #include <sys/cdefs.h> | |
9bafe280 | 35 | |
44bd5ea7 | 36 | #ifndef lint |
9bafe280 A |
37 | static const char copyright[] = |
38 | "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ | |
39 | The Regents of the University of California. All rights reserved.\n"; | |
40 | #endif | |
44bd5ea7 A |
41 | |
42 | #ifndef lint | |
9bafe280 | 43 | static const char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94"; |
44bd5ea7 | 44 | #endif |
44bd5ea7 A |
45 | |
46 | /* | |
47 | * w - print system status (who and what) | |
48 | * | |
49 | * This program is similar to the systat command on Tenex/Tops 10/20 | |
50 | * | |
51 | */ | |
52 | #include <sys/param.h> | |
53 | #include <sys/time.h> | |
54 | #include <sys/stat.h> | |
55 | #include <sys/sysctl.h> | |
56 | #include <sys/proc.h> | |
57 | #include <sys/user.h> | |
58 | #include <sys/ioctl.h> | |
59 | #include <sys/socket.h> | |
9bafe280 | 60 | #include <sys/tty.h> |
44bd5ea7 A |
61 | |
62 | #include <machine/cpu.h> | |
63 | #include <netinet/in.h> | |
64 | #include <arpa/inet.h> | |
9bafe280 | 65 | #include <arpa/nameser.h> |
44bd5ea7 A |
66 | |
67 | #include <ctype.h> | |
68 | #include <err.h> | |
69 | #include <errno.h> | |
70 | #include <fcntl.h> | |
71 | #include <kvm.h> | |
72 | #include <limits.h> | |
9bafe280 | 73 | #include <locale.h> |
44bd5ea7 A |
74 | #include <netdb.h> |
75 | #include <nlist.h> | |
76 | #include <paths.h> | |
9bafe280 | 77 | #include <resolv.h> |
44bd5ea7 A |
78 | #include <stdio.h> |
79 | #include <stdlib.h> | |
80 | #include <string.h> | |
44bd5ea7 A |
81 | #include <unistd.h> |
82 | #include <utmp.h> | |
83 | #include <vis.h> | |
84 | ||
85 | #include "extern.h" | |
86 | ||
87 | struct timeval boottime; | |
88 | struct utmp utmp; | |
89 | struct winsize ws; | |
90 | kvm_t *kd; | |
91 | time_t now; /* the current time of day */ | |
44bd5ea7 A |
92 | int ttywidth; /* width of tty */ |
93 | int argwidth; /* width of tty */ | |
94 | int header = 1; /* true if -h flag: don't print heading */ | |
95 | int nflag; /* true if -n flag: don't convert addrs */ | |
9bafe280 A |
96 | int dflag; /* true if -d flag: output debug info */ |
97 | int sortidle; /* sort by idle time */ | |
98 | int use_ampm; /* use AM/PM time */ | |
99 | int use_comma; /* use comma as floats separator */ | |
100 | char **sel_users; /* login array of particular users selected */ | |
44bd5ea7 A |
101 | |
102 | /* | |
103 | * One of these per active utmp entry. | |
104 | */ | |
105 | struct entry { | |
106 | struct entry *next; | |
107 | struct utmp utmp; | |
9bafe280 A |
108 | dev_t tdev; /* dev_t of terminal */ |
109 | time_t idle; /* idle time of terminal in seconds */ | |
110 | struct kinfo_proc *kp; /* `most interesting' proc */ | |
111 | char *args; /* arg list of interesting process */ | |
112 | struct kinfo_proc *dkp; /* debug option proc list */ | |
44bd5ea7 A |
113 | } *ep, *ehead = NULL, **nextp = &ehead; |
114 | ||
9bafe280 A |
115 | #define debugproc(p) *((struct kinfo_proc **)&(p)->ki_spare[0]) |
116 | ||
117 | /* W_DISPHOSTSIZE should not be greater than UT_HOSTSIZE */ | |
118 | #define W_DISPHOSTSIZE 16 | |
119 | ||
120 | static void pr_header(time_t *, int); | |
121 | static struct stat *ttystat(char *, int); | |
122 | static void usage(int); | |
123 | static int this_is_uptime(const char *s); | |
124 | static void w_getargv(void); | |
125 | ||
126 | char *fmt_argv(char **, char *, int); /* ../../bin/ps/fmt.c */ | |
44bd5ea7 A |
127 | |
128 | int | |
129 | main(argc, argv) | |
130 | int argc; | |
131 | char **argv; | |
132 | { | |
9bafe280 A |
133 | struct kinfo_proc *kp, *kprocbuf; |
134 | struct kinfo_proc *dkp; | |
44bd5ea7 A |
135 | struct stat *stp; |
136 | FILE *ut; | |
9bafe280 A |
137 | time_t touched; |
138 | int ch, i, nentries, nusers, wcmd, longidle, dropgid; | |
139 | const char *memf, *nlistf, *p; | |
140 | char *x_suffix; | |
44bd5ea7 | 141 | char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX]; |
9bafe280 A |
142 | char fn[MAXHOSTNAMELEN]; |
143 | char *dot; | |
144 | int local_error = 0, retry_count = 0; | |
145 | size_t bufSize = 0; | |
146 | size_t orig_bufSize = 0; | |
147 | int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; | |
148 | ||
149 | (void)setlocale(LC_ALL, ""); | |
150 | /* | |
151 | use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0'); | |
152 | use_comma = (*nl_langinfo(RADIXCHAR) != ','); | |
153 | */ | |
44bd5ea7 A |
154 | |
155 | /* Are we w(1) or uptime(1)? */ | |
9bafe280 | 156 | if (this_is_uptime(argv[0]) == 0) { |
44bd5ea7 A |
157 | wcmd = 0; |
158 | p = ""; | |
159 | } else { | |
160 | wcmd = 1; | |
9bafe280 | 161 | p = "dhiflM:N:nsuw"; |
44bd5ea7 A |
162 | } |
163 | ||
9bafe280 | 164 | dropgid = 0; |
44bd5ea7 A |
165 | memf = nlistf = NULL; |
166 | while ((ch = getopt(argc, argv, p)) != -1) | |
167 | switch (ch) { | |
9bafe280 A |
168 | case 'd': |
169 | dflag = 1; | |
170 | break; | |
44bd5ea7 A |
171 | case 'h': |
172 | header = 0; | |
173 | break; | |
174 | case 'i': | |
175 | sortidle = 1; | |
176 | break; | |
177 | case 'M': | |
178 | header = 0; | |
179 | memf = optarg; | |
9bafe280 | 180 | dropgid = 1; |
44bd5ea7 A |
181 | break; |
182 | case 'N': | |
183 | nlistf = optarg; | |
9bafe280 | 184 | dropgid = 1; |
44bd5ea7 A |
185 | break; |
186 | case 'n': | |
187 | nflag = 1; | |
188 | break; | |
189 | case 'f': case 'l': case 's': case 'u': case 'w': | |
190 | warnx("[-flsuw] no longer supported"); | |
191 | /* FALLTHROUGH */ | |
192 | case '?': | |
193 | default: | |
194 | usage(wcmd); | |
195 | } | |
196 | argc -= optind; | |
197 | argv += optind; | |
198 | ||
9bafe280 A |
199 | if (!(_res.options & RES_INIT)) |
200 | res_init(); | |
201 | _res.retrans = 2; /* resolver timeout to 2 seconds per try */ | |
202 | _res.retry = 1; /* only try once.. */ | |
203 | ||
44bd5ea7 | 204 | /* |
9bafe280 A |
205 | * Discard setgid privileges if not the running kernel so that bad |
206 | * guys can't print interesting stuff from kernel memory. | |
44bd5ea7 | 207 | */ |
9bafe280 A |
208 | if (dropgid) |
209 | setgid(getgid()); | |
44bd5ea7 | 210 | |
9bafe280 | 211 | #ifdef OLD_PROC |
c0fcf4e1 | 212 | if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL) |
44bd5ea7 | 213 | errx(1, "%s", errbuf); |
9bafe280 | 214 | #endif |
44bd5ea7 A |
215 | |
216 | (void)time(&now); | |
217 | if ((ut = fopen(_PATH_UTMP, "r")) == NULL) | |
218 | err(1, "%s", _PATH_UTMP); | |
219 | ||
220 | if (*argv) | |
9bafe280 | 221 | sel_users = argv; |
44bd5ea7 A |
222 | |
223 | for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) { | |
224 | if (utmp.ut_name[0] == '\0') | |
225 | continue; | |
9bafe280 A |
226 | if (!(stp = ttystat(utmp.ut_line, UT_LINESIZE))) |
227 | continue; /* corrupted record */ | |
44bd5ea7 | 228 | ++nusers; |
9bafe280 | 229 | if (wcmd == 0) |
44bd5ea7 | 230 | continue; |
9bafe280 A |
231 | if (sel_users) { |
232 | int usermatch; | |
233 | char **user; | |
234 | ||
235 | usermatch = 0; | |
236 | for (user = sel_users; !usermatch && *user; user++) | |
237 | if (!strncmp(utmp.ut_name, *user, UT_NAMESIZE)) | |
238 | usermatch = 1; | |
239 | if (!usermatch) | |
240 | continue; | |
241 | } | |
44bd5ea7 | 242 | if ((ep = calloc(1, sizeof(struct entry))) == NULL) |
9bafe280 | 243 | errx(1, "calloc"); |
44bd5ea7 | 244 | *nextp = ep; |
9bafe280 A |
245 | nextp = &ep->next; |
246 | memmove(&ep->utmp, &utmp, sizeof(struct utmp)); | |
44bd5ea7 A |
247 | ep->tdev = stp->st_rdev; |
248 | #ifdef CPU_CONSDEV | |
249 | /* | |
250 | * If this is the console device, attempt to ascertain | |
251 | * the true console device dev_t. | |
252 | */ | |
253 | if (ep->tdev == 0) { | |
254 | int mib[2]; | |
255 | size_t size; | |
256 | ||
257 | mib[0] = CTL_MACHDEP; | |
258 | mib[1] = CPU_CONSDEV; | |
259 | size = sizeof(dev_t); | |
9bafe280 | 260 | (void)sysctl(mib, 2, &ep->tdev, &size, NULL, 0); |
44bd5ea7 A |
261 | } |
262 | #endif | |
9bafe280 A |
263 | touched = stp->st_atime; |
264 | if (touched < ep->utmp.ut_time) { | |
265 | /* tty untouched since before login */ | |
266 | touched = ep->utmp.ut_time; | |
267 | } | |
268 | if ((ep->idle = now - touched) < 0) | |
44bd5ea7 A |
269 | ep->idle = 0; |
270 | } | |
271 | (void)fclose(ut); | |
272 | ||
273 | if (header || wcmd == 0) { | |
274 | pr_header(&now, nusers); | |
9bafe280 A |
275 | if (wcmd == 0) { |
276 | #ifdef OLD_PROC | |
277 | (void)kvm_close(kd); | |
278 | #endif | |
279 | exit(0); | |
280 | } | |
44bd5ea7 | 281 | |
9bafe280 A |
282 | #define HEADER_USER "USER" |
283 | #define HEADER_TTY "TTY" | |
284 | #define HEADER_FROM "FROM" | |
285 | #define HEADER_LOGIN_IDLE "LOGIN@ IDLE " | |
286 | #define HEADER_WHAT "WHAT\n" | |
287 | #define WUSED (UT_NAMESIZE + UT_LINESIZE + W_DISPHOSTSIZE + \ | |
288 | sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */ | |
289 | (void)printf("%-*.*s %-*.*s %-*.*s %s", | |
290 | UT_NAMESIZE, UT_NAMESIZE, HEADER_USER, | |
291 | UT_LINESIZE, UT_LINESIZE, HEADER_TTY, | |
292 | W_DISPHOSTSIZE, W_DISPHOSTSIZE, HEADER_FROM, | |
293 | HEADER_LOGIN_IDLE HEADER_WHAT); | |
294 | } | |
44bd5ea7 | 295 | |
9bafe280 | 296 | #ifdef OLD_PROC |
44bd5ea7 | 297 | if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL) |
9bafe280 | 298 | err(1, "%s", kvm_geterr(kd)); |
44bd5ea7 | 299 | #else |
9bafe280 A |
300 | mib[0] = CTL_KERN; |
301 | mib[1] = KERN_PROC; | |
302 | mib[2] = KERN_PROC_ALL; | |
303 | mib[3] = 0; | |
304 | ||
305 | if (sysctl(mib, 4, NULL, &bufSize, NULL, 0) < 0) { | |
306 | perror("Failure calling sysctl"); | |
307 | exit(1); | |
308 | } | |
44bd5ea7 | 309 | |
9bafe280 A |
310 | kprocbuf = kp = (struct kinfo_proc *)malloc(bufSize); |
311 | ||
312 | retry_count = 0; | |
313 | orig_bufSize = bufSize; | |
314 | for (retry_count = 0; ; retry_count++) { | |
315 | local_error = 0; | |
316 | bufSize = orig_bufSize; | |
317 | if ((local_error = sysctl(mib, 4, kp, &bufSize, NULL, 0)) < 0) { | |
318 | if (retry_count < 1000) { | |
319 | sleep(1); | |
320 | continue; | |
321 | } | |
322 | perror("Failure calling sysctl"); | |
323 | exit(1); | |
324 | } else if (local_error == 0) { | |
325 | break; | |
326 | } | |
327 | sleep(1); | |
328 | } | |
329 | nentries = bufSize / sizeof(struct kinfo_proc); | |
330 | #endif | |
331 | for (i = 0; i < nentries; i++, kp++) { | |
332 | if (kp->kp_proc.p_stat == SIDL || kp->kp_proc.p_stat == SZOMB) | |
44bd5ea7 | 333 | continue; |
44bd5ea7 | 334 | for (ep = ehead; ep != NULL; ep = ep->next) { |
9bafe280 | 335 | if (ep->tdev == kp->kp_eproc.e_tdev) { |
44bd5ea7 | 336 | /* |
9bafe280 | 337 | * proc is associated with this terminal |
44bd5ea7 | 338 | */ |
9bafe280 A |
339 | if (ep->kp == NULL && kp->kp_eproc.e_pgid == kp->kp_eproc.e_tpgid) { |
340 | /* | |
341 | * Proc is 'most interesting' | |
342 | */ | |
343 | if (proc_compare(&ep->kp->kp_proc, &kp->kp_proc)) | |
344 | ep->kp = kp; | |
345 | } | |
346 | /* | |
347 | * Proc debug option info; add to debug | |
348 | * list using kinfo_proc ki_spare[0] | |
349 | * as next pointer; ptr to ptr avoids the | |
350 | * ptr = long assumption. | |
351 | */ | |
352 | dkp = ep->dkp; | |
353 | ep->dkp = kp; | |
354 | /* --bbraun | |
355 | debugproc(kp) = dkp; | |
356 | */ | |
44bd5ea7 A |
357 | } |
358 | } | |
359 | } | |
360 | if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 && | |
361 | ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 && | |
362 | ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0) | |
363 | ttywidth = 79; | |
364 | else | |
365 | ttywidth = ws.ws_col - 1; | |
366 | argwidth = ttywidth - WUSED; | |
367 | if (argwidth < 4) | |
368 | argwidth = 8; | |
9bafe280 A |
369 | for (ep = ehead; ep != NULL; ep = ep->next) { |
370 | if (ep->kp == NULL) { | |
371 | ep->args = strdup("-"); | |
372 | continue; | |
373 | } | |
374 | #ifdef OLD_PROC | |
375 | ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth), | |
376 | ep->kp->kp_proc.p_comm, MAXCOMLEN); | |
377 | #else | |
378 | w_getargv(); | |
379 | #endif | |
380 | if (ep->args == NULL) | |
381 | err(1, NULL); | |
382 | } | |
44bd5ea7 A |
383 | /* sort by idle time */ |
384 | if (sortidle && ehead != NULL) { | |
9bafe280 A |
385 | struct entry *from, *save; |
386 | ||
387 | from = ehead; | |
44bd5ea7 A |
388 | ehead = NULL; |
389 | while (from != NULL) { | |
390 | for (nextp = &ehead; | |
391 | (*nextp) && from->idle >= (*nextp)->idle; | |
392 | nextp = &(*nextp)->next) | |
393 | continue; | |
394 | save = from; | |
395 | from = from->next; | |
396 | save->next = *nextp; | |
397 | *nextp = save; | |
398 | } | |
399 | } | |
44bd5ea7 A |
400 | |
401 | for (ep = ehead; ep != NULL; ep = ep->next) { | |
9bafe280 A |
402 | char host_buf[UT_HOSTSIZE + 1]; |
403 | struct sockaddr_storage ss; | |
404 | struct sockaddr *sa = (struct sockaddr *)&ss; | |
405 | struct sockaddr_in *lsin = (struct sockaddr_in *)&ss; | |
406 | #ifdef SUCKAGE | |
407 | struct hostent *hp; | |
408 | #else | |
409 | struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)&ss; | |
410 | #endif | |
411 | int isaddr; | |
412 | ||
413 | host_buf[UT_HOSTSIZE] = '\0'; | |
414 | strncpy(host_buf, ep->utmp.ut_host, UT_HOSTSIZE); | |
415 | p = *host_buf ? host_buf : "-"; | |
416 | if ((x_suffix = strrchr(p, ':')) != NULL) { | |
417 | if ((dot = strchr(x_suffix, '.')) != NULL && | |
418 | strchr(dot+1, '.') == NULL) | |
419 | *x_suffix++ = '\0'; | |
420 | else | |
421 | x_suffix = NULL; | |
422 | } | |
423 | if (!nflag) { | |
424 | /* Attempt to change an IP address into a name */ | |
425 | isaddr = 0; | |
426 | memset(&ss, '\0', sizeof(ss)); | |
427 | #ifdef SUCKAGE | |
428 | if (inet_aton(p, &lsin->sin_addr) ) { | |
429 | lsin->sin_len = sizeof(*lsin); | |
430 | lsin->sin_family = AF_INET; | |
431 | isaddr = 1; | |
44bd5ea7 | 432 | } |
9bafe280 A |
433 | |
434 | hp = gethostbyaddr((char *)&lsin->sin_addr, sizeof(lsin->sin_addr), AF_INET); | |
435 | if( hp ) { | |
436 | p = hp->h_name; | |
437 | } | |
438 | #else | |
439 | if (inet_pton(AF_INET6, p, &lsin6->sin6_addr) == 1) { | |
440 | lsin6->sin6_len = sizeof(*lsin6); | |
441 | lsin6->sin6_family = AF_INET6; | |
442 | isaddr = 1; | |
443 | } else if (inet_pton(AF_INET, p, &lsin->sin_addr) == 1) { | |
444 | lsin->sin_len = sizeof(*lsin); | |
445 | lsin->sin_family = AF_INET; | |
446 | isaddr = 1; | |
447 | } | |
448 | if (isaddr && realhostname_sa(fn, sizeof(fn), sa, | |
449 | sa->sa_len) == HOSTNAME_FOUND) | |
450 | p = fn; | |
451 | #endif | |
44bd5ea7 | 452 | } |
9bafe280 A |
453 | if (x_suffix) { |
454 | (void)snprintf(buf, sizeof(buf), "%s:%s", p, x_suffix); | |
44bd5ea7 A |
455 | p = buf; |
456 | } | |
9bafe280 A |
457 | #ifndef SUCKAGE |
458 | if (dflag) { | |
459 | for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) { | |
460 | const char *ptr; | |
461 | ||
462 | ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth), | |
463 | dkp->ki_comm, MAXCOMLEN); | |
464 | if (ptr == NULL) | |
465 | ptr = "-"; | |
466 | (void)printf("\t\t%-9d %s\n", | |
467 | dkp->ki_pid, ptr); | |
468 | } | |
469 | } | |
470 | #endif | |
471 | (void)printf("%-*.*s %-*.*s %-*.*s ", | |
44bd5ea7 | 472 | UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name, |
9bafe280 A |
473 | UT_LINESIZE, UT_LINESIZE, |
474 | strncmp(ep->utmp.ut_line, "tty", 3) && | |
475 | strncmp(ep->utmp.ut_line, "cua", 3) ? | |
44bd5ea7 | 476 | ep->utmp.ut_line : ep->utmp.ut_line + 3, |
9bafe280 | 477 | W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-"); |
44bd5ea7 | 478 | pr_attime(&ep->utmp.ut_time, &now); |
9bafe280 A |
479 | longidle = pr_idle(ep->idle); |
480 | (void)printf("%.*s\n", argwidth - longidle, ep->args); | |
481 | #ifndef OLD_PROC | |
482 | free(ep->args); | |
483 | #endif | |
44bd5ea7 | 484 | } |
9bafe280 A |
485 | #ifdef OLD_PROC |
486 | (void)kvm_close(kd); | |
487 | #else | |
488 | free(kprocbuf); | |
489 | #endif | |
44bd5ea7 A |
490 | exit(0); |
491 | } | |
492 | ||
44bd5ea7 A |
493 | static void |
494 | pr_header(nowp, nusers) | |
495 | time_t *nowp; | |
496 | int nusers; | |
497 | { | |
498 | double avenrun[3]; | |
499 | time_t uptime; | |
9bafe280 | 500 | int days, hrs, i, mins, secs; |
44bd5ea7 A |
501 | int mib[2]; |
502 | size_t size; | |
503 | char buf[256]; | |
504 | ||
505 | /* | |
506 | * Print time of day. | |
44bd5ea7 | 507 | */ |
9bafe280 A |
508 | (void)strftime(buf, sizeof(buf) - 1, |
509 | use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp)); | |
44bd5ea7 A |
510 | buf[sizeof(buf) - 1] = '\0'; |
511 | (void)printf("%s ", buf); | |
512 | ||
513 | /* | |
514 | * Print how long system has been up. | |
515 | * (Found by looking getting "boottime" from the kernel) | |
516 | */ | |
517 | mib[0] = CTL_KERN; | |
518 | mib[1] = KERN_BOOTTIME; | |
519 | size = sizeof(boottime); | |
520 | if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && | |
521 | boottime.tv_sec != 0) { | |
522 | uptime = now - boottime.tv_sec; | |
9bafe280 A |
523 | if (uptime > 60) |
524 | uptime += 30; | |
525 | days = uptime / 86400; | |
526 | uptime %= 86400; | |
527 | hrs = uptime / 3600; | |
528 | uptime %= 3600; | |
529 | mins = uptime / 60; | |
530 | secs = uptime % 60; | |
531 | (void)printf(" up"); | |
532 | if (days > 0) | |
533 | (void)printf(" %d day%s,", days, days > 1 ? "s" : ""); | |
534 | if (hrs > 0 && mins > 0) | |
535 | (void)printf(" %2d:%02d,", hrs, mins); | |
536 | else if (hrs > 0) | |
537 | (void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : ""); | |
538 | else if (mins > 0) | |
539 | (void)printf(" %d min%s,", mins, mins > 1 ? "s" : ""); | |
540 | else | |
541 | (void)printf(" %d sec%s,", secs, secs > 1 ? "s" : ""); | |
44bd5ea7 A |
542 | } |
543 | ||
544 | /* Print number of users logged in to system */ | |
9bafe280 | 545 | (void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s"); |
44bd5ea7 A |
546 | |
547 | /* | |
548 | * Print 1, 5, and 15 minute load averages. | |
549 | */ | |
550 | if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1) | |
551 | (void)printf(", no load average information available\n"); | |
552 | else { | |
553 | (void)printf(", load averages:"); | |
9bafe280 A |
554 | for (i = 0; i < (int)(sizeof(avenrun) / sizeof(avenrun[0])); i++) { |
555 | if (use_comma && i > 0) | |
44bd5ea7 A |
556 | (void)printf(","); |
557 | (void)printf(" %.2f", avenrun[i]); | |
558 | } | |
559 | (void)printf("\n"); | |
560 | } | |
561 | } | |
562 | ||
563 | static struct stat * | |
9bafe280 | 564 | ttystat(line, sz) |
44bd5ea7 | 565 | char *line; |
9bafe280 | 566 | int sz; |
44bd5ea7 A |
567 | { |
568 | static struct stat sb; | |
569 | char ttybuf[MAXPATHLEN]; | |
570 | ||
9bafe280 A |
571 | (void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, sz, line); |
572 | if (stat(ttybuf, &sb)) { | |
573 | warn("%s", ttybuf); | |
44bd5ea7 | 574 | return (NULL); |
9bafe280 | 575 | } |
44bd5ea7 A |
576 | return (&sb); |
577 | } | |
578 | ||
579 | static void | |
580 | usage(wcmd) | |
581 | int wcmd; | |
582 | { | |
583 | if (wcmd) | |
584 | (void)fprintf(stderr, | |
9bafe280 | 585 | "usage: w [-dhin] [-M core] [-N system] [user ...]\n"); |
44bd5ea7 | 586 | else |
9bafe280 | 587 | (void)fprintf(stderr, "usage: uptime\n"); |
44bd5ea7 A |
588 | exit(1); |
589 | } | |
9bafe280 A |
590 | |
591 | static int | |
592 | this_is_uptime(s) | |
593 | const char *s; | |
594 | { | |
595 | const char *u; | |
596 | ||
597 | if ((u = strrchr(s, '/')) != NULL) | |
598 | ++u; | |
599 | else | |
600 | u = s; | |
601 | if (strcmp(u, "uptime") == 0) | |
602 | return (0); | |
603 | return (-1); | |
604 | } | |
605 | ||
606 | static void | |
607 | w_getargv(void) | |
608 | { | |
609 | int mib[3], argmax; | |
610 | size_t size; | |
611 | char *procargs, *sp, *np, *cp; | |
612 | ||
613 | mib[0] = CTL_KERN; | |
614 | mib[1] = KERN_ARGMAX; | |
615 | ||
616 | size = sizeof(argmax); | |
617 | if (sysctl(mib, 2, &argmax, &size, NULL, 0) == -1) { | |
618 | goto ERROR; | |
619 | } | |
620 | ||
621 | procargs = malloc(argmax); | |
622 | if (procargs == NULL) { | |
623 | goto ERROR; | |
624 | } | |
625 | ||
626 | mib[0] = CTL_KERN; | |
627 | mib[1] = KERN_PROCARGS; | |
628 | mib[2] = KI_PROC(ep)->p_pid; | |
629 | ||
630 | size = (size_t)argmax; | |
631 | if (sysctl(mib, 3, procargs, &size, NULL, 0) == -1) { | |
632 | goto ERROR_FREE; | |
633 | } | |
634 | ||
635 | for (cp = procargs; cp < &procargs[size]; cp++) { | |
636 | if (*cp == '\0') { | |
637 | break; | |
638 | } | |
639 | } | |
640 | if (cp == &procargs[size]) { | |
641 | goto ERROR_FREE; | |
642 | } | |
643 | ||
644 | sp = cp; | |
645 | ||
646 | for (np = NULL; cp < &procargs[size]; cp++) { | |
647 | if (*cp == '\0') { | |
648 | if (np != NULL) { | |
649 | *np = ' '; | |
650 | } | |
651 | np = cp; | |
652 | } else if (*cp == '=') { | |
653 | break; | |
654 | } | |
655 | } | |
656 | ||
657 | for (np = sp; (np < &procargs[size]) && (*np == ' '); np++); | |
658 | ||
659 | ep->args = strdup(np); | |
660 | free(procargs); | |
661 | return; | |
662 | ||
663 | ERROR_FREE: | |
664 | free(procargs); | |
665 | ERROR: | |
666 | /* | |
667 | ep->args = malloc(2); | |
668 | ep->args[0] = '-'; | |
669 | ep->args[1] = '\0'; | |
670 | */ | |
671 | asprintf(&ep->args, "%s", KI_PROC(ep)->p_comm); | |
672 | return; | |
673 | } |