2 * Copyright (c) 1983, 1990 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 "@(#) Copyright (c) 1983, 1990 The Regents of the University of California.\n\
37 All rights reserved.\n";
42 static char sccsid
[] = "@(#)rlogin.c 5.33 (Berkeley) 3/1/91";
48 * $Header: mit/rlogin/RCS/rlogin.c,v 5.2 89/07/26 12:11:21 kfall
53 * rlogin - remote login
55 #include <sys/ioctl.h>
56 #include <sys/types.h>
59 #include <net/netlib.h>
61 #include <net/gen/in.h>
63 #include <net/gen/tcp.h>
64 #include <net/gen/tcp_io.h>
80 #include <kerberosIV/des.h>
81 #include <kerberosIV/krb.h>
84 Key_schedule schedule
;
85 int use_kerberos
= 1, doencrypt
;
86 char dst_realm_buf
[REALM_SZ
], *dest_realm
= NULL
;
87 extern char *krb_realmofhost();
90 #ifndef TIOCPKT_WINDOW
91 #define TIOCPKT_WINDOW 0x80
94 /* concession to Sun */
100 int eight
, litout
, rem
;
103 u_char escapechar
= '~';
110 { B0
, "0" }, { B50
, "50" }, { B75
, "75" }, { B110
, "110" },
111 { B134
, "134" }, { B150
, "150" }, { B200
, "200" }, { B300
, "300" },
112 { B600
, "600" }, { B1200
, "1200" }, { B1800
, "1800" },
113 { B2400
, "2400" }, { B4800
, "4800" }, { B9600
, "9600" },
114 { B19200
, "19200" }, { B38400
, "38400" }, { B57600
, "57600" },
115 { B115200
, "115200" },
120 /* flow control variables */
126 int inprogress_rd_rem
;
128 int inprogress_wr_rem
;
130 /* write to remote */
132 size_t wr_rem_offset
;
133 size_t extra_wr_rem_size
;
134 size_t extra_wr_rem_offset
;
136 size_t extra_wr_rem_new_size
;
137 char *extra_wr_rem_new
;
139 #endif /* __minix_vmd */
141 struct winsize winsize
;
143 #define get_window_size(fd, wp) ioctl(fd, TIOCGWINSZ, wp)
145 extern int main( int argc
, char **argv
);
146 static void usage( void ) __attribute__((noreturn
));
147 static u_char
getescape( char *p
);
148 static char *speeds2str( speed_t speed
);
149 static void lostpeer( int sig
);
150 static void doit( void );
151 static void setsignal( int sig
, void (*act
)(int sig
) );
152 static void msg( char *str
);
153 static void done( int status
);
155 static int reader( void );
157 static void mode( int f
);
159 static void mark_async( int fd
);
160 static void init_0( void );
161 static void init_1( void );
162 static void init_rd_rem( void );
163 static void init_wr_rem( void );
164 static void restart_0( void );
165 static void restart_1( void );
166 static void restart_rd_rem( void );
167 static void restart_wr_rem( void );
168 static void completed_0( int result
, int error
);
169 static void completed_1( int result
, int error
);
170 static void completed_rd_rem( int result
, int error
);
171 static void completed_wr_rem( int result
, int error
);
172 static void do_urg( int urg_byte
);
175 static void catch_child( int sig
);
176 static void writer( void );
178 static void echo( int c
);
180 static void finish( void );
181 static void sendwindow( void );
182 static void sigwinch( int sig
);
183 static void subshell( void );
195 nwio_tcpopt_t tcpopt
;
197 int argoff
, ch
, dflag
, one
, uid
;
198 char *host
, *p
, *user
, term
[1024];
204 if ((p
= rindex(argv
[0], '/')))
209 if (strcmp(p
, "rlogin"))
212 /* handle "rlogin host flags" */
213 if (!host
&& argc
> 2 && argv
[1][0] != '-') {
219 #define OPTIONS "8EKLde:k:l:x"
221 #define OPTIONS "8EKLde:l:"
223 while ((ch
= getopt(argc
- argoff
, argv
+ argoff
, OPTIONS
)) != EOF
)
243 escapechar
= getescape(optarg
);
247 dest_realm
= dst_realm_buf
;
248 (void)strncpy(dest_realm
, optarg
, REALM_SZ
);
258 des_set_key(cred
.session
, schedule
);
270 /* if haven't gotten a host yet, do so */
271 if (!host
&& !(host
= *argv
++))
277 if (!(pw
= getpwuid(uid
= getuid()))) {
278 (void)fprintf(stderr
, "rlogin: unknown user id.\n");
287 sp
= getservbyname((doencrypt
? "eklogin" : "klogin"), "tcp");
290 warning("can't get entry for %s/tcp service",
291 doencrypt
? "eklogin" : "klogin");
296 sp
= getservbyname("login", "tcp");
298 (void)fprintf(stderr
, "rlogin: login/tcp: unknown service.\n");
302 (void)strncpy(term
, (p
= getenv("TERM")) ? p
: "network", sizeof(term
));
303 term
[sizeof(term
)-1]= 0;
305 if (tcgetattr(0, &ttyb
) == 0) {
306 (void)strcat(term
, "/");
307 (void)strcat(term
, speeds2str(cfgetospeed(&ttyb
)));
310 (void)get_window_size(0, &winsize
);
312 (void)signal(SIGPIPE
, lostpeer
);
319 if (dest_realm
== NULL
)
320 dest_realm
= krb_realmofhost(host
);
324 rem
= krcmd_mutual(&host
, sp
->s_port
, user
, term
, 0,
325 dest_realm
, &cred
, schedule
);
328 rem
= krcmd(&host
, sp
->s_port
, user
, term
, 0,
332 sp
= getservbyname("login", "tcp");
334 (void)fprintf(stderr
,
335 "rlogin: unknown service login/tcp.\n");
338 if (errno
== ECONNREFUSED
)
339 warning("remote host doesn't support Kerberos");
341 warning("can't provide Kerberos auth data");
347 (void)fprintf(stderr
,
348 "rlogin: the -x flag requires Kerberos authentication.\n");
352 rem
= rcmd(&host
, sp
->s_port
, pw
->pw_name
, user
, term
, 0);
355 rem
= rcmd(&host
, sp
->s_port
, pw
->pw_name
, user
, term
, 0);
356 #endif /* KERBEROS */
361 /* Enable BSD compatibility for urgent data. */
362 tcpopt
.nwto_flags
= NWTO_BSD_URG
;
363 error
= ioctl(rem
, NWIOSTCPOPT
, &tcpopt
);
366 fprintf(stderr
, "rlogin: NWIOSTCPOPT failed: %s\n",
375 struct termios defattr
, rawattr
;
389 asio_fd_set_t fd_set
;
394 (void)tcgetattr(0, &sb
);
398 rawattr
.c_iflag
&= ~(ICRNL
| IGNCR
| INLCR
| ISTRIP
| IXOFF
| IXON
|
400 rawattr
.c_oflag
&= ~(OPOST
);
401 rawattr
.c_lflag
&= ~(ECHONL
| ECHO
| ICANON
| IEXTEN
| ISIG
);
403 (void)signal(SIGINT
, SIG_IGN
);
404 setsignal(SIGHUP
, exit
);
405 setsignal(SIGQUIT
, exit
);
410 (void)fprintf(stderr
, "rlogin: fork: %s.\n", strerror(errno
));
417 msg("connection closed.");
421 msg("\007connection closed.");
425 (void)signal(SIGCHLD
, catch_child
);
428 #else /* __minix_vmd */
431 /* mark the file descriptors 0, 1, and rem as asynchronous. */
442 ASIO_FD_ZERO(&fd_set
);
444 fw
.fw_bits
= fd_set
.afds_bits
;
445 fw
.fw_maxfd
= ASIO_FD_SETSIZE
;
447 if (more2read_0
&& !inprogress_0
)
450 fw
.fw_flags
|= FWF_NONBLOCK
;
453 if (more2write_1
&& !inprogress_1
)
456 fw
.fw_flags
|= FWF_NONBLOCK
;
459 if (more2read_rem
&& !inprogress_rd_rem
)
462 fw
.fw_flags
|= FWF_NONBLOCK
;
465 if (more2write_rem
&& !inprogress_wr_rem
)
468 fw
.fw_flags
|= FWF_NONBLOCK
;
471 if (more2read_0
&& inprogress_0
)
472 ASIO_FD_SET(0, ASIO_READ
, &fd_set
);
473 if (more2write_1
&& inprogress_1
)
474 ASIO_FD_SET(1, ASIO_WRITE
, &fd_set
);
475 if (more2read_rem
&& inprogress_rd_rem
)
476 ASIO_FD_SET(rem
, ASIO_READ
, &fd_set
);
477 if (more2write_rem
&& inprogress_wr_rem
)
478 ASIO_FD_SET(rem
, ASIO_WRITE
, &fd_set
);
483 if (result
== -1 && (errno
== EAGAIN
||
490 fprintf(stderr
, "fwait failed (%s)\n",
496 printf("fwait: fw_fw= %d, fw_operation= %d, fw_result= %d, fw.fw_errno= %d\n",
497 fw
.fw_fd
, fw
.fw_operation
, fw
.fw_result
, fw
.fw_errno
);
499 if (fw
.fw_fd
== 0 && fw
.fw_operation
== ASIO_READ
)
501 completed_0(fw
.fw_result
, fw
.fw_errno
);
503 else if (fw
.fw_fd
== 1 &&
504 fw
.fw_operation
== ASIO_WRITE
)
506 completed_1(fw
.fw_result
, fw
.fw_errno
);
508 else if (fw
.fw_fd
== rem
&&
509 fw
.fw_operation
== ASIO_READ
)
511 completed_rd_rem(fw
.fw_result
, fw
.fw_errno
);
513 else if (fw
.fw_fd
== rem
&&
514 fw
.fw_operation
== ASIO_WRITE
)
516 completed_wr_rem(fw
.fw_result
, fw
.fw_errno
);
521 "strange result from fwait: fd= %d, operation= %d\n",
522 fw
.fw_fd
, fw
.fw_operation
);
525 if (!(fw
.fw_flags
& FWF_MORE
))
534 #endif /* __minix_vmd */
535 msg("connection closed.");
539 /* trap a signal, unless it is being ignored. */
543 void(*act
) ( int sig
);
545 if (signal(sig
, act
) == SIG_IGN
)
546 (void)signal(sig
, SIG_IGN
);
558 /* make sure catch_child does not snap it up */
559 (void)signal(SIGCHLD
, SIG_DFL
);
560 if (kill(child
, SIGKILL
) >= 0)
561 while ((w
= wait(&wstatus
)) > 0 && w
!= child
);
581 pid
= waitpid(-1, &status
, WNOHANG
|WUNTRACED
);
584 /* if the child (reader) dies, just quit */
585 if (pid
< 0 || ((pid
== child
) && (!WIFSTOPPED(status
))))
586 done(WTERMSIG(status
) | WEXITSTATUS(status
));
594 * writer: write to remote: 0 -> line.
596 * ~^Z suspend rlogin process.
597 * ~<delayed-suspend char> suspend rlogin process, but leave reader alone.
602 register int bol
, local
, n
;
606 bol
= 1; /* beginning of line */
609 n
= read(STDIN_FILENO
, &ch
, 1);
611 if (n
< 0 && errno
== EINTR
)
617 * If we're at the beginning of the line and recognize a
618 * command character, then we echo locally. Otherwise,
619 * characters are echo'd remotely. If the command character
620 * is doubled, this acts as a force and local echo is
625 if (!noescape
&& c
== escapechar
) {
631 if (c
== '.' || c
== defattr
.c_cc
[VEOF
]) {
636 if (c
== defattr
.c_cc
[VSUSP
]) {
647 (void)des_write(rem
, &escapechar
, 1);
651 (void)write(rem
, &escapechar
, 1);
658 if (des_write(rem
, &ch
, 1) == 0) {
665 if (write(rem
, &ch
, 1) == 0) {
669 bol
= c
== defattr
.c_cc
[VKILL
] ||
670 c
== defattr
.c_cc
[VEOF
] ||
671 c
== defattr
.c_cc
[VINTR
] ||
672 c
== defattr
.c_cc
[VSUSP
] ||
673 c
== '\r' || c
== '\n';
692 } else if (c
== 0177) {
699 (void)write(STDOUT_FILENO
, buf
, p
- buf
);
708 (void)signal(SIGCHLD
, SIG_IGN
);
709 (void)kill(cmdc
== defltc
.t_suspc
? 0 : getpid(), SIGTSTP
);
710 (void)signal(SIGCHLD
, catch_child
);
712 sigwinch(); /* check for size changes */
725 signal(SIGWINCH
, sigwinch
);
728 if (dosigwinch
&& get_window_size(0, &ws
) == 0 &&
729 memcmp(&ws
, &winsize
, sizeof(ws
))) {
736 * Send the window size to the server via the magic escape
742 char *obuf
, *new_buf
;
744 new_buf
= realloc(extra_wr_rem_new
,
745 extra_wr_rem_new_size
+4+sizeof(*wp
));
748 extra_wr_rem_new
= new_buf
;
749 obuf
= new_buf
+extra_wr_rem_new_size
;
750 extra_wr_rem_new_size
+= 4+sizeof(*wp
);
755 wp
= (struct winsize
*)(obuf
+4);
760 wp
->ws_row
= htons(winsize
.ws_row
);
761 wp
->ws_col
= htons(winsize
.ws_col
);
762 wp
->ws_xpixel
= htons(winsize
.ws_xpixel
);
763 wp
->ws_ypixel
= htons(winsize
.ws_ypixel
);
765 #endif /* SIGWINCH */
770 * reader: read from remote: line -> 1
775 int rcvcnt
, rcvstate
;
776 char rcvbuf
[8 * 1024];
785 while ((remaining
= rcvcnt
- (bufp
- rcvbuf
)) > 0) {
787 n
= write(STDOUT_FILENO
, bufp
, remaining
);
802 rcvcnt
= des_read(rem
, rcvbuf
, sizeof(rcvbuf
));
806 rcvcnt
= read(rem
, rcvbuf
, sizeof (rcvbuf
));
813 nwio_tcpopt_t tcpopt
;
815 fprintf(stderr
, "\n\rEURG\n\r");
817 tcpopt
.nwto_flags
= NWTO_RCV_URG
;
818 if (ioctl(rem
, NWIOSTCPOPT
, &tcpopt
) == -1) {
820 "rlogin: trouble with urgent data: %s\n",
826 if (errno
== ENOURG
) {
827 nwio_tcpopt_t tcpopt
;
829 fprintf(stderr
, "\n\rENOURG\n\r");
831 tcpopt
.nwto_flags
= NWTO_RCV_NOTURG
;
832 if (ioctl(rem
, NWIOSTCPOPT
, &tcpopt
) == -1) {
834 "rlogin: trouble with not-urgent data: %s\n",
840 (void)fprintf(stderr
, "rlogin: read: %s\n",
846 #endif /* !__minix_vmd */
864 (void)tcsetattr(0, TCSAFLUSH
, sb
);
871 (void)signal(SIGPIPE
, SIG_IGN
);
872 msg("\007connection closed.");
880 (void)fprintf(stderr
, "rlogin: %s\r\n", str
);
891 (void)fprintf(stderr
, "rlogin: warning, using standard rlogin: ");
893 fmt
= va_arg(ap
, char *);
894 vfprintf(stderr
, fmt
, ap
);
896 (void)fprintf(stderr
, ".\n");
903 (void)fprintf(stderr
,
904 "Usage: rlogin [-%s]%s[-e char] [-l username] host\n",
907 "8ELx", " [-k realm] ");
909 "8EL", " [-k realm] ");
918 * The following routine provides compatibility (such as it is) between 4.2BSD
919 * Suns and others. Suns have only a `ttysize', so we convert it to a winsize.
922 get_window_size(fd
, wp
)
929 if ((error
= ioctl(0, TIOCGSIZE
, &ts
)) != 0)
931 wp
->ws_row
= ts
.ts_lines
;
932 wp
->ws_col
= ts
.ts_cols
;
946 if ((len
= strlen(p
)) == 1) /* use any single char, including '\' */
948 /* otherwise, \nnn */
949 if (*p
== '\\' && len
>= 2 && len
<= 4) {
950 val
= strtol(++p
, (char **)NULL
, 8);
954 if (*p
< '0' || *p
> '8')
958 msg("illegal option value -- e");
968 for (i
= 0; speeds
[i
].name
!= NULL
&& speeds
[i
].speed
!= speed
; i
++) {
969 if (speeds
[i
].speed
== speed
) return speeds
[i
].name
;
982 result
= fcntl(fd
, F_GETFD
);
986 "rlogin: mark_async: fcntl(%d, GETFD) failed (%s)\n",
987 fd
, strerror(errno
));
990 v
= result
| FD_ASYNCHIO
;
991 result
= fcntl(fd
, F_SETFD
, v
);
995 "rlogin: mark_async: fcntl(%d, SETFD, %d) failed (%s)\n",
996 fd
, v
, strerror(errno
));
1001 #define RD_0_BUFSIZE 256
1002 char rd_0_buf
[RD_0_BUFSIZE
];
1018 size_t extra_1_size
;
1019 size_t extra_1_offset
;
1021 size_t extra_1_new_size
;
1022 #define MAX_EXTRA_1_NEW_SIZE (16*1024)
1037 extra_1_new_size
= 0;
1040 #define RD_REM_BUFSIZE (8*1024)
1041 char rd_rem_buf
[RD_REM_BUFSIZE
];
1042 size_t rd_rem_offset
;
1049 inprogress_rd_rem
= 0;
1058 inprogress_wr_rem
= 0;
1061 extra_wr_rem_size
= 0;
1062 extra_wr_rem_offset
= 0;
1064 extra_wr_rem_new_size
= 0;
1065 extra_wr_rem_new
= NULL
;
1074 assert(!inprogress_0
);
1077 while (offset
< RD_0_BUFSIZE
)
1079 result
= read(0, rd_0_buf
+rd_0_offset
+offset
,
1080 RD_0_BUFSIZE
-rd_0_offset
-offset
);
1084 assert(rd_0_offset
+offset
<= RD_0_BUFSIZE
);
1090 completed_0(offset
, 0);
1091 rd_0_offset
+= offset
;
1092 if (result
== -1 && error
== EINPROGRESS
)
1097 completed_0(result
, error
);
1100 completed_0(offset
, 0);
1109 assert(!inprogress_1
);
1111 while (extra_1
!= NULL
|| extra_1_new
!= NULL
)
1113 if (extra_1
== NULL
)
1115 extra_1
= extra_1_new
;
1117 extra_1_size
= extra_1_new_size
;
1118 extra_1_new_size
= 0;
1123 if (extra_1_size
== 0)
1124 fprintf(stderr
, "restart_1: extra_1_size= 0\n");
1126 while (offset
< extra_1_size
)
1128 result
= write(1, extra_1
+extra_1_offset
+offset
,
1129 extra_1_size
-offset
);
1132 assert (result
<= extra_1_size
-offset
);
1138 completed_1(offset
, 0);
1140 if (result
== -1 && errno
== EINPROGRESS
)
1145 completed_1(result
, errno
);
1148 completed_1(offset
, 0);
1160 while (offset
< wr_1_size
)
1162 result
= write(1, rd_rem_buf
+wr_1_offset
+offset
,
1166 assert (result
<= wr_1_size
-offset
);
1172 completed_1(offset
, 0);
1174 if (result
== -1 && errno
== EINPROGRESS
)
1179 completed_1(result
, errno
);
1182 completed_1(offset
, 0);
1191 assert(!inprogress_rd_rem
);
1194 while (offset
< RD_REM_BUFSIZE
)
1196 result
= read(rem
, rd_rem_buf
+offset
, RD_REM_BUFSIZE
-offset
);
1200 assert(offset
<= RD_REM_BUFSIZE
);
1206 completed_rd_rem(offset
, 0);
1207 rd_rem_offset
= offset
;
1208 if (result
== -1 && error
== EINPROGRESS
)
1210 inprogress_rd_rem
= 1;
1213 completed_rd_rem(result
, error
);
1216 completed_rd_rem(offset
, 0);
1225 assert(!inprogress_wr_rem
);
1227 if (extra_wr_rem_new
!= NULL
&& extra_wr_rem
== NULL
)
1229 extra_wr_rem
= extra_wr_rem_new
;
1230 extra_wr_rem_size
= extra_wr_rem_new_size
;
1231 extra_wr_rem_offset
= 0;
1232 extra_wr_rem_new
= NULL
;
1233 extra_wr_rem_new_size
= 0;
1235 if (extra_wr_rem
!= NULL
)
1238 while (offset
< extra_wr_rem_size
)
1241 extra_wr_rem
+extra_wr_rem_offset
+offset
,
1242 extra_wr_rem_size
-offset
);
1245 assert (result
<= extra_wr_rem_size
-offset
);
1251 completed_wr_rem(offset
, 0);
1253 if (result
== -1 && errno
== EINPROGRESS
)
1255 inprogress_wr_rem
= 1;
1258 completed_wr_rem(result
, errno
);
1261 completed_wr_rem(offset
, 0);
1263 if (wr_rem_size
== 0)
1267 while (offset
< wr_rem_size
)
1269 result
= write(rem
, rd_0_buf
+wr_rem_offset
+offset
,
1270 wr_rem_size
-offset
);
1273 assert (result
<= wr_rem_size
-offset
);
1279 completed_wr_rem(offset
, 0);
1281 if (result
== -1 && errno
== EINPROGRESS
)
1283 inprogress_wr_rem
= 1;
1286 completed_wr_rem(result
, errno
);
1289 completed_wr_rem(offset
, 0);
1293 completed_0(result
, error
)
1297 static int bol
= 0, local
= 0;
1307 assert(rd_0_offset
> 0);
1310 iptr
= rd_0_buf
+rd_0_offset
;
1311 optr
= rd_0_buf
+wr_rem_offset
;
1312 for (i
= 0; i
<result
; iptr
++, i
++)
1318 if (!noescape
&& c
== escapechar
)
1327 if (c
== '.' || (c
!= _POSIX_VDISABLE
&&
1328 c
== defattr
.c_cc
[VEOF
]))
1339 if (c
!= escapechar
)
1343 *(optr
++)= escapechar
;
1347 assert(optr
== iptr
);
1348 assert(iptr
== rd_0_buf
+
1350 assert(rd_0_offset
> 0);
1352 optr
[-1]= escapechar
;
1357 bol
= (c
!= _POSIX_VDISABLE
) &&
1358 (c
== defattr
.c_cc
[VKILL
] ||
1359 c
== defattr
.c_cc
[VEOF
] ||
1360 c
== defattr
.c_cc
[VINTR
] ||
1361 c
== defattr
.c_cc
[VSUSP
] ||
1362 c
== '\r' || c
== '\n');
1364 wr_rem_size
+= optr
-rd_0_buf
-wr_rem_offset
;
1365 if (wr_rem_size
!= 0)
1373 fprintf(stderr
, "rlogin: %s\n", strerror(error
));
1379 completed_1(result
, error
)
1387 if (extra_1
!= NULL
)
1389 assert (result
<= extra_1_size
);
1390 extra_1_size
-= result
;
1391 extra_1_offset
+= result
;
1392 if (extra_1_size
== 0)
1401 assert (result
<= wr_1_size
);
1402 wr_1_size
-= result
;
1403 wr_1_offset
+= result
;
1412 fprintf(stderr
, "rlogin: %s\n", strerror(error
));
1418 completed_rd_rem(result
, error
)
1422 nwio_tcpopt_t tcpopt
;
1428 inprogress_rd_rem
= 0;
1435 fprintf(stderr
, "\n\r%d urg bytes\n\r", result
);
1437 if (urg_1_size
> MAX_EXTRA_1_NEW_SIZE
)
1439 keep_size
= MAX_EXTRA_1_NEW_SIZE
/2;
1440 memmove(urg_1
, urg_1
+urg_1_size
-keep_size
,
1442 urg_1_size
= keep_size
;
1444 new_buf
= realloc(urg_1
, urg_1_size
+result
);
1445 if (new_buf
== NULL
)
1448 "rlogin: warning realloc %d failed\n",
1452 memcpy(new_buf
+urg_1_size
,
1453 rd_rem_buf
+rd_rem_offset
, result
);
1455 urg_1_size
+= result
;
1461 wr_1_offset
= rd_rem_offset
;
1464 if (result
== -1 && error
== EURG
)
1467 fprintf(stderr
, "\n\rEURG\n\r");
1470 tcpopt
.nwto_flags
= NWTO_RCV_URG
;
1471 result
= ioctl(rem
, NWIOSTCPOPT
, &tcpopt
);
1475 "rlogin: NWIOSTCPOPT on %d failed (%s)\n",
1476 rem
, strerror(errno
));
1481 if (result
== -1 && error
== ENOURG
)
1484 fprintf(stderr
, "\n\rENOURG\n\r");
1487 tcpopt
.nwto_flags
= NWTO_RCV_NOTURG
;
1488 result
= ioctl(rem
, NWIOSTCPOPT
, &tcpopt
);
1492 "rlogin: NWIOSTCPOPT on %d failed (%s)\n",
1493 rem
, strerror(errno
));
1496 if (urg_1_size
!= 0)
1498 urg_byte
= urg_1
[urg_1_size
-1];
1501 if (urg_1_size
== 0)
1503 if (extra_1_new_size
+ urg_1_size
> MAX_EXTRA_1_NEW_SIZE
)
1505 extra_1_new_size
= 0;
1509 if (extra_1_new_size
!= 0)
1511 new_buf
= realloc(extra_1_new
,
1512 extra_1_new_size
+urg_1_size
);
1515 extra_1_new_size
= 0;
1521 extra_1_new
= new_buf
;
1522 memcpy(extra_1_new
+extra_1_new_size
,
1524 extra_1_new_size
+= urg_1_size
;
1530 if (extra_1_new_size
== 0)
1532 extra_1_new_size
= urg_1_size
;
1542 if (result
== -1 && error
== EINTR
)
1549 msg("connection closed.");
1553 fprintf(stderr
, "rlogin: %s\n", strerror(error
));
1559 completed_wr_rem(result
, error
)
1563 inprogress_wr_rem
= 0;
1567 if (extra_wr_rem
!= NULL
)
1569 assert (result
<= extra_wr_rem_size
);
1570 extra_wr_rem_size
-= result
;
1571 extra_wr_rem_offset
+= result
;
1572 if (extra_wr_rem_size
== 0)
1576 if (wr_rem_size
== 0)
1585 assert (result
<= wr_rem_size
);
1586 wr_rem_size
-= result
;
1587 wr_rem_offset
+= result
;
1588 if (wr_rem_size
== 0)
1596 fprintf(stderr
, "rlogin: %s\n", strerror(error
));
1606 fprintf(stderr
, "rlogin: warning got urg_byte 0x%x\r\n", urg_byte
);
1608 if (urg_byte
& TIOCPKT_WINDOW
)
1610 if (dosigwinch
== 0)
1613 signal(SIGWINCH
, sigwinch
);
1626 new_buf
= realloc(extra_1_new
, extra_1_new_size
+6);
1627 if (new_buf
== NULL
)
1629 extra_1_new
= new_buf
;
1630 new_buf
= extra_1_new
+extra_1_new_size
;
1636 *new_buf
++= c1
+ '@';
1638 else if (c1
== 0x7f)
1649 *new_buf
++= c
+ '@';
1661 extra_1_new_size
= new_buf
-extra_1_new
;
1671 static char cmdbuf
[256];
1676 /* Start a subshell. Based on the first character of the command,
1677 * the tcp connection will be present at fd 3 ('+'), or at
1678 * fd 0 and fd 1 ('=')
1680 int r
, pid
, stat
, len
;
1683 /* cancel the reads and writes that are in progress. */
1686 r
= fcancel(0, ASIO_READ
);
1687 if (r
!= 0) abort();
1691 r
= fcancel(1, ASIO_WRITE
);
1692 if (r
!= 0) abort();
1694 if (inprogress_rd_rem
)
1696 r
= fcancel(rem
, ASIO_READ
);
1697 if (r
!= 0) abort();
1699 if (inprogress_wr_rem
)
1701 r
= fcancel(rem
, ASIO_WRITE
);
1702 if (r
!= 0) abort();
1708 if (pid
== -1) abort();
1711 r
= waitpid(pid
, &stat
, 0);
1712 if (r
!= pid
) abort();
1715 fprintf(stderr
, "stat: 0x%x\n", stat
);
1721 (void)signal(SIGINT
, SIG_DFL
);
1723 shell
= getenv("SHELL");
1726 printf("~!\ncommand [%s]: ", shell
);
1727 cmd
= fgets(cmdbuf
, sizeof(cmdbuf
), stdin
);
1731 printf("got command '%s'\n", cmd
);
1734 /* Strip the trailing newline */
1736 if (len
> 0 && cmd
[len
-1] == '\n')
1741 /* Skip leading white space */
1742 while (*cmd
!= '\0' && isspace(*cmd
))
1754 else if (*cmd
== '=')
1765 r
= execl(shell
, shell
, NULL
);
1769 r
= execl("/bin/sh", "sh", "-c", cmd
, NULL
);
1771 printf("exec failed: %d, %d\n", r
, errno
);
1774 #endif /* __minix_vmd */