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"
41 static FILE *uf = NULL;
45 setutxdb(int db, const char *file)
52 file = _PATH_UTX_ACTIVE;
56 file = _PATH_UTX_LASTLOGIN;
69 uf = fopen(file, "r");
73 if (db != UTXDB_LOG) {
74 /* Safety check: never use broken files. */
75 if (_fstat(fileno(uf), &sb) != -1 &&
76 sb.st_size % sizeof(struct futx) != 0) {
82 /* Prevent reading of partial records. */
83 (void)setvbuf(uf, NULL, _IOFBF,
84 rounddown(BUFSIZ, sizeof(struct futx)));
95 setutxdb(UTXDB_ACTIVE, NULL);
109 getfutxent(struct futx *fu)
117 if (udb == UTXDB_LOG) {
120 if (fread(&len, sizeof(len), 1, uf) != 1)
123 if (len > sizeof *fu) {
124 /* Forward compatibility. */
125 if (fread(fu, sizeof(*fu), 1, uf) != 1)
127 fseek(uf, len - sizeof(*fu), SEEK_CUR);
129 /* Partial record. */
130 memset(fu, 0, sizeof(*fu));
131 if (fread(fu, len, 1, uf) != 1)
135 if (fread(fu, sizeof(*fu), 1, uf) != 1)
146 if (getfutxent(&fu) != 0)
148 return (futx_to_utx(&fu));
152 getutxid(const struct utmpx *id)
157 if (getfutxent(&fu) != 0)
160 switch (fu.fu_type) {
165 switch (id->ut_type) {
170 if (memcmp(fu.fu_id, id->ut_id,
171 MIN(sizeof(fu.fu_id), sizeof(id->ut_id))) ==
177 if (fu.fu_type == id->ut_type)
184 return (futx_to_utx(&fu));
188 getutxline(const struct utmpx *line)
193 if (getfutxent(&fu) != 0)
196 switch (fu.fu_type) {
199 if (strncmp(fu.fu_line, line->ut_line,
200 MIN(sizeof(fu.fu_line), sizeof(line->ut_line))) ==
208 return (futx_to_utx(&fu));
212 getutxuser(const char *user)
217 if (getfutxent(&fu) != 0)
220 switch (fu.fu_type) {
222 if (strncmp(fu.fu_user, user, sizeof(fu.fu_user)) == 0)
229 return (futx_to_utx(&fu));