1UTMP(5) Linux Programmer's Manual UTMP(5)
2
3
4
6 utmp, wtmp - login records
7
9 #include <utmp.h>
10
12 The utmp file allows one to discover information about who is currently
13 using the system. There may be more users currently using the system,
14 because not all programs use utmp logging.
15
16 Warning: utmp must not be writable by the user class "other", because
17 many system programs (foolishly) depend on its integrity. You risk
18 faked system logfiles and modifications of system files if you leave
19 utmp writable to any user other than the owner and group owner of the
20 file.
21
22 The file is a sequence of utmp structures, declared as follows in
23 <utmp.h> (note that this is only one of several definitions around;
24 details depend on the version of libc):
25 /* Values for ut_type field, below */
26
27 #define EMPTY 0 /* Record does not contain valid info
28 (formerly known as UT_UNKNOWN on Linux) */
29 #define RUN_LVL 1 /* Change in system run-level (see
30 init(8)) */
31 #define BOOT_TIME 2 /* Time of system boot (in ut_tv) */
32 #define NEW_TIME 3 /* Time after system clock change
33 (in ut_tv) */
34 #define OLD_TIME 4 /* Time before system clock change
35 (in ut_tv) */
36 #define INIT_PROCESS 5 /* Process spawned by init(8) */
37 #define LOGIN_PROCESS 6 /* Session leader process for user login */
38 #define USER_PROCESS 7 /* Normal process */
39 #define DEAD_PROCESS 8 /* Terminated process */
40 #define ACCOUNTING 9 /* Not implemented */
41
42 #define UT_LINESIZE 32
43 #define UT_NAMESIZE 32
44 #define UT_HOSTSIZE 256
45
46 struct exit_status { /* Type for ut_exit, below */
47 short int e_termination; /* Process termination status */
48 short int e_exit; /* Process exit status */
49 };
50
51 struct utmp {
52 short ut_type; /* Type of record */
53 pid_t ut_pid; /* PID of login process */
54 char ut_line[UT_LINESIZE]; /* Device name of tty - "/dev/" */
55 char ut_id[4]; /* Terminal name suffix,
56 or inittab(5) ID */
57 char ut_user[UT_NAMESIZE]; /* Username */
58 char ut_host[UT_HOSTSIZE]; /* Hostname for remote login, or
59 kernel version for run-level
60 messages */
61 struct exit_status ut_exit; /* Exit status of a process
62 marked as DEAD_PROCESS; not
63 used by Linux init (1 */
64 /* The ut_session and ut_tv fields must be the same size when
65 compiled 32- and 64-bit. This allows data files and shared
66 memory to be shared between 32- and 64-bit applications. */
67 #if __WORDSIZE == 64 && defined __WORDSIZE_COMPAT32
68 int32_t ut_session; /* Session ID (getsid(2)),
69 used for windowing */
70 struct {
71 int32_t tv_sec; /* Seconds */
72 int32_t tv_usec; /* Microseconds */
73 } ut_tv; /* Time entry was made */
74 #else
75 long ut_session; /* Session ID */
76 struct timeval ut_tv; /* Time entry was made */
77 #endif
78
79 int32_t ut_addr_v6[4]; /* Internet address of remote
80 host; IPv4 address uses
81 just ut_addr_v6[0] */
82 char __unused[20]; /* Reserved for future use */
83 };
84
85 /* Backward compatibility hacks */
86 #define ut_name ut_user
87 #ifndef _NO_UT_TIME
88 #define ut_time ut_tv.tv_sec
89 #endif
90 #define ut_xtime ut_tv.tv_sec
91 #define ut_addr ut_addr_v6[0]
92
93 This structure gives the name of the special file associated with the
94 user's terminal, the user's login name, and the time of login in the
95 form of time(2). String fields are terminated by a null byte ('\0') if
96 they are shorter than the size of the field.
97
98 The first entries ever created result from init(1) processing init‐
99 tab(5). Before an entry is processed, though, init(1) cleans up utmp
100 by setting ut_type to DEAD_PROCESS, clearing ut_user, ut_host, and
101 ut_time with null bytes for each record which ut_type is not
102 DEAD_PROCESS or RUN_LVL and where no process with PID ut_pid exists.
103 If no empty record with the needed ut_id can be found, init(1) creates
104 a new one. It sets ut_id from the inittab, ut_pid and ut_time to the
105 current values, and ut_type to INIT_PROCESS.
106
107 mingetty(8) (or agetty(8)) locates the entry by the PID, changes
108 ut_type to LOGIN_PROCESS, changes ut_time, sets ut_line, and waits for
109 connection to be established. login(1), after a user has been authen‐
110 ticated, changes ut_type to USER_PROCESS, changes ut_time, and sets
111 ut_host and ut_addr. Depending on mingetty(8) (or agetty(8)) and
112 login(1), records may be located by ut_line instead of the preferable
113 ut_pid.
114
115 When init(1) finds that a process has exited, it locates its utmp entry
116 by ut_pid, sets ut_type to DEAD_PROCESS, and clears ut_user, ut_host
117 and ut_time with null bytes.
118
119 xterm(1) and other terminal emulators directly create a USER_PROCESS
120 record and generate the ut_id by using the string that suffix part of
121 the terminal name (the characters following /dev/[pt]ty). If they find
122 a DEAD_PROCESS for this ID, they recycle it, otherwise they create a
123 new entry. If they can, they will mark it as DEAD_PROCESS on exiting
124 and it is advised that they null ut_line, ut_time, ut_user, and ut_host
125 as well.
126
127 telnetd(8) sets up a LOGIN_PROCESS entry and leaves the rest to
128 login(1) as usual. After the telnet session ends, telnetd(8) cleans up
129 utmp in the described way.
130
131 The wtmp file records all logins and logouts. Its format is exactly
132 like utmp except that a null username indicates a logout on the associ‐
133 ated terminal. Furthermore, the terminal name ~ with username shutdown
134 or reboot indicates a system shutdown or reboot and the pair of termi‐
135 nal names |/} logs the old/new system time when date(1) changes it.
136 wtmp is maintained by login(1), init(1), and some versions of getty(8)
137 (e.g., mingetty(8) or agetty(8)). None of these programs creates the
138 file, so if it is removed, record-keeping is turned off.
139
141 /var/run/utmp
142 /var/log/wtmp
143
145 POSIX.1 does not specify a utmp structure, but rather one named utmpx,
146 with specifications for the fields ut_type, ut_pid, ut_line, ut_id,
147 ut_user, and ut_tv. POSIX.1 does not specify the lengths of the
148 ut_line and ut_user fields.
149
150 Linux defines the utmpx structure to be the same as the utmp structure.
151
152 Comparison with historical systems
153 Linux utmp entries conform neither to v7/BSD nor to System V; they are
154 a mix of the two.
155
156 v7/BSD has fewer fields; most importantly it lacks ut_type, which
157 causes native v7/BSD-like programs to display (for example) dead or
158 login entries. Further, there is no configuration file which allocates
159 slots to sessions. BSD does so because it lacks ut_id fields.
160
161 In Linux (as in System V), the ut_id field of a record will never
162 change once it has been set, which reserves that slot without needing a
163 configuration file. Clearing ut_id may result in race conditions lead‐
164 ing to corrupted utmp entries and potential security holes. Clearing
165 the abovementioned fields by filling them with null bytes is not
166 required by System V semantics, but makes it possible to run many pro‐
167 grams which assume BSD semantics and which do not modify utmp. Linux
168 uses the BSD conventions for line contents, as documented above.
169
170 System V has no ut_host or ut_addr_v6 fields.
171
173 Unlike various other systems, where utmp logging can be disabled by
174 removing the file, utmp must always exist on Linux. If you want to
175 disable who(1), then do not make utmp world readable.
176
177 The file format is machine-dependent, so it is recommended that it be
178 processed only on the machine architecture where it was created.
179
180 Note that on biarch platforms, that is, systems which can run both
181 32-bit and 64-bit applications (x86-64, ppc64, s390x, etc.), ut_tv is
182 the same size in 32-bit mode as in 64-bit mode. The same goes for
183 ut_session and ut_time if they are present. This allows data files and
184 shared memory to be shared between 32-bit and 64-bit applications.
185 This is achieved by changing the type of ut_session to int32_t, and
186 that of ut_tv to a struct with two int32_t fields tv_sec and tv_usec.
187 Since ut_tv may not be the same as struct timeval, then instead of the
188 call:
189
190 gettimeofday((struct timeval *) &ut.ut_tv, NULL);
191
192 the following method of setting this field is recommended:
193
194 struct utmp ut;
195 struct timeval tv;
196
197 gettimeofday(&tv, NULL);
198 ut.ut_tv.tv_sec = tv.tv_sec;
199 ut.ut_tv.tv_usec = tv.tv_usec;
200
202 ac(1), date(1), init(1), last(1), login(1), logname(1), lslogins(1),
203 users(1), utmpdump(1), who(1), getutent(3), getutmp(3), login(3),
204 logout(3), logwtmp(3), updwtmp(3)
205
207 This page is part of release 4.15 of the Linux man-pages project. A
208 description of the project, information about reporting bugs, and the
209 latest version of this page, can be found at
210 https://www.kernel.org/doc/man-pages/.
211
212
213
214Linux 2017-09-15 UTMP(5)