1 .\" Copyright 1995 Mark D. Roth (roth@uiuc.edu)
3 .\" SPDX-License-Identifier: GPL-2.0-or-later
5 .\" References consulted:
6 .\" Linux libc source code
9 .\" Modified Thu Jul 25 14:43:46 MET DST 1996 by Michael Haardt
10 .\" <michael@cantor.informatik.rwth-aachen.de>
12 .TH GETUTENT 3 2021-03-22 "Linux man-pages (unreleased)"
14 getutent, getutid, getutline, pututline, setutent, endutent,
15 utmpname \- access utmp file entries
18 .RI ( libc ", " \-lc )
23 .B struct utmp *getutent(void);
24 .BI "struct utmp *getutid(const struct utmp *" ut );
25 .BI "struct utmp *getutline(const struct utmp *" ut );
27 .BI "struct utmp *pututline(const struct utmp *" ut );
29 .B void setutent(void);
30 .B void endutent(void);
32 .BI "int utmpname(const char *" file );
35 New applications should use the POSIX.1-specified "utmpx" versions of
36 these functions; see STANDARDS.
39 sets the name of the utmp-format file for the other utmp
43 is not used to set the filename
44 before the other functions are used, they assume \fB_PATH_UTMP\fP, as
45 defined in \fI<paths.h>\fP.
48 rewinds the file pointer to the beginning of the utmp file.
49 It is generally a good idea to call it before any of the other
54 It should be called when the user
55 code is done accessing the file with the other functions.
58 reads a line from the current file position in the utmp file.
59 It returns a pointer to a structure containing the fields of
61 The definition of this structure is shown in
65 searches forward from the current file position in the utmp
66 file based upon \fIut\fP.
67 If \fIut\->ut_type\fP is one of \fBRUN_LVL\fP,
68 \fBBOOT_TIME\fP, \fBNEW_TIME\fP, or \fBOLD_TIME\fP,
71 find the first entry whose \fIut_type\fP field matches \fIut\->ut_type\fP.
72 If \fIut\->ut_type\fP is one of \fBINIT_PROCESS\fP, \fBLOGIN_PROCESS\fP,
73 \fBUSER_PROCESS\fP, or \fBDEAD_PROCESS\fP,
78 field matches \fIut\->ut_id\fP.
81 searches forward from the current file position in the utmp file.
82 It scans entries whose
85 or \fBLOGIN_PROCESS\fP and returns the first one whose
88 matches \fIut\->ut_line\fP.
93 structure \fIut\fP into the utmp file.
96 to search for the proper place in the file to insert
98 If it cannot find an appropriate slot for \fIut\fP,
100 will append the new entry to the end of the file.
106 return a pointer to a \fIstruct utmp\fP on success,
107 and NULL on failure (which includes the "record not found" case).
108 This \fIstruct utmp\fP is allocated in static storage, and may be
109 overwritten by subsequent calls.
115 on failure, it returns NULL.
118 returns 0 if the new name was successfully stored, or \-1 on failure.
120 On failure, these functions
122 set to indicate the error.
135 functions can also fail for the reasons described in
140 database of currently logged-in users
143 database of past user logins
145 For an explanation of the terms used in this section, see
153 Interface Attribute Value
157 MT-Unsafe init race:utent
158 race:utentbuf sig:ALRM timer
164 MT-Unsafe init race:utent
177 T} Thread safety MT-Unsafe race:utent
186 signifies that if any of the functions
195 are used in parallel in different threads of a program,
196 then data races could occur.
200 In XPG2 and SVID 2 the function
202 is documented to return void, and that is what it does on many systems
204 HP-UX introduces a new function
206 with the prototype given above for
209 All these functions are obsolete now on non-Linux systems.
210 POSIX.1-2001 and POSIX.1-2008, following SUSv1,
211 does not have any of these functions, but instead uses
215 .B #include <utmpx.h>
217 .B struct utmpx *getutxent(void);
218 .B struct utmpx *getutxid(const struct utmpx *);
219 .B struct utmpx *getutxline(const struct utmpx *);
220 .B struct utmpx *pututxline(const struct utmpx *);
221 .B void setutxent(void);
222 .B void endutxent(void);
226 These functions are provided by glibc,
227 and perform the same task as their equivalents without the "x", but use
229 defined on Linux to be the same as
231 For completeness, glibc also provides
233 although this function is not specified by POSIX.1.
235 On some other systems,
236 the \fIutmpx\fP structure is a superset of the \fIutmp\fP structure,
237 with additional fields, and larger versions of the existing fields,
238 and parallel files are maintained, often
243 Linux glibc on the other hand does not use a parallel \fIutmpx\fP file
244 since its \fIutmp\fP structure is already large enough.
245 The "x" functions listed above are just aliases for
246 their counterparts without the "x" (e.g.,
252 The above functions are not thread-safe.
253 Glibc adds reentrant versions
258 .BI "int getutent_r(struct utmp *" ubuf ", struct utmp **" ubufp );
259 .BI "int getutid_r(struct utmp *" ut ,
260 .BI " struct utmp *" ubuf ", struct utmp **" ubufp );
261 .BI "int getutline_r(struct utmp *" ut ,
262 .BI " struct utmp *" ubuf ", struct utmp **" ubufp );
265 Feature Test Macro Requirements for glibc (see
266 .BR feature_test_macros (7)):
273 || /* Since glibc 2.19: */ _DEFAULT_SOURCE
274 || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
277 These functions are GNU extensions, analogs of the functions of the
278 same name without the _r suffix.
281 argument gives these functions a place to store their result.
282 On success, they return 0, and a pointer to the result is written in
284 On error, these functions return \-1.
285 There are no utmpx equivalents of the above functions.
286 (POSIX.1 does not specify such functions.)
288 The following example adds and removes a utmp record, assuming it is run
289 from within a pseudo terminal.
290 For usage in a real application, you
291 should check the return values of
305 main(int argc, char *argv[])
309 system("echo before adding entry:;who");
311 entry.ut_type = USER_PROCESS;
312 entry.ut_pid = getpid();
313 strcpy(entry.ut_line, ttyname(STDIN_FILENO) + strlen("/dev/"));
314 /* only correct for ptys named /dev/tty[pqr][0\-9a\-z] */
315 strcpy(entry.ut_id, ttyname(STDIN_FILENO) + strlen("/dev/tty"));
316 time(&entry.ut_time);
317 strcpy(entry.ut_user, getpwuid(getuid())\->pw_name);
318 memset(entry.ut_host, 0, UT_HOSTSIZE);
323 system("echo after adding entry:;who");
325 entry.ut_type = DEAD_PROCESS;
326 memset(entry.ut_line, 0, UT_LINESIZE);
328 memset(entry.ut_user, 0, UT_NAMESIZE);
332 system("echo after removing entry:;who");