Debian Codegraph @

lib/readutmp.h

raw
1 /* Declarations for GNU's read utmp module.
2
3 Copyright (C) 1992-2007, 2009-2026 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 /* Written by jla; revised by djm */
19
20 #ifndef __READUTMP_H__
21 #define __READUTMP_H__
22
23 /* This file uses _GL_ATTRIBUTE_MALLOC, _GL_ATTRIBUTE_RETURNS_NONNULL,
24 HAVE_UTMP_H, HAVE_UTMPX_H, HAVE_STRUCT_UTMP_*, HAVE_STRUCT_UTMPX_*,
25 HAVE_UTMPNAME, HAVE_UTMPXNAME. */
26 #if !_GL_CONFIG_H_INCLUDED
27 # error "Please include config.h first."
28 #endif
29
30 #include "idx.h"
31
32 #include <stdlib.h>
33 #include <sys/types.h>
34 #include <time.h>
35
36 /* AIX 4.3.3 has both utmp.h and utmpx.h, but only struct utmp
37 has the ut_exit member. */
38 #if (HAVE_UTMPX_H && HAVE_UTMP_H && HAVE_STRUCT_UTMP_UT_EXIT \
39 && ! HAVE_STRUCT_UTMPX_UT_EXIT)
40 # undef HAVE_UTMPX_H
41 #endif
42
43 /* HPUX 10.20 needs utmp.h, for the definition of e.g., UTMP_FILE. */
44 #if HAVE_UTMP_H
45 # include <utmp.h>
46 #endif
47
48 /* Needed for BOOT_TIME, USER_PROCESS, LOGIN_PROCESS. */
49 #if HAVE_UTMPX_H
50 # if defined _THREAD_SAFE && defined UTMP_DATA_INIT
51 /* When including both utmp.h and utmpx.h on AIX 4.3, with _THREAD_SAFE
52 defined, work around the duplicate struct utmp_data declaration. */
53 # define utmp_data gl_aix_4_3_workaround_utmp_data
54 # endif
55 # include <utmpx.h>
56 #endif
57
58
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62
63
64 /* Type of entries returned by read_utmp on all platforms. */
65 struct gl_utmp
66 {
67 /* All 'char *' here are of arbitrary length and point to storage
68 with lifetime equal to that of this struct. */
69 char *ut_user; /* User name */
70 char *ut_id; /* Session ID */
71 char *ut_line; /* seat / device */
72 char *ut_host; /* for remote sessions: user@host or host,
73 for local sessions: the X11 display :N */
74 struct timespec ut_ts; /* time */
75 pid_t ut_pid; /* process ID of ? */
76 pid_t ut_session; /* process ID of session leader */
77 short ut_type; /* BOOT_TIME, USER_PROCESS, LOGIN_PROCESS,
78 or other */
79 struct { int e_termination; int e_exit; } ut_exit;
80 };
81
82 /* The following types, macros, and constants describe the 'struct gl_utmp'. */
83 #define UT_USER(UT) ((UT)->ut_user)
84 #define UT_TIME_MEMBER(UT) ((UT)->ut_ts.tv_sec)
85 #define UT_PID(UT) ((UT)->ut_pid)
86 #define UT_TYPE_EQ(UT, V) ((UT)->ut_type == (V))
87 #define UT_TYPE_NOT_DEFINED 0
88 #define UT_EXIT_E_TERMINATION(UT) ((UT)->ut_exit.e_termination)
89 #define UT_EXIT_E_EXIT(UT) ((UT)->ut_exit.e_exit)
90
91 /* Type of entry returned by read_utmp(). */
92 typedef struct gl_utmp STRUCT_UTMP;
93
94 /* Size of the UT_USER (ut) member, or -1 if unbounded. */
95 enum { UT_USER_SIZE = -1 };
96
97 /* Size of the ut->ut_id member, or -1 if unbounded. */
98 enum { UT_ID_SIZE = -1 };
99
100 /* Size of the ut->ut_line member, or -1 if unbounded. */
101 enum { UT_LINE_SIZE = -1 };
102
103 /* Size of the ut->ut_host member, or -1 if unbounded. */
104 enum { UT_HOST_SIZE = -1 };
105
106
107 /* When read_utmp accesses a file (as opposed to fetching the information
108 from systemd), it uses the following low-level types and macros.
109 Keep them here, rather than moving them into readutmp.c, for backward
110 compatibility. */
111
112 #if HAVE_UTMPX_H
113
114 /* <utmpx.h> defines 'struct utmpx' with the following fields:
115
116 Field Type Platforms
117 ---------- ------ ---------
118 ⎡ ut_user char[] glibc, musl, macOS, FreeBSD, AIX, HP-UX, Solaris, Cygwin, Android
119 ⎣ ut_name char[] NetBSD, Minix
120 ut_id char[] glibc, musl, macOS, FreeBSD, NetBSD, Minix, AIX, HP-UX, Solaris, Cygwin, Android
121 ut_line char[] glibc, musl, macOS, FreeBSD, NetBSD, Minix, AIX, HP-UX, Solaris, Cygwin, Android
122 ut_pid pid_t glibc, musl, macOS, FreeBSD, NetBSD, Minix, AIX, HP-UX, Solaris, Cygwin, Android
123 ut_type short glibc, musl, macOS, FreeBSD, NetBSD, Minix, AIX, HP-UX, Solaris, Cygwin, Android
124 ⎡ ut_tv struct glibc, musl, macOS, FreeBSD, NetBSD, Minix, AIX, HP-UX, Solaris, Cygwin, Android
125 ⎢ { tv_sec; tv_usec; }
126 ⎣ ut_time time_t Cygwin
127 ut_host char[] glibc, musl, macOS, FreeBSD, NetBSD, Minix, AIX, HP-UX, Solaris, Cygwin, Android
128 ut_exit struct glibc, musl, NetBSD, Minix, HP-UX, Solaris, Android
129 { e_termination; e_exit; }
130 ut_session [long] int glibc, musl, NetBSD, Minix, Solaris, Android
131 ⎡ ut_addr [long] int HP-UX, Cygwin
132 ⎢ ut_addr_v6 [u]int[4] glibc, musl, Android
133 ⎣ ut_ss struct sockaddr_storage NetBSD, Minix
134 */
135
136 # if __GLIBC__ && _TIME_BITS == 64
137 /* This is a near-copy of glibc's struct utmpx, which stops working
138 after the year 2038. Unlike the glibc version, struct utmpx32
139 describes the file format even if time_t is 64 bits. */
140 #define _GL_UT_USER_SIZE sizeof (((struct utmpx *) 0)->ut_user)
141 #define _GL_UT_ID_SIZE sizeof (((struct utmpx *) 0)->ut_id)
142 #define _GL_UT_LINE_SIZE sizeof (((struct utmpx *) 0)->ut_line)
143 #define _GL_UT_HOST_SIZE sizeof (((struct utmpx *) 0)->ut_host)
144 struct utmpx32
145 {
146 short int ut_type; /* Type of login. */
147 pid_t ut_pid; /* Process ID of login process. */
148 char ut_line[_GL_UT_LINE_SIZE]; /* Devicename. */
149 char ut_id[_GL_UT_ID_SIZE]; /* Inittab ID. */
150 char ut_user[_GL_UT_USER_SIZE]; /* Username. */
151 char ut_host[_GL_UT_HOST_SIZE]; /* Hostname for remote login. */
152 struct __exit_status ut_exit; /* Exit status of a process marked
153 as DEAD_PROCESS. */
154 /* The fields ut_session and ut_tv must be the same size when compiled
155 32- and 64-bit. This allows files and shared memory to be shared
156 between 32- and 64-bit applications. */
157 int ut_session; /* Session ID, used for windowing. */
158 struct
159 {
160 /* Seconds. Unsigned not signed, as glibc did not exist before 1970,
161 and if the format is still in use after 2038 its timestamps
162 will surely have the sign bit on. This hack stops working
163 at 2106-02-07 06:28:16 UTC. */
164 unsigned int tv_sec;
165 int tv_usec; /* Microseconds. */
166 } ut_tv; /* Time entry was made. */
167 int ut_addr_v6[4]; /* Internet address of remote host. */
168 char ut_reserved[20]; /* Reserved for future use. */
169 };
170 # define UTMP_STRUCT_NAME utmpx32
171 # else
172 # define UTMP_STRUCT_NAME utmpx
173 # endif
174 # define SET_UTMP_ENT setutxent
175 # define GET_UTMP_ENT getutxent
176 # define END_UTMP_ENT endutxent
177 # ifdef HAVE_UTMPXNAME /* glibc, musl, macOS, NetBSD, Minix, Solaris, Cygwin */
178 # define UTMP_NAME_FUNCTION utmpxname
179 # elif defined UTXDB_ACTIVE /* FreeBSD */
180 # define UTMP_NAME_FUNCTION(x) setutxdb (UTXDB_ACTIVE, x)
181 # elif defined __ANDROID__ /* Android */
182 /* As of Android NDK r26, the getutxent, setutxent functions are no-ops.
183 Therefore we can ignore the file name. */
184 # define UTMP_NAME_FUNCTION(x) ((void) (x))
185 # endif
186
187 #elif HAVE_UTMP_H
188
189 /* <utmp.h> defines 'struct utmp' with the following fields:
190
191 Field Type Platforms
192 ---------- ------ ---------
193 ⎡ ut_user char[] glibc, musl, AIX, HP-UX, Solaris, Cygwin, Android
194 ⎣ ut_name char[] macOS, old FreeBSD, NetBSD, OpenBSD, Minix
195 ut_id char[] glibc, musl, AIX, HP-UX, Solaris, Cygwin, Android
196 ut_line char[] glibc, musl, macOS, old FreeBSD, NetBSD, OpenBSD, Minix, AIX, HP-UX, Solaris, Cygwin, Android
197 ut_pid pid_t glibc, musl, AIX, HP-UX, Solaris, Cygwin, Android
198 ut_type short glibc, musl, AIX, HP-UX, Solaris, Cygwin, Android
199 ⎡ ut_tv struct glibc, musl, Android
200 ⎢ { tv_sec; tv_usec; }
201 ⎣ ut_time time_t macOS, old FreeBSD, NetBSD, OpenBSD, Minix, AIX, HP-UX, Solaris, Cygwin
202 ut_host char[] glibc, musl, macOS, old FreeBSD, NetBSD, OpenBSD, Minix, AIX, HP-UX, Cygwin, Android
203 ut_exit struct glibc, musl, AIX, HP-UX, Solaris, Android
204 { e_termination; e_exit; }
205 ut_session [long] int glibc, musl, Android
206 ⎡ ut_addr [long] int HP-UX, Cygwin
207 ⎣ ut_addr_v6 [u]int[4] glibc, musl, Android
208 */
209
210 # define UTMP_STRUCT_NAME utmp
211 # define SET_UTMP_ENT setutent
212 # define GET_UTMP_ENT getutent
213 # define END_UTMP_ENT endutent
214 # ifdef HAVE_UTMPNAME /* glibc, musl, NetBSD, Minix, AIX, HP-UX, Solaris, Cygwin, Android */
215 # define UTMP_NAME_FUNCTION utmpname
216 # endif
217
218 #endif
219
220 /* Evaluates to 1 if gl_utmp's ut_id field may ever have a non-zero value. */
221 #define HAVE_STRUCT_XTMP_UT_ID \
222 (READUTMP_USE_SYSTEMD \
223 || (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_ID : HAVE_STRUCT_UTMP_UT_ID))
224
225 /* Evaluates to 1 if gl_utmp's ut_pid field may ever have a non-zero value. */
226 #define HAVE_STRUCT_XTMP_UT_PID \
227 (READUTMP_USE_SYSTEMD \
228 || (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_PID : HAVE_STRUCT_UTMP_UT_PID))
229
230 /* Evaluates to 1 if gl_utmp's ut_host field may ever be non-empty. */
231 #define HAVE_STRUCT_XTMP_UT_HOST \
232 (READUTMP_USE_SYSTEMD \
233 || (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_HOST : HAVE_STRUCT_UTMP_UT_HOST))
234
235 /* Definition of UTMP_FILE.
236 On glibc systems, UTMP_FILE is "/var/run/utmp". */
237 #if !defined UTMP_FILE && defined _PATH_UTMP
238 # define UTMP_FILE _PATH_UTMP
239 #endif
240 #ifdef UTMPX_FILE /* Solaris, SysVr4 */
241 # undef UTMP_FILE
242 # define UTMP_FILE UTMPX_FILE
243 #endif
244 #ifndef UTMP_FILE
245 # define UTMP_FILE "/etc/utmp"
246 #endif
247
248 /* Definition of WTMP_FILE.
249 On glibc systems, UTMP_FILE is "/var/log/wtmp". */
250 #if !defined WTMP_FILE && defined _PATH_WTMP
251 # define WTMP_FILE _PATH_WTMP
252 #endif
253 #ifdef WTMPX_FILE /* Solaris, SysVr4 */
254 # undef WTMP_FILE
255 # define WTMP_FILE WTMPX_FILE
256 #endif
257 #ifndef WTMP_FILE
258 # define WTMP_FILE "/etc/wtmp"
259 #endif
260
261 /* In early versions of Android, <utmp.h> did not define BOOT_TIME or
262 LOGIN_PROCESS, only USER_PROCESS. We need to use the value that is defined
263 in newer versions of Android. */
264 #if defined __ANDROID__ && !defined BOOT_TIME
265 # define BOOT_TIME 2
266 # define LOGIN_PROCESS 6
267 #endif
268
269 /* Some platforms, such as OpenBSD, don't have an ut_type field and don't have
270 the BOOT_TIME, USER_PROCESS, and LOGIN_PROCESS macros. But we want to
271 support them in 'struct gl_utmp'. */
272 #if !(HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_TYPE : HAVE_STRUCT_UTMP_UT_TYPE)
273 # define BOOT_TIME 2
274 # define USER_PROCESS 0
275 # define LOGIN_PROCESS 6
276 #endif
277
278 /* Macros that test (UT)->ut_type. */
279 #ifdef BOOT_TIME
280 # define UT_TYPE_BOOT_TIME(UT) ((UT)->ut_type == BOOT_TIME)
281 #else
282 # define UT_TYPE_BOOT_TIME(UT) 0
283 #endif
284 #ifdef USER_PROCESS
285 # define UT_TYPE_USER_PROCESS(UT) ((UT)->ut_type == USER_PROCESS)
286 #else
287 # define UT_TYPE_USER_PROCESS(UT) 0
288 #endif
289 #ifdef LOGIN_PROCESS
290 # define UT_TYPE_LOGIN_PROCESS(UT) ((UT)->ut_type == LOGIN_PROCESS)
291 #else
292 # define UT_TYPE_LOGIN_PROCESS(UT) 0
293 #endif
294
295 /* Determines whether an entry *UT corresponds to a user process. */
296 #define IS_USER_PROCESS(UT) \
297 ((UT)->ut_user[0] && UT_TYPE_USER_PROCESS (UT))
298
299 /* Define if read_utmp is not just a dummy. */
300 #if READUTMP_USE_SYSTEMD || HAVE_UTMPX_H || HAVE_UTMP_H || defined __CYGWIN__ || defined _WIN32
301 # define READ_UTMP_SUPPORTED 1
302 #endif
303
304 /* Options for read_utmp. */
305 enum
306 {
307 READ_UTMP_CHECK_PIDS = 1,
308 READ_UTMP_USER_PROCESS = 2,
309 READ_UTMP_BOOT_TIME = 4,
310 READ_UTMP_NO_BOOT_TIME = 8
311 };
312
313 /* Return a copy of (UT)->ut_user, without trailing spaces,
314 as a freshly allocated string. */
315 char *extract_trimmed_name (const STRUCT_UTMP *ut)
316 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
317 _GL_ATTRIBUTE_RETURNS_NONNULL;
318
319 /* Read the utmp entries corresponding to file FILE into freshly-
320 malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to
321 the number of entries, and return zero. If there is any error,
322 return -1, setting errno, and don't modify the parameters.
323 A good candidate for FILE is UTMP_FILE.
324 If OPTIONS & READ_UTMP_CHECK_PIDS is nonzero, omit entries whose
325 process-IDs do not currently exist.
326 If OPTIONS & READ_UTMP_USER_PROCESS is nonzero, omit entries which
327 do not correspond to a user process.
328 If OPTIONS & READ_UTMP_BOOT_TIME is nonzero, omit all entries except
329 the one that contains the boot time.
330 If OPTIONS & READ_UTMP_NO_BOOT_TIME is nonzero, omit the boot time
331 entries.
332
333 This function is not multithread-safe, since on many platforms it
334 invokes the functions setutxent, getutxent, endutxent. These
335 functions are needed because they may lock FILE (so that we don't
336 read garbage when a concurrent process writes to FILE), but their
337 drawback is that they have a common global state. */
338 int read_utmp (char const *file, idx_t *n_entries, STRUCT_UTMP **utmp_buf,
339 int options);
340
341
342 #ifdef __cplusplus
343 }
344 #endif
345
346 #endif /* __READUTMP_H__ */