2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Ed Schouten <ed@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "namespace.h"
33 #include <sys/endian.h>
43 #include "un-namespace.h"
46 futx_open(const char *file)
52 fd = _open(file, O_CREAT|O_RDWR|O_EXLOCK|O_CLOEXEC, 0644);
56 /* Safety check: never use broken files. */
57 if (_fstat(fd, &sb) != -1 && sb.st_size % sizeof(struct futx) != 0) {
63 fp = fdopen(fd, "r+");
72 utx_active_add(const struct futx *fu)
83 * Register user login sessions. Overwrite entries of sessions
84 * that have already been terminated.
86 fp = futx_open(_PATH_UTX_ACTIVE);
89 while (fread(&fe, sizeof(fe), 1, fp) == 1) {
92 /* Leave these intact. */
98 /* Overwrite when ut_id matches. */
99 if (memcmp(fu->fu_id, fe.fu_id, sizeof(fe.fu_id)) ==
101 ret = fseeko(fp, -(off_t)sizeof(fe), SEEK_CUR);
104 if (fe.fu_type != DEAD_PROCESS)
108 /* Allow us to overwrite unused records. */
110 partial = ftello(fp);
112 * Distinguish errors from valid values so we
113 * don't overwrite good data by accident.
116 partial -= (off_t)sizeof(fe);
123 * No exact match found. Use the partial match. If no partial
124 * match was found, just append a new record.
127 ret = fseeko(fp, partial, SEEK_SET);
131 else if (fwrite(fu, sizeof(*fu), 1, fp) < 1)
138 return (error == 0 ? 0 : 1);
142 utx_active_remove(struct futx *fu)
149 * Remove user login sessions, having the same ut_id.
151 fp = futx_open(_PATH_UTX_ACTIVE);
156 while (fread(&fe, sizeof(fe), 1, fp) == 1 && ret != 0)
157 switch (fe.fu_type) {
161 if (memcmp(fu->fu_id, fe.fu_id, sizeof(fe.fu_id)) != 0)
164 /* Terminate session. */
165 if (fseeko(fp, -(off_t)sizeof(fe), SEEK_CUR) == -1)
167 else if (fwrite(fu, sizeof(*fu), 1, fp) < 1)
181 utx_active_init(const struct futx *fu)
185 /* Initialize utx.active with a single BOOT_TIME record. */
186 fd = _open(_PATH_UTX_ACTIVE, O_CREAT|O_RDWR|O_TRUNC, 0644);
189 _write(fd, fu, sizeof(*fu));
194 utx_active_purge(void)
197 truncate(_PATH_UTX_ACTIVE, 0);
201 utx_lastlogin_add(const struct futx *fu)
210 * Write an entry to lastlogin. Overwrite the entry if the
211 * current user already has an entry. If not, append a new
214 fp = futx_open(_PATH_UTX_LASTLOGIN);
217 while (fread(&fe, sizeof fe, 1, fp) == 1) {
218 if (strncmp(fu->fu_user, fe.fu_user, sizeof fe.fu_user) != 0)
221 /* Found a previous lastlogin entry for this user. */
222 ret = fseeko(fp, -(off_t)sizeof fe, SEEK_CUR);
227 else if (fwrite(fu, sizeof *fu, 1, fp) < 1) {
238 utx_lastlogin_upgrade(void)
243 fd = _open(_PATH_UTX_LASTLOGIN, O_RDWR|O_CLOEXEC, 0644);
248 * Truncate broken lastlogin files. In the future we should
249 * check for older versions of the file format here and try to
252 if (_fstat(fd, &sb) != -1 && sb.st_size % sizeof(struct futx) != 0)
258 utx_log_add(const struct futx *fu)
265 * Append an entry to the log file. We only need to append
266 * records to this file, so to conserve space, trim any trailing
267 * zero-bytes. Prepend a length field, indicating the length of
268 * the record, excluding the length field itself.
270 for (l = sizeof(*fu); l > 0 && ((const char *)fu)[l - 1] == '\0'; l--) ;
271 vec[0].iov_base = &l;
272 vec[0].iov_len = sizeof(l);
273 vec[1].iov_base = __DECONST(void *, fu);
277 fd = _open(_PATH_UTX_LOG, O_CREAT|O_WRONLY|O_APPEND|O_CLOEXEC, 0644);
280 if (_writev(fd, vec, 2) == -1)
287 return (error == 0 ? 0 : 1);
291 pututxline(const struct utmpx *utmpx)
298 utx_to_futx(utmpx, &fu);
300 switch (fu.fu_type) {
302 utx_active_init(&fu);
303 utx_lastlogin_upgrade();
312 bad |= utx_active_add(&fu);
313 bad |= utx_lastlogin_add(&fu);
315 #if 0 /* XXX: Are these records of any use to us? */
318 bad |= utx_active_add(&fu);
323 * In case writing a logout entry fails, never attempt
324 * to write it to utx.log. The logout entry's ut_id
327 if (utx_active_remove(&fu) != 0)
335 bad |= utx_log_add(&fu);
336 return (bad ? NULL : futx_to_utx(&fu));