[flashrom] [PATCHv2] remove exit calls from sp_openserport

Niklas Söderlund niso at kth.se
Tue May 29 23:19:39 CEST 2012


Instead:
- Print same error message as before but use msg_perr instead of perror
- Return -1 instead of calling exit(1)

All callers of sp_openserport where able to handle this change without
much trouble. There already where other error cases that where taken
care of, so adding one more will not change behavior of there callers.

Signed-off-by: Niklas Söderlund <niso at kth.se>
---
 buspirate_spi.c |    2 ++
 pony_spi.c      |    4 ++++
 serial.c        |   30 +++++++++++++++++++-----------
 serprog.c       |    4 ++++
 4 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/buspirate_spi.c b/buspirate_spi.c
index f816afd..f18538a 100644
--- a/buspirate_spi.c
+++ b/buspirate_spi.c
@@ -39,6 +39,8 @@ static int buspirate_serialport_setup(char *dev)
 {
 	/* 115200bps, 8 databits, no parity, 1 stopbit */
 	sp_fd = sp_openserport(dev, 115200);
+	if (sp_fd < 0)
+		return 1;
 	return 0;
 }
 #else
diff --git a/pony_spi.c b/pony_spi.c
index b5dfc2f..6ce467e 100644
--- a/pony_spi.c
+++ b/pony_spi.c
@@ -99,6 +99,10 @@ int pony_spi_init(void)
 
 	if (arg && strlen(arg)) {
 		sp_fd = sp_openserport( arg, 9600 );
+		if (sp_fd < 0) {
+			free(arg);
+			return 1;
+		}
 		have_device++;
 	}
 	free(arg);
diff --git a/serial.c b/serial.c
index 0a56568..474cc96 100644
--- a/serial.c
+++ b/serial.c
@@ -113,8 +113,10 @@ fdtype sp_openserport(char *dev, unsigned int baud)
 	    (tolower((unsigned char)dev[1]) == 'o') &&
 	    (tolower((unsigned char)dev[2]) == 'm')) {
 		dev2 = malloc(strlen(dev) + 5);
-		if (!dev2)
-			sp_die("Error: Out of memory");
+		if (!dev2) {
+			msg_perr("Error: Out of memory: %s\n", strerror(errno));
+			return -1;
+		}
 		strcpy(dev2, "\\\\.\\");
 		strcpy(dev2 + 4, dev);
 	}
@@ -123,41 +125,47 @@ fdtype sp_openserport(char *dev, unsigned int baud)
 	if (dev2 != dev)
 		free(dev2);
 	if (fd == INVALID_HANDLE_VALUE) {
-		sp_die("Error: cannot open serial port");
+		msg_perr("Error: cannot open serial port: %s\n", strerror(errno));
+		return -1;
 	}
 	DCB dcb;
 	if (!GetCommState(fd, &dcb)) {
-		sp_die("Error: Could not fetch serial port configuration");
+		msg_perr("Error: Could not fetch serial port configuration: %s\n", strerror(errno));
+		return -1;
 	}
+
 	switch (baud) {
 		case 9600: dcb.BaudRate = CBR_9600; break;
 		case 19200: dcb.BaudRate = CBR_19200; break;
 		case 38400: dcb.BaudRate = CBR_38400; break;
 		case 57600: dcb.BaudRate = CBR_57600; break;
 		case 115200: dcb.BaudRate = CBR_115200; break;
-		default: sp_die("Error: Could not set baud rate");
+		default: msg_perr("Error: Could not set baud rate: %s\n", strerror(errno));
+			 return -1;
 	}
 	dcb.ByteSize = 8;
 	dcb.Parity = NOPARITY;
 	dcb.StopBits = ONESTOPBIT;
 	if (!SetCommState(fd, &dcb)) {
-		sp_die("Error: Could not change serial port configuration");
+		msg_perr("Error: Could not change serial port configuration: %s\n", strerror(errno));
+		return -1;
 	}
 	return fd;
 #else
 	struct termios options;
 	int fd, i;
 	fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
-	if (fd < 0)
-		sp_die("Error: cannot open serial port");
+	if (fd < 0) {
+		msg_perr("Error: cannot open serial port: %s\n", strerror(errno));
+		return -1;
+	}
 	fcntl(fd, F_SETFL, 0);
 	tcgetattr(fd, &options);
 	for (i = 0;; i++) {
 		if (sp_baudtable[i].baud == 0) {
 			close(fd);
-			msg_perr("Error: cannot configure for baudrate %d\n",
-				 baud);
-			exit(1);
+			msg_perr("Error: cannot configure for baudrate %d\n", baud);
+			return -1;
 		}
 		if (sp_baudtable[i].baud == baud) {
 			cfsetispeed(&options, sp_baudtable[i].flag);
diff --git a/serprog.c b/serprog.c
index 65539a1..d06fb0b 100644
--- a/serprog.c
+++ b/serprog.c
@@ -361,6 +361,10 @@ int serprog_init(void)
 		}
 		if (strlen(device)) {
 			sp_fd = sp_openserport(device, atoi(baudport));
+			if (sp_fd < 0) {
+				free(device);
+				return 1;
+			}
 			have_device++;
 		}
 	}
-- 
1.7.10.2





More information about the flashrom mailing list