| 150 | } |
| 151 | |
| 152 | static int |
| 153 | tty_drain(struct tty *tp, int leaving) |
| 154 | { |
| 155 | sbintime_t timeout_at; |
| 156 | size_t bytes; |
| 157 | int error; |
| 158 | |
| 159 | if (ttyhook_hashook(tp, getc_inject)) |
| 160 | /* buffer is inaccessible */ |
| 161 | return (0); |
| 162 | |
| 163 | /* |
| 164 | * For close(), use the recent historic timeout of "1 second without |
| 165 | * making progress". For tcdrain(), use t_drainwait as the timeout, |
| 166 | * with zero meaning "no timeout" which gives POSIX behavior. |
| 167 | */ |
| 168 | if (leaving) |
| 169 | timeout_at = getsbinuptime() + SBT_1S; |
| 170 | else if (tp->t_drainwait != 0) |
| 171 | timeout_at = getsbinuptime() + SBT_1S * tp->t_drainwait; |
| 172 | else |
| 173 | timeout_at = 0; |
| 174 | |
| 175 | /* |
| 176 | * Poll the output buffer and the hardware for completion, at 10 Hz. |
| 177 | * Polling is required for devices which are not able to signal an |
| 178 | * interrupt when the transmitter becomes idle (most USB serial devs). |
| 179 | * The unusual structure of this loop ensures we check for busy one more |
| 180 | * time after tty_timedwait() returns EWOULDBLOCK, so that success has |
| 181 | * higher priority than timeout if the IO completed in the last 100mS. |
| 182 | */ |
| 183 | error = 0; |
| 184 | bytes = ttyoutq_bytesused(&tp->t_outq); |
| 185 | for (;;) { |
| 186 | if (ttyoutq_bytesused(&tp->t_outq) == 0 && !ttydevsw_busy(tp)) |
| 187 | return (0); |
| 188 | if (error != 0) |
| 189 | return (error); |
| 190 | ttydevsw_outwakeup(tp); |
| 191 | error = tty_timedwait(tp, &tp->t_outwait, hz / 10); |
| 192 | if (error != 0 && error != EWOULDBLOCK) |
| 193 | return (error); |
| 194 | else if (timeout_at == 0 || getsbinuptime() < timeout_at) |
| 195 | error = 0; |
| 196 | else if (leaving && ttyoutq_bytesused(&tp->t_outq) < bytes) { |
| 197 | /* In close, making progress, grant an extra second. */ |
| 198 | error = 0; |
| 199 | timeout_at += SBT_1S; |
| 200 | bytes = ttyoutq_bytesused(&tp->t_outq); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Though ttydev_enter() and ttydev_leave() seem to be related, they |
no test coverage detected