*: Switch to POSIX utmpx API

UTMP is SVID legacy, UTMPX is mandated by POSIX.

Glibc and uClibc have identical layout of UTMP and UTMPX, both of these
libc treat _PATH_UTMPX as _PATH_UTMP so from a user-perspective nothing
changes except the names of the API entrypoints.

Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
This commit is contained in:
Bernhard Reutner-Fischer 2015-04-02 23:03:46 +02:00
parent 1186894f77
commit 86a7f18f21
9 changed files with 56 additions and 52 deletions

View File

@ -73,7 +73,7 @@ static void idle_string(char *str6, time_t t)
int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int who_main(int argc UNUSED_PARAM, char **argv) int who_main(int argc UNUSED_PARAM, char **argv)
{ {
struct utmp *ut; struct utmpx *ut;
unsigned opt; unsigned opt;
int do_users = (ENABLE_USERS && (!ENABLE_WHO || applet_name[0] == 'u')); int do_users = (ENABLE_USERS && (!ENABLE_WHO || applet_name[0] == 'u'));
const char *fmt = "%s"; const char *fmt = "%s";
@ -83,8 +83,8 @@ int who_main(int argc UNUSED_PARAM, char **argv)
if (opt & 2) // -H if (opt & 2) // -H
printf("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST\n"); printf("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST\n");
setutent(); setutxent();
while ((ut = getutent()) != NULL) { while ((ut = getutxent()) != NULL) {
if (ut->ut_user[0] if (ut->ut_user[0]
&& ((opt & 1) || ut->ut_type == USER_PROCESS) && ((opt & 1) || ut->ut_type == USER_PROCESS)
) { ) {
@ -126,6 +126,6 @@ int who_main(int argc UNUSED_PARAM, char **argv)
if (do_users) if (do_users)
bb_putchar('\n'); bb_putchar('\n');
if (ENABLE_FEATURE_CLEAN_UP) if (ENABLE_FEATURE_CLEAN_UP)
endutent(); endutxent();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -84,7 +84,7 @@
# include <selinux/av_permissions.h> # include <selinux/av_permissions.h>
#endif #endif
#if ENABLE_FEATURE_UTMP #if ENABLE_FEATURE_UTMP
# include <utmp.h> # include <utmpx.h>
#endif #endif
#if ENABLE_LOCALE_SUPPORT #if ENABLE_LOCALE_SUPPORT
# include <locale.h> # include <locale.h>

View File

@ -74,7 +74,7 @@
static void write_wtmp(void) static void write_wtmp(void)
{ {
struct utmp utmp; struct utmpx utmp;
struct utsname uts; struct utsname uts;
/* "man utmp" says wtmp file should *not* be created automagically */ /* "man utmp" says wtmp file should *not* be created automagically */
/*if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) { /*if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
@ -88,7 +88,7 @@ static void write_wtmp(void)
utmp.ut_line[0] = '~'; utmp.ut_line[1] = '~'; /* = strcpy(utmp.ut_line, "~~"); */ utmp.ut_line[0] = '~'; utmp.ut_line[1] = '~'; /* = strcpy(utmp.ut_line, "~~"); */
uname(&uts); uname(&uts);
safe_strncpy(utmp.ut_host, uts.release, sizeof(utmp.ut_host)); safe_strncpy(utmp.ut_host, uts.release, sizeof(utmp.ut_host));
updwtmp(bb_path_wtmp_file, &utmp); updwtmpx(bb_path_wtmp_file, &utmp);
} }
#else #else
#define write_wtmp() ((void)0) #define write_wtmp() ((void)0)

View File

@ -16,7 +16,7 @@ static void touch(const char *filename)
void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname) void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname)
{ {
struct utmp utent; struct utmpx utent;
char *id; char *id;
unsigned width; unsigned width;
@ -45,17 +45,17 @@ void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, con
tty_name += 3; tty_name += 3;
strncpy(id, tty_name, width); strncpy(id, tty_name, width);
touch(_PATH_UTMP); touch(_PATH_UTMPX);
//utmpname(_PATH_UTMP); //utmpxname(_PATH_UTMPX);
setutent(); setutxent();
/* Append new one (hopefully, unless we collide on ut_id) */ /* Append new one (hopefully, unless we collide on ut_id) */
pututline(&utent); pututxline(&utent);
endutent(); endutxent();
#if ENABLE_FEATURE_WTMP #if ENABLE_FEATURE_WTMP
/* "man utmp" says wtmp file should *not* be created automagically */ /* "man utmp" says wtmp file should *not* be created automagically */
/*touch(bb_path_wtmp_file);*/ /*touch(bb_path_wtmp_file);*/
updwtmp(bb_path_wtmp_file, &utent); updwtmpx(bb_path_wtmp_file, &utent);
#endif #endif
} }
@ -64,17 +64,17 @@ void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, con
*/ */
void FAST_FUNC update_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname) void FAST_FUNC update_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname)
{ {
struct utmp utent; struct utmpx utent;
struct utmp *utp; struct utmpx *utp;
touch(_PATH_UTMP); touch(_PATH_UTMPX);
//utmpname(_PATH_UTMP); //utmpxname(_PATH_UTMPX);
setutent(); setutxent();
/* Did init/getty/telnetd/sshd/... create an entry for us? /* Did init/getty/telnetd/sshd/... create an entry for us?
* It should be (new_type-1), but we'd also reuse * It should be (new_type-1), but we'd also reuse
* any other potentially stale xxx_PROCESS entry */ * any other potentially stale xxx_PROCESS entry */
while ((utp = getutent()) != NULL) { while ((utp = getutxent()) != NULL) {
if (utp->ut_pid == pid if (utp->ut_pid == pid
// && ut->ut_line[0] // && ut->ut_line[0]
&& utp->ut_id[0] /* must have nonzero id */ && utp->ut_id[0] /* must have nonzero id */
@ -88,25 +88,25 @@ void FAST_FUNC update_utmp(pid_t pid, int new_type, const char *tty_name, const
/* Stale record. Nuke hostname */ /* Stale record. Nuke hostname */
memset(utp->ut_host, 0, sizeof(utp->ut_host)); memset(utp->ut_host, 0, sizeof(utp->ut_host));
} }
/* NB: pututline (see later) searches for matching utent /* NB: pututxline (see later) searches for matching utxent
* using getutid(utent) - we must not change ut_id * using getutxid(utent) - we must not change ut_id
* if we want *exactly this* record to be overwritten! * if we want *exactly this* record to be overwritten!
*/ */
break; break;
} }
} }
//endutent(); - no need, pututline can deal with (and actually likes) //endutxent(); - no need, pututxline can deal with (and actually likes)
//the situation when utmp file is positioned on found record //the situation when utmp file is positioned on found record
if (!utp) { if (!utp) {
if (new_type != DEAD_PROCESS) if (new_type != DEAD_PROCESS)
write_new_utmp(pid, new_type, tty_name, username, hostname); write_new_utmp(pid, new_type, tty_name, username, hostname);
else else
endutent(); endutxent();
return; return;
} }
/* Make a copy. We can't use *utp, pututline's internal getutid /* Make a copy. We can't use *utp, pututxline's internal getutxid
* will overwrite it before it is used! */ * will overwrite it before it is used! */
utent = *utp; utent = *utp;
@ -120,14 +120,14 @@ void FAST_FUNC update_utmp(pid_t pid, int new_type, const char *tty_name, const
utent.ut_tv.tv_sec = time(NULL); utent.ut_tv.tv_sec = time(NULL);
/* Update, or append new one */ /* Update, or append new one */
//setutent(); //setutxent();
pututline(&utent); pututxline(&utent);
endutent(); endutxent();
#if ENABLE_FEATURE_WTMP #if ENABLE_FEATURE_WTMP
/* "man utmp" says wtmp file should *not* be created automagically */ /* "man utmp" says wtmp file should *not* be created automagically */
/*touch(bb_path_wtmp_file);*/ /*touch(bb_path_wtmp_file);*/
updwtmp(bb_path_wtmp_file, &utent); updwtmpx(bb_path_wtmp_file, &utent);
#endif #endif
} }

View File

@ -32,21 +32,21 @@
#if defined UT_LINESIZE \ #if defined UT_LINESIZE \
&& ((UT_LINESIZE != 32) || (UT_NAMESIZE != 32) || (UT_HOSTSIZE != 256)) && ((UT_LINESIZE != 32) || (UT_NAMESIZE != 32) || (UT_HOSTSIZE != 256))
#error struct utmp member char[] size(s) have changed! #error struct utmpx member char[] size(s) have changed!
#elif defined __UT_LINESIZE \ #elif defined __UT_LINESIZE \
&& ((__UT_LINESIZE != 32) || (__UT_NAMESIZE != 64) || (__UT_HOSTSIZE != 256)) && ((__UT_LINESIZE != 32) || (__UT_NAMESIZE != 64) || (__UT_HOSTSIZE != 256))
#error struct utmp member char[] size(s) have changed! #error struct utmpx member char[] size(s) have changed!
#endif #endif
#if EMPTY != 0 || RUN_LVL != 1 || BOOT_TIME != 2 || NEW_TIME != 3 || \ #if EMPTY != 0 || RUN_LVL != 1 || BOOT_TIME != 2 || NEW_TIME != 3 || \
OLD_TIME != 4 OLD_TIME != 4
#error Values for the ut_type field of struct utmp changed #error Values for the ut_type field of struct utmpx changed
#endif #endif
int last_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int last_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int last_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) int last_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
{ {
struct utmp ut; struct utmpx ut;
int n, file = STDIN_FILENO; int n, file = STDIN_FILENO;
time_t t_tmp; time_t t_tmp;
off_t pos; off_t pos;

View File

@ -22,6 +22,10 @@
#define HEADER_LINE_WIDE "USER", "TTY", \ #define HEADER_LINE_WIDE "USER", "TTY", \
INET6_ADDRSTRLEN, INET6_ADDRSTRLEN, "HOST", "LOGIN", " TIME", "" INET6_ADDRSTRLEN, INET6_ADDRSTRLEN, "HOST", "LOGIN", " TIME", ""
#if !defined __UT_LINESIZE && defined UT_LINESIZE
# define __UT_LINESIZE UT_LINESIZE
#endif
enum { enum {
NORMAL, NORMAL,
LOGGED, LOGGED,
@ -39,7 +43,7 @@ enum {
#define show_wide (option_mask32 & LAST_OPT_W) #define show_wide (option_mask32 & LAST_OPT_W)
static void show_entry(struct utmp *ut, int state, time_t dur_secs) static void show_entry(struct utmpx *ut, int state, time_t dur_secs)
{ {
unsigned days, hours, mins; unsigned days, hours, mins;
char duration[sizeof("(%u+02:02)") + sizeof(int)*3]; char duration[sizeof("(%u+02:02)") + sizeof(int)*3];
@ -104,7 +108,7 @@ static void show_entry(struct utmp *ut, int state, time_t dur_secs)
duration_str); duration_str);
} }
static int get_ut_type(struct utmp *ut) static int get_ut_type(struct utmpx *ut)
{ {
if (ut->ut_line[0] == '~') { if (ut->ut_line[0] == '~') {
if (strcmp(ut->ut_user, "shutdown") == 0) { if (strcmp(ut->ut_user, "shutdown") == 0) {
@ -142,7 +146,7 @@ static int get_ut_type(struct utmp *ut)
return ut->ut_type; return ut->ut_type;
} }
static int is_runlevel_shutdown(struct utmp *ut) static int is_runlevel_shutdown(struct utmpx *ut)
{ {
if (((ut->ut_pid & 255) == '0') || ((ut->ut_pid & 255) == '6')) { if (((ut->ut_pid & 255) == '0') || ((ut->ut_pid & 255) == '6')) {
return 1; return 1;
@ -154,7 +158,7 @@ static int is_runlevel_shutdown(struct utmp *ut)
int last_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int last_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int last_main(int argc UNUSED_PARAM, char **argv) int last_main(int argc UNUSED_PARAM, char **argv)
{ {
struct utmp ut; struct utmpx ut;
const char *filename = _PATH_WTMP; const char *filename = _PATH_WTMP;
llist_t *zlist; llist_t *zlist;
off_t pos; off_t pos;
@ -242,9 +246,9 @@ int last_main(int argc UNUSED_PARAM, char **argv)
{ {
llist_t *el, *next; llist_t *el, *next;
for (el = zlist; el; el = next) { for (el = zlist; el; el = next) {
struct utmp *up = (struct utmp *)el->data; struct utmpx *up = (struct utmpx *)el->data;
next = el->link; next = el->link;
if (strncmp(up->ut_line, ut.ut_line, UT_LINESIZE) == 0) { if (strncmp(up->ut_line, ut.ut_line, __UT_LINESIZE) == 0) {
if (show) { if (show) {
show_entry(&ut, NORMAL, up->ut_tv.tv_sec); show_entry(&ut, NORMAL, up->ut_tv.tv_sec);
show = 0; show = 0;

View File

@ -29,19 +29,19 @@
int runlevel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int runlevel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int runlevel_main(int argc UNUSED_PARAM, char **argv) int runlevel_main(int argc UNUSED_PARAM, char **argv)
{ {
struct utmp *ut; struct utmpx *ut;
char prev; char prev;
if (argv[1]) utmpname(argv[1]); if (argv[1]) utmpxname(argv[1]);
setutent(); setutxent();
while ((ut = getutent()) != NULL) { while ((ut = getutxent()) != NULL) {
if (ut->ut_type == RUN_LVL) { if (ut->ut_type == RUN_LVL) {
prev = ut->ut_pid / 256; prev = ut->ut_pid / 256;
if (prev == 0) prev = 'N'; if (prev == 0) prev = 'N';
printf("%c %c\n", prev, ut->ut_pid % 256); printf("%c %c\n", prev, ut->ut_pid % 256);
if (ENABLE_FEATURE_CLEAN_UP) if (ENABLE_FEATURE_CLEAN_UP)
endutent(); endutxent();
return 0; return 0;
} }
} }
@ -49,6 +49,6 @@ int runlevel_main(int argc UNUSED_PARAM, char **argv)
puts("unknown"); puts("unknown");
if (ENABLE_FEATURE_CLEAN_UP) if (ENABLE_FEATURE_CLEAN_UP)
endutent(); endutxent();
return 1; return 1;
} }

View File

@ -32,7 +32,7 @@
int wall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int wall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int wall_main(int argc UNUSED_PARAM, char **argv) int wall_main(int argc UNUSED_PARAM, char **argv)
{ {
struct utmp *ut; struct utmpx *ut;
char *msg; char *msg;
int fd; int fd;
@ -46,8 +46,8 @@ int wall_main(int argc UNUSED_PARAM, char **argv)
msg = xmalloc_read(fd, NULL); msg = xmalloc_read(fd, NULL);
if (ENABLE_FEATURE_CLEAN_UP && argv[1]) if (ENABLE_FEATURE_CLEAN_UP && argv[1])
close(fd); close(fd);
setutent(); setutxent();
while ((ut = getutent()) != NULL) { while ((ut = getutxent()) != NULL) {
char *line; char *line;
if (ut->ut_type != USER_PROCESS) if (ut->ut_type != USER_PROCESS)
continue; continue;
@ -56,7 +56,7 @@ int wall_main(int argc UNUSED_PARAM, char **argv)
free(line); free(line);
} }
if (ENABLE_FEATURE_CLEAN_UP) { if (ENABLE_FEATURE_CLEAN_UP) {
endutent(); endutxent();
free(msg); free(msg);
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -81,10 +81,10 @@ int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
#if ENABLE_FEATURE_UPTIME_UTMP_SUPPORT #if ENABLE_FEATURE_UPTIME_UTMP_SUPPORT
{ {
struct utmp *ut; struct utmpx *ut;
unsigned users = 0; unsigned users = 0;
while ((ut = getutent()) != NULL) { while ((ut = getutxent()) != NULL) {
if ((ut->ut_type == USER_PROCESS) && (ut->ut_name[0] != '\0')) if ((ut->ut_type == USER_PROCESS) && (ut->ut_user[0] != '\0'))
users++; users++;
} }
printf(", %u users", users); printf(", %u users", users);