Skip to content

Commit 620dd55

Browse files
committed
Show session age in the "detached Mosh sessions" warning
Previously, the message just told you the pid of the detached mosh session: - mosh [3892360] But this doesn't immediately give you any hints about if this is an ancient session that's clearly from a dead process or not. Now it looks like this: - mosh [3892360] (age 58d 11h 5m) Now it's more obvious if this session is part of a stale session or not
1 parent 1105d48 commit 620dd55

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/frontend/mosh-server.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,28 @@ static bool device_exists( const char* ut_line )
10251025
struct stat buf;
10261026
return 0 == lstat( device_name.c_str(), &buf );
10271027
}
1028+
1029+
static std::string format_duration( time_t duration )
1030+
{
1031+
if ( duration < 0 )
1032+
return "?";
1033+
int days = duration / 86400;
1034+
duration %= 86400;
1035+
int hours = duration / 3600;
1036+
duration %= 3600;
1037+
int minutes = duration / 60;
1038+
int seconds = duration % 60;
1039+
1040+
char buf[64];
1041+
if ( days > 0 ) {
1042+
snprintf( buf, 64, "%dd %dh %dm", days, hours, minutes );
1043+
} else if ( hours > 0 ) {
1044+
snprintf( buf, 64, "%dh %dm", hours, minutes );
1045+
} else {
1046+
snprintf( buf, 64, "%dm %ds", minutes, seconds );
1047+
}
1048+
return std::string( buf );
1049+
}
10281050
#endif
10291051

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

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

10461069
while ( struct utmpx* entry = getutxent() ) {
10471070
if ( ( entry->ut_type == USER_PROCESS ) && ( username == std::string( entry->ut_user ) ) ) {
10481071
/* does line show unattached mosh session */
10491072
std::string text( entry->ut_host );
10501073
if ( ( text.size() >= 5 ) && ( text.substr( 0, 5 ) == "mosh " ) && ( text[text.size() - 1] == ']' )
10511074
&& ( text != ignore_entry ) && device_exists( entry->ut_line ) ) {
1075+
long int age = now - entry->ut_tv.tv_sec;
1076+
if ( age < 0 )
1077+
age = 0;
1078+
text += " (age " + format_duration( age ) + ")";
10521079
unattached_mosh_servers.push_back( text );
10531080
}
10541081
}

0 commit comments

Comments
 (0)