2 .\" Copyright 1995 Mark D. Roth (roth@uiuc.edu)
4 .\" SPDX-License-Identifier: GPL-2.0-or-later
6 .\" References consulted:
7 .\" Linux libc source code
10 .\" Modified Thu Jul 25 14:43:46 MET DST 1996 by Michael Haardt
11 .\" <michael@cantor.informatik.rwth-aachen.de>
13 .TH getutent 3 (date) "Linux man-pages (unreleased)"
15 getutent, getutid, getutline, pututline, setutent, endutent,
16 utmpname \- access utmp file entries
19 .RI ( libc ", " \-lc )
24 .B struct utmp *getutent(void);
25 .BI "struct utmp *getutid(const struct utmp *" ut );
26 .BI "struct utmp *getutline(const struct utmp *" ut );
28 .BI "struct utmp *pututline(const struct utmp *" ut );
30 .B void setutent(void);
31 .B void endutent(void);
33 .BI "int utmpname(const char *" file );
36 New applications should use the POSIX.1-specified "utmpx" versions of
37 these functions; see STANDARDS.
40 sets the name of the utmp-format file for the other utmp
44 is not used to set the filename
45 before the other functions are used, they assume \fB_PATH_UTMP\fP, as
46 defined in \fI<paths.h>\fP.
49 rewinds the file pointer to the beginning of the utmp file.
50 It is generally a good idea to call it before any of the other
55 It should be called when the user
56 code is done accessing the file with the other functions.
59 reads a line from the current file position in the utmp file.
60 It returns a pointer to a structure containing the fields of
62 The definition of this structure is shown in
66 searches forward from the current file position in the utmp
67 file based upon \fIut\fP.
68 If \fIut\->ut_type\fP is one of \fBRUN_LVL\fP,
69 \fBBOOT_TIME\fP, \fBNEW_TIME\fP, or \fBOLD_TIME\fP,
72 find the first entry whose \fIut_type\fP field matches \fIut\->ut_type\fP.
73 If \fIut\->ut_type\fP is one of \fBINIT_PROCESS\fP, \fBLOGIN_PROCESS\fP,
74 \fBUSER_PROCESS\fP, or \fBDEAD_PROCESS\fP,
79 field matches \fIut\->ut_id\fP.
82 searches forward from the current file position in the utmp file.
83 It scans entries whose
86 or \fBLOGIN_PROCESS\fP and returns the first one whose
89 matches \fIut\->ut_line\fP.
94 structure \fIut\fP into the utmp file.
97 to search for the proper place in the file to insert
99 If it cannot find an appropriate slot for \fIut\fP,
101 will append the new entry to the end of the file.
107 return a pointer to a \fIstruct utmp\fP on success,
108 and NULL on failure (which includes the "record not found" case).
109 This \fIstruct utmp\fP is allocated in static storage, and may be
110 overwritten by subsequent calls.
116 on failure, it returns NULL.
119 returns 0 if the new name was successfully stored, or \-1 on failure.
121 On failure, these functions
123 set to indicate the error.
136 functions can also fail for the reasons described in
141 database of currently logged-in users
144 database of past user logins
146 For an explanation of the terms used in this section, see
154 Interface Attribute Value
158 MT-Unsafe init race:utent
159 race:utentbuf sig:ALRM timer
165 MT-Unsafe init race:utent
178 T} Thread safety MT-Unsafe race:utent
187 signifies that if any of the functions
196 are used in parallel in different threads of a program,
197 then data races could occur.
203 In XPG2 and SVID 2 the function
205 is documented to return void, and that is what it does on many systems
207 HP-UX introduces a new function
209 with the prototype given above for
212 All these functions are obsolete now on non-Linux systems.
213 POSIX.1-2001 and POSIX.1-2008, following SUSv1,
214 does not have any of these functions, but instead uses
218 .B #include <utmpx.h>
220 .B struct utmpx *getutxent(void);
221 .B struct utmpx *getutxid(const struct utmpx *);
222 .B struct utmpx *getutxline(const struct utmpx *);
223 .B struct utmpx *pututxline(const struct utmpx *);
224 .B void setutxent(void);
225 .B void endutxent(void);
229 These functions are provided by glibc,
230 and perform the same task as their equivalents without the "x", but use
232 defined on Linux to be the same as
234 For completeness, glibc also provides
236 although this function is not specified by POSIX.1.
238 On some other systems,
239 the \fIutmpx\fP structure is a superset of the \fIutmp\fP structure,
240 with additional fields, and larger versions of the existing fields,
241 and parallel files are maintained, often
246 Linux glibc on the other hand does not use a parallel \fIutmpx\fP file
247 since its \fIutmp\fP structure is already large enough.
248 The "x" functions listed above are just aliases for
249 their counterparts without the "x" (e.g.,
255 The above functions are not thread-safe.
256 glibc adds reentrant versions
261 .BI "int getutent_r(struct utmp *" ubuf ", struct utmp **" ubufp );
262 .BI "int getutid_r(struct utmp *" ut ,
263 .BI " struct utmp *" ubuf ", struct utmp **" ubufp );
264 .BI "int getutline_r(struct utmp *" ut ,
265 .BI " struct utmp *" ubuf ", struct utmp **" ubufp );
268 Feature Test Macro Requirements for glibc (see
269 .BR feature_test_macros (7)):
276 || /* Since glibc 2.19: */ _DEFAULT_SOURCE
277 || /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
280 These functions are GNU extensions, analogs of the functions of the
281 same name without the _r suffix.
284 argument gives these functions a place to store their result.
285 On success, they return 0, and a pointer to the result is written in
287 On error, these functions return \-1.
288 There are no utmpx equivalents of the above functions.
289 (POSIX.1 does not specify such functions.)
291 The following example adds and removes a utmp record, assuming it is run
292 from within a pseudo terminal.
293 For usage in a real application, you
294 should check the return values of
299 .\" SRC BEGIN (getutent.c)
313 system("echo before adding entry:;who");
315 entry.ut_type = USER_PROCESS;
316 entry.ut_pid = getpid();
317 strcpy(entry.ut_line, ttyname(STDIN_FILENO) + strlen("/dev/"));
318 /* only correct for ptys named /dev/tty[pqr][0\-9a\-z] */
319 strcpy(entry.ut_id, ttyname(STDIN_FILENO) + strlen("/dev/tty"));
320 time(&entry.ut_time);
321 strcpy(entry.ut_user, getpwuid(getuid())\->pw_name);
322 memset(entry.ut_host, 0, UT_HOSTSIZE);
327 system("echo after adding entry:;who");
329 entry.ut_type = DEAD_PROCESS;
330 memset(entry.ut_line, 0, UT_LINESIZE);
332 memset(entry.ut_user, 0, UT_NAMESIZE);
336 system("echo after removing entry:;who");