]>
git.saurik.com Git - apple/shell_cmds.git/blob - who/utmpentry.c
1 /* $NetBSD: utmpentry.c,v 1.15 2008/07/13 20:07:48 dholland 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.15 2008/07/13 20:07:48 dholland Exp $");
45 #endif /* __APPLE__ */
54 #include "utmpentry.h"
57 #define timespecclear(tsp) (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
58 #define timespeccmp(tsp, usp, cmp) \
59 (((tsp)->tv_sec == (usp)->tv_sec) ? \
60 ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
61 ((tsp)->tv_sec cmp (usp)->tv_sec))
62 #endif /* __APPLE__ */
64 /* Fail the compile if x is not true, by constructing an illegal type. */
65 #define COMPILE_ASSERT(x) ((void)sizeof(struct { unsigned : ((x) ? 1 : -1); }))
69 static void getentry(struct utmpentry
*, struct utmp
*);
70 static struct timespec utmptime
= {0, 0};
73 static void getentryx(struct utmpentry
*, struct utmpx
*);
74 static struct timespec utmpxtime
= {0, 0};
76 #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
77 static int setup(const char *);
78 static void adjust_size(struct utmpentry
*e
);
81 int maxname
= 8, maxline
= 8, maxhost
= 16;
82 int etype
= 1 << USER_PROCESS
;
83 static int numutmp
= 0;
84 static struct utmpentry
*ehead
;
86 #if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
88 adjust_size(struct utmpentry
*e
)
92 if ((max
= strlen(e
->name
)) > maxname
)
94 if ((max
= strlen(e
->line
)) > maxline
)
96 if ((max
= strlen(e
->host
)) > maxhost
)
101 setup(const char *fname
)
115 size_t len
= strlen(fname
);
117 errx(1, "Filename cannot be 0 length.");
120 #else /* !__APPLE__ */
121 what
= fname
[len
- 1] == 'x' ? 1 : 2;
122 #endif /* __APPLE__ */
125 if (utmpxname(fname
) == 0)
126 warnx("Cannot set utmpx file to `%s'",
129 warnx("utmpx support not compiled in");
133 if (utmpname(fname
) == 0)
134 warnx("Cannot set utmp file to `%s'",
137 warnx("utmp support not compiled in");
143 sfname
= fname
? fname
: _PATH_UTMPX
;
144 if (stat(sfname
, &st
) == -1) {
145 warn("Cannot stat `%s'", sfname
);
148 if (timespeccmp(&st
.st_mtimespec
, &utmpxtime
, >))
149 utmpxtime
= st
.st_mtimespec
;
157 sfname
= fname
? fname
: _PATH_UTMP
;
158 if (stat(sfname
, &st
) == -1) {
159 warn("Cannot stat `%s'", sfname
);
162 if (timespeccmp(&st
.st_mtimespec
, &utmptime
, >))
163 utmptime
= st
.st_mtimespec
;
176 struct utmpentry
*ep
;
179 timespecclear(&utmptime
);
182 timespecclear(&utmpxtime
);
186 struct utmpentry
*sep
= ep
;
195 getutentries(const char *fname
, struct utmpentry
**epp
)
203 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
204 struct utmpentry
*ep
;
205 int what
= setup(fname
);
206 struct utmpentry
**nextp
= &ehead
;
213 /* Need to re-scan */
220 while ((what
& 1) && (utx
= getutxent()) != NULL
) {
222 if (((1 << utx
->ut_type
) & etype
) == 0)
223 #else /* !__APPLE__ */
224 if (fname
== NULL
&& ((1 << utx
->ut_type
) & etype
) == 0)
225 #endif /* __APPLE__ */
227 if ((ep
= calloc(1, sizeof(struct utmpentry
))) == NULL
) {
238 if ((etype
& (1 << USER_PROCESS
)) != 0) {
239 while ((what
& 2) && (ut
= getutent()) != NULL
) {
240 if (fname
== NULL
&& (*ut
->ut_name
== '\0' ||
241 *ut
->ut_line
== '\0'))
243 /* Don't process entries that we have utmpx for */
244 for (ep
= ehead
; ep
!= NULL
; ep
= ep
->next
) {
245 if (strncmp(ep
->line
, ut
->ut_line
,
246 sizeof(ut
->ut_line
)) == 0)
251 if ((ep
= calloc(1, sizeof(*ep
))) == NULL
) {
262 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
264 struct utmpentry
*from
= ehead
, *save
;
267 while (from
!= NULL
) {
269 (*nextp
) && strcmp(from
->line
, (*nextp
)->line
) > 0;
270 nextp
= &(*nextp
)->next
)
289 getentry(struct utmpentry
*e
, struct utmp
*up
)
291 COMPILE_ASSERT(sizeof(e
->name
) > sizeof(up
->ut_name
));
292 COMPILE_ASSERT(sizeof(e
->line
) > sizeof(up
->ut_line
));
293 COMPILE_ASSERT(sizeof(e
->host
) > sizeof(up
->ut_host
));
296 * e has just been calloc'd. We don't need to clear it or
297 * append null-terminators, because its length is strictly
298 * greater than the source string. Use strncpy to _read_
299 * up->ut_* because they may not be terminated. For this
300 * reason we use the size of the _source_ as the length
303 (void)strncpy(e
->name
, up
->ut_name
, sizeof(up
->ut_name
));
304 (void)strncpy(e
->line
, up
->ut_line
, sizeof(up
->ut_line
));
305 (void)strncpy(e
->host
, up
->ut_host
, sizeof(up
->ut_host
));
307 e
->tv
.tv_sec
= up
->ut_time
;
313 e
->type
= USER_PROCESS
;
320 getentryx(struct utmpentry
*e
, struct utmpx
*up
)
322 COMPILE_ASSERT(sizeof(e
->name
) > sizeof(up
->ut_name
));
323 COMPILE_ASSERT(sizeof(e
->line
) > sizeof(up
->ut_line
));
324 COMPILE_ASSERT(sizeof(e
->host
) > sizeof(up
->ut_host
));
327 * e has just been calloc'd. We don't need to clear it or
328 * append null-terminators, because its length is strictly
329 * greater than the source string. Use strncpy to _read_
330 * up->ut_* because they may not be terminated. For this
331 * reason we use the size of the _source_ as the length
334 (void)strncpy(e
->name
, up
->ut_name
, sizeof(up
->ut_name
));
335 (void)strncpy(e
->line
, up
->ut_line
, sizeof(up
->ut_line
));
336 (void)strncpy(e
->host
, up
->ut_host
, sizeof(up
->ut_host
));
341 e
->term
= up
->ut_exit
.e_termination
;
342 e
->exit
= up
->ut_exit
.e_exit
;
343 e
->sess
= up
->ut_session
;
344 #endif /* !__APPLE__ */
345 e
->type
= up
->ut_type
;