mobile-shell / mosh

Mobile Shell
https://mosh.org
GNU General Public License v3.0
12.7k stars 743 forks source link

Implement a -u flag in mosh-server to print detached mosh sessions th… #1327

Open john-at-charpa opened 4 months ago

john-at-charpa commented 4 months ago

Greetings, I noticed that there was no way (aside from manually starting mosh-server) to get a list of currently detached sessions. Since a few other people have asked for this feature, and I needed it myself, I went ahead and took at swing at seeing if it was possible. As it turns out very little code was needed. I tried to implement it as simply and unobtrusively as possible. Please consider merging this in as it will satisfy a feature request for 1.5. The code can also be applied to the current 1.4.0 release.

Cheers!

achernya commented 4 months ago

I took a look at the proposed change and I'm a bit confused --- won't this print an unattached session, not all unattached sessions?

john-at-charpa commented 4 months ago

The function warn_unattached() finds them all and prints based on the count.

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 {
    std::string pid_string;

    for ( std::vector<std::string>::const_iterator it = unattached_mosh_servers.begin();
          it != unattached_mosh_servers.end();
          it++ ) {
      pid_string += "        - " + *it + "\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() );
  }

The call I make is the same call made automatically when mosh-server is run. The only change is that by using the -u flag it calls it and exits (0) instead of spawning another mosh-server.

$ mosh-server -u
Mosh: You have 3 detached Mosh sessions on this server, with PIDs:
        - mosh [81287]
        - mosh [82619]
        - mosh [84020]

So the sum total of changes is really just: o Move char utmp_entry[64] = { 0 }; to be global so that main can also access it o Add a check for the -u flag and call the same code called by the server start-up code (in the same manner, too).