* Checks a socket, using poll or select, for data to be read, written, * or both. Returns >0 if one or more conditions are met, 0 if it timed * out, -1 if an error occurred. * * If SSL is in use, the SSL buffer is checked prior to checking the socket * for read data directly. */
| 1152 | * for read data directly. |
| 1153 | */ |
| 1154 | static int |
| 1155 | pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time) |
| 1156 | { |
| 1157 | int result; |
| 1158 | |
| 1159 | if (!conn) |
| 1160 | return -1; |
| 1161 | if (conn->sock == PGINVALID_SOCKET) |
| 1162 | { |
| 1163 | appendPQExpBufferStr(&conn->errorMessage, |
| 1164 | libpq_gettext("invalid socket\n")); |
| 1165 | return -1; |
| 1166 | } |
| 1167 | |
| 1168 | #ifdef USE_SSL |
| 1169 | /* Check for SSL library buffering read bytes */ |
| 1170 | if (forRead && conn->ssl_in_use && pgtls_read_pending(conn)) |
| 1171 | { |
| 1172 | /* short-circuit the select */ |
| 1173 | return 1; |
| 1174 | } |
| 1175 | #endif |
| 1176 | |
| 1177 | /* We will retry as long as we get EINTR */ |
| 1178 | do |
| 1179 | result = pqSocketPoll(conn->sock, forRead, forWrite, end_time); |
| 1180 | while (result < 0 && SOCK_ERRNO == EINTR); |
| 1181 | |
| 1182 | if (result < 0) |
| 1183 | { |
| 1184 | char sebuf[PG_STRERROR_R_BUFLEN]; |
| 1185 | |
| 1186 | appendPQExpBuffer(&conn->errorMessage, |
| 1187 | libpq_gettext("%s() failed: %s\n"), |
| 1188 | "select", |
| 1189 | SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); |
| 1190 | } |
| 1191 | |
| 1192 | return result; |
| 1193 | } |
| 1194 | |
| 1195 | |
| 1196 | /* |
no test coverage detected