summaryrefslogtreecommitdiffstats
path: root/lib/libc/gen/getutxent.3
blob: 5c8b793602752d47258a1fd6dfad54dbdc02c609 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
.\" Copyright (c) 2010 Ed Schouten <ed@FreeBSD.org>
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $FreeBSD$
.\"
.Dd February 19, 2011
.Dt GETUTXENT 3
.Os
.Sh NAME
.Nm endutxent ,
.Nm getutxent ,
.Nm getutxid ,
.Nm getutxline ,
.Nm getutxuser ,
.Nm pututxline ,
.Nm setutxdb ,
.Nm setutxent
.Nd user accounting database functions
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In utmpx.h
.Ft void
.Fn endutxent "void"
.Ft struct utmpx *
.Fn getutxent "void"
.Ft struct utmpx *
.Fn getutxid "const struct utmpx *id"
.Ft struct utmpx *
.Fn getutxline "const struct utmpx *line"
.Ft struct utmpx *
.Fn getutxuser "const char *user"
.Ft struct utmpx *
.Fn pututxline "const struct utmpx *utmpx"
.Ft int
.Fn setutxdb "int type" "const char *file"
.Ft void
.Fn setutxent "void"
.Sh DESCRIPTION
These functions operate on the user accounting database which stores
records of various system activities, such as user login and logouts,
but also system startups and shutdowns and modifications to the system's
clock.
The system stores these records in three databases, each having a
different purpose:
.Bl -tag -width indent
.It Pa /var/run/utx.active
Log of currently active user login sessions.
This file is similar to the traditional
.Pa utmp
file.
This file only contains process related entries, such as user login and
logout records.
.It Pa /var/log/utx.lastlogin
Log of last user login entries per user.
This file is similar to the traditional
.Pa lastlog
file.
This file only contains user login records for users who have at least
logged in once.
.It Pa /var/log/utx.log
Log of all entries, sorted by date of addition.
This file is similar to the traditional
.Pa wtmp
file.
This file may contain any type of record described below.
.El
.Pp
Each entry in these databases is defined by the structure
.Vt utmpx
found in the include file
.In utmpx.h :
.Bd -literal -offset indent
struct utmpx {
	short           ut_type;    /* Type of entry. */
	struct timeval  ut_tv;      /* Time entry was made. */
	char            ut_id[];    /* Record identifier. */
	pid_t           ut_pid;     /* Process ID. */
	char            ut_user[];  /* User login name. */
	char            ut_line[];  /* Device name. */
	char            ut_host[];  /* Remote hostname. */
};
.Ed
.Pp
The
.Fa ut_type
field indicates the type of the log entry, which can have one of the
following values:
.Bl -tag -width LOGIN_PROCESS
.It Dv EMPTY
No valid user accounting information.
.It Dv BOOT_TIME
Identifies time of system boot.
.It Dv SHUTDOWN_TIME
Identifies time of system shutdown.
.It Dv OLD_TIME
Identifies time when system clock changed.
.It Dv NEW_TIME
Identifies time after system clock changed.
.It Dv USER_PROCESS
Identifies a process.
.It Dv INIT_PROCESS
Identifies a process spawned by the init process.
.It Dv LOGIN_PROCESS
Identifies the session leader of a logged-in user.
.It Dv DEAD_PROCESS
Identifies a session leader who has exited.
.El
.Pp
Entries of type
.Dv INIT_PROCESS
and
.Dv LOGIN_PROCESS
are not processed by this implementation.
.Pp
Other fields inside the structure are:
.Bl -tag -width ut_user
.It Fa ut_tv
The time the event occurred.
This field is used for all types of entries, except
.Dv EMPTY .
.It Fa ut_id
An identifier that is used to refer to the entry.
This identifier can be used to remove or replace a login entry by
writing a new entry to the database containing the same value for
.Fa ut_id .
This field is only applicable to entries of type
.Dv USER_PROCESS ,
.Dv INIT_PROCESS ,
.Dv LOGIN_PROCESS
and
.Dv DEAD_PROCESS .
.It Fa ut_pid
The process identifier of the session leader of the login session.
This field is only applicable to entries of type
.Dv USER_PROCESS ,
.Dv INIT_PROCESS ,
.Dv LOGIN_PROCESS
and
.Dv DEAD_PROCESS .
.It Fa ut_user
The user login name corresponding with the login session.
This field is only applicable to entries of type
.Dv USER_PROCESS
and
.Dv INIT_PROCESS .
For
.Dv INIT_PROCESS
entries this entry typically contains the name of the login process.
.It Fa ut_line
The name of the TTY character device, without the leading
.Pa /dev/
prefix, corresponding with the device used to facilitate the user login
session.
If no TTY character device is used, this field is left blank.
This field is only applicable to entries of type
.Dv USER_PROCESS 
and
.Dv LOGIN_PROCESS .
.It Fa ut_host
The network hostname of the remote system, connecting to perform a user
login.
If the user login session is not performed across a network, this field
is left blank.
This field is only applicable to entries of type
.Dv USER_PROCESS .
.El
.Pp
This implementation guarantees all inapplicable fields are discarded.
The
.Fa ut_user ,
.Fa ut_line
and
.Fa ut_host
fields of the structure returned by the library functions are also
guaranteed to be null-terminated in this implementation.
.Pp
The
.Fn getutxent
function can be used to read the next entry from the user accounting
database.
.Pp
The
.Fn getutxid
function searches for the next entry in the database of which the
behaviour is based on the
.Fa ut_type
field of
.Fa id .
If
.Fa ut_type
has a value of
.Dv BOOT_TIME ,
.Dv SHUTDOWN_TIME ,
.Dv OLD_TIME
or
.Dv NEW_TIME ,
it will return the next entry whose
.Fa ut_type
has an equal value.
If
.Fa ut_type
has a value of
.Dv USER_PROCESS ,
.Dv INIT_PROCESS ,
.Dv LOGIN_PROCESS
or
.Dv DEAD_PROCESS ,
it will return the next entry whose
.Fa ut_type
has one of the previously mentioned values and whose
.Fa ut_id
is equal.
.Pp
The
.Fn getutxline
function searches for the next entry in the database whose
.Fa ut_type
has a value of
.Dv USER_PROCESS
or
.Dv LOGIN_PROCESS
and whose
.Fa ut_line
is equal to the the same field in
.Fa line .
.Pp
The
.Fn getutxuser
function searches for the next entry in the database whose
.Fa ut_type
has a value of
.Dv USER_PROCESS
and whose
.Fa ut_user
is equal to
.Fa user .
.Pp
The previously mentioned functions will automatically try to open the
user accounting database if not already done so.
The
.Fn setutxdb
and
.Fn setutxent
functions allow the database to be opened manually, causing the offset
within the user accounting database to be rewound.
The
.Fn endutxent
function closes the database.
.Pp
The
.Fn setutxent
database always opens the active sessions database.
The
.Fn setutxdb
function opens the database identified by
.Fa type ,
whose value is either
.Dv UTXDB_ACTIVE ,
.Dv UTXDB_LASTLOGIN
or
.Dv UTXDB_LOG .
It will open a custom file with filename
.Fa file
instead of the system-default if
.Fa file
is not null.
Care must be taken that when using a custom filename,
.Fa type
still has to match with the actual format, since each database may use
its own file format.
.Pp
The
.Fn pututxline
function writes record
.Fa utmpx
to the system-default user accounting databases.
The value of
.Fa ut_type
determines which databases are modified.
.Pp
Entries of type
.Dv BOOT_TIME ,
.Dv SHUTDOWN_TIME ,
.Dv OLD_TIME
and
.Dv NEW_TIME
will only be written to
.Pa /var/log/utx.log .
.Pp
Entries of type
.Dv USER_PROCESS
will also be written to
.Pa /var/run/utx.active
and
.Pa /var/log/utx.lastlogin .
.Pp
Entries of type
.Dv DEAD_PROCESS
will only be written to
.Pa /var/log/utx.log
and
.Pa /var/run/utx.active
if a corresponding
.Dv USER_PROCESS ,
.Dv INIT_PROCESS
or
.Dv LOGIN_PROCESS
entry whose
.Fa ut_id
is equal has been found in the latter.
.Pp
In addition, entries of type
.Dv BOOT_TIME
and
.Dv SHUTDOWN_TIME
will cause all entries in
.Pa /var/run/utx.active
to be discarded.
.Pp
All entries whose type has not been mentioned previously, are discarded
by this implementation of
.Fn pututxline .
This implementation also ignores the value of
.Fa ut_tv .
.Sh RETURN VALUES
The
.Fn getutxent ,
.Fn getutxid ,
.Fn getutxline ,
and
.Fn getutxuser
functions return a pointer to an
.Vt utmpx
structure that matches the mentioned constraints on success or
.Dv NULL
when reaching the end-of-file or when an error occurs.
.Pp
The
.Fn pututxline
function returns a pointer to an
.Vt utmpx
structure containing a copy of the structure written to disk upon
success.
It returns
.Dv NULL
when the provided
.Vt utmpx
is invalid, or
.Fa ut_type
has a value of
.Dv DEAD_PROCESS
and an entry with an identifier with a value equal to the field
.Fa ut_id
was not found; the global variable
.Va errno
is set to indicate the error.
.Pp
The
.Fn setutxdb
function returns 0 if the user accounting database was opened
successfully.
Otherwise, -1 is returned and the global variable
.Va errno
is set to indicate the error.
.Sh ERRORS
In addition to the error conditions described in
.Xr fdopen 3 ,
.Xr fopen 3 ,
.Xr fseek 3 ,
.Xr open 3 ,
the
.Fn pututxline
function can generate the following errors:
.Bl -tag -width Er
.It Bq Er ESRCH
The value of
.Fa ut_type
is DEAD_PROCESS, and the process entry could not be found.
.It Bq Er EINVAL
The value of
.Fa ut_type
is not supported by this implementation.
.El
In addition to the error conditions described in
.Xr fopen 3 ,
the
.Fn setutxdb
function can generate the following errors:
.Bl -tag -width Er
.It Bq Er EINVAL
The
.Fa type
argument contains a value not supported by this implementation.
.It Bq Er EFTYPE
The file format is invalid.
.El
.Sh SEE ALSO
.Xr last 1 ,
.Xr write 1 ,
.Xr wtmpcvt 1 ,
.Xr getpid 2 ,
.Xr gettimeofday 2 ,
.Xr tty 4 ,
.Xr ac 8 ,
.Xr newsyslog 8 ,
.Xr utxrm 8
.Sh STANDARDS
The
.Fn endutxent ,
.Fn getutxent ,
.Fn getutxid ,
.Fn getutxline
and
.Fn setutxent
functions are expected to conform to
.St -p1003.1-2008 .
.Pp
The
.Fn pututxline
function deviates from the standard by writing its records to multiple
database files, depending on its
.Fa ut_type .
This prevents the need for special utility functions to update the other
databases, such as the
.Fn updlastlogx
and
.Fn updwtmpx
functions which are available in other implementations.
It also tries to replace
.Dv DEAD_PROCESS
entries in the active sessions database when storing
.Dv USER_PROCESS
entries and no entry with the same value for
.Fa ut_id
has been found.
The standard always requires a new entry to be allocated, which could
cause an unbounded growth of the database.
.Pp
The
.Fn getutxuser
and
.Fn setutxdb
functions,
the
.Fa ut_host
field of the
.Vt utmpx
structure and
.Dv SHUTDOWN_TIME
are extensions.
.Sh HISTORY
These functions appeared in
.Fx 9.0 .
They replaced the 
.In utmp.h
interface.
.Sh AUTHORS
.An Ed Schouten Aq ed@FreeBSD.org
OpenPOWER on IntegriCloud