From: Davin McCall Date: Sun, 29 Dec 2019 04:17:09 +0000 (+1000) Subject: Use bp_sys aliases to write from log X-Git-Tag: v0.8.1~9 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=8fc3058147d2a0efc32d40ca4a738b26fd246b4f;p=oweals%2Fdinit.git Use bp_sys aliases to write from log This allows for future unit testing of the log infrastructure. --- diff --git a/src/dinit-log.cc b/src/dinit-log.cc index 9d86913..e0ee69a 100644 --- a/src/dinit-log.cc +++ b/src/dinit-log.cc @@ -169,7 +169,7 @@ rearm buffered_log_stream::fd_event(eventloop_t &loop, int fd, int flags) noexce const char * start = special_buf + msg_index; const char * end = start; while (*end != '\n') end++; - int r = write(fd, start, end - start + 1); + int r = bp_sys::write(fd, start, end - start + 1); if (r >= 0) { if (start + r > end) { // All written: go on to next message in queue @@ -239,7 +239,7 @@ rearm buffered_log_stream::fd_event(eventloop_t &loop, int fd, int flags) noexce iovs_to_write = 2; } - ssize_t r = writev(fd, logiov, iovs_to_write); + ssize_t r = bp_sys::writev(fd, logiov, iovs_to_write); if (r >= 0) { bool complete = (r == len) && will_complete; diff --git a/src/includes/baseproc-sys.h b/src/includes/baseproc-sys.h index 4c65513..fcf5056 100644 --- a/src/includes/baseproc-sys.h +++ b/src/includes/baseproc-sys.h @@ -8,7 +8,11 @@ #ifndef BPSYS_INCLUDED #define BPSYS_INCLUDED -#include "dasynq.h" +#include "dasynq.h" // for pipe2 + +#include // writev +#include +#include namespace bp_sys { @@ -22,6 +26,7 @@ using ::tcsetpgrp; using ::getpgrp; using ::read; using ::write; +using ::writev; // Wrapper around a POSIX exit status class exit_status diff --git a/src/tests/test-bpsys.cc b/src/tests/test-bpsys.cc index 9f9ada2..21e9b46 100644 --- a/src/tests/test-bpsys.cc +++ b/src/tests/test-bpsys.cc @@ -150,4 +150,23 @@ ssize_t write(int fd, const void *buf, size_t count) return count; } +ssize_t writev(int fd, const struct iovec *iov, int iovcnt) +{ + ssize_t r = 0; + for (int i = 0; i < iovcnt; i++) { + ssize_t wr = write(fd, iov[i].iov_base, iov[i].iov_len); + if (wr < 0) { + if (r > 0) { + return r; + } + return wr; + } + r += wr; + if (size_t(wr) < iov[i].iov_len) { + return r; + } + } + return r; +} + } diff --git a/src/tests/test-includes/baseproc-sys.h b/src/tests/test-includes/baseproc-sys.h index 78e3408..c8e2441 100644 --- a/src/tests/test-includes/baseproc-sys.h +++ b/src/tests/test-includes/baseproc-sys.h @@ -6,6 +6,7 @@ #include #include +#include // Mock system functions for testing. @@ -108,6 +109,7 @@ inline pid_t waitpid(pid_t p, exit_status *statusp, int flags) ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, const void *buf, size_t count); +ssize_t writev (int fd, const struct iovec *iovec, int count); }