From bb7f5e0f18493996095a19dd8b6d162cc2eda33f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 22 Oct 2019 14:14:02 +0200 Subject: Handle duplicate entries in utmp for X sessions --- src/userlist.c | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/userlist.c b/src/userlist.c index d4dde86..4b03959 100644 --- a/src/userlist.c +++ b/src/userlist.c @@ -78,18 +78,44 @@ int getUserList( struct user *outbuf, int size ) fixString( u->ut_user, UT_NAMESIZE ); fixString( u->ut_line, UT_LINESIZE ); fixString( u->ut_host, UT_HOSTSIZE ); - // Find slot in outbuf (matching one, first free one if not found) int use = -1; - for ( int j = 0; j < deadzone; ++j ) { - if ( outbuf[j].user[0] == '\0' ) { - if ( use == -1 ) { + // Skip pts sessions within x (xterm) + if ( u->ut_host[0] == ':' && isdigit( u->ut_host[1] ) ) { + for ( int j = 0; j < deadzone; ++j ) { + if ( outbuf[j].user[0] != '\0' && strcmp( outbuf[j].display, u->ut_host ) == 0 ) { + if ( outbuf[j].sessionLeader == u->ut_pid + && strcmp( outbuf[j].user, u->ut_user ) == 0 + && strcmp( outbuf[j].device, u->ut_line ) == 0 ) { + use = j; + break; + } + bool oldPts = strncmp( outbuf[j].device, "pts", 3 ) == 0; + bool newTty = strncmp( u->ut_line, "tty", 3 ) == 0; + if ( oldPts && newTty ) { + use = j; // Replace pts entry with tty entry + outbuf[j].user[0] = '\0'; + } else if ( !newTty ) { + use = -2; // Ignore non-tty entry if we have another one for that display + } + break; + } + } + if ( use == -2 ) + continue; // Ignore this entry, already taken care of + } + if ( use == -1 ) { + // OK so far, find slot in outbuf (matching one, first free one if not found) + for ( int j = 0; j < deadzone; ++j ) { + if ( outbuf[j].user[0] == '\0' ) { + if ( use == -1 ) { + use = j; + } + } else if ( outbuf[j].sessionLeader == u->ut_pid + && strcmp( outbuf[j].user, u->ut_user ) == 0 + && strcmp( outbuf[j].device, u->ut_line ) == 0 ) { use = j; + break; } - } else if ( outbuf[j].sessionLeader == u->ut_pid - && strcmp( outbuf[j].user, u->ut_user ) == 0 - && strcmp( outbuf[j].device, u->ut_line ) == 0 ) { - use = j; - break; } } if ( use == -1 ) { // Not found and no free slot, expand if possible -- cgit v1.2.3-55-g7522