From a43e69c7ae0e5f45f9cb6e2be1b75ecd580724e7 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Sat, 13 Nov 2010 08:18:23 -0500 Subject: [PATCH] Factor out safe_write() into ncmlib and use it in ndhc as well. --- ifchd/ifchd.c | 27 +++++++----------------- ncmlib/CMakeLists.txt | 1 + ncmlib/io.c | 49 +++++++++++++++++++++++++++++++++++++++++++ ncmlib/io.h | 35 +++++++++++++++++++++++++++++++ ndhc/packet.c | 18 ++++------------ ndhc/script.c | 20 +++++------------- 6 files changed, 102 insertions(+), 48 deletions(-) create mode 100644 ncmlib/io.c create mode 100644 ncmlib/io.h diff --git a/ifchd/ifchd.c b/ifchd/ifchd.c index 4029a53..3269282 100644 --- a/ifchd/ifchd.c +++ b/ifchd/ifchd.c @@ -1,5 +1,5 @@ /* ifchd.c - interface change daemon - * Time-stamp: <2010-11-12 18:40:54 njk> + * Time-stamp: <2010-11-13 08:07:54 njk> * * (C) 2004-2010 Nicholas J. Kain * @@ -49,6 +49,7 @@ #include "ifproto.h" #include "strl.h" #include "cap.h" +#include "io.h" #include "linux.h" enum states { @@ -126,22 +127,10 @@ static void die_nulstr(strlist_t *p) suicide("FATAL - NULL string in strlist"); } -static void safe_write(int fd, const char *buf, int len) +static void writeordie(int fd, const char *buf, int len) { - ssize_t r; - retry: - r = write(fd, buf, len); - if (r != len) { - if (r == -1) { - if (errno == EINTR) - goto retry; - else - suicide("write returned error"); - } else { - len -= r; - goto retry; - } - } + if (safe_write(fd, buf, len) == -1) + suicide("write returned error"); } /* Writes out each element in a strlist as an argument to a keyword in @@ -159,9 +148,9 @@ static void write_resolve_list(const char *keyword, strlist_t *list) buf = p->str; len = strlen(buf); if (len) { - safe_write(resolv_conf_fd, keyword, strlen(keyword)); - safe_write(resolv_conf_fd, buf, strlen(buf)); - safe_write(resolv_conf_fd, "\n", 1); + writeordie(resolv_conf_fd, keyword, strlen(keyword)); + writeordie(resolv_conf_fd, buf, strlen(buf)); + writeordie(resolv_conf_fd, "\n", 1); } p = p->next; } diff --git a/ncmlib/CMakeLists.txt b/ncmlib/CMakeLists.txt index 6afd004..c152341 100644 --- a/ncmlib/CMakeLists.txt +++ b/ncmlib/CMakeLists.txt @@ -7,6 +7,7 @@ set(NCMLIB_SRCS strl.c log.c cap.c + io.c ) add_library(ncmlib ${NCMLIB_SRCS}) \ No newline at end of file diff --git a/ncmlib/io.c b/ncmlib/io.c new file mode 100644 index 0000000..97cf8a7 --- /dev/null +++ b/ncmlib/io.c @@ -0,0 +1,49 @@ +/* io.c - light wrappers for POSIX i/o functions + * Time-stamp: <2010-11-13 08:08:39 njk> + * + * (c) 2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +// returns -1 on error, >= 0 on success +int safe_write(int fd, const char *buf, int len) +{ + int r; + int s = 0; + while (s < len) { + r = write(fd, buf + s, len - s); + if (r == -1) { + if (errno == EINTR) + continue; + else + return -1; + } + s += r; + } + return s; +} diff --git a/ncmlib/io.h b/ncmlib/io.h new file mode 100644 index 0000000..719dce9 --- /dev/null +++ b/ncmlib/io.h @@ -0,0 +1,35 @@ +/* io.h - light wrappers for POSIX i/o functions + * Time-stamp: <2010-11-13 08:07:43 njk> + * + * (c) 2010 Nicholas J. Kain + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef NCM_IO_H_ +#define NCM_IO_H_ + +int safe_write(int fd, const char *buf, int len); + +#endif /* NCM_IO_H_ */ diff --git a/ndhc/packet.c b/ndhc/packet.c index 3e678d0..1fc5763 100644 --- a/ndhc/packet.c +++ b/ndhc/packet.c @@ -10,6 +10,7 @@ #include "packet.h" #include "log.h" +#include "io.h" #include "dhcpd.h" #include "options.h" @@ -199,20 +200,9 @@ int kernel_packet(struct dhcpMessage *payload, uint32_t source_ip, if (connect(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) goto out_fd; - int remain = sizeof(struct dhcpMessage); - int sent = 0; - while (1) { - result = write(fd, ((char *)payload) + sent, remain - sent); - if (result == -1) { - if (errno == EINTR) - continue; - log_error("kernel_packet: write failed: %s", strerror(errno)); - break; - } - sent += result; - if (remain == sent) - break; - } + result = safe_write(fd, (const char *)payload, sizeof(struct dhcpMessage)); + if (result == -1) + log_error("kernel_packet: write failed: %s", strerror(errno)); out_fd: close(fd); out: diff --git a/ndhc/script.c b/ndhc/script.c index 8536acb..8418a3e 100644 --- a/ndhc/script.c +++ b/ndhc/script.c @@ -38,6 +38,7 @@ #include "packet.h" #include "options.h" #include "log.h" +#include "io.h" #include "script.h" static int snprintip(char *dest, size_t size, unsigned char *ip) @@ -152,21 +153,10 @@ static int open_ifch(void) { static void sockwrite(int fd, const char *buf, size_t count) { - int ret; - int sent = 0; - while (1) { - ret = write(fd, buf + sent, count - sent); - if (ret == -1) { - if (errno == EINTR) - continue; - log_error("sockwrite: write failed: %s", strerror(errno)); - break; - } - sent += ret; - if (sent == count) - break; - } - log_line("writing: %s", buf); + if (safe_write(fd, buf, count) == -1) + log_error("sockwrite: write failed: %s", strerror(errno)); + else + log_line("sent to ifchd: %s", buf); } static void deconfig_if(void)