Properly check whether get_io_perms() worked and propagate errors (if any).

Also, call programmer_shutdown() whenever we exit(). Actually,
programmer_shutdown() was not called in most cases at all for any of the
(external) programmers until now. You can check that by comparing
'flashrom -p dummy -V' output.

Signed-off-by: Uwe Hermann <uwe@hermann-uwe.de>

Index: flash.h
===================================================================
--- flash.h	(Revision 627)
+++ flash.h	(Arbeitskopie)
@@ -307,7 +307,7 @@
 struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device);
 struct pci_dev *pci_card_find(uint16_t vendor, uint16_t device,
 			      uint16_t card_vendor, uint16_t card_device);
-void get_io_perms(void);
+int get_io_perms(void);
 int internal_init(void);
 int internal_shutdown(void);
 void internal_chip_writeb(uint8_t val, chipaddr addr);
Index: it87spi.c
===================================================================
--- it87spi.c	(Revision 627)
+++ it87spi.c	(Arbeitskopie)
@@ -111,7 +111,9 @@
 {
 	int ret;
 
-	get_io_perms();
+	if (get_io_perms() < 0)
+		return -1;
+
 	ret = it87spi_common_init();
 	if (!ret)
 		buses_supported = CHIP_BUSTYPE_SPI;
Index: nic3com.c
===================================================================
--- nic3com.c	(Revision 627)
+++ nic3com.c	(Arbeitskopie)
@@ -59,7 +59,8 @@
 
 int nic3com_init(void)
 {
-	get_io_perms();
+	if (get_io_perms() < 0)
+		return -1;
 
 	io_base_addr = pcidev_init(PCI_VENDOR_ID_3COM, nics_3com);
 	id = pcidev_dev->device_id;
Index: satasii.c
===================================================================
--- satasii.c	(Revision 627)
+++ satasii.c	(Arbeitskopie)
@@ -49,7 +49,8 @@
 	uint32_t addr;
 	uint16_t reg_offset;
 
-	get_io_perms();
+	if (get_io_perms() < 0)
+		return -1;
 
 	pcidev_init(PCI_VENDOR_ID_SII, satas_sii);
 	id = pcidev_dev->device_id;
Index: flashrom.c
===================================================================
--- flashrom.c	(Revision 627)
+++ flashrom.c	(Arbeitskopie)
@@ -712,10 +712,11 @@
 	if (optind < argc)
 		filename = argv[optind++];
 
-	ret = programmer_init();
-
 	myusec_calibrate_delay();
 
+	if ((ret = programmer_init()) < 0)
+		exit(1);
+
 	for (i = 0; i < ARRAY_SIZE(flashes); i++) {
 		flashes[i] =
 		    probe_flash(i ? flashes[i - 1] + 1 : flashchips, 0);
@@ -729,6 +730,7 @@
 		for (i = 0; i < ARRAY_SIZE(flashes) && flashes[i]; i++)
 			printf(" %s", flashes[i]->name);
 		printf("\nPlease specify which chip to use with the -c <chipname> option.\n");
+		programmer_shutdown();
 		exit(1);
 	} else if (!flashes[0]) {
 		printf("No EEPROM/flash device found.\n");
@@ -747,9 +749,11 @@
 				printf("Run flashrom -L to view the hardware supported in this flashrom version.\n");
 				exit(1);
 			}
+
 			return read_flash(flashes[0], filename);
 		}
 		// FIXME: flash writes stay enabled!
+		programmer_shutdown();
 		exit(1);
 	}
 
@@ -795,12 +799,14 @@
 	if (!(read_it | write_it | verify_it | erase_it)) {
 		printf("No operations were specified.\n");
 		// FIXME: flash writes stay enabled!
+		programmer_shutdown();
 		exit(1);
 	}
 
 	if (!filename && !erase_it) {
 		printf("Error: No filename specified.\n");
 		// FIXME: flash writes stay enabled!
+		programmer_shutdown();
 		exit(1);
 	}
 
@@ -808,24 +814,31 @@
 	buf = (uint8_t *) calloc(size, sizeof(char));
 
 	if (erase_it) {
-		if (erase_flash(flash))
-			return 1;
+		if (erase_flash(flash)) {
+			programmer_shutdown();
+			exit(1);
+		}
 	} else if (read_it) {
-		if (read_flash(flash, filename))
-			return 1;
+		if (read_flash(flash, filename)) {
+			programmer_shutdown();
+			exit(1);
+		}
 	} else {
 		struct stat image_stat;
 
 		if ((image = fopen(filename, "r")) == NULL) {
 			perror(filename);
+			programmer_shutdown();
 			exit(1);
 		}
 		if (fstat(fileno(image), &image_stat) != 0) {
 			perror(filename);
+			programmer_shutdown();
 			exit(1);
 		}
 		if (image_stat.st_size != flash->total_size * 1024) {
 			fprintf(stderr, "Error: Image size doesn't match\n");
+			programmer_shutdown();
 			exit(1);
 		}
 
@@ -834,7 +847,8 @@
 		fclose(image);
 		if (numbytes != size) {
 			fprintf(stderr, "Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
-			return 1;
+			programmer_shutdown();
+			exit(1);
 		}
 	}
 
@@ -849,7 +863,8 @@
 		printf("Writing flash chip... ");
 		if (!flash->write) {
 			fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
-			return 1;
+			programmer_shutdown();
+			exit(1);
 		}
 		ret |= flash->write(flash, buf);
 		if (!ret) printf("COMPLETE.\n");
Index: internal.c
===================================================================
--- internal.c	(Revision 627)
+++ internal.c	(Arbeitskopie)
@@ -80,7 +80,7 @@
 	return NULL;
 }
 
-void get_io_perms(void)
+int get_io_perms(void)
 {
 #if defined (__sun) && (defined(__i386) || defined(__amd64))
 	if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
@@ -91,15 +91,18 @@
 #endif
 		fprintf(stderr, "ERROR: Could not get I/O privileges (%s).\n"
 			"You need to be root.\n", strerror(errno));
-		exit(1);
+		return -1;
 	}
+
+	return 0;
 }
 
 int internal_init(void)
 {
 	int ret = 0;
 
-	get_io_perms();
+	if (get_io_perms() < 0)
+		return -1;
 
 	/* Initialize PCI access for flash enables */
 	pacc = pci_alloc();	/* Get the pci_access structure */
