+chan_mark_dead(Channel *c)
+{
+ c->type = SSH_CHANNEL_ZOMBIE;
+}
+
+int
+chan_is_dead(Channel *c, int send)
+{
+ if (c->type == SSH_CHANNEL_ZOMBIE) {
+ debug("channel %d: zombie", c->self);
+ return 1;
+ }
+ if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
+ return 0;
+ if (!compat20) {
+ debug("channel %d: is dead", c->self);
+ return 1;
+ }
+ /*
+ * we have to delay the close message if the efd (for stderr) is
+ * still active
+ */
+ if (((c->extended_usage != CHAN_EXTENDED_IGNORE) &&
+ buffer_len(&c->extended) > 0)
+#if 0
+ || ((c->extended_usage == CHAN_EXTENDED_READ) &&
+ c->efd != -1)
+#endif
+ ) {
+ debug2("channel %d: active efd: %d len %d type %s",
+ c->self, c->efd, buffer_len(&c->extended),
+ c->extended_usage==CHAN_EXTENDED_READ ?
+ "read": "write");
+ } else {
+ if (!(c->flags & CHAN_CLOSE_SENT)) {
+ if (send) {
+ chan_send_close2(c);
+ } else {
+ /* channel would be dead if we sent a close */
+ if (c->flags & CHAN_CLOSE_RCVD) {
+ debug("channel %d: almost dead",
+ c->self);
+ return 1;
+ }
+ }
+ }
+ if ((c->flags & CHAN_CLOSE_SENT) &&
+ (c->flags & CHAN_CLOSE_RCVD)) {
+ debug("channel %d: is dead", c->self);
+ return 1;
+ }
+ }
+ return 0;
+}
+
+void
+chan_init_iostates(Channel *c)
+{
+ c->ostate = CHAN_OUTPUT_OPEN;
+ c->istate = CHAN_INPUT_OPEN;
+ c->flags = 0;
+}
+
+/* init */
+void
+chan_init(void)
+{
+ if (compat20) {
+ chan_rcvd_oclose = chan_rcvd_oclose2;
+ chan_read_failed = chan_read_failed_12;
+ chan_ibuf_empty = chan_ibuf_empty2;
+
+ chan_rcvd_ieof = chan_rcvd_ieof2;
+ chan_write_failed = chan_write_failed2;
+ chan_obuf_empty = chan_obuf_empty2;
+ } else {
+ chan_rcvd_oclose = chan_rcvd_oclose1;
+ chan_read_failed = chan_read_failed_12;
+ chan_ibuf_empty = chan_ibuf_empty1;
+
+ chan_rcvd_ieof = chan_rcvd_ieof1;
+ chan_write_failed = chan_write_failed1;
+ chan_obuf_empty = chan_obuf_empty1;
+ }
+}
+
+/* helper */
+static void
+chan_shutdown_write(Channel *c)
+{
+ buffer_consume(&c->output, buffer_len(&c->output));
+ if (compat20 && c->type == SSH_CHANNEL_LARVAL)
+ return;
+ /* shutdown failure is allowed if write failed already */
+ debug("channel %d: close_write", c->self);
+ if (c->sock != -1) {
+ if (shutdown(c->sock, SHUT_WR) < 0)
+ debug("channel %d: chan_shutdown_write: "
+ "shutdown() failed for fd%d: %.100s",
+ c->self, c->sock, strerror(errno));
+ } else {
+ if (channel_close_fd(&c->wfd) < 0)
+ log("channel %d: chan_shutdown_write: "
+ "close() failed for fd%d: %.100s",
+ c->self, c->wfd, strerror(errno));
+ }
+}
+static void
+chan_shutdown_read(Channel *c)
+{
+ if (compat20 && c->type == SSH_CHANNEL_LARVAL)
+ return;
+ debug("channel %d: close_read", c->self);
+ if (c->sock != -1) {
+ /*
+ * shutdown(sock, SHUT_READ) may return ENOTCONN if the
+ * write side has been closed already. (bug on Linux)
+ * HP-UX may return ENOTCONN also.
+ */
+ if (shutdown(c->sock, SHUT_RD) < 0
+ && errno != ENOTCONN)
+ error("channel %d: chan_shutdown_read: "
+ "shutdown() failed for fd%d [i%d o%d]: %.100s",
+ c->self, c->sock, c->istate, c->ostate,
+ strerror(errno));
+ } else {
+ if (channel_close_fd(&c->rfd) < 0)
+ log("channel %d: chan_shutdown_read: "
+ "close() failed for fd%d: %.100s",
+ c->self, c->rfd, strerror(errno));
+ }