Skip to content

Commit

Permalink
Allow some utmpx fields to be optional
Browse files Browse the repository at this point in the history
POSIX.1 doesn't define ut_host in struct utmpx. Also, Linux has support
for an exit status value in ut_exit. This commit adds conditional code
for both ut_host and ut_exit to maximise portability.
  • Loading branch information
matt335672 committed Sep 20, 2023
1 parent ac0bcd2 commit 553b6f3
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 12 deletions.
9 changes: 9 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,10 @@ AC_CHECK_HEADER([X11/extensions/Xrandr.h], [],
if test "x$enable_utmp" = "xyes"
then
AC_CHECK_HEADERS(utmp.h utmpx.h)

# Test for non-standard extensions in struct utmpx
AXRDP_CHECK_UTMPX_MEMBER_EXISTS([ut_host], [HAVE_UTMPX_UT_HOST])
AXRDP_CHECK_UTMPX_MEMBER_EXISTS([ut_exit], [HAVE_UTMPX_UT_EXIT])
fi

CFLAGS="$save_CFLAGS"
Expand Down Expand Up @@ -640,6 +644,11 @@ echo " vsock $enable_vsock"
echo " auth mechanism $auth_mech"
echo " rdpsndaudin $enable_rdpsndaudin"
echo " utmp support $enable_utmp"
if test x$enable_utmp = xyes; then
echo " utmpx.ut_host $ac_cv_utmpx_has_ut_host"
echo " utmpx.ut_exit $ac_cv_utmpx_has_ut_exit"
fi

echo
echo " with imlib2 $use_imlib2"
echo " with freetype2 $use_freetype2"
Expand Down
43 changes: 43 additions & 0 deletions m4/axrdp.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# SYNOPSIS
#
# AXRDP_CHECK_UTMPX_MEMBER_EXISTS(MEMBER, COMPILE-DEFINE)
#
# EXAMPLE
#
# CHECK_UTMPX_MEMBER_EXISTS([ut_exit], [HAVE_UTMPX_UT_EXIT])
#
# DESCRIPTION
#
# If the member MEMBER exists in the utmpx struct, the COMPILE-DEFINE
# is set for the C compiler.
#
# The shell variable 'ac_cv_utmpx_has_$MEMBER' is set to 'yes' or 'no'
# and cached
#
AC_DEFUN([AXRDP_CHECK_UTMPX_MEMBER_EXISTS],
[
AS_VAR_PUSHDEF([x_var], [ac_cv_utmpx_has_$1])
AS_VAR_PUSHDEF([x_define], [$2])
AC_CACHE_CHECK(
[for $1 in struct utmpx],
[x_var],
[AC_COMPILE_IFELSE(
[AC_LANG_SOURCE([[
# include <utmpx.h>
# include <stddef.h>
int main()
{
return offsetof(struct utmpx,$1);
}]])],
[AS_VAR_SET([x_var], [yes])],
[AS_VAR_SET([x_var], [no])])]
)
AS_VAR_IF(
[x_var],
[yes],
[AC_DEFINE([x_define], [1], [Define if '$1' is in struct utmpx.])])
AS_VAR_POPDEF([x_var])
AS_VAR_POPDEF([x_define])
])


2 changes: 1 addition & 1 deletion sesman/sesexec/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ session_process_child_exit(struct session_data *sd,
sd->win_mgr, sd->params.display, wm_wait_time);
}

utmp_logout(sd->win_mgr, sd->params.display);
utmp_logout(sd->win_mgr, sd->params.display, e);
sd->win_mgr = -1;

if (sd->x_server > 0)
Expand Down
27 changes: 18 additions & 9 deletions sesman/sesexec/sessionrecord.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ enum add_xtmp_mode

#ifdef USE_UTMP

#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>

Expand Down Expand Up @@ -80,7 +76,7 @@ typedef struct utmp _utmp;

static void
add_xtmp_entry(int pid, int display, const struct login_info *login_info,
enum add_xtmp_mode mode)
enum add_xtmp_mode mode, const struct proc_exit_status *e)
{
#if USE_UTMP
char idbuff[16];
Expand All @@ -102,9 +98,22 @@ add_xtmp_entry(int pid, int display, const struct login_info *login_info,
g_strncpy(ut.ut_id, idbuff, sizeof(ut.ut_id));
if (login_info != NULL)
{
g_strncpy(ut.ut_user, login_info->username , sizeof(ut.ut_user));
g_strncpy(ut.ut_user, login_info->username, sizeof(ut.ut_user));
#ifdef HAVE_UTMPX_UT_HOST
g_strncpy(ut.ut_host, login_info->ip_addr, sizeof(ut.ut_host));
#endif
}

#ifdef HAVE_UTMPX_UT_EXIT
if (e != NULL && e->reason == E_PXR_STATUS_CODE)
{
ut.ut_exit.e_exit = e->val;
}
else if (e != NULL && e->reason == E_PXR_SIGNAL)
{
ut.ut_exit.e_termination = e->val;
}
#endif

/* update the utmp file */
/* open utmp */
Expand All @@ -124,15 +133,15 @@ utmp_login(int pid, int display, const struct login_info *login_info)
"adding login info for utmp: %d - %d - %s - %s",
pid, display, login_info->username, login_info->ip_addr);

add_xtmp_entry(pid, display, login_info, MODE_LOGIN);
add_xtmp_entry(pid, display, login_info, MODE_LOGIN, NULL);
}

void
utmp_logout(int pid, int display)
utmp_logout(int pid, int display, const struct proc_exit_status *exit_status)
{

log_message(LOG_LEVEL_DEBUG, "adding logout info for utmp: %d - %d",
pid, display);

add_xtmp_entry(pid, display, NULL, MODE_LOGOUT);
add_xtmp_entry(pid, display, NULL, MODE_LOGOUT, exit_status);
}
8 changes: 6 additions & 2 deletions sesman/sesexec/sessionrecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#define SESSIONRECORD_H

struct login_info;
struct proc_exit_status;

/**
* @brief Record login in utmp
Expand All @@ -35,14 +36,17 @@ struct login_info;
* @param display Display number
* @param login_info Information about logged in user
*/
void utmp_login(int pid, int display, const struct login_info *login_info);
void
utmp_login(int pid, int display, const struct login_info *login_info);

/**
* @brief Record logout in utmp
*
* @param pid PID of window manager
* @param display Display number
* @param exit_status Exit status of process
*/
void utmp_logout(int pid, int display);
void
utmp_logout(int pid, int display, const struct proc_exit_status *exit_status);

#endif

0 comments on commit 553b6f3

Please sign in to comment.