debuggers.hg
changeset 16408:f669bf5c6720
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@xensource.com>
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@xensource.com>
author | Keir Fraser <keir@xensource.com> |
---|---|
date | Sun Nov 11 18:22:33 2007 +0000 (2007-11-11) |
parents | a790ad6ef073 |
children | dfca1120813f |
files | tools/libxc/ia64/xc_ia64_linux_restore.c tools/libxc/ia64/xc_ia64_linux_save.c tools/libxc/xc_core.c tools/libxc/xc_domain_restore.c tools/libxc/xc_domain_save.c tools/libxc/xc_linux.c tools/libxc/xc_netbsd.c tools/libxc/xc_private.c tools/libxc/xc_private.h tools/libxc/xc_solaris.c |
line diff
1.1 --- a/tools/libxc/ia64/xc_ia64_linux_restore.c Sat Nov 10 10:55:27 2007 +0000 1.2 +++ b/tools/libxc/ia64/xc_ia64_linux_restore.c Sun Nov 11 18:22:33 2007 +0000 1.3 @@ -28,25 +28,6 @@ static unsigned long p2m_size; 1.4 /* number of 'in use' pfns in the guest (i.e. #P2M entries with a valid mfn) */ 1.5 static unsigned long nr_pfns; 1.6 1.7 -static ssize_t 1.8 -read_exact(int fd, void *buf, size_t count) 1.9 -{ 1.10 - int r = 0, s; 1.11 - unsigned char *b = buf; 1.12 - 1.13 - while (r < count) { 1.14 - s = read(fd, &b[r], count - r); 1.15 - if ((s == -1) && (errno == EINTR)) 1.16 - continue; 1.17 - if (s <= 0) { 1.18 - break; 1.19 - } 1.20 - r += s; 1.21 - } 1.22 - 1.23 - return (r == count) ? 1 : 0; 1.24 -} 1.25 - 1.26 static int 1.27 populate_page_if_necessary(int xc_handle, uint32_t dom, unsigned long gmfn, 1.28 struct xen_ia64_p2m_table *p2m_table) 1.29 @@ -68,7 +49,7 @@ read_page(int xc_handle, int io_fd, uint 1.30 ERROR("cannot map page"); 1.31 return -1; 1.32 } 1.33 - if (!read_exact(io_fd, mem, PAGE_SIZE)) { 1.34 + if (read_exact(io_fd, mem, PAGE_SIZE)) { 1.35 ERROR("Error when reading from state file (5)"); 1.36 munmap(mem, PAGE_SIZE); 1.37 return -1; 1.38 @@ -93,7 +74,7 @@ xc_ia64_recv_unallocated_list(int xc_han 1.39 unsigned long *pfntab = NULL; 1.40 unsigned int nr_frees; 1.41 1.42 - if (!read_exact(io_fd, &count, sizeof(count))) { 1.43 + if (read_exact(io_fd, &count, sizeof(count))) { 1.44 ERROR("Error when reading pfn count"); 1.45 goto out; 1.46 } 1.47 @@ -104,7 +85,7 @@ xc_ia64_recv_unallocated_list(int xc_han 1.48 goto out; 1.49 } 1.50 1.51 - if (!read_exact(io_fd, pfntab, sizeof(unsigned long)*count)) { 1.52 + if (read_exact(io_fd, pfntab, sizeof(unsigned long)*count)) { 1.53 ERROR("Error when reading pfntab"); 1.54 goto out; 1.55 } 1.56 @@ -138,7 +119,7 @@ static int 1.57 xc_ia64_recv_vcpu_context(int xc_handle, int io_fd, uint32_t dom, 1.58 uint32_t vcpu, vcpu_guest_context_t *ctxt) 1.59 { 1.60 - if (!read_exact(io_fd, ctxt, sizeof(*ctxt))) { 1.61 + if (read_exact(io_fd, ctxt, sizeof(*ctxt))) { 1.62 ERROR("Error when reading ctxt"); 1.63 return -1; 1.64 } 1.65 @@ -182,7 +163,7 @@ xc_ia64_recv_shared_info(int xc_handle, 1.66 return -1; 1.67 } 1.68 1.69 - if (!read_exact(io_fd, shared_info, PAGE_SIZE)) { 1.70 + if (read_exact(io_fd, shared_info, PAGE_SIZE)) { 1.71 ERROR("Error when reading shared_info page"); 1.72 munmap(shared_info, PAGE_SIZE); 1.73 return -1; 1.74 @@ -308,7 +289,7 @@ xc_ia64_hvm_recv_context(int xc_handle, 1.75 ERROR("Could not get domain info"); 1.76 goto out; 1.77 } 1.78 - if (!read_exact(io_fd, &max_virt_cpus, sizeof(max_virt_cpus))) { 1.79 + if (read_exact(io_fd, &max_virt_cpus, sizeof(max_virt_cpus))) { 1.80 ERROR("error reading max_virt_cpus"); 1.81 goto out; 1.82 } 1.83 @@ -325,7 +306,7 @@ xc_ia64_hvm_recv_context(int xc_handle, 1.84 goto out; 1.85 } 1.86 memset(vcpumap, 0, vcpumap_size); 1.87 - if (!read_exact(io_fd, vcpumap, vcpumap_size)) { 1.88 + if (read_exact(io_fd, vcpumap, vcpumap_size)) { 1.89 ERROR("read vcpumap"); 1.90 goto out; 1.91 } 1.92 @@ -345,7 +326,7 @@ xc_ia64_hvm_recv_context(int xc_handle, 1.93 } 1.94 1.95 /* Set HVM-specific parameters */ 1.96 - if (!read_exact(io_fd, magic_pfns, sizeof(magic_pfns))) { 1.97 + if (read_exact(io_fd, magic_pfns, sizeof(magic_pfns))) { 1.98 ERROR("error reading magic page addresses"); 1.99 goto out; 1.100 } 1.101 @@ -372,7 +353,7 @@ xc_ia64_hvm_recv_context(int xc_handle, 1.102 *store_mfn = magic_pfns[0]; 1.103 1.104 /* Read HVM context */ 1.105 - if (!read_exact(io_fd, &rec_size, sizeof(rec_size))) { 1.106 + if (read_exact(io_fd, &rec_size, sizeof(rec_size))) { 1.107 ERROR("error read hvm context size!\n"); 1.108 goto out; 1.109 } 1.110 @@ -384,7 +365,7 @@ xc_ia64_hvm_recv_context(int xc_handle, 1.111 goto out; 1.112 } 1.113 1.114 - if (!read_exact(io_fd, hvm_buf, rec_size)) { 1.115 + if (read_exact(io_fd, hvm_buf, rec_size)) { 1.116 ERROR("error loading the HVM context"); 1.117 goto out; 1.118 } 1.119 @@ -445,14 +426,14 @@ xc_domain_restore(int xc_handle, int io_ 1.120 /* For info only */ 1.121 nr_pfns = 0; 1.122 1.123 - if ( !read_exact(io_fd, &p2m_size, sizeof(unsigned long)) ) 1.124 + if ( read_exact(io_fd, &p2m_size, sizeof(unsigned long)) ) 1.125 { 1.126 ERROR("read: p2m_size"); 1.127 goto out; 1.128 } 1.129 DPRINTF("xc_linux_restore start: p2m_size = %lx\n", p2m_size); 1.130 1.131 - if (!read_exact(io_fd, &ver, sizeof(unsigned long))) { 1.132 + if (read_exact(io_fd, &ver, sizeof(unsigned long))) { 1.133 ERROR("Error when reading version"); 1.134 goto out; 1.135 } 1.136 @@ -461,7 +442,7 @@ xc_domain_restore(int xc_handle, int io_ 1.137 goto out; 1.138 } 1.139 1.140 - if (!read_exact(io_fd, &domctl.u.arch_setup, sizeof(domctl.u.arch_setup))) { 1.141 + if (read_exact(io_fd, &domctl.u.arch_setup, sizeof(domctl.u.arch_setup))) { 1.142 ERROR("read: domain setup"); 1.143 goto out; 1.144 } 1.145 @@ -492,7 +473,7 @@ xc_domain_restore(int xc_handle, int io_ 1.146 unsigned long memmap_size; 1.147 xen_ia64_memmap_info_t *memmap_info; 1.148 1.149 - if (!read_exact(io_fd, &memmap_info_num_pages, 1.150 + if (read_exact(io_fd, &memmap_info_num_pages, 1.151 sizeof(memmap_info_num_pages))) { 1.152 ERROR("read: memmap_info_num_pages"); 1.153 goto out; 1.154 @@ -503,7 +484,7 @@ xc_domain_restore(int xc_handle, int io_ 1.155 ERROR("Could not allocate memory for memmap_info"); 1.156 goto out; 1.157 } 1.158 - if (!read_exact(io_fd, memmap_info, memmap_size)) { 1.159 + if (read_exact(io_fd, memmap_info, memmap_size)) { 1.160 ERROR("read: memmap_info"); 1.161 goto out; 1.162 } 1.163 @@ -546,7 +527,7 @@ xc_domain_restore(int xc_handle, int io_ 1.164 1.165 while (1) { 1.166 unsigned long gmfn; 1.167 - if (!read_exact(io_fd, &gmfn, sizeof(unsigned long))) { 1.168 + if (read_exact(io_fd, &gmfn, sizeof(unsigned long))) { 1.169 ERROR("Error when reading batch size"); 1.170 goto out; 1.171 }
2.1 --- a/tools/libxc/ia64/xc_ia64_linux_save.c Sat Nov 10 10:55:27 2007 +0000 2.2 +++ b/tools/libxc/ia64/xc_ia64_linux_save.c Sun Nov 11 18:22:33 2007 +0000 2.3 @@ -79,14 +79,6 @@ static int xc_ia64_shadow_control(int xc 2.4 dirty_bitmap, pages, NULL, 0, stats); 2.5 } 2.6 2.7 -static inline ssize_t 2.8 -write_exact(int fd, void *buf, size_t count) 2.9 -{ 2.10 - if (write(fd, buf, count) != count) 2.11 - return 0; 2.12 - return 1; 2.13 -} 2.14 - 2.15 static int 2.16 suspend_and_state(int (*suspend)(int), int xc_handle, int io_fd, 2.17 int dom, xc_dominfo_t *info) 2.18 @@ -174,7 +166,7 @@ xc_ia64_send_unallocated_list(int xc_han 2.19 j++; 2.20 } 2.21 } 2.22 - if (!write_exact(io_fd, &j, sizeof(unsigned int))) { 2.23 + if (write_exact(io_fd, &j, sizeof(unsigned int))) { 2.24 ERROR("Error when writing to state file (6a)"); 2.25 return -1; 2.26 } 2.27 @@ -195,7 +187,7 @@ xc_ia64_send_unallocated_list(int xc_han 2.28 if (!xc_ia64_p2m_allocated(p2m_table, N)) 2.29 pfntab[j++] = N; 2.30 if (j == sizeof(pfntab)/sizeof(pfntab[0])) { 2.31 - if (!write_exact(io_fd, &pfntab, sizeof(pfntab[0]) * j)) { 2.32 + if (write_exact(io_fd, &pfntab, sizeof(pfntab[0]) * j)) { 2.33 ERROR("Error when writing to state file (6b)"); 2.34 return -1; 2.35 } 2.36 @@ -204,7 +196,7 @@ xc_ia64_send_unallocated_list(int xc_han 2.37 } 2.38 } 2.39 if (j > 0) { 2.40 - if (!write_exact(io_fd, &pfntab, sizeof(pfntab[0]) * j)) { 2.41 + if (write_exact(io_fd, &pfntab, sizeof(pfntab[0]) * j)) { 2.42 ERROR("Error when writing to state file (6c)"); 2.43 return -1; 2.44 } 2.45 @@ -222,7 +214,7 @@ xc_ia64_send_vcpu_context(int xc_handle, 2.46 return -1; 2.47 } 2.48 2.49 - if (!write_exact(io_fd, ctxt, sizeof(*ctxt))) { 2.50 + if (write_exact(io_fd, ctxt, sizeof(*ctxt))) { 2.51 ERROR("Error when writing to state file (1)"); 2.52 return -1; 2.53 } 2.54 @@ -234,7 +226,7 @@ xc_ia64_send_vcpu_context(int xc_handle, 2.55 static int 2.56 xc_ia64_send_shared_info(int xc_handle, int io_fd, shared_info_t *live_shinfo) 2.57 { 2.58 - if (!write_exact(io_fd, live_shinfo, PAGE_SIZE)) { 2.59 + if (write_exact(io_fd, live_shinfo, PAGE_SIZE)) { 2.60 ERROR("Error when writing to state file (1)"); 2.61 return -1; 2.62 } 2.63 @@ -258,7 +250,7 @@ xc_ia64_pv_send_context(int xc_handle, i 2.64 ERROR("cannot map privreg page"); 2.65 return -1; 2.66 } 2.67 - if (!write_exact(io_fd, mem, PAGE_SIZE)) { 2.68 + if (write_exact(io_fd, mem, PAGE_SIZE)) { 2.69 ERROR("Error when writing privreg to state file (5)"); 2.70 munmap(mem, PAGE_SIZE); 2.71 return -1; 2.72 @@ -319,12 +311,12 @@ xc_ia64_hvm_send_context(int xc_handle, 2.73 __set_bit(i, vcpumap); 2.74 } 2.75 2.76 - if (!write_exact(io_fd, &max_virt_cpus, sizeof(max_virt_cpus))) { 2.77 + if (write_exact(io_fd, &max_virt_cpus, sizeof(max_virt_cpus))) { 2.78 ERROR("write max_virt_cpus"); 2.79 goto out; 2.80 } 2.81 2.82 - if (!write_exact(io_fd, vcpumap, vcpumap_size)) { 2.83 + if (write_exact(io_fd, vcpumap, vcpumap_size)) { 2.84 ERROR("write vcpumap"); 2.85 goto out; 2.86 } 2.87 @@ -352,7 +344,7 @@ xc_ia64_hvm_send_context(int xc_handle, 2.88 } 2.89 } 2.90 2.91 - if (!write_exact(io_fd, magic_pfns, sizeof(magic_pfns))) { 2.92 + if (write_exact(io_fd, magic_pfns, sizeof(magic_pfns))) { 2.93 ERROR("Error when writing to state file (7)"); 2.94 goto out; 2.95 } 2.96 @@ -377,12 +369,12 @@ xc_ia64_hvm_send_context(int xc_handle, 2.97 goto out; 2.98 } 2.99 2.100 - if (!write_exact(io_fd, &rec_size, sizeof(rec_size))) { 2.101 + if (write_exact(io_fd, &rec_size, sizeof(rec_size))) { 2.102 ERROR("error write hvm buffer size"); 2.103 goto out; 2.104 } 2.105 2.106 - if (!write_exact(io_fd, hvm_buf, rec_size)) { 2.107 + if (write_exact(io_fd, hvm_buf, rec_size)) { 2.108 ERROR("write HVM info failed!\n"); 2.109 goto out; 2.110 } 2.111 @@ -496,7 +488,7 @@ xc_domain_save(int xc_handle, int io_fd, 2.112 p2m_size = xc_memory_op(xc_handle, XENMEM_maximum_gpfn, &dom); 2.113 2.114 /* This is expected by xm restore. */ 2.115 - if (!write_exact(io_fd, &p2m_size, sizeof(unsigned long))) { 2.116 + if (write_exact(io_fd, &p2m_size, sizeof(unsigned long))) { 2.117 ERROR("write: p2m_size"); 2.118 goto out; 2.119 } 2.120 @@ -509,7 +501,7 @@ xc_domain_save(int xc_handle, int io_fd, 2.121 { 2.122 unsigned long version = XC_IA64_SR_FORMAT_VER_CURRENT; 2.123 2.124 - if (!write_exact(io_fd, &version, sizeof(unsigned long))) { 2.125 + if (write_exact(io_fd, &version, sizeof(unsigned long))) { 2.126 ERROR("write: version"); 2.127 goto out; 2.128 } 2.129 @@ -522,7 +514,7 @@ xc_domain_save(int xc_handle, int io_fd, 2.130 ERROR("Could not get domain setup"); 2.131 goto out; 2.132 } 2.133 - if (!write_exact(io_fd, &domctl.u.arch_setup, 2.134 + if (write_exact(io_fd, &domctl.u.arch_setup, 2.135 sizeof(domctl.u.arch_setup))) { 2.136 ERROR("write: domain setup"); 2.137 goto out; 2.138 @@ -605,12 +597,12 @@ xc_domain_save(int xc_handle, int io_fd, 2.139 PERROR("xc_ia64_p2m_map"); 2.140 goto out; 2.141 } 2.142 - if (!write_exact(io_fd, 2.143 + if (write_exact(io_fd, 2.144 &memmap_info_num_pages, sizeof(memmap_info_num_pages))) { 2.145 PERROR("write: arch.memmap_info_num_pages"); 2.146 goto out; 2.147 } 2.148 - if (!write_exact(io_fd, memmap_info, memmap_size)) { 2.149 + if (write_exact(io_fd, memmap_info, memmap_size)) { 2.150 PERROR("write: memmap_info"); 2.151 goto out; 2.152 } 2.153 @@ -679,7 +671,7 @@ xc_domain_save(int xc_handle, int io_fd, 2.154 continue; 2.155 } 2.156 2.157 - if (!write_exact(io_fd, &N, sizeof(N))) { 2.158 + if (write_exact(io_fd, &N, sizeof(N))) { 2.159 ERROR("write: p2m_size"); 2.160 munmap(mem, PAGE_SIZE); 2.161 goto out; 2.162 @@ -751,7 +743,7 @@ xc_domain_save(int xc_handle, int io_fd, 2.163 /* terminate */ 2.164 { 2.165 unsigned long pfn = INVALID_MFN; 2.166 - if (!write_exact(io_fd, &pfn, sizeof(pfn))) { 2.167 + if (write_exact(io_fd, &pfn, sizeof(pfn))) { 2.168 ERROR("Error when writing to state file (6)"); 2.169 goto out; 2.170 }
3.1 --- a/tools/libxc/xc_core.c Sat Nov 10 10:55:27 2007 +0000 3.2 +++ b/tools/libxc/xc_core.c Sun Nov 11 18:22:33 2007 +0000 3.3 @@ -860,19 +860,15 @@ struct dump_args { 3.4 static int local_file_dump(void *args, char *buffer, unsigned int length) 3.5 { 3.6 struct dump_args *da = args; 3.7 - int bytes, offset; 3.8 3.9 - for ( offset = 0; offset < length; offset += bytes ) 3.10 + if ( write_exact(da->fd, buffer, length) == -1 ) 3.11 { 3.12 - bytes = write(da->fd, &buffer[offset], length-offset); 3.13 - if ( bytes <= 0 ) 3.14 - { 3.15 - PERROR("Failed to write buffer"); 3.16 - return -errno; 3.17 - } 3.18 + PERROR("Failed to write buffer"); 3.19 + return -errno; 3.20 } 3.21 3.22 - if (length >= DUMP_INCREMENT*PAGE_SIZE) { 3.23 + if ( length >= (DUMP_INCREMENT * PAGE_SIZE) ) 3.24 + { 3.25 // Now dumping pages -- make sure we discard clean pages from 3.26 // the cache after each write 3.27 discard_file_cache(da->fd, 0 /* no flush */);
4.1 --- a/tools/libxc/xc_domain_restore.c Sat Nov 10 10:55:27 2007 +0000 4.2 +++ b/tools/libxc/xc_domain_restore.c Sun Nov 11 18:22:33 2007 +0000 4.3 @@ -59,26 +59,6 @@ static xen_pfn_t *p2m_batch = NULL; 4.4 /* Address size of the guest, in bytes */ 4.5 unsigned int guest_width; 4.6 4.7 - 4.8 -static ssize_t 4.9 -read_exact(int fd, void *buf, size_t count) 4.10 -{ 4.11 - int r = 0, s; 4.12 - unsigned char *b = buf; 4.13 - 4.14 - while ( r < count ) 4.15 - { 4.16 - s = read(fd, &b[r], count - r); 4.17 - if ( (s == -1) && (errno == EINTR) ) 4.18 - continue; 4.19 - if ( s <= 0 ) 4.20 - break; 4.21 - r += s; 4.22 - } 4.23 - 4.24 - return (r == count); 4.25 -} 4.26 - 4.27 /* 4.28 ** In the state file (or during transfer), all page-table pages are 4.29 ** converted into a 'canonical' form where references to actual mfns 4.30 @@ -177,7 +157,7 @@ static xen_pfn_t *load_p2m_frame_list( 4.31 xen_pfn_t p2m_fl_zero; 4.32 4.33 /* Read first entry of P2M list, or extended-info signature (~0UL). */ 4.34 - if ( !read_exact(io_fd, &p2m_fl_zero, sizeof(long)) ) 4.35 + if ( read_exact(io_fd, &p2m_fl_zero, sizeof(long)) ) 4.36 { 4.37 ERROR("read extended-info signature failed"); 4.38 return NULL; 4.39 @@ -188,7 +168,7 @@ static xen_pfn_t *load_p2m_frame_list( 4.40 uint32_t tot_bytes; 4.41 4.42 /* Next 4 bytes: total size of following extended info. */ 4.43 - if ( !read_exact(io_fd, &tot_bytes, sizeof(tot_bytes)) ) 4.44 + if ( read_exact(io_fd, &tot_bytes, sizeof(tot_bytes)) ) 4.45 { 4.46 ERROR("read extended-info size failed"); 4.47 return NULL; 4.48 @@ -200,8 +180,8 @@ static xen_pfn_t *load_p2m_frame_list( 4.49 char chunk_sig[4]; 4.50 4.51 /* 4-character chunk signature + 4-byte remaining chunk size. */ 4.52 - if ( !read_exact(io_fd, chunk_sig, sizeof(chunk_sig)) || 4.53 - !read_exact(io_fd, &chunk_bytes, sizeof(chunk_bytes)) || 4.54 + if ( read_exact(io_fd, chunk_sig, sizeof(chunk_sig)) || 4.55 + read_exact(io_fd, &chunk_bytes, sizeof(chunk_bytes)) || 4.56 (tot_bytes < (chunk_bytes + 8)) ) 4.57 { 4.58 ERROR("read extended-info chunk signature failed"); 4.59 @@ -230,7 +210,7 @@ static xen_pfn_t *load_p2m_frame_list( 4.60 return NULL; 4.61 } 4.62 4.63 - if ( !read_exact(io_fd, &ctxt, chunk_bytes) ) 4.64 + if ( read_exact(io_fd, &ctxt, chunk_bytes) ) 4.65 { 4.66 ERROR("read extended-info vcpu context failed"); 4.67 return NULL; 4.68 @@ -251,7 +231,7 @@ static xen_pfn_t *load_p2m_frame_list( 4.69 while ( chunk_bytes ) 4.70 { 4.71 unsigned long sz = MIN(chunk_bytes, sizeof(xen_pfn_t)); 4.72 - if ( !read_exact(io_fd, &p2m_fl_zero, sz) ) 4.73 + if ( read_exact(io_fd, &p2m_fl_zero, sz) ) 4.74 { 4.75 ERROR("read-and-discard extended-info chunk bytes failed"); 4.76 return NULL; 4.77 @@ -262,7 +242,7 @@ static xen_pfn_t *load_p2m_frame_list( 4.78 } 4.79 4.80 /* Now read the real first entry of P2M list. */ 4.81 - if ( !read_exact(io_fd, &p2m_fl_zero, sizeof(xen_pfn_t)) ) 4.82 + if ( read_exact(io_fd, &p2m_fl_zero, sizeof(xen_pfn_t)) ) 4.83 { 4.84 ERROR("read first entry of p2m_frame_list failed"); 4.85 return NULL; 4.86 @@ -279,8 +259,8 @@ static xen_pfn_t *load_p2m_frame_list( 4.87 4.88 /* First entry has already been read. */ 4.89 p2m_frame_list[0] = p2m_fl_zero; 4.90 - if ( !read_exact(io_fd, &p2m_frame_list[1], 4.91 - (P2M_FL_ENTRIES - 1) * sizeof(xen_pfn_t)) ) 4.92 + if ( read_exact(io_fd, &p2m_frame_list[1], 4.93 + (P2M_FL_ENTRIES - 1) * sizeof(xen_pfn_t)) ) 4.94 { 4.95 ERROR("read p2m_frame_list failed"); 4.96 return NULL; 4.97 @@ -349,7 +329,7 @@ int xc_domain_restore(int xc_handle, int 4.98 /* For info only */ 4.99 nr_pfns = 0; 4.100 4.101 - if ( !read_exact(io_fd, &p2m_size, sizeof(unsigned long)) ) 4.102 + if ( read_exact(io_fd, &p2m_size, sizeof(unsigned long)) ) 4.103 { 4.104 ERROR("read: p2m_size"); 4.105 goto out; 4.106 @@ -464,7 +444,7 @@ int xc_domain_restore(int xc_handle, int 4.107 prev_pc = this_pc; 4.108 } 4.109 4.110 - if ( !read_exact(io_fd, &j, sizeof(int)) ) 4.111 + if ( read_exact(io_fd, &j, sizeof(int)) ) 4.112 { 4.113 ERROR("Error when reading batch size"); 4.114 goto out; 4.115 @@ -482,9 +462,9 @@ int xc_domain_restore(int xc_handle, int 4.116 if ( j == -2 ) 4.117 { 4.118 new_ctxt_format = 1; 4.119 - if ( !read_exact(io_fd, &max_vcpu_id, sizeof(int)) || 4.120 + if ( read_exact(io_fd, &max_vcpu_id, sizeof(int)) || 4.121 (max_vcpu_id >= 64) || 4.122 - !read_exact(io_fd, &vcpumap, sizeof(uint64_t)) ) 4.123 + read_exact(io_fd, &vcpumap, sizeof(uint64_t)) ) 4.124 { 4.125 ERROR("Error when reading max_vcpu_id"); 4.126 goto out; 4.127 @@ -501,7 +481,7 @@ int xc_domain_restore(int xc_handle, int 4.128 goto out; 4.129 } 4.130 4.131 - if ( !read_exact(io_fd, region_pfn_type, j*sizeof(unsigned long)) ) 4.132 + if ( read_exact(io_fd, region_pfn_type, j*sizeof(unsigned long)) ) 4.133 { 4.134 ERROR("Error when reading region pfn types"); 4.135 goto out; 4.136 @@ -594,7 +574,7 @@ int xc_domain_restore(int xc_handle, int 4.137 /* In verify mode, we use a copy; otherwise we work in place */ 4.138 page = verify ? (void *)buf : (region_base + i*PAGE_SIZE); 4.139 4.140 - if ( !read_exact(io_fd, page, PAGE_SIZE) ) 4.141 + if ( read_exact(io_fd, page, PAGE_SIZE) ) 4.142 { 4.143 ERROR("Error when reading page (type was %lx)", pagetype); 4.144 goto out; 4.145 @@ -704,7 +684,7 @@ int xc_domain_restore(int xc_handle, int 4.146 uint32_t rec_len; 4.147 4.148 /* Set HVM-specific parameters */ 4.149 - if ( !read_exact(io_fd, magic_pfns, sizeof(magic_pfns)) ) 4.150 + if ( read_exact(io_fd, magic_pfns, sizeof(magic_pfns)) ) 4.151 { 4.152 ERROR("error reading magic page addresses"); 4.153 goto out; 4.154 @@ -737,7 +717,7 @@ int xc_domain_restore(int xc_handle, int 4.155 *store_mfn = magic_pfns[2]; 4.156 4.157 /* Read HVM context */ 4.158 - if ( !read_exact(io_fd, &rec_len, sizeof(uint32_t)) ) 4.159 + if ( read_exact(io_fd, &rec_len, sizeof(uint32_t)) ) 4.160 { 4.161 ERROR("error read hvm context size!\n"); 4.162 goto out; 4.163 @@ -751,7 +731,7 @@ int xc_domain_restore(int xc_handle, int 4.164 goto out; 4.165 } 4.166 4.167 - if ( !read_exact(io_fd, hvm_buf, rec_len) ) 4.168 + if ( read_exact(io_fd, hvm_buf, rec_len) ) 4.169 { 4.170 ERROR("error loading the HVM context"); 4.171 goto out; 4.172 @@ -943,7 +923,7 @@ int xc_domain_restore(int xc_handle, int 4.173 unsigned long *pfntab; 4.174 int nr_frees; 4.175 4.176 - if ( !read_exact(io_fd, &count, sizeof(count)) || 4.177 + if ( read_exact(io_fd, &count, sizeof(count)) || 4.178 (count > (1U << 28)) ) /* up to 1TB of address space */ 4.179 { 4.180 ERROR("Error when reading pfn count (= %u)", count); 4.181 @@ -956,7 +936,7 @@ int xc_domain_restore(int xc_handle, int 4.182 goto out; 4.183 } 4.184 4.185 - if ( !read_exact(io_fd, pfntab, sizeof(unsigned long)*count) ) 4.186 + if ( read_exact(io_fd, pfntab, sizeof(unsigned long)*count) ) 4.187 { 4.188 ERROR("Error when reading pfntab"); 4.189 goto out; 4.190 @@ -1001,9 +981,9 @@ int xc_domain_restore(int xc_handle, int 4.191 if ( !(vcpumap & (1ULL << i)) ) 4.192 continue; 4.193 4.194 - if ( !read_exact(io_fd, &ctxt, ((guest_width == 8) 4.195 - ? sizeof(ctxt.x64) 4.196 - : sizeof(ctxt.x32))) ) 4.197 + if ( read_exact(io_fd, &ctxt, ((guest_width == 8) 4.198 + ? sizeof(ctxt.x64) 4.199 + : sizeof(ctxt.x32))) ) 4.200 { 4.201 ERROR("Error when reading ctxt %d", i); 4.202 goto out; 4.203 @@ -1112,7 +1092,7 @@ int xc_domain_restore(int xc_handle, int 4.204 4.205 if ( !ext_vcpucontext ) 4.206 continue; 4.207 - if ( !read_exact(io_fd, &domctl.u.ext_vcpucontext, 128) || 4.208 + if ( read_exact(io_fd, &domctl.u.ext_vcpucontext, 128) || 4.209 (domctl.u.ext_vcpucontext.vcpu != i) ) 4.210 { 4.211 ERROR("Error when reading extended ctxt %d", i); 4.212 @@ -1128,7 +1108,7 @@ int xc_domain_restore(int xc_handle, int 4.213 } 4.214 } 4.215 4.216 - if ( !read_exact(io_fd, shared_info_page, PAGE_SIZE) ) 4.217 + if ( read_exact(io_fd, shared_info_page, PAGE_SIZE) ) 4.218 { 4.219 ERROR("Error when reading shared info page"); 4.220 goto out;
5.1 --- a/tools/libxc/xc_domain_save.c Sat Nov 10 10:55:27 2007 +0000 5.2 +++ b/tools/libxc/xc_domain_save.c Sun Nov 11 18:22:33 2007 +0000 5.3 @@ -175,8 +175,7 @@ static uint64_t tv_delta(struct timeval 5.4 static int noncached_write(int fd, int live, void *buffer, int len) 5.5 { 5.6 static int write_count = 0; 5.7 - 5.8 - int rc = write(fd,buffer,len); 5.9 + int rc = (write_exact(fd, buffer, len) == 0) ? len : -1; 5.10 5.11 write_count += len; 5.12 if ( write_count >= (MAX_PAGECACHE_USAGE * PAGE_SIZE) ) 5.13 @@ -281,11 +280,6 @@ static int ratewrite(int io_fd, int live 5.14 5.15 #endif 5.16 5.17 -static inline ssize_t write_exact(int fd, void *buf, size_t count) 5.18 -{ 5.19 - return (write(fd, buf, count) == count); 5.20 -} 5.21 - 5.22 static int print_stats(int xc_handle, uint32_t domid, int pages_sent, 5.23 xc_shadow_op_stats_t *stats, int print) 5.24 { 5.25 @@ -782,21 +776,21 @@ static xen_pfn_t *map_and_save_p2m_table 5.26 : sizeof(ctxt.x32)); 5.27 uint32_t chunk2_sz = 0; 5.28 uint32_t tot_sz = (chunk1_sz + 8) + (chunk2_sz + 8); 5.29 - if ( !write_exact(io_fd, &signature, sizeof(signature)) || 5.30 - !write_exact(io_fd, &tot_sz, sizeof(tot_sz)) || 5.31 - !write_exact(io_fd, "vcpu", 4) || 5.32 - !write_exact(io_fd, &chunk1_sz, sizeof(chunk1_sz)) || 5.33 - !write_exact(io_fd, &ctxt, chunk1_sz) || 5.34 - !write_exact(io_fd, "extv", 4) || 5.35 - !write_exact(io_fd, &chunk2_sz, sizeof(chunk2_sz)) ) 5.36 + if ( write_exact(io_fd, &signature, sizeof(signature)) || 5.37 + write_exact(io_fd, &tot_sz, sizeof(tot_sz)) || 5.38 + write_exact(io_fd, "vcpu", 4) || 5.39 + write_exact(io_fd, &chunk1_sz, sizeof(chunk1_sz)) || 5.40 + write_exact(io_fd, &ctxt, chunk1_sz) || 5.41 + write_exact(io_fd, "extv", 4) || 5.42 + write_exact(io_fd, &chunk2_sz, sizeof(chunk2_sz)) ) 5.43 { 5.44 ERROR("write: extended info"); 5.45 goto out; 5.46 } 5.47 } 5.48 5.49 - if ( !write_exact(io_fd, p2m_frame_list, 5.50 - P2M_FL_ENTRIES * sizeof(xen_pfn_t)) ) 5.51 + if ( write_exact(io_fd, p2m_frame_list, 5.52 + P2M_FL_ENTRIES * sizeof(xen_pfn_t)) ) 5.53 { 5.54 ERROR("write: p2m_frame_list"); 5.55 goto out; 5.56 @@ -1044,7 +1038,7 @@ int xc_domain_save(int xc_handle, int io 5.57 } 5.58 5.59 /* Start writing out the saved-domain record. */ 5.60 - if ( !write_exact(io_fd, &p2m_size, sizeof(unsigned long)) ) 5.61 + if ( write_exact(io_fd, &p2m_size, sizeof(unsigned long)) ) 5.62 { 5.63 ERROR("write: p2m_size"); 5.64 goto out; 5.65 @@ -1249,14 +1243,14 @@ int xc_domain_save(int xc_handle, int io 5.66 } 5.67 } 5.68 5.69 - if ( !write_exact(io_fd, &batch, sizeof(unsigned int)) ) 5.70 + if ( write_exact(io_fd, &batch, sizeof(unsigned int)) ) 5.71 { 5.72 ERROR("Error when writing to state file (2) (errno %d)", 5.73 errno); 5.74 goto out; 5.75 } 5.76 5.77 - if ( !write_exact(io_fd, pfn_type, sizeof(unsigned long)*batch) ) 5.78 + if ( write_exact(io_fd, pfn_type, sizeof(unsigned long)*batch) ) 5.79 { 5.80 ERROR("Error when writing to state file (3) (errno %d)", 5.81 errno); 5.82 @@ -1342,7 +1336,7 @@ int xc_domain_save(int xc_handle, int io 5.83 DPRINTF("Entering debug resend-all mode\n"); 5.84 5.85 /* send "-1" to put receiver into debug mode */ 5.86 - if ( !write_exact(io_fd, &minusone, sizeof(int)) ) 5.87 + if ( write_exact(io_fd, &minusone, sizeof(int)) ) 5.88 { 5.89 ERROR("Error when writing to state file (6) (errno %d)", 5.90 errno); 5.91 @@ -1434,7 +1428,7 @@ int xc_domain_save(int xc_handle, int io 5.92 } 5.93 5.94 chunk.vcpumap = vcpumap; 5.95 - if ( !write_exact(io_fd, &chunk, sizeof(chunk)) ) 5.96 + if ( write_exact(io_fd, &chunk, sizeof(chunk)) ) 5.97 { 5.98 ERROR("Error when writing to state file (errno %d)", errno); 5.99 goto out; 5.100 @@ -1443,7 +1437,7 @@ int xc_domain_save(int xc_handle, int io 5.101 5.102 /* Zero terminate */ 5.103 i = 0; 5.104 - if ( !write_exact(io_fd, &i, sizeof(int)) ) 5.105 + if ( write_exact(io_fd, &i, sizeof(int)) ) 5.106 { 5.107 ERROR("Error when writing to state file (6') (errno %d)", errno); 5.108 goto out; 5.109 @@ -1461,7 +1455,7 @@ int xc_domain_save(int xc_handle, int io 5.110 (unsigned long *)&magic_pfns[1]); 5.111 xc_get_hvm_param(xc_handle, dom, HVM_PARAM_STORE_PFN, 5.112 (unsigned long *)&magic_pfns[2]); 5.113 - if ( !write_exact(io_fd, magic_pfns, sizeof(magic_pfns)) ) 5.114 + if ( write_exact(io_fd, magic_pfns, sizeof(magic_pfns)) ) 5.115 { 5.116 ERROR("Error when writing to state file (7)"); 5.117 goto out; 5.118 @@ -1475,13 +1469,13 @@ int xc_domain_save(int xc_handle, int io 5.119 goto out; 5.120 } 5.121 5.122 - if ( !write_exact(io_fd, &rec_size, sizeof(uint32_t)) ) 5.123 + if ( write_exact(io_fd, &rec_size, sizeof(uint32_t)) ) 5.124 { 5.125 ERROR("error write hvm buffer size"); 5.126 goto out; 5.127 } 5.128 5.129 - if ( !write_exact(io_fd, hvm_buf, rec_size) ) 5.130 + if ( write_exact(io_fd, hvm_buf, rec_size) ) 5.131 { 5.132 ERROR("write HVM info failed!\n"); 5.133 goto out; 5.134 @@ -1505,7 +1499,7 @@ int xc_domain_save(int xc_handle, int io 5.135 j++; 5.136 } 5.137 5.138 - if ( !write_exact(io_fd, &j, sizeof(unsigned int)) ) 5.139 + if ( write_exact(io_fd, &j, sizeof(unsigned int)) ) 5.140 { 5.141 ERROR("Error when writing to state file (6a) (errno %d)", errno); 5.142 goto out; 5.143 @@ -1519,7 +1513,7 @@ int xc_domain_save(int xc_handle, int io 5.144 i++; 5.145 if ( (j == 1024) || (i == p2m_size) ) 5.146 { 5.147 - if ( !write_exact(io_fd, &pfntab, sizeof(unsigned long)*j) ) 5.148 + if ( write_exact(io_fd, &pfntab, sizeof(unsigned long)*j) ) 5.149 { 5.150 ERROR("Error when writing to state file (6b) (errno %d)", 5.151 errno); 5.152 @@ -1596,9 +1590,9 @@ int xc_domain_save(int xc_handle, int io 5.153 mfn_to_pfn(xen_cr3_to_pfn(ctxt.x64.ctrlreg[1]))); 5.154 } 5.155 5.156 - if ( !write_exact(io_fd, &ctxt, ((guest_width==8) 5.157 - ? sizeof(ctxt.x64) 5.158 - : sizeof(ctxt.x32))) ) 5.159 + if ( write_exact(io_fd, &ctxt, ((guest_width==8) 5.160 + ? sizeof(ctxt.x64) 5.161 + : sizeof(ctxt.x32))) ) 5.162 { 5.163 ERROR("Error when writing to state file (1) (errno %d)", errno); 5.164 goto out; 5.165 @@ -1612,7 +1606,7 @@ int xc_domain_save(int xc_handle, int io 5.166 ERROR("No extended context for VCPU%d", i); 5.167 goto out; 5.168 } 5.169 - if ( !write_exact(io_fd, &domctl.u.ext_vcpucontext, 128) ) 5.170 + if ( write_exact(io_fd, &domctl.u.ext_vcpucontext, 128) ) 5.171 { 5.172 ERROR("Error when writing to state file (2) (errno %d)", errno); 5.173 goto out; 5.174 @@ -1625,7 +1619,7 @@ int xc_domain_save(int xc_handle, int io 5.175 memcpy(page, live_shinfo, PAGE_SIZE); 5.176 SET_FIELD(((shared_info_either_t *)page), 5.177 arch.pfn_to_mfn_frame_list_list, 0); 5.178 - if ( !write_exact(io_fd, page, PAGE_SIZE) ) 5.179 + if ( write_exact(io_fd, page, PAGE_SIZE) ) 5.180 { 5.181 ERROR("Error when writing to state file (1) (errno %d)", errno); 5.182 goto out;
6.1 --- a/tools/libxc/xc_linux.c Sat Nov 10 10:55:27 2007 +0000 6.2 +++ b/tools/libxc/xc_linux.c Sun Nov 11 18:22:33 2007 +0000 6.3 @@ -295,37 +295,12 @@ int xc_evtchn_unbind(int xce_handle, evt 6.4 return ioctl(xce_handle, IOCTL_EVTCHN_UNBIND, &unbind); 6.5 } 6.6 6.7 -static int dorw(int fd, char *data, size_t size, int do_write) 6.8 -{ 6.9 - size_t offset = 0; 6.10 - ssize_t len; 6.11 - 6.12 - while ( offset < size ) 6.13 - { 6.14 - if (do_write) 6.15 - len = write(fd, data + offset, size - offset); 6.16 - else 6.17 - len = read(fd, data + offset, size - offset); 6.18 - 6.19 - if ( len == -1 ) 6.20 - { 6.21 - if ( errno == EINTR ) 6.22 - continue; 6.23 - return -1; 6.24 - } 6.25 - 6.26 - offset += len; 6.27 - } 6.28 - 6.29 - return 0; 6.30 -} 6.31 - 6.32 evtchn_port_or_error_t 6.33 xc_evtchn_pending(int xce_handle) 6.34 { 6.35 evtchn_port_t port; 6.36 6.37 - if ( dorw(xce_handle, (char *)&port, sizeof(port), 0) == -1 ) 6.38 + if ( read_exact(xce_handle, (char *)&port, sizeof(port)) == -1 ) 6.39 return -1; 6.40 6.41 return port; 6.42 @@ -333,7 +308,7 @@ xc_evtchn_pending(int xce_handle) 6.43 6.44 int xc_evtchn_unmask(int xce_handle, evtchn_port_t port) 6.45 { 6.46 - return dorw(xce_handle, (char *)&port, sizeof(port), 1); 6.47 + return write_exact(xce_handle, (char *)&port, sizeof(port)); 6.48 } 6.49 6.50 /* Optionally flush file to disk and discard page cache */
7.1 --- a/tools/libxc/xc_netbsd.c Sat Nov 10 10:55:27 2007 +0000 7.2 +++ b/tools/libxc/xc_netbsd.c Sun Nov 11 18:22:33 2007 +0000 7.3 @@ -209,37 +209,12 @@ xc_evtchn_bind_virq(int xce_handle, unsi 7.4 return bind.port; 7.5 } 7.6 7.7 -static int dorw(int fd, char *data, size_t size, int do_write) 7.8 -{ 7.9 - size_t offset = 0; 7.10 - ssize_t len; 7.11 - 7.12 - while ( offset < size ) 7.13 - { 7.14 - if (do_write) 7.15 - len = write(fd, data + offset, size - offset); 7.16 - else 7.17 - len = read(fd, data + offset, size - offset); 7.18 - 7.19 - if ( len == -1 ) 7.20 - { 7.21 - if ( errno == EINTR ) 7.22 - continue; 7.23 - return -1; 7.24 - } 7.25 - 7.26 - offset += len; 7.27 - } 7.28 - 7.29 - return 0; 7.30 -} 7.31 - 7.32 evtchn_port_or_error_t 7.33 xc_evtchn_pending(int xce_handle) 7.34 { 7.35 evtchn_port_t port; 7.36 7.37 - if ( dorw(xce_handle, (char *)&port, sizeof(port), 0) == -1 ) 7.38 + if ( read_exact(xce_handle, (char *)&port, sizeof(port)) == -1 ) 7.39 return -1; 7.40 7.41 return port; 7.42 @@ -247,7 +222,7 @@ xc_evtchn_pending(int xce_handle) 7.43 7.44 int xc_evtchn_unmask(int xce_handle, evtchn_port_t port) 7.45 { 7.46 - return dorw(xce_handle, (char *)&port, sizeof(port), 1); 7.47 + return write_exact(xce_handle, (char *)&port, sizeof(port)); 7.48 } 7.49 7.50 /* Optionally flush file to disk and discard page cache */
8.1 --- a/tools/libxc/xc_private.c Sat Nov 10 10:55:27 2007 +0000 8.2 +++ b/tools/libxc/xc_private.c Sun Nov 11 18:22:33 2007 +0000 8.3 @@ -581,6 +581,42 @@ void bitmap_byte_to_64(uint64_t *lp, con 8.4 } 8.5 } 8.6 8.7 +int read_exact(int fd, void *data, size_t size) 8.8 +{ 8.9 + size_t offset = 0; 8.10 + ssize_t len; 8.11 + 8.12 + while ( offset < size ) 8.13 + { 8.14 + len = read(fd, (char *)data + offset, size - offset); 8.15 + if ( (len == -1) && (errno == EINTR) ) 8.16 + continue; 8.17 + if ( len <= 0 ) 8.18 + return -1; 8.19 + offset += len; 8.20 + } 8.21 + 8.22 + return 0; 8.23 +} 8.24 + 8.25 +int write_exact(int fd, const void *data, size_t size) 8.26 +{ 8.27 + size_t offset = 0; 8.28 + ssize_t len; 8.29 + 8.30 + while ( offset < size ) 8.31 + { 8.32 + len = write(fd, (const char *)data + offset, size - offset); 8.33 + if ( (len == -1) && (errno == EINTR) ) 8.34 + continue; 8.35 + if ( len <= 0 ) 8.36 + return -1; 8.37 + offset += len; 8.38 + } 8.39 + 8.40 + return 0; 8.41 +} 8.42 + 8.43 /* 8.44 * Local variables: 8.45 * mode: C
9.1 --- a/tools/libxc/xc_private.h Sat Nov 10 10:55:27 2007 +0000 9.2 +++ b/tools/libxc/xc_private.h Sun Nov 11 18:22:33 2007 +0000 9.3 @@ -180,4 +180,8 @@ int xc_add_mmu_update(int xc_handle, str 9.4 unsigned long long ptr, unsigned long long val); 9.5 int xc_flush_mmu_updates(int xc_handle, struct xc_mmu *mmu); 9.6 9.7 +/* Return 0 on success; -1 on error. */ 9.8 +int read_exact(int fd, void *data, size_t size); 9.9 +int write_exact(int fd, const void *data, size_t size); 9.10 + 9.11 #endif /* __XC_PRIVATE_H__ */
10.1 --- a/tools/libxc/xc_solaris.c Sat Nov 10 10:55:27 2007 +0000 10.2 +++ b/tools/libxc/xc_solaris.c Sun Nov 11 18:22:33 2007 +0000 10.3 @@ -206,37 +206,12 @@ int xc_evtchn_unbind(int xce_handle, evt 10.4 return ioctl(xce_handle, IOCTL_EVTCHN_UNBIND, &unbind); 10.5 } 10.6 10.7 -static int dorw(int fd, char *data, size_t size, int do_write) 10.8 -{ 10.9 - size_t offset = 0; 10.10 - ssize_t len; 10.11 - 10.12 - while ( offset < size ) 10.13 - { 10.14 - if (do_write) 10.15 - len = write(fd, data + offset, size - offset); 10.16 - else 10.17 - len = read(fd, data + offset, size - offset); 10.18 - 10.19 - if ( len == -1 ) 10.20 - { 10.21 - if ( errno == EINTR ) 10.22 - continue; 10.23 - return -1; 10.24 - } 10.25 - 10.26 - offset += len; 10.27 - } 10.28 - 10.29 - return 0; 10.30 -} 10.31 - 10.32 evtchn_port_or_error_t 10.33 xc_evtchn_pending(int xce_handle) 10.34 { 10.35 evtchn_port_t port; 10.36 10.37 - if ( dorw(xce_handle, (char *)&port, sizeof(port), 0) == -1 ) 10.38 + if ( read_exact(xce_handle, (char *)&port, sizeof(port)) == -1 ) 10.39 return -1; 10.40 10.41 return port; 10.42 @@ -244,7 +219,7 @@ xc_evtchn_pending(int xce_handle) 10.43 10.44 int xc_evtchn_unmask(int xce_handle, evtchn_port_t port) 10.45 { 10.46 - return dorw(xce_handle, (char *)&port, sizeof(port), 1); 10.47 + return write_exact(xce_handle, (char *)&port, sizeof(port)); 10.48 } 10.49 10.50 /* Optionally flush file to disk and discard page cache */