[Lguest] [PATCH 2/3] tools: lguest: use EXIT_FAILURE

Davidlohr Bueso dave at gnu.org
Tue Aug 14 03:48:44 EST 2012


From: Davidlohr Bueso <dave at gnu.org>

Instead of exiting with 1.

Signed-off-by: Davidlohr Bueso <dave at gnu.org>
---
 tools/lguest/lguest.c |  120 ++++++++++++++++++++++++------------------------
 1 files changed, 60 insertions(+), 60 deletions(-)

diff --git a/tools/lguest/lguest.c b/tools/lguest/lguest.c
index 931b6ff..ea8cccd 100644
--- a/tools/lguest/lguest.c
+++ b/tools/lguest/lguest.c
@@ -196,9 +196,9 @@ static void *_convert(struct iovec *iov, size_t size, size_t align,
 		      const char *name)
 {
 	if (iov->iov_len != size)
-		errx(1, "Bad iovec size %zu for %s", iov->iov_len, name);
+		errx(EXIT_FAILURE, "Bad iovec size %zu for %s", iov->iov_len, name);
 	if ((unsigned long)iov->iov_base % align != 0)
-		errx(1, "Bad alignment %p for %s", iov->iov_base, name);
+		errx(EXIT_FAILURE, "Bad alignment %p for %s", iov->iov_base, name);
 	return iov->iov_base;
 }
 
@@ -285,7 +285,7 @@ static int open_or_die(const char *name, int flags)
 {
 	int fd = open(name, flags);
 	if (fd < 0)
-		err(1, "Failed to open %s", name);
+		err(EXIT_FAILURE, "Failed to open %s", name);
 	return fd;
 }
 
@@ -304,11 +304,11 @@ static void *map_zeroed_pages(unsigned int num)
 		    PROT_NONE, MAP_PRIVATE, fd, 0);
 
 	if (addr == MAP_FAILED)
-		err(1, "Mmapping %u pages of /dev/zero", num);
+		err(EXIT_FAILURE, "Mmapping %u pages of /dev/zero", num);
 
 	if (mprotect(addr + getpagesize(), getpagesize() * num,
 		     PROT_READ|PROT_WRITE) == -1)
-		err(1, "mprotect rw %u pages failed", num);
+		err(EXIT_FAILURE, "mprotect rw %u pages failed", num);
 
 	/*
 	 * One neat mmap feature is that you can close the fd, and it
@@ -327,7 +327,7 @@ static void *get_pages(unsigned int num)
 
 	guest_limit += num * getpagesize();
 	if (guest_limit > guest_max)
-		errx(1, "Not enough memory for devices");
+		errx(EXIT_FAILURE, "Not enough memory for devices");
 	return addr;
 }
 
@@ -356,7 +356,7 @@ static void map_at(int fd, void *addr, unsigned long offset, unsigned long len)
 	/* pread does a seek and a read in one shot: saves a few lines. */
 	r = pread(fd, addr, len, offset);
 	if (r != len)
-		err(1, "Reading offset %lu len %lu gave %zi", offset, len, r);
+		err(EXIT_FAILURE, "Reading offset %lu len %lu gave %zi", offset, len, r);
 }
 
 /*
@@ -383,7 +383,7 @@ static unsigned long map_elf(int elf_fd, const Elf32_Ehdr *ehdr)
 	    || ehdr->e_machine != EM_386
 	    || ehdr->e_phentsize != sizeof(Elf32_Phdr)
 	    || ehdr->e_phnum < 1 || ehdr->e_phnum > 65536U/sizeof(Elf32_Phdr))
-		errx(1, "Malformed elf header");
+		errx(EXIT_FAILURE, "Malformed elf header");
 
 	/*
 	 * An ELF executable contains an ELF header and a number of "program"
@@ -393,9 +393,9 @@ static unsigned long map_elf(int elf_fd, const Elf32_Ehdr *ehdr)
 
 	/* We read in all the program headers at once: */
 	if (lseek(elf_fd, ehdr->e_phoff, SEEK_SET) < 0)
-		err(1, "Seeking to program headers");
+		err(EXIT_FAILURE, "Seeking to program headers");
 	if (read(elf_fd, phdr, sizeof(phdr)) != sizeof(phdr))
-		err(1, "Reading program headers");
+		err(EXIT_FAILURE, "Reading program headers");
 
 	/*
 	 * Try all the headers: there are usually only three.  A read-only one,
@@ -443,7 +443,7 @@ static unsigned long load_bzimage(int fd)
 
 	/* Inside the setup_hdr, we expect the magic "HdrS" */
 	if (memcmp(&boot.hdr.header, "HdrS", 4) != 0)
-		errx(1, "This doesn't look like a bzImage to me");
+		errx(EXIT_FAILURE, "This doesn't look like a bzImage to me");
 
 	/* Skip over the extra sectors of the header. */
 	lseek(fd, (boot.hdr.setup_sects+1) * 512, SEEK_SET);
@@ -467,7 +467,7 @@ static unsigned long load_kernel(int fd)
 
 	/* Read in the first few bytes. */
 	if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
-		err(1, "Reading kernel");
+		err(EXIT_FAILURE, "Reading kernel");
 
 	/* If it's an ELF file, it starts with "\177ELF" */
 	if (memcmp(hdr.e_ident, ELFMAG, SELFMAG) == 0)
@@ -508,7 +508,7 @@ static unsigned long load_initrd(const char *name, unsigned long mem)
 	ifd = open_or_die(name, O_RDONLY);
 	/* fstat() is needed to get the file size. */
 	if (fstat(ifd, &st) < 0)
-		err(1, "fstat() on initrd '%s'", name);
+		err(EXIT_FAILURE, "fstat() on initrd '%s'", name);
 
 	/*
 	 * We map the initrd at the top of memory, but mmap wants it to be
@@ -563,7 +563,7 @@ static void tell_kernel(unsigned long start)
 		guest_base, guest_base + guest_limit, guest_limit);
 	lguest_fd = open_or_die("/dev/lguest", O_RDWR);
 	if (write(lguest_fd, args, sizeof(args)) < 0)
-		err(1, "Writing to /dev/lguest");
+		err(EXIT_FAILURE, "Writing to /dev/lguest");
 }
 /*:*/
 
@@ -583,7 +583,7 @@ static void *_check_pointer(unsigned long addr, unsigned int size,
 	 * or addr + size wraps around.
 	 */
 	if ((addr + size) > guest_limit || (addr + size) < addr)
-		errx(1, "%s:%i: Invalid address %#lx", __FILE__, line, addr);
+		errx(EXIT_FAILURE, "%s:%i: Invalid address %#lx", __FILE__, line, addr);
 	/*
 	 * We return a pointer for the caller's convenience, now we know it's
 	 * safe to use.
@@ -613,7 +613,7 @@ static unsigned next_desc(struct vring_desc *desc,
 	wmb();
 
 	if (next >= max)
-		errx(1, "Desc next is %u", next);
+		errx(EXIT_FAILURE, "Desc next is %u", next);
 
 	return next;
 }
@@ -638,7 +638,7 @@ static void trigger_irq(struct virtqueue *vq)
 
 	/* Send the Guest an interrupt tell them we used something up. */
 	if (write(lguest_fd, buf, sizeof(buf)) != 0)
-		err(1, "Triggering irq %i", vq->config.irq);
+		err(EXIT_FAILURE, "Triggering irq %i", vq->config.irq);
 }
 
 /*
@@ -682,7 +682,7 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq,
 
 		/* Nothing new?  Wait for eventfd to tell us they refilled. */
 		if (read(vq->eventfd, &event, sizeof(event)) != sizeof(event))
-			errx(1, "Event read failed?");
+			errx(EXIT_FAILURE, "Event read failed?");
 
 		/* We don't need to be notified again. */
 		vq->vring.used->flags |= VRING_USED_F_NO_NOTIFY;
@@ -690,7 +690,7 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq,
 
 	/* Check it isn't doing very strange things with descriptor numbers. */
 	if ((u16)(vq->vring.avail->idx - last_avail) > vq->vring.num)
-		errx(1, "Guest moved used index from %u to %u",
+		errx(EXIT_FAILURE, "Guest moved used index from %u to %u",
 		     last_avail, vq->vring.avail->idx);
 
 	/*
@@ -702,7 +702,7 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq,
 
 	/* If their number is silly, that's a fatal mistake. */
 	if (head >= vq->vring.num)
-		errx(1, "Guest says index %u is available", head);
+		errx(EXIT_FAILURE, "Guest says index %u is available", head);
 
 	/* When we start there are none of either input nor output. */
 	*out_num = *in_num = 0;
@@ -717,7 +717,7 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq,
 	 */
 	if (desc[i].flags & VRING_DESC_F_INDIRECT) {
 		if (desc[i].len % sizeof(struct vring_desc))
-			errx(1, "Invalid size for indirect buffer table");
+			errx(EXIT_FAILURE, "Invalid size for indirect buffer table");
 
 		max = desc[i].len / sizeof(struct vring_desc);
 		desc = check_pointer(desc[i].addr, desc[i].len);
@@ -738,13 +738,13 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq,
 			 * to come before any input descriptors.
 			 */
 			if (*in_num)
-				errx(1, "Descriptor has out after in");
+				errx(EXIT_FAILURE, "Descriptor has out after in");
 			(*out_num)++;
 		}
 
 		/* If we've got too many, that implies a descriptor loop. */
 		if (*out_num + *in_num > max)
-			errx(1, "Looped descriptor");
+			errx(EXIT_FAILURE, "Looped descriptor");
 	} while ((i = next_desc(desc, i, max)) != max);
 
 	return head;
@@ -802,7 +802,7 @@ static void console_input(struct virtqueue *vq)
 	/* Make sure there's a descriptor available. */
 	head = wait_for_vq_desc(vq, iov, &out_num, &in_num);
 	if (out_num)
-		errx(1, "Output buffers in console in queue?");
+		errx(EXIT_FAILURE, "Output buffers in console in queue?");
 
 	/* Read into it.  This is where we usually wait. */
 	len = readv(STDIN_FILENO, iov, in_num);
@@ -855,7 +855,7 @@ static void console_output(struct virtqueue *vq)
 	/* We usually wait in here, for the Guest to give us something. */
 	head = wait_for_vq_desc(vq, iov, &out, &in);
 	if (in)
-		errx(1, "Input buffers in console output queue?");
+		errx(EXIT_FAILURE, "Input buffers in console output queue?");
 
 	/* writev can return a partial write, so we loop here. */
 	while (!iov_empty(iov, out)) {
@@ -893,7 +893,7 @@ static void net_output(struct virtqueue *vq)
 	/* We usually wait in here for the Guest to give us a packet. */
 	head = wait_for_vq_desc(vq, iov, &out, &in);
 	if (in)
-		errx(1, "Input buffers in net output queue?");
+		errx(EXIT_FAILURE, "Input buffers in net output queue?");
 	/*
 	 * Send the whole thing through to /dev/net/tun.  It expects the exact
 	 * same format: what a coincidence!
@@ -941,7 +941,7 @@ static void net_input(struct virtqueue *vq)
 	 */
 	head = wait_for_vq_desc(vq, iov, &out, &in);
 	if (out)
-		errx(1, "Output buffers in net input queue?");
+		errx(EXIT_FAILURE, "Output buffers in net input queue?");
 
 	/*
 	 * If it looks like we'll block reading from the tun device, send them
@@ -1030,7 +1030,7 @@ static void create_thread(struct virtqueue *vq)
 	/* Create a zero-initialized eventfd. */
 	vq->eventfd = eventfd(0, 0);
 	if (vq->eventfd < 0)
-		err(1, "Creating eventfd");
+		err(EXIT_FAILURE, "Creating eventfd");
 	args[2] = vq->eventfd;
 
 	/*
@@ -1038,7 +1038,7 @@ static void create_thread(struct virtqueue *vq)
 	 * does an LHCALL_NOTIFY for this vq.
 	 */
 	if (write(lguest_fd, &args, sizeof(args)) != 0)
-		err(1, "Attaching eventfd");
+		err(EXIT_FAILURE, "Attaching eventfd");
 
 	/*
 	 * CLONE_VM: because it has to access the Guest memory, and SIGCHLD so
@@ -1046,7 +1046,7 @@ static void create_thread(struct virtqueue *vq)
 	 */
 	vq->thread = clone(do_thread, stack + 32768, CLONE_VM | SIGCHLD, vq);
 	if (vq->thread == (pid_t)-1)
-		err(1, "Creating clone");
+		err(EXIT_FAILURE, "Creating clone");
 
 	/* We close our local copy now the child has it. */
 	close(vq->eventfd);
@@ -1096,7 +1096,7 @@ static void update_device_status(struct device *dev)
 			reset_device(dev);
 	} else {
 		if (dev->running)
-			err(1, "Device %s features finalized twice", dev->name);
+			err(EXIT_FAILURE, "Device %s features finalized twice", dev->name);
 		start_device(dev);
 	}
 }
@@ -1126,7 +1126,7 @@ static void handle_output(unsigned long addr)
 		for (vq = i->vq; vq; vq = vq->next) {
 			if (addr != vq->config.pfn*getpagesize())
 				continue;
-			errx(1, "Notification on %s before setup!", i->name);
+			errx(EXIT_FAILURE, "Notification on %s before setup!", i->name);
 		}
 	}
 
@@ -1136,7 +1136,7 @@ static void handle_output(unsigned long addr)
 	 * into a Guest.
 	 */
 	if (addr >= guest_limit)
-		errx(1, "Bad NOTIFY %#lx", addr);
+		errx(EXIT_FAILURE, "Bad NOTIFY %#lx", addr);
 
 	write(STDOUT_FILENO, from_guest_phys(addr),
 	      strnlen(from_guest_phys(addr), guest_limit - addr));
@@ -1182,7 +1182,7 @@ static struct lguest_device_desc *new_dev_desc(u16 type)
 
 	/* We only have one page for all the descriptors. */
 	if (p + sizeof(d) > (void *)devices.descpage + getpagesize())
-		errx(1, "Too many devices");
+		errx(EXIT_FAILURE, "Too many devices");
 
 	/* p might not be aligned, so we memcpy in. */
 	return memcpy(p, &d, sizeof(d));
@@ -1271,7 +1271,7 @@ static void set_config(struct device *dev, unsigned len, const void *conf)
 {
 	/* Check we haven't overflowed our single page. */
 	if (device_config(dev) + len > devices.descpage + getpagesize())
-		errx(1, "Too many devices");
+		errx(EXIT_FAILURE, "Too many devices");
 
 	/* Copy in the config information, and store the length. */
 	memcpy(device_config(dev), conf, len);
@@ -1376,7 +1376,7 @@ static u32 str2ip(const char *ipaddr)
 	unsigned int b[4];
 
 	if (sscanf(ipaddr, "%u.%u.%u.%u", &b[0], &b[1], &b[2], &b[3]) != 4)
-		errx(1, "Failed to parse IP address '%s'", ipaddr);
+		errx(EXIT_FAILURE, "Failed to parse IP address '%s'", ipaddr);
 	return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3];
 }
 
@@ -1385,7 +1385,7 @@ static void str2mac(const char *macaddr, unsigned char mac[6])
 	unsigned int m[6];
 	if (sscanf(macaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
 		   &m[0], &m[1], &m[2], &m[3], &m[4], &m[5]) != 6)
-		errx(1, "Failed to parse mac address '%s'", macaddr);
+		errx(EXIT_FAILURE, "Failed to parse mac address '%s'", macaddr);
 	mac[0] = m[0];
 	mac[1] = m[1];
 	mac[2] = m[2];
@@ -1407,17 +1407,17 @@ static void add_to_bridge(int fd, const char *if_name, const char *br_name)
 	struct ifreq ifr;
 
 	if (!*br_name)
-		errx(1, "must specify bridge name");
+		errx(EXIT_FAILURE, "must specify bridge name");
 
 	ifidx = if_nametoindex(if_name);
 	if (!ifidx)
-		errx(1, "interface %s does not exist!", if_name);
+		errx(EXIT_FAILURE, "interface %s does not exist!", if_name);
 
 	strncpy(ifr.ifr_name, br_name, IFNAMSIZ);
 	ifr.ifr_name[IFNAMSIZ-1] = '\0';
 	ifr.ifr_ifindex = ifidx;
 	if (ioctl(fd, SIOCBRADDIF, &ifr) < 0)
-		err(1, "can't add %s to bridge %s", if_name, br_name);
+		err(EXIT_FAILURE, "can't add %s to bridge %s", if_name, br_name);
 }
 
 /*
@@ -1438,10 +1438,10 @@ static void configure_device(int fd, const char *tapif, u32 ipaddr)
 	sin.sin_addr.s_addr = htonl(ipaddr);
 	memcpy(&ifr.ifr_addr, &sin, sizeof(sin));
 	if (ioctl(fd, SIOCSIFADDR, &ifr) != 0)
-		err(1, "Setting %s interface address", tapif);
+		err(EXIT_FAILURE, "Setting %s interface address", tapif);
 	ifr.ifr_flags = IFF_UP;
 	if (ioctl(fd, SIOCSIFFLAGS, &ifr) != 0)
-		err(1, "Bringing interface %s up", tapif);
+		err(EXIT_FAILURE, "Bringing interface %s up", tapif);
 }
 
 static int get_tun_device(char tapif[IFNAMSIZ])
@@ -1462,11 +1462,11 @@ static int get_tun_device(char tapif[IFNAMSIZ])
 	ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
 	strcpy(ifr.ifr_name, "tap%d");
 	if (ioctl(netfd, TUNSETIFF, &ifr) != 0)
-		err(1, "configuring /dev/net/tun");
+		err(EXIT_FAILURE, "configuring /dev/net/tun");
 
 	if (ioctl(netfd, TUNSETOFFLOAD,
 		  TUN_F_CSUM|TUN_F_TSO4|TUN_F_TSO6|TUN_F_TSO_ECN) != 0)
-		err(1, "Could not set features for tun device");
+		err(EXIT_FAILURE, "Could not set features for tun device");
 
 	/*
 	 * We don't need checksums calculated for packets coming in this
@@ -1510,7 +1510,7 @@ static void setup_tun_net(char *arg)
 	 */
 	ipfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
 	if (ipfd < 0)
-		err(1, "opening IP socket");
+		err(EXIT_FAILURE, "opening IP socket");
 
 	/* If the command line was --tunnet=bridge:<name> do bridging. */
 	if (!strncmp(BRIDGE_PFX, arg, strlen(BRIDGE_PFX))) {
@@ -1608,7 +1608,7 @@ static void blk_request(struct virtqueue *vq)
 	 * input buffer (to hold the result).
 	 */
 	if (out_num == 0 || in_num == 0)
-		errx(1, "Bad virtblk cmd %u out=%u in=%u",
+		errx(EXIT_FAILURE, "Bad virtblk cmd %u out=%u in=%u",
 		     head, out_num, in_num);
 
 	out = convert(&iov[0], struct virtio_blk_outhdr);
@@ -1635,7 +1635,7 @@ static void blk_request(struct virtqueue *vq)
 		 * if they try to write past end.
 		 */
 		if (lseek64(vblk->fd, off, SEEK_SET) != off)
-			err(1, "Bad seek to sector %llu", out->sector);
+			err(EXIT_FAILURE, "Bad seek to sector %llu", out->sector);
 
 		ret = writev(vblk->fd, iov+1, out_num-1);
 		verbose("WRITE to sector %llu: %i\n", out->sector, ret);
@@ -1649,7 +1649,7 @@ static void blk_request(struct virtqueue *vq)
 			/* Trim it back to the correct length */
 			ftruncate64(vblk->fd, vblk->len);
 			/* Die, bad Guest, die. */
-			errx(1, "Write past end %llu+%u", off, ret);
+			errx(EXIT_FAILURE, "Write past end %llu+%u", off, ret);
 		}
 
 		wlen = sizeof(*in);
@@ -1668,7 +1668,7 @@ static void blk_request(struct virtqueue *vq)
 		 * if they try to read past end.
 		 */
 		if (lseek64(vblk->fd, off, SEEK_SET) != off)
-			err(1, "Bad seek to sector %llu", out->sector);
+			err(EXIT_FAILURE, "Bad seek to sector %llu", out->sector);
 
 		ret = readv(vblk->fd, iov+1, in_num-1);
 		verbose("READ from sector %llu: %i\n", out->sector, ret);
@@ -1747,7 +1747,7 @@ static void rng_input(struct virtqueue *vq)
 	/* First we need a buffer from the Guests's virtqueue. */
 	head = wait_for_vq_desc(vq, iov, &out_num, &in_num);
 	if (out_num)
-		errx(1, "Output buffers in rng?");
+		errx(EXIT_FAILURE, "Output buffers in rng?");
 
 	/*
 	 * Just like the console write, we loop to cover the whole iovec.
@@ -1756,7 +1756,7 @@ static void rng_input(struct virtqueue *vq)
 	while (!iov_empty(iov, in_num)) {
 		len = readv(rng_info->rfd, iov, in_num);
 		if (len <= 0)
-			err(1, "Read from /dev/random gave %i", len);
+			err(EXIT_FAILURE, "Read from /dev/random gave %i", len);
 		iov_consume(iov, in_num, len);
 		totlen += len;
 	}
@@ -1803,7 +1803,7 @@ static void __attribute__((noreturn)) restart_guest(void)
 	cleanup_devices();
 
 	execv(main_args[0], main_args);
-	err(1, "Could not exec %s", main_args[0]);
+	err(EXIT_FAILURE, "Could not exec %s", main_args[0]);
 }
 
 /*L:220
@@ -1828,13 +1828,13 @@ static void __attribute__((noreturn)) run_guest(void)
 		} else if (errno == ENOENT) {
 			char reason[1024] = { 0 };
 			pread(lguest_fd, reason, sizeof(reason)-1, cpu_id);
-			errx(1, "%s", reason);
+			errx(EXIT_FAILURE, "%s", reason);
 		/* ERESTART means that we need to reboot the guest */
 		} else if (errno == ERESTART) {
 			restart_guest();
 		/* Anything else means a bug or incompatible change. */
 		} else
-			err(1, "Running guest failed");
+			err(EXIT_FAILURE, "Running guest failed");
 	}
 }
 /*L:240
@@ -1944,7 +1944,7 @@ int main(int argc, char *argv[])
 		case 'u':
 			user_details = getpwnam(optarg);
 			if (!user_details)
-				err(1, "getpwnam failed, incorrect username?");
+				err(EXIT_FAILURE, "getpwnam failed, incorrect username?");
 			break;
 		case 'c':
 			chroot_path = optarg;
@@ -2023,10 +2023,10 @@ int main(int argc, char *argv[])
 	/* If requested, chroot to a directory */
 	if (chroot_path) {
 		if (chroot(chroot_path) != 0)
-			err(1, "chroot(\"%s\") failed", chroot_path);
+			err(EXIT_FAILURE, "chroot(\"%s\") failed", chroot_path);
 
 		if (chdir("/") != 0)
-			err(1, "chdir(\"/\") failed");
+			err(EXIT_FAILURE, "chdir(\"/\") failed");
 
 		verbose("chroot done\n");
 	}
@@ -2040,13 +2040,13 @@ int main(int argc, char *argv[])
 		g = user_details->pw_gid;
 
 		if (initgroups(user_details->pw_name, g) != 0)
-			err(1, "initgroups failed");
+			err(EXIT_FAILURE, "initgroups failed");
 
 		if (setresgid(g, g, g) != 0)
-			err(1, "setresgid failed");
+			err(EXIT_FAILURE, "setresgid failed");
 
 		if (setresuid(u, u, u) != 0)
-			err(1, "setresuid failed");
+			err(EXIT_FAILURE, "setresuid failed");
 
 		verbose("Dropping privileges completed\n");
 	}
-- 
1.7.4.1






More information about the Lguest mailing list