From: Davin McCall Date: Thu, 7 Jan 2016 22:02:05 +0000 (+0000) Subject: Make sure to not write a partial command packet. X-Git-Tag: v0.01~47 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=212adde075dfa25fcbab41dd0935a5d701c65cc7;p=oweals%2Fdinit.git Make sure to not write a partial command packet. --- diff --git a/src/dinitctl.cc b/src/dinitctl.cc index 1d1a553..f307660 100644 --- a/src/dinitctl.cc +++ b/src/dinitctl.cc @@ -50,6 +50,25 @@ static const char * describeVerb(bool stop) return stop ? "stop" : "start"; } +// Write *all* the requested buffer and re-try if necessary until +// the buffer is written or an unrecoverable error occurs. +static int write_all(int fd, const void *buf, size_t count) +{ + const char *cbuf = static_cast(buf); + int w = 0; + while (count > 0) { + int r = write(fd, cbuf, count); + if (r == -1) { + if (errno == EINTR) continue; + return r; + } + w += r; + cbuf += r; + count -= r; + } + return w; +} + int main(int argc, char **argv) { using namespace std; @@ -182,8 +201,7 @@ int main(int argc, char **argv) memcpy(buf + 1, &sname_len, 2); memcpy(buf + 3, service_name, sname_len); - int r = write(socknum, buf, bufsize); - // TODO make sure we write it all + int r = write_all(socknum, buf, bufsize); delete [] buf; if (r == -1) { perror("write");