[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen-unstable] libxc: Consolidate read()/write() syscall wrappers to read/write an



# HG changeset patch
# User Keir Fraser <keir@xxxxxxxxxxxxx>
# Date 1194805353 0
# Node ID f669bf5c67206b5b0be080e35203d5a40e418e7d
# Parent  a790ad6ef0732b8ce77cc7c81ac7e63f7bfb9dd5
libxc: Consolidate read()/write() syscall wrappers to read/write an
exact number of bytes. The consolidated versions are more watertight
than the various versions previously distributed around the library
source code.
Signed-off-by: Keir Fraser <keir@xxxxxxxxxxxxx>
---
 tools/libxc/ia64/xc_ia64_linux_restore.c |   51 +++++++---------------
 tools/libxc/ia64/xc_ia64_linux_save.c    |   44 +++++++------------
 tools/libxc/xc_core.c                    |   22 +++------
 tools/libxc/xc_domain_restore.c          |   70 +++++++++++--------------------
 tools/libxc/xc_domain_save.c             |   58 +++++++++++--------------
 tools/libxc/xc_linux.c                   |   29 ------------
 tools/libxc/xc_netbsd.c                  |   29 ------------
 tools/libxc/xc_private.c                 |   36 +++++++++++++++
 tools/libxc/xc_private.h                 |    4 +
 tools/libxc/xc_solaris.c                 |   29 ------------
 10 files changed, 140 insertions(+), 232 deletions(-)

diff -r a790ad6ef073 -r f669bf5c6720 tools/libxc/ia64/xc_ia64_linux_restore.c
--- a/tools/libxc/ia64/xc_ia64_linux_restore.c  Sat Nov 10 10:55:27 2007 +0000
+++ b/tools/libxc/ia64/xc_ia64_linux_restore.c  Sun Nov 11 18:22:33 2007 +0000
@@ -28,25 +28,6 @@ static unsigned long p2m_size;
 /* number of 'in use' pfns in the guest (i.e. #P2M entries with a valid mfn) */
 static unsigned long nr_pfns;
 
-static ssize_t
-read_exact(int fd, void *buf, size_t count)
-{
-    int r = 0, s;
-    unsigned char *b = buf;
-
-    while (r < count) {
-        s = read(fd, &b[r], count - r);
-        if ((s == -1) && (errno == EINTR))
-            continue;
-        if (s <= 0) {
-            break;
-        }
-        r += s;
-    }
-
-    return (r == count) ? 1 : 0;
-}
-
 static int
 populate_page_if_necessary(int xc_handle, uint32_t dom, unsigned long gmfn,
                            struct xen_ia64_p2m_table *p2m_table)
@@ -68,7 +49,7 @@ read_page(int xc_handle, int io_fd, uint
         ERROR("cannot map page");
         return -1;
     }
-    if (!read_exact(io_fd, mem, PAGE_SIZE)) {
+    if (read_exact(io_fd, mem, PAGE_SIZE)) {
         ERROR("Error when reading from state file (5)");
         munmap(mem, PAGE_SIZE);
         return -1;
@@ -93,7 +74,7 @@ xc_ia64_recv_unallocated_list(int xc_han
     unsigned long *pfntab = NULL;
     unsigned int nr_frees;
 
-    if (!read_exact(io_fd, &count, sizeof(count))) {
+    if (read_exact(io_fd, &count, sizeof(count))) {
         ERROR("Error when reading pfn count");
         goto out;
     }
@@ -104,7 +85,7 @@ xc_ia64_recv_unallocated_list(int xc_han
         goto out;
     }
 
-    if (!read_exact(io_fd, pfntab, sizeof(unsigned long)*count)) {
+    if (read_exact(io_fd, pfntab, sizeof(unsigned long)*count)) {
         ERROR("Error when reading pfntab");
         goto out;
     }
@@ -138,7 +119,7 @@ xc_ia64_recv_vcpu_context(int xc_handle,
 xc_ia64_recv_vcpu_context(int xc_handle, int io_fd, uint32_t dom,
                           uint32_t vcpu, vcpu_guest_context_t *ctxt)
 {
-    if (!read_exact(io_fd, ctxt, sizeof(*ctxt))) {
+    if (read_exact(io_fd, ctxt, sizeof(*ctxt))) {
         ERROR("Error when reading ctxt");
         return -1;
     }
@@ -182,7 +163,7 @@ xc_ia64_recv_shared_info(int xc_handle, 
         return -1;
     }
 
-    if (!read_exact(io_fd, shared_info, PAGE_SIZE)) {
+    if (read_exact(io_fd, shared_info, PAGE_SIZE)) {
         ERROR("Error when reading shared_info page");
         munmap(shared_info, PAGE_SIZE);
         return -1;
@@ -308,7 +289,7 @@ xc_ia64_hvm_recv_context(int xc_handle, 
         ERROR("Could not get domain info");
         goto out;
     }
-    if (!read_exact(io_fd, &max_virt_cpus, sizeof(max_virt_cpus))) {
+    if (read_exact(io_fd, &max_virt_cpus, sizeof(max_virt_cpus))) {
         ERROR("error reading max_virt_cpus");
         goto out;
     }
@@ -325,7 +306,7 @@ xc_ia64_hvm_recv_context(int xc_handle, 
         goto out;
     }
     memset(vcpumap, 0, vcpumap_size);
-    if (!read_exact(io_fd, vcpumap, vcpumap_size)) {
+    if (read_exact(io_fd, vcpumap, vcpumap_size)) {
         ERROR("read vcpumap");
         goto out;
     }
@@ -345,7 +326,7 @@ xc_ia64_hvm_recv_context(int xc_handle, 
     }    
 
     /* Set HVM-specific parameters */
-    if (!read_exact(io_fd, magic_pfns, sizeof(magic_pfns))) {
+    if (read_exact(io_fd, magic_pfns, sizeof(magic_pfns))) {
         ERROR("error reading magic page addresses");
         goto out;
     }
@@ -372,7 +353,7 @@ xc_ia64_hvm_recv_context(int xc_handle, 
     *store_mfn = magic_pfns[0];
 
     /* Read HVM context */
-    if (!read_exact(io_fd, &rec_size, sizeof(rec_size))) {
+    if (read_exact(io_fd, &rec_size, sizeof(rec_size))) {
         ERROR("error read hvm context size!\n");
         goto out;
     }
@@ -384,7 +365,7 @@ xc_ia64_hvm_recv_context(int xc_handle, 
         goto out;
     }
 
-    if (!read_exact(io_fd, hvm_buf, rec_size)) {
+    if (read_exact(io_fd, hvm_buf, rec_size)) {
         ERROR("error loading the HVM context");
         goto out;
     }
@@ -445,14 +426,14 @@ xc_domain_restore(int xc_handle, int io_
     /* For info only */
     nr_pfns = 0;
 
-    if ( !read_exact(io_fd, &p2m_size, sizeof(unsigned long)) )
+    if ( read_exact(io_fd, &p2m_size, sizeof(unsigned long)) )
     {
         ERROR("read: p2m_size");
         goto out;
     }
     DPRINTF("xc_linux_restore start: p2m_size = %lx\n", p2m_size);
 
-    if (!read_exact(io_fd, &ver, sizeof(unsigned long))) {
+    if (read_exact(io_fd, &ver, sizeof(unsigned long))) {
         ERROR("Error when reading version");
         goto out;
     }
@@ -461,7 +442,7 @@ xc_domain_restore(int xc_handle, int io_
         goto out;
     }
 
-    if (!read_exact(io_fd, &domctl.u.arch_setup, sizeof(domctl.u.arch_setup))) 
{
+    if (read_exact(io_fd, &domctl.u.arch_setup, sizeof(domctl.u.arch_setup))) {
         ERROR("read: domain setup");
         goto out;
     }
@@ -492,7 +473,7 @@ xc_domain_restore(int xc_handle, int io_
         unsigned long memmap_size;
         xen_ia64_memmap_info_t *memmap_info;
 
-        if (!read_exact(io_fd, &memmap_info_num_pages,
+        if (read_exact(io_fd, &memmap_info_num_pages,
                         sizeof(memmap_info_num_pages))) {
             ERROR("read: memmap_info_num_pages");
             goto out;
@@ -503,7 +484,7 @@ xc_domain_restore(int xc_handle, int io_
             ERROR("Could not allocate memory for memmap_info");
             goto out;
         }
-        if (!read_exact(io_fd, memmap_info, memmap_size)) {
+        if (read_exact(io_fd, memmap_info, memmap_size)) {
             ERROR("read: memmap_info");
             goto out;
         }
@@ -546,7 +527,7 @@ xc_domain_restore(int xc_handle, int io_
 
     while (1) {
         unsigned long gmfn;
-        if (!read_exact(io_fd, &gmfn, sizeof(unsigned long))) {
+        if (read_exact(io_fd, &gmfn, sizeof(unsigned long))) {
             ERROR("Error when reading batch size");
             goto out;
         }
diff -r a790ad6ef073 -r f669bf5c6720 tools/libxc/ia64/xc_ia64_linux_save.c
--- a/tools/libxc/ia64/xc_ia64_linux_save.c     Sat Nov 10 10:55:27 2007 +0000
+++ b/tools/libxc/ia64/xc_ia64_linux_save.c     Sun Nov 11 18:22:33 2007 +0000
@@ -79,14 +79,6 @@ static int xc_ia64_shadow_control(int xc
                              dirty_bitmap, pages, NULL, 0, stats);
 }
 
-static inline ssize_t
-write_exact(int fd, void *buf, size_t count)
-{
-    if (write(fd, buf, count) != count)
-        return 0;
-    return 1;
-}
-
 static int
 suspend_and_state(int (*suspend)(int), int xc_handle, int io_fd,
                   int dom, xc_dominfo_t *info)
@@ -174,7 +166,7 @@ xc_ia64_send_unallocated_list(int xc_han
                 j++;
         }
     }
-    if (!write_exact(io_fd, &j, sizeof(unsigned int))) {
+    if (write_exact(io_fd, &j, sizeof(unsigned int))) {
         ERROR("Error when writing to state file (6a)");
         return -1;
     }
@@ -195,7 +187,7 @@ xc_ia64_send_unallocated_list(int xc_han
             if (!xc_ia64_p2m_allocated(p2m_table, N))
                 pfntab[j++] = N;
             if (j == sizeof(pfntab)/sizeof(pfntab[0])) {
-                if (!write_exact(io_fd, &pfntab, sizeof(pfntab[0]) * j)) {
+                if (write_exact(io_fd, &pfntab, sizeof(pfntab[0]) * j)) {
                     ERROR("Error when writing to state file (6b)");
                     return -1;
                 }
@@ -204,7 +196,7 @@ xc_ia64_send_unallocated_list(int xc_han
         }
     }
     if (j > 0) {
-        if (!write_exact(io_fd, &pfntab, sizeof(pfntab[0]) * j)) {
+        if (write_exact(io_fd, &pfntab, sizeof(pfntab[0]) * j)) {
             ERROR("Error when writing to state file (6c)");
             return -1;
         }
@@ -222,7 +214,7 @@ xc_ia64_send_vcpu_context(int xc_handle,
         return -1;
     }
 
-    if (!write_exact(io_fd, ctxt, sizeof(*ctxt))) {
+    if (write_exact(io_fd, ctxt, sizeof(*ctxt))) {
         ERROR("Error when writing to state file (1)");
         return -1;
     }
@@ -234,7 +226,7 @@ static int
 static int
 xc_ia64_send_shared_info(int xc_handle, int io_fd, shared_info_t *live_shinfo)
 {
-    if (!write_exact(io_fd, live_shinfo, PAGE_SIZE)) {
+    if (write_exact(io_fd, live_shinfo, PAGE_SIZE)) {
         ERROR("Error when writing to state file (1)");
         return -1;
     }
@@ -258,7 +250,7 @@ xc_ia64_pv_send_context(int xc_handle, i
         ERROR("cannot map privreg page");
         return -1;
     }
-    if (!write_exact(io_fd, mem, PAGE_SIZE)) {
+    if (write_exact(io_fd, mem, PAGE_SIZE)) {
         ERROR("Error when writing privreg to state file (5)");
         munmap(mem, PAGE_SIZE);
         return -1;
@@ -319,12 +311,12 @@ xc_ia64_hvm_send_context(int xc_handle, 
             __set_bit(i, vcpumap);
     }
 
-    if (!write_exact(io_fd, &max_virt_cpus, sizeof(max_virt_cpus))) {
+    if (write_exact(io_fd, &max_virt_cpus, sizeof(max_virt_cpus))) {
         ERROR("write max_virt_cpus");
         goto out;
     }
 
-    if (!write_exact(io_fd, vcpumap, vcpumap_size)) {
+    if (write_exact(io_fd, vcpumap, vcpumap_size)) {
         ERROR("write vcpumap");
         goto out;
     }
@@ -352,7 +344,7 @@ xc_ia64_hvm_send_context(int xc_handle, 
         }
     }
 
-    if (!write_exact(io_fd, magic_pfns, sizeof(magic_pfns))) {
+    if (write_exact(io_fd, magic_pfns, sizeof(magic_pfns))) {
         ERROR("Error when writing to state file (7)");
         goto out;
     }
@@ -377,12 +369,12 @@ xc_ia64_hvm_send_context(int xc_handle, 
         goto out;
     }
         
-    if (!write_exact(io_fd, &rec_size, sizeof(rec_size))) {
+    if (write_exact(io_fd, &rec_size, sizeof(rec_size))) {
         ERROR("error write hvm buffer size");
         goto out;
     }
         
-    if (!write_exact(io_fd, hvm_buf, rec_size)) {
+    if (write_exact(io_fd, hvm_buf, rec_size)) {
         ERROR("write HVM info failed!\n");
         goto out;
     }
@@ -496,7 +488,7 @@ xc_domain_save(int xc_handle, int io_fd,
     p2m_size = xc_memory_op(xc_handle, XENMEM_maximum_gpfn, &dom);
 
     /* This is expected by xm restore.  */
-    if (!write_exact(io_fd, &p2m_size, sizeof(unsigned long))) {
+    if (write_exact(io_fd, &p2m_size, sizeof(unsigned long))) {
         ERROR("write: p2m_size");
         goto out;
     }
@@ -509,7 +501,7 @@ xc_domain_save(int xc_handle, int io_fd,
     {
         unsigned long version = XC_IA64_SR_FORMAT_VER_CURRENT;
 
-        if (!write_exact(io_fd, &version, sizeof(unsigned long))) {
+        if (write_exact(io_fd, &version, sizeof(unsigned long))) {
             ERROR("write: version");
             goto out;
         }
@@ -522,7 +514,7 @@ xc_domain_save(int xc_handle, int io_fd,
         ERROR("Could not get domain setup");
         goto out;
     }
-    if (!write_exact(io_fd, &domctl.u.arch_setup,
+    if (write_exact(io_fd, &domctl.u.arch_setup,
                      sizeof(domctl.u.arch_setup))) {
         ERROR("write: domain setup");
         goto out;
@@ -605,12 +597,12 @@ xc_domain_save(int xc_handle, int io_fd,
         PERROR("xc_ia64_p2m_map");
         goto out;
     }
-    if (!write_exact(io_fd,
+    if (write_exact(io_fd,
                      &memmap_info_num_pages, sizeof(memmap_info_num_pages))) {
         PERROR("write: arch.memmap_info_num_pages");
         goto out;
     }
-    if (!write_exact(io_fd, memmap_info, memmap_size)) {
+    if (write_exact(io_fd, memmap_info, memmap_size)) {
         PERROR("write: memmap_info");
         goto out;
     }
@@ -679,7 +671,7 @@ xc_domain_save(int xc_handle, int io_fd,
                     continue;
                 }
 
-                if (!write_exact(io_fd, &N, sizeof(N))) {
+                if (write_exact(io_fd, &N, sizeof(N))) {
                     ERROR("write: p2m_size");
                     munmap(mem, PAGE_SIZE);
                     goto out;
@@ -751,7 +743,7 @@ xc_domain_save(int xc_handle, int io_fd,
     /* terminate */
     {
         unsigned long pfn = INVALID_MFN;
-        if (!write_exact(io_fd, &pfn, sizeof(pfn))) {
+        if (write_exact(io_fd, &pfn, sizeof(pfn))) {
             ERROR("Error when writing to state file (6)");
             goto out;
         }
diff -r a790ad6ef073 -r f669bf5c6720 tools/libxc/xc_core.c
--- a/tools/libxc/xc_core.c     Sat Nov 10 10:55:27 2007 +0000
+++ b/tools/libxc/xc_core.c     Sun Nov 11 18:22:33 2007 +0000
@@ -860,19 +860,15 @@ static int local_file_dump(void *args, c
 static int local_file_dump(void *args, char *buffer, unsigned int length)
 {
     struct dump_args *da = args;
-    int bytes, offset;
-
-    for ( offset = 0; offset < length; offset += bytes )
-    {
-        bytes = write(da->fd, &buffer[offset], length-offset);
-        if ( bytes <= 0 )
-        {
-            PERROR("Failed to write buffer");
-            return -errno;
-        }
-    }
-
-    if (length >= DUMP_INCREMENT*PAGE_SIZE) {
+
+    if ( write_exact(da->fd, buffer, length) == -1 )
+    {
+        PERROR("Failed to write buffer");
+        return -errno;
+    }
+
+    if ( length >= (DUMP_INCREMENT * PAGE_SIZE) )
+    {
         // Now dumping pages -- make sure we discard clean pages from
         // the cache after each write
         discard_file_cache(da->fd, 0 /* no flush */);
diff -r a790ad6ef073 -r f669bf5c6720 tools/libxc/xc_domain_restore.c
--- a/tools/libxc/xc_domain_restore.c   Sat Nov 10 10:55:27 2007 +0000
+++ b/tools/libxc/xc_domain_restore.c   Sun Nov 11 18:22:33 2007 +0000
@@ -59,26 +59,6 @@ static xen_pfn_t *p2m_batch = NULL;
 /* Address size of the guest, in bytes */
 unsigned int guest_width;
 
-
-static ssize_t
-read_exact(int fd, void *buf, size_t count)
-{
-    int r = 0, s;
-    unsigned char *b = buf;
-
-    while ( r < count )
-    {
-        s = read(fd, &b[r], count - r);
-        if ( (s == -1) && (errno == EINTR) )
-            continue;
-        if ( s <= 0 )
-            break;
-        r += s;
-    }
-
-    return (r == count);
-}
-
 /*
 ** In the state file (or during transfer), all page-table pages are
 ** converted into a 'canonical' form where references to actual mfns
@@ -177,7 +157,7 @@ static xen_pfn_t *load_p2m_frame_list(
     xen_pfn_t p2m_fl_zero;
 
     /* Read first entry of P2M list, or extended-info signature (~0UL). */
-    if ( !read_exact(io_fd, &p2m_fl_zero, sizeof(long)) )
+    if ( read_exact(io_fd, &p2m_fl_zero, sizeof(long)) )
     {
         ERROR("read extended-info signature failed");
         return NULL;
@@ -188,7 +168,7 @@ static xen_pfn_t *load_p2m_frame_list(
         uint32_t tot_bytes;
         
         /* Next 4 bytes: total size of following extended info. */
-        if ( !read_exact(io_fd, &tot_bytes, sizeof(tot_bytes)) )
+        if ( read_exact(io_fd, &tot_bytes, sizeof(tot_bytes)) )
         {
             ERROR("read extended-info size failed");
             return NULL;
@@ -200,8 +180,8 @@ static xen_pfn_t *load_p2m_frame_list(
             char     chunk_sig[4];
             
             /* 4-character chunk signature + 4-byte remaining chunk size. */
-            if ( !read_exact(io_fd, chunk_sig, sizeof(chunk_sig)) ||
-                 !read_exact(io_fd, &chunk_bytes, sizeof(chunk_bytes)) ||
+            if ( read_exact(io_fd, chunk_sig, sizeof(chunk_sig)) ||
+                 read_exact(io_fd, &chunk_bytes, sizeof(chunk_bytes)) ||
                  (tot_bytes < (chunk_bytes + 8)) )
             {
                 ERROR("read extended-info chunk signature failed");
@@ -230,7 +210,7 @@ static xen_pfn_t *load_p2m_frame_list(
                     return NULL;
                 }
 
-                if ( !read_exact(io_fd, &ctxt, chunk_bytes) )
+                if ( read_exact(io_fd, &ctxt, chunk_bytes) )
                 {
                     ERROR("read extended-info vcpu context failed");
                     return NULL;
@@ -251,7 +231,7 @@ static xen_pfn_t *load_p2m_frame_list(
             while ( chunk_bytes )
             {
                 unsigned long sz = MIN(chunk_bytes, sizeof(xen_pfn_t));
-                if ( !read_exact(io_fd, &p2m_fl_zero, sz) )
+                if ( read_exact(io_fd, &p2m_fl_zero, sz) )
                 {
                     ERROR("read-and-discard extended-info chunk bytes failed");
                     return NULL;
@@ -262,7 +242,7 @@ static xen_pfn_t *load_p2m_frame_list(
         }
 
         /* Now read the real first entry of P2M list. */
-        if ( !read_exact(io_fd, &p2m_fl_zero, sizeof(xen_pfn_t)) )
+        if ( read_exact(io_fd, &p2m_fl_zero, sizeof(xen_pfn_t)) )
         {
             ERROR("read first entry of p2m_frame_list failed");
             return NULL;
@@ -279,8 +259,8 @@ static xen_pfn_t *load_p2m_frame_list(
 
     /* First entry has already been read. */
     p2m_frame_list[0] = p2m_fl_zero;
-    if ( !read_exact(io_fd, &p2m_frame_list[1], 
-                     (P2M_FL_ENTRIES - 1) * sizeof(xen_pfn_t)) )
+    if ( read_exact(io_fd, &p2m_frame_list[1], 
+                    (P2M_FL_ENTRIES - 1) * sizeof(xen_pfn_t)) )
     {
         ERROR("read p2m_frame_list failed");
         return NULL;
@@ -349,7 +329,7 @@ int xc_domain_restore(int xc_handle, int
     /* For info only */
     nr_pfns = 0;
 
-    if ( !read_exact(io_fd, &p2m_size, sizeof(unsigned long)) )
+    if ( read_exact(io_fd, &p2m_size, sizeof(unsigned long)) )
     {
         ERROR("read: p2m_size");
         goto out;
@@ -464,7 +444,7 @@ int xc_domain_restore(int xc_handle, int
             prev_pc = this_pc;
         }
 
-        if ( !read_exact(io_fd, &j, sizeof(int)) )
+        if ( read_exact(io_fd, &j, sizeof(int)) )
         {
             ERROR("Error when reading batch size");
             goto out;
@@ -482,9 +462,9 @@ int xc_domain_restore(int xc_handle, int
         if ( j == -2 )
         {
             new_ctxt_format = 1;
-            if ( !read_exact(io_fd, &max_vcpu_id, sizeof(int)) ||
+            if ( read_exact(io_fd, &max_vcpu_id, sizeof(int)) ||
                  (max_vcpu_id >= 64) ||
-                 !read_exact(io_fd, &vcpumap, sizeof(uint64_t)) )
+                 read_exact(io_fd, &vcpumap, sizeof(uint64_t)) )
             {
                 ERROR("Error when reading max_vcpu_id");
                 goto out;
@@ -501,7 +481,7 @@ int xc_domain_restore(int xc_handle, int
             goto out;
         }
 
-        if ( !read_exact(io_fd, region_pfn_type, j*sizeof(unsigned long)) )
+        if ( read_exact(io_fd, region_pfn_type, j*sizeof(unsigned long)) )
         {
             ERROR("Error when reading region pfn types");
             goto out;
@@ -594,7 +574,7 @@ int xc_domain_restore(int xc_handle, int
             /* In verify mode, we use a copy; otherwise we work in place */
             page = verify ? (void *)buf : (region_base + i*PAGE_SIZE);
 
-            if ( !read_exact(io_fd, page, PAGE_SIZE) )
+            if ( read_exact(io_fd, page, PAGE_SIZE) )
             {
                 ERROR("Error when reading page (type was %lx)", pagetype);
                 goto out;
@@ -704,7 +684,7 @@ int xc_domain_restore(int xc_handle, int
         uint32_t rec_len;
 
         /* Set HVM-specific parameters */
-        if ( !read_exact(io_fd, magic_pfns, sizeof(magic_pfns)) )
+        if ( read_exact(io_fd, magic_pfns, sizeof(magic_pfns)) )
         {
             ERROR("error reading magic page addresses");
             goto out;
@@ -737,7 +717,7 @@ int xc_domain_restore(int xc_handle, int
         *store_mfn = magic_pfns[2];
 
         /* Read HVM context */
-        if ( !read_exact(io_fd, &rec_len, sizeof(uint32_t)) )
+        if ( read_exact(io_fd, &rec_len, sizeof(uint32_t)) )
         {
             ERROR("error read hvm context size!\n");
             goto out;
@@ -751,7 +731,7 @@ int xc_domain_restore(int xc_handle, int
             goto out;
         }
         
-        if ( !read_exact(io_fd, hvm_buf, rec_len) )
+        if ( read_exact(io_fd, hvm_buf, rec_len) )
         {
             ERROR("error loading the HVM context");
             goto out;
@@ -943,7 +923,7 @@ int xc_domain_restore(int xc_handle, int
         unsigned long *pfntab;
         int nr_frees;
 
-        if ( !read_exact(io_fd, &count, sizeof(count)) ||
+        if ( read_exact(io_fd, &count, sizeof(count)) ||
              (count > (1U << 28)) ) /* up to 1TB of address space */
         {
             ERROR("Error when reading pfn count (= %u)", count);
@@ -956,7 +936,7 @@ int xc_domain_restore(int xc_handle, int
             goto out;
         }
 
-        if ( !read_exact(io_fd, pfntab, sizeof(unsigned long)*count) )
+        if ( read_exact(io_fd, pfntab, sizeof(unsigned long)*count) )
         {
             ERROR("Error when reading pfntab");
             goto out;
@@ -1001,9 +981,9 @@ int xc_domain_restore(int xc_handle, int
         if ( !(vcpumap & (1ULL << i)) )
             continue;
 
-        if ( !read_exact(io_fd, &ctxt, ((guest_width == 8)
-                                        ? sizeof(ctxt.x64)
-                                        : sizeof(ctxt.x32))) )
+        if ( read_exact(io_fd, &ctxt, ((guest_width == 8)
+                                       ? sizeof(ctxt.x64)
+                                       : sizeof(ctxt.x32))) )
         {
             ERROR("Error when reading ctxt %d", i);
             goto out;
@@ -1112,7 +1092,7 @@ int xc_domain_restore(int xc_handle, int
 
         if ( !ext_vcpucontext )
             continue;
-        if ( !read_exact(io_fd, &domctl.u.ext_vcpucontext, 128) ||
+        if ( read_exact(io_fd, &domctl.u.ext_vcpucontext, 128) ||
              (domctl.u.ext_vcpucontext.vcpu != i) )
         {
             ERROR("Error when reading extended ctxt %d", i);
@@ -1128,7 +1108,7 @@ int xc_domain_restore(int xc_handle, int
         }
     }
 
-    if ( !read_exact(io_fd, shared_info_page, PAGE_SIZE) )
+    if ( read_exact(io_fd, shared_info_page, PAGE_SIZE) )
     {
         ERROR("Error when reading shared info page");
         goto out;
diff -r a790ad6ef073 -r f669bf5c6720 tools/libxc/xc_domain_save.c
--- a/tools/libxc/xc_domain_save.c      Sat Nov 10 10:55:27 2007 +0000
+++ b/tools/libxc/xc_domain_save.c      Sun Nov 11 18:22:33 2007 +0000
@@ -175,8 +175,7 @@ static int noncached_write(int fd, int l
 static int noncached_write(int fd, int live, void *buffer, int len) 
 {
     static int write_count = 0;
-
-    int rc = write(fd,buffer,len);
+    int rc = (write_exact(fd, buffer, len) == 0) ? len : -1;
 
     write_count += len;
     if ( write_count >= (MAX_PAGECACHE_USAGE * PAGE_SIZE) )
@@ -281,11 +280,6 @@ static int ratewrite(int io_fd, int live
 
 #endif
 
-static inline ssize_t write_exact(int fd, void *buf, size_t count)
-{
-    return (write(fd, buf, count) == count);
-}
-
 static int print_stats(int xc_handle, uint32_t domid, int pages_sent,
                        xc_shadow_op_stats_t *stats, int print)
 {
@@ -782,21 +776,21 @@ static xen_pfn_t *map_and_save_p2m_table
                               : sizeof(ctxt.x32));
         uint32_t chunk2_sz = 0;
         uint32_t tot_sz    = (chunk1_sz + 8) + (chunk2_sz + 8);
-        if ( !write_exact(io_fd, &signature, sizeof(signature)) ||
-             !write_exact(io_fd, &tot_sz, sizeof(tot_sz)) ||
-             !write_exact(io_fd, "vcpu", 4) ||
-             !write_exact(io_fd, &chunk1_sz, sizeof(chunk1_sz)) ||
-             !write_exact(io_fd, &ctxt, chunk1_sz) ||
-             !write_exact(io_fd, "extv", 4) ||
-             !write_exact(io_fd, &chunk2_sz, sizeof(chunk2_sz)) )
+        if ( write_exact(io_fd, &signature, sizeof(signature)) ||
+             write_exact(io_fd, &tot_sz, sizeof(tot_sz)) ||
+             write_exact(io_fd, "vcpu", 4) ||
+             write_exact(io_fd, &chunk1_sz, sizeof(chunk1_sz)) ||
+             write_exact(io_fd, &ctxt, chunk1_sz) ||
+             write_exact(io_fd, "extv", 4) ||
+             write_exact(io_fd, &chunk2_sz, sizeof(chunk2_sz)) )
         {
             ERROR("write: extended info");
             goto out;
         }
     }
 
-    if ( !write_exact(io_fd, p2m_frame_list, 
-                      P2M_FL_ENTRIES * sizeof(xen_pfn_t)) )
+    if ( write_exact(io_fd, p2m_frame_list, 
+                     P2M_FL_ENTRIES * sizeof(xen_pfn_t)) )
     {
         ERROR("write: p2m_frame_list");
         goto out;
@@ -1044,7 +1038,7 @@ int xc_domain_save(int xc_handle, int io
     }
 
     /* Start writing out the saved-domain record. */
-    if ( !write_exact(io_fd, &p2m_size, sizeof(unsigned long)) )
+    if ( write_exact(io_fd, &p2m_size, sizeof(unsigned long)) )
     {
         ERROR("write: p2m_size");
         goto out;
@@ -1249,14 +1243,14 @@ int xc_domain_save(int xc_handle, int io
                 }
             }
 
-            if ( !write_exact(io_fd, &batch, sizeof(unsigned int)) )
+            if ( write_exact(io_fd, &batch, sizeof(unsigned int)) )
             {
                 ERROR("Error when writing to state file (2) (errno %d)",
                       errno);
                 goto out;
             }
 
-            if ( !write_exact(io_fd, pfn_type, sizeof(unsigned long)*batch) )
+            if ( write_exact(io_fd, pfn_type, sizeof(unsigned long)*batch) )
             {
                 ERROR("Error when writing to state file (3) (errno %d)",
                       errno);
@@ -1342,7 +1336,7 @@ int xc_domain_save(int xc_handle, int io
             DPRINTF("Entering debug resend-all mode\n");
 
             /* send "-1" to put receiver into debug mode */
-            if ( !write_exact(io_fd, &minusone, sizeof(int)) )
+            if ( write_exact(io_fd, &minusone, sizeof(int)) )
             {
                 ERROR("Error when writing to state file (6) (errno %d)",
                       errno);
@@ -1434,7 +1428,7 @@ int xc_domain_save(int xc_handle, int io
         }
 
         chunk.vcpumap = vcpumap;
-        if ( !write_exact(io_fd, &chunk, sizeof(chunk)) )
+        if ( write_exact(io_fd, &chunk, sizeof(chunk)) )
         {
             ERROR("Error when writing to state file (errno %d)", errno);
             goto out;
@@ -1443,7 +1437,7 @@ int xc_domain_save(int xc_handle, int io
 
     /* Zero terminate */
     i = 0;
-    if ( !write_exact(io_fd, &i, sizeof(int)) )
+    if ( write_exact(io_fd, &i, sizeof(int)) )
     {
         ERROR("Error when writing to state file (6') (errno %d)", errno);
         goto out;
@@ -1461,7 +1455,7 @@ int xc_domain_save(int xc_handle, int io
                          (unsigned long *)&magic_pfns[1]);
         xc_get_hvm_param(xc_handle, dom, HVM_PARAM_STORE_PFN,
                          (unsigned long *)&magic_pfns[2]);
-        if ( !write_exact(io_fd, magic_pfns, sizeof(magic_pfns)) )
+        if ( write_exact(io_fd, magic_pfns, sizeof(magic_pfns)) )
         {
             ERROR("Error when writing to state file (7)");
             goto out;
@@ -1475,13 +1469,13 @@ int xc_domain_save(int xc_handle, int io
             goto out;
         }
         
-        if ( !write_exact(io_fd, &rec_size, sizeof(uint32_t)) )
+        if ( write_exact(io_fd, &rec_size, sizeof(uint32_t)) )
         {
             ERROR("error write hvm buffer size");
             goto out;
         }
         
-        if ( !write_exact(io_fd, hvm_buf, rec_size) )
+        if ( write_exact(io_fd, hvm_buf, rec_size) )
         {
             ERROR("write HVM info failed!\n");
             goto out;
@@ -1505,7 +1499,7 @@ int xc_domain_save(int xc_handle, int io
                 j++;
         }
 
-        if ( !write_exact(io_fd, &j, sizeof(unsigned int)) )
+        if ( write_exact(io_fd, &j, sizeof(unsigned int)) )
         {
             ERROR("Error when writing to state file (6a) (errno %d)", errno);
             goto out;
@@ -1519,7 +1513,7 @@ int xc_domain_save(int xc_handle, int io
             i++;
             if ( (j == 1024) || (i == p2m_size) )
             {
-                if ( !write_exact(io_fd, &pfntab, sizeof(unsigned long)*j) )
+                if ( write_exact(io_fd, &pfntab, sizeof(unsigned long)*j) )
                 {
                     ERROR("Error when writing to state file (6b) (errno %d)",
                           errno);
@@ -1596,9 +1590,9 @@ int xc_domain_save(int xc_handle, int io
                     mfn_to_pfn(xen_cr3_to_pfn(ctxt.x64.ctrlreg[1])));
         }
 
-        if ( !write_exact(io_fd, &ctxt, ((guest_width==8) 
-                                         ? sizeof(ctxt.x64) 
-                                         : sizeof(ctxt.x32))) )
+        if ( write_exact(io_fd, &ctxt, ((guest_width==8) 
+                                        ? sizeof(ctxt.x64) 
+                                        : sizeof(ctxt.x32))) )
         {
             ERROR("Error when writing to state file (1) (errno %d)", errno);
             goto out;
@@ -1612,7 +1606,7 @@ int xc_domain_save(int xc_handle, int io
             ERROR("No extended context for VCPU%d", i);
             goto out;
         }
-        if ( !write_exact(io_fd, &domctl.u.ext_vcpucontext, 128) )
+        if ( write_exact(io_fd, &domctl.u.ext_vcpucontext, 128) )
         {
             ERROR("Error when writing to state file (2) (errno %d)", errno);
             goto out;
@@ -1625,7 +1619,7 @@ int xc_domain_save(int xc_handle, int io
     memcpy(page, live_shinfo, PAGE_SIZE);
     SET_FIELD(((shared_info_either_t *)page), 
               arch.pfn_to_mfn_frame_list_list, 0);
-    if ( !write_exact(io_fd, page, PAGE_SIZE) )
+    if ( write_exact(io_fd, page, PAGE_SIZE) )
     {
         ERROR("Error when writing to state file (1) (errno %d)", errno);
         goto out;
diff -r a790ad6ef073 -r f669bf5c6720 tools/libxc/xc_linux.c
--- a/tools/libxc/xc_linux.c    Sat Nov 10 10:55:27 2007 +0000
+++ b/tools/libxc/xc_linux.c    Sun Nov 11 18:22:33 2007 +0000
@@ -295,37 +295,12 @@ int xc_evtchn_unbind(int xce_handle, evt
     return ioctl(xce_handle, IOCTL_EVTCHN_UNBIND, &unbind);
 }
 
-static int dorw(int fd, char *data, size_t size, int do_write)
-{
-    size_t offset = 0;
-    ssize_t len;
-
-    while ( offset < size )
-    {
-        if (do_write)
-            len = write(fd, data + offset, size - offset);
-        else
-            len = read(fd, data + offset, size - offset);
-
-        if ( len == -1 )
-        {
-             if ( errno == EINTR )
-                 continue;
-             return -1;
-        }
-
-        offset += len;
-    }
-
-    return 0;
-}
-
 evtchn_port_or_error_t
 xc_evtchn_pending(int xce_handle)
 {
     evtchn_port_t port;
 
-    if ( dorw(xce_handle, (char *)&port, sizeof(port), 0) == -1 )
+    if ( read_exact(xce_handle, (char *)&port, sizeof(port)) == -1 )
         return -1;
 
     return port;
@@ -333,7 +308,7 @@ xc_evtchn_pending(int xce_handle)
 
 int xc_evtchn_unmask(int xce_handle, evtchn_port_t port)
 {
-    return dorw(xce_handle, (char *)&port, sizeof(port), 1);
+    return write_exact(xce_handle, (char *)&port, sizeof(port));
 }
 
 /* Optionally flush file to disk and discard page cache */
diff -r a790ad6ef073 -r f669bf5c6720 tools/libxc/xc_netbsd.c
--- a/tools/libxc/xc_netbsd.c   Sat Nov 10 10:55:27 2007 +0000
+++ b/tools/libxc/xc_netbsd.c   Sun Nov 11 18:22:33 2007 +0000
@@ -209,37 +209,12 @@ xc_evtchn_bind_virq(int xce_handle, unsi
        return bind.port;
 }
 
-static int dorw(int fd, char *data, size_t size, int do_write)
-{
-    size_t offset = 0;
-    ssize_t len;
-
-    while ( offset < size )
-    {
-        if (do_write)
-            len = write(fd, data + offset, size - offset);
-        else
-            len = read(fd, data + offset, size - offset);
-
-        if ( len == -1 )
-        {
-             if ( errno == EINTR )
-                 continue;
-             return -1;
-        }
-
-        offset += len;
-    }
-
-    return 0;
-}
-
 evtchn_port_or_error_t
 xc_evtchn_pending(int xce_handle)
 {
     evtchn_port_t port;
 
-    if ( dorw(xce_handle, (char *)&port, sizeof(port), 0) == -1 )
+    if ( read_exact(xce_handle, (char *)&port, sizeof(port)) == -1 )
         return -1;
 
     return port;
@@ -247,7 +222,7 @@ xc_evtchn_pending(int xce_handle)
 
 int xc_evtchn_unmask(int xce_handle, evtchn_port_t port)
 {
-    return dorw(xce_handle, (char *)&port, sizeof(port), 1);
+    return write_exact(xce_handle, (char *)&port, sizeof(port));
 }
 
 /* Optionally flush file to disk and discard page cache */
diff -r a790ad6ef073 -r f669bf5c6720 tools/libxc/xc_private.c
--- a/tools/libxc/xc_private.c  Sat Nov 10 10:55:27 2007 +0000
+++ b/tools/libxc/xc_private.c  Sun Nov 11 18:22:33 2007 +0000
@@ -581,6 +581,42 @@ void bitmap_byte_to_64(uint64_t *lp, con
     }
 }
 
+int read_exact(int fd, void *data, size_t size)
+{
+    size_t offset = 0;
+    ssize_t len;
+
+    while ( offset < size )
+    {
+        len = read(fd, (char *)data + offset, size - offset);
+        if ( (len == -1) && (errno == EINTR) )
+            continue;
+        if ( len <= 0 )
+            return -1;
+        offset += len;
+    }
+
+    return 0;
+}
+
+int write_exact(int fd, const void *data, size_t size)
+{
+    size_t offset = 0;
+    ssize_t len;
+
+    while ( offset < size )
+    {
+        len = write(fd, (const char *)data + offset, size - offset);
+        if ( (len == -1) && (errno == EINTR) )
+            continue;
+        if ( len <= 0 )
+            return -1;
+        offset += len;
+    }
+
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
diff -r a790ad6ef073 -r f669bf5c6720 tools/libxc/xc_private.h
--- a/tools/libxc/xc_private.h  Sat Nov 10 10:55:27 2007 +0000
+++ b/tools/libxc/xc_private.h  Sun Nov 11 18:22:33 2007 +0000
@@ -180,4 +180,8 @@ int xc_add_mmu_update(int xc_handle, str
                    unsigned long long ptr, unsigned long long val);
 int xc_flush_mmu_updates(int xc_handle, struct xc_mmu *mmu);
 
+/* Return 0 on success; -1 on error. */
+int read_exact(int fd, void *data, size_t size);
+int write_exact(int fd, const void *data, size_t size);
+
 #endif /* __XC_PRIVATE_H__ */
diff -r a790ad6ef073 -r f669bf5c6720 tools/libxc/xc_solaris.c
--- a/tools/libxc/xc_solaris.c  Sat Nov 10 10:55:27 2007 +0000
+++ b/tools/libxc/xc_solaris.c  Sun Nov 11 18:22:33 2007 +0000
@@ -206,37 +206,12 @@ int xc_evtchn_unbind(int xce_handle, evt
     return ioctl(xce_handle, IOCTL_EVTCHN_UNBIND, &unbind);
 }
 
-static int dorw(int fd, char *data, size_t size, int do_write)
-{
-    size_t offset = 0;
-    ssize_t len;
-
-    while ( offset < size )
-    {
-        if (do_write)
-            len = write(fd, data + offset, size - offset);
-        else
-            len = read(fd, data + offset, size - offset);
-
-        if ( len == -1 )
-        {
-             if ( errno == EINTR )
-                 continue;
-             return -1;
-        }
-
-        offset += len;
-    }
-
-    return 0;
-}
-
 evtchn_port_or_error_t
 xc_evtchn_pending(int xce_handle)
 {
     evtchn_port_t port;
 
-    if ( dorw(xce_handle, (char *)&port, sizeof(port), 0) == -1 )
+    if ( read_exact(xce_handle, (char *)&port, sizeof(port)) == -1 )
         return -1;
 
     return port;
@@ -244,7 +219,7 @@ xc_evtchn_pending(int xce_handle)
 
 int xc_evtchn_unmask(int xce_handle, evtchn_port_t port)
 {
-    return dorw(xce_handle, (char *)&port, sizeof(port), 1);
+    return write_exact(xce_handle, (char *)&port, sizeof(port));
 }
 
 /* Optionally flush file to disk and discard page cache */

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.