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>
34 __RCSID("$NetBSD: utmpentry.c,v 1.16 2008/10/28 14:01:46 christos Exp $");
51 #include "utmpentry.h"
54 /* Fail the compile if x is not true, by constructing an illegal type. */
55 #define COMPILE_ASSERT(x) /*LINTED null effect */ \
56 ((void)sizeof(struct { unsigned : ((x) ? 1 : -1); }))
60 static void getentry(struct utmpentry
*, struct utmp
*);
61 static struct timespec utmptime
= {0, 0};
64 static void getentryx(struct utmpentry
*, struct utmpx
*);
65 static struct timespec utmpxtime
= {0, 0};
67 #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
68 static int setup(const char *);
69 static void adjust_size(struct utmpentry
*e
);
72 int maxname
= 8, maxline
= 8, maxhost
= 16;
73 int etype
= 1 << USER_PROCESS
;
74 static int numutmp
= 0;
75 static struct utmpentry
*ehead
;
77 #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
79 adjust_size(struct utmpentry
*e
)
83 if ((max
= strlen(e
->name
)) > maxname
)
85 if ((max
= strlen(e
->line
)) > maxline
)
87 if ((max
= strlen(e
->host
)) > maxhost
)
92 setup(const char *fname
)
106 size_t len
= strlen(fname
);
108 errx(1, "Filename cannot be 0 length.");
109 what
= fname
[len
- 1] == 'x' ? 1 : 2;
112 if (utmpxname(fname
) == 0)
113 warnx("Cannot set utmpx file to `%s'",
116 warnx("utmpx support not compiled in");
120 if (utmpname(fname
) == 0)
121 warnx("Cannot set utmp file to `%s'",
124 warnx("utmp support not compiled in");
130 sfname
= fname
? fname
: _PATH_UTMPX
;
131 if (stat(sfname
, &st
) == -1) {
132 warn("Cannot stat `%s'", sfname
);
135 if (timespeccmp(&st
.st_mtimespec
, &utmpxtime
, >))
136 utmpxtime
= st
.st_mtimespec
;
144 sfname
= fname
? fname
: _PATH_UTMP
;
145 if (stat(sfname
, &st
) == -1) {
146 warn("Cannot stat `%s'", sfname
);
149 if (timespeccmp(&st
.st_mtimespec
, &utmptime
, >))
150 utmptime
= st
.st_mtimespec
;
163 struct utmpentry
*ep
;
166 timespecclear(&utmptime
);
169 timespecclear(&utmpxtime
);
173 struct utmpentry
*sep
= ep
;
182 getutentries(const char *fname
, struct utmpentry
**epp
)
190 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
191 struct utmpentry
*ep
;
192 int what
= setup(fname
);
193 struct utmpentry
**nextp
= &ehead
;
200 /* Need to re-scan */
207 while ((what
& 1) && (utx
= getutxent()) != NULL
) {
208 if (fname
== NULL
&& ((1 << utx
->ut_type
) & etype
) == 0)
210 if ((ep
= calloc(1, sizeof(struct utmpentry
))) == NULL
) {
221 if ((etype
& (1 << USER_PROCESS
)) != 0) {
222 while ((what
& 2) && (ut
= getutent()) != NULL
) {
223 if (fname
== NULL
&& (*ut
->ut_name
== '\0' ||
224 *ut
->ut_line
== '\0'))
226 /* Don't process entries that we have utmpx for */
227 for (ep
= ehead
; ep
!= NULL
; ep
= ep
->next
) {
228 if (strncmp(ep
->line
, ut
->ut_line
,
229 sizeof(ut
->ut_line
)) == 0)
234 if ((ep
= calloc(1, sizeof(*ep
))) == NULL
) {
245 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
247 struct utmpentry
*from
= ehead
, *save
;
250 while (from
!= NULL
) {
252 (*nextp
) && strcmp(from
->line
, (*nextp
)->line
) > 0;
253 nextp
= &(*nextp
)->next
)
272 getentry(struct utmpentry
*e
, struct utmp
*up
)
274 COMPILE_ASSERT(sizeof(e
->name
) > sizeof(up
->ut_name
));
275 COMPILE_ASSERT(sizeof(e
->line
) > sizeof(up
->ut_line
));
276 COMPILE_ASSERT(sizeof(e
->host
) > sizeof(up
->ut_host
));
279 * e has just been calloc'd. We don't need to clear it or
280 * append null-terminators, because its length is strictly
281 * greater than the source string. Use strncpy to _read_
282 * up->ut_* because they may not be terminated. For this
283 * reason we use the size of the _source_ as the length
286 (void)strncpy(e
->name
, up
->ut_name
, sizeof(up
->ut_name
));
287 (void)strncpy(e
->line
, up
->ut_line
, sizeof(up
->ut_line
));
288 (void)strncpy(e
->host
, up
->ut_host
, sizeof(up
->ut_host
));
290 e
->tv
.tv_sec
= up
->ut_time
;
296 e
->type
= USER_PROCESS
;
303 getentryx(struct utmpentry
*e
, struct utmpx
*up
)
305 COMPILE_ASSERT(sizeof(e
->name
) > sizeof(up
->ut_name
));
306 COMPILE_ASSERT(sizeof(e
->line
) > sizeof(up
->ut_line
));
307 COMPILE_ASSERT(sizeof(e
->host
) > sizeof(up
->ut_host
));
310 * e has just been calloc'd. We don't need to clear it or
311 * append null-terminators, because its length is strictly
312 * greater than the source string. Use strncpy to _read_
313 * up->ut_* because they may not be terminated. For this
314 * reason we use the size of the _source_ as the length
317 (void)strncpy(e
->name
, up
->ut_name
, sizeof(up
->ut_name
));
318 (void)strncpy(e
->line
, up
->ut_line
, sizeof(up
->ut_line
));
319 (void)strncpy(e
->host
, up
->ut_host
, sizeof(up
->ut_host
));
323 e
->term
= up
->ut_exit
.e_termination
;
324 e
->exit
= up
->ut_exit
.e_exit
;
325 e
->sess
= up
->ut_session
;
326 e
->type
= up
->ut_type
;