summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: d81bfcc)
raw | patch | inline | side by side (parent: d81bfcc)
author | Sebastian Harl <sh@tokkee.org> | |
Mon, 15 Dec 2014 09:39:05 +0000 (10:39 +0100) | ||
committer | Sebastian 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.
system calls.
diff --git a/src/client/sock.c b/src/client/sock.c
index eaa10ffdb95307a31c0233d10d46b68f33d7f912..d4596cd7a858cda76e1144492c5e2cf71c11291b 100644 (file)
--- a/src/client/sock.c
+++ b/src/client/sock.c
#include "utils/error.h"
#include "utils/strbuf.h"
#include "utils/proto.h"
+#include "utils/os.h"
#include <arpa/inet.h>
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
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)
#include "utils/error.h"
#include "utils/strbuf.h"
#include "utils/proto.h"
+#include "utils/os.h"
#include <assert.h>
#include <errno.h>
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 07328dd11a2b0d9aaf96c82c0f920e1d88082070..38d53b2a8bb1281aa13987403ea60f96b3375a6d 100644 (file)
--- a/src/include/utils/os.h
+++ b/src/include/utils/os.h
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.
diff --git a/src/utils/os.c b/src/utils/os.c
index e3f1fe2d42bb61d072940949a7ddee345836d4f6..747a3700014bd248f19230bfd0363ccba55437ee 100644 (file)
--- a/src/utils/os.c
+++ b/src/utils/os.c
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 3315e0c7878056393df7a581b558f4a70df8e417..16007f2e5bd48d1ebe9a6d2958cdf8624c52f4fb 100644 (file)
--- a/src/utils/proto.c
+++ b/src/utils/proto.c
* 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)
#include "frontend/connection.h"
#include "frontend/connection-private.h"
-#include "utils/proto.h"
#include "utils/os.h"
#include "libsysdb_test.h"
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);