1 /* $NetBSD: utmpentry.c,v 1.16 2008/10/28 14:01:46 christos Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
48 #include "utmpentry.h"
50 /* Operations on timespecs. */
51 #define timespecclear(tsp) (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
52 #define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
53 #define timespeccmp(tsp, usp, cmp) \
54 (((tsp)->tv_sec == (usp)->tv_sec) ? \
55 ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
56 ((tsp)->tv_sec cmp (usp)->tv_sec))
57 #define timespecadd(tsp, usp, vsp) \
59 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
60 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
61 if ((vsp)->tv_nsec >= 1000000000L) { \
63 (vsp)->tv_nsec -= 1000000000L; \
65 } while (/* CONSTCOND */ 0)
66 #define timespecsub(tsp, usp, vsp) \
68 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
69 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
70 if ((vsp)->tv_nsec < 0) { \
72 (vsp)->tv_nsec += 1000000000L; \
74 } while (/* CONSTCOND */ 0)
75 #define timespec2ns(x) (((uint64_t)(x)->tv_sec) * 1000000000L + (x)->tv_nsec)
78 /* Fail the compile if x is not true, by constructing an illegal type. */
79 #define COMPILE_ASSERT(x) /*LINTED null effect */ \
80 ((void)sizeof(struct { unsigned : ((x) ? 1 : -1); }))
84 static void getentry(struct utmpentry
*, struct utmp
*);
85 static struct timespec utmptime
= {0, 0};
88 static void getentryx(struct utmpentry
*, struct utmpx
*);
89 static struct timespec utmpxtime
= {0, 0};
91 #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
92 static int setup(const char *);
93 static void adjust_size(struct utmpentry
*e
);
96 int maxname
= 8, maxline
= 8, maxhost
= 16;
97 int etype
= 1 << USER_PROCESS
;
98 static int numutmp
= 0;
99 static struct utmpentry
*ehead
;
101 #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
103 adjust_size(struct utmpentry
*e
)
107 if ((max
= strlen(e
->name
)) > maxname
)
109 if ((max
= strlen(e
->line
)) > maxline
)
111 if ((max
= strlen(e
->host
)) > maxhost
)
116 setup(const char *fname
)
130 size_t len
= strlen(fname
);
132 errx(1, "Filename cannot be 0 length.");
133 what
= fname
[len
- 1] == 'x' ? 1 : 2;
136 if (utmpxname(fname
) == 0)
137 warnx("Cannot set utmpx file to `%s'",
140 warnx("utmpx support not compiled in");
144 if (utmpname(fname
) == 0)
145 warnx("Cannot set utmp file to `%s'",
148 warnx("utmp support not compiled in");
154 sfname
= fname
? fname
: _PATH_UTMPX
;
155 if (stat(sfname
, &st
) == -1) {
156 warn("Cannot stat `%s'", sfname
);
159 if (timespeccmp(&st
.st_mtimespec
, &utmpxtime
, >))
160 utmpxtime
= st
.st_mtimespec
;
168 sfname
= fname
? fname
: _PATH_UTMP
;
169 if (stat(sfname
, &st
) == -1) {
170 warn("Cannot stat `%s'", sfname
);
173 if (timespeccmp(&st
.st_mtimespec
, &utmptime
, >))
174 utmptime
= st
.st_mtimespec
;
187 struct utmpentry
*ep
;
190 timespecclear(&utmptime
);
193 timespecclear(&utmpxtime
);
197 struct utmpentry
*sep
= ep
;
206 getutentries(const char *fname
, struct utmpentry
**epp
)
214 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
215 struct utmpentry
*ep
;
216 int what
= setup(fname
);
217 struct utmpentry
**nextp
= &ehead
;
224 /* Need to re-scan */
231 while ((what
& 1) && (utx
= getutxent()) != NULL
) {
232 if (fname
== NULL
&& ((1 << utx
->ut_type
) & etype
) == 0) {
235 if ((ep
= calloc(1, sizeof(struct utmpentry
))) == NULL
) {
246 if ((etype
& (1 << USER_PROCESS
)) != 0) {
247 while ((what
& 2) && (ut
= getutent()) != NULL
) {
248 if (fname
== NULL
&& (*ut
->ut_name
== '\0' ||
249 *ut
->ut_line
== '\0'))
251 /* Don't process entries that we have utmpx for */
252 for (ep
= ehead
; ep
!= NULL
; ep
= ep
->next
) {
253 if (strncmp(ep
->line
, ut
->ut_line
,
254 sizeof(ut
->ut_line
)) == 0)
259 if ((ep
= calloc(1, sizeof(*ep
))) == NULL
) {
270 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
272 struct utmpentry
*from
= ehead
, *save
;
275 while (from
!= NULL
) {
277 (*nextp
) && strcmp(from
->line
, (*nextp
)->line
) > 0;
278 nextp
= &(*nextp
)->next
)
297 getentry(struct utmpentry
*e
, struct utmp
*up
)
300 COMPILE_ASSERT(sizeof(e
->name
) > sizeof(up
->ut_name
));
301 COMPILE_ASSERT(sizeof(e
->line
) > sizeof(up
->ut_line
));
302 COMPILE_ASSERT(sizeof(e
->host
) > sizeof(up
->ut_host
));
306 * e has just been calloc'd. We don't need to clear it or
307 * append null-terminators, because its length is strictly
308 * greater than the source string. Use strncpy to _read_
309 * up->ut_* because they may not be terminated. For this
310 * reason we use the size of the _source_ as the length
313 (void)strncpy(e
->name
, up
->ut_name
, sizeof(up
->ut_name
));
314 (void)strncpy(e
->line
, up
->ut_line
, sizeof(up
->ut_line
));
315 (void)strncpy(e
->host
, up
->ut_host
, sizeof(up
->ut_host
));
317 e
->tv
.tv_sec
= up
->ut_time
;
323 e
->type
= USER_PROCESS
;
330 getentryx(struct utmpentry
*e
, struct utmpx
*up
)
333 COMPILE_ASSERT(sizeof(e
->name
) > sizeof(up
->ut_name
));
334 COMPILE_ASSERT(sizeof(e
->line
) > sizeof(up
->ut_line
));
335 COMPILE_ASSERT(sizeof(e
->host
) > sizeof(up
->ut_host
));
339 * e has just been calloc'd. We don't need to clear it or
340 * append null-terminators, because its length is strictly
341 * greater than the source string. Use strncpy to _read_
342 * up->ut_* because they may not be terminated. For this
343 * reason we use the size of the _source_ as the length
346 (void)strncpy(e
->name
, up
->ut_name
, sizeof(up
->ut_name
));
347 (void)strncpy(e
->line
, up
->ut_line
, sizeof(up
->ut_line
));
348 (void)strncpy(e
->host
, up
->ut_host
, sizeof(up
->ut_host
));
352 e
->term
= up
->ut_exit
.e_termination
;
353 e
->exit
= up
->ut_exit
.e_exit
;
354 e
->sess
= up
->ut_session
;
355 e
->type
= up
->ut_type
;