Skip to contentSkip to conversation

demo/alchemy/cross-link: Properly handle syscall errors

PATCH Superseded Michael Welling 2 participants 4 messages Feb 22, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 demo/alchemy/cross-link: Properly handle syscall errors

Update cross link demo to properly handle errors returned from syscalls.

---
 demo/alchemy/cobalt/cross-link.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
demo/alchemy/cobalt/cross-link.c +13 -12
index 8c0f53f..dceb928 100644
@@ -23,6 +23,7 @@
 #include <stdio.h>
 #include <signal.h>
 #include <unistd.h>
+#include <errno.h>
 #include <sys/mman.h>
 #include <alchemy/task.h>
 #include <alchemy/timer.h>
@@ -78,8 +79,8 @@ static int close_file( int fd, char *name)
 	do {
 		i++;
 		err = close(fd);
-		switch (err) {
-		case -EAGAIN:
+		switch (errno) {
+		case EAGAIN:
 			printf(MAIN_PREFIX "%s -> EAGAIN (%d times)\n",
 			       name, i);
 			rt_task_sleep(50000); /* wait 50us */
@@ -89,10 +90,10 @@ static int close_file( int fd, char *name)
 			break;
 		default:
 			printf(MAIN_PREFIX "%s -> %s\n", name,
-			       strerror(-err));
+			       strerror(errno));
 			break;
 		}
-	} while (err == -EAGAIN && i < 10);
+	} while (errno == EAGAIN && i < 10);
 
 	return err;
 }
@@ -158,7 +159,7 @@ static void write_task_proc(void *arg)
 		written = write(write_fd, &write_time, sz);
 		if (written < 0 ) {
 			printf(WTASK_PREFIX "error on write, %s\n",
-			       strerror(-err));
+			       strerror(errno));
 			break;
 		} else if (written != sz) {
 			printf(WTASK_PREFIX "only %d / %zd byte transmitted\n",
@@ -201,8 +202,8 @@ static void read_task_proc(void *arg)
 		if (err) {
 			printf(RTASK_PREFIX
 			       "error on RTSER_RTIOC_WAIT_EVENT, %s\n",
-			       strerror(-err));
-			if (err == -ETIMEDOUT)
+			       strerror(errno));
+			if (err == ETIMEDOUT)
 				continue;
 			break;
 		}
@@ -218,7 +219,7 @@ static void read_task_proc(void *arg)
 			nr++;
 		} else if (rd < 0 ) {
 			printf(RTASK_PREFIX "error on read, code %s\n",
-			       strerror(-err));
+			       strerror(errno));
 			break;
 		} else {
 			printf(RTASK_PREFIX "only %d / %zd byte received \n",
@@ -245,7 +246,7 @@ int main(int argc, char* argv[])
 	write_fd = open( WRITE_FILE, 0);
 	if (write_fd < 0) {
 		printf(MAIN_PREFIX "can't open %s (write), %s\n", WRITE_FILE,
-		       strerror(-write_fd));
+		       strerror(errno));
 		goto error;
 	}
 	write_state |= STATE_FILE_OPENED;
@@ -255,7 +256,7 @@ int main(int argc, char* argv[])
 	err = ioctl(write_fd, RTSER_RTIOC_SET_CONFIG, &write_config);
 	if (err) {
 		printf(MAIN_PREFIX "error while RTSER_RTIOC_SET_CONFIG, %s\n",
-		       strerror(-err));
+		       strerror(errno));
 		goto error;
 	}
 	printf(MAIN_PREFIX "write-config written\n");
@@ -264,7 +265,7 @@ int main(int argc, char* argv[])
 	read_fd = open( READ_FILE, 0 );
 	if (read_fd < 0) {
 		printf(MAIN_PREFIX "can't open %s (read), %s\n", READ_FILE,
-		       strerror(-read_fd));
+		       strerror(errno));
 		goto error;
 	}
 	read_state |= STATE_FILE_OPENED;
@@ -274,7 +275,7 @@ int main(int argc, char* argv[])
 	err = ioctl(read_fd, RTSER_RTIOC_SET_CONFIG, &read_config);
 	if (err) {
 		printf(MAIN_PREFIX "error while ioctl, %s\n",
-		       strerror(-err));
+		       strerror(errno));
 		goto error;
 	}
 	printf(MAIN_PREFIX "read-config written\n");
Signature · 1 line
2.1.4
#1 lore raw
Quoted 20 lines · Michael Welling

Update cross link demo to properly handle errors returned from syscalls.

---
 demo/alchemy/cobalt/cross-link.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
demo/alchemy/cobalt/cross-link.c +1 -0
index 8c0f53f..dceb928 100644
@@ -23,6 +23,7 @@
 #include <stdio.h>
 #include <signal.h>
 #include <unistd.h>
+#include <errno.h>
 #include <sys/mman.h>
 #include <alchemy/task.h>
 #include <alchemy/timer.h>

demo/alchemy/cobalt/cross-link.c static int close_file( int fd, char *name) · line 83

Quoted 7 lines
demo/alchemy/cobalt/cross-link.c +2 -2
@@ -78,8 +79,8 @@ static int close_file( int fd, char *name)
 	do {
 		i++;
 		err = close(fd);
-		switch (err) {
-		case -EAGAIN:
+		switch (errno) {
+		case EAGAIN:

This looks incorrect. errno has a meaningful value only if err is -1. You could get away with setting errno to 0 before calling close, this would probably work, but for a more complex service which calls several syscalls under the hoold, this would not even work.

So, you should probably just add:
err = err < 0 ? -errno : 0;

Signature · 2 lines
					    Gilles.
https://click-hack.org
#2 lore raw
Quoted 34 lines · Gilles Chanteperdrix
Quoted 20 lines · Michael Welling

Update cross link demo to properly handle errors returned from syscalls.

---
 demo/alchemy/cobalt/cross-link.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
demo/alchemy/cobalt/cross-link.c +1 -0
index 8c0f53f..dceb928 100644
@@ -23,6 +23,7 @@
 #include <stdio.h>
 #include <signal.h>
 #include <unistd.h>
+#include <errno.h>
 #include <sys/mman.h>
 #include <alchemy/task.h>
 #include <alchemy/timer.h>

demo/alchemy/cobalt/cross-link.c static int close_file( int fd, char *name) · line 83

Quoted 7 lines
demo/alchemy/cobalt/cross-link.c +2 -2
@@ -78,8 +79,8 @@ static int close_file( int fd, char *name)
 	do {
 		i++;
 		err = close(fd);
-		switch (err) {
-		case -EAGAIN:
+		switch (errno) {
+		case EAGAIN:

This looks incorrect. errno has a meaningful value only if err is -1. You could get away with setting errno to 0 before calling close, this would probably work, but for a more complex service which calls several syscalls under the hoold, this would not even work.

Quoted 2 lines

So, you should probably just add:
err = err < 0 ? -errno : 0;

Okay I will fix that up in v2 along with the other things that I missed.

Quoted 4 lines
Signature · 3 lines
-- 
					    Gilles.
https://click-hack.org
#3 lore raw
In reply to Michael Welling
Quoted 32 lines · Michael Welling

Update cross link demo to properly handle errors returned from syscalls.

---
 demo/alchemy/cobalt/cross-link.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
demo/alchemy/cobalt/cross-link.c +3 -2
index 8c0f53f..dceb928 100644
@@ -23,6 +23,7 @@
 #include <stdio.h>
 #include <signal.h>
 #include <unistd.h>
+#include <errno.h>
 #include <sys/mman.h>
 #include <alchemy/task.h>
 #include <alchemy/timer.h>
@@ -78,8 +79,8 @@ static int close_file( int fd, char *name)
 	do {
 		i++;
 		err = close(fd);
-		switch (err) {
-		case -EAGAIN:
+		switch (errno) {
+		case EAGAIN:
 			printf(MAIN_PREFIX "%s -> EAGAIN (%d times)\n",
 			       name, i);
 			rt_task_sleep(50000); /* wait 50us */
@@ -89,10 +90,10 @@ static int close_file( int fd, char *name)
 			break;

demo/alchemy/cobalt/cross-link.c static int close_file( int fd, char *name) · line 98

Quoted 10 lines
demo/alchemy/cobalt/cross-link.c +2 -2
 		default:
 			printf(MAIN_PREFIX "%s -> %s\n", name,
-			       strerror(-err));
+			       strerror(errno));
 			break;
 		}
-	} while (err == -EAGAIN && i < 10);
+	} while (errno == EAGAIN && i < 10);
 
 	return err;

Missed the return code here.

Quoted 11 lines
demo/alchemy/cobalt/cross-link.c +1 -1
 }
@@ -158,7 +159,7 @@ static void write_task_proc(void *arg)
 		written = write(write_fd, &write_time, sz);
 		if (written < 0 ) {
 			printf(WTASK_PREFIX "error on write, %s\n",
-			       strerror(-err));
+			       strerror(errno));
 			break;
 		} else if (written != sz) {
 			printf(WTASK_PREFIX "only %d / %zd byte transmitted\n",

demo/alchemy/cobalt/cross-link.c static void read_task_proc(void *arg) · line 206

Quoted 7 lines
demo/alchemy/cobalt/cross-link.c +2 -2
@@ -201,8 +202,8 @@ static void read_task_proc(void *arg)
 		if (err) {
 			printf(RTASK_PREFIX
 			       "error on RTSER_RTIOC_WAIT_EVENT, %s\n",
-			       strerror(-err));
-			if (err == -ETIMEDOUT)
+			       strerror(errno));
+			if (err == ETIMEDOUT)

This should be if (errno == ETIMEDOUT)

Quoted 22 lines
demo/alchemy/cobalt/cross-link.c +2 -2
 				continue;
 			break;
 		}
@@ -218,7 +219,7 @@ static void read_task_proc(void *arg)
 			nr++;
 		} else if (rd < 0 ) {
 			printf(RTASK_PREFIX "error on read, code %s\n",
-			       strerror(-err));
+			       strerror(errno));
 			break;
 		} else {
 			printf(RTASK_PREFIX "only %d / %zd byte received \n",
@@ -245,7 +246,7 @@ int main(int argc, char* argv[])
 	write_fd = open( WRITE_FILE, 0);
 	if (write_fd < 0) {
 		printf(MAIN_PREFIX "can't open %s (write), %s\n", WRITE_FILE,
-		       strerror(-write_fd));
+		       strerror(errno));
 		goto error;
 	}
 	write_state |= STATE_FILE_OPENED;

demo/alchemy/cobalt/cross-link.c int main(int argc, char* argv[]) · line 260

Quoted 6 lines
demo/alchemy/cobalt/cross-link.c +1 -1
@@ -255,7 +256,7 @@ int main(int argc, char* argv[])
 	err = ioctl(write_fd, RTSER_RTIOC_SET_CONFIG, &write_config);
 	if (err) {
 		printf(MAIN_PREFIX "error while RTSER_RTIOC_SET_CONFIG, %s\n",
-		       strerror(-err));
+		       strerror(errno));
 		goto error;

Seems I forgot to consider the return code after the jump.

Here comes v2.

Quoted 23 lines
demo/alchemy/cobalt/cross-link.c +2 -2
 	}
 	printf(MAIN_PREFIX "write-config written\n");
@@ -264,7 +265,7 @@ int main(int argc, char* argv[])
 	read_fd = open( READ_FILE, 0 );
 	if (read_fd < 0) {
 		printf(MAIN_PREFIX "can't open %s (read), %s\n", READ_FILE,
-		       strerror(-read_fd));
+		       strerror(errno));
 		goto error;
 	}
 	read_state |= STATE_FILE_OPENED;
@@ -274,7 +275,7 @@ int main(int argc, char* argv[])
 	err = ioctl(read_fd, RTSER_RTIOC_SET_CONFIG, &read_config);
 	if (err) {
 		printf(MAIN_PREFIX "error while ioctl, %s\n",
-		       strerror(-err));
+		       strerror(errno));
 		goto error;
 	}
 	printf(MAIN_PREFIX "read-config written\n");
Signature · 1 line
2.1.4