6 #include "internal/utmp.h"
8 /* Write the given entry into utmp and wtmp.
9 * Note: the match in utmp is done against ut_id field,
10 * which is NOT set by this function - caller must set it.
12 void login(const struct utmp
*entry
)
15 char tty_name
[sizeof(copy
.ut_line
) + 6];
19 // login() takes the argument ut struct, fills the field ut->ut_type
20 // (if there is such a field) with the value USER_PROCESS,
21 // and fills the field ut->ut_pid (if there is such a field)
22 // with the process ID of the calling process.
23 copy
= *((const struct UT
*)(entry
));
25 copy
.ut_type
= USER_PROCESS
;
28 copy
.ut_pid
= getpid();
31 // Then it tries to fill the field ut->ut_line. It takes the first of stdin,
32 // stdout, stderr that is a tty, and stores the corresponding pathname minus
33 // a possible leading /dev/ into this field, and then writes the struct
34 // to the utmp file. On the other hand, if no tty name was found,
35 // this field is filled with "???" and the struct is not written
38 while (fd
!= 3 && ttyname_r(fd
, tty_name
, sizeof(tty_name
)) != 0)
41 if (strncmp(tty_name
, "/dev/", 5) == 0)
42 strncpy(copy
.ut_line
, tty_name
+ 5, sizeof(copy
.ut_line
)-1);
44 strncpy(copy
.ut_line
, tty_name
, sizeof(copy
.ut_line
)-1);
45 copy
.ut_line
[sizeof(copy
.ut_line
)-1] = '\0';
47 /* utmpname(_PATH_UTMP); - why?
48 * this makes it impossible for caller to use other file!
49 * Does any standard or historical precedent says this must be done? */
51 /* Replaces record with matching ut_id, or appends new one: */
55 strncpy(copy
.ut_line
, "???", sizeof(copy
.ut_line
));
58 // After this, the struct is written to the wtmp file.
59 updwtmp(_PATH_WTMP
, ©
);