1 .\" Copyright 1995 Mark D. Roth (roth@uiuc.edu)
3 .\" This is free documentation; you can redistribute it and/or
4 .\" modify it under the terms of the GNU General Public License as
5 .\" published by the Free Software Foundation; either version 2 of
6 .\" the License, or (at your option) any later version.
8 .\" The GNU General Public License's references to "object code"
9 .\" and "executables" are to be interpreted as the output of any
10 .\" document formatting or typesetting system, including
11 .\" intermediate and printed output.
13 .\" This manual is distributed in the hope that it will be useful,
14 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
15 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 .\" GNU General Public License for more details.
18 .\" You should have received a copy of the GNU General Public
19 .\" License along with this manual; if not, see
20 .\" <http://www.gnu.org/licenses/>.
22 .\" References consulted:
23 .\" Linux libc source code
26 .\" Modified Thu Jul 25 14:43:46 MET DST 1996 by Michael Haardt
27 .\" <michael@cantor.informatik.rwth-aachen.de>
29 .TH GETUTENT 3 2008-06-29 "" "Linux Programmer's Manual"
31 getutent, getutid, getutline, pututline, setutent, endutent,
32 utmpname \- access utmp file entries
36 .B struct utmp *getutent(void);
38 .BI "struct utmp *getutid(struct utmp *" ut );
40 .BI "struct utmp *getutline(struct utmp *" ut );
42 .BI "struct utmp *pututline(struct utmp *" ut );
44 .B void setutent(void);
46 .B void endutent(void);
48 .BI "int utmpname(const char *" file );
50 New applications should use the POSIX.1-specified "utmpx" versions of
51 these functions; see CONFORMING TO.
54 sets the name of the utmp-format file for the other utmp
58 is not used to set the filename
59 before the other functions are used, they assume \fB_PATH_UTMP\fP, as
60 defined in \fI<paths.h>\fP.
63 rewinds the file pointer to the beginning of the utmp file.
64 It is generally a good idea to call it before any of the other
69 It should be called when the user
70 code is done accessing the file with the other functions.
73 reads a line from the current file position in the utmp file.
74 It returns a pointer to a structure containing the fields of
76 The definition of this structure is shown in
80 searches forward from the current file position in the utmp
81 file based upon \fIut\fP.
82 If \fIut\->ut_type\fP is one of \fBRUN_LVL\fP,
83 \fBBOOT_TIME\fP, \fBNEW_TIME\fP, or \fBOLD_TIME\fP,
86 find the first entry whose \fIut_type\fP field matches \fIut\->ut_type\fP.
87 If \fIut\->ut_type\fP is one of \fBINIT_PROCESS\fP, \fBLOGIN_PROCESS\fP,
88 \fBUSER_PROCESS\fP, or \fBDEAD_PROCESS\fP,
93 field matches \fIut\->ut_id\fP.
96 searches forward from the current file position in the utmp file.
97 It scans entries whose
100 or \fBLOGIN_PROCESS\fP and returns the first one whose
103 matches \fIut\->ut_line\fP.
108 structure \fIut\fP into the utmp file.
111 to search for the proper place in the file to insert
113 If it cannot find an appropriate slot for \fIut\fP,
115 will append the new entry to the end of the file.
121 return a pointer to a \fIstruct utmp\fP on success,
122 and NULL on failure (which includes the "record not found" case).
123 This \fIstruct utmp\fP is allocated in static storage, and may be
124 overwritten by subsequent calls.
130 on failure, it returns NULL.
133 returns 0 if the new name was successfully stored, or \-1 on failure.
146 functions can also fail for the reasons described in
149 /var/run/utmp database of currently logged-in users
151 /var/log/wtmp database of past user logins
155 In XPG2 and SVID 2 the function
157 is documented to return void, and that is what it does on many systems
158 (AIX, HP-UX, Linux libc5).
159 HP-UX introduces a new function
161 with the prototype given above for
163 (also found in Linux libc5).
165 All these functions are obsolete now on non-Linux systems.
166 POSIX.1-2001, following SUSv1,
167 does not have any of these functions, but instead uses
169 .B #include <utmpx.h>
171 .B struct utmpx *getutxent(void);
173 .B struct utmpx *getutxid(const struct utmpx *);
175 .B struct utmpx *getutxline(const struct utmpx *);
177 .B struct utmpx *pututxline(const struct utmpx *);
179 .B void setutxent(void);
181 .B void endutxent(void);
183 These functions are provided by glibc,
184 and perform the same task as their equivalents without the "x", but use
186 defined on Linux to be the same as
188 For completeness, glibc also provides
190 although this function is not specified by POSIX.1.
192 On some other systems,
193 the \fIutmpx\fP structure is a superset of the \fIutmp\fP structure,
194 with additional fields, and larger versions of the existing fields,
195 and parallel files are maintained, often
200 Linux glibc on the other hand does not use a parallel \fIutmpx\fP file
201 since its \fIutmp\fP structure is already large enough.
202 The "x" functions listed above are just aliases for
203 their counterparts without the "x" (e.g.,
209 The above functions are not thread-safe.
210 Glibc adds reentrant versions
213 .BR "#define _GNU_SOURCE" " /* or _SVID_SOURCE or _BSD_SOURCE;
214 .RB "\& see " feature_test_macros "(7) */"
217 .BI "int getutent_r(struct utmp *" ubuf ", struct utmp **" ubufp );
219 .BI "int getutid_r(struct utmp *" ut ,
220 .BI " struct utmp *" ubuf ", struct utmp **" ubufp );
222 .BI "int getutline_r(struct utmp *" ut ,
223 .BI " struct utmp *" ubuf ", struct utmp **" ubufp );
226 These functions are GNU extensions, analogs of the functions of the
227 same name without the _r suffix.
230 argument gives these functions a place to store their result.
231 On success they return 0, and a pointer to the result is written in
233 On error these functions return \-1.
234 There are no utmpx equivalents of the above functions.
235 (POSIX.1 does not specify such functions.)
237 The following example adds and removes a utmp record, assuming it is run
238 from within a pseudo terminal.
239 For usage in a real application, you
240 should check the return values of
253 main(int argc, char *argv[])
257 system("echo before adding entry:;who");
259 entry.ut_type = USER_PROCESS;
260 entry.ut_pid = getpid();
261 strcpy(entry.ut_line, ttyname(STDIN_FILENO) + strlen("/dev/"));
262 /* only correct for ptys named /dev/tty[pqr][0\-9a\-z] */
263 strcpy(entry.ut_id, ttyname(STDIN_FILENO) + strlen("/dev/tty"));
264 time(&entry.ut_time);
265 strcpy(entry.ut_user, getpwuid(getuid())\->pw_name);
266 memset(entry.ut_host, 0, UT_HOSTSIZE);
271 system("echo after adding entry:;who");
273 entry.ut_type = DEAD_PROCESS;
274 memset(entry.ut_line, 0, UT_LINESIZE);
276 memset(entry.ut_user, 0, UT_NAMESIZE);
280 system("echo after removing entry:;who");