utmpentry.c revision 1.12 1 1.12 dholland /* $NetBSD: utmpentry.c,v 1.12 2008/03/08 22:29:26 dholland Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the NetBSD
21 1.1 christos * Foundation, Inc. and its contributors.
22 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 christos * contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos
39 1.1 christos #include <sys/cdefs.h>
40 1.1 christos #ifndef lint
41 1.12 dholland __RCSID("$NetBSD: utmpentry.c,v 1.12 2008/03/08 22:29:26 dholland Exp $");
42 1.1 christos #endif
43 1.1 christos
44 1.1 christos #include <sys/stat.h>
45 1.1 christos
46 1.1 christos #include <time.h>
47 1.1 christos #include <string.h>
48 1.1 christos #include <err.h>
49 1.1 christos #include <stdlib.h>
50 1.1 christos
51 1.1 christos #ifdef SUPPORT_UTMP
52 1.1 christos #include <utmp.h>
53 1.1 christos #endif
54 1.1 christos #ifdef SUPPORT_UTMPX
55 1.1 christos #include <utmpx.h>
56 1.1 christos #endif
57 1.1 christos
58 1.1 christos #include "utmpentry.h"
59 1.1 christos
60 1.1 christos
61 1.12 dholland /* Fail the compile if x is not true, by constructing an illegal type. */
62 1.12 dholland #define COMPILE_ASSERT(x) ((void)sizeof(struct { unsigned : ((x) ? 1 : -1); }))
63 1.12 dholland
64 1.12 dholland
65 1.1 christos #ifdef SUPPORT_UTMP
66 1.1 christos static void getentry(struct utmpentry *, struct utmp *);
67 1.11 christos static struct timespec utmptime = {0, 0};
68 1.1 christos #endif
69 1.1 christos #ifdef SUPPORT_UTMPX
70 1.1 christos static void getentryx(struct utmpentry *, struct utmpx *);
71 1.11 christos static struct timespec utmpxtime = {0, 0};
72 1.1 christos #endif
73 1.3 christos #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
74 1.1 christos static int setup(const char *);
75 1.1 christos static void adjust_size(struct utmpentry *e);
76 1.1 christos #endif
77 1.1 christos
78 1.1 christos int maxname = 8, maxline = 8, maxhost = 16;
79 1.9 christos int etype = 1 << USER_PROCESS;
80 1.1 christos static int numutmp = 0;
81 1.1 christos static struct utmpentry *ehead;
82 1.1 christos
83 1.3 christos #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
84 1.1 christos static void
85 1.1 christos adjust_size(struct utmpentry *e)
86 1.1 christos {
87 1.1 christos int max;
88 1.1 christos
89 1.1 christos if ((max = strlen(e->name)) > maxname)
90 1.1 christos maxname = max;
91 1.1 christos if ((max = strlen(e->line)) > maxline)
92 1.1 christos maxline = max;
93 1.1 christos if ((max = strlen(e->host)) > maxhost)
94 1.1 christos maxhost = max;
95 1.1 christos }
96 1.1 christos
97 1.1 christos static int
98 1.1 christos setup(const char *fname)
99 1.1 christos {
100 1.1 christos int what = 3;
101 1.1 christos struct stat st;
102 1.5 christos const char *sfname;
103 1.1 christos
104 1.1 christos if (fname == NULL) {
105 1.1 christos #ifdef SUPPORT_UTMPX
106 1.3 christos setutxent();
107 1.1 christos #endif
108 1.1 christos #ifdef SUPPORT_UTMP
109 1.3 christos setutent();
110 1.1 christos #endif
111 1.1 christos } else {
112 1.1 christos size_t len = strlen(fname);
113 1.1 christos if (len == 0)
114 1.1 christos errx(1, "Filename cannot be 0 length.");
115 1.1 christos what = fname[len - 1] == 'x' ? 1 : 2;
116 1.1 christos if (what == 1) {
117 1.1 christos #ifdef SUPPORT_UTMPX
118 1.1 christos if (utmpxname(fname) == 0)
119 1.5 christos warnx("Cannot set utmpx file to `%s'",
120 1.5 christos fname);
121 1.1 christos #else
122 1.5 christos warnx("utmpx support not compiled in");
123 1.1 christos #endif
124 1.1 christos } else {
125 1.3 christos #ifdef SUPPORT_UTMP
126 1.1 christos if (utmpname(fname) == 0)
127 1.5 christos warnx("Cannot set utmp file to `%s'",
128 1.5 christos fname);
129 1.1 christos #else
130 1.5 christos warnx("utmp support not compiled in");
131 1.1 christos #endif
132 1.1 christos }
133 1.1 christos }
134 1.1 christos #ifdef SUPPORT_UTMPX
135 1.1 christos if (what & 1) {
136 1.5 christos sfname = fname ? fname : _PATH_UTMPX;
137 1.5 christos if (stat(sfname, &st) == -1) {
138 1.5 christos warn("Cannot stat `%s'", sfname);
139 1.1 christos what &= ~1;
140 1.5 christos } else {
141 1.11 christos if (timespeccmp(&st.st_mtimespec, &utmpxtime, >))
142 1.11 christos utmpxtime = st.st_mtimespec;
143 1.5 christos else
144 1.5 christos what &= ~1;
145 1.5 christos }
146 1.1 christos }
147 1.1 christos #endif
148 1.1 christos #ifdef SUPPORT_UTMP
149 1.1 christos if (what & 2) {
150 1.5 christos sfname = fname ? fname : _PATH_UTMP;
151 1.5 christos if (stat(sfname, &st) == -1) {
152 1.5 christos warn("Cannot stat `%s'", sfname);
153 1.1 christos what &= ~2;
154 1.5 christos } else {
155 1.11 christos if (timespeccmp(&st.st_mtimespec, &utmptime, >))
156 1.11 christos utmptime = st.st_mtimespec;
157 1.5 christos else
158 1.5 christos what &= ~2;
159 1.5 christos }
160 1.1 christos }
161 1.1 christos #endif
162 1.1 christos return what;
163 1.1 christos }
164 1.1 christos #endif
165 1.1 christos
166 1.1 christos void
167 1.1 christos freeutentries(struct utmpentry *ep)
168 1.1 christos {
169 1.4 christos #ifdef SUPPORT_UTMP
170 1.11 christos timespecclear(&utmptime);
171 1.4 christos #endif
172 1.4 christos #ifdef SUPPORT_UTMPX
173 1.11 christos timespecclear(&utmpxtime);
174 1.4 christos #endif
175 1.1 christos if (ep == ehead) {
176 1.1 christos ehead = NULL;
177 1.1 christos numutmp = 0;
178 1.1 christos }
179 1.1 christos while (ep) {
180 1.2 christos struct utmpentry *sep = ep;
181 1.1 christos ep = ep->next;
182 1.1 christos free(sep);
183 1.1 christos }
184 1.1 christos }
185 1.1 christos
186 1.1 christos int
187 1.1 christos getutentries(const char *fname, struct utmpentry **epp)
188 1.1 christos {
189 1.1 christos #ifdef SUPPORT_UTMPX
190 1.1 christos struct utmpx *utx;
191 1.1 christos #endif
192 1.1 christos #ifdef SUPPORT_UTMP
193 1.1 christos struct utmp *ut;
194 1.1 christos #endif
195 1.3 christos #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
196 1.2 christos struct utmpentry *ep;
197 1.1 christos int what = setup(fname);
198 1.1 christos struct utmpentry **nextp = &ehead;
199 1.5 christos switch (what) {
200 1.5 christos case 0:
201 1.5 christos /* No updates */
202 1.1 christos *epp = ehead;
203 1.1 christos return numutmp;
204 1.5 christos default:
205 1.5 christos /* Need to re-scan */
206 1.1 christos ehead = NULL;
207 1.1 christos numutmp = 0;
208 1.1 christos }
209 1.3 christos #endif
210 1.1 christos
211 1.1 christos #ifdef SUPPORT_UTMPX
212 1.1 christos while ((what & 1) && (utx = getutxent()) != NULL) {
213 1.9 christos if (fname == NULL && ((1 << utx->ut_type) & etype) == 0)
214 1.1 christos continue;
215 1.5 christos if ((ep = calloc(1, sizeof(struct utmpentry))) == NULL) {
216 1.5 christos warn(NULL);
217 1.5 christos return 0;
218 1.5 christos }
219 1.1 christos getentryx(ep, utx);
220 1.1 christos *nextp = ep;
221 1.1 christos nextp = &(ep->next);
222 1.1 christos }
223 1.1 christos #endif
224 1.1 christos
225 1.1 christos #ifdef SUPPORT_UTMP
226 1.9 christos if ((etype & (1 << USER_PROCESS)) != 0) {
227 1.9 christos while ((what & 2) && (ut = getutent()) != NULL) {
228 1.9 christos if (fname == NULL && (*ut->ut_name == '\0' ||
229 1.9 christos *ut->ut_line == '\0'))
230 1.9 christos continue;
231 1.9 christos /* Don't process entries that we have utmpx for */
232 1.9 christos for (ep = ehead; ep != NULL; ep = ep->next) {
233 1.9 christos if (strncmp(ep->line, ut->ut_line,
234 1.9 christos sizeof(ut->ut_line)) == 0)
235 1.9 christos break;
236 1.9 christos }
237 1.9 christos if (ep != NULL)
238 1.9 christos continue;
239 1.9 christos if ((ep = calloc(1, sizeof(*ep))) == NULL) {
240 1.9 christos warn(NULL);
241 1.9 christos return 0;
242 1.9 christos }
243 1.9 christos getentry(ep, ut);
244 1.9 christos *nextp = ep;
245 1.9 christos nextp = &(ep->next);
246 1.5 christos }
247 1.1 christos }
248 1.1 christos #endif
249 1.1 christos numutmp = 0;
250 1.1 christos #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
251 1.1 christos if (ehead != NULL) {
252 1.1 christos struct utmpentry *from = ehead, *save;
253 1.1 christos
254 1.1 christos ehead = NULL;
255 1.1 christos while (from != NULL) {
256 1.1 christos for (nextp = &ehead;
257 1.1 christos (*nextp) && strcmp(from->line, (*nextp)->line) > 0;
258 1.1 christos nextp = &(*nextp)->next)
259 1.1 christos continue;
260 1.1 christos save = from;
261 1.1 christos from = from->next;
262 1.1 christos save->next = *nextp;
263 1.1 christos *nextp = save;
264 1.1 christos numutmp++;
265 1.1 christos }
266 1.1 christos }
267 1.1 christos *epp = ehead;
268 1.1 christos return numutmp;
269 1.3 christos #else
270 1.3 christos *epp = NULL;
271 1.3 christos return 0;
272 1.3 christos #endif
273 1.1 christos }
274 1.1 christos
275 1.1 christos #ifdef SUPPORT_UTMP
276 1.1 christos static void
277 1.1 christos getentry(struct utmpentry *e, struct utmp *up)
278 1.1 christos {
279 1.12 dholland COMPILE_ASSERT(sizeof(e->name) > sizeof(up->ut_name));
280 1.12 dholland COMPILE_ASSERT(sizeof(e->line) > sizeof(up->ut_line));
281 1.12 dholland COMPILE_ASSERT(sizeof(e->host) > sizeof(up->ut_host));
282 1.12 dholland
283 1.12 dholland /*
284 1.12 dholland * e has just been calloc'd. We don't need to clear it or
285 1.12 dholland * append null-terminators. Use strncpy to _read_ up->ut_*
286 1.12 dholland * because they may not be terminated.
287 1.12 dholland */
288 1.1 christos (void)strncpy(e->name, up->ut_name, sizeof(up->ut_name));
289 1.1 christos (void)strncpy(e->line, up->ut_line, sizeof(up->ut_line));
290 1.1 christos (void)strncpy(e->host, up->ut_host, sizeof(up->ut_host));
291 1.12 dholland
292 1.1 christos e->tv.tv_sec = up->ut_time;
293 1.1 christos e->tv.tv_usec = 0;
294 1.8 hubertf e->pid = 0;
295 1.8 hubertf e->term = 0;
296 1.8 hubertf e->exit = 0;
297 1.8 hubertf e->sess = 0;
298 1.10 christos e->type = USER_PROCESS;
299 1.1 christos adjust_size(e);
300 1.1 christos }
301 1.1 christos #endif
302 1.1 christos
303 1.1 christos #ifdef SUPPORT_UTMPX
304 1.1 christos static void
305 1.1 christos getentryx(struct utmpentry *e, struct utmpx *up)
306 1.1 christos {
307 1.12 dholland COMPILE_ASSERT(sizeof(e->name) > sizeof(up->ut_name));
308 1.12 dholland COMPILE_ASSERT(sizeof(e->line) > sizeof(up->ut_line));
309 1.12 dholland COMPILE_ASSERT(sizeof(e->host) > sizeof(up->ut_host));
310 1.12 dholland
311 1.12 dholland /*
312 1.12 dholland * e has just been calloc'd. We don't need to clear it or
313 1.12 dholland * append null-terminators. Use strncpy to _read_ up->ut_*
314 1.12 dholland * because they may not be terminated.
315 1.12 dholland */
316 1.1 christos (void)strncpy(e->name, up->ut_name, sizeof(up->ut_name));
317 1.1 christos (void)strncpy(e->line, up->ut_line, sizeof(up->ut_line));
318 1.1 christos (void)strncpy(e->host, up->ut_host, sizeof(up->ut_host));
319 1.12 dholland
320 1.1 christos e->tv = up->ut_tv;
321 1.8 hubertf e->pid = up->ut_pid;
322 1.8 hubertf e->term = up->ut_exit.e_termination;
323 1.8 hubertf e->exit = up->ut_exit.e_exit;
324 1.8 hubertf e->sess = up->ut_session;
325 1.8 hubertf e->type = up->ut_type;
326 1.1 christos adjust_size(e);
327 1.1 christos }
328 1.1 christos #endif
329