OSDN Git Service

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