]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getutent.3
getpass.3: Remove ancient Linux libc details
[thirdparty/man-pages.git] / man3 / getutent.3
CommitLineData
fea681da
MK
1.\" Copyright 1995 Mark D. Roth (roth@uiuc.edu)
2.\"
1dd72f9c 3.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
fea681da
MK
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.
8.\"
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.
13.\"
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.
18.\"
19.\" You should have received a copy of the GNU General Public
c715f741
MK
20.\" License along with this manual; if not, see
21.\" <http://www.gnu.org/licenses/>.
6a8d8745 22.\" %%%LICENSE_END
fea681da
MK
23.\"
24.\" References consulted:
25.\" Linux libc source code
26.\" Solaris manpages
27.\"
ce515cd4
MK
28.\" Modified Thu Jul 25 14:43:46 MET DST 1996 by Michael Haardt
29.\" <michael@cantor.informatik.rwth-aachen.de>
fea681da 30.\"
8660ef9f 31.TH GETUTENT 3 2014-05-28 "" "Linux Programmer's Manual"
fea681da 32.SH NAME
ce515cd4
MK
33getutent, getutid, getutline, pututline, setutent, endutent,
34utmpname \- access utmp file entries
fea681da
MK
35.SH SYNOPSIS
36.B #include <utmp.h>
37.sp
38.B struct utmp *getutent(void);
39.br
bf8f95ee 40.BI "struct utmp *getutid(const struct utmp *" ut );
fea681da 41.br
bf8f95ee 42.BI "struct utmp *getutline(const struct utmp *" ut );
fea681da 43.sp
bf8f95ee 44.BI "struct utmp *pututline(const struct utmp *" ut );
fea681da
MK
45.sp
46.B void setutent(void);
47.br
48.B void endutent(void);
49.sp
ce515cd4 50.BI "int utmpname(const char *" file );
fea681da 51.SH DESCRIPTION
490b25e1
MK
52New applications should use the POSIX.1-specified "utmpx" versions of
53these functions; see CONFORMING TO.
54
60a90ecd
MK
55.BR utmpname ()
56sets the name of the utmp-format file for the other utmp
c13182ef 57functions to access.
60a90ecd
MK
58If
59.BR utmpname ()
60is not used to set the filename
fea681da
MK
61before the other functions are used, they assume \fB_PATH_UTMP\fP, as
62defined in \fI<paths.h>\fP.
63.PP
60a90ecd
MK
64.BR setutent ()
65rewinds the file pointer to the beginning of the utmp file.
acabf1c1 66It is generally a good idea to call it before any of the other
fea681da
MK
67functions.
68.PP
60a90ecd
MK
69.BR endutent ()
70closes the utmp file.
c13182ef 71It should be called when the user
fea681da
MK
72code is done accessing the file with the other functions.
73.PP
60a90ecd 74.BR getutent ()
acabf1c1 75reads a line from the current file position in the utmp file.
c13182ef 76It returns a pointer to a structure containing the fields of
fea681da 77the line.
8404ecb5 78The definition of this structure is shown in
ff6953dc 79.BR utmp (5).
fea681da 80.PP
60a90ecd
MK
81.BR getutid ()
82searches forward from the current file position in the utmp
c13182ef 83file based upon \fIut\fP.
acabf1c1 84If \fIut\->ut_type\fP is one of \fBRUN_LVL\fP,
60a90ecd
MK
85\fBBOOT_TIME\fP, \fBNEW_TIME\fP, or \fBOLD_TIME\fP,
86.BR getutid ()
87will
acabf1c1
MK
88find the first entry whose \fIut_type\fP field matches \fIut\->ut_type\fP.
89If \fIut\->ut_type\fP is one of \fBINIT_PROCESS\fP, \fBLOGIN_PROCESS\fP,
60a90ecd
MK
90\fBUSER_PROCESS\fP, or \fBDEAD_PROCESS\fP,
91.BR getutid ()
92will find the
acabf1c1
MK
93first entry whose
94.I ut_id
95field matches \fIut\->ut_id\fP.
fea681da 96.PP
60a90ecd 97.BR getutline ()
acabf1c1
MK
98searches forward from the current file position in the utmp file.
99It scans entries whose
100.I ut_type
101is \fBUSER_PROCESS\fP
102or \fBLOGIN_PROCESS\fP and returns the first one whose
103.I ut_line
104field
105matches \fIut\->ut_line\fP.
fea681da 106.PP
60a90ecd 107.BR pututline ()
acabf1c1
MK
108writes the
109.I utmp
110structure \fIut\fP into the utmp file.
60a90ecd
MK
111It uses
112.BR getutid ()
113to search for the proper place in the file to insert
c13182ef
MK
114the new entry.
115If it cannot find an appropriate slot for \fIut\fP,
60a90ecd
MK
116.BR pututline ()
117will append the new entry to the end of the file.
47297adb 118.SH RETURN VALUE
60a90ecd
MK
119.BR getutent (),
120.BR getutid (),
60a90ecd 121and
043945ef 122.BR getutline ()
853a397f
MK
123return a pointer to a \fIstruct utmp\fP on success,
124and NULL on failure (which includes the "record not found" case).
89f9f5b2 125This \fIstruct utmp\fP is allocated in static storage, and may be
fea681da 126overwritten by subsequent calls.
ce515cd4 127
043945ef
MK
128On success
129.BR pututline ()
130returns
131.IR ut ;
132on failure, it returns NULL.
133
ce515cd4
MK
134.BR utmpname ()
135returns 0 if the new name was successfully stored, or \-1 on failure.
b3adaa68
MK
136
137In the event of an error, these functions
138.I errno
139set to indicate the cause.
2b587e09
MK
140.SH ERRORS
141.TP
142.B ENOMEM
143Out of memory.
144.TP
145.B ESRCH
146Record not found.
147.PP
148.BR setutent (),
dbf9befc 149.BR pututline (),
2b587e09 150and the
0ae31c56 151.BR getut* ()
2b587e09
MK
152functions can also fail for the reasons described in
153.BR open (2).
2b2581ee
MK
154.SH FILES
155/var/run/utmp database of currently logged-in users
156.br
157/var/log/wtmp database of past user logins
47297adb 158.SH CONFORMING TO
2b2581ee
MK
159XPG2, SVr4.
160.LP
161In XPG2 and SVID 2 the function
162.BR pututline ()
163is documented to return void, and that is what it does on many systems
164(AIX, HP-UX, Linux libc5).
988db661 165HP-UX introduces a new function
2b2581ee 166.BR _pututline ()
988db661 167with the prototype given above for
2b2581ee
MK
168.BR pututline ()
169(also found in Linux libc5).
170.LP
171All these functions are obsolete now on non-Linux systems.
172POSIX.1-2001, following SUSv1,
173does not have any of these functions, but instead uses
174.sp
175.B #include <utmpx.h>
176.sp
177.B struct utmpx *getutxent(void);
178.br
179.B struct utmpx *getutxid(const struct utmpx *);
180.br
181.B struct utmpx *getutxline(const struct utmpx *);
182.br
183.B struct utmpx *pututxline(const struct utmpx *);
184.br
185.B void setutxent(void);
186.br
187.B void endutxent(void);
490b25e1
MK
188.PP
189These functions are provided by glibc,
190and perform the same task as their equivalents without the "x", but use
191.IR "struct utmpx" ,
192defined on Linux to be the same as
193.IR "struct utmp" .
194For completeness, glibc also provides
195.BR utmpxname (),
196although this function is not specified by POSIX.1.
197.PP
5018c1b4
MK
198On some other systems,
199the \fIutmpx\fP structure is a superset of the \fIutmp\fP structure,
5412da6f
MK
200with additional fields, and larger versions of the existing fields,
201and parallel files are maintained, often
2b2581ee
MK
202.I /var/*/utmpx
203and
204.IR /var/*/wtmpx .
205.LP
5412da6f
MK
206Linux glibc on the other hand does not use a parallel \fIutmpx\fP file
207since its \fIutmp\fP structure is already large enough.
0b07fbdc
MK
208The "x" functions listed above are just aliases for
209their counterparts without the "x" (e.g.,
210.BR getutxent ()
211is an alias for
662902bf 212.BR getutent ()).
d597239c 213.SH NOTES
c634028a 214.SS Glibc notes
6b8fa90b 215The above functions are not thread-safe.
c13182ef 216Glibc adds reentrant versions
fea681da
MK
217.sp
218.nf
b80f966b 219.BR "#define _GNU_SOURCE" " /* or _SVID_SOURCE or _BSD_SOURCE;
453a26b8 220.RB "\& see " feature_test_macros "(7) */"
fea681da
MK
221.B #include <utmp.h>
222.sp
223.BI "int getutent_r(struct utmp *" ubuf ", struct utmp **" ubufp );
224.sp
225.BI "int getutid_r(struct utmp *" ut ,
226.BI " struct utmp *" ubuf ", struct utmp **" ubufp );
227.sp
228.BI "int getutline_r(struct utmp *" ut ,
229.BI " struct utmp *" ubuf ", struct utmp **" ubufp );
230.fi
231.sp
232These functions are GNU extensions, analogs of the functions of the
c13182ef
MK
233same name without the _r suffix.
234The
fea681da 235.I ubuf
c4bb193f 236argument gives these functions a place to store their result.
fea681da 237On success they return 0, and a pointer to the result is written in
acabf1c1 238.IR *ubufp .
dec985f9 239On error, these functions return \-1.
00ac6ce4 240There are no utmpx equivalents of the above functions.
8404ecb5 241(POSIX.1 does not specify such functions.)
fea681da
MK
242.SH EXAMPLE
243The following example adds and removes a utmp record, assuming it is run
c13182ef
MK
244from within a pseudo terminal.
245For usage in a real application, you
246should check the return values of
fb186734 247.BR getpwuid (3)
c13182ef 248and
fb186734 249.BR ttyname (3).
fea681da
MK
250.PP
251.nf
252#include <string.h>
253#include <stdlib.h>
254#include <pwd.h>
255#include <unistd.h>
256#include <utmp.h>
257
c13182ef 258int
cf0a9ace 259main(int argc, char *argv[])
fea681da 260{
cf0a9ace 261 struct utmp entry;
fea681da 262
cf0a9ace 263 system("echo before adding entry:;who");
fea681da 264
8de12063
MK
265 entry.ut_type = USER_PROCESS;
266 entry.ut_pid = getpid();
267 strcpy(entry.ut_line, ttyname(STDIN_FILENO) + strlen("/dev/"));
cf0a9ace 268 /* only correct for ptys named /dev/tty[pqr][0\-9a\-z] */
8de12063 269 strcpy(entry.ut_id, ttyname(STDIN_FILENO) + strlen("/dev/tty"));
cf0a9ace 270 time(&entry.ut_time);
8de12063
MK
271 strcpy(entry.ut_user, getpwuid(getuid())\->pw_name);
272 memset(entry.ut_host, 0, UT_HOSTSIZE);
273 entry.ut_addr = 0;
cf0a9ace
MK
274 setutent();
275 pututline(&entry);
fea681da 276
cf0a9ace 277 system("echo after adding entry:;who");
fea681da 278
8de12063
MK
279 entry.ut_type = DEAD_PROCESS;
280 memset(entry.ut_line, 0, UT_LINESIZE);
281 entry.ut_time = 0;
282 memset(entry.ut_user, 0, UT_NAMESIZE);
cf0a9ace
MK
283 setutent();
284 pututline(&entry);
fea681da 285
cf0a9ace 286 system("echo after removing entry:;who");
fea681da 287
cf0a9ace 288 endutent();
5bc8c34c 289 exit(EXIT_SUCCESS);
fea681da
MK
290}
291.fi
47297adb 292.SH SEE ALSO
0ad7707a 293.BR getutmp (3),
0a4f8b7b 294.BR utmp (5)