srvssh 2.2 KB

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