Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 2 | /* |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 3 | * Copyright: Copyright (C) 2001, Hewlett-Packard Company |
| 4 | * Author: Christopher Hoover <ch@hpl.hp.com> |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Description: xmodem functionality for uploading of kernels |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 6 | * and the like |
| 7 | * Created at: Thu Dec 20 01:58:08 PST 2001 |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 8 | * |
| 9 | * xmodem functionality for uploading of kernels and the like |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2001 Hewlett-Packard Laboratories |
| 12 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 13 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 14 | * |
| 15 | * This was originally written for blob and then adapted for busybox. |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 16 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 17 | //config:config RX |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 18 | //config: bool "rx (2.9 kb)" |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 19 | //config: default y |
| 20 | //config: select PLATFORM_LINUX |
| 21 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 22 | //config: Receive files using the Xmodem protocol. |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 23 | |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 24 | //applet:IF_RX(APPLET(rx, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 25 | |
| 26 | //kbuild:lib-$(CONFIG_RX) += rx.o |
| 27 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 28 | //usage:#define rx_trivial_usage |
| 29 | //usage: "FILE" |
| 30 | //usage:#define rx_full_usage "\n\n" |
| 31 | //usage: "Receive a file using the xmodem protocol" |
| 32 | //usage: |
| 33 | //usage:#define rx_example_usage |
| 34 | //usage: "$ rx /tmp/foo\n" |
| 35 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 36 | #include "libbb.h" |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 37 | |
| 38 | #define SOH 0x01 |
| 39 | #define STX 0x02 |
| 40 | #define EOT 0x04 |
| 41 | #define ACK 0x06 |
| 42 | #define NAK 0x15 |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 43 | #define BS 0x08 |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 44 | #define PAD 0x1A |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 45 | |
| 46 | /* |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 47 | Cf: |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 48 | http://www.textfiles.com/apple/xmodem |
| 49 | http://www.phys.washington.edu/~belonis/xmodem/docxmodem.txt |
| 50 | http://www.phys.washington.edu/~belonis/xmodem/docymodem.txt |
| 51 | http://www.phys.washington.edu/~belonis/xmodem/modmprot.col |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 52 | */ |
| 53 | |
| 54 | #define TIMEOUT 1 |
| 55 | #define TIMEOUT_LONG 10 |
| 56 | #define MAXERRORS 10 |
| 57 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 58 | #define read_fd STDIN_FILENO |
| 59 | #define write_fd STDOUT_FILENO |
| 60 | |
| 61 | static int read_byte(unsigned timeout) |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 62 | { |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 63 | unsigned char buf; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 64 | int n; |
| 65 | |
| 66 | alarm(timeout); |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 67 | /* NOT safe_read! We want ALRM to interrupt us */ |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 68 | n = read(read_fd, &buf, 1); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 69 | alarm(0); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 70 | if (n == 1) |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 71 | return buf; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 72 | return -1; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 75 | static int receive(/*int read_fd, */int file_fd) |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 76 | { |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 77 | unsigned char blockBuf[1024]; |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 78 | unsigned blockLength = 0; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 79 | unsigned errors = 0; |
| 80 | unsigned wantBlockNo = 1; |
| 81 | unsigned length = 0; |
| 82 | int do_crc = 1; |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 83 | char reply_char; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 84 | unsigned timeout = TIMEOUT_LONG; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 85 | |
Rob Landley | 399d45f | 2006-06-21 01:49:17 +0000 | [diff] [blame] | 86 | /* Flush pending input */ |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 87 | tcflush(read_fd, TCIFLUSH); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 88 | |
| 89 | /* Ask for CRC; if we get errors, we will go with checksum */ |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 90 | reply_char = 'C'; |
| 91 | full_write(write_fd, &reply_char, 1); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 92 | |
| 93 | for (;;) { |
| 94 | int blockBegin; |
| 95 | int blockNo, blockNoOnesCompl; |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 96 | int cksum_or_crc; |
Rostislav Skudnov | 8762512 | 2017-02-01 18:35:13 +0000 | [diff] [blame] | 97 | unsigned expected; |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 98 | int i, j; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 99 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 100 | blockBegin = read_byte(timeout); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 101 | if (blockBegin < 0) |
| 102 | goto timeout; |
| 103 | |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 104 | /* If last block, remove padding */ |
| 105 | if (blockBegin == EOT) { |
| 106 | /* Data blocks can be padded with ^Z characters */ |
| 107 | /* This code tries to detect and remove them */ |
| 108 | if (blockLength >= 3 |
| 109 | && blockBuf[blockLength - 1] == PAD |
| 110 | && blockBuf[blockLength - 2] == PAD |
| 111 | && blockBuf[blockLength - 3] == PAD |
| 112 | ) { |
| 113 | while (blockLength |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 114 | && blockBuf[blockLength - 1] == PAD |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 115 | ) { |
| 116 | blockLength--; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | /* Write previously received block */ |
Denys Vlasenko | f3efd3c | 2011-11-03 10:19:53 +0100 | [diff] [blame] | 121 | errno = 0; |
| 122 | if (full_write(file_fd, blockBuf, blockLength) != blockLength) { |
| 123 | bb_perror_msg(bb_msg_write_error); |
| 124 | goto fatal; |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 127 | timeout = TIMEOUT; |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 128 | reply_char = NAK; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 129 | |
| 130 | switch (blockBegin) { |
| 131 | case SOH: |
| 132 | case STX: |
| 133 | break; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 134 | case EOT: |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 135 | reply_char = ACK; |
| 136 | full_write(write_fd, &reply_char, 1); |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 137 | return length; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 138 | default: |
| 139 | goto error; |
| 140 | } |
| 141 | |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 142 | /* Block no */ |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 143 | blockNo = read_byte(TIMEOUT); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 144 | if (blockNo < 0) |
| 145 | goto timeout; |
| 146 | |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 147 | /* Block no, in one's complement form */ |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 148 | blockNoOnesCompl = read_byte(TIMEOUT); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 149 | if (blockNoOnesCompl < 0) |
| 150 | goto timeout; |
| 151 | |
| 152 | if (blockNo != (255 - blockNoOnesCompl)) { |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 153 | bb_error_msg("bad block ones compl"); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 154 | goto error; |
| 155 | } |
| 156 | |
| 157 | blockLength = (blockBegin == SOH) ? 128 : 1024; |
| 158 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 159 | for (i = 0; i < blockLength; i++) { |
| 160 | int cc = read_byte(TIMEOUT); |
| 161 | if (cc < 0) |
| 162 | goto timeout; |
| 163 | blockBuf[i] = cc; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Denys Vlasenko | f3efd3c | 2011-11-03 10:19:53 +0100 | [diff] [blame] | 166 | cksum_or_crc = read_byte(TIMEOUT); |
| 167 | if (cksum_or_crc < 0) |
| 168 | goto timeout; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 169 | if (do_crc) { |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 170 | cksum_or_crc = (cksum_or_crc << 8) | read_byte(TIMEOUT); |
| 171 | if (cksum_or_crc < 0) |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 172 | goto timeout; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | if (blockNo == ((wantBlockNo - 1) & 0xff)) { |
| 176 | /* a repeat of the last block is ok, just ignore it. */ |
| 177 | /* this also ignores the initial block 0 which is */ |
| 178 | /* meta data. */ |
Dan Fandrich | f808e77 | 2011-11-03 10:18:33 +0100 | [diff] [blame] | 179 | blockLength = 0; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 180 | goto next; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 181 | } |
| 182 | if (blockNo != (wantBlockNo & 0xff)) { |
| 183 | bb_error_msg("unexpected block no, 0x%08x, expecting 0x%08x", blockNo, wantBlockNo); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 184 | goto error; |
| 185 | } |
| 186 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 187 | expected = 0; |
| 188 | if (do_crc) { |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 189 | for (i = 0; i < blockLength; i++) { |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 190 | expected = expected ^ blockBuf[i] << 8; |
| 191 | for (j = 0; j < 8; j++) { |
| 192 | if (expected & 0x8000) |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 193 | expected = (expected << 1) ^ 0x1021; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 194 | else |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 195 | expected = (expected << 1); |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 196 | } |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 197 | } |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 198 | expected &= 0xffff; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 199 | } else { |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 200 | for (i = 0; i < blockLength; i++) |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 201 | expected += blockBuf[i]; |
| 202 | expected &= 0xff; |
| 203 | } |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 204 | if (cksum_or_crc != expected) { |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 205 | bb_error_msg(do_crc ? "crc error, expected 0x%04x, got 0x%04x" |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 206 | : "checksum error, expected 0x%02x, got 0x%02x", |
| 207 | expected, cksum_or_crc); |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 208 | goto error; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | wantBlockNo++; |
| 212 | length += blockLength; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 213 | next: |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 214 | errors = 0; |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 215 | reply_char = ACK; |
| 216 | full_write(write_fd, &reply_char, 1); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 217 | continue; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 218 | error: |
| 219 | timeout: |
Daniel Fandrich | 65a1ee9 | 2011-11-23 12:07:31 +0100 | [diff] [blame] | 220 | blockLength = 0; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 221 | errors++; |
| 222 | if (errors == MAXERRORS) { |
| 223 | /* Abort */ |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 224 | |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 225 | /* If were asking for crc, try again w/o crc */ |
| 226 | if (reply_char == 'C') { |
| 227 | reply_char = NAK; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 228 | errors = 0; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 229 | do_crc = 0; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 230 | goto timeout; |
| 231 | } |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 232 | bb_error_msg("too many errors; giving up"); |
| 233 | fatal: |
| 234 | /* 5 CAN followed by 5 BS. Don't try too hard... */ |
| 235 | safe_write(write_fd, "\030\030\030\030\030\010\010\010\010\010", 10); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 236 | return -1; |
| 237 | } |
| 238 | |
Rob Landley | 399d45f | 2006-06-21 01:49:17 +0000 | [diff] [blame] | 239 | /* Flush pending input */ |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 240 | tcflush(read_fd, TCIFLUSH); |
Rob Landley | 399d45f | 2006-06-21 01:49:17 +0000 | [diff] [blame] | 241 | |
Denys Vlasenko | dc9495d | 2009-08-02 19:45:31 +0200 | [diff] [blame] | 242 | full_write(write_fd, &reply_char, 1); |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 243 | } /* for (;;) */ |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 246 | static void sigalrm_handler(int UNUSED_PARAM signum) |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 247 | { |
| 248 | } |
| 249 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 250 | int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 251 | int rx_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 252 | { |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 253 | struct termios tty, orig_tty; |
| 254 | int termios_err; |
| 255 | int file_fd; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 256 | int n; |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 257 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 258 | /* Disabled by vda: |
| 259 | * why we can't receive from stdin? Why we *require* |
| 260 | * controlling tty?? */ |
| 261 | /*read_fd = xopen(CURRENT_TTY, O_RDWR);*/ |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 262 | file_fd = xopen(single_argv(argv), O_RDWR|O_CREAT|O_TRUNC); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 263 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 264 | termios_err = tcgetattr(read_fd, &tty); |
| 265 | if (termios_err == 0) { |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 266 | //TODO: use set_termios_to_raw() |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 267 | orig_tty = tty; |
| 268 | cfmakeraw(&tty); |
| 269 | tcsetattr(read_fd, TCSAFLUSH, &tty); |
| 270 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 271 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 272 | /* No SA_RESTART: we want ALRM to interrupt read() */ |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 273 | signal_no_SA_RESTART_empty_mask(SIGALRM, sigalrm_handler); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 274 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 275 | n = receive(file_fd); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 276 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 277 | if (termios_err == 0) |
| 278 | tcsetattr(read_fd, TCSAFLUSH, &orig_tty); |
| 279 | if (ENABLE_FEATURE_CLEAN_UP) |
| 280 | close(file_fd); |
| 281 | fflush_stdout_and_exit(n >= 0); |
Glenn L McGrath | 8f3bc4c | 2003-12-20 07:30:35 +0000 | [diff] [blame] | 282 | } |