ia64/xen-unstable

view tools/libxc/xg_private.c @ 7238:971e7c7411b3

Raise an exception if an error appears on the pipes to our children, and make
sure that the child's pipes are closed even under that exception. Move the
handling of POLLHUP to the end of the loop, so that we guarantee to read any
remaining data from the child if POLLHUP and POLLIN appear at the same time.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@ewan
date Thu Oct 06 10:13:11 2005 +0100 (2005-10-06)
parents 06d84bf87159
children c503c3887971
line source
1 /******************************************************************************
2 * xg_private.c
3 *
4 * Helper functions for the rest of the library.
5 */
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <zlib.h>
11 #include "xg_private.h"
13 char *xc_read_kernel_image(const char *filename, unsigned long *size)
14 {
15 int kernel_fd = -1;
16 gzFile kernel_gfd = NULL;
17 char *image = NULL;
18 unsigned int bytes;
20 if ( (kernel_fd = open(filename, O_RDONLY)) < 0 )
21 {
22 PERROR("Could not open kernel image");
23 goto out;
24 }
26 if ( (*size = xc_get_filesz(kernel_fd)) == 0 )
27 {
28 PERROR("Could not read kernel image");
29 goto out;
30 }
32 if ( (kernel_gfd = gzdopen(kernel_fd, "rb")) == NULL )
33 {
34 PERROR("Could not allocate decompression state for state file");
35 goto out;
36 }
38 if ( (image = malloc(*size)) == NULL )
39 {
40 PERROR("Could not allocate memory for kernel image");
41 goto out;
42 }
44 if ( (bytes = gzread(kernel_gfd, image, *size)) != *size )
45 {
46 PERROR("Error reading kernel image, could not"
47 " read the whole image (%d != %ld).", bytes, *size);
48 free(image);
49 image = NULL;
50 }
52 out:
53 if ( kernel_gfd != NULL )
54 gzclose(kernel_gfd);
55 else if ( kernel_fd >= 0 )
56 close(kernel_fd);
57 return image;
58 }
60 /*******************/
62 int pin_table(
63 int xc_handle, unsigned int type, unsigned long mfn, domid_t dom)
64 {
65 struct mmuext_op op;
67 op.cmd = type;
68 op.arg1.mfn = mfn;
70 if ( xc_mmuext_op(xc_handle, &op, 1, dom) < 0 )
71 return 1;
73 return 0;
74 }
76 /* This is shared between save and restore, and may generally be useful. */
77 unsigned long csum_page (void * page)
78 {
79 int i;
80 unsigned long *p = page;
81 unsigned long long sum=0;
83 for ( i = 0; i < (PAGE_SIZE/sizeof(unsigned long)); i++ )
84 sum += p[i];
86 return sum ^ (sum>>32);
87 }