Skip to content
This repository
Browse code

Read utmpx directly instead of using "who", and prettify detached war…

…ning
  • Loading branch information...
commit 0311365810f11b1db8bc6a01bfc5ab95ee241ba8 1 parent 9ac3b65
Keith Winstein authored

Showing 2 changed files with 32 additions and 34 deletions. Show diff stats Hide diff stats

  1. +1 0  configure.ac
  2. +31 34 src/frontend/mosh-server.cc
1  configure.ac
@@ -180,6 +180,7 @@ AC_CHECK_HEADERS([arpa/inet.h fcntl.h langinfo.h limits.h locale.h netinet/in.h
180 180
181 181 AC_CHECK_HEADERS([pty.h util.h libutil.h paths.h])
182 182 AC_CHECK_HEADERS([endian.h sys/endian.h])
  183 +AC_CHECK_HEADERS([utmpx.h])
183 184
184 185 # Checks for typedefs, structures, and compiler characteristics.
185 186 AC_HEADER_STDBOOL
65 src/frontend/mosh-server.cc
@@ -55,6 +55,10 @@
55 55 #include <time.h>
56 56 #include <sys/stat.h>
57 57
  58 +#ifdef HAVE_UTMPX_H
  59 +#include <utmpx.h>
  60 +#endif
  61 +
58 62 #ifdef HAVE_PATHS_H
59 63 #include <paths.h>
60 64 #endif
@@ -104,7 +108,7 @@ void print_usage( const char *argv0 )
104 108 void print_motd( void );
105 109 void chdir_homedir( void );
106 110 bool motd_hushed( void );
107   -void warn_unattached( const char *ignore_entry );
  111 +void warn_unattached( const string & ignore_entry );
108 112
109 113 /* Simple spinloop */
110 114 void spin( void )
@@ -779,8 +783,9 @@ string mosh_read_line( FILE *file )
779 783 return ret;
780 784 }
781 785
782   -void warn_unattached( const char *ignore_entry )
  786 +void warn_unattached( const string & ignore_entry )
783 787 {
  788 +#ifdef HAVE_UTMPX_H
784 789 /* get username */
785 790 const struct passwd *pw = getpwuid( geteuid() );
786 791 if ( pw == NULL ) {
@@ -792,45 +797,37 @@ void warn_unattached( const char *ignore_entry )
792 797 const string username( pw->pw_name );
793 798
794 799 /* look for unattached sessions */
795   - FILE *who_cmd = popen( "who", "r" );
796   -
797   - if ( who_cmd == NULL ) {
798   - return;
799   - }
800   -
801   - vector< string > unattached_who_lines;
802   -
803   - while ( !feof( who_cmd ) ) {
804   - /* read the line */
805   - const string line = mosh_read_line( who_cmd );
806   -
807   - /* does line start with username? */
808   - if ( line.substr( 0, username.size() + 1 ) == username + " " ) {
809   - /* does line show unattached mosh session? */
810   - if ( line.npos != line.find( "(mosh" ) ) {
811   - /* is line showing _this_ mosh session? */
812   - const string our_entry = string( "(" ) + ignore_entry + string( ")" );
813   - if ( line.npos == line.find( our_entry ) ) {
814   - unattached_who_lines.push_back( line );
815   - }
  800 + vector< string > unattached_mosh_servers;
  801 +
  802 + while ( struct utmpx *entry = getutxent() ) {
  803 + if ( (entry->ut_type == USER_PROCESS)
  804 + && (username == string( entry->ut_user )) ) {
  805 + /* does line show unattached mosh session */
  806 + string text( entry->ut_host );
  807 + if ( (text.substr( 0, 5 ) == "mosh ")
  808 + && (text != ignore_entry) ) {
  809 + unattached_mosh_servers.push_back( text );
816 810 }
817 811 }
818 812 }
819 813
820 814 /* print out warning if necessary */
821   - if ( unattached_who_lines.empty() ) {
  815 + if ( unattached_mosh_servers.empty() ) {
822 816 return;
823   - } else if ( unattached_who_lines.size() == 1 ) {
824   - printf( "\nNote: This Mosh server is detached.\n" );
  817 + } else if ( unattached_mosh_servers.size() == 1 ) {
  818 + printf( "\033[37;44mMosh: You have a detached Mosh session on this server (%s).\033[m\n\n",
  819 + unattached_mosh_servers.front().c_str() );
825 820 } else {
826   - printf( "\nNote: These Mosh servers are detached.\n" );
827   - }
  821 + string pid_string;
828 822
829   - for ( vector< string >::const_iterator it = unattached_who_lines.begin();
830   - it != unattached_who_lines.end();
831   - it++ ) {
832   - printf( "| %s\n", it->c_str() );
833   - }
  823 + for ( vector< string >::const_iterator it = unattached_mosh_servers.begin();
  824 + it != unattached_mosh_servers.end();
  825 + it++ ) {
  826 + pid_string += " - " + *it + "\n";
  827 + }
834 828
835   - printf( "\n" );
  829 + printf( "\033[37;44mMosh: You have %d detached Mosh sessions on this server, with PIDs:\n%s\033[m\n",
  830 + (int)unattached_mosh_servers.size(), pid_string.c_str() );
  831 + }
  832 +#endif /* HAVE_UTMPX_H */
836 833 }

0 comments on commit 0311365

Please sign in to comment.
Something went wrong with that request. Please try again.