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