[libvirt] PATCH: Remove util-lib file and replace with libvirt_util.la

Daniel P. Berrange berrange at redhat.com
Fri Oct 24 13:30:40 UTC 2008


We've currently got a slightly nasty hack where some of the stuff from
util.c is split into  a util-lib.c file so it can be compiled twice,
once for libvirt.la, and second for virsh, without exporting it in our
public API. I'm trying to clean up the code / file deps and build stuff
in preparation of dlopen() support of drivers.  We also have a bunch of
stuff in util.a that is exported unofficially in the ABI. 

This patch demonstrates a different approach, putting the util-lib.c code
back into util.c. We then define a libtool convenience library covering
a bunch of our generic common code, that is linked to both libvirt.la
and virsh.  This also lets us remove most of the secret exports from
util.c.

The main change is that a little more code is statically linked into
virsh binary itself. Personally I don't think this is a huge problem.
Another possible approach though could be to create a real libvirt-common.so
file which gets installed into /usr/lib, but is explicitly not preserving
ABI. We could enforce this by using a version script to encode a version
number against every symbol. This would mean anyone linking to the 
libvirt-common.so would be forced to relink on every new update - which
would be such a PITA only in-tree programs like virsh & libvirtd would
do it.

Daniel

diff -r 5bd402f3762e configure.in
--- a/configure.in	Fri Oct 24 14:13:53 2008 +0100
+++ b/configure.in	Fri Oct 24 14:14:07 2008 +0100
@@ -671,6 +671,8 @@
 
 with_storage_dir=yes
 if test "$with_libvirtd" = "no"; then
+  with_qemu=no
+  with_lxc=no
   with_storage_dir=no
   with_storage_fs=no
   with_storage_lvm=no
diff -r 5bd402f3762e qemud/Makefile.am
--- a/qemud/Makefile.am	Fri Oct 24 14:13:53 2008 +0100
+++ b/qemud/Makefile.am	Fri Oct 24 14:14:07 2008 +0100
@@ -9,8 +9,7 @@
 		remote_dispatch_prototypes.h		\
 		remote_dispatch_localvars.h		\
 		remote_dispatch_proc_switch.h		\
-		remote_protocol.h remote_protocol.c	\
-		$(srcdir)/../src/util-lib.c
+		remote_protocol.h remote_protocol.c
 
 AVAHI_SOURCES =						\
 		mdns.c mdns.h
@@ -88,7 +87,7 @@
 	$(POLKIT_LIBS)
 
 libvirtd_DEPENDENCIES = ../src/libvirt.la
-libvirtd_LDADD = ../src/libvirt.la ../gnulib/lib/libgnu.la
+libvirtd_LDADD = ../src/libvirt.la ../src/libvirt_util.la ../gnulib/lib/libgnu.la
 
 if HAVE_POLKIT
 policydir = $(datadir)/PolicyKit/policy
diff -r 5bd402f3762e src/Makefile.am
--- a/src/Makefile.am	Fri Oct 24 14:13:53 2008 +0100
+++ b/src/Makefile.am	Fri Oct 24 14:14:07 2008 +0100
@@ -37,21 +37,30 @@
 
 lib_LTLIBRARIES = libvirt.la
 
+noinst_LTLIBRARIES = libvirt_util.la
+
 # These files are not related to driver APIs. Simply generic
 # helper APIs for various purposes
-GENERIC_LIB_SOURCES =						\
-		bridge.c bridge.h				\
+UTIL_SOURCES =							\
 		buf.c buf.h					\
 		conf.c conf.h					\
 		event.c event.h					\
-		iptables.c iptables.h				\
 		memory.c memory.h				\
 		qparams.c qparams.h				\
-		stats_linux.c stats_linux.h			\
 		uuid.c uuid.h					\
 		util.c util.h					\
 		virterror.c					\
 		xml.c xml.h
+
+DRIVER_SOURCES =						\
+		driver.h					\
+		hash.c hash.h					\
+		internal.h					\
+		libvirt.c					\
+		bridge.c bridge.h				\
+		iptables.c iptables.h				\
+		stats_linux.c stats_linux.h
+
 
 # Domain driver generic impl APIs
 DOMAIN_CONF_SOURCES =						\
@@ -147,14 +156,13 @@
 # First deal with sources usable in non-daemon context
 
 libvirt_la_SOURCES =						\
-		driver.h					\
-		hash.c hash.h					\
-		internal.h					\
-		libvirt.c					\
-		$(GENERIC_LIB_SOURCES)				\
+		$(DRIVER_SOURCES)				\
 		$(DOMAIN_CONF_SOURCES)				\
 		$(NETWORK_CONF_SOURCES)				\
 		$(STORAGE_CONF_SOURCES)
+
+libvirt_util_la_SOURCES = \
+		$(UTIL_SOURCES)
 
 # Drivers usable outside daemon context
 if WITH_TEST
@@ -180,6 +188,7 @@
 endif
 libvirt_la_SOURCES += $(STORAGE_DRIVER_SOURCES)
 libvirt_la_SOURCES += $(STORAGE_DRIVER_FS_SOURCES)
+endif
 
 if WITH_QEMU
 libvirt_la_SOURCES += $(QEMU_DRIVER_SOURCES)
@@ -200,7 +209,6 @@
 if WITH_STORAGE_DISK
 libvirt_la_SOURCES += $(STORAGE_DRIVER_DISK_SOURCES)
 endif
-endif
 
 # Add all conditional sources just in case...
 EXTRA_DIST +=							\
@@ -220,17 +228,18 @@
 
 libvirt_la_LIBADD = $(LIBXML_LIBS) $(GNUTLS_LIBS) $(SASL_LIBS) $(SELINUX_LIBS) \
                     $(NUMACTL_LIBS) \
+                    libvirt_util.la \
 		    @CYGWIN_EXTRA_LIBADD@ ../gnulib/lib/libgnu.la
 libvirt_la_LDFLAGS = -Wl,--version-script=$(srcdir)/libvirt_sym.version \
                      -version-info @LIBVIRT_VERSION_INFO@ \
                     $(COVERAGE_CFLAGS:-f%=-Wc,-f%) \
 		    @CYGWIN_EXTRA_LDFLAGS@ @MINGW_EXTRA_LDFLAGS@
-libvirt_la_CFLAGS = $(COVERAGE_CFLAGS) -DIN_LIBVIRT
+libvirt_la_CFLAGS = $(COVERAGE_CFLAGS)
 
 # Create an automake "convenience library" version of libvirt_la,
 # just for testing, since the test harness requires access to internal
 # bits and pieces that we don't want to make publicly accessible.
-noinst_LTLIBRARIES = libvirt_test.la
+noinst_LTLIBRARIES += libvirt_test.la
 
 # Convert libvirt_sym.version
 #      to libvirt_test_sym.version, and
@@ -250,12 +259,11 @@
 
 virsh_SOURCES =							\
 		console.c console.h				\
-		util-lib.c util-lib.h				\
 		virsh.c
 
 virsh_LDFLAGS = $(WARN_CFLAGS) $(COVERAGE_LDFLAGS)
 virsh_DEPENDENCIES = $(DEPS)
-virsh_LDADD = $(LDADDS) $(VIRSH_LIBS)
+virsh_LDADD = $(LDADDS) $(VIRSH_LIBS) libvirt_util.la
 virsh_CFLAGS = $(COVERAGE_CFLAGS) $(READLINE_CFLAGS)
 BUILT_SOURCES = virsh-net-edit.c virsh-pool-edit.c
 
@@ -320,7 +328,6 @@
 libexec_PROGRAMS =
 
 if WITH_STORAGE_DISK
-if WITH_LIBVIRTD
 libexec_PROGRAMS += libvirt_parthelper
 
 libvirt_parthelper_SOURCES = $(STORAGE_HELPER_DISK_SOURCES)
@@ -328,22 +335,19 @@
 libvirt_parthelper_LDADD = $(LIBPARTED_LIBS)
 libvirt_parthelper_CFLAGS =  $(LIBPARTED_CFLAGS)
 endif
-endif
 EXTRA_DIST += $(STORAGE_HELPER_DISK_SOURCES)
 
 
 if WITH_LXC
-if WITH_LIBVIRTD
 libexec_PROGRAMS += libvirt_lxc
 
 libvirt_lxc_SOURCES =						\
 		$(LXC_CONTROLLER_SOURCES)			\
-		$(GENERIC_LIB_SOURCES) 				\
 		$(DOMAIN_CONF_SOURCES)
 libvirt_lxc_LDFLAGS = $(WARN_CFLAGS) $(COVERAGE_LDCFLAGS)
-libvirt_lxc_LDADD = $(LIBXML_LIBS) ../gnulib/lib/libgnu.la
+libvirt_lxc_LDADD = $(LIBXML_LIBS) ../gnulib/lib/libgnu.la \
+			libvirt_util.la
 libvirt_lxc_CFLAGS =  $(LIBPARTED_CFLAGS)
-endif
 endif
 EXTRA_DIST += $(LXC_CONTROLLER_SOURCES)
 
diff -r 5bd402f3762e src/buf.c
--- a/src/buf.c	Fri Oct 24 14:13:53 2008 +0100
+++ b/src/buf.c	Fri Oct 24 14:14:07 2008 +0100
@@ -88,7 +88,7 @@
  *
  */
 void
-__virBufferAdd(const virBufferPtr buf, const char *str, int len)
+virBufferAdd(const virBufferPtr buf, const char *str, int len)
 {
     unsigned int needSize;
 
@@ -120,7 +120,7 @@
  *
  */
 void
-__virBufferAddChar (virBufferPtr buf, char c)
+virBufferAddChar (virBufferPtr buf, char c)
 {
     unsigned int needSize;
 
@@ -150,7 +150,7 @@
  * Returns the buffer content or NULL in case of error.
  */
 char *
-__virBufferContentAndReset(const virBufferPtr buf)
+virBufferContentAndReset(const virBufferPtr buf)
 {
     char *str;
     if (buf == NULL)
@@ -176,7 +176,7 @@
  * Return true if in error, 0 if normal
  */
 int
-__virBufferError(const virBufferPtr buf)
+virBufferError(const virBufferPtr buf)
 {
     if (buf == NULL)
         return 1;
@@ -208,7 +208,7 @@
  * Do a formatted print to an XML buffer.
  */
 void
-__virBufferVSprintf(const virBufferPtr buf, const char *format, ...)
+virBufferVSprintf(const virBufferPtr buf, const char *format, ...)
 {
     int size, count, grow_size;
     va_list locarg, argptr;
diff -r 5bd402f3762e src/buf.h
--- a/src/buf.h	Fri Oct 24 14:13:53 2008 +0100
+++ b/src/buf.h	Fri Oct 24 14:14:07 2008 +0100
@@ -34,25 +34,18 @@
 };
 #endif
 
-char *__virBufferContentAndReset(const virBufferPtr buf);
-int __virBufferError(const virBufferPtr buf);
+char *virBufferContentAndReset(const virBufferPtr buf);
+int virBufferError(const virBufferPtr buf);
 unsigned int virBufferUse(const virBufferPtr buf);
-void __virBufferAdd(const virBufferPtr buf, const char *str, int len);
-void __virBufferAddChar(const virBufferPtr buf, char c);
-void __virBufferVSprintf(const virBufferPtr buf, const char *format, ...)
+void virBufferAdd(const virBufferPtr buf, const char *str, int len);
+void virBufferAddChar(const virBufferPtr buf, char c);
+void virBufferVSprintf(const virBufferPtr buf, const char *format, ...)
   ATTRIBUTE_FORMAT(printf, 2, 3);
 void virBufferStrcat(const virBufferPtr buf, ...);
 void virBufferEscapeString(const virBufferPtr buf, const char *format, const char *str);
 void virBufferURIEncodeString (const virBufferPtr buf, const char *str);
 
 #define virBufferAddLit(buf_, literal_string_) \
-  __virBufferAdd (buf_, "" literal_string_ "", sizeof literal_string_ - 1)
-
-#define virBufferAdd(b,s,l) __virBufferAdd((b),(s),(l))
-#define virBufferAddChar(b,c) __virBufferAddChar((b),(c))
-#define virBufferVSprintf(b,f,...) __virBufferVSprintf((b),(f), __VA_ARGS__)
-
-#define virBufferContentAndReset(b) __virBufferContentAndReset((b))
-#define virBufferError(b) __virBufferError((b))
+  virBufferAdd (buf_, "" literal_string_ "", sizeof literal_string_ - 1)
 
 #endif /* __VIR_BUFFER_H__ */
diff -r 5bd402f3762e src/conf.c
--- a/src/conf.c	Fri Oct 24 14:13:53 2008 +0100
+++ b/src/conf.c	Fri Oct 24 14:14:07 2008 +0100
@@ -142,7 +142,7 @@
  * Free a value
  */
 void
-__virConfFreeValue(virConfValuePtr val)
+virConfFreeValue(virConfValuePtr val)
 {
     if (val == NULL)
         return;
@@ -156,7 +156,7 @@
 }
 
 virConfPtr
-__virConfNew(void)
+virConfNew(void)
 {
     virConfPtr ret;
 
@@ -691,7 +691,7 @@
 #define MAX_CONFIG_FILE_SIZE (1024*1024*10)
 
 /**
- * __virConfReadFile:
+ * virConfReadFile:
  * @filename: the path to the configuration file.
  *
  * Reads a configuration file.
@@ -700,7 +700,7 @@
  *         read or parse the file, use virConfFree() to free the data.
  */
 virConfPtr
-__virConfReadFile(const char *filename)
+virConfReadFile(const char *filename)
 {
     char *content;
     int len;
@@ -724,7 +724,7 @@
 }
 
 /**
- * __virConfReadMem:
+ * virConfReadMem:
  * @memory: pointer to the content of the configuration file
  * @len: length in byte
  *
@@ -735,7 +735,7 @@
  *         parse the content, use virConfFree() to free the data.
  */
 virConfPtr
-__virConfReadMem(const char *memory, int len)
+virConfReadMem(const char *memory, int len)
 {
     if ((memory == NULL) || (len < 0)) {
         virConfError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
@@ -748,7 +748,7 @@
 }
 
 /**
- * __virConfFree:
+ * virConfFree:
  * @conf: a configuration file handle
  *
  * Frees all data associated to the handle
@@ -756,7 +756,7 @@
  * Returns 0 in case of success, -1 in case of error.
  */
 int
-__virConfFree(virConfPtr conf)
+virConfFree(virConfPtr conf)
 {
     virConfEntryPtr tmp;
     if (conf == NULL) {
@@ -779,7 +779,7 @@
 }
 
 /**
- * __virConfGetValue:
+ * virConfGetValue:
  * @conf: a configuration file handle
  * @entry: the name of the entry
  *
@@ -789,7 +789,7 @@
  *         associated will be freed when virConfFree() is called
  */
 virConfValuePtr
-__virConfGetValue(virConfPtr conf, const char *setting)
+virConfGetValue(virConfPtr conf, const char *setting)
 {
     virConfEntryPtr cur;
 
@@ -803,7 +803,7 @@
 }
 
 /**
- * __virConfSetValue:
+ * virConfSetValue:
  * @conf: a configuration file handle
  * @entry: the name of the entry
  * @value: the new configuration value
@@ -816,7 +816,7 @@
  * Returns 0 on success, or -1 on failure.
  */
 int
-__virConfSetValue (virConfPtr conf,
+virConfSetValue (virConfPtr conf,
                   const char *setting,
                   virConfValuePtr value)
 {
@@ -861,7 +861,7 @@
 
 
 /**
- * __virConfWriteFile:
+ * virConfWriteFile:
  * @filename: the path to the configuration file.
  * @conf: the conf
  *
@@ -870,7 +870,7 @@
  * Returns the number of bytes written or -1 in case of error.
  */
 int
-__virConfWriteFile(const char *filename, virConfPtr conf)
+virConfWriteFile(const char *filename, virConfPtr conf)
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     virConfEntryPtr cur;
@@ -915,7 +915,7 @@
 }
 
 /**
- * __virConfWriteMem:
+ * virConfWriteMem:
  * @memory: pointer to the memory to store the config file
  * @len: pointer to the length in bytes of the store, on output the size
  * @conf: the conf
@@ -928,7 +928,7 @@
  * Returns the number of bytes written or -1 in case of error.
  */
 int
-__virConfWriteMem(char *memory, int *len, virConfPtr conf)
+virConfWriteMem(char *memory, int *len, virConfPtr conf)
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     virConfEntryPtr cur;
diff -r 5bd402f3762e src/conf.h
--- a/src/conf.h	Fri Oct 24 14:13:53 2008 +0100
+++ b/src/conf.h	Fri Oct 24 14:14:07 2008 +0100
@@ -61,32 +61,22 @@
 typedef struct _virConf virConf;
 typedef virConf *virConfPtr;
 
-virConfPtr      __virConfNew             (void);
-virConfPtr	__virConfReadFile	(const char *filename);
-virConfPtr	__virConfReadMem		(const char *memory,
+virConfPtr      virConfNew             (void);
+virConfPtr	virConfReadFile	(const char *filename);
+virConfPtr	virConfReadMem		(const char *memory,
                                          int len);
-int		__virConfFree		(virConfPtr conf);
-void            __virConfFreeValue      (virConfValuePtr val);
+int		virConfFree		(virConfPtr conf);
+void            virConfFreeValue      (virConfValuePtr val);
 
-virConfValuePtr	__virConfGetValue	(virConfPtr conf,
+virConfValuePtr	virConfGetValue	(virConfPtr conf,
                                          const char *setting);
-int             __virConfSetValue        (virConfPtr conf,
+int             virConfSetValue        (virConfPtr conf,
                                          const char *setting,
                                          virConfValuePtr value);
-int		__virConfWriteFile	(const char *filename,
+int		virConfWriteFile	(const char *filename,
                                          virConfPtr conf);
-int		__virConfWriteMem	(char *memory,
+int		virConfWriteMem	(char *memory,
                                          int *len,
                                          virConfPtr conf);
 
-#define virConfNew() __virConfNew()
-#define virConfReadFile(f) __virConfReadFile((f))
-#define virConfReadMem(m,l) __virConfReadMem((m),(l))
-#define virConfFree(c) __virConfFree((c))
-#define virConfFreeValue(v) __virConfFreeValue((v))
-#define virConfGetValue(c,s) __virConfGetValue((c),(s))
-#define virConfSetValue(c,s,v) __virConfSetValue((c),(s),(v))
-#define virConfWriteFile(f,c) __virConfWriteFile((f),(c))
-#define virConfWriteMem(m,l,c) __virConfWriteMem((m),(l),(c))
-
 #endif /* __VIR_CONF_H__ */
diff -r 5bd402f3762e src/console.c
--- a/src/console.c	Fri Oct 24 14:13:53 2008 +0100
+++ b/src/console.c	Fri Oct 24 14:14:07 2008 +0100
@@ -37,7 +37,7 @@
 
 #include "console.h"
 #include "internal.h"
-#include "util-lib.h"
+#include "util.h"
 
 /* ie  Ctrl-]  as per telnet */
 #define CTRL_CLOSE_BRACKET '\35'
diff -r 5bd402f3762e src/libvirt_sym.version
--- a/src/libvirt_sym.version	Fri Oct 24 14:13:53 2008 +0100
+++ b/src/libvirt_sym.version	Fri Oct 24 14:14:07 2008 +0100
@@ -157,15 +157,6 @@
            guarentee provided for the public
            symbols above */
 
-	__virConfNew;
-	__virConfReadFile;
-	__virConfReadMem;
-	__virConfFree;
-	__virConfGetValue;
-	__virConfSetValue;
-	__virConfWriteFile;
-	__virConfWriteMem;
-
 	__virGetDomain;
 	__virGetNetwork;
 	__virGetStoragePool;
@@ -183,21 +174,5 @@
 	__virDomainMigratePerform;
 	__virDomainMigrateFinish;
 
-        __virFileReadAll;
-        __virStrToLong_i;
-        __virStrToLong_ull;
-
-        __virBufferVSprintf;
-        __virBufferAdd;
-        __virBufferAddChar;
-        __virBufferContentAndReset;
-        __virBufferError;
-
-	__virMacAddrCompare;
-
-        __virAlloc;
-        __virAllocN;
-        __virReallocN;
-        __virFree;
     local: *;
 };
diff -r 5bd402f3762e src/util-lib.c
--- a/src/util-lib.c	Fri Oct 24 14:13:53 2008 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,52 +0,0 @@
-/*
- * 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 -r 5bd402f3762e src/util-lib.h
--- a/src/util-lib.h	Fri Oct 24 14:13:53 2008 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,25 +0,0 @@
-/*
- * 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>
-
-/*
- * To avoid a double definition of the function when compiling
- * programs using both util-lib and libvirt, like virsh
- */
-#ifdef IN_LIBVIRT
-#define saferead libvirt_saferead
-#define safewrite libvirt_safewrite
-#endif
-
-int saferead(int fd, void *buf, size_t count);
-ssize_t safewrite(int fd, const void *buf, size_t count);
-
-#endif
diff -r 5bd402f3762e src/util.c
--- a/src/util.c	Fri Oct 24 14:13:53 2008 +0100
+++ b/src/util.c	Fri Oct 24 14:14:07 2008 +0100
@@ -52,7 +52,6 @@
 #include "buf.h"
 #include "util.h"
 #include "memory.h"
-#include "util-lib.c"
 
 #ifndef NSIG
 # define NSIG 32
@@ -502,7 +501,7 @@
 }
 
 /* Like virFileReadLimFP, but use a file descriptor rather than a FILE*.  */
-int __virFileReadLimFD(int fd_arg, int maxlen, char **buf)
+int virFileReadLimFD(int fd_arg, int maxlen, char **buf)
 {
     int fd = dup (fd_arg);
     if (fd >= 0) {
@@ -522,7 +521,7 @@
     return -1;
 }
 
-int __virFileReadAll(const char *path, int maxlen, char **buf)
+int virFileReadAll(const char *path, int maxlen, char **buf)
 {
     FILE *fh = fopen(path, "r");
     if (fh == NULL) {
@@ -812,7 +811,7 @@
    validity.  This function is careful to return -1 when the string S
    represents a number that is not representable as an "int". */
 int
-__virStrToLong_i(char const *s, char **end_ptr, int base, int *result)
+virStrToLong_i(char const *s, char **end_ptr, int base, int *result)
 {
     long int val;
     char *p;
@@ -869,7 +868,7 @@
 
 /* Just like virStrToLong_i, above, but produce an "unsigned long long" value.  */
 int
-__virStrToLong_ull(char const *s, char **end_ptr, int base, unsigned long long *result)
+virStrToLong_ull(char const *s, char **end_ptr, int base, unsigned long long *result)
 {
     unsigned long long val;
     char *p;
@@ -941,7 +940,7 @@
  * as well as leading zeros.
  */
 int
-__virMacAddrCompare (const char *p, const char *q)
+virMacAddrCompare (const char *p, const char *q)
 {
     unsigned char c, d;
     do {
@@ -1090,3 +1089,44 @@
 
     return idx;
 }
+
+
+/* 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 -r 5bd402f3762e src/util.h
--- a/src/util.h	Fri Oct 24 14:13:53 2008 +0100
+++ b/src/util.h	Fri Oct 24 14:14:07 2008 +0100
@@ -25,7 +25,6 @@
 #ifndef __VIR_UTIL_H__
 #define __VIR_UTIL_H__
 
-#include "util-lib.h"
 #include "verify.h"
 #include <sys/select.h>
 
@@ -46,11 +45,12 @@
             int flags);
 int virRun(virConnectPtr conn, const char *const*argv, int *status);
 
-int __virFileReadLimFD(int fd, int maxlen, char **buf);
-#define virFileReadLimFD(fd,m,b) __virFileReadLimFD((fd),(m),(b))
+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);
-#define virFileReadAll(p,m,b) __virFileReadAll((p),(m),(b))
+int virFileReadLimFD(int fd, int maxlen, char **buf);
+
+int virFileReadAll(const char *path, int maxlen, char **buf);
 
 int virFileMatchesNameSuffix(const char *file,
                              const char *name,
@@ -88,11 +88,10 @@
 int virFileDeletePid(const char *dir,
                      const char *name);
 
-int __virStrToLong_i(char const *s,
-                     char **end_ptr,
-                     int base,
-                     int *result);
-#define virStrToLong_i(s,e,b,r) __virStrToLong_i((s),(e),(b),(r))
+int virStrToLong_i(char const *s,
+                   char **end_ptr,
+                   int base,
+                   int *result);
 
 int virStrToLong_ui(char const *s,
                     char **end_ptr,
@@ -102,14 +101,12 @@
                     char **end_ptr,
                     int base,
                     long long *result);
-int __virStrToLong_ull(char const *s,
-                       char **end_ptr,
-                       int base,
-                       unsigned long long *result);
-#define virStrToLong_ull(s,e,b,r) __virStrToLong_ull((s),(e),(b),(r))
+int virStrToLong_ull(char const *s,
+                     char **end_ptr,
+                     int base,
+                     unsigned long long *result);
 
-int __virMacAddrCompare (const char *mac1, const char *mac2);
-#define virMacAddrCompare(mac1,mac2) __virMacAddrCompare((mac1),(mac2))
+int virMacAddrCompare (const char *mac1, const char *mac2);
 
 void virSkipSpaces(const char **str);
 int virParseNumber(const char **str);
diff -r 5bd402f3762e src/virsh.c
--- a/src/virsh.c	Fri Oct 24 14:13:53 2008 +0100
+++ b/src/virsh.c	Fri Oct 24 14:14:07 2008 +0100
@@ -47,7 +47,6 @@
 #include "buf.h"
 #include "console.h"
 #include "util.h"
-#include "util-lib.h"
 
 static char *progname;
 

-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|




More information about the libvir-list mailing list