From: Sebastian Harl Date: Mon, 15 Dec 2014 09:39:05 +0000 (+0100) Subject: Moved sdb_proto_send/sdb_proto_select to sdb_write/sdb_select. X-Git-Tag: sysdb-0.7.0~108 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=e2258e6ee3c933351f81490bac576438ff973ae4;p=sysdb.git Moved sdb_proto_send/sdb_proto_select to sdb_write/sdb_select. These functions are not protocol-specific but rather simple wrappers around system calls. --- diff --git a/src/client/sock.c b/src/client/sock.c index eaa10ff..d4596cd 100644 --- a/src/client/sock.c +++ b/src/client/sock.c @@ -33,6 +33,7 @@ #include "utils/error.h" #include "utils/strbuf.h" #include "utils/proto.h" +#include "utils/os.h" #include @@ -255,7 +256,7 @@ sdb_client_send(sdb_client_t *client, if (sdb_proto_marshal(buf, sizeof(buf), cmd, msg_len, msg) < 0) return -1; - return sdb_proto_send(client->fd, sizeof(buf), buf); + return sdb_write(client->fd, sizeof(buf), buf); } /* sdb_client_send */ ssize_t @@ -281,7 +282,7 @@ sdb_client_recv(sdb_client_t *client, while (42) { ssize_t status; - if (sdb_proto_select(client->fd, SDB_PROTO_SELECTIN)) + if (sdb_select(client->fd, SDB_SELECTIN)) return -1; errno = 0; diff --git a/src/frontend/connection.c b/src/frontend/connection.c index cf610ac..58a2cb4 100644 --- a/src/frontend/connection.c +++ b/src/frontend/connection.c @@ -36,6 +36,7 @@ #include "utils/error.h" #include "utils/strbuf.h" #include "utils/proto.h" +#include "utils/os.h" #include #include @@ -510,7 +511,7 @@ sdb_connection_send(sdb_conn_t *conn, uint32_t code, if (sdb_proto_marshal(buf, sizeof(buf), code, msg_len, msg) < 0) return -1; - status = sdb_proto_send(conn->fd, sizeof(buf), buf); + status = sdb_write(conn->fd, sizeof(buf), buf); if (status < 0) { char errbuf[1024]; diff --git a/src/include/utils/os.h b/src/include/utils/os.h index 07328dd..38d53b2 100644 --- a/src/include/utils/os.h +++ b/src/include/utils/os.h @@ -69,6 +69,37 @@ sdb_remove_all(const char *pathname); char * sdb_get_current_user(void); +enum { + SDB_SELECTIN = 0, + SDB_SELECTOUT, + SDB_SELECTERR, +}; + +/* + * sdb_select: + * Wait for a file-descriptor to become ready for I/O operations of the + * specified type. This is a simple wrapper around the select() system call. + * The type argument may be any of the SDB_SELECT* constants. + * + * Returns: + * - the number of file descriptors ready for I/O + * - a negative value on error + */ +int +sdb_select(int fd, int type); + +/* + * sdb_write: + * Write a message to a file-descriptor. This is a simple wrapper around the + * write() system call ensuring that all data is written on success. + * + * Returns: + * - the number of bytes written + * - a negative value on error + */ +ssize_t +sdb_write(int fd, size_t msg_len, const void *msg); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/src/include/utils/proto.h b/src/include/utils/proto.h index 9d4748a..e762119 100644 --- a/src/include/utils/proto.h +++ b/src/include/utils/proto.h @@ -37,28 +37,6 @@ extern "C" { #endif -enum { - SDB_PROTO_SELECTIN = 0, - SDB_PROTO_SELECTOUT, - SDB_PROTO_SELECTERR, -}; - -/* - * sdb_proto_select: - * Wait for a file-descriptor to become ready for I/O operations of the - * specified type. This is a simple wrapper around the select() system call. - * The type argument may be any of the SDB_PROTO_SELECT* constants. - * - * Returns: - * - the number of file descriptors ready for I/O - * - a negative value on error - */ -int -sdb_proto_select(int fd, int type); - -ssize_t -sdb_proto_send(int fd, size_t msg_len, const char *msg); - /* * sdb_proto_marshal: * Encode the message into the wire format by adding an appropriate header. diff --git a/src/utils/os.c b/src/utils/os.c index e3f1fe2..747a370 100644 --- a/src/utils/os.c +++ b/src/utils/os.c @@ -166,5 +166,87 @@ sdb_get_current_user(void) return strdup(result->pw_name); } /* sdb_get_current_user */ +int +sdb_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_SELECTIN: + readfds = &fds; + break; + case SDB_SELECTOUT: + writefds = &fds; + break; + case SDB_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_select */ + +ssize_t +sdb_write(int fd, size_t msg_len, const void *msg) +{ + const char *buf; + size_t len; + + if ((fd < 0) || (msg_len && (! msg))) + return -1; + if (! msg_len) + return 0; + + buf = msg; + len = msg_len; + while (len > 0) { + ssize_t status; + + if (sdb_select(fd, SDB_SELECTOUT)) + return -1; + + errno = 0; + status = write(fd, buf, len); + if (status < 0) { + if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) + continue; + if (errno == EINTR) + continue; + + return status; + } + + len -= (size_t)status; + buf += status; + } + + return (ssize_t)msg_len; +} /* sdb_write */ + /* vim: set tw=78 sw=4 ts=4 noexpandtab : */ diff --git a/src/utils/proto.c b/src/utils/proto.c index 3315e0c..16007f2 100644 --- a/src/utils/proto.c +++ b/src/utils/proto.c @@ -42,88 +42,6 @@ * 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) -{ - const char *buf; - size_t len; - - if ((fd < 0) || (msg_len && (! msg))) - return -1; - if (! msg_len) - return 0; - - buf = msg; - len = msg_len; - while (len > 0) { - ssize_t status; - - if (sdb_proto_select(fd, SDB_PROTO_SELECTOUT)) - return -1; - - errno = 0; - status = write(fd, buf, len); - if (status < 0) { - if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) - continue; - if (errno == EINTR) - continue; - - return status; - } - - len -= (size_t)status; - buf += status; - } - - return (ssize_t)msg_len; -} /* sdb_proto_send */ - ssize_t sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code, uint32_t msg_len, const char *msg) diff --git a/t/unit/frontend/connection_test.c b/t/unit/frontend/connection_test.c index 6ce11ff..1e9b351 100644 --- a/t/unit/frontend/connection_test.c +++ b/t/unit/frontend/connection_test.c @@ -31,7 +31,6 @@ #include "frontend/connection.h" #include "frontend/connection-private.h" -#include "utils/proto.h" #include "utils/os.h" #include "libsysdb_test.h" @@ -371,9 +370,9 @@ START_TEST(test_conn_io) memcpy(buffer + offset, golden_data[i].msg, strlen(golden_data[i].msg)); - check = sdb_proto_send(conn->fd, msg_len, buffer); + check = sdb_write(conn->fd, msg_len, buffer); fail_unless(check == (ssize_t)msg_len, - "sdb_proto_send(%s) = %zi; expected: %zu", + "sdb_write(%s) = %zi; expected: %zu", check, msg_len); mock_conn_rewind(conn);