xref: /plan9/rc/bin/srvssh (revision ddb951e381e761084e6b3ea97b340f3f88e014f8)
1#!/bin/rc
2
3# Serve Unix u9fs over SSH
4#
5# Basically, try each of the following until you find one that works:
6#
7#	srvssh unix
8#	srvssh -r unix
9#	srvssh -R unix
10#	srvssh -r -s unix
11#	srvssh -R -s unix
12#
13# and then never look back.  Note that "srvssh unix" should always
14# work.  It's just that if you're talking with certain sshd's, you'll get
15# hit by Nagle's algorithm and need to explore the other flags.
16
17# When using ssh to start u9fs, the only way to turn off
18# Nagle's algorithm (which kills the performance of RPC-based
19# protocols like 9P) is to allocate a pseudo-terminal.  The
20# command ssh -Rmp attempts to allocate a pseudo-terminal and
21# then put it in a transparent mode.  Especially when
22# connected to older SSH daemons, the connection ends up not
23# quite transparent.  To get around this, we explicity set the tty
24# mode on the command line as well.  The hope is that -Rmp makes
25# the connection transparent enough for the Tversion, and the stty
26# command will do the rest.  If -Rmp doesn't make the connection
27# transparent enough for the Tversion (but the stty commands do
28# make the connection fully transparent) then add "-s 5" to the srv
29# command to tell it to wait 5 seconds before sending the Tversion.
30# That should be enough time for the stty to take effect.
31
32rfork e
33
34fn usage {
35	echo 'usage: srvssh [-R] [-r] [-s] [-u u9fspath] system [srvname [mtpt]]' >[1=2]
36	exit usage
37}
38
39rawhack=''
40sleephack=()
41u9fspath=u9fs
42rawflags=''
43
44while(~ $1 -*){
45	switch($1){
46	case -r
47		rawflags='-Rmp'
48		shift
49	case -R
50		rawflags='-Rmp'
51		rawhack=('stty raw -echo '';''')
52		shift
53	case -s
54		sleephack=(-s 5)
55		shift
56	case -u
57		shift
58		u9fspath=$1
59		shift
60	case -u*
61		u9fspath=`{echo $1 | sed s/-u//}
62		shift
63	case *
64		usage
65	}
66}
67
68if(! ~ $#* 1 2 3)
69	usage
70
71switch($#*){
72case 1
73	srv=$1
74	mtpt=/n/$1
75case 2
76	srv=$2
77	mtpt=/n/$1
78case 3
79	srv=$2
80	mtpt=$3
81}
82
83x=(srv $sleephack -e \
84	'ssh '$rawflags' '$1' '$rawhack' '$u9fspath' -na none -u ''$''USER -l ''$''HOME/u9fs.log' \
85	$srv $mtpt)
86$x
87
88# Sometimes /srv/whatever can be a closed pipe, in which case
89# srv will have been killed for writing to it, without a chance to
90# defend itself.  Rerun it in this case.
91
92ss=$status
93if(~ $ss *'write on closed pipe'*){
94	rm -f /srv/$srv
95	$x
96	ss=$status
97}
98
99if(! ~ $ss '')
100	echo srvssh: $ss >[1=2]
101exit $ss
102