1getutent(3)                Library Functions Manual                getutent(3)
2
3
4

NAME

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

LIBRARY

10       Standard C library (libc, -lc)
11

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

FILES

84       /var/run/utmp
85              database of currently logged-in users
86
87       /var/log/wtmp
88              database of past user logins
89

ATTRIBUTES

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

STANDARDS

115       None.
116

HISTORY

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

NOTES

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

EXAMPLES

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

SEE ALSO

226       getutmp(3), utmp(5)
227
228
229
230Linux man-pages 6.04              2023-03-30                       getutent(3)
Impressum