X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Futils%2Fproto.c;h=26c4826a7f949a003d67932bd283cc59eff2befd;hb=033aa984a8b4be78ced113a3715422841a48d9a8;hp=e9e3e6c7ca9984396a2c55bb4b299cd3be34ef4a;hpb=bf37a5afbf7364f853140c6859822ae52d2b61d8;p=sysdb.git diff --git a/src/utils/proto.c b/src/utils/proto.c index e9e3e6c..26c4826 100644 --- a/src/utils/proto.c +++ b/src/utils/proto.c @@ -25,8 +25,8 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "utils/error.h" #include "utils/proto.h" -#include "core/error.h" #include #include @@ -36,10 +36,57 @@ #include #include +#include + /* * public API */ +int +sdb_proto_select(int fd, int type) +{ + fd_set fds; + fd_set *readfds = NULL; + fd_set *writefds = NULL; + fd_set *exceptfds = NULL; + + if (fd < 0) { + errno = EBADF; + return -1; + } + + FD_ZERO(&fds); + + switch (type) { + case SDB_PROTO_SELECTIN: + readfds = &fds; + break; + case SDB_PROTO_SELECTOUT: + writefds = &fds; + break; + case SDB_PROTO_SELECTERR: + exceptfds = &fds; + break; + default: + errno = EINVAL; + return -1; + } + + FD_SET(fd, &fds); + + while (42) { + int n; + errno = 0; + n = select(fd + 1, readfds, writefds, exceptfds, NULL); + + if ((n < 0) && (errno != EINTR)) + return n; + if (n > 0) + break; + } + return 0; +} /* sdb_proto_select */ + ssize_t sdb_proto_send(int fd, size_t msg_len, const char *msg) { @@ -56,7 +103,8 @@ sdb_proto_send(int fd, size_t msg_len, const char *msg) while (len > 0) { ssize_t status; - /* XXX: use select() */ + if (sdb_proto_select(fd, SDB_PROTO_SELECTOUT)) + return -1; errno = 0; status = write(fd, buf, len); @@ -106,7 +154,7 @@ sdb_proto_get_int(sdb_strbuf_t *buf, size_t offset) return UINT32_MAX; /* not enough data to read */ - if (offset + sizeof(uint32_t) < sdb_strbuf_len(buf)) + if (offset + sizeof(uint32_t) > sdb_strbuf_len(buf)) return UINT32_MAX; data = sdb_strbuf_string(buf);