Also add "make syntax-check" rules to ensure no new uses sneak in.
There are many uses of write like this:
if (write (fd, xml, towrite) != towrite)
return -1;
The problem is that the syscall can succeed, yet write less than
the requested number of bytes, so the caller should retry
rather than simply failing.
This patch changes most of them to use util.c's safewrite wrapper,
which encapsulates the process. Also, there were a few cases in
which the retry loop was open-coded, and I replaced those, too.
* Makefile.maint (sc_avoid_write): New rule, to avoid recurrence.
* .x-sc_avoid_write: New file. Record two legitimate exemptions.
* qemud/qemud.c (sig_handler, qemudClientWriteBuf): Use safewrite, not write.
* src/conf.c (__virConfWriteFile): Likewise.
* src/qemu_conf.c (qemudSaveConfig, qemudSaveNetworkConfig): Likewise.
* src/qemu_driver.c (qemudWaitForMonitor, qemudStartVMDaemon)
(qemudVMData, PROC_IP_FORWARD): Likewise.
* proxy/libvirt_proxy.c: Include "util.h".
(proxyWriteClientSocket): Use safewrite.
* src/test.c (testDomainSave, testDomainCoreDump): Likewise.
* src/proxy_internal.c (virProxyWriteClientSocket): Likewise.
* src/virsh.c: Include "util-lib.h".
(vshOutputLogFile): Use safewrite.
* src/console.c: Include "util-lib.h".
(vshRunConsole): Use safewrite.
--- /dev/null
+^src/util\.c$
+^src/xend_internal\.c$
Fri Feb 22 13:32:11 CET 2008 Jim Meyering <meyering@redhat.com>
+ Use safewrite in place of write, in many cases.
+ Also add "make syntax-check" rules to ensure no new uses sneak in.
+ * Makefile.maint (sc_avoid_write): New rule, to avoid recurrence.
+ * .x-sc_avoid_write: New file. Record two legitimate exemptions.
+ * qemud/qemud.c (sig_handler, qemudClientWriteBuf): Use safewrite,
+ not write.
+ * src/conf.c (__virConfWriteFile): Likewise.
+ * src/qemu_conf.c (qemudSaveConfig, qemudSaveNetworkConfig): Likewise.
+ * src/qemu_driver.c (qemudWaitForMonitor, qemudStartVMDaemon)
+ (qemudVMData, PROC_IP_FORWARD): Likewise.
+ * proxy/libvirt_proxy.c: Include "util.h".
+ (proxyWriteClientSocket): Use safewrite.
+ * src/test.c (testDomainSave, testDomainCoreDump): Likewise.
+ * src/proxy_internal.c (virProxyWriteClientSocket): Likewise.
+ * src/virsh.c: Include "util-lib.h".
+ (vshOutputLogFile): Use safewrite.
+ * src/console.c: Include "util-lib.h".
+ (vshRunConsole): Use safewrite.
+
Move safewrite and saferead to a separate file.
* src/util.c (saferead, safewrite): Move function definitions to
util-lib.c and include that .c file.
{ echo '$(ME): found useless "if" before "free" above' 1>&2; \
exit 1; } || :
+# Avoid uses of write(2). Either switch to streams (fwrite), or use
+# the safewrite wrapper.
+sc_avoid_write:
+ @if $(CVS_LIST_EXCEPT) | grep '\.c$$' > /dev/null; then \
+ grep '\<write *(' $$($(CVS_LIST_EXCEPT) | grep '\.c$$') && \
+ { echo "$(ME): the above files use write;" \
+ " consider using the safewrite wrapper instead" \
+ 1>&2; exit 1; } || :; \
+ else :; \
+ fi
+
sc_cast_of_argument_to_free:
@grep -nE '\<free \(\(' $$($(CVS_LIST_EXCEPT)) && \
{ echo '$(ME): don'\''t cast free argument' 1>&2; \
return;
origerrno = errno;
- r = write(sigwrite, &sigc, 1);
+ r = safewrite(sigwrite, &sigc, 1);
if (r == -1) {
sig_errors++;
sig_lasterrno = errno;
const char *data, int len) {
int ret;
if (!client->tlssession) {
- if ((ret = write(client->fd, data, len)) == -1) {
- if (errno != EAGAIN) {
- qemudLog (QEMUD_ERR, _("write: %s"), strerror (errno));
- qemudDispatchClientFailure(server, client);
- }
+ if ((ret = safewrite(client->fd, data, len)) == -1) {
+ qemudLog (QEMUD_ERR, _("write: %s"), strerror (errno));
+ qemudDispatchClientFailure(server, client);
return -1;
}
} else {
goto error;
}
- ret = write(fd, buf->content, buf->use);
+ ret = safewrite(fd, buf->content, buf->use);
close(fd);
if (ret != (int) buf->use) {
virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to save content"), 0);
/*
* console.c: A dumb serial console client
*
- * Copyright (C) 2007 Red Hat, Inc.
+ * Copyright (C) 2007, 2008 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
#include "console.h"
#include "internal.h"
+#include "util-lib.h"
/* ie Ctrl-] as per telnet */
#define CTRL_CLOSE_BRACKET '\35'
while (sent < got) {
int done;
- if ((done = write(destfd, buf + sent, got - sent)) <= 0) {
+ if ((done = safewrite(destfd, buf + sent, got - sent))
+ <= 0) {
fprintf(stderr, _("failure writing output: %s\n"),
strerror(errno));
goto cleanup;
/*
* proxy_client.c: client side of the communication with the libvirt proxy.
*
- * Copyright (C) 2006 Red Hat, Inc.
+ * Copyright (C) 2006, 2008 Red Hat, Inc.
*
* See COPYING.LIB for the License of this software
*
#include "internal.h"
#include "driver.h"
#include "proxy_internal.h"
+#include "util.h"
#include "xen_unified.h"
#define STANDALONE
if ((fd < 0) || (data == NULL) || (len < 0))
return(-1);
-retry:
- ret = write(fd, data, len);
+ ret = safewrite(fd, data, len);
if (ret < 0) {
- if (errno == EINTR) {
- if (debug > 0)
- fprintf(stderr, "write socket %d, %d bytes interrupted\n",
- fd, len);
- goto retry;
- }
fprintf(stderr, _("Failed to write to socket %d\n"), fd);
- return(-1);
+ return(-1);
}
if (debug)
fprintf(stderr, "wrote %d bytes to socket %d\n",
}
towrite = strlen(xml);
- if (write(fd, xml, towrite) != towrite) {
+ if (safewrite(fd, xml, towrite) < 0) {
qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
"cannot write config file %s: %s",
vm->configFile, strerror(errno));
}
towrite = strlen(xml);
- if (write(fd, xml, towrite) != towrite) {
+ if (safewrite(fd, xml, towrite) < 0) {
qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
"cannot write config file %s: %s",
network->configFile, strerror(errno));
"console");
buf[sizeof(buf)-1] = '\0';
- retry:
- if (write(vm->logfile, buf, strlen(buf)) < 0) {
+
+ if (safewrite(vm->logfile, buf, strlen(buf)) < 0) {
/* Log, but ignore failures to write logfile for VM */
- if (errno == EINTR)
- goto retry;
qemudLog(QEMUD_WARN, _("Unable to log VM console data: %s"),
strerror(errno));
}
tmp = argv;
while (*tmp) {
- if (write(vm->logfile, *tmp, strlen(*tmp)) < 0)
+ if (safewrite(vm->logfile, *tmp, strlen(*tmp)) < 0)
qemudLog(QEMUD_WARN, _("Unable to write argv to logfile %d: %s"),
errno, strerror(errno));
- if (write(vm->logfile, " ", 1) < 0)
+ if (safewrite(vm->logfile, " ", 1) < 0)
qemudLog(QEMUD_WARN, _("Unable to write argv to logfile %d: %s"),
errno, strerror(errno));
tmp++;
}
- if (write(vm->logfile, "\n", 1) < 0)
+ if (safewrite(vm->logfile, "\n", 1) < 0)
qemudLog(QEMUD_WARN, _("Unable to write argv to logfile %d: %s"),
errno, strerror(errno));
}
buf[ret] = '\0';
- retry:
- if (write(vm->logfile, buf, ret) < 0) {
+ if (safewrite(vm->logfile, buf, ret) < 0) {
/* Log, but ignore failures to write logfile for VM */
- if (errno == EINTR)
- goto retry;
qemudLog(QEMUD_WARN, _("Unable to log VM console data: %s"),
strerror(errno));
}
if ((fd = open(PROC_IP_FORWARD, O_WRONLY|O_TRUNC)) == -1)
return 0;
- if (write(fd, "1\n", 2) < 0)
+ if (safewrite(fd, "1\n", 2) < 0)
ret = 0;
close (fd);
#include "test.h"
#include "xml.h"
#include "buf.h"
+#include "util.h"
#include "uuid.h"
/* Flags that determine the action to take on a shutdown or crash of a domain
return (-1);
}
len = strlen(xml);
- if (write(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) != sizeof(TEST_SAVE_MAGIC)) {
+ if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
"cannot write header");
close(fd);
return (-1);
}
- if (write(fd, (char*)&len, sizeof(len)) != sizeof(len)) {
+ if (safewrite(fd, (char*)&len, sizeof(len)) < 0) {
testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
"cannot write metadata length");
close(fd);
return (-1);
}
- if (write(fd, xml, len) != len) {
+ if (safewrite(fd, xml, len) < 0) {
testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
"cannot write metadata");
free(xml);
"cannot save domain core");
return (-1);
}
- if (write(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) != sizeof(TEST_SAVE_MAGIC)) {
+ if (safewrite(fd, TEST_SAVE_MAGIC, sizeof(TEST_SAVE_MAGIC)) < 0) {
testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR,
"cannot write header");
close(fd);
#include <readline/history.h>
#endif
+#include "buf.h"
#include "console.h"
#include "util.h"
-#include "buf.h"
+#include "util-lib.h"
static char *progname;
snprintf(msg_buf + strlen(msg_buf), sizeof(msg_buf) - strlen(msg_buf), "\n");
/* write log */
- if (write(ctl->log_fd, msg_buf, strlen(msg_buf)) == -1) {
+ if (safewrite(ctl->log_fd, msg_buf, strlen(msg_buf)) < 0) {
vshCloseLogFile(ctl);
vshError(ctl, FALSE, "%s", _("failed to write the log file"));
}