aboutsummaryrefslogtreecommitdiff
path: root/lib/init/init_socket_send_status.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <goliath@infraroot.at>2019-03-18 14:00:20 +0100
committerDavid Oberhollenzer <goliath@infraroot.at>2019-03-18 18:29:02 +0100
commit23b713c3b5345c6df187cd494bac4ef20669c1cf (patch)
treefa4c2eb792c5f6dee19e48eff01638d94dd49ef7 /lib/init/init_socket_send_status.c
parent08f72865b29598faa5074ef21f583508f94dea98 (diff)
Add functions to transmit service status accross initd socket
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
Diffstat (limited to 'lib/init/init_socket_send_status.c')
-rw-r--r--lib/init/init_socket_send_status.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/lib/init/init_socket_send_status.c b/lib/init/init_socket_send_status.c
new file mode 100644
index 0000000..ecad94c
--- /dev/null
+++ b/lib/init/init_socket_send_status.c
@@ -0,0 +1,65 @@
+/* SPDX-License-Identifier: ISC */
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "initsock.h"
+
+
+static int send_retry(int fd, const void *dst, size_t addrlen,
+ const void *buffer, size_t size)
+{
+ ssize_t ret;
+retry:
+ ret = sendto(fd, buffer, size, MSG_NOSIGNAL, dst, addrlen);
+ if (ret < 0 && errno == EINTR)
+ goto retry;
+
+ if (ret < 0 || (size_t)ret < size)
+ return -1;
+
+ return 0;
+}
+
+static int send_string(int fd, const void *dst, size_t addrlen,
+ const char *str)
+{
+ size_t len = strlen(str);
+ uint8_t raw_len[2];
+
+ raw_len[0] = (len >> 8) & 0xFF;
+ raw_len[1] = len & 0xFF;
+
+ if (send_retry(fd, dst, addrlen, raw_len, 2))
+ return -1;
+
+ return len > 0 ? send_retry(fd, dst, addrlen, str, len) : 0;
+}
+
+int init_socket_send_status(int fd, const void *dest_addr, size_t addrlen,
+ E_SERVICE_STATE state, service_t *svc)
+{
+ uint8_t info[2];
+
+ if (svc == NULL || state == ESS_NONE) {
+ info[0] = ESS_NONE;
+ info[1] = 0;
+ } else {
+ info[0] = state;
+ info[1] = svc->status & 0xFF;
+ }
+
+ if (send_retry(fd, dest_addr, addrlen, info, 2))
+ return -1;
+
+ if (svc != NULL && state != ESS_NONE) {
+ if (send_string(fd, dest_addr, addrlen, svc->fname))
+ return -1;
+ if (send_string(fd, dest_addr, addrlen, svc->name))
+ return -1;
+ }
+ return 0;
+}