cross-test-ssh.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/bin/bash
  2. # Run a testcase on a remote system, via ssh.
  3. # Copyright (C) 2012-2026 Free Software Foundation, Inc.
  4. # This file is part of the GNU C Library.
  5. # The GNU C Library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2.1 of the License, or (at your option) any later version.
  9. # The GNU C Library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. # You should have received a copy of the GNU Lesser General Public
  14. # License along with the GNU C Library; if not, see
  15. # <https://www.gnu.org/licenses/>.
  16. # usage: cross-test-ssh.sh [--ssh SSH] HOST COMMAND ...
  17. # Run with --help flag to get more detailed help.
  18. progname="$(basename $0)"
  19. usage="usage: ${progname} [--ssh SSH] [--allow-time-setting] HOST COMMAND ..."
  20. help="Run a glibc test COMMAND on the remote machine HOST, via ssh,
  21. preserving the current working directory, and respecting quoting.
  22. If the '--ssh SSH' flag is present, use SSH as the SSH command,
  23. instead of ordinary 'ssh'.
  24. If the '--timeoutfactor FACTOR' flag is present, set TIMEOUTFACTOR on
  25. the remote machine to the specified FACTOR.
  26. If the '--glibctunables VALUE' flag is present, set GLIBC_TUNABLES on
  27. the remote machine to the specified VALUE.
  28. If the '--allow-time-setting' flag is present, set
  29. GLIBC_TEST_ALLOW_TIME_SETTING on the remote machine to indicate that
  30. time can be safely adjusted (e.g. on a virtual machine).
  31. To use this to run glibc tests, invoke the tests as follows:
  32. $ make test-wrapper='ABSPATH/cross-test-ssh.sh HOST' tests
  33. where ABSPATH is the absolute path to this script, and HOST is the
  34. name of the machine to connect to via ssh.
  35. If you need to connect to the test machine as a different user, you
  36. may specify that just as you would to SSH:
  37. $ make test-wrapper='ABSPATH/cross-test-ssh.sh USER@HOST' tests
  38. Naturally, the remote user must have an appropriate public key, and
  39. you will want to ensure that SSH does not prompt interactively for a
  40. password on each connection.
  41. HOST and the build machines (on which 'make check' is being run) must
  42. share a filesystem; all files needed by the tests must be visible at
  43. the same paths on both machines.
  44. ${progname} runs COMMAND in the same directory on the HOST that
  45. ${progname} itself is run in on the build machine.
  46. The command and arguments are passed to the remote host in a way that
  47. avoids any further shell substitution or expansion, on the assumption
  48. that the shell on the build machine has already done them
  49. appropriately."
  50. ssh='ssh'
  51. timeoutfactor=$TIMEOUTFACTOR
  52. glibctunables=$GLIBC_TUNABLES
  53. while [ $# -gt 0 ]; do
  54. case "$1" in
  55. "--ssh")
  56. shift
  57. if [ $# -lt 1 ]; then
  58. break
  59. fi
  60. ssh="$1"
  61. ;;
  62. "--timeoutfactor")
  63. shift
  64. if [ $# -lt 1 ]; then
  65. break
  66. fi
  67. timeoutfactor="$1"
  68. ;;
  69. "--glibctunables")
  70. shift
  71. if [ $# -lt 1 ]; then
  72. break
  73. fi
  74. glibctunables="$1"
  75. ;;
  76. "--allow-time-setting")
  77. settimeallowed="1"
  78. ;;
  79. "--help")
  80. echo "$usage"
  81. echo "$help"
  82. exit 0
  83. ;;
  84. *)
  85. break
  86. ;;
  87. esac
  88. shift
  89. done
  90. if [ $# -lt 1 ]; then
  91. echo "$usage" >&2
  92. echo "Type '${progname} --help' for more detailed help." >&2
  93. exit 1
  94. fi
  95. host="$1"; shift
  96. # Print the sequence of arguments as strings properly quoted for the
  97. # Bourne shell, separated by spaces.
  98. bourne_quote ()
  99. {
  100. local arg qarg
  101. for arg in "$@"; do
  102. qarg=${arg//\'/\'\\\'\'}
  103. echo -n "'$qarg' "
  104. done
  105. }
  106. # Transform the current argument list into a properly quoted Bourne shell
  107. # command string.
  108. command="$(bourne_quote "$@")"
  109. # Add command to set the current directory.
  110. command="cd $(bourne_quote "$PWD")
  111. ${command}"
  112. # Add command to set the timeout factor, if required.
  113. if [ "$timeoutfactor" ]; then
  114. command="export TIMEOUTFACTOR=$(bourne_quote "$timeoutfactor")
  115. ${command}"
  116. fi
  117. # Add command to set glibc tunables, if required.
  118. if [ "$glibctunables" ]; then
  119. command="export GLIBC_TUNABLES=$(bourne_quote "$glibctunables")
  120. ${command}"
  121. fi
  122. # Add command to set the info that time on target can be adjusted,
  123. # if required.
  124. # Serialize execution of this script on target to prevent from unintended
  125. # change of target time.
  126. FLOCK_PATH="${FLOCK_PATH:-/var/lock/clock_settime}"
  127. FLOCK_TIMEOUT="${FLOCK_TIMEOUT:-20}"
  128. FLOCK_FD="${FLOCK_FD:-99}"
  129. if [ "$settimeallowed" ]; then
  130. command="exec ${FLOCK_FD}<>${FLOCK_PATH}
  131. flock -w ${FLOCK_TIMEOUT} ${FLOCK_FD}
  132. if [ $? -ne 0 ]; then exit 1; fi
  133. export GLIBC_TEST_ALLOW_TIME_SETTING=1
  134. ${command}"
  135. fi
  136. # HOST's sshd simply concatenates its arguments with spaces and
  137. # passes them to some shell. We want to force the use of /bin/sh,
  138. # so we need to re-quote the whole command to ensure it appears as
  139. # the sole argument of the '-c' option.
  140. full_command="$(bourne_quote "${command}")"
  141. $ssh "$host" /bin/sh -c "$full_command"