direct-io.hg

changeset 5543:9ffcc5189703

bitkeeper revision 1.1726.2.3 (42ba7587nwhnhQJ_hDkHTk8kTKtAFw)

Make sure we save errno across error-path printfs and munlocks. Based
on a patch from Anthony Liguori.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Thu Jun 23 08:40:39 2005 +0000 (2005-06-23)
parents 8f480ca525ca
children 6446c0567c5c
files tools/libxc/xc_domain.c tools/libxc/xc_evtchn.c tools/libxc/xc_gnttab.c tools/libxc/xc_misc.c tools/libxc/xc_private.c tools/libxc/xc_private.h
line diff
     1.1 --- a/tools/libxc/xc_domain.c	Thu Jun 23 08:08:12 2005 +0000
     1.2 +++ b/tools/libxc/xc_domain.c	Thu Jun 23 08:40:39 2005 +0000
     1.3 @@ -128,7 +128,7 @@ int xc_domain_get_vcpu_context(int xc_ha
     1.4                                 u32 vcpu,
     1.5                                 vcpu_guest_context_t *ctxt)
     1.6  {
     1.7 -    int rc, errno_saved;
     1.8 +    int rc;
     1.9      dom0_op_t op;
    1.10  
    1.11      op.cmd = DOM0_GETVCPUCONTEXT;
    1.12 @@ -143,11 +143,7 @@ int xc_domain_get_vcpu_context(int xc_ha
    1.13      rc = do_dom0_op(xc_handle, &op);
    1.14  
    1.15      if ( ctxt != NULL )
    1.16 -    {
    1.17 -        errno_saved = errno;
    1.18 -        (void)munlock(ctxt, sizeof(*ctxt));
    1.19 -        errno = errno_saved;
    1.20 -    }
    1.21 +        safe_munlock(ctxt, sizeof(*ctxt));
    1.22  
    1.23      if ( rc > 0 )
    1.24          return -ESRCH;
     2.1 --- a/tools/libxc/xc_evtchn.c	Thu Jun 23 08:08:12 2005 +0000
     2.2 +++ b/tools/libxc/xc_evtchn.c	Thu Jun 23 08:40:39 2005 +0000
     2.3 @@ -26,7 +26,7 @@ static int do_evtchn_op(int xc_handle, e
     2.4      if ((ret = do_xen_hypercall(xc_handle, &hypercall)) < 0)
     2.5          ERROR("do_evtchn_op: HYPERVISOR_event_channel_op failed: %d", ret);
     2.6  
     2.7 -    (void)munlock(op, sizeof(*op));
     2.8 +    safe_munlock(op, sizeof(*op));
     2.9   out:
    2.10      return ret;
    2.11  }
     3.1 --- a/tools/libxc/xc_gnttab.c	Thu Jun 23 08:08:12 2005 +0000
     3.2 +++ b/tools/libxc/xc_gnttab.c	Thu Jun 23 08:40:39 2005 +0000
     3.3 @@ -33,7 +33,7 @@ do_gnttab_op( int xc_handle,
     3.4      if ( (ret = do_xen_hypercall(xc_handle, &hypercall)) < 0 )
     3.5          ERROR("do_gnttab_op: HYPERVISOR_grant_table_op failed: %d", ret);
     3.6  
     3.7 -    (void)munlock(op, sizeof(*op));
     3.8 +    safe_munlock(op, sizeof(*op));
     3.9   out:
    3.10      return ret;
    3.11  }
     4.1 --- a/tools/libxc/xc_misc.c	Thu Jun 23 08:08:12 2005 +0000
     4.2 +++ b/tools/libxc/xc_misc.c	Thu Jun 23 08:40:39 2005 +0000
     4.3 @@ -43,7 +43,7 @@ int xc_readconsolering(int xc_handle,
     4.4          *pnr_chars = op.u.readconsole.count;
     4.5      }
     4.6  
     4.7 -    (void)munlock(buffer, nr_chars);
     4.8 +    safe_munlock(buffer, nr_chars);
     4.9  
    4.10      return ret;
    4.11  }    
     5.1 --- a/tools/libxc/xc_private.c	Thu Jun 23 08:08:12 2005 +0000
     5.2 +++ b/tools/libxc/xc_private.c	Thu Jun 23 08:40:39 2005 +0000
     5.3 @@ -22,8 +22,10 @@ void *xc_map_foreign_batch(int xc_handle
     5.4      ioctlx.arr=arr;
     5.5      if ( ioctl( xc_handle, IOCTL_PRIVCMD_MMAPBATCH, &ioctlx ) < 0 )
     5.6      {
     5.7 +        int saved_errno = errno;
     5.8  	perror("XXXXXXXX");
     5.9 -	munmap(addr, num*PAGE_SIZE);
    5.10 +	(void)munmap(addr, num*PAGE_SIZE);
    5.11 +        errno = saved_errno;
    5.12  	return NULL;
    5.13      }
    5.14      return addr;
    5.15 @@ -51,7 +53,9 @@ void *xc_map_foreign_range(int xc_handle
    5.16      entry.npages=(size+PAGE_SIZE-1)>>PAGE_SHIFT;
    5.17      if ( ioctl( xc_handle, IOCTL_PRIVCMD_MMAP, &ioctlx ) < 0 )
    5.18      {
    5.19 -	munmap(addr, size);
    5.20 +        int saved_errno = errno;
    5.21 +	(void)munmap(addr, size);
    5.22 +        errno = saved_errno;
    5.23  	return NULL;
    5.24      }
    5.25      return addr;
    5.26 @@ -134,8 +138,8 @@ static int flush_mmu_updates(int xc_hand
    5.27      }
    5.28  
    5.29      mmu->idx = 0;
    5.30 -    
    5.31 -    (void)munlock(mmu->updates, sizeof(mmu->updates));
    5.32 +
    5.33 +    safe_munlock(mmu->updates, sizeof(mmu->updates));
    5.34  
    5.35   out:
    5.36      return err;
    5.37 @@ -232,7 +236,7 @@ int xc_get_pfn_list(int xc_handle,
    5.38  
    5.39      ret = do_dom0_op(xc_handle, &op);
    5.40  
    5.41 -    (void)munlock(pfn_buf, max_pfns * sizeof(unsigned long));
    5.42 +    safe_munlock(pfn_buf, max_pfns * sizeof(unsigned long));
    5.43  
    5.44  #if 0
    5.45  #ifdef DEBUG
     6.1 --- a/tools/libxc/xc_private.h	Thu Jun 23 08:08:12 2005 +0000
     6.2 +++ b/tools/libxc/xc_private.h	Thu Jun 23 08:40:39 2005 +0000
     6.3 @@ -101,12 +101,28 @@ struct load_funcs
     6.4      loadimagefunc loadimage;
     6.5  };
     6.6  
     6.7 -#define ERROR(_m, _a...)  \
     6.8 -    fprintf(stderr, "ERROR: " _m "\n" , ## _a )
     6.9 +#define ERROR(_m, _a...)                                \
    6.10 +do {                                                    \
    6.11 +    int __saved_errno = errno;                          \
    6.12 +    fprintf(stderr, "ERROR: " _m "\n" , ## _a );        \
    6.13 +    errno = __saved_errno;                              \
    6.14 +} while (0)
    6.15 +
    6.16  
    6.17 -#define PERROR(_m, _a...) \
    6.18 -    fprintf(stderr, "ERROR: " _m " (%d = %s)\n" , ## _a , \
    6.19 -            errno, strerror(errno))
    6.20 +#define PERROR(_m, _a...)                                       \
    6.21 +do {                                                            \
    6.22 +    int __saved_errno = errno;                                  \
    6.23 +    fprintf(stderr, "ERROR: " _m " (%d = %s)\n" , ## _a ,       \
    6.24 +            __saved_errno, strerror(__saved_errno));            \
    6.25 +    errno = __saved_errno;                                      \
    6.26 +} while (0)
    6.27 +
    6.28 +static inline void safe_munlock(const void *addr, size_t len)
    6.29 +{
    6.30 +    int saved_errno = errno;
    6.31 +    (void)munlock(addr, len);
    6.32 +    errno = saved_errno;
    6.33 +}
    6.34  
    6.35  static inline int do_privcmd(int xc_handle,
    6.36                               unsigned int cmd, 
    6.37 @@ -125,7 +141,7 @@ static inline int do_xen_hypercall(int x
    6.38  
    6.39  static inline int do_dom0_op(int xc_handle, dom0_op_t *op)
    6.40  {
    6.41 -    int ret = -1, errno_saved;
    6.42 +    int ret = -1;
    6.43      privcmd_hypercall_t hypercall;
    6.44  
    6.45      op->interface_version = DOM0_INTERFACE_VERSION;
    6.46 @@ -146,9 +162,7 @@ static inline int do_dom0_op(int xc_hand
    6.47                      " rebuild the user-space tool set?\n");
    6.48      }
    6.49  
    6.50 -    errno_saved = errno;
    6.51 -    (void)munlock(op, sizeof(*op));
    6.52 -    errno = errno_saved;
    6.53 +    safe_munlock(op, sizeof(*op));
    6.54  
    6.55   out1:
    6.56      return ret;
    6.57 @@ -163,7 +177,6 @@ static inline int do_dom_mem_op(int     
    6.58  {
    6.59      privcmd_hypercall_t hypercall;
    6.60      long ret = -EINVAL;
    6.61 -    int errno_saved;
    6.62  
    6.63      hypercall.op     = __HYPERVISOR_dom_mem_op;
    6.64      hypercall.arg[0] = (unsigned long)memop;
    6.65 @@ -186,11 +199,7 @@ static inline int do_dom_mem_op(int     
    6.66      }
    6.67  
    6.68      if ( extent_list != NULL )
    6.69 -    {
    6.70 -        errno_saved = errno;
    6.71 -        (void)munlock(extent_list, nr_extents*sizeof(unsigned long));
    6.72 -        errno = errno_saved;
    6.73 -    }
    6.74 +        safe_munlock(extent_list, nr_extents*sizeof(unsigned long));
    6.75  
    6.76   out1:
    6.77      return ret;
    6.78 @@ -204,7 +213,6 @@ static inline int do_mmuext_op(
    6.79  {
    6.80      privcmd_hypercall_t hypercall;
    6.81      long ret = -EINVAL;
    6.82 -    int errno_saved;
    6.83  
    6.84      hypercall.op     = __HYPERVISOR_mmuext_op;
    6.85      hypercall.arg[0] = (unsigned long)op;
    6.86 @@ -224,9 +232,7 @@ static inline int do_mmuext_op(
    6.87                      " rebuild the user-space tool set?\n",ret,errno);
    6.88      }
    6.89  
    6.90 -    errno_saved = errno;
    6.91 -    (void)munlock(op, nr_ops*sizeof(*op));
    6.92 -    errno = errno_saved;
    6.93 +    safe_munlock(op, nr_ops*sizeof(*op));
    6.94  
    6.95   out1:
    6.96      return ret;