1a627ac61SEd Schouten /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni *
4a627ac61SEd Schouten * Copyright (c) 2010 Ed Schouten <ed@FreeBSD.org>
5a627ac61SEd Schouten * All rights reserved.
6a627ac61SEd Schouten *
7a627ac61SEd Schouten * Redistribution and use in source and binary forms, with or without
8a627ac61SEd Schouten * modification, are permitted provided that the following conditions
9a627ac61SEd Schouten * are met:
10a627ac61SEd Schouten * 1. Redistributions of source code must retain the above copyright
11a627ac61SEd Schouten * notice, this list of conditions and the following disclaimer.
12a627ac61SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright
13a627ac61SEd Schouten * notice, this list of conditions and the following disclaimer in the
14a627ac61SEd Schouten * documentation and/or other materials provided with the distribution.
15a627ac61SEd Schouten *
16a627ac61SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a627ac61SEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a627ac61SEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a627ac61SEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a627ac61SEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a627ac61SEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a627ac61SEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a627ac61SEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a627ac61SEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a627ac61SEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a627ac61SEd Schouten * SUCH DAMAGE.
27a627ac61SEd Schouten */
28a627ac61SEd Schouten
29a627ac61SEd Schouten #include "namespace.h"
30a627ac61SEd Schouten #include <sys/endian.h>
31a627ac61SEd Schouten #include <sys/param.h>
32a7607816SEd Schouten #include <sys/time.h>
3398c63a48SEd Schouten #include <stdlib.h>
34a627ac61SEd Schouten #include <string.h>
35a627ac61SEd Schouten #include <utmpx.h>
36a627ac61SEd Schouten #include "utxdb.h"
37a627ac61SEd Schouten #include "un-namespace.h"
38a627ac61SEd Schouten
39a627ac61SEd Schouten #define UTOF_STRING(ut, fu, field) do { \
40a627ac61SEd Schouten strncpy((fu)->fu_ ## field, (ut)->ut_ ## field, \
41a627ac61SEd Schouten MIN(sizeof (fu)->fu_ ## field, sizeof (ut)->ut_ ## field)); \
42a627ac61SEd Schouten } while (0)
43a627ac61SEd Schouten #define UTOF_ID(ut, fu) do { \
44a627ac61SEd Schouten memcpy((fu)->fu_id, (ut)->ut_id, \
45a627ac61SEd Schouten MIN(sizeof (fu)->fu_id, sizeof (ut)->ut_id)); \
46a627ac61SEd Schouten } while (0)
47a627ac61SEd Schouten #define UTOF_PID(ut, fu) do { \
48a627ac61SEd Schouten (fu)->fu_pid = htobe32((ut)->ut_pid); \
49a627ac61SEd Schouten } while (0)
50a627ac61SEd Schouten #define UTOF_TYPE(ut, fu) do { \
51a627ac61SEd Schouten (fu)->fu_type = (ut)->ut_type; \
52a627ac61SEd Schouten } while (0)
53a7607816SEd Schouten #define UTOF_TV(fu) do { \
54a7607816SEd Schouten struct timeval tv; \
55a7607816SEd Schouten gettimeofday(&tv, NULL); \
56a7607816SEd Schouten (fu)->fu_tv = htobe64((uint64_t)tv.tv_sec * 1000000 + \
57a7607816SEd Schouten (uint64_t)tv.tv_usec); \
58a627ac61SEd Schouten } while (0)
59a627ac61SEd Schouten
60a627ac61SEd Schouten void
utx_to_futx(const struct utmpx * ut,struct futx * fu)61a627ac61SEd Schouten utx_to_futx(const struct utmpx *ut, struct futx *fu)
62a627ac61SEd Schouten {
63a627ac61SEd Schouten
64a627ac61SEd Schouten memset(fu, 0, sizeof *fu);
65a627ac61SEd Schouten
66a627ac61SEd Schouten switch (ut->ut_type) {
67a627ac61SEd Schouten case BOOT_TIME:
68a627ac61SEd Schouten case OLD_TIME:
69a627ac61SEd Schouten case NEW_TIME:
70a627ac61SEd Schouten /* Extension: shutdown time. */
71a627ac61SEd Schouten case SHUTDOWN_TIME:
72a627ac61SEd Schouten break;
73a627ac61SEd Schouten case USER_PROCESS:
74a627ac61SEd Schouten UTOF_ID(ut, fu);
75a627ac61SEd Schouten UTOF_STRING(ut, fu, user);
76a627ac61SEd Schouten UTOF_STRING(ut, fu, line);
77a627ac61SEd Schouten /* Extension: host name. */
78a627ac61SEd Schouten UTOF_STRING(ut, fu, host);
79a627ac61SEd Schouten UTOF_PID(ut, fu);
80a627ac61SEd Schouten break;
81a627ac61SEd Schouten case INIT_PROCESS:
82a627ac61SEd Schouten UTOF_ID(ut, fu);
83a627ac61SEd Schouten UTOF_PID(ut, fu);
84a627ac61SEd Schouten break;
85a627ac61SEd Schouten case LOGIN_PROCESS:
86a627ac61SEd Schouten UTOF_ID(ut, fu);
87a627ac61SEd Schouten UTOF_STRING(ut, fu, user);
8898c63a48SEd Schouten UTOF_STRING(ut, fu, line);
89a627ac61SEd Schouten UTOF_PID(ut, fu);
90a627ac61SEd Schouten break;
91a627ac61SEd Schouten case DEAD_PROCESS:
92a627ac61SEd Schouten UTOF_ID(ut, fu);
93a627ac61SEd Schouten UTOF_PID(ut, fu);
94a627ac61SEd Schouten break;
95a627ac61SEd Schouten default:
96a627ac61SEd Schouten fu->fu_type = EMPTY;
97a627ac61SEd Schouten return;
98a627ac61SEd Schouten }
99a627ac61SEd Schouten
100a627ac61SEd Schouten UTOF_TYPE(ut, fu);
101a7607816SEd Schouten UTOF_TV(fu);
102a627ac61SEd Schouten }
103a627ac61SEd Schouten
104a627ac61SEd Schouten #define FTOU_STRING(fu, ut, field) do { \
105a627ac61SEd Schouten strncpy((ut)->ut_ ## field, (fu)->fu_ ## field, \
106a627ac61SEd Schouten MIN(sizeof (ut)->ut_ ## field - 1, sizeof (fu)->fu_ ## field)); \
107a627ac61SEd Schouten } while (0)
108a627ac61SEd Schouten #define FTOU_ID(fu, ut) do { \
109a627ac61SEd Schouten memcpy((ut)->ut_id, (fu)->fu_id, \
110a627ac61SEd Schouten MIN(sizeof (ut)->ut_id, sizeof (fu)->fu_id)); \
111a627ac61SEd Schouten } while (0)
112a627ac61SEd Schouten #define FTOU_PID(fu, ut) do { \
113a627ac61SEd Schouten (ut)->ut_pid = be32toh((fu)->fu_pid); \
114a627ac61SEd Schouten } while (0)
115a627ac61SEd Schouten #define FTOU_TYPE(fu, ut) do { \
116a627ac61SEd Schouten (ut)->ut_type = (fu)->fu_type; \
117a627ac61SEd Schouten } while (0)
118a627ac61SEd Schouten #define FTOU_TV(fu, ut) do { \
119a627ac61SEd Schouten uint64_t t; \
120a627ac61SEd Schouten t = be64toh((fu)->fu_tv); \
121a627ac61SEd Schouten (ut)->ut_tv.tv_sec = t / 1000000; \
122a627ac61SEd Schouten (ut)->ut_tv.tv_usec = t % 1000000; \
123a627ac61SEd Schouten } while (0)
124a627ac61SEd Schouten
12598c63a48SEd Schouten struct utmpx *
futx_to_utx(const struct futx * fu)12698c63a48SEd Schouten futx_to_utx(const struct futx *fu)
127a627ac61SEd Schouten {
128126b6df9SEd Schouten static _Thread_local struct utmpx *ut;
129a627ac61SEd Schouten
13098c63a48SEd Schouten if (ut == NULL) {
13198c63a48SEd Schouten ut = calloc(1, sizeof *ut);
13298c63a48SEd Schouten if (ut == NULL)
13398c63a48SEd Schouten return (NULL);
1341ae6a21dSEd Schouten } else
135a627ac61SEd Schouten memset(ut, 0, sizeof *ut);
136a627ac61SEd Schouten
137a627ac61SEd Schouten switch (fu->fu_type) {
138a627ac61SEd Schouten case BOOT_TIME:
139a627ac61SEd Schouten case OLD_TIME:
140a627ac61SEd Schouten case NEW_TIME:
141a627ac61SEd Schouten /* Extension: shutdown time. */
142a627ac61SEd Schouten case SHUTDOWN_TIME:
143a627ac61SEd Schouten break;
144a627ac61SEd Schouten case USER_PROCESS:
145a627ac61SEd Schouten FTOU_ID(fu, ut);
146a627ac61SEd Schouten FTOU_STRING(fu, ut, user);
147a627ac61SEd Schouten FTOU_STRING(fu, ut, line);
148a627ac61SEd Schouten /* Extension: host name. */
149a627ac61SEd Schouten FTOU_STRING(fu, ut, host);
150a627ac61SEd Schouten FTOU_PID(fu, ut);
151a627ac61SEd Schouten break;
152a627ac61SEd Schouten case INIT_PROCESS:
153a627ac61SEd Schouten FTOU_ID(fu, ut);
154a627ac61SEd Schouten FTOU_PID(fu, ut);
155a627ac61SEd Schouten break;
156a627ac61SEd Schouten case LOGIN_PROCESS:
157a627ac61SEd Schouten FTOU_ID(fu, ut);
158a627ac61SEd Schouten FTOU_STRING(fu, ut, user);
15998c63a48SEd Schouten FTOU_STRING(fu, ut, line);
160a627ac61SEd Schouten FTOU_PID(fu, ut);
161a627ac61SEd Schouten break;
162a627ac61SEd Schouten case DEAD_PROCESS:
163a627ac61SEd Schouten FTOU_ID(fu, ut);
164a627ac61SEd Schouten FTOU_PID(fu, ut);
165a627ac61SEd Schouten break;
166a627ac61SEd Schouten default:
167a627ac61SEd Schouten ut->ut_type = EMPTY;
16898c63a48SEd Schouten return (ut);
169a627ac61SEd Schouten }
170a627ac61SEd Schouten
171a627ac61SEd Schouten FTOU_TYPE(fu, ut);
172a627ac61SEd Schouten FTOU_TV(fu, ut);
17398c63a48SEd Schouten return (ut);
174a627ac61SEd Schouten }
175