2 * Copyright (c) 1983, 1993 The Regents of the University of California.
3 * Copyright (c) 2013 Mariusz Zaborski <oshogbo@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char copyright[] =
33 "@(#) Copyright (c) 1983, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
39 static char sccsid[] = "@(#)rwho.c 8.1 (Berkeley) 6/6/93";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/capsicum.h>
47 #include <sys/param.h>
50 #include <protocols/rwhod.h>
65 #define WHDRSIZE (ssize_t)(sizeof(wd) - sizeof(wd.wd_we))
67 * this macro should be shared with ruptime.
69 #define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60)
72 static struct whod wd;
74 static struct myutmp {
75 char myhost[sizeof(wd.wd_hostname)];
83 static void usage(void);
84 static int utmpcmp(const void *, const void *);
87 main(int argc, char *argv[])
103 (void) setlocale(LC_TIME, "");
104 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
106 while ((ch = getopt(argc, argv, "a")) != -1) {
122 if (chdir(_PATH_RWHODIR) < 0)
123 err(1, "chdir(%s)", _PATH_RWHODIR);
124 if ((dirp = opendir(".")) == NULL)
125 err(1, "opendir(%s)", _PATH_RWHODIR);
128 cap_rights_init(&rights, CAP_READ, CAP_LOOKUP);
129 if (cap_rights_limit(dfd, &rights) < 0 && errno != ENOSYS)
130 err(1, "cap_rights_limit failed: %s", _PATH_RWHODIR);
132 * Cache files required for time(3) and localtime(3) before entering
136 (void) localtime(&ct);
137 if (cap_enter() < 0 && errno != ENOSYS)
140 cap_rights_init(&rights, CAP_READ);
141 while ((dp = readdir(dirp)) != NULL) {
142 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
144 f = openat(dfd, dp->d_name, O_RDONLY);
147 if (cap_rights_limit(f, &rights) < 0 && errno != ENOSYS)
148 err(1, "cap_rights_limit failed: %s", dp->d_name);
149 cc = read(f, (char *)&wd, sizeof(struct whod));
154 if (down(w, now) != 0) {
160 for (n = cc / sizeof(struct whoent); n > 0; n--) {
161 if (aflg == 0 && we->we_idle >= 60 * 60) {
165 if (nusers >= NUSERS)
166 errx(1, "too many users");
167 mp->myutmp = we->we_utmp;
168 mp->myidle = we->we_idle;
169 (void) strcpy(mp->myhost, w->wd_hostname);
176 qsort((char *)myutmp, nusers, sizeof(struct myutmp), utmpcmp);
179 for (i = 0; i < nusers; i++) {
180 /* append one for the blank and use 8 for the out_line */
183 j = strlen(mp->myhost) + 1 + sizeof(mp->myutmp.out_line);
189 for (i = 0; i < nusers; i++) {
190 char buf[BUFSIZ], cbuf[80];
193 t = _int_to_time(mp->myutmp.out_time);
194 strftime(cbuf, sizeof(cbuf), d_first ? "%e %b %R" : "%b %e %R",
196 (void) sprintf(buf, "%s:%-.*s", mp->myhost,
197 (int)sizeof(mp->myutmp.out_line), mp->myutmp.out_line);
198 printf("%-*.*s %-*s %s",
199 (int)sizeof(mp->myutmp.out_name),
200 (int)sizeof(mp->myutmp.out_name),
201 mp->myutmp.out_name, width, buf, cbuf);
203 if (mp->myidle != 0) {
205 if (mp->myidle >= 100 * 60)
206 mp->myidle = 100 * 60 - 1;
207 if (mp->myidle >= 60)
208 printf(" %2d", mp->myidle / 60);
214 printf(":%02d", mp->myidle % 60);
227 fprintf(stderr, "usage: rwho [-a]\n");
231 #define MYUTMP(a) ((const struct myutmp *)(a))
234 utmpcmp(const void *u1, const void *u2)
238 rc = strncmp(MYUTMP(u1)->myutmp.out_name, MYUTMP(u2)->myutmp.out_name,
239 sizeof(MYUTMP(u2)->myutmp.out_name));
242 rc = strcmp(MYUTMP(u1)->myhost, MYUTMP(u2)->myhost);
245 return (strncmp(MYUTMP(u1)->myutmp.out_line,
246 MYUTMP(u2)->myutmp.out_line, sizeof(MYUTMP(u2)->myutmp.out_line)));