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>
38 #include "un-namespace.h"
40 #define UTOF_STRING(ut, fu, field) do { \
41 strncpy((fu)->fu_ ## field, (ut)->ut_ ## field, \
42 MIN(sizeof (fu)->fu_ ## field, sizeof (ut)->ut_ ## field)); \
44 #define UTOF_ID(ut, fu) do { \
45 memcpy((fu)->fu_id, (ut)->ut_id, \
46 MIN(sizeof (fu)->fu_id, sizeof (ut)->ut_id)); \
48 #define UTOF_PID(ut, fu) do { \
49 (fu)->fu_pid = htobe32((ut)->ut_pid); \
51 #define UTOF_TYPE(ut, fu) do { \
52 (fu)->fu_type = (ut)->ut_type; \
54 #define UTOF_TV(fu) do { \
56 gettimeofday(&tv, NULL); \
57 (fu)->fu_tv = htobe64((uint64_t)tv.tv_sec * 1000000 + \
58 (uint64_t)tv.tv_usec); \
62 utx_to_futx(const struct utmpx *ut, struct futx *fu)
65 memset(fu, 0, sizeof *fu);
67 switch (ut->ut_type) {
71 /* Extension: shutdown time. */
76 UTOF_STRING(ut, fu, user);
77 UTOF_STRING(ut, fu, line);
78 /* Extension: host name. */
79 UTOF_STRING(ut, fu, host);
88 UTOF_STRING(ut, fu, user);
89 UTOF_STRING(ut, fu, line);
105 #define FTOU_STRING(fu, ut, field) do { \
106 strncpy((ut)->ut_ ## field, (fu)->fu_ ## field, \
107 MIN(sizeof (ut)->ut_ ## field - 1, sizeof (fu)->fu_ ## field)); \
109 #define FTOU_ID(fu, ut) do { \
110 memcpy((ut)->ut_id, (fu)->fu_id, \
111 MIN(sizeof (ut)->ut_id, sizeof (fu)->fu_id)); \
113 #define FTOU_PID(fu, ut) do { \
114 (ut)->ut_pid = be32toh((fu)->fu_pid); \
116 #define FTOU_TYPE(fu, ut) do { \
117 (ut)->ut_type = (fu)->fu_type; \
119 #define FTOU_TV(fu, ut) do { \
121 t = be64toh((fu)->fu_tv); \
122 (ut)->ut_tv.tv_sec = t / 1000000; \
123 (ut)->ut_tv.tv_usec = t % 1000000; \
127 futx_to_utx(const struct futx *fu)
130 static struct utmpx *ut;
132 static _Thread_local struct utmpx *ut;
136 ut = calloc(1, sizeof *ut);
140 memset(ut, 0, sizeof *ut);
142 switch (fu->fu_type) {
146 /* Extension: shutdown time. */
151 FTOU_STRING(fu, ut, user);
152 FTOU_STRING(fu, ut, line);
153 /* Extension: host name. */
154 FTOU_STRING(fu, ut, host);
163 FTOU_STRING(fu, ut, user);
164 FTOU_STRING(fu, ut, line);