ia64/xen-unstable

changeset 4502:b5bf5415d400

bitkeeper revision 1.1159.258.90 (425a82f9SNVZLPiwpO07IDSTr_VE7g)

[PATCH] stream fixes for migration

I've attached a patch for libxutil/libxc. This fixes one of the hangs =
I've seen during migrations. It applies against 2.0 and 2.0-testing.

Changes:
* Encountering EOF or error when xfrd reads from stream could cause an =
infinite loop.
* Cleaned up the closing of streams.
* Fixed several memory leaks.

Signed-off-by: Charles Coffing <ccoffing@novell.com>
author ccoffing@novell.com[iap10]
date Mon Apr 11 14:00:25 2005 +0000 (2005-04-11)
parents b719ddfcd506
children e82a0bb5637c
files BitKeeper/etc/logging_ok tools/libxc/xc_io.h tools/libxc/xc_linux_save.c tools/libxutil/file_stream.c tools/libxutil/gzip_stream.c tools/libxutil/iostream.h
line diff
     1.1 --- a/BitKeeper/etc/logging_ok	Tue Apr 05 08:04:19 2005 +0000
     1.2 +++ b/BitKeeper/etc/logging_ok	Mon Apr 11 14:00:25 2005 +0000
     1.3 @@ -10,6 +10,7 @@ br260@br260.wolfson.cam.ac.uk
     1.4  br260@labyrinth.cl.cam.ac.uk
     1.5  br260@laudney.cl.cam.ac.uk
     1.6  bren@anvil.research
     1.7 +ccoffing@novell.com
     1.8  cl349@arcadians.cl.cam.ac.uk
     1.9  cl349@firebug.cl.cam.ac.uk
    1.10  cl349@freefall.cl.cam.ac.uk
     2.1 --- a/tools/libxc/xc_io.h	Tue Apr 05 08:04:19 2005 +0000
     2.2 +++ b/tools/libxc/xc_io.h	Mon Apr 11 14:00:25 2005 +0000
     2.3 @@ -45,14 +45,14 @@ static inline int xcio_read(XcIOContext 
     2.4      int rc;
     2.5  
     2.6      rc = IOStream_read(ctxt->io, buf, n);
     2.7 -    return (rc == n ? 0 : rc);
     2.8 +    return (rc == n ? 0 : -1);
     2.9  }
    2.10  
    2.11  static inline int xcio_write(XcIOContext *ctxt, void *buf, int n){
    2.12      int rc;
    2.13  
    2.14      rc = IOStream_write(ctxt->io, buf, n);
    2.15 -    return (rc == n ? 0 : rc);
    2.16 +    return (rc == n ? 0 : -1);
    2.17  }
    2.18  
    2.19  static inline int xcio_flush(XcIOContext *ctxt){
     3.1 --- a/tools/libxc/xc_linux_save.c	Tue Apr 05 08:04:19 2005 +0000
     3.2 +++ b/tools/libxc/xc_linux_save.c	Mon Apr 11 14:00:25 2005 +0000
     3.3 @@ -172,7 +172,6 @@ static int xcio_ratewrite(XcIOContext *i
     3.4      struct timeval now;
     3.5      struct timespec delay;
     3.6      long long delta;
     3.7 -    int rc;
     3.8  
     3.9      if (START_MBIT_RATE == 0)
    3.10  	return xcio_write(ioctxt, buf, n);
    3.11 @@ -212,8 +211,7 @@ static int xcio_ratewrite(XcIOContext *i
    3.12  	    }
    3.13  	}
    3.14      }
    3.15 -    rc = IOStream_write(ioctxt->io, buf, n);
    3.16 -    return (rc == n ? 0 : rc);
    3.17 +    return xcio_write(ioctxt, buf, n);
    3.18  }
    3.19  
    3.20  static int print_stats( int xc_handle, u32 domid, 
     4.1 --- a/tools/libxutil/file_stream.c	Tue Apr 05 08:04:19 2005 +0000
     4.2 +++ b/tools/libxutil/file_stream.c	Mon Apr 11 14:00:25 2005 +0000
     4.3 @@ -151,7 +151,12 @@ static int file_error(IOStream *s){
     4.4   * @return result of the close
     4.5   */
     4.6  static int file_close(IOStream *s){
     4.7 -    return fclose(get_file(s));
     4.8 +    int result = 0;
     4.9 +    if (s->data){
    4.10 +        result = fclose(get_file(s));
    4.11 +        s->data = (void*)0;
    4.12 +    }
    4.13 +    return result;
    4.14  }
    4.15  
    4.16  /** Free a file stream.
    4.17 @@ -159,7 +164,7 @@ static int file_close(IOStream *s){
    4.18   * @param s file stream
    4.19   */
    4.20  static void file_free(IOStream *s){
    4.21 -    // Do nothing - fclose does it all?
    4.22 +    file_close(s);
    4.23  }
    4.24  
    4.25  /** Create an IOStream for a stream.
    4.26 @@ -189,7 +194,6 @@ IOStream *file_stream_fopen(const char *
    4.27  	io = file_stream_new(fin);
    4.28  	if(!io){
    4.29  	    fclose(fin);
    4.30 -	    //free(fin); // fclose frees ?
    4.31  	}
    4.32      }
    4.33      return io;
    4.34 @@ -199,13 +203,16 @@ IOStream *file_stream_fopen(const char *
    4.35   *
    4.36   * @param fd file descriptor
    4.37   * @param flags giving the mode to open in (as for fdopen())
    4.38 - * @return new stream for the open file, or 0 if failed
    4.39 + * @return new stream for the open file, or 0 if failed.  Always takes
    4.40 + *         ownership of fd.
    4.41   */
    4.42  IOStream *file_stream_fdopen(int fd, const char *flags){
    4.43      IOStream *io = 0;
    4.44      FILE *fin = fdopen(fd, flags);
    4.45      if(fin){
    4.46 -	io = file_stream_new(fin);
    4.47 +        io = file_stream_new(fin);
    4.48 +        if(!io)
    4.49 +            fclose(fin);
    4.50      }
    4.51      return io;
    4.52  }
     5.1 --- a/tools/libxutil/gzip_stream.c	Tue Apr 05 08:04:19 2005 +0000
     5.2 +++ b/tools/libxutil/gzip_stream.c	Mon Apr 11 14:00:25 2005 +0000
     5.3 @@ -107,7 +107,12 @@ static int gzip_error(IOStream *s){
     5.4   * @return result of the close
     5.5   */
     5.6  static int gzip_close(IOStream *s){
     5.7 -    return gzclose(get_gzfile(s));
     5.8 +    int result = 0;
     5.9 +    if (s->data){
    5.10 +        result = gzclose(get_gzfile(s));
    5.11 +        s->data = (void*)0;
    5.12 +    }
    5.13 +    return result;
    5.14  }
    5.15  
    5.16  /** Free a gzip stream.
    5.17 @@ -115,7 +120,7 @@ static int gzip_close(IOStream *s){
    5.18   * @param s gzip stream
    5.19   */
    5.20  static void gzip_free(IOStream *s){
    5.21 -    // Do nothing - fclose does it all?
    5.22 +    gzip_close(s);
    5.23  }
    5.24  
    5.25  /** Create an IOStream for a gzip stream.
    5.26 @@ -143,11 +148,10 @@ IOStream *gzip_stream_fopen(const char *
    5.27      gzFile *fgz;
    5.28      fgz = gzopen(file, flags);
    5.29      if(fgz){
    5.30 -	io = gzip_stream_new(fgz);
    5.31 -	if(!io){
    5.32 -	    gzclose(fgz);
    5.33 -	    //free(fgz); // gzclose frees ?
    5.34 -	}
    5.35 +        io = gzip_stream_new(fgz);
    5.36 +        if(!io){
    5.37 +            gzclose(fgz);
    5.38 +        }
    5.39      }
    5.40      return io;
    5.41  }
    5.42 @@ -156,14 +160,17 @@ IOStream *gzip_stream_fopen(const char *
    5.43   *
    5.44   * @param fd file descriptor
    5.45   * @param flags giving the mode to open in (as for fdopen())
    5.46 - * @return new stream for the open file, or NULL if failed
    5.47 + * @return new stream for the open file, or NULL if failed.  Always takes
    5.48 + *         ownership of fd.
    5.49   */
    5.50  IOStream *gzip_stream_fdopen(int fd, const char *flags){
    5.51      IOStream *io = NULL;
    5.52      gzFile *fgz;
    5.53      fgz = gzdopen(fd, flags);
    5.54      if(fgz){
    5.55 -	io = gzip_stream_new(fgz);
    5.56 +        io = gzip_stream_new(fgz);
    5.57 +        if(!io)
    5.58 +            gzclose(fgz);
    5.59      }
    5.60      return io;
    5.61  }
     6.1 --- a/tools/libxutil/iostream.h	Tue Apr 05 08:04:19 2005 +0000
     6.2 +++ b/tools/libxutil/iostream.h	Mon Apr 11 14:00:25 2005 +0000
     6.3 @@ -105,8 +105,11 @@ extern int IOStream_vprint(IOStream *io,
     6.4   * @return if ok, number of bytes read, otherwise negative error code
     6.5   */
     6.6  static inline int IOStream_read(IOStream *stream, void *buf, size_t n){
     6.7 -    int result = 0;
     6.8 -    if(stream->closed) goto exit;
     6.9 +    int result;
    6.10 +    if(stream->closed){
    6.11 +        result = IOSTREAM_EOF;
    6.12 +        goto exit;
    6.13 +    }
    6.14      if(!stream->methods || !stream->methods->read){
    6.15          result = -EINVAL;
    6.16          goto exit;
    6.17 @@ -124,11 +127,14 @@ static inline int IOStream_read(IOStream
    6.18   * @param stream input
    6.19   * @param buf where to put input
    6.20   * @param n number of bytes to write
    6.21 - * @return if ok, number of bytes read, otherwise negative error code
    6.22 + * @return if ok, number of bytes written, otherwise negative error code
    6.23   */
    6.24  static inline int IOStream_write(IOStream *stream, const void *buf, size_t n){
    6.25 -    int result = 0;
    6.26 -    if(stream->closed) goto exit;
    6.27 +    int result;
    6.28 +    if(stream->closed){
    6.29 +        result = IOSTREAM_EOF;
    6.30 +        goto exit;
    6.31 +    }
    6.32      if(!stream->methods || !stream->methods->write){
    6.33          result = -EINVAL;
    6.34          goto exit;
    6.35 @@ -179,6 +185,7 @@ static inline int IOStream_close(IOStrea
    6.36      int err = 1;
    6.37      if(stream->methods && stream->methods->close){
    6.38          err = stream->methods->close(stream);
    6.39 +        stream->closed = 1;
    6.40      }
    6.41      return err;
    6.42  }
    6.43 @@ -189,7 +196,7 @@ static inline int IOStream_close(IOStrea
    6.44   * @return 1 if closed, 0 otherwise
    6.45   */
    6.46  static inline int IOStream_is_closed(IOStream *stream){
    6.47 -  return stream->closed;
    6.48 +    return stream->closed;
    6.49  }
    6.50  
    6.51  /** Free the memory used by the stream.
    6.52 @@ -197,11 +204,14 @@ static inline int IOStream_is_closed(IOS
    6.53   * @param stream to free
    6.54   */
    6.55  static inline void IOStream_free(IOStream *stream){
    6.56 -  if(stream->methods && stream->methods->free){
    6.57 -    stream->methods->free(stream);
    6.58 -  }
    6.59 -  *stream = (IOStream){};
    6.60 -  deallocate(stream);
    6.61 +    if(!stream->closed && stream->methods && stream->methods->close){
    6.62 +        stream->methods->close(stream);
    6.63 +    }
    6.64 +    if(stream->methods && stream->methods->free){
    6.65 +        stream->methods->free(stream);
    6.66 +    }
    6.67 +    *stream = (IOStream){};
    6.68 +    deallocate(stream);
    6.69  }
    6.70  
    6.71