1GETUTENT(3)                Linux Programmer's Manual               GETUTENT(3)
2
3
4

NAME

6       getutent, getutid, getutline, pututline, setutent, endutent, utmpname -
7       access utmp file entries
8

SYNOPSIS

10       #include <utmp.h>
11
12       struct utmp *getutent(void);
13       struct utmp *getutid(const struct utmp *ut);
14       struct utmp *getutline(const struct utmp *ut);
15
16       struct utmp *pututline(const struct utmp *ut);
17
18       void setutent(void);
19       void endutent(void);
20
21       int utmpname(const char *file);
22

DESCRIPTION

24       New applications should use the POSIX.1-specified "utmpx"  versions  of
25       these functions; see CONFORMING TO.
26
27       utmpname()  sets  the  name  of the utmp-format file for the other utmp
28       functions to access.  If utmpname() is not used to set the filename be‐
29       fore  the  other functions are used, they assume _PATH_UTMP, as defined
30       in <paths.h>.
31
32       setutent() rewinds the file pointer to the beginning of the utmp  file.
33       It  is  generally  a good idea to call it before any of the other func‐
34       tions.
35
36       endutent() closes the utmp file.  It should be  called  when  the  user
37       code is done accessing the file with the other functions.
38
39       getutent()  reads  a  line  from  the current file position in the utmp
40       file.  It returns a pointer to a structure containing the fields of the
41       line.  The definition of this structure is shown in utmp(5).
42
43       getutid()  searches  forward from the current file position in the utmp
44       file based upon ut.  If  ut->ut_type  is  one  of  RUN_LVL,  BOOT_TIME,
45       NEW_TIME,  or  OLD_TIME,  getutid()  will  find  the  first entry whose
46       ut_type  field  matches  ut->ut_type.   If  ut->ut_type   is   one   of
47       INIT_PROCESS,  LOGIN_PROCESS,  USER_PROCESS, or DEAD_PROCESS, getutid()
48       will find the first entry whose ut_id field matches ut->ut_id.
49
50       getutline() searches forward from the current file position in the utmp
51       file.   It scans entries whose ut_type is USER_PROCESS or LOGIN_PROCESS
52       and returns the first one whose ut_line field matches ut->ut_line.
53
54       pututline() writes the utmp structure ut into the utmp file.   It  uses
55       getutid()  to search for the proper place in the file to insert the new
56       entry.  If it cannot find an appropriate slot for ut, pututline()  will
57       append the new entry to the end of the file.
58

RETURN VALUE

60       getutent(),  getutid(),  and  getutline()  return a pointer to a struct
61       utmp on success, and NULL on failure (which includes  the  "record  not
62       found" case).  This struct utmp is allocated in static storage, and may
63       be overwritten by subsequent calls.
64
65       On success pututline() returns ut; on failure, it returns NULL.
66
67       utmpname() returns 0 if the new name was successfully stored, or -1  on
68       failure.
69
70       On failure, these functions errno set to indicate the error.
71

ERRORS

73       ENOMEM Out of memory.
74
75       ESRCH  Record not found.
76
77       setutent(),  pututline(),  and the getut*() functions can also fail for
78       the reasons described in open(2).
79

FILES

81       /var/run/utmp
82              database of currently logged-in users
83
84       /var/log/wtmp
85              database of past user logins
86

ATTRIBUTES

88       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
89       tributes(7).
90
91       ┌────────────┬───────────────┬─────────────────────────────────────────┐
92Interface   Attribute     Value                                   
93       ├────────────┼───────────────┼─────────────────────────────────────────┤
94getutent()  │ Thread safety │ MT-Unsafe init race:utent race:utentbuf │
95       │            │               │ sig:ALRM timer                          │
96       ├────────────┼───────────────┼─────────────────────────────────────────┤
97getutid(),  │ Thread safety │ MT-Unsafe init race:utent sig:ALRM      │
98getutline() │               │ timer                                   │
99       ├────────────┼───────────────┼─────────────────────────────────────────┤
100pututline() │ Thread safety │ MT-Unsafe race:utent sig:ALRM timer     │
101       ├────────────┼───────────────┼─────────────────────────────────────────┤
102setutent(), │ Thread safety │ MT-Unsafe race:utent                    │
103endutent(), │               │                                         │
104utmpname()  │               │                                         │
105       └────────────┴───────────────┴─────────────────────────────────────────┘
106       In the above table, utent in race:utent signifies that if  any  of  the
107       functions  setutent(), getutent(), getutid(), getutline(), pututline(),
108       utmpname(), or endutent() are used in parallel in different threads  of
109       a program, then data races could occur.
110

CONFORMING TO

112       XPG2, SVr4.
113
114       In  XPG2  and  SVID  2 the function pututline() is documented to return
115       void, and that is what it does on many systems (AIX, HP-UX).  HP-UX in‐
116       troduces a new function _pututline() with the prototype given above for
117       pututline().
118
119       All  these  functions  are   obsolete   now   on   non-Linux   systems.
120       POSIX.1-2001  and  POSIX.1-2008,  following SUSv1, does not have any of
121       these functions, but instead uses
122
123           #include <utmpx.h>
124
125           struct utmpx *getutxent(void);
126           struct utmpx *getutxid(const struct utmpx *);
127           struct utmpx *getutxline(const struct utmpx *);
128           struct utmpx *pututxline(const struct utmpx *);
129           void setutxent(void);
130           void endutxent(void);
131
132       These functions are provided by glibc, and perform  the  same  task  as
133       their  equivalents  without  the  "x", but use struct utmpx, defined on
134       Linux to be the same as struct utmp.  For completeness, glibc also pro‐
135       vides utmpxname(), although this function is not specified by POSIX.1.
136
137       On  some  other  systems, the utmpx structure is a superset of the utmp
138       structure, with additional fields, and larger versions of the  existing
139       fields,  and  parallel  files  are  maintained,  often /var/*/utmpx and
140       /var/*/wtmpx.
141
142       Linux glibc on the other hand does not use a parallel utmpx file  since
143       its  utmp  structure is already large enough.  The "x" functions listed
144       above are just aliases for their counterparts without  the  "x"  (e.g.,
145       getutxent() is an alias for getutent()).
146

NOTES

148   Glibc notes
149       The above functions are not thread-safe.  Glibc adds reentrant versions
150
151       #include <utmp.h>
152
153       int getutent_r(struct utmp *ubuf, struct utmp **ubufp);
154       int getutid_r(struct utmp *ut,
155                     struct utmp *ubuf, struct utmp **ubufp);
156       int getutline_r(struct utmp *ut,
157                       struct utmp *ubuf, struct utmp **ubufp);
158
159       Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
160
161       getutent_r(), getutid_r(), getutline_r():
162           _GNU_SOURCE
163               || /* Since glibc 2.19: */ _DEFAULT_SOURCE
164               || /* Glibc <= 2.19: */    _SVID_SOURCE || _BSD_SOURCE
165
166       These  functions  are  GNU  extensions, analogs of the functions of the
167       same name without the _r suffix.  The ubuf argument gives  these  func‐
168       tions  a place to store their result.  On success, they return 0, and a
169       pointer to the result is written in *ubufp.  On error, these  functions
170       return  -1.   There  are  no  utmpx equivalents of the above functions.
171       (POSIX.1 does not specify such functions.)
172

EXAMPLES

174       The following example adds and removes a utmp record,  assuming  it  is
175       run  from  within  a pseudo terminal.  For usage in a real application,
176       you should check the return values of getpwuid(3) and ttyname(3).
177
178       #include <string.h>
179       #include <stdlib.h>
180       #include <pwd.h>
181       #include <unistd.h>
182       #include <utmp.h>
183       #include <time.h>
184
185       int
186       main(int argc, char *argv[])
187       {
188           struct utmp entry;
189
190           system("echo before adding entry:;who");
191
192           entry.ut_type = USER_PROCESS;
193           entry.ut_pid = getpid();
194           strcpy(entry.ut_line, ttyname(STDIN_FILENO) + strlen("/dev/"));
195           /* only correct for ptys named /dev/tty[pqr][0-9a-z] */
196           strcpy(entry.ut_id, ttyname(STDIN_FILENO) + strlen("/dev/tty"));
197           time(&entry.ut_time);
198           strcpy(entry.ut_user, getpwuid(getuid())->pw_name);
199           memset(entry.ut_host, 0, UT_HOSTSIZE);
200           entry.ut_addr = 0;
201           setutent();
202           pututline(&entry);
203
204           system("echo after adding entry:;who");
205
206           entry.ut_type = DEAD_PROCESS;
207           memset(entry.ut_line, 0, UT_LINESIZE);
208           entry.ut_time = 0;
209           memset(entry.ut_user, 0, UT_NAMESIZE);
210           setutent();
211           pututline(&entry);
212
213           system("echo after removing entry:;who");
214
215           endutent();
216           exit(EXIT_SUCCESS);
217       }
218

SEE ALSO

220       getutmp(3), utmp(5)
221

COLOPHON

223       This page is part of release 5.12 of the Linux  man-pages  project.   A
224       description  of  the project, information about reporting bugs, and the
225       latest    version    of    this    page,    can     be     found     at
226       https://www.kernel.org/doc/man-pages/.
227
228
229
230                                  2021-03-22                       GETUTENT(3)
Impressum