2 * Copyright (c) 2010 Ed Schouten <ed@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "namespace.h"
31 #include <sys/endian.h>
32 #include <sys/param.h>
39 #include "un-namespace.h"
42 static FILE *uf = NULL;
45 static _Thread_local FILE *uf = NULL;
46 static _Thread_local int udb;
50 setutxdb(int db, const char *file)
57 file = _PATH_UTX_ACTIVE;
61 file = _PATH_UTX_LASTLOGIN;
74 uf = fopen(file, "re");
78 if (db != UTXDB_LOG) {
79 /* Safety check: never use broken files. */
80 if (_fstat(fileno(uf), &sb) != -1 &&
81 sb.st_size % sizeof(struct futx) != 0) {
87 /* Prevent reading of partial records. */
88 (void)setvbuf(uf, NULL, _IOFBF,
89 rounddown(BUFSIZ, sizeof(struct futx)));
100 setutxdb(UTXDB_ACTIVE, NULL);
114 getfutxent(struct futx *fu)
122 if (udb == UTXDB_LOG) {
126 if (fread(&len, sizeof(len), 1, uf) != 1)
131 * XXX: Though zero-size records are valid in theory,
132 * they can never occur in practice. Zero-size records
133 * indicate file corruption. Seek one byte forward, to
134 * see if we can find a record there.
139 if (len > sizeof *fu) {
140 /* Forward compatibility. */
141 if (fread(fu, sizeof(*fu), 1, uf) != 1)
143 fseek(uf, len - sizeof(*fu), SEEK_CUR);
145 /* Partial record. */
146 memset(fu, 0, sizeof(*fu));
147 if (fread(fu, len, 1, uf) != 1)
151 if (fread(fu, sizeof(*fu), 1, uf) != 1)
162 if (getfutxent(&fu) != 0)
164 return (futx_to_utx(&fu));
168 getutxid(const struct utmpx *id)
173 if (getfutxent(&fu) != 0)
176 switch (fu.fu_type) {
181 switch (id->ut_type) {
186 if (memcmp(fu.fu_id, id->ut_id,
187 MIN(sizeof(fu.fu_id), sizeof(id->ut_id))) ==
193 if (fu.fu_type == id->ut_type)
200 return (futx_to_utx(&fu));
204 getutxline(const struct utmpx *line)
209 if (getfutxent(&fu) != 0)
212 switch (fu.fu_type) {
215 if (strncmp(fu.fu_line, line->ut_line,
216 MIN(sizeof(fu.fu_line), sizeof(line->ut_line))) ==
224 return (futx_to_utx(&fu));
228 getutxuser(const char *user)
233 if (getfutxent(&fu) != 0)
236 switch (fu.fu_type) {
238 if (strncmp(fu.fu_user, user, sizeof(fu.fu_user)) == 0)
245 return (futx_to_utx(&fu));