mosh-server now takes command argument

This commit is contained in:
Keith Winstein
2012-03-17 15:21:33 -04:00
parent df54a869e9
commit b497cf42ef
2 changed files with 32 additions and 14 deletions
+4
View File
@@ -21,6 +21,7 @@ mosh-server \- server-side helper for mosh
.B mosh-server .B mosh-server
[IP address to bind] [IP address to bind]
[port number to bind] [port number to bind]
[command]
.br .br
.SH DESCRIPTION .SH DESCRIPTION
\fBmosh-server\fP is a helper program for the \fBmosh-server\fP is a helper program for the
@@ -36,6 +37,9 @@ By default, \fBmosh-server\fP binds to a port between 60000 and
61000. If a port number is supplied on the command line, 61000. If a port number is supplied on the command line,
\fBmosh-server\fP will bind to it instead. \fBmosh-server\fP will bind to it instead.
Without a command argument, \fBmosh-server\fP executes the user's
login shell.
\fBmosh-server\fP maintains an entry in the \fBmosh-server\fP maintains an entry in the
.BR utmp(5) .BR utmp(5)
file to indicate its process ID, whether the session is connected, file to indicate its process ID, whether the session is connected,
+28 -14
View File
@@ -62,7 +62,8 @@ void serve( int host_fd,
Terminal::Complete &terminal, Terminal::Complete &terminal,
ServerConnection &network ); ServerConnection &network );
int run_server( const char *desired_ip, const char *desired_port ); int run_server( const char *desired_ip, const char *desired_port,
const char *command );
using namespace std; using namespace std;
@@ -70,6 +71,7 @@ int main( int argc, char *argv[] )
{ {
char *desired_ip = NULL; char *desired_ip = NULL;
char *desired_port = NULL; char *desired_port = NULL;
char *command = NULL;
if ( argc == 1 ) { if ( argc == 1 ) {
desired_ip = NULL; desired_ip = NULL;
} else if ( argc == 2 ) { } else if ( argc == 2 ) {
@@ -77,8 +79,12 @@ int main( int argc, char *argv[] )
} else if ( argc == 3 ) { } else if ( argc == 3 ) {
desired_ip = argv[ 1 ]; desired_ip = argv[ 1 ];
desired_port = argv[ 2 ]; desired_port = argv[ 2 ];
} else if ( argc == 4 ) {
desired_ip = argv[ 1 ];
desired_port = argv[ 2 ];
command = argv[ 3 ];
} else { } else {
fprintf( stderr, "Usage: %s [LOCALADDR] [PORT]\n", argv[ 0 ] ); fprintf( stderr, "Usage: %s [LOCALADDR] [PORT] [COMMAND]\n", argv[ 0 ] );
exit( 1 ); exit( 1 );
} }
@@ -95,7 +101,7 @@ int main( int argc, char *argv[] )
} }
try { try {
return run_server( desired_ip, desired_port ); return run_server( desired_ip, desired_port, command );
} catch ( Network::NetworkException e ) { } catch ( Network::NetworkException e ) {
fprintf( stderr, "Network exception: %s: %s\n", fprintf( stderr, "Network exception: %s: %s\n",
e.function.c_str(), strerror( e.the_errno ) ); e.function.c_str(), strerror( e.the_errno ) );
@@ -107,7 +113,8 @@ int main( int argc, char *argv[] )
} }
} }
int run_server( const char *desired_ip, const char *desired_port ) { int run_server( const char *desired_ip, const char *desired_port,
const char *command ) {
/* get initial window size */ /* get initial window size */
struct winsize window_size; struct winsize window_size;
if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) { if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
@@ -201,16 +208,23 @@ int run_server( const char *desired_ip, const char *desired_port ) {
exit( 1 ); exit( 1 );
} }
/* get shell name */ /* get command to run */
struct passwd *pw = getpwuid( geteuid() ); string shell_name;
if ( pw == NULL ) {
perror( "getpwuid" );
exit( 1 );
}
string shell_name( pw->pw_shell ); if ( command ) {
if ( shell_name.empty() ) { /* empty shell means Bourne shell */ shell_name = command;
shell_name = "/bin/sh"; } else {
/* get shell name */
struct passwd *pw = getpwuid( geteuid() );
if ( pw == NULL ) {
perror( "getpwuid" );
exit( 1 );
}
shell_name = pw->pw_shell;
if ( shell_name.empty() ) { /* empty shell means Bourne shell */
shell_name = "/bin/sh";
}
} }
string login_shell = "-" + shell_name; string login_shell = "-" + shell_name;
@@ -220,7 +234,7 @@ int run_server( const char *desired_ip, const char *desired_port ) {
fatal_assert( my_argv[ 0 ] ); fatal_assert( my_argv[ 0 ] );
my_argv[ 1 ] = NULL; my_argv[ 1 ] = NULL;
if ( execv( pw->pw_shell, my_argv ) < 0 ) { if ( execv( shell_name.c_str(), my_argv ) < 0 ) {
perror( "execve" ); perror( "execve" );
exit( 1 ); exit( 1 );
} }