Code

Moved sdb_proto_send/sdb_proto_select to sdb_write/sdb_select.
authorSebastian Harl <sh@tokkee.org>
Mon, 15 Dec 2014 09:39:05 +0000 (10:39 +0100)
committerSebastian Harl <sh@tokkee.org>
Mon, 15 Dec 2014 09:39:05 +0000 (10:39 +0100)
These functions are not protocol-specific but rather simple wrappers around
system calls.

src/client/sock.c
src/frontend/connection.c
src/include/utils/os.h
src/include/utils/proto.h
src/utils/os.c
src/utils/proto.c
t/unit/frontend/connection_test.c

index eaa10ffdb95307a31c0233d10d46b68f33d7f912..d4596cd7a858cda76e1144492c5e2cf71c11291b 100644 (file)
@@ -33,6 +33,7 @@
 #include "utils/error.h"
 #include "utils/strbuf.h"
 #include "utils/proto.h"
+#include "utils/os.h"
 
 #include <arpa/inet.h>
 
@@ -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;
index cf610ac45a06951a82c00644a943ef43d5da8ced..58a2cb4d3e3d06d510927b57b414f37bc3af425c 100644 (file)
@@ -36,6 +36,7 @@
 #include "utils/error.h"
 #include "utils/strbuf.h"
 #include "utils/proto.h"
+#include "utils/os.h"
 
 #include <assert.h>
 #include <errno.h>
@@ -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];
 
index 07328dd11a2b0d9aaf96c82c0f920e1d88082070..38d53b2a8bb1281aa13987403ea60f96b3375a6d 100644 (file)
@@ -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
index 9d4748a43ce879a662c38624ffd01622b61e7ad3..e76211965a416d4e06d6fa93bdfbcc3e1d32b2e9 100644 (file)
 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.
index e3f1fe2d42bb61d072940949a7ddee345836d4f6..747a3700014bd248f19230bfd0363ccba55437ee 100644 (file)
@@ -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 : */
 
index 3315e0c7878056393df7a581b558f4a70df8e417..16007f2e5bd48d1ebe9a6d2958cdf8624c52f4fb 100644 (file)
  * 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)
index 6ce11ff511a97a30f67f9d482d1368806ff21a94..1e9b351679e2b17533fd82d43fe8f46dee01cb3f 100644 (file)
@@ -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);