]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/rwho/rwho.c
Optionally bind ktls threads to NUMA domains
[FreeBSD/FreeBSD.git] / usr.bin / rwho / rwho.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993 The Regents of the University of California.
5  * Copyright (c) 2013 Mariusz Zaborski <oshogbo@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1983, 1993\n\
36         The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)rwho.c      8.1 (Berkeley) 6/6/93";
42 #endif
43 #endif /* not lint */
44
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47
48 #include <sys/capsicum.h>
49 #include <sys/param.h>
50 #include <sys/file.h>
51
52 #include <protocols/rwhod.h>
53
54 #include <capsicum_helpers.h>
55 #include <dirent.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <langinfo.h>
59 #include <locale.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <time.h>
64 #include <timeconv.h>
65 #include <unistd.h>
66
67 #define NUSERS          1000
68 #define WHDRSIZE        (ssize_t)(sizeof(wd) - sizeof(wd.wd_we))
69 /*
70  * this macro should be shared with ruptime.
71  */
72 #define down(w,now)     ((now) - (w)->wd_recvtime > 11 * 60)
73
74 static DIR      *dirp;
75 static struct   whod wd;
76 static int      nusers;
77 static struct   myutmp {
78         char    myhost[sizeof(wd.wd_hostname)];
79         int     myidle;
80         struct  outmp myutmp;
81 } myutmp[NUSERS];
82
83 static time_t   now;
84 static int      aflg;
85
86 static void usage(void);
87 static int utmpcmp(const void *, const void *);
88
89 int
90 main(int argc, char *argv[])
91 {
92         int ch;
93         struct dirent *dp;
94         int width;
95         ssize_t cc;
96         struct whod *w;
97         struct whoent *we;
98         struct myutmp *mp;
99         cap_rights_t rights;
100         int f, n, i;
101         int d_first;
102         int dfd;
103         time_t ct;
104
105         w = &wd;
106         (void) setlocale(LC_TIME, "");
107         d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
108
109         while ((ch = getopt(argc, argv, "a")) != -1) {
110                 switch ((char)ch) {
111                 case 'a':
112                         aflg = 1;
113                         break;
114                 case '?':
115                 default:
116                         usage();
117                 }
118         }
119         argc -= optind;
120         argv += optind;
121
122         if (argc != 0)
123                 usage();
124
125         if (chdir(_PATH_RWHODIR) < 0)
126                 err(1, "chdir(%s)", _PATH_RWHODIR);
127         if ((dirp = opendir(".")) == NULL)
128                 err(1, "opendir(%s)", _PATH_RWHODIR);
129         dfd = dirfd(dirp);
130         mp = myutmp;
131         cap_rights_init(&rights, CAP_READ, CAP_LOOKUP);
132         if (caph_rights_limit(dfd, &rights) < 0)
133                 err(1, "cap_rights_limit failed: %s", _PATH_RWHODIR);
134         /*
135          * Cache files required for time(3) and localtime(3) before entering
136          * capability mode.
137          */
138         (void) time(&ct);
139         (void) localtime(&ct);
140         if (caph_enter() < 0)
141                 err(1, "cap_enter");
142         (void) time(&now);
143         cap_rights_init(&rights, CAP_READ);
144         while ((dp = readdir(dirp)) != NULL) {
145                 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
146                         continue;
147                 f = openat(dfd, dp->d_name, O_RDONLY);
148                 if (f < 0)
149                         continue;
150                 if (caph_rights_limit(f, &rights) < 0)
151                         err(1, "cap_rights_limit failed: %s", dp->d_name);
152                 cc = read(f, (char *)&wd, sizeof(struct whod));
153                 if (cc < WHDRSIZE) {
154                         (void) close(f);
155                         continue;
156                 }
157                 if (down(w, now) != 0) {
158                         (void) close(f);
159                         continue;
160                 }
161                 cc -= WHDRSIZE;
162                 we = w->wd_we;
163                 for (n = cc / sizeof(struct whoent); n > 0; n--) {
164                         if (aflg == 0 && we->we_idle >= 60 * 60) {
165                                 we++;
166                                 continue;
167                         }
168                         if (nusers >= NUSERS)
169                                 errx(1, "too many users");
170                         mp->myutmp = we->we_utmp;
171                         mp->myidle = we->we_idle;
172                         (void) strcpy(mp->myhost, w->wd_hostname);
173                         nusers++;
174                         we++;
175                         mp++;
176                 }
177                 (void) close(f);
178         }
179         qsort((char *)myutmp, nusers, sizeof(struct myutmp), utmpcmp);
180         mp = myutmp;
181         width = 0;
182         for (i = 0; i < nusers; i++) {
183                 /* append one for the blank and use 8 for the out_line */
184                 int j;
185
186                 j = strlen(mp->myhost) + 1 + sizeof(mp->myutmp.out_line);
187                 if (j > width)
188                         width = j;
189                 mp++;
190         }
191         mp = myutmp;
192         for (i = 0; i < nusers; i++) {
193                 char buf[BUFSIZ], cbuf[80];
194                 time_t t;
195
196                 t = _int_to_time(mp->myutmp.out_time);
197                 strftime(cbuf, sizeof(cbuf), d_first ? "%e %b %R" : "%b %e %R",
198                     localtime(&t));
199                 (void) sprintf(buf, "%s:%-.*s", mp->myhost,
200                     (int)sizeof(mp->myutmp.out_line), mp->myutmp.out_line);
201                 printf("%-*.*s %-*s %s",
202                     (int)sizeof(mp->myutmp.out_name),
203                     (int)sizeof(mp->myutmp.out_name),
204                     mp->myutmp.out_name, width, buf, cbuf);
205                 mp->myidle /= 60;
206                 if (mp->myidle != 0) {
207                         if (aflg != 0) {
208                                 if (mp->myidle >= 100 * 60)
209                                         mp->myidle = 100 * 60 - 1;
210                                 if (mp->myidle >= 60)
211                                         printf(" %2d", mp->myidle / 60);
212                                 else
213                                         printf("   ");
214                         } else {
215                                 printf(" ");
216                         }
217                         printf(":%02d", mp->myidle % 60);
218                 }
219                 printf("\n");
220                 mp++;
221         }
222         exit(0);
223 }
224
225
226 static void
227 usage(void)
228 {
229
230         fprintf(stderr, "usage: rwho [-a]\n");
231         exit(1);
232 }
233
234 #define MYUTMP(a) ((const struct myutmp *)(a))
235
236 static int
237 utmpcmp(const void *u1, const void *u2)
238 {
239         int rc;
240
241         rc = strncmp(MYUTMP(u1)->myutmp.out_name, MYUTMP(u2)->myutmp.out_name,
242             sizeof(MYUTMP(u2)->myutmp.out_name));
243         if (rc != 0)
244                 return (rc);
245         rc = strcmp(MYUTMP(u1)->myhost, MYUTMP(u2)->myhost);
246         if (rc != 0)
247                 return (rc);
248         return (strncmp(MYUTMP(u1)->myutmp.out_line,
249             MYUTMP(u2)->myutmp.out_line, sizeof(MYUTMP(u2)->myutmp.out_line)));
250 }