xref: /freebsd/lib/libc/gen/utxdb.c (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 Ed Schouten <ed@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include "namespace.h"
31 #include <sys/endian.h>
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <utmpx.h>
37 #include "utxdb.h"
38 #include "un-namespace.h"
39 
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));	\
43 } while (0)
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));		\
47 } while (0)
48 #define	UTOF_PID(ut, fu) do { \
49 	(fu)->fu_pid = htobe32((ut)->ut_pid);				\
50 } while (0)
51 #define	UTOF_TYPE(ut, fu) do { \
52 	(fu)->fu_type = (ut)->ut_type;					\
53 } while (0)
54 #define	UTOF_TV(fu) do { \
55 	struct timeval tv;						\
56 	gettimeofday(&tv, NULL);					\
57 	(fu)->fu_tv = htobe64((uint64_t)tv.tv_sec * 1000000 +		\
58 	    (uint64_t)tv.tv_usec);					\
59 } while (0)
60 
61 void
62 utx_to_futx(const struct utmpx *ut, struct futx *fu)
63 {
64 
65 	memset(fu, 0, sizeof *fu);
66 
67 	switch (ut->ut_type) {
68 	case BOOT_TIME:
69 	case OLD_TIME:
70 	case NEW_TIME:
71 	/* Extension: shutdown time. */
72 	case SHUTDOWN_TIME:
73 		break;
74 	case USER_PROCESS:
75 		UTOF_ID(ut, fu);
76 		UTOF_STRING(ut, fu, user);
77 		UTOF_STRING(ut, fu, line);
78 		/* Extension: host name. */
79 		UTOF_STRING(ut, fu, host);
80 		UTOF_PID(ut, fu);
81 		break;
82 	case INIT_PROCESS:
83 		UTOF_ID(ut, fu);
84 		UTOF_PID(ut, fu);
85 		break;
86 	case LOGIN_PROCESS:
87 		UTOF_ID(ut, fu);
88 		UTOF_STRING(ut, fu, user);
89 		UTOF_STRING(ut, fu, line);
90 		UTOF_PID(ut, fu);
91 		break;
92 	case DEAD_PROCESS:
93 		UTOF_ID(ut, fu);
94 		UTOF_PID(ut, fu);
95 		break;
96 	default:
97 		fu->fu_type = EMPTY;
98 		return;
99 	}
100 
101 	UTOF_TYPE(ut, fu);
102 	UTOF_TV(fu);
103 }
104 
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)); \
108 } while (0)
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));		\
112 } while (0)
113 #define	FTOU_PID(fu, ut) do { \
114 	(ut)->ut_pid = be32toh((fu)->fu_pid);				\
115 } while (0)
116 #define	FTOU_TYPE(fu, ut) do { \
117 	(ut)->ut_type = (fu)->fu_type;					\
118 } while (0)
119 #define	FTOU_TV(fu, ut) do { \
120 	uint64_t t;							\
121 	t = be64toh((fu)->fu_tv);					\
122 	(ut)->ut_tv.tv_sec = t / 1000000;				\
123 	(ut)->ut_tv.tv_usec = t % 1000000;				\
124 } while (0)
125 
126 struct utmpx *
127 futx_to_utx(const struct futx *fu)
128 {
129 	static _Thread_local struct utmpx *ut;
130 
131 	if (ut == NULL) {
132 		ut = calloc(1, sizeof *ut);
133 		if (ut == NULL)
134 			return (NULL);
135 	} else
136 		memset(ut, 0, sizeof *ut);
137 
138 	switch (fu->fu_type) {
139 	case BOOT_TIME:
140 	case OLD_TIME:
141 	case NEW_TIME:
142 	/* Extension: shutdown time. */
143 	case SHUTDOWN_TIME:
144 		break;
145 	case USER_PROCESS:
146 		FTOU_ID(fu, ut);
147 		FTOU_STRING(fu, ut, user);
148 		FTOU_STRING(fu, ut, line);
149 		/* Extension: host name. */
150 		FTOU_STRING(fu, ut, host);
151 		FTOU_PID(fu, ut);
152 		break;
153 	case INIT_PROCESS:
154 		FTOU_ID(fu, ut);
155 		FTOU_PID(fu, ut);
156 		break;
157 	case LOGIN_PROCESS:
158 		FTOU_ID(fu, ut);
159 		FTOU_STRING(fu, ut, user);
160 		FTOU_STRING(fu, ut, line);
161 		FTOU_PID(fu, ut);
162 		break;
163 	case DEAD_PROCESS:
164 		FTOU_ID(fu, ut);
165 		FTOU_PID(fu, ut);
166 		break;
167 	default:
168 		ut->ut_type = EMPTY;
169 		return (ut);
170 	}
171 
172 	FTOU_TYPE(fu, ut);
173 	FTOU_TV(fu, ut);
174 	return (ut);
175 }
176