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>
41 #include "un-namespace.h"
44 futx_open(const char *file)
50 fd = _open(file, O_CREAT|O_RDWR|O_EXLOCK|O_CLOEXEC, 0644);
54 /* Safety check: never use broken files. */
55 if (_fstat(fd, &sb) != -1 && sb.st_size % sizeof(struct futx) != 0) {
61 fp = fdopen(fd, "r+");
70 utx_active_add(const struct futx *fu)
81 * Register user login sessions. Overwrite entries of sessions
82 * that have already been terminated.
84 fp = futx_open(_PATH_UTX_ACTIVE);
87 while (fread(&fe, sizeof(fe), 1, fp) == 1) {
90 /* Leave these intact. */
96 /* Overwrite when ut_id matches. */
97 if (memcmp(fu->fu_id, fe.fu_id, sizeof(fe.fu_id)) ==
99 ret = fseeko(fp, -(off_t)sizeof(fe), SEEK_CUR);
102 if (fe.fu_type != DEAD_PROCESS)
106 /* Allow us to overwrite unused records. */
108 partial = ftello(fp);
110 * Distinguish errors from valid values so we
111 * don't overwrite good data by accident.
114 partial -= (off_t)sizeof(fe);
121 * No exact match found. Use the partial match. If no partial
122 * match was found, just append a new record.
125 ret = fseeko(fp, partial, SEEK_SET);
129 else if (fwrite(fu, sizeof(*fu), 1, fp) < 1)
135 return (error == 0 ? 0 : 1);
139 utx_active_remove(struct futx *fu)
146 * Remove user login sessions, having the same ut_id.
148 fp = futx_open(_PATH_UTX_ACTIVE);
153 while (fread(&fe, sizeof(fe), 1, fp) == 1 && ret != 0)
154 switch (fe.fu_type) {
158 if (memcmp(fu->fu_id, fe.fu_id, sizeof(fe.fu_id)) != 0)
161 /* Terminate session. */
162 if (fseeko(fp, -(off_t)sizeof(fe), SEEK_CUR) == -1)
164 else if (fwrite(fu, sizeof(*fu), 1, fp) < 1)
177 utx_active_init(const struct futx *fu)
181 /* Initialize utx.active with a single BOOT_TIME record. */
182 fd = _open(_PATH_UTX_ACTIVE, O_CREAT|O_RDWR|O_TRUNC, 0644);
185 _write(fd, fu, sizeof(*fu));
190 utx_active_purge(void)
193 truncate(_PATH_UTX_ACTIVE, 0);
197 utx_lastlogin_add(const struct futx *fu)
206 * Write an entry to lastlogin. Overwrite the entry if the
207 * current user already has an entry. If not, append a new
210 fp = futx_open(_PATH_UTX_LASTLOGIN);
213 while (fread(&fe, sizeof fe, 1, fp) == 1) {
214 if (strncmp(fu->fu_user, fe.fu_user, sizeof fe.fu_user) != 0)
217 /* Found a previous lastlogin entry for this user. */
218 ret = fseeko(fp, -(off_t)sizeof fe, SEEK_CUR);
223 else if (fwrite(fu, sizeof *fu, 1, fp) < 1) {
233 utx_lastlogin_upgrade(void)
238 fd = _open(_PATH_UTX_LASTLOGIN, O_RDWR|O_CLOEXEC, 0644);
243 * Truncate broken lastlogin files. In the future we should
244 * check for older versions of the file format here and try to
247 if (_fstat(fd, &sb) != -1 && sb.st_size % sizeof(struct futx) != 0)
253 utx_log_add(const struct futx *fu)
260 * Append an entry to the log file. We only need to append
261 * records to this file, so to conserve space, trim any trailing
262 * zero-bytes. Prepend a length field, indicating the length of
263 * the record, excluding the length field itself.
265 for (l = sizeof(*fu); l > 0 && ((const char *)fu)[l - 1] == '\0'; l--) ;
266 vec[0].iov_base = &l;
267 vec[0].iov_len = sizeof(l);
268 vec[1].iov_base = __DECONST(void *, fu);
272 fd = _open(_PATH_UTX_LOG, O_CREAT|O_WRONLY|O_APPEND|O_CLOEXEC, 0644);
275 if (_writev(fd, vec, 2) == -1)
281 return (error == 0 ? 0 : 1);
285 pututxline(const struct utmpx *utmpx)
292 utx_to_futx(utmpx, &fu);
294 switch (fu.fu_type) {
296 utx_active_init(&fu);
297 utx_lastlogin_upgrade();
306 bad |= utx_active_add(&fu);
307 bad |= utx_lastlogin_add(&fu);
309 #if 0 /* XXX: Are these records of any use to us? */
312 bad |= utx_active_add(&fu);
317 * In case writing a logout entry fails, never attempt
318 * to write it to utx.log. The logout entry's ut_id
321 if (utx_active_remove(&fu) != 0)
329 bad |= utx_log_add(&fu);
330 return (bad ? NULL : futx_to_utx(&fu));