aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmdsserver
diff options
context:
space:
mode:
Diffstat (limited to 'src/libmdsserver')
-rw-r--r--src/libmdsserver/util.c39
-rw-r--r--src/libmdsserver/util.h13
2 files changed, 52 insertions, 0 deletions
diff --git a/src/libmdsserver/util.c b/src/libmdsserver/util.c
index 81793ae..b8b61d3 100644
--- a/src/libmdsserver/util.c
+++ b/src/libmdsserver/util.c
@@ -17,12 +17,15 @@
*/
#include "util.h"
#include "config.h"
+#include "macros.h"
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <signal.h>
+#include <sys/socket.h>
+#include <errno.h>
/**
@@ -106,3 +109,39 @@ int xsigaction(int signo, void (*function)(int signo))
return sigaction(signo, &action, NULL);
}
+
+/**
+ * Send a message over a socket
+ *
+ * @param socket The file descriptor of the socket
+ * @param message The message to send
+ * @param length The length of the message
+ * @return The number of bytes that have been sent (even on error)
+ */
+size_t send_message(int socket, const char* message, size_t length)
+{
+ size_t block_size = length;
+ size_t sent = 0;
+ ssize_t just_sent;
+
+ while (length > 0)
+ if ((just_sent = send(socket, message, min(block_size, length), MSG_NOSIGNAL)) < 0)
+ {
+ if (errno == EMSGSIZE)
+ {
+ block_size >>= 1;
+ if (block_size == 0)
+ return sent;
+ }
+ else if (errno != EINTR)
+ return sent;
+ }
+ else
+ {
+ message += (size_t)just_sent;
+ length -= (size_t)just_sent;
+ }
+
+ return sent;
+}
+
diff --git a/src/libmdsserver/util.h b/src/libmdsserver/util.h
index 8bfaa71..3a6e9b1 100644
--- a/src/libmdsserver/util.h
+++ b/src/libmdsserver/util.h
@@ -19,6 +19,9 @@
#define MDS_LIBMDSSERVER_UTIL_H
+#include <stddef.h>
+
+
/**
* Read an environment variable, but handle it as undefined if empty
*
@@ -50,6 +53,16 @@ void reexec_server(int argc, char** argv, int reexeced);
*/
int xsigaction(int signo, void (*function)(int signo));
+/**
+ * Send a message over a socket
+ *
+ * @param socket The file descriptor of the socket
+ * @param message The message to send
+ * @param length The length of the message
+ * @return The number of bytes that have been sent (even on error)
+ */
+size_t send_message(int socket, const char* message, size_t length);
+
#endif