direct-io.hg

changeset 15266:b515e66234e8

tools: warn_unused_result build fixes.

Note that some of the existing error checking for asprintf was wrong
for Linux. asprintf in glibc returns -1 on error, but leaves the
pointer uninitialized. Only the BSDs zero out the pointer on error.

Also, while fixing these warnings I saw several error paths that were
incorrect. This patch minimally fixes the warn_unused_result; more
complete error path cleanup will be a later patch.

Signed-off-by: Charles Coffing <ccoffing@novell.com>
author kfraser@localhost.localdomain
date Tue Jun 12 11:38:31 2007 +0100 (2007-06-12)
parents 634b7f7f8584
children be33028fcda5
files tools/blktap/drivers/blktapctrl.c tools/blktap/drivers/block-qcow.c tools/blktap/drivers/tapdisk.c tools/blktap/lib/xenbus.c tools/blktap/lib/xs_api.c tools/console/daemon/io.c
line diff
     1.1 --- a/tools/blktap/drivers/blktapctrl.c	Tue Jun 12 11:32:03 2007 +0100
     1.2 +++ b/tools/blktap/drivers/blktapctrl.c	Tue Jun 12 11:38:31 2007 +0100
     1.3 @@ -143,7 +143,8 @@ static int get_new_dev(int *major, int *
     1.4  		return -1;
     1.5  	}
     1.6  
     1.7 -	asprintf(&devname,"%s/%s%d",BLKTAP_DEV_DIR, BLKTAP_DEV_NAME, *minor);
     1.8 +	if (asprintf(&devname,"%s/%s%d",BLKTAP_DEV_DIR, BLKTAP_DEV_NAME, *minor) == -1)
     1.9 +		return -1;
    1.10  	make_blktap_dev(devname,*major,*minor);	
    1.11  	DPRINTF("Received device id %d and major %d, "
    1.12  		"sent domid %d and be_id %d\n",
    1.13 @@ -495,20 +496,27 @@ int blktapctrl_new_blkif(blkif_t *blkif)
    1.14  
    1.15  		if (!exist) {
    1.16  			DPRINTF("Process does not exist:\n");
    1.17 -			asprintf(&rdctldev, 
    1.18 -				 "%s/tapctrlread%d", BLKTAP_CTRL_DIR, minor);
    1.19 -			blkif->fds[READ] = open_ctrl_socket(rdctldev);
    1.20 +			if (asprintf(&rdctldev,
    1.21 +				     "%s/tapctrlread%d", BLKTAP_CTRL_DIR, minor) == -1)
    1.22 +				return -1;
    1.23 +			if (asprintf(&wrctldev,
    1.24 +				     "%s/tapctrlwrite%d", BLKTAP_CTRL_DIR, minor) == -1) {
    1.25 +				free(rdctldev);
    1.26 +				return -1;
    1.27 +			}
    1.28 +			if (asprintf(&cmd, "tapdisk %s %s", wrctldev, rdctldev) == -1) {
    1.29 +				free(rdctldev);
    1.30 +				free(wrctldev);
    1.31 +				return -1;
    1.32 +			}
    1.33  
    1.34 -
    1.35 -			asprintf(&wrctldev, 
    1.36 -				 "%s/tapctrlwrite%d", BLKTAP_CTRL_DIR, minor);
    1.37 +			blkif->fds[READ] = open_ctrl_socket(rdctldev);
    1.38  			blkif->fds[WRITE] = open_ctrl_socket(wrctldev);
    1.39  			
    1.40  			if (blkif->fds[READ] == -1 || blkif->fds[WRITE] == -1) 
    1.41  				goto fail;
    1.42  
    1.43  			/*launch the new process*/
    1.44 -			asprintf(&cmd, "tapdisk %s %s", wrctldev, rdctldev);
    1.45  			DPRINTF("Launching process, CMDLINE [%s]\n",cmd);
    1.46  			if (system(cmd) == -1) {
    1.47  				DPRINTF("Unable to fork, cmdline: [%s]\n",cmd);
    1.48 @@ -692,7 +700,8 @@ int main(int argc, char *argv[])
    1.49  	register_new_unmap_hook(unmap_blktapctrl);
    1.50  
    1.51  	/* Attach to blktap0 */
    1.52 -	asprintf(&devname,"%s/%s0", BLKTAP_DEV_DIR, BLKTAP_DEV_NAME);
    1.53 +	if (asprintf(&devname,"%s/%s0", BLKTAP_DEV_DIR, BLKTAP_DEV_NAME) == -1)
    1.54 +                goto open_failed;
    1.55  	if ((ret = xc_find_device_number("blktap0")) < 0) {
    1.56  		DPRINTF("couldn't find device number for 'blktap0'\n");
    1.57  		goto open_failed;
     2.1 --- a/tools/blktap/drivers/block-qcow.c	Tue Jun 12 11:32:03 2007 +0100
     2.2 +++ b/tools/blktap/drivers/block-qcow.c	Tue Jun 12 11:38:31 2007 +0100
     2.3 @@ -871,7 +871,10 @@ int tdqcow_open (struct disk_driver *dd,
     2.4  	}
     2.5  
     2.6  	s->fd = fd;
     2.7 -	asprintf(&s->name,"%s", name);
     2.8 +	if (asprintf(&s->name,"%s", name) == -1) {
     2.9 +		close(fd);
    2.10 +		return -1;
    2.11 +	}
    2.12  
    2.13  	ASSERT(sizeof(QCowHeader) + sizeof(QCowHeader_ext) < 512);
    2.14  
    2.15 @@ -1165,7 +1168,7 @@ int tdqcow_close(struct disk_driver *dd)
    2.16  		offset = sizeof(QCowHeader) + sizeof(uint32_t);
    2.17  		lseek(fd, offset, SEEK_SET);
    2.18  		out = cpu_to_be32(cksum);
    2.19 -		write(fd, &out, sizeof(uint32_t));
    2.20 +		if (write(fd, &out, sizeof(uint32_t))) ;
    2.21  		close(fd);
    2.22  	}
    2.23  
     3.1 --- a/tools/blktap/drivers/tapdisk.c	Tue Jun 12 11:32:03 2007 +0100
     3.2 +++ b/tools/blktap/drivers/tapdisk.c	Tue Jun 12 11:38:31 2007 +0100
     3.3 @@ -220,7 +220,8 @@ static int map_new_dev(struct td_state *
     3.4  	fd_list_entry_t *ptr;
     3.5  	int page_size;
     3.6  
     3.7 -	asprintf(&devname,"%s/%s%d", BLKTAP_DEV_DIR, BLKTAP_DEV_NAME, minor);
     3.8 +	if (asprintf(&devname,"%s/%s%d", BLKTAP_DEV_DIR, BLKTAP_DEV_NAME, minor) == -1)
     3.9 +		return -1;
    3.10  	tap_fd = open(devname, O_RDWR);
    3.11  	if (tap_fd == -1) 
    3.12  	{
     4.1 --- a/tools/blktap/lib/xenbus.c	Tue Jun 12 11:32:03 2007 +0100
     4.2 +++ b/tools/blktap/lib/xenbus.c	Tue Jun 12 11:38:31 2007 +0100
     4.3 @@ -360,8 +360,7 @@ int add_blockdevice_probe_watch(struct x
     4.4  	char *path;
     4.5  	struct xenbus_watch *vbd_watch;
     4.6  	
     4.7 -	asprintf(&path, "/local/domain/%s/backend/tap", domid);
     4.8 -	if (path == NULL) 
     4.9 +	if (asprintf(&path, "/local/domain/%s/backend/tap", domid) == -1)
    4.10  		return -ENOMEM;
    4.11  	
    4.12  	vbd_watch = (struct xenbus_watch *)malloc(sizeof(struct xenbus_watch));
    4.13 @@ -399,8 +398,7 @@ int watch_for_domid(struct xs_handle *h)
    4.14  	struct xenbus_watch *domid_watch;
    4.15  	char *path = NULL;
    4.16  
    4.17 -	asprintf(&path, "/local/domain");
    4.18 -	if (path == NULL) 
    4.19 +	if (asprintf(&path, "/local/domain") == -1)
    4.20  		return -ENOMEM;
    4.21  
    4.22  	domid_watch = malloc(sizeof(struct xenbus_watch));
     5.1 --- a/tools/blktap/lib/xs_api.c	Tue Jun 12 11:32:03 2007 +0100
     5.2 +++ b/tools/blktap/lib/xs_api.c	Tue Jun 12 11:38:31 2007 +0100
     5.3 @@ -126,10 +126,12 @@ int xs_printf(struct xs_handle *h, const
     5.4  	ret = vasprintf(&buf, fmt, ap);
     5.5  	va_end(ap);
     5.6  	
     5.7 -	asprintf(&path, "%s/%s", dir, node);
     5.8 -	
     5.9 -	if ((path == NULL) || (buf == NULL))
    5.10 -		return 0;
    5.11 +	if (ret == -1)
    5.12 +		return ENOMEM;
    5.13 +	if (asprintf(&path, "%s/%s", dir, node) == -1) {
    5.14 +		free(buf);
    5.15 +		return ENOMEM;
    5.16 +	}
    5.17  
    5.18  	ret = xs_write(h, XBT_NULL, path, buf, strlen(buf)+1);
    5.19  	
    5.20 @@ -180,10 +182,11 @@ char *get_dom_domid(struct xs_handle *h)
    5.21  	
    5.22  	e = xs_directory(h, xth, "/local/domain", &num);
    5.23  	if (e == NULL)
    5.24 -		return NULL;
    5.25 +		goto done;
    5.26  
    5.27  	for (i = 0; (i < num) && (domid == NULL); i++) {
    5.28 -		asprintf(&path, "/local/domain/%s/name", e[i]);
    5.29 +		if (asprintf(&path, "/local/domain/%s/name", e[i]) == -1)
    5.30 +			break;
    5.31  		val = xs_read(h, xth, path, &len);
    5.32  		free(path);
    5.33  		if (val == NULL)
    5.34 @@ -191,29 +194,31 @@ char *get_dom_domid(struct xs_handle *h)
    5.35  		
    5.36  		if (strcmp(val, DOMNAME) == 0) {
    5.37  			/* match! */
    5.38 -			asprintf(&path, "/local/domain/%s/domid", e[i]);
    5.39 +			if (asprintf(&path, "/local/domain/%s/domid", e[i]) == -1) {
    5.40 +				free(val);
    5.41 +				break;
    5.42 +			}
    5.43  			domid = xs_read(h, xth, path, &len);
    5.44  			free(path);
    5.45  		}
    5.46  		free(val);
    5.47  	}
    5.48 +done:
    5.49  	xs_transaction_end(h, xth, 0);
    5.50 -	
    5.51 -	free(e);
    5.52 +	if (e)
    5.53 +		free(e);
    5.54  	return domid;
    5.55  }
    5.56  
    5.57  int convert_dev_name_to_num(char *name) {
    5.58 -	char *p_sd, *p_hd, *p_xvd, *p_plx, *p, *alpha,*ptr;
    5.59 +	char *p, *ptr;
    5.60  	int majors[10] = {3,22,33,34,56,57,88,89,90,91};
    5.61  	int maj,i,ret = 0;
    5.62 -
    5.63 -	asprintf(&p_sd,"/dev/sd");
    5.64 -	asprintf(&p_hd,"/dev/hd");
    5.65 -	asprintf(&p_xvd,"/dev/xvd");
    5.66 -	asprintf(&p_plx,"plx");
    5.67 -	asprintf(&alpha,"abcdefghijklmnop");
    5.68 -	
    5.69 +	char *p_sd = "/dev/sd";
    5.70 +	char *p_hd = "/dev/hd";
    5.71 +	char *p_xvd = "/dev/xvd";
    5.72 +	char *p_plx = "plx";
    5.73 +	char *alpha = "abcdefghijklmnop";
    5.74  
    5.75  	if (strstr(name, p_sd) != NULL) {
    5.76  		p = name + strlen(p_sd);
    5.77 @@ -251,12 +256,6 @@ int convert_dev_name_to_num(char *name) 
    5.78  		ret = BASE_DEV_VAL;
    5.79  	}
    5.80  
    5.81 -	free(p_sd);
    5.82 -	free(p_hd);
    5.83 -	free(p_xvd);
    5.84 -	free(p_plx);
    5.85 -	free(alpha);
    5.86 -
    5.87  	return ret;
    5.88  }
    5.89  
     6.1 --- a/tools/console/daemon/io.c	Tue Jun 12 11:32:03 2007 +0100
     6.2 +++ b/tools/console/daemon/io.c	Tue Jun 12 11:38:31 2007 +0100
     6.3 @@ -303,7 +303,10 @@ int xs_gather(struct xs_handle *xs, cons
     6.4  		void *result = va_arg(ap, void *);
     6.5  		char *p;
     6.6  
     6.7 -		asprintf(&path, "%s/%s", dir, name);
     6.8 +		if (asprintf(&path, "%s/%s", dir, name) == -1) {
     6.9 +			ret = ENOMEM;
    6.10 +			break;
    6.11 +		}
    6.12  		p = xs_read(xs, XBT_NULL, path, NULL);
    6.13  		free(path);
    6.14  		if (p == NULL) {