utmpentry.c revision 1.7 1 1.7 elad /* $NetBSD: utmpentry.c,v 1.7 2006/03/17 20:44:28 elad 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.7 elad __RCSID("$NetBSD: utmpentry.c,v 1.7 2006/03/17 20:44:28 elad 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.1 christos #ifdef SUPPORT_UTMP
62 1.1 christos static void getentry(struct utmpentry *, struct utmp *);
63 1.1 christos static time_t utmptime = 0;
64 1.1 christos #endif
65 1.1 christos #ifdef SUPPORT_UTMPX
66 1.1 christos static void getentryx(struct utmpentry *, struct utmpx *);
67 1.1 christos static time_t utmpxtime = 0;
68 1.1 christos #endif
69 1.3 christos #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
70 1.1 christos static int setup(const char *);
71 1.1 christos static void adjust_size(struct utmpentry *e);
72 1.1 christos #endif
73 1.1 christos
74 1.1 christos int maxname = 8, maxline = 8, maxhost = 16;
75 1.1 christos static int numutmp = 0;
76 1.1 christos static struct utmpentry *ehead;
77 1.1 christos
78 1.3 christos #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
79 1.1 christos static void
80 1.1 christos adjust_size(struct utmpentry *e)
81 1.1 christos {
82 1.1 christos int max;
83 1.1 christos
84 1.1 christos if ((max = strlen(e->name)) > maxname)
85 1.1 christos maxname = max;
86 1.1 christos if ((max = strlen(e->line)) > maxline)
87 1.1 christos maxline = max;
88 1.1 christos if ((max = strlen(e->host)) > maxhost)
89 1.1 christos maxhost = max;
90 1.1 christos }
91 1.1 christos
92 1.1 christos static int
93 1.1 christos setup(const char *fname)
94 1.1 christos {
95 1.1 christos int what = 3;
96 1.1 christos struct stat st;
97 1.5 christos const char *sfname;
98 1.1 christos
99 1.1 christos if (fname == NULL) {
100 1.1 christos #ifdef SUPPORT_UTMPX
101 1.3 christos setutxent();
102 1.1 christos #endif
103 1.1 christos #ifdef SUPPORT_UTMP
104 1.3 christos setutent();
105 1.1 christos #endif
106 1.1 christos } else {
107 1.1 christos size_t len = strlen(fname);
108 1.1 christos if (len == 0)
109 1.1 christos errx(1, "Filename cannot be 0 length.");
110 1.1 christos what = fname[len - 1] == 'x' ? 1 : 2;
111 1.1 christos if (what == 1) {
112 1.1 christos #ifdef SUPPORT_UTMPX
113 1.1 christos if (utmpxname(fname) == 0)
114 1.5 christos warnx("Cannot set utmpx file to `%s'",
115 1.5 christos fname);
116 1.1 christos #else
117 1.5 christos warnx("utmpx support not compiled in");
118 1.1 christos #endif
119 1.1 christos } else {
120 1.3 christos #ifdef SUPPORT_UTMP
121 1.1 christos if (utmpname(fname) == 0)
122 1.5 christos warnx("Cannot set utmp file to `%s'",
123 1.5 christos fname);
124 1.1 christos #else
125 1.5 christos warnx("utmp support not compiled in");
126 1.1 christos #endif
127 1.1 christos }
128 1.1 christos }
129 1.1 christos #ifdef SUPPORT_UTMPX
130 1.1 christos if (what & 1) {
131 1.5 christos sfname = fname ? fname : _PATH_UTMPX;
132 1.5 christos if (stat(sfname, &st) == -1) {
133 1.5 christos warn("Cannot stat `%s'", sfname);
134 1.1 christos what &= ~1;
135 1.5 christos } else {
136 1.5 christos if (st.st_mtime > utmpxtime)
137 1.5 christos utmpxtime = st.st_mtime;
138 1.5 christos else
139 1.5 christos what &= ~1;
140 1.5 christos }
141 1.1 christos }
142 1.1 christos #endif
143 1.1 christos #ifdef SUPPORT_UTMP
144 1.1 christos if (what & 2) {
145 1.5 christos sfname = fname ? fname : _PATH_UTMP;
146 1.5 christos if (stat(sfname, &st) == -1) {
147 1.5 christos warn("Cannot stat `%s'", sfname);
148 1.1 christos what &= ~2;
149 1.5 christos } else {
150 1.5 christos if (st.st_mtime > utmptime)
151 1.5 christos utmptime = st.st_mtime;
152 1.5 christos else
153 1.5 christos what &= ~2;
154 1.5 christos }
155 1.1 christos }
156 1.1 christos #endif
157 1.1 christos return what;
158 1.1 christos }
159 1.1 christos #endif
160 1.1 christos
161 1.1 christos void
162 1.1 christos freeutentries(struct utmpentry *ep)
163 1.1 christos {
164 1.4 christos #ifdef SUPPORT_UTMP
165 1.4 christos utmptime = 0;
166 1.4 christos #endif
167 1.4 christos #ifdef SUPPORT_UTMPX
168 1.4 christos utmpxtime = 0;
169 1.4 christos #endif
170 1.1 christos if (ep == ehead) {
171 1.1 christos ehead = NULL;
172 1.1 christos numutmp = 0;
173 1.1 christos }
174 1.1 christos while (ep) {
175 1.2 christos struct utmpentry *sep = ep;
176 1.1 christos ep = ep->next;
177 1.1 christos free(sep);
178 1.1 christos }
179 1.1 christos }
180 1.1 christos
181 1.1 christos int
182 1.1 christos getutentries(const char *fname, struct utmpentry **epp)
183 1.1 christos {
184 1.1 christos #ifdef SUPPORT_UTMPX
185 1.1 christos struct utmpx *utx;
186 1.1 christos #endif
187 1.1 christos #ifdef SUPPORT_UTMP
188 1.1 christos struct utmp *ut;
189 1.1 christos #endif
190 1.3 christos #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
191 1.2 christos struct utmpentry *ep;
192 1.1 christos int what = setup(fname);
193 1.1 christos struct utmpentry **nextp = &ehead;
194 1.5 christos switch (what) {
195 1.5 christos case 0:
196 1.5 christos /* No updates */
197 1.1 christos *epp = ehead;
198 1.1 christos return numutmp;
199 1.5 christos default:
200 1.5 christos /* Need to re-scan */
201 1.1 christos ehead = NULL;
202 1.1 christos numutmp = 0;
203 1.1 christos }
204 1.3 christos #endif
205 1.1 christos
206 1.1 christos #ifdef SUPPORT_UTMPX
207 1.1 christos while ((what & 1) && (utx = getutxent()) != NULL) {
208 1.1 christos if (fname == NULL && utx->ut_type != USER_PROCESS)
209 1.1 christos continue;
210 1.5 christos if ((ep = calloc(1, sizeof(struct utmpentry))) == NULL) {
211 1.5 christos warn(NULL);
212 1.5 christos return 0;
213 1.5 christos }
214 1.1 christos getentryx(ep, utx);
215 1.1 christos *nextp = ep;
216 1.1 christos nextp = &(ep->next);
217 1.1 christos }
218 1.1 christos #endif
219 1.1 christos
220 1.1 christos #ifdef SUPPORT_UTMP
221 1.1 christos while ((what & 2) && (ut = getutent()) != NULL) {
222 1.1 christos if (fname == NULL && (*ut->ut_name == '\0' ||
223 1.1 christos *ut->ut_line == '\0'))
224 1.1 christos continue;
225 1.1 christos /* Don't process entries that we have utmpx for */
226 1.1 christos for (ep = ehead; ep != NULL; ep = ep->next) {
227 1.1 christos if (strncmp(ep->line, ut->ut_line,
228 1.1 christos sizeof(ut->ut_line)) == 0)
229 1.1 christos break;
230 1.1 christos }
231 1.1 christos if (ep != NULL)
232 1.1 christos continue;
233 1.5 christos if ((ep = calloc(1, sizeof(struct utmpentry))) == NULL) {
234 1.5 christos warn(NULL);
235 1.5 christos return 0;
236 1.5 christos }
237 1.1 christos getentry(ep, ut);
238 1.1 christos *nextp = ep;
239 1.1 christos nextp = &(ep->next);
240 1.1 christos }
241 1.1 christos #endif
242 1.1 christos numutmp = 0;
243 1.1 christos #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
244 1.1 christos if (ehead != NULL) {
245 1.1 christos struct utmpentry *from = ehead, *save;
246 1.1 christos
247 1.1 christos ehead = NULL;
248 1.1 christos while (from != NULL) {
249 1.1 christos for (nextp = &ehead;
250 1.1 christos (*nextp) && strcmp(from->line, (*nextp)->line) > 0;
251 1.1 christos nextp = &(*nextp)->next)
252 1.1 christos continue;
253 1.1 christos save = from;
254 1.1 christos from = from->next;
255 1.1 christos save->next = *nextp;
256 1.1 christos *nextp = save;
257 1.1 christos numutmp++;
258 1.1 christos }
259 1.1 christos }
260 1.1 christos *epp = ehead;
261 1.1 christos return numutmp;
262 1.3 christos #else
263 1.3 christos *epp = NULL;
264 1.3 christos return 0;
265 1.3 christos #endif
266 1.1 christos }
267 1.1 christos
268 1.1 christos #ifdef SUPPORT_UTMP
269 1.1 christos static void
270 1.1 christos getentry(struct utmpentry *e, struct utmp *up)
271 1.1 christos {
272 1.1 christos (void)strncpy(e->name, up->ut_name, sizeof(up->ut_name));
273 1.1 christos e->name[sizeof(e->name) - 1] = '\0';
274 1.1 christos (void)strncpy(e->line, up->ut_line, sizeof(up->ut_line));
275 1.1 christos e->line[sizeof(e->line) - 1] = '\0';
276 1.1 christos (void)strncpy(e->host, up->ut_host, sizeof(up->ut_host));
277 1.7 elad e->name[sizeof(e->name) - 1] = '\0';
278 1.1 christos e->tv.tv_sec = up->ut_time;
279 1.1 christos e->tv.tv_usec = 0;
280 1.1 christos adjust_size(e);
281 1.1 christos }
282 1.1 christos #endif
283 1.1 christos
284 1.1 christos #ifdef SUPPORT_UTMPX
285 1.1 christos static void
286 1.1 christos getentryx(struct utmpentry *e, struct utmpx *up)
287 1.1 christos {
288 1.1 christos (void)strncpy(e->name, up->ut_name, sizeof(up->ut_name));
289 1.1 christos e->name[sizeof(e->name) - 1] = '\0';
290 1.1 christos (void)strncpy(e->line, up->ut_line, sizeof(up->ut_line));
291 1.1 christos e->line[sizeof(e->line) - 1] = '\0';
292 1.1 christos (void)strncpy(e->host, up->ut_host, sizeof(up->ut_host));
293 1.6 elad e->name[sizeof(e->name) - 1] = '\0';
294 1.1 christos e->tv = up->ut_tv;
295 1.1 christos adjust_size(e);
296 1.1 christos }
297 1.1 christos #endif
298