]>
Commit | Line | Data |
---|---|---|
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 | .\" |
bffbb22f | 31 | .TH GETUTENT 3 2020-06-09 "" "Linux Programmer's Manual" |
fea681da | 32 | .SH NAME |
ce515cd4 MK |
33 | getutent, getutid, getutline, pututline, setutent, endutent, |
34 | utmpname \- access utmp file entries | |
fea681da | 35 | .SH SYNOPSIS |
c7db92b9 | 36 | .nf |
fea681da | 37 | .B #include <utmp.h> |
68e4db0a | 38 | .PP |
fea681da | 39 | .B struct utmp *getutent(void); |
bf8f95ee | 40 | .BI "struct utmp *getutid(const struct utmp *" ut ); |
bf8f95ee | 41 | .BI "struct utmp *getutline(const struct utmp *" ut ); |
68e4db0a | 42 | .PP |
bf8f95ee | 43 | .BI "struct utmp *pututline(const struct utmp *" ut ); |
68e4db0a | 44 | .PP |
fea681da | 45 | .B void setutent(void); |
fea681da | 46 | .B void endutent(void); |
68e4db0a | 47 | .PP |
ce515cd4 | 48 | .BI "int utmpname(const char *" file ); |
e3528453 | 49 | .fi |
fea681da | 50 | .SH DESCRIPTION |
490b25e1 MK |
51 | New applications should use the POSIX.1-specified "utmpx" versions of |
52 | these functions; see CONFORMING TO. | |
847e0d88 | 53 | .PP |
60a90ecd MK |
54 | .BR utmpname () |
55 | sets the name of the utmp-format file for the other utmp | |
c13182ef | 56 | functions to access. |
60a90ecd MK |
57 | If |
58 | .BR utmpname () | |
59 | is not used to set the filename | |
fea681da MK |
60 | before the other functions are used, they assume \fB_PATH_UTMP\fP, as |
61 | defined in \fI<paths.h>\fP. | |
62 | .PP | |
60a90ecd MK |
63 | .BR setutent () |
64 | rewinds the file pointer to the beginning of the utmp file. | |
acabf1c1 | 65 | It is generally a good idea to call it before any of the other |
fea681da MK |
66 | functions. |
67 | .PP | |
60a90ecd MK |
68 | .BR endutent () |
69 | closes the utmp file. | |
c13182ef | 70 | It should be called when the user |
fea681da MK |
71 | code is done accessing the file with the other functions. |
72 | .PP | |
60a90ecd | 73 | .BR getutent () |
acabf1c1 | 74 | reads a line from the current file position in the utmp file. |
c13182ef | 75 | It returns a pointer to a structure containing the fields of |
fea681da | 76 | the line. |
8404ecb5 | 77 | The definition of this structure is shown in |
ff6953dc | 78 | .BR utmp (5). |
fea681da | 79 | .PP |
60a90ecd MK |
80 | .BR getutid () |
81 | searches forward from the current file position in the utmp | |
c13182ef | 82 | file based upon \fIut\fP. |
acabf1c1 | 83 | If \fIut\->ut_type\fP is one of \fBRUN_LVL\fP, |
60a90ecd MK |
84 | \fBBOOT_TIME\fP, \fBNEW_TIME\fP, or \fBOLD_TIME\fP, |
85 | .BR getutid () | |
86 | will | |
acabf1c1 MK |
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, | |
60a90ecd MK |
89 | \fBUSER_PROCESS\fP, or \fBDEAD_PROCESS\fP, |
90 | .BR getutid () | |
91 | will find the | |
acabf1c1 MK |
92 | first entry whose |
93 | .I ut_id | |
94 | field matches \fIut\->ut_id\fP. | |
fea681da | 95 | .PP |
60a90ecd | 96 | .BR getutline () |
acabf1c1 MK |
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. | |
fea681da | 105 | .PP |
60a90ecd | 106 | .BR pututline () |
acabf1c1 MK |
107 | writes the |
108 | .I utmp | |
109 | structure \fIut\fP into the utmp file. | |
60a90ecd MK |
110 | It uses |
111 | .BR getutid () | |
112 | to search for the proper place in the file to insert | |
c13182ef MK |
113 | the new entry. |
114 | If it cannot find an appropriate slot for \fIut\fP, | |
60a90ecd MK |
115 | .BR pututline () |
116 | will append the new entry to the end of the file. | |
47297adb | 117 | .SH RETURN VALUE |
60a90ecd MK |
118 | .BR getutent (), |
119 | .BR getutid (), | |
60a90ecd | 120 | and |
043945ef | 121 | .BR getutline () |
853a397f MK |
122 | return a pointer to a \fIstruct utmp\fP on success, |
123 | and NULL on failure (which includes the "record not found" case). | |
89f9f5b2 | 124 | This \fIstruct utmp\fP is allocated in static storage, and may be |
fea681da | 125 | overwritten by subsequent calls. |
847e0d88 | 126 | .PP |
043945ef MK |
127 | On success |
128 | .BR pututline () | |
129 | returns | |
130 | .IR ut ; | |
131 | on failure, it returns NULL. | |
847e0d88 | 132 | .PP |
ce515cd4 MK |
133 | .BR utmpname () |
134 | returns 0 if the new name was successfully stored, or \-1 on failure. | |
847e0d88 | 135 | .PP |
b3adaa68 MK |
136 | In the event of an error, these functions |
137 | .I errno | |
138 | set to indicate the cause. | |
2b587e09 MK |
139 | .SH ERRORS |
140 | .TP | |
141 | .B ENOMEM | |
142 | Out of memory. | |
143 | .TP | |
144 | .B ESRCH | |
145 | Record not found. | |
146 | .PP | |
147 | .BR setutent (), | |
dbf9befc | 148 | .BR pututline (), |
2b587e09 | 149 | and the |
0ae31c56 | 150 | .BR getut* () |
2b587e09 MK |
151 | functions can also fail for the reasons described in |
152 | .BR open (2). | |
2b2581ee | 153 | .SH FILES |
7457600f MK |
154 | .TP |
155 | .I /var/run/utmp | |
156 | database of currently logged-in users | |
157 | .TP | |
158 | .I /var/log/wtmp | |
159 | database of past user logins | |
a5047c38 ZL |
160 | .SH ATTRIBUTES |
161 | For an explanation of the terms used in this section, see | |
162 | .BR attributes (7). | |
163 | .TS | |
164 | allbox; | |
165 | lb lb lbw28 | |
166 | l l l. | |
167 | Interface Attribute Value | |
168 | T{ | |
169 | .BR getutent () | |
170 | T} Thread safety T{ | |
171 | MT-Unsafe init race:utent | |
172 | .br | |
173 | race:utentbuf sig:ALRM timer | |
174 | T} | |
175 | T{ | |
176 | .BR getutid (), | |
177 | .br | |
178 | .BR getutline () | |
179 | T} Thread safety T{ | |
180 | MT-Unsafe init race:utent | |
181 | .br | |
182 | sig:ALRM timer | |
183 | T} | |
184 | T{ | |
185 | .BR pututline () | |
186 | T} Thread safety T{ | |
187 | MT-Unsafe race:utent | |
188 | .br | |
189 | sig:ALRM timer | |
190 | T} | |
191 | T{ | |
192 | .BR setutent (), | |
193 | .br | |
194 | .BR endutent (), | |
195 | .br | |
196 | .BR utmpname () | |
197 | T} Thread safety MT-Unsafe race:utent | |
198 | .TE | |
847e0d88 | 199 | .sp 1 |
a5047c38 ZL |
200 | In the above table, |
201 | .I utent | |
202 | in | |
203 | .I race:utent | |
204 | signifies that if any of the functions | |
bf7bc8b8 MK |
205 | .BR setutent (), |
206 | .BR getutent (), | |
207 | .BR getutid (), | |
208 | .BR getutline (), | |
209 | .BR pututline (), | |
210 | .BR utmpname (), | |
a5047c38 | 211 | or |
bf7bc8b8 | 212 | .BR endutent () |
a5047c38 ZL |
213 | are used in parallel in different threads of a program, |
214 | then data races could occur. | |
47297adb | 215 | .SH CONFORMING TO |
2b2581ee | 216 | XPG2, SVr4. |
dd3568a1 | 217 | .PP |
2b2581ee MK |
218 | In XPG2 and SVID 2 the function |
219 | .BR pututline () | |
220 | is documented to return void, and that is what it does on many systems | |
07fc3765 | 221 | (AIX, HP-UX). |
988db661 | 222 | HP-UX introduces a new function |
2b2581ee | 223 | .BR _pututline () |
988db661 | 224 | with the prototype given above for |
07fc3765 | 225 | .BR pututline (). |
dd3568a1 | 226 | .PP |
2b2581ee | 227 | All these functions are obsolete now on non-Linux systems. |
975dc2bb | 228 | POSIX.1-2001 and POSIX.1-2008, following SUSv1, |
2b2581ee | 229 | does not have any of these functions, but instead uses |
bdd915e2 | 230 | .PP |
71c58278 | 231 | .RS 4 |
bdd915e2 | 232 | .EX |
2b2581ee | 233 | .B #include <utmpx.h> |
bdd915e2 | 234 | .PP |
2b2581ee | 235 | .B struct utmpx *getutxent(void); |
2b2581ee | 236 | .B struct utmpx *getutxid(const struct utmpx *); |
2b2581ee | 237 | .B struct utmpx *getutxline(const struct utmpx *); |
2b2581ee | 238 | .B struct utmpx *pututxline(const struct utmpx *); |
2b2581ee | 239 | .B void setutxent(void); |
2b2581ee | 240 | .B void endutxent(void); |
bdd915e2 | 241 | .EE |
71c58278 | 242 | .RE |
490b25e1 MK |
243 | .PP |
244 | These functions are provided by glibc, | |
245 | and perform the same task as their equivalents without the "x", but use | |
246 | .IR "struct utmpx" , | |
247 | defined on Linux to be the same as | |
248 | .IR "struct utmp" . | |
249 | For completeness, glibc also provides | |
250 | .BR utmpxname (), | |
251 | although this function is not specified by POSIX.1. | |
252 | .PP | |
5018c1b4 MK |
253 | On some other systems, |
254 | the \fIutmpx\fP structure is a superset of the \fIutmp\fP structure, | |
5412da6f MK |
255 | with additional fields, and larger versions of the existing fields, |
256 | and parallel files are maintained, often | |
2b2581ee MK |
257 | .I /var/*/utmpx |
258 | and | |
259 | .IR /var/*/wtmpx . | |
dd3568a1 | 260 | .PP |
5412da6f MK |
261 | Linux glibc on the other hand does not use a parallel \fIutmpx\fP file |
262 | since its \fIutmp\fP structure is already large enough. | |
0b07fbdc MK |
263 | The "x" functions listed above are just aliases for |
264 | their counterparts without the "x" (e.g., | |
265 | .BR getutxent () | |
266 | is an alias for | |
662902bf | 267 | .BR getutent ()). |
d597239c | 268 | .SH NOTES |
c634028a | 269 | .SS Glibc notes |
6b8fa90b | 270 | The above functions are not thread-safe. |
c13182ef | 271 | Glibc adds reentrant versions |
bdd915e2 | 272 | .PP |
71c58278 | 273 | .nf |
fea681da | 274 | .B #include <utmp.h> |
eaa18d3c | 275 | .PP |
fea681da | 276 | .BI "int getutent_r(struct utmp *" ubuf ", struct utmp **" ubufp ); |
fea681da MK |
277 | .BI "int getutid_r(struct utmp *" ut , |
278 | .BI " struct utmp *" ubuf ", struct utmp **" ubufp ); | |
fea681da MK |
279 | .BI "int getutline_r(struct utmp *" ut , |
280 | .BI " struct utmp *" ubuf ", struct utmp **" ubufp ); | |
71c58278 | 281 | .fi |
bdd915e2 | 282 | .PP |
7f0ec8ee MK |
283 | Feature Test Macro Requirements for glibc (see |
284 | .BR feature_test_macros (7)): | |
51f5698d | 285 | .PP |
7f0ec8ee MK |
286 | .BR getutent_r (), |
287 | .BR getutid_r (), | |
288 | .BR getutline_r (): | |
289 | .nf | |
290 | _GNU_SOURCE | |
b15b92a5 MK |
291 | || /* Since glibc 2.19: */ _DEFAULT_SOURCE |
292 | || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE | |
7f0ec8ee | 293 | .fi |
51f5698d | 294 | .PP |
fea681da | 295 | These functions are GNU extensions, analogs of the functions of the |
c13182ef MK |
296 | same name without the _r suffix. |
297 | The | |
fea681da | 298 | .I ubuf |
c4bb193f | 299 | argument gives these functions a place to store their result. |
c49a8e2a | 300 | On success, they return 0, and a pointer to the result is written in |
acabf1c1 | 301 | .IR *ubufp . |
dec985f9 | 302 | On error, these functions return \-1. |
00ac6ce4 | 303 | There are no utmpx equivalents of the above functions. |
8404ecb5 | 304 | (POSIX.1 does not specify such functions.) |
a14af333 | 305 | .SH EXAMPLES |
fea681da | 306 | The following example adds and removes a utmp record, assuming it is run |
c13182ef MK |
307 | from within a pseudo terminal. |
308 | For usage in a real application, you | |
309 | should check the return values of | |
fb186734 | 310 | .BR getpwuid (3) |
c13182ef | 311 | and |
fb186734 | 312 | .BR ttyname (3). |
fea681da | 313 | .PP |
207050fa | 314 | .EX |
fea681da MK |
315 | #include <string.h> |
316 | #include <stdlib.h> | |
317 | #include <pwd.h> | |
318 | #include <unistd.h> | |
319 | #include <utmp.h> | |
ed9e645a | 320 | #include <time.h> |
fea681da | 321 | |
c13182ef | 322 | int |
cf0a9ace | 323 | main(int argc, char *argv[]) |
fea681da | 324 | { |
cf0a9ace | 325 | struct utmp entry; |
fea681da | 326 | |
cf0a9ace | 327 | system("echo before adding entry:;who"); |
fea681da | 328 | |
8de12063 MK |
329 | entry.ut_type = USER_PROCESS; |
330 | entry.ut_pid = getpid(); | |
331 | strcpy(entry.ut_line, ttyname(STDIN_FILENO) + strlen("/dev/")); | |
cf0a9ace | 332 | /* only correct for ptys named /dev/tty[pqr][0\-9a\-z] */ |
8de12063 | 333 | strcpy(entry.ut_id, ttyname(STDIN_FILENO) + strlen("/dev/tty")); |
cf0a9ace | 334 | time(&entry.ut_time); |
8de12063 MK |
335 | strcpy(entry.ut_user, getpwuid(getuid())\->pw_name); |
336 | memset(entry.ut_host, 0, UT_HOSTSIZE); | |
337 | entry.ut_addr = 0; | |
cf0a9ace MK |
338 | setutent(); |
339 | pututline(&entry); | |
fea681da | 340 | |
cf0a9ace | 341 | system("echo after adding entry:;who"); |
fea681da | 342 | |
8de12063 MK |
343 | entry.ut_type = DEAD_PROCESS; |
344 | memset(entry.ut_line, 0, UT_LINESIZE); | |
345 | entry.ut_time = 0; | |
346 | memset(entry.ut_user, 0, UT_NAMESIZE); | |
cf0a9ace MK |
347 | setutent(); |
348 | pututline(&entry); | |
fea681da | 349 | |
cf0a9ace | 350 | system("echo after removing entry:;who"); |
fea681da | 351 | |
cf0a9ace | 352 | endutent(); |
5bc8c34c | 353 | exit(EXIT_SUCCESS); |
fea681da | 354 | } |
207050fa | 355 | .EE |
47297adb | 356 | .SH SEE ALSO |
0ad7707a | 357 | .BR getutmp (3), |
0a4f8b7b | 358 | .BR utmp (5) |