mirror of
https://git.busybox.net/uClibc
synced 2025-10-14 01:32:00 +08:00

Note: _PATH_UTMPX == _PATH_UTMP and the utmp struct is identical to the utmpx struct so this only changes the external API entrypoints and NOT the underlying data source. This saves about 500b (~1300b from previously ~1950) while at it. Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
61 lines
1.9 KiB
C
61 lines
1.9 KiB
C
#include <errno.h>
|
|
#include <limits.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include "internal/utmp.h"
|
|
|
|
/* Write the given entry into utmp and wtmp.
|
|
* Note: the match in utmp is done against ut_id field,
|
|
* which is NOT set by this function - caller must set it.
|
|
*/
|
|
void login(const struct utmp *entry)
|
|
{
|
|
struct UT copy;
|
|
char tty_name[sizeof(copy.ut_line) + 6];
|
|
int fd;
|
|
|
|
// Manpage:
|
|
// login() takes the argument ut struct, fills the field ut->ut_type
|
|
// (if there is such a field) with the value USER_PROCESS,
|
|
// and fills the field ut->ut_pid (if there is such a field)
|
|
// with the process ID of the calling process.
|
|
copy = *((const struct UT *)(entry));
|
|
#if _HAVE_UT_TYPE - 0
|
|
copy.ut_type = USER_PROCESS;
|
|
#endif
|
|
#if _HAVE_UT_PID - 0
|
|
copy.ut_pid = getpid();
|
|
#endif
|
|
|
|
// Then it tries to fill the field ut->ut_line. It takes the first of stdin,
|
|
// stdout, stderr that is a tty, and stores the corresponding pathname minus
|
|
// a possible leading /dev/ into this field, and then writes the struct
|
|
// to the utmp file. On the other hand, if no tty name was found,
|
|
// this field is filled with "???" and the struct is not written
|
|
// to the utmp file.
|
|
fd = 0;
|
|
while (fd != 3 && ttyname_r(fd, tty_name, sizeof(tty_name)) != 0)
|
|
fd++;
|
|
if (fd != 3) {
|
|
if (strncmp(tty_name, "/dev/", 5) == 0)
|
|
strncpy(copy.ut_line, tty_name + 5, sizeof(copy.ut_line)-1);
|
|
else
|
|
strncpy(copy.ut_line, tty_name, sizeof(copy.ut_line)-1);
|
|
copy.ut_line[sizeof(copy.ut_line)-1] = '\0';
|
|
|
|
/* utmpname(_PATH_UTMP); - why?
|
|
* this makes it impossible for caller to use other file!
|
|
* Does any standard or historical precedent says this must be done? */
|
|
setutent();
|
|
/* Replaces record with matching ut_id, or appends new one: */
|
|
pututline(©);
|
|
endutent();
|
|
} else {
|
|
strncpy(copy.ut_line, "???", sizeof(copy.ut_line));
|
|
}
|
|
|
|
// After this, the struct is written to the wtmp file.
|
|
updwtmp(_PATH_WTMP, ©);
|
|
}
|