Factor out safe_write() into ncmlib and use it in ndhc as well.

This commit is contained in:
Nicholas J. Kain 2010-11-13 08:18:23 -05:00
parent edf3b02b6a
commit a43e69c7ae
6 changed files with 102 additions and 48 deletions

View File

@ -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 <njkain at gmail dot com>
*
@ -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;
}

View File

@ -7,6 +7,7 @@ set(NCMLIB_SRCS
strl.c
log.c
cap.c
io.c
)
add_library(ncmlib ${NCMLIB_SRCS})

49
ncmlib/io.c Normal file
View File

@ -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 <njkain at gmail dot com>
* 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 <unistd.h>
#include <errno.h>
// 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;
}

35
ncmlib/io.h Normal file
View File

@ -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 <njkain at gmail dot com>
* 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_ */

View File

@ -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:

View File

@ -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)