Add tests for mixed C/C++ unit test library usage
[dejagnu.git] / lib / rsh.exp
blob43f543041bdd08006da7cb9055bff9164c30aa9b
1 # Copyright (C) 1992-2019, 2020 Free Software Foundation, Inc.
3 # This file is part of DejaGnu.
5 # DejaGnu is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # DejaGnu is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with DejaGnu; if not, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
19 # Connect to HOSTNAME using rsh(1).
21 proc rsh_open { hostname } {
22 global spawn_id
24 set tries 0
25 set result -1
27 if {![board_info $hostname exists rsh_prog]} {
28 if { [which remsh] != 0 } {
29 set RSH remsh
30 } else {
31 set RSH rsh
33 } else {
34 set RSH [board_info $hostname rsh_prog]
37 if {[board_info $hostname exists username]} {
38 set rsh_useropts "-l [board_info $hostname username]"
39 } else {
40 set rsh_useropts ""
43 # Get the hostname and port number from the config array.
44 if {[board_info $hostname exists name]} {
45 set hostname [board_info $hostname name]
47 set hostname [lindex [split [board_info $hostname netport] ":"] 0]
48 if {[board_info $hostname exists shell_prompt]} {
49 set shell_prompt [board_info $hostname shell_prompt]
50 } else {
51 set shell_prompt ".*> "
54 if {[board_info $hostname exists fileid]} {
55 unset board_info($hostname,fileid)
58 spawn $RSH $rsh_useropts $hostname
59 if { $spawn_id < 0 } {
60 perror "invalid spawn id from $RSH"
61 return -1
64 send "\r\n"
65 while { $tries <= 3 } {
66 expect {
67 -re ".*$shell_prompt.*$" {
68 verbose "Got prompt\n"
69 set result 0
70 break
72 -re "TERM = .*$" {
73 warning "Setting terminal type to vt100"
74 set result 0
75 send "vt100\n"
76 break
78 "unknown host" {
79 exp_send "\003"
80 perror "telnet: unknown host"
81 break
83 "has logged on from" {
84 exp_continue
86 -re "isn't registered for Kerberos.*service.*$" {
87 warning "$RSH: isn't registered for Kerberos, please kinit"
88 catch close
89 catch wait
90 break
92 -re "Kerberos rcmd failed.*$" {
93 warning "$RSH: Kerberos rcmd failed, please kinit"
94 catch close
95 catch wait
96 break
98 -re "You have no Kerberos tickets.*$" {
99 warning "$RSH: No kerberos Tickets, please kinit"
100 catch close
101 catch wait
102 break
104 "Terminal type is" {
105 verbose "$RSH: connected, got terminal prompt" 2
106 set result 0
107 break
109 -re "trying normal rlogin.*$" {
110 warning "$RSH: trying normal rlogin."
111 catch close
112 catch wait
113 break
115 -re "unencrypted connection.*$" {
116 warning "$RSH: unencrypted connection, please kinit"
117 catch close
118 catch wait
119 break
121 -re "Sorry, shell is locked.*Connection closed.*$" {
122 warning "$RSH: already connected."
124 timeout {
125 warning "$RSH: timed out trying to connect."
127 eof {
128 perror "$RSH: got EOF while trying to connect."
129 break
132 incr tries
135 if { $result < 0 } {
136 close -i $spawn_id
137 set spawn_id -1
138 } else {
139 set board_info($hostname,fileid) $spawn_id
142 return $spawn_id
145 # Download SRCFILE to DESTFILE on DESTHOST.
147 proc rsh_download {desthost srcfile destfile} {
148 # must be done before desthost is rewritten
149 if {[board_info $desthost exists rcp_prog]} {
150 set RCP [board_info $desthost rcp_prog]
151 } else {
152 set RCP rcp
155 if {[board_info $desthost exists rsh_prog]} {
156 set RSH [board_info $desthost rsh_prog]
157 } else {
158 if { [which remsh] != 0 } {
159 set RSH remsh
160 } else {
161 set RSH rsh
165 if {[board_info $desthost exists username]} {
166 set rsh_useropts "-l [board_info $desthost username]"
167 set rcp_user "[board_info $desthost username]@"
168 } else {
169 set rsh_useropts ""
170 set rcp_user ""
173 if {[board_info $desthost exists name]} {
174 set desthost [board_info $desthost name]
177 if {[board_info $desthost exists hostname]} {
178 set desthost [board_info $desthost hostname]
181 set status [catch "exec $RSH $rsh_useropts $desthost rm -f $destfile |& cat" output]
182 set status [catch "exec $RCP $srcfile $rcp_user$desthost:$destfile |& cat" output]
183 if { $status == 0 } {
184 verbose "Copied $srcfile to $desthost:$destfile" 2
185 return $destfile
186 } else {
187 verbose "Download to $desthost failed, $output."
188 return ""
192 proc rsh_upload {desthost srcfile destfile} {
193 if {[board_info $desthost exists rcp_prog]} {
194 set RCP [board_info $desthost rcp_prog]
195 } else {
196 set RCP rcp
199 if {[board_info $desthost exists username]} {
200 set rcp_user "[board_info $desthost username]@"
201 } else {
202 set rcp_user ""
205 if {[board_info $desthost exists name]} {
206 set desthost [board_info $desthost name]
209 if {[board_info $desthost exists hostname]} {
210 set desthost [board_info $desthost hostname]
213 set status [catch "exec $RCP $rcp_user$desthost:$srcfile $destfile" output]
214 if { $status == 0 } {
215 verbose "Copied $desthost:$srcfile to $destfile" 2
216 return $destfile
217 } else {
218 verbose "Upload from $desthost failed, $output."
219 return ""
223 # Execute CMD on BOARDNAME.
225 proc rsh_exec { boardname program pargs inp outp } {
226 global timeout
228 verbose "Executing on $boardname: $program $pargs < $inp"
230 if {![board_info $boardname exists rsh_prog]} {
231 if { [which remsh] != 0 } {
232 set RSH remsh
233 } else {
234 set RSH rsh
236 } else {
237 set RSH [board_info $boardname rsh_prog]
240 if {[board_info $boardname exists username]} {
241 set rsh_useropts "-l [board_info $boardname username]"
242 } else {
243 set rsh_useropts ""
246 if {[board_info $boardname exists name]} {
247 set boardname [board_info $boardname name]
250 if {[board_info $boardname exists hostname]} {
251 set hostname [board_info $boardname hostname]
252 } else {
253 set hostname $boardname
256 # If CMD sends any output to stderr, exec will think it failed.
257 # More often than not that will be true, but it doesn't catch the
258 # case where there is no output but the exit code is non-zero.
259 if { $inp eq "" } {
260 set inp "/dev/null"
263 set ret [local_exec "$RSH $rsh_useropts $hostname sh -c '$program $pargs \\; echo XYZ\\\${?}ZYX'" $inp $outp $timeout]
264 set status [lindex $ret 0]
265 set output [lindex $ret 1]
267 verbose "$RSH status is $status, output is $output"
269 # `status' doesn't mean much here other than rsh worked ok.
270 # What we want is whether $program ran ok. Return $status
271 # if the program timed out, status will be 1 indicating that
272 # rsh ran and failed. If rsh fails, we will get FAIL rather
273 # than UNRESOLVED - this will help the problem be noticed.
274 if { $status != 0 } {
275 regsub "XYZ(\[0-9\]*)ZYX\n?" $output "" output
276 return [list $status "$RSH to $boardname failed for $program, $output"]
278 if { [regexp "XYZ(\[0-9\]*)ZYX" $output junk status] == 0 } {
279 set status ""
281 verbose "rsh_exec: status:$status text:$output" 4
282 if { $status eq "" } {
283 return [list -1 "Couldn't parse $RSH output, $output."]
285 regsub "XYZ(\[0-9\]*)ZYX\n?" $output "" output
286 return [list [expr {$status != 0}] $output]