/* * Free the SockHandler structure */ void a_Dpip_dsh_free(Dsh *dsh) { dReturn_if (dsh == NULL); dStr_free(dsh->wrbuf, 1); dStr_free(dsh->rdbuf, 1); dFree(dsh); }
static void Dpi_close_fd(int fd) { int st; dReturn_if (fd < 0); do st = close(fd); while (st < 0 && errno == EINTR); }
/* * Read raw data from the socket into our buffer in * either BLOCKING or NONBLOCKING mode. */ static void Dpip_dsh_read(Dsh *dsh, int blocking) { char buf[RBUF_SZ]; int req_mode, old_flags = 0, st, ret = -3, nb = !blocking; dReturn_if (dsh->status == DPIP_ERROR || dsh->status == DPIP_EOF); req_mode = (nb) ? DPIP_NONBLOCK : 0; if ((dsh->mode & DPIP_NONBLOCK) != req_mode) { /* change mode temporarily... */ old_flags = fcntl(dsh->fd_in, F_GETFL); fcntl(dsh->fd_in, F_SETFL, (nb) ? O_NONBLOCK | old_flags : old_flags & ~O_NONBLOCK); } while (1) { st = read(dsh->fd_in, buf, RBUF_SZ); if (st < 0) { if (errno == EINTR) { continue; } else if (errno == EAGAIN) { dsh->status = DPIP_EAGAIN; ret = -1; break; } else { MSG_ERR("[Dpip_dsh_read] %s\n", dStrerror(errno)); dsh->status = DPIP_ERROR; break; } } else if (st == 0) { dsh->status = DPIP_EOF; break; } else { /* append to buf */ dStr_append_l(dsh->rdbuf, buf, st); if (blocking) break; } } if ((dsh->mode & DPIP_NONBLOCK) != req_mode) { /* restore old mode */ fcntl(dsh->fd_out, F_SETFL, old_flags); } /* assert there's no more data in the wire... * (st < buf upon interrupt || st == buf and no more data) */ if (blocking) Dpip_dsh_read(dsh, 0); }