want.c revision 1.3 1 1.3 christos /* $NetBSD: want.c,v 1.3 2004/11/11 00:54:23 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1987, 1993, 1994
5 1.1 christos * The Regents of the University of California. All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.2 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 christos * may be used to endorse or promote products derived from this software
17 1.1 christos * without specific prior written permission.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 christos * SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos static struct utmp *buf;
32 1.1 christos
33 1.1 christos static void onintr(int);
34 1.1 christos static int want(struct utmp *, int);
35 1.3 christos static const char *gethost(struct utmp *, int);
36 1.3 christos
37 1.3 christos static const char *
38 1.3 christos gethost(struct utmp* ut, int numeric)
39 1.3 christos {
40 1.3 christos #if FIRSTVALID == 0
41 1.3 christos return numeric ? "" : ut->ut_host;
42 1.3 christos #else
43 1.3 christos if (numeric) {
44 1.3 christos static char buf[512];
45 1.3 christos const char *p;
46 1.3 christos struct sockaddr_storage *ss = &ut->ut_ss;
47 1.3 christos void *a;
48 1.3 christos switch (ss->ss_family) {
49 1.3 christos default:
50 1.3 christos (void)snprintf(buf, sizeof(buf), "%d: unknown family",
51 1.3 christos ss->ss_family);
52 1.3 christos return buf;
53 1.3 christos case 0: /* reboot etc. entries */
54 1.3 christos return "";
55 1.3 christos case AF_INET:
56 1.3 christos a = &((struct sockaddr_in *)(void *)ss)->sin_addr;
57 1.3 christos break;
58 1.3 christos case AF_INET6:
59 1.3 christos a = &((struct sockaddr_in6 *)(void *)ss)->sin6_addr;
60 1.3 christos break;
61 1.3 christos }
62 1.3 christos if ((p = inet_ntop(ss->ss_family, a, buf, ss->ss_len)) != NULL)
63 1.3 christos return p;
64 1.3 christos (void)snprintf(buf, sizeof(buf), "%s", strerror(errno));
65 1.3 christos return buf;
66 1.3 christos } else
67 1.3 christos return ut->ut_host;
68 1.3 christos #endif
69 1.3 christos }
70 1.1 christos
71 1.1 christos /*
72 1.1 christos * wtmp --
73 1.1 christos * read through the wtmp file
74 1.1 christos */
75 1.1 christos void
76 1.3 christos wtmp(const char *file, int namesz, int linesz, int hostsz, int numeric)
77 1.1 christos {
78 1.1 christos struct utmp *bp; /* current structure */
79 1.1 christos TTY *T; /* tty list entry */
80 1.1 christos struct stat stb; /* stat of file for sz */
81 1.1 christos time_t delta; /* time difference */
82 1.1 christos off_t bl;
83 1.1 christos int bytes, wfd;
84 1.1 christos char *ct, *crmsg;
85 1.1 christos size_t len = sizeof(*buf) * MAXUTMP;
86 1.1 christos
87 1.1 christos if ((buf = malloc(len)) == NULL)
88 1.1 christos err(1, "Cannot allocate utmp buffer");
89 1.1 christos
90 1.1 christos crmsg = NULL;
91 1.1 christos
92 1.1 christos if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
93 1.1 christos err(1, "%s", file);
94 1.1 christos bl = (stb.st_size + len - 1) / len;
95 1.1 christos
96 1.1 christos buf[FIRSTVALID].ut_timefld = time(NULL);
97 1.1 christos (void)signal(SIGINT, onintr);
98 1.1 christos (void)signal(SIGQUIT, onintr);
99 1.1 christos
100 1.1 christos while (--bl >= 0) {
101 1.1 christos if (lseek(wfd, bl * len, SEEK_SET) == -1 ||
102 1.1 christos (bytes = read(wfd, buf, len)) == -1)
103 1.1 christos err(1, "%s", file);
104 1.1 christos for (bp = &buf[bytes / sizeof(*buf) - 1]; bp >= buf; --bp) {
105 1.1 christos /*
106 1.1 christos * if the terminal line is '~', the machine stopped.
107 1.1 christos * see utmp(5) for more info.
108 1.1 christos */
109 1.1 christos if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
110 1.1 christos /* everybody just logged out */
111 1.1 christos for (T = ttylist; T; T = T->next)
112 1.1 christos T->logout = -bp->ut_timefld;
113 1.1 christos currentout = -bp->ut_timefld;
114 1.1 christos crmsg = strncmp(bp->ut_name, "shutdown",
115 1.1 christos namesz) ? "crash" : "shutdown";
116 1.1 christos if (want(bp, NO)) {
117 1.1 christos ct = fmttime(bp->ut_timefld, fulltime);
118 1.1 christos printf("%-*.*s %-*.*s %-*.*s %s\n",
119 1.1 christos namesz, namesz, bp->ut_name,
120 1.1 christos linesz, linesz, bp->ut_line,
121 1.3 christos hostsz, hostsz,
122 1.3 christos gethost(bp, numeric), ct);
123 1.1 christos if (maxrec != -1 && !--maxrec)
124 1.1 christos return;
125 1.1 christos }
126 1.1 christos continue;
127 1.1 christos }
128 1.1 christos /*
129 1.1 christos * if the line is '{' or '|', date got set; see
130 1.1 christos * utmp(5) for more info.
131 1.1 christos */
132 1.1 christos if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
133 1.1 christos && !bp->ut_line[1]) {
134 1.1 christos if (want(bp, NO)) {
135 1.1 christos ct = fmttime(bp->ut_timefld, fulltime);
136 1.1 christos printf("%-*.*s %-*.*s %-*.*s %s\n",
137 1.1 christos namesz, namesz,
138 1.1 christos bp->ut_name,
139 1.1 christos linesz, linesz,
140 1.1 christos bp->ut_line,
141 1.1 christos hostsz, hostsz,
142 1.3 christos gethost(bp, numeric),
143 1.1 christos ct);
144 1.1 christos if (maxrec && !--maxrec)
145 1.1 christos return;
146 1.1 christos }
147 1.1 christos continue;
148 1.1 christos }
149 1.1 christos /* find associated tty */
150 1.1 christos for (T = ttylist;; T = T->next) {
151 1.1 christos if (!T) {
152 1.1 christos /* add new one */
153 1.1 christos T = addtty(bp->ut_line);
154 1.1 christos break;
155 1.1 christos }
156 1.1 christos if (!strncmp(T->tty, bp->ut_line, LINESIZE))
157 1.1 christos break;
158 1.1 christos }
159 1.1 christos if (TYPE(bp) == SIGNATURE)
160 1.1 christos continue;
161 1.1 christos if (bp->ut_name[0] && want(bp, YES)) {
162 1.1 christos ct = fmttime(bp->ut_timefld, fulltime);
163 1.1 christos printf("%-*.*s %-*.*s %-*.*s %s ",
164 1.1 christos namesz, namesz, bp->ut_name,
165 1.1 christos linesz, linesz, bp->ut_line,
166 1.3 christos hostsz, hostsz,
167 1.3 christos gethost(bp, numeric),
168 1.1 christos ct);
169 1.1 christos if (!T->logout)
170 1.1 christos puts(" still logged in");
171 1.1 christos else {
172 1.1 christos if (T->logout < 0) {
173 1.1 christos T->logout = -T->logout;
174 1.1 christos printf("- %s", crmsg);
175 1.1 christos }
176 1.1 christos else
177 1.1 christos printf("- %s",
178 1.1 christos fmttime(T->logout,
179 1.1 christos fulltime | TIMEONLY));
180 1.1 christos delta = T->logout - bp->ut_timefld;
181 1.1 christos if (delta < SECSPERDAY)
182 1.1 christos printf(" (%s)\n",
183 1.1 christos fmttime(delta,
184 1.1 christos fulltime | TIMEONLY | GMT));
185 1.1 christos else
186 1.1 christos printf(" (%ld+%s)\n",
187 1.1 christos delta / SECSPERDAY,
188 1.1 christos fmttime(delta,
189 1.1 christos fulltime | TIMEONLY | GMT));
190 1.1 christos }
191 1.1 christos if (maxrec != -1 && !--maxrec)
192 1.1 christos return;
193 1.1 christos }
194 1.1 christos T->logout = bp->ut_timefld;
195 1.1 christos }
196 1.1 christos }
197 1.1 christos fulltime = 1; /* show full time */
198 1.1 christos crmsg = fmttime(buf[FIRSTVALID].ut_timefld, FULLTIME);
199 1.1 christos if ((ct = strrchr(file, '/')) != NULL)
200 1.1 christos ct++;
201 1.1 christos printf("\n%s begins %s\n", ct ? ct : file, crmsg);
202 1.1 christos }
203 1.1 christos
204 1.1 christos /*
205 1.1 christos * want --
206 1.1 christos * see if want this entry
207 1.1 christos */
208 1.1 christos static int
209 1.1 christos want(struct utmp *bp, int check)
210 1.1 christos {
211 1.1 christos ARG *step;
212 1.1 christos
213 1.1 christos if (check) {
214 1.1 christos /*
215 1.1 christos * when uucp and ftp log in over a network, the entry in
216 1.1 christos * the utmp file is the name plus their process id. See
217 1.1 christos * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
218 1.1 christos */
219 1.1 christos if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
220 1.1 christos bp->ut_line[3] = '\0';
221 1.1 christos else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
222 1.1 christos bp->ut_line[4] = '\0';
223 1.1 christos }
224 1.1 christos if (!arglist)
225 1.1 christos return (YES);
226 1.1 christos
227 1.1 christos for (step = arglist; step; step = step->next)
228 1.1 christos switch(step->type) {
229 1.1 christos case HOST_TYPE:
230 1.1 christos if (!strncasecmp(step->name, bp->ut_host, HOSTSIZE))
231 1.1 christos return (YES);
232 1.1 christos break;
233 1.1 christos case TTY_TYPE:
234 1.1 christos if (!strncmp(step->name, bp->ut_line, LINESIZE))
235 1.1 christos return (YES);
236 1.1 christos break;
237 1.1 christos case USER_TYPE:
238 1.1 christos if (!strncmp(step->name, bp->ut_name, NAMESIZE))
239 1.1 christos return (YES);
240 1.1 christos break;
241 1.1 christos }
242 1.1 christos return (NO);
243 1.1 christos }
244 1.1 christos
245 1.1 christos /*
246 1.1 christos * onintr --
247 1.1 christos * on interrupt, we inform the user how far we've gotten
248 1.1 christos */
249 1.1 christos static void
250 1.1 christos onintr(int signo)
251 1.1 christos {
252 1.1 christos
253 1.1 christos printf("\ninterrupted %s\n", fmttime(buf[FIRSTVALID].ut_timefld,
254 1.1 christos FULLTIME));
255 1.1 christos if (signo == SIGINT)
256 1.1 christos exit(1);
257 1.1 christos (void)fflush(stdout); /* fix required for rsh */
258 1.1 christos }
259