Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/frontend/mosh-server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,28 @@ static bool device_exists( const char* ut_line )
struct stat buf;
return 0 == lstat( device_name.c_str(), &buf );
}

static std::string format_duration( time_t duration )
{
if ( duration < 0 )
return "?";
int days = duration / 86400;
duration %= 86400;
int hours = duration / 3600;
duration %= 3600;
int minutes = duration / 60;
int seconds = duration % 60;

char buf[64];
if ( days > 0 ) {
snprintf( buf, 64, "%dd %dh %dm", days, hours, minutes );
} else if ( hours > 0 ) {
snprintf( buf, 64, "%dh %dm", hours, minutes );
} else {
snprintf( buf, 64, "%dm %ds", minutes, seconds );
}
return std::string( buf );
}
#endif

static void warn_unattached( const std::string& ignore_entry )
Expand All @@ -1042,13 +1064,18 @@ static void warn_unattached( const std::string& ignore_entry )

/* look for unattached sessions */
std::vector<std::string> unattached_mosh_servers;
time_t now = time( NULL );

while ( struct utmpx* entry = getutxent() ) {
if ( ( entry->ut_type == USER_PROCESS ) && ( username == std::string( entry->ut_user ) ) ) {
/* does line show unattached mosh session */
std::string text( entry->ut_host );
if ( ( text.size() >= 5 ) && ( text.substr( 0, 5 ) == "mosh " ) && ( text[text.size() - 1] == ']' )
&& ( text != ignore_entry ) && device_exists( entry->ut_line ) ) {
long int age = now - entry->ut_tv.tv_sec;
if ( age < 0 )
age = 0;
text += " (age " + format_duration( age ) + ")";
unattached_mosh_servers.push_back( text );
}
}
Expand Down
Loading