Merge branch 'vendor/OPENPAM'
[dragonfly.git] / usr.bin / ruptime / ruptime.c
1 /*
2  * Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1983, 1993, 1994 The Regents of the University of California.  All rights reserved.
30  * @(#)ruptime.c        8.2 (Berkeley) 4/5/94
31  * $FreeBSD: src/usr.bin/ruptime/ruptime.c,v 1.12.2.1 2000/06/30 09:45:00 ps Exp $
32  */
33
34 #include <sys/param.h>
35
36 #include <protocols/rwhod.h>
37
38 #include <dirent.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
46 #include <unistd.h>
47
48 static struct hs {
49         struct  whod *hs_wd;
50         int     hs_nusers;
51 } *hs;
52
53 #define LEFTEARTH(h)    (now - (h) > 4*24*60*60)
54 #define ISDOWN(h)       (now - (h)->hs_wd->wd_recvtime > 11 * 60)
55 #define WHDRSIZE        (sizeof(struct whod) - \
56                             sizeof(((struct whod *)NULL)->wd_we))
57
58 static size_t nhosts;
59 static time_t now;
60 static int rflg = 1;
61
62 static int       hscmp(const void *, const void *);
63 static char     *interval(time_t, const char *);
64 static int       lcmp(const void *, const void *);
65 static int       tcmp(const void *, const void *);
66 static int       ucmp(const void *, const void *);
67 static void      usage(void) __dead2;
68
69 int
70 main(int argc, char *argv[])
71 {
72         struct dirent *dp;
73         struct hs *hsp = NULL;
74         struct whod *wd;
75         struct whoent *we;
76         DIR *dirp;
77         size_t hspace;
78         int aflg, ch, fd, maxloadav;
79         char buf[sizeof(struct whod)];
80         int (*cmp)(const void *, const void *);
81         u_int cc, i;
82
83         aflg = 0;
84         cmp = hscmp;
85         while ((ch = getopt(argc, argv, "alrut")) != -1)
86                 switch (ch) {
87                 case 'a':
88                         aflg = 1;
89                         break;
90                 case 'l':
91                         cmp = lcmp;
92                         break;
93                 case 'r':
94                         rflg = -1;
95                         break;
96                 case 't':
97                         cmp = tcmp;
98                         break;
99                 case 'u':
100                         cmp = ucmp;
101                         break;
102                 default:
103                         usage();
104                 }
105         argc -= optind;
106         argv += optind;
107
108         if (argc != 0)
109                 usage();
110
111         if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
112                 err(1, "%s", _PATH_RWHODIR);
113
114         maxloadav = -1;
115         for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
116                 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
117                         continue;
118                 if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
119                         warn("%s", dp->d_name);
120                         continue;
121                 }
122                 cc = read(fd, buf, sizeof(struct whod));
123                 (void)close(fd);
124
125                 if (cc < WHDRSIZE)
126                         continue;
127                 if (LEFTEARTH(((struct whod *)buf)->wd_recvtime))
128                         continue;
129                 if (nhosts == hspace) {
130                         hspace += 40;
131                         if ((hs = realloc(hs, hspace * sizeof(*hs))) == NULL)
132                                 err(1, NULL);
133                         hsp = hs + nhosts;
134                 }
135
136                 if ((hsp->hs_wd = malloc((size_t)WHDRSIZE)) == NULL)
137                         err(1, NULL);
138                 memmove(hsp->hs_wd, buf, (size_t)WHDRSIZE);
139
140                 for (wd = (struct whod *)buf, i = 0; i < 2; ++i)
141                         if (wd->wd_loadav[i] > maxloadav)
142                                 maxloadav = wd->wd_loadav[i];
143
144                 for (hsp->hs_nusers = 0,
145                     we = (struct whoent *)(buf + cc); --we >= wd->wd_we;)
146                         if (aflg || we->we_idle < 3600)
147                                 ++hsp->hs_nusers;
148                 ++hsp;
149                 ++nhosts;
150         }
151         if (nhosts == 0)
152                 errx(1, "no hosts in %s", _PATH_RWHODIR);
153
154         (void)time(&now);
155         qsort(hs, nhosts, sizeof(hs[0]), cmp);
156         for (i = 0; i < nhosts; i++) {
157                 hsp = &hs[i];
158                 if (ISDOWN(hsp)) {
159                         (void)printf("%-12.12s%s\n", hsp->hs_wd->wd_hostname,
160                             interval(now - hsp->hs_wd->wd_recvtime, "down"));
161                         continue;
162                 }
163                 (void)printf(
164                     "%-12.12s%s,  %4d user%s  load %*.2f, %*.2f, %*.2f\n",
165                     hsp->hs_wd->wd_hostname,
166                     interval((time_t)hsp->hs_wd->wd_sendtime -
167                         (time_t)hsp->hs_wd->wd_boottime, "  up"),
168                     hsp->hs_nusers,
169                     hsp->hs_nusers == 1 ? ", " : "s,",
170                     maxloadav >= 1000 ? 5 : 4,
171                         hsp->hs_wd->wd_loadav[0] / 100.0,
172                     maxloadav >= 1000 ? 5 : 4,
173                         hsp->hs_wd->wd_loadav[1] / 100.0,
174                     maxloadav >= 1000 ? 5 : 4,
175                         hsp->hs_wd->wd_loadav[2] / 100.0);
176         }
177         exit(0);
178 }
179
180 static char *
181 interval(time_t tval, const char *updown)
182 {
183         static char resbuf[32];
184         int days, hours, minutes;
185
186         if (tval < 0) {
187                 (void)snprintf(resbuf, sizeof(resbuf), "   %s ??:??", updown);
188                 return (resbuf);
189         }
190                                                 /* round to minutes. */
191         minutes = (tval + (60 - 1)) / 60;
192         hours = minutes / 60;
193         minutes %= 60;
194         days = hours / 24;
195         hours %= 24;
196         if (days)
197                 (void)snprintf(resbuf, sizeof(resbuf),
198                     "%s %3d+%02d:%02d", updown, days, hours, minutes);
199         else
200                 (void)snprintf(resbuf, sizeof(resbuf),
201                     "%s     %2d:%02d", updown, hours, minutes);
202         return (resbuf);
203 }
204
205 #define HS(a)   ((const struct hs *)(a))
206
207 /* Alphabetical comparison. */
208 static int
209 hscmp(const void *a1, const void *a2)
210 {
211         return (rflg *
212             strcmp(HS(a1)->hs_wd->wd_hostname, HS(a2)->hs_wd->wd_hostname));
213 }
214
215 /* Load average comparison. */
216 static int
217 lcmp(const void *a1, const void *a2)
218 {
219         if (ISDOWN(HS(a1)))
220                 if (ISDOWN(HS(a2)))
221                         return (tcmp(a1, a2));
222                 else
223                         return (rflg);
224         else if (ISDOWN(HS(a2)))
225                 return (-rflg);
226         else
227                 return (rflg *
228                    (HS(a2)->hs_wd->wd_loadav[0] - HS(a1)->hs_wd->wd_loadav[0]));
229 }
230
231 /* Number of users comparison. */
232 static int
233 ucmp(const void *a1, const void *a2)
234 {
235         if (ISDOWN(HS(a1)))
236                 if (ISDOWN(HS(a2)))
237                         return (tcmp(a1, a2));
238                 else
239                         return (rflg);
240         else if (ISDOWN(HS(a2)))
241                 return (-rflg);
242         else
243                 return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
244 }
245
246 /* Uptime comparison. */
247 static int
248 tcmp(const void *a1, const void *a2)
249 {
250         return (rflg * (
251                 (ISDOWN(HS(a2)) ? HS(a2)->hs_wd->wd_recvtime - now
252                     : HS(a2)->hs_wd->wd_sendtime - HS(a2)->hs_wd->wd_boottime)
253                 -
254                 (ISDOWN(HS(a1)) ? HS(a1)->hs_wd->wd_recvtime - now
255                     : HS(a1)->hs_wd->wd_sendtime - HS(a1)->hs_wd->wd_boottime)
256         ));
257 }
258
259 static void
260 usage(void)
261 {
262         (void)fprintf(stderr, "usage: ruptime [-alrut]\n");
263         exit(1);
264 }