]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getutent.3
getent.1, _syscall.2, acct.2, adjtimex.2, bdflush.2, brk.2, cacheflush.2, getsid...
[thirdparty/man-pages.git] / man3 / getutent.3
1 .\" Copyright 1995 Mark D. Roth (roth@uiuc.edu)
2 .\"
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.
7 .\"
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.
12 .\"
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.
17 .\"
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/>.
21 .\"
22 .\" References consulted:
23 .\" Linux libc source code
24 .\" Solaris manpages
25 .\"
26 .\" Modified Thu Jul 25 14:43:46 MET DST 1996 by Michael Haardt
27 .\" <michael@cantor.informatik.rwth-aachen.de>
28 .\"
29 .TH GETUTENT 3 2008-06-29 "" "Linux Programmer's Manual"
30 .SH NAME
31 getutent, getutid, getutline, pututline, setutent, endutent,
32 utmpname \- access utmp file entries
33 .SH SYNOPSIS
34 .B #include <utmp.h>
35 .sp
36 .B struct utmp *getutent(void);
37 .br
38 .BI "struct utmp *getutid(struct utmp *" ut );
39 .br
40 .BI "struct utmp *getutline(struct utmp *" ut );
41 .sp
42 .BI "struct utmp *pututline(struct utmp *" ut );
43 .sp
44 .B void setutent(void);
45 .br
46 .B void endutent(void);
47 .sp
48 .BI "int utmpname(const char *" file );
49 .SH DESCRIPTION
50 New applications should use the POSIX.1-specified "utmpx" versions of
51 these functions; see CONFORMING TO.
52
53 .BR utmpname ()
54 sets the name of the utmp-format file for the other utmp
55 functions to access.
56 If
57 .BR utmpname ()
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.
61 .PP
62 .BR setutent ()
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
65 functions.
66 .PP
67 .BR endutent ()
68 closes the utmp file.
69 It should be called when the user
70 code is done accessing the file with the other functions.
71 .PP
72 .BR getutent ()
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
75 the line.
76 The definition of this structure is shown in
77 .BR utmp (5).
78 .PP
79 .BR getutid ()
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,
84 .BR getutid ()
85 will
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,
89 .BR getutid ()
90 will find the
91 first entry whose
92 .I ut_id
93 field matches \fIut\->ut_id\fP.
94 .PP
95 .BR getutline ()
96 searches forward from the current file position in the utmp file.
97 It scans entries whose
98 .I ut_type
99 is \fBUSER_PROCESS\fP
100 or \fBLOGIN_PROCESS\fP and returns the first one whose
101 .I ut_line
102 field
103 matches \fIut\->ut_line\fP.
104 .PP
105 .BR pututline ()
106 writes the
107 .I utmp
108 structure \fIut\fP into the utmp file.
109 It uses
110 .BR getutid ()
111 to search for the proper place in the file to insert
112 the new entry.
113 If it cannot find an appropriate slot for \fIut\fP,
114 .BR pututline ()
115 will append the new entry to the end of the file.
116 .SH RETURN VALUE
117 .BR getutent (),
118 .BR getutid (),
119 and
120 .BR getutline ()
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.
125
126 On success
127 .BR pututline ()
128 returns
129 .IR ut ;
130 on failure, it returns NULL.
131
132 .BR utmpname ()
133 returns 0 if the new name was successfully stored, or \-1 on failure.
134 .SH ERRORS
135 .TP
136 .B ENOMEM
137 Out of memory.
138 .TP
139 .B ESRCH
140 Record not found.
141 .PP
142 .BR setutent (),
143 .BR pututline (),
144 and the
145 .B getut* ()
146 functions can also fail for the reasons described in
147 .BR open (2).
148 .SH FILES
149 /var/run/utmp database of currently logged-in users
150 .br
151 /var/log/wtmp database of past user logins
152 .SH CONFORMING TO
153 XPG2, SVr4.
154 .LP
155 In XPG2 and SVID 2 the function
156 .BR pututline ()
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
160 .BR _pututline ()
161 with the prototype given above for
162 .BR pututline ()
163 (also found in Linux libc5).
164 .LP
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
168 .sp
169 .B #include <utmpx.h>
170 .sp
171 .B struct utmpx *getutxent(void);
172 .br
173 .B struct utmpx *getutxid(const struct utmpx *);
174 .br
175 .B struct utmpx *getutxline(const struct utmpx *);
176 .br
177 .B struct utmpx *pututxline(const struct utmpx *);
178 .br
179 .B void setutxent(void);
180 .br
181 .B void endutxent(void);
182 .PP
183 These functions are provided by glibc,
184 and perform the same task as their equivalents without the "x", but use
185 .IR "struct utmpx" ,
186 defined on Linux to be the same as
187 .IR "struct utmp" .
188 For completeness, glibc also provides
189 .BR utmpxname (),
190 although this function is not specified by POSIX.1.
191 .PP
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
196 .I /var/*/utmpx
197 and
198 .IR /var/*/wtmpx .
199 .LP
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.,
204 .BR getutxent ()
205 is an alias for
206 .BR getutent ()).
207 .SH NOTES
208 .SS Glibc notes
209 The above functions are not thread-safe.
210 Glibc adds reentrant versions
211 .sp
212 .nf
213 .BR "#define _GNU_SOURCE" " /* or _SVID_SOURCE or _BSD_SOURCE;
214 .RB "\& see " feature_test_macros "(7) */"
215 .B #include <utmp.h>
216 .sp
217 .BI "int getutent_r(struct utmp *" ubuf ", struct utmp **" ubufp );
218 .sp
219 .BI "int getutid_r(struct utmp *" ut ,
220 .BI " struct utmp *" ubuf ", struct utmp **" ubufp );
221 .sp
222 .BI "int getutline_r(struct utmp *" ut ,
223 .BI " struct utmp *" ubuf ", struct utmp **" ubufp );
224 .fi
225 .sp
226 These functions are GNU extensions, analogs of the functions of the
227 same name without the _r suffix.
228 The
229 .I ubuf
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
232 .IR *ubufp .
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.)
236 .SH EXAMPLE
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
241 .BR getpwuid (3)
242 and
243 .BR ttyname (3).
244 .PP
245 .nf
246 #include <string.h>
247 #include <stdlib.h>
248 #include <pwd.h>
249 #include <unistd.h>
250 #include <utmp.h>
251
252 int
253 main(int argc, char *argv[])
254 {
255 struct utmp entry;
256
257 system("echo before adding entry:;who");
258
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);
267 entry.ut_addr = 0;
268 setutent();
269 pututline(&entry);
270
271 system("echo after adding entry:;who");
272
273 entry.ut_type = DEAD_PROCESS;
274 memset(entry.ut_line, 0, UT_LINESIZE);
275 entry.ut_time = 0;
276 memset(entry.ut_user, 0, UT_NAMESIZE);
277 setutent();
278 pututline(&entry);
279
280 system("echo after removing entry:;who");
281
282 endutent();
283 exit(EXIT_SUCCESS);
284 }
285 .fi
286 .SH SEE ALSO
287 .BR getutmp (3),
288 .BR utmp (5)