aboutsummaryrefslogtreecommitdiff
path: root/lib/rlogin.exp
blob: f4a30ea85869d865326fc671a541343b158ef976 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Copyright (C) 1992-2010 Free Software Foundation, Inc.
#
# This file is part of DejaGnu.
#
# DejaGnu is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DejaGnu is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with DejaGnu; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.

# Connect to ARG using rlogin. This is for systems using rlogin to
# braindead targets. It returns either the spawn_id or a -1.
#
proc rlogin_open { arg } {
    global board_info

    set tries 0
    set result -1

    if {[board_info $arg exists fileid]} {
	return [board_info $arg fileid]
    }

    # get the hostname and port number from the config array
    if {[board_info $arg exists netport]} {
	set hostname [lindex [split [board_info $arg netport] ":"] 0]
    } else {
	set hostname $arg
    }

    if {![board_info $arg exists shell_prompt]} {
	# if no prompt, then set it to something generic
	set shell_prompt ".*> "
    } else {
	set shell_prompt [board_info $arg shell_prompt]
    }

    if {[board_info $arg exists fileid]} {
	unset board_info($arg,fileid)
    }
    # get the right version of rlogin
    if {![board_info $arg exists rlogin_prog]} {
	set RLOGIN rlogin
    } else {
	set RLOGIN [board_info $arg rlogin_prog]
    }

    # start connection and store the spawn_id
    verbose "Opening a $RLOGIN connection to $hostname" 2
    spawn $RLOGIN $hostname
    if { $spawn_id < 0 } {
	perror "invalid spawn id from rlogin"
	return -1
    }
    set board_info($arg,fileid) $spawn_id

    # Try to connect to the target. We give up after 3 attempts.
    while { $tries <= 3 } {
	expect {
	    -re ".*$shell_prompt.*$" {
		verbose "Got prompt\n"
		set result 0
		break
	    }
	    -re "TERM = .*\\)\[ ]*$" {
		send "dumb\r\n"
		expect {
		    "Terminal type is*$" {
			verbose "rlogin: set the terminal to dumb" 2
		    }
		    default {
			warning "rlogin: couldn't set terminmal type"
		    }
		}
		set result 10
		break
	    }
	    "unknown host" {
		perror "rlogin: unknown host"
		break
	    }
	    "has logged on from" {
		exp_continue
	    }
	    "Terminal type is" {
		verbose "rlogin: connected, got terminal prompt" 2
		set result 0
		break
	    }
	    -re "Maximum number of users already logged in.*$" {
		warning "rlogin: maximum number of users already logged in"
	    }
	    -re "Sorry, shell is locked.*Connection closed.*$" {
		warning "rlogin: lready connected."
	    }
	    -re "Sorry, this system is engaged.*Connection closed.*$" {
		warning "rlogin: system engaged."
	    }
	    timeout	{
		warning "rlogin: timed out trying to connect."
	    }
	    eof {
		perror "rlogin: got EOF while trying to connect."
		break
	    }
	}
	incr tries
    }

    # see if we maxed out on errors
    if { $result < 0 } {
	catch "close -i $spawn_id"
	catch "wait -i $spawn_id"
	set spawn_id -1
    } else {
	verbose "rlogin: connected to $hostname" 2
    }

    return $spawn_id
}

# Start CMDLINE running on DEST. Return the shell_id associated with
# the command.
#
proc rlogin_spawn { dest cmdline } {
    if {![board_info $dest exists shell_prompt]} {
	set shell_prompt "(^|\[\r\n\])\[^\r\n\]*>"
    } else {
	set shell_prompt [board_info $dest shell_prompt]
    }
    set prefix ""
    set ok 0
    for { set i 0 } {$i <= 2 && ! $ok} { incr i } {
	set shell_id [remote_open $dest]
	if { $shell_id != "" && $shell_id > 0 } {
	    remote_send $dest "echo k\r"
	    remote_expect $dest 20 {
		-re "\\(gdb\\)" {
		    set shell_prompt "\\(gdb\\)"
		    # gdb uses 'shell command'.
		    set prefix "shell "
		    set ok 1
		}
		-re ".*$shell_prompt" {
		    set ok 1
		}
		default { }
	    }
	}
	if { ! $ok } {
	    remote_close $dest
	    remote_reboot $dest
	}
    }
    if { ! $ok } {
	return "unable to start command"
    } else {
	remote_send $dest "${prefix}${cmdline}\n"
	return [board_info $dest fileid]
    }
}