Skip to content

Commit

Permalink
Read utmpx directly instead of using "who", and prettify detached war…
Browse files Browse the repository at this point in the history
…ning
  • Loading branch information
keithw committed Sep 29, 2012
1 parent 9ac3b65 commit 0311365
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
1 change: 1 addition & 0 deletions configure.ac
Expand Up @@ -180,6 +180,7 @@ AC_CHECK_HEADERS([arpa/inet.h fcntl.h langinfo.h limits.h locale.h netinet/in.h

AC_CHECK_HEADERS([pty.h util.h libutil.h paths.h])
AC_CHECK_HEADERS([endian.h sys/endian.h])
AC_CHECK_HEADERS([utmpx.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
Expand Down
65 changes: 31 additions & 34 deletions src/frontend/mosh-server.cc
Expand Up @@ -55,6 +55,10 @@
#include <time.h>
#include <sys/stat.h>

#ifdef HAVE_UTMPX_H
#include <utmpx.h>
#endif

#ifdef HAVE_PATHS_H
#include <paths.h>
#endif
Expand Down Expand Up @@ -104,7 +108,7 @@ void print_usage( const char *argv0 )
void print_motd( void );
void chdir_homedir( void );
bool motd_hushed( void );
void warn_unattached( const char *ignore_entry );
void warn_unattached( const string & ignore_entry );

/* Simple spinloop */
void spin( void )
Expand Down Expand Up @@ -779,8 +783,9 @@ string mosh_read_line( FILE *file )
return ret;
}

void warn_unattached( const char *ignore_entry )
void warn_unattached( const string & ignore_entry )
{
#ifdef HAVE_UTMPX_H
/* get username */
const struct passwd *pw = getpwuid( geteuid() );
if ( pw == NULL ) {
Expand All @@ -792,45 +797,37 @@ void warn_unattached( const char *ignore_entry )
const string username( pw->pw_name );

/* look for unattached sessions */
FILE *who_cmd = popen( "who", "r" );

if ( who_cmd == NULL ) {
return;
}

vector< string > unattached_who_lines;

while ( !feof( who_cmd ) ) {
/* read the line */
const string line = mosh_read_line( who_cmd );

/* does line start with username? */
if ( line.substr( 0, username.size() + 1 ) == username + " " ) {
/* does line show unattached mosh session? */
if ( line.npos != line.find( "(mosh" ) ) {
/* is line showing _this_ mosh session? */
const string our_entry = string( "(" ) + ignore_entry + string( ")" );
if ( line.npos == line.find( our_entry ) ) {
unattached_who_lines.push_back( line );
}
vector< string > unattached_mosh_servers;

while ( struct utmpx *entry = getutxent() ) {
if ( (entry->ut_type == USER_PROCESS)
&& (username == string( entry->ut_user )) ) {
/* does line show unattached mosh session */
string text( entry->ut_host );
if ( (text.substr( 0, 5 ) == "mosh ")
&& (text != ignore_entry) ) {
unattached_mosh_servers.push_back( text );
}
}
}

/* print out warning if necessary */
if ( unattached_who_lines.empty() ) {
if ( unattached_mosh_servers.empty() ) {
return;
} else if ( unattached_who_lines.size() == 1 ) {
printf( "\nNote: This Mosh server is detached.\n" );
} else if ( unattached_mosh_servers.size() == 1 ) {
printf( "\033[37;44mMosh: You have a detached Mosh session on this server (%s).\033[m\n\n",
unattached_mosh_servers.front().c_str() );
} else {
printf( "\nNote: These Mosh servers are detached.\n" );
}
string pid_string;

for ( vector< string >::const_iterator it = unattached_who_lines.begin();
it != unattached_who_lines.end();
it++ ) {
printf( "| %s\n", it->c_str() );
}
for ( vector< string >::const_iterator it = unattached_mosh_servers.begin();
it != unattached_mosh_servers.end();
it++ ) {
pid_string += " - " + *it + "\n";
}

printf( "\n" );
printf( "\033[37;44mMosh: You have %d detached Mosh sessions on this server, with PIDs:\n%s\033[m\n",
(int)unattached_mosh_servers.size(), pid_string.c_str() );
}
#endif /* HAVE_UTMPX_H */
}

0 comments on commit 0311365

Please sign in to comment.