]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Move safewrite and saferead to a separate file.
authorJim Meyering <meyering@redhat.com>
Fri, 22 Feb 2008 15:53:13 +0000 (15:53 +0000)
committerJim Meyering <meyering@redhat.com>
Fri, 22 Feb 2008 15:53:13 +0000 (15:53 +0000)
We currently use safewrite from inside libvirt and don't want to publish
any such function name.  However, we do want to use it in applications
like virsh, libvirtd and libvirt_proxy that link with libvirt.  To that
end, this change moves that function definition (along with the nearly
identical saferead) into a new file, util-lib.c.  To avoid maintaining
separate copies of even such small functions, we simply include that new
file from util.c.  Then, the separate applications that need to use
safewrite simply compile and link with util-lib.c.

Of course, this does mean that each of those applications will
containing two copies of these functions.  However, the functions
are so small that it's not worth worrying about that.

* src/util.c (saferead, safewrite): Move function definitions to
util-lib.c and include that .c file.
* src/util-lib.c (saferead, safewrite): New file.  Functions from src/util.c
with slight change (s/int r =/ssize_t r =/) to reflect read/write return type.
* src/util-lib.h: Declare the two moved functions.
* src/util.h: Remove declarations.  Include src/util-lib.h.
* proxy/Makefile.am (libvirt_proxy_SOURCES): Add src/util-lib.c.
* qemud/Makefile.am (libvirtd_SOURCES): Likewise.
* src/Makefile.am (virsh_SOURCES): Add util-lib.c.  Remove some SP-before-TAB.

ChangeLog
proxy/Makefile.am
proxy/libvirt_proxy.c
qemud/Makefile.am
src/Makefile.am
src/util-lib.c [new file with mode: 0644]
src/util-lib.h [new file with mode: 0644]
src/util.c
src/util.h

index 1ca93a6cf3d09b5fc7911cda558807782e639bc6..8700dd302d167c6faa625410c0fabc579b39a1d2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,18 @@
 Fri Feb 22 13:32:11 CET 2008 Jim Meyering <meyering@redhat.com>
 
+       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.
+       * src/util-lib.c (saferead, safewrite): New file.
+       Functions from src/util.c with slight change (s/int r =/ssize_t r =/)
+       to reflect read/write return type.
+       * src/util-lib.h: Declare the two moved functions.
+       * src/util.h: Remove declarations.  Include src/util-lib.h.
+       * proxy/Makefile.am (libvirt_proxy_SOURCES): Add src/util-lib.c.
+       * qemud/Makefile.am (libvirtd_SOURCES): Likewise.
+       * src/Makefile.am (virsh_SOURCES): Add util-lib.c.
+       Remove some SP-before-TAB.
+
        With --enable-iptables-lokkit=no, avoid warning about unused parameter.
        * src/iptables.c (iptRulesSave) [!ENABLE_IPTABLES_LOKKIT]:
        Mark parameter as used.
index ff9a3eb1d2b39bab773ce5a36c7eff4d5e9f325b..68e331744f48aab6e6b1df19e7fbff2a2bac879e 100644 (file)
@@ -12,11 +12,13 @@ libexec_PROGRAMS = libvirt_proxy
 libvirt_proxy_SOURCES = libvirt_proxy.c @top_srcdir@/src/xend_internal.c \
            @top_srcdir@/src/xen_internal.c @top_srcdir@/src/virterror.c \
            @top_srcdir@/src/sexpr.c @top_srcdir@/src/xml.c \
-            @top_srcdir@/src/xs_internal.c @top_srcdir@/src/buf.c @top_srcdir@/src/uuid.c
+            @top_srcdir@/src/xs_internal.c @top_srcdir@/src/buf.c \
+            @top_srcdir@/src/util-lib.c \
+           @top_srcdir@/src/uuid.c
 libvirt_proxy_LDFLAGS = $(WARN_CFLAGS)
 libvirt_proxy_DEPENDENCIES =
 libvirt_proxy_LDADD =
 
 install-exec-hook:
        chmod u+s $(DESTDIR)$(libexecdir)/libvirt_proxy
-endif
\ No newline at end of file
+endif
index d96d3db2defb132ad457db42759ffef1d33df870..a22ba6cf6db7e9578a7f3d55c1785f1d866def0f 100644 (file)
@@ -2,7 +2,7 @@
  * proxy_svr.c: root suid proxy server for Xen access to APIs with no
  *              side effects from unauthenticated clients.
  *
- * Copyright (C) 2006, 2007 Red Hat, Inc.
+ * Copyright (C) 2006, 2007, 2008 Red Hat, Inc.
  *
  * See COPYING.LIB for the License of this software
  *
@@ -26,6 +26,7 @@
 #include "internal.h"
 
 #include "proxy_internal.h"
+#include "util.h"
 #include "xen_internal.h"
 #include "xend_internal.h"
 #include "xs_internal.h"
@@ -317,19 +318,12 @@ proxyWriteClientSocket(int nr, virProxyPacketPtr req) {
        return(-1);
     }
 
-retry:
-    ret = write(pollInfos[nr].fd, (char *) req, req->len);
+    ret = safewrite(pollInfos[nr].fd, (char *) req, req->len);
     if (ret < 0) {
-        if (errno == EINTR) {
-           if (debug > 0)
-               fprintf(stderr, "write socket %d to client %d interrupted\n",
-                       pollInfos[nr].fd, nr);
-           goto retry;
-       }
         fprintf(stderr, "write %d bytes to socket %d from client %d failed\n",
                req->len, pollInfos[nr].fd, nr);
-       proxyCloseClientSocket(nr);
-       return(-1);
+        proxyCloseClientSocket(nr);
+        return(-1);
     }
     if (ret == 0) {
        if (debug)
index db6adb18a76f5f7f5470b27614453d54e800e1da..1e1f86175e5d0aed060bae15e7fad6a929cddb1e 100644 (file)
@@ -42,6 +42,7 @@ libvirtd_SOURCES = \
                qemud.c internal.h \
                remote_protocol.h remote_protocol.c \
                remote.c \
+               $(srcdir)/../src/util-lib.c \
                event.c event.h
 
 #-D_XOPEN_SOURCE=600 -D_XOPEN_SOURCE_EXTENDED=1 -D_POSIX_C_SOURCE=199506L
@@ -151,4 +152,4 @@ uninstall-init:
 
 endif # DBUS_INIT_SCRIPTS_RED_HAT
 
-endif # WITH_LIBVIRTD
\ No newline at end of file
+endif # WITH_LIBVIRTD
index 7bbf816d85a3ae518f1eb541fae064ba1656ef16..1da0d73865a701a789e3c5da5b2d01813325648e 100644 (file)
@@ -51,21 +51,21 @@ CLIENT_SOURCES =                                            \
                conf.c conf.h                                   \
                xm_internal.c xm_internal.h                     \
                remote_internal.c remote_internal.h             \
-               bridge.c bridge.h                               \
-               iptables.c iptables.h                           \
-               uuid.c uuid.h                                   \
-               qemu_driver.c qemu_driver.h                     \
+               bridge.c bridge.h                               \
+               iptables.c iptables.h                           \
+               uuid.c uuid.h                                   \
+               qemu_driver.c qemu_driver.h                     \
                qemu_conf.c qemu_conf.h                         \
                openvz_conf.c openvz_conf.h                     \
-               openvz_driver.c openvz_driver.h                 \
+               openvz_driver.c openvz_driver.h                 \
                 nodeinfo.h nodeinfo.c                           \
                storage_conf.h storage_conf.c                   \
                storage_driver.h storage_driver.c               \
                storage_backend.h storage_backend.c             \
-               storage_backend_fs.h storage_backend_fs.c       \
+               storage_backend_fs.h storage_backend_fs.c       \
                util.c util.h
 
-SERVER_SOURCES =                                               \
+SERVER_SOURCES =                                               \
                ../qemud/remote_protocol.c ../qemud/remote_protocol.h
 
 if WITH_STORAGE_LVM
@@ -100,7 +100,7 @@ libvirt_la_CFLAGS = $(COVERAGE_CFLAGS)
 
 bin_PROGRAMS = virsh
 
-virsh_SOURCES = virsh.c console.c console.h
+virsh_SOURCES = virsh.c console.c console.h util-lib.c
 virsh_LDFLAGS = $(WARN_CFLAGS) $(COVERAGE_LDFLAGS)
 virsh_DEPENDENCIES = $(DEPS)
 virsh_LDADD = $(LDADDS) $(VIRSH_LIBS)
diff --git a/src/util-lib.c b/src/util-lib.c
new file mode 100644 (file)
index 0000000..92496cd
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * common, generic utility functions
+ *
+ * Copyright (C) 2006, 2007, 2008 Red Hat, Inc.
+ * See COPYING.LIB for the License of this software
+ */
+
+#include <config.h>
+
+#include <unistd.h>
+#include <errno.h>
+
+#include "util-lib.h"
+
+/* Like read(), but restarts after EINTR */
+int saferead(int fd, void *buf, size_t count)
+{
+       size_t nread = 0;
+       while (count > 0) {
+               ssize_t r = read(fd, buf, count);
+               if (r < 0 && errno == EINTR)
+                       continue;
+               if (r < 0)
+                       return r;
+               if (r == 0)
+                       return nread;
+               buf = (char *)buf + r;
+               count -= r;
+               nread += r;
+       }
+       return nread;
+}
+
+/* Like write(), but restarts after EINTR */
+ssize_t safewrite(int fd, const void *buf, size_t count)
+{
+       size_t nwritten = 0;
+       while (count > 0) {
+               ssize_t r = write(fd, buf, count);
+
+               if (r < 0 && errno == EINTR)
+                       continue;
+               if (r < 0)
+                       return r;
+               if (r == 0)
+                       return nwritten;
+               buf = (const char *)buf + r;
+               count -= r;
+               nwritten += r;
+       }
+       return nwritten;
+}
diff --git a/src/util-lib.h b/src/util-lib.h
new file mode 100644 (file)
index 0000000..4a14810
--- /dev/null
@@ -0,0 +1,16 @@
+/*
+ * private utility functions
+ *
+ * Copyright (C) 2008 Red Hat, Inc.
+ * See COPYING.LIB for the License of this software
+ */
+
+#ifndef __UTIL_LIB_H__
+#define __UTIL_LIB_H__
+
+#include <sys/types.h>
+
+int saferead(int fd, void *buf, size_t count);
+ssize_t safewrite(int fd, const void *buf, size_t count);
+
+#endif
index 19131a793f8d2e80c9444b4562b687031bb6baf4..67cb0b816ea92fcd55c33b580725344878ce3deb 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * utils.c: common, generic utility functions
  *
- * Copyright (C) 2006, 2007 Red Hat, Inc.
+ * Copyright (C) 2006, 2007, 2008 Red Hat, Inc.
  * Copyright (C) 2006 Daniel P. Berrange
  * Copyright (C) 2006, 2007 Binary Karma
  * Copyright (C) 2006 Shuveb Hussain
@@ -46,6 +46,8 @@
 #include "buf.h"
 #include "util.h"
 
+#include "util-lib.c"
+
 #define MAX_ERROR_LEN   1024
 
 #define virLog(msg...) fprintf(stderr, msg)
@@ -276,45 +278,6 @@ virExecNonBlock(virConnectPtr conn,
 
 #endif /* __MINGW32__ */
 
-/* Like read(), but restarts after EINTR */
-int saferead(int fd, void *buf, size_t count)
-{
-       size_t nread = 0;
-       while (count > 0) {
-               int r = read(fd, buf, count);
-               if (r < 0 && errno == EINTR)
-                       continue;
-               if (r < 0)
-                       return r;
-               if (r == 0)
-                       return nread;
-               buf = (unsigned char *)buf + r;
-               count -= r;
-               nread += r;
-       }
-       return nread;
-}
-
-/* Like write(), but restarts after EINTR */
-ssize_t safewrite(int fd, const void *buf, size_t count)
-{
-       size_t nwritten = 0;
-       while (count > 0) {
-               int r = write(fd, buf, count);
-
-               if (r < 0 && errno == EINTR)
-                       continue;
-               if (r < 0)
-                       return r;
-               if (r == 0)
-                       return nwritten;
-               buf = (unsigned char *)buf + r;
-               count -= r;
-               nwritten += r;
-       }
-       return nwritten;
-}
-
 
 int __virFileReadAll(const char *path,
                      int maxlen,
index 7080ed0e03412d14134a723c1e9fafc215a65994..23a016de5c68ee577872e443dcdec0e25d103762 100644 (file)
 #define __VIR_UTIL_H__
 
 #include "internal.h"
+#include "util-lib.h"
 
-int virExec(virConnectPtr conn, char **argv, int *retpid, int infd, int *outfd, int *errfd);
-int virExecNonBlock(virConnectPtr conn, char **argv, int *retpid, int infd, int *outfd, int *errfd);
+int virExec(virConnectPtr conn, char **argv, int *retpid,
+           int infd, int *outfd, int *errfd);
+int virExecNonBlock(virConnectPtr conn, char **argv, int *retpid,
+                   int infd, int *outfd, int *errfd);
 int virRun(virConnectPtr conn, char **argv, int *status);
 
-int saferead(int fd, void *buf, size_t count);
-ssize_t safewrite(int fd, const void *buf, size_t count);
-
 int __virFileReadAll(const char *path,
                     int maxlen,
                     char **buf);