~ubuntu-branches/ubuntu/maverick/mosh/maverick-backports

« back to all changes in this revision

Viewing changes to src/frontend/mosh-server.cc

  • Committer: Package Import Robot
  • Author(s): Keith Winstein
  • Date: 2012-03-12 04:51:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120312045143-0pib9hs84wiauznl
Tags: 1.0-1
* Version 1.0 released.

* mosh now supports --version option and prints no-warranty message.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
17
*/
18
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <errno.h>
19
22
#include <locale.h>
20
23
#include <string.h>
21
24
#include <langinfo.h>
22
25
#include <termios.h>
23
26
#include <unistd.h>
24
27
#include <stdio.h>
25
 
#include <pty.h>
26
28
#include <stdlib.h>
27
29
#include <poll.h>
28
30
#include <sys/ioctl.h>
30
32
#include <pwd.h>
31
33
#include <typeinfo>
32
34
#include <signal.h>
33
 
#include <sys/signalfd.h>
 
35
#ifdef HAVE_UTEMPTER
34
36
#include <utempter.h>
 
37
#endif
35
38
#include <sys/socket.h>
36
39
#include <netinet/in.h>
37
40
#include <arpa/inet.h>
38
41
 
 
42
extern "C" {
 
43
#include "selfpipe.h"
 
44
}
 
45
 
39
46
#include "completeterminal.h"
40
47
#include "swrite.h"
41
48
#include "user.h"
 
49
#include "fatal_assert.h"
 
50
 
 
51
#if HAVE_PTY_H
 
52
#include <pty.h>
 
53
#elif HAVE_UTIL_H
 
54
#include <util.h>
 
55
#endif
42
56
 
43
57
#include "networktransport.cc"
44
58
 
48
62
            Terminal::Complete &terminal,
49
63
            ServerConnection &network );
50
64
 
 
65
int run_server( const char *desired_ip, const char *desired_port );
 
66
 
51
67
using namespace std;
52
68
 
53
69
int main( int argc, char *argv[] )
54
70
{
55
71
  char *desired_ip = NULL;
 
72
  char *desired_port = NULL;
56
73
  if ( argc == 1 ) {
57
74
    desired_ip = NULL;
58
75
  } else if ( argc == 2 ) {
59
76
    desired_ip = argv[ 1 ];
 
77
  } else if ( argc == 3 ) {
 
78
    desired_ip = argv[ 1 ];
 
79
    desired_port = argv[ 2 ];
60
80
  } else {
61
 
    fprintf( stderr, "Usage: %s [LOCALADDR]\n", argv[ 0 ] );
 
81
    fprintf( stderr, "Usage: %s [LOCALADDR] [PORT]\n", argv[ 0 ] );
62
82
    exit( 1 );
63
83
  }
64
84
 
74
94
    exit( 1 );
75
95
  }
76
96
 
 
97
  try {
 
98
    return run_server( desired_ip, desired_port );
 
99
  } catch ( Network::NetworkException e ) {
 
100
    fprintf( stderr, "Network exception: %s: %s\n",
 
101
             e.function.c_str(), strerror( e.the_errno ) );
 
102
    return 1;
 
103
  } catch ( Crypto::CryptoException e ) {
 
104
    fprintf( stderr, "Crypto exception: %s\n",
 
105
             e.text.c_str() );
 
106
    return 1;
 
107
  }
 
108
}
 
109
 
 
110
int run_server( const char *desired_ip, const char *desired_port ) {
77
111
  /* get initial window size */
78
112
  struct winsize window_size;
79
113
  if ( ioctl( STDIN_FILENO, TIOCGWINSZ, &window_size ) < 0 ) {
86
120
 
87
121
  /* open network */
88
122
  Network::UserStream blank;
89
 
  ServerConnection network( terminal, blank, desired_ip );
 
123
  ServerConnection network( terminal, blank, desired_ip, desired_port );
90
124
 
91
125
  /* network.set_verbose(); */
92
126
 
96
130
  /* don't let signals kill us */
97
131
  sigset_t signals_to_block;
98
132
 
99
 
  assert( sigemptyset( &signals_to_block ) == 0 );
100
 
  assert( sigaddset( &signals_to_block, SIGTERM ) == 0 );
101
 
  assert( sigaddset( &signals_to_block, SIGINT ) == 0 );
102
 
  assert( sigaddset( &signals_to_block, SIGHUP ) == 0 );
103
 
  assert( sigaddset( &signals_to_block, SIGPIPE ) == 0 );
104
 
  assert( sigprocmask( SIG_BLOCK, &signals_to_block, NULL ) == 0 );
 
133
  fatal_assert( sigemptyset( &signals_to_block ) == 0 );
 
134
  fatal_assert( sigaddset( &signals_to_block, SIGHUP ) == 0 );
 
135
  fatal_assert( sigaddset( &signals_to_block, SIGPIPE ) == 0 );
 
136
  fatal_assert( sigprocmask( SIG_BLOCK, &signals_to_block, NULL ) == 0 );
105
137
 
106
138
  struct termios child_termios;
107
139
 
119
151
    _exit( 0 );
120
152
  }
121
153
 
122
 
  fprintf( stderr, "[mosh-server detached, pid=%d.]\n", (int)getpid() );
 
154
  fprintf( stderr, "\nmosh-server (%s)\n", PACKAGE_STRING );
 
155
  fprintf( stderr, "Copyright 2012 Keith Winstein <mosh-devel@mit.edu>\n" );
 
156
  fprintf( stderr, "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.\n\n" );
 
157
 
 
158
  fprintf( stderr, "[mosh-server detached, pid = %d]\n", (int)getpid() );
123
159
 
124
160
  int master;
125
161
 
142
178
 
143
179
    /* unblock signals */
144
180
    sigset_t signals_to_block;
145
 
    assert( sigemptyset( &signals_to_block ) == 0 );
146
 
    assert( sigprocmask( SIG_SETMASK, &signals_to_block, NULL ) == 0 );
 
181
    fatal_assert( sigemptyset( &signals_to_block ) == 0 );
 
182
    fatal_assert( sigprocmask( SIG_SETMASK, &signals_to_block, NULL ) == 0 );
147
183
 
148
184
    /* set TERM */
149
185
    if ( setenv( "TERM", "xterm", true ) < 0 ) {
172
208
 
173
209
    char *my_argv[ 2 ];
174
210
    my_argv[ 0 ] = strdup( pw->pw_shell );
175
 
    assert( my_argv[ 0 ] );
 
211
    fatal_assert( my_argv[ 0 ] );
176
212
    my_argv[ 1 ] = NULL;
177
213
    
178
 
    if ( execve( pw->pw_shell, my_argv, environ ) < 0 ) {
 
214
    if ( execv( pw->pw_shell, my_argv ) < 0 ) {
179
215
      perror( "execve" );
180
216
      exit( 1 );
181
217
    }
182
218
    exit( 0 );
183
219
  } else {
184
220
    /* parent */
 
221
 
 
222
    #ifdef HAVE_UTEMPTER
185
223
    /* make utmp entry */
186
224
    char tmp[ 64 ];
187
225
    snprintf( tmp, 64, "mosh [%d]", getpid() );
188
226
    utempter_add_record( master, tmp );
 
227
    #endif
189
228
 
190
229
    try {
191
230
      serve( master, terminal, network );
202
241
      exit( 1 );
203
242
    }
204
243
 
 
244
    #ifdef HAVE_UTEMPTER
205
245
    utempter_remove_added_record();
 
246
    #endif
206
247
  }
207
248
 
208
249
  printf( "\n[mosh-server is exiting.]\n" );
213
254
void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network )
214
255
{
215
256
  /* establish fd for shutdown signals */
216
 
  sigset_t signal_mask;
217
 
 
218
 
  assert( sigemptyset( &signal_mask ) == 0 );
219
 
  assert( sigaddset( &signal_mask, SIGTERM ) == 0 );
220
 
  assert( sigaddset( &signal_mask, SIGINT ) == 0 );
221
 
 
222
 
  int shutdown_signal_fd = signalfd( -1, &signal_mask, 0 );
223
 
  if ( shutdown_signal_fd < 0 ) {
224
 
    perror( "signalfd" );
 
257
  int signal_fd = selfpipe_init();
 
258
  if ( signal_fd < 0 ) {
 
259
    perror( "selfpipe_init" );
225
260
    return;
226
261
  }
227
262
 
 
263
  fatal_assert( selfpipe_trap( SIGTERM ) == 0 );
 
264
  fatal_assert( selfpipe_trap( SIGINT ) == 0 );
 
265
 
228
266
  /* prepare to poll for events */
229
267
  struct pollfd pollfds[ 3 ];
230
268
 
234
272
  pollfds[ 1 ].fd = host_fd;
235
273
  pollfds[ 1 ].events = POLLIN;
236
274
 
237
 
  pollfds[ 2 ].fd = shutdown_signal_fd;
 
275
  pollfds[ 2 ].fd = signal_fd;
238
276
  pollfds[ 2 ].events = POLLIN;
239
277
 
240
278
  uint64_t last_remote_num = network.get_remote_state_num();
241
279
 
 
280
  #ifdef HAVE_UTEMPTER
242
281
  bool connected_utmp = false;
 
282
  #endif
 
283
 
243
284
  struct in_addr saved_addr;
244
285
  saved_addr.s_addr = 0;
245
286
 
247
288
    try {
248
289
      uint64_t now = Network::timestamp();
249
290
 
250
 
      int active_fds = poll( pollfds, 3, min( network.wait_time(), terminal.wait_time( now ) ) );
251
 
      if ( active_fds < 0 ) {
 
291
      const int timeout_if_no_client = 60000;
 
292
      int poll_timeout = min( network.wait_time(), terminal.wait_time( now ) );
 
293
      if ( !network.has_remote_addr() ) {
 
294
        poll_timeout = min( poll_timeout, timeout_if_no_client );
 
295
      }
 
296
 
 
297
      int active_fds = poll( pollfds, 3, poll_timeout );
 
298
      if ( active_fds < 0 && errno == EINTR ) {
 
299
        continue;
 
300
      } else if ( active_fds < 0 ) {
252
301
        perror( "poll" );
253
302
        break;
254
303
      }
255
304
 
256
305
      now = Network::timestamp();
 
306
      uint64_t time_since_remote_state = now - network.get_latest_remote_state().timestamp;
257
307
 
258
308
      if ( pollfds[ 0 ].revents & POLLIN ) {
259
309
        /* packet received from the network */
298
348
            break;
299
349
          }
300
350
 
 
351
          #ifdef HAVE_UTEMPTER
301
352
          /* update utmp entry if we have become "connected" */
302
353
          if ( (!connected_utmp)
303
354
               || ( saved_addr.s_addr != network.get_remote_ip().s_addr ) ) {
311
362
 
312
363
            connected_utmp = true;
313
364
          }
 
365
          #endif
314
366
        }
315
367
      }
316
368
      
322
374
        /* fill buffer if possible */
323
375
        ssize_t bytes_read = read( pollfds[ 1 ].fd, buf, buf_size );
324
376
        if ( bytes_read == 0 ) { /* EOF */
325
 
          return;
 
377
          if ( !network.has_remote_addr() ) {
 
378
            return;
 
379
          } else if ( !network.shutdown_in_progress() ) {
 
380
            network.start_shutdown();
 
381
          }
326
382
        } else if ( bytes_read < 0 ) {
327
383
          perror( "read" );
328
384
          return;
343
399
 
344
400
      if ( pollfds[ 2 ].revents & POLLIN ) {
345
401
        /* shutdown signal */
346
 
        struct signalfd_siginfo the_siginfo;
347
 
        ssize_t bytes_read = read( pollfds[ 2 ].fd, &the_siginfo, sizeof( the_siginfo ) );
348
 
        if ( bytes_read == 0 ) {
 
402
        int signo = selfpipe_read();
 
403
        if ( signo == 0 ) {
349
404
          break;
350
 
        } else if ( bytes_read < 0 ) {
351
 
          perror( "read" );
 
405
        } else if ( signo < 0 ) {
 
406
          perror( "selfpipe_read" );
352
407
          break;
353
408
        }
354
409
 
355
 
        if ( network.attached() && (!network.shutdown_in_progress()) ) {
 
410
        if ( network.has_remote_addr() && (!network.shutdown_in_progress()) ) {
356
411
          network.start_shutdown();
357
412
        } else {
358
413
          break;
368
423
      if ( (pollfds[ 1 ].revents)
369
424
           & (POLLERR | POLLHUP | POLLNVAL) ) {
370
425
        /* host problem */
371
 
        if ( network.attached() ) {
 
426
        if ( network.has_remote_addr() ) {
372
427
          network.start_shutdown();
373
428
        } else {
374
429
          break;
390
445
        break;
391
446
      }
392
447
 
 
448
      #ifdef HAVE_UTEMPTER
393
449
      /* update utmp if has been more than 10 seconds since heard from client */
394
450
      if ( connected_utmp ) {
395
 
        if ( network.get_latest_remote_state().timestamp < now - 10000 ) {
 
451
        if ( time_since_remote_state > 10000 ) {
396
452
          utempter_remove_added_record();
397
453
 
398
454
          char tmp[ 64 ];
402
458
          connected_utmp = false;
403
459
        }
404
460
      }
 
461
      #endif
405
462
 
406
463
      if ( terminal.set_echo_ack( now ) ) {
407
464
        /* update client with new echo ack */
410
467
        }
411
468
      }
412
469
 
 
470
      if ( !network.has_remote_addr()
 
471
           && time_since_remote_state >= uint64_t( timeout_if_no_client ) ) {
 
472
        fprintf( stderr, "No connection within %d seconds.\n",
 
473
                 timeout_if_no_client / 1000 );
 
474
        break;
 
475
      }
 
476
 
413
477
      network.tick();
414
478
    } catch ( Network::NetworkException e ) {
415
479
      fprintf( stderr, "%s: %s\n", e.function.c_str(), strerror( e.the_errno ) );