1 .\" Copyright 1995 Mark D. Roth (roth@uiuc.edu)
3 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
4 .\" This is free documentation; you can redistribute it and/or
5 .\" modify it under the terms of the GNU General Public License as
6 .\" published by the Free Software Foundation; either version 2 of
7 .\" the License, or (at your option) any later version.
9 .\" The GNU General Public License's references to "object code"
10 .\" and "executables" are to be interpreted as the output of any
11 .\" document formatting or typesetting system, including
12 .\" intermediate and printed output.
14 .\" This manual is distributed in the hope that it will be useful,
15 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
16 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 .\" GNU General Public License for more details.
19 .\" You should have received a copy of the GNU General Public
20 .\" License along with this manual; if not, see
21 .\" <http://www.gnu.org/licenses/>.
24 .\" References consulted:
25 .\" Linux libc source code
28 .\" Modified Thu Jul 25 14:43:46 MET DST 1996 by Michael Haardt
29 .\" <michael@cantor.informatik.rwth-aachen.de>
31 .TH GETUTENT 3 2017-09-15 "" "Linux Programmer's Manual"
33 getutent, getutid, getutline, pututline, setutent, endutent,
34 utmpname \- access utmp file entries
38 .B struct utmp *getutent(void);
40 .BI "struct utmp *getutid(const struct utmp *" ut );
42 .BI "struct utmp *getutline(const struct utmp *" ut );
44 .BI "struct utmp *pututline(const struct utmp *" ut );
46 .B void setutent(void);
48 .B void endutent(void);
50 .BI "int utmpname(const char *" file );
52 New applications should use the POSIX.1-specified "utmpx" versions of
53 these functions; see CONFORMING TO.
56 sets the name of the utmp-format file for the other utmp
60 is not used to set the filename
61 before the other functions are used, they assume \fB_PATH_UTMP\fP, as
62 defined in \fI<paths.h>\fP.
65 rewinds the file pointer to the beginning of the utmp file.
66 It is generally a good idea to call it before any of the other
71 It should be called when the user
72 code is done accessing the file with the other functions.
75 reads a line from the current file position in the utmp file.
76 It returns a pointer to a structure containing the fields of
78 The definition of this structure is shown in
82 searches forward from the current file position in the utmp
83 file based upon \fIut\fP.
84 If \fIut\->ut_type\fP is one of \fBRUN_LVL\fP,
85 \fBBOOT_TIME\fP, \fBNEW_TIME\fP, or \fBOLD_TIME\fP,
88 find the first entry whose \fIut_type\fP field matches \fIut\->ut_type\fP.
89 If \fIut\->ut_type\fP is one of \fBINIT_PROCESS\fP, \fBLOGIN_PROCESS\fP,
90 \fBUSER_PROCESS\fP, or \fBDEAD_PROCESS\fP,
95 field matches \fIut\->ut_id\fP.
98 searches forward from the current file position in the utmp file.
99 It scans entries whose
101 is \fBUSER_PROCESS\fP
102 or \fBLOGIN_PROCESS\fP and returns the first one whose
105 matches \fIut\->ut_line\fP.
110 structure \fIut\fP into the utmp file.
113 to search for the proper place in the file to insert
115 If it cannot find an appropriate slot for \fIut\fP,
117 will append the new entry to the end of the file.
123 return a pointer to a \fIstruct utmp\fP on success,
124 and NULL on failure (which includes the "record not found" case).
125 This \fIstruct utmp\fP is allocated in static storage, and may be
126 overwritten by subsequent calls.
132 on failure, it returns NULL.
135 returns 0 if the new name was successfully stored, or \-1 on failure.
137 In the event of an error, these functions
139 set to indicate the cause.
152 functions can also fail for the reasons described in
157 database of currently logged-in users
160 database of past user logins
162 For an explanation of the terms used in this section, see
168 Interface Attribute Value
172 MT-Unsafe init race:utent
174 race:utentbuf sig:ALRM timer
181 MT-Unsafe init race:utent
198 T} Thread safety MT-Unsafe race:utent
205 signifies that if any of the functions
214 are used in parallel in different threads of a program,
215 then data races could occur.
219 In XPG2 and SVID 2 the function
221 is documented to return void, and that is what it does on many systems
223 HP-UX introduces a new function
225 with the prototype given above for
228 All these functions are obsolete now on non-Linux systems.
229 POSIX.1-2001 and POSIX.1-2008, following SUSv1,
230 does not have any of these functions, but instead uses
234 .B #include <utmpx.h>
236 .B struct utmpx *getutxent(void);
237 .B struct utmpx *getutxid(const struct utmpx *);
238 .B struct utmpx *getutxline(const struct utmpx *);
239 .B struct utmpx *pututxline(const struct utmpx *);
240 .B void setutxent(void);
241 .B void endutxent(void);
245 These functions are provided by glibc,
246 and perform the same task as their equivalents without the "x", but use
248 defined on Linux to be the same as
250 For completeness, glibc also provides
252 although this function is not specified by POSIX.1.
254 On some other systems,
255 the \fIutmpx\fP structure is a superset of the \fIutmp\fP structure,
256 with additional fields, and larger versions of the existing fields,
257 and parallel files are maintained, often
262 Linux glibc on the other hand does not use a parallel \fIutmpx\fP file
263 since its \fIutmp\fP structure is already large enough.
264 The "x" functions listed above are just aliases for
265 their counterparts without the "x" (e.g.,
271 The above functions are not thread-safe.
272 Glibc adds reentrant versions
278 .BI "int getutent_r(struct utmp *" ubuf ", struct utmp **" ubufp );
280 .BI "int getutid_r(struct utmp *" ut ,
281 .BI " struct utmp *" ubuf ", struct utmp **" ubufp );
283 .BI "int getutline_r(struct utmp *" ut ,
284 .BI " struct utmp *" ubuf ", struct utmp **" ubufp );
288 Feature Test Macro Requirements for glibc (see
289 .BR feature_test_macros (7)):
296 || /* since glibc 2.19: */ _DEFAULT_SOURCE
297 || /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
300 These functions are GNU extensions, analogs of the functions of the
301 same name without the _r suffix.
304 argument gives these functions a place to store their result.
305 On success, they return 0, and a pointer to the result is written in
307 On error, these functions return \-1.
308 There are no utmpx equivalents of the above functions.
309 (POSIX.1 does not specify such functions.)
311 The following example adds and removes a utmp record, assuming it is run
312 from within a pseudo terminal.
313 For usage in a real application, you
314 should check the return values of
327 main(int argc, char *argv[])
331 system("echo before adding entry:;who");
333 entry.ut_type = USER_PROCESS;
334 entry.ut_pid = getpid();
335 strcpy(entry.ut_line, ttyname(STDIN_FILENO) + strlen("/dev/"));
336 /* only correct for ptys named /dev/tty[pqr][0\-9a\-z] */
337 strcpy(entry.ut_id, ttyname(STDIN_FILENO) + strlen("/dev/tty"));
338 time(&entry.ut_time);
339 strcpy(entry.ut_user, getpwuid(getuid())\->pw_name);
340 memset(entry.ut_host, 0, UT_HOSTSIZE);
345 system("echo after adding entry:;who");
347 entry.ut_type = DEAD_PROCESS;
348 memset(entry.ut_line, 0, UT_LINESIZE);
350 memset(entry.ut_user, 0, UT_NAMESIZE);
354 system("echo after removing entry:;who");