Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use blocking socket in rs-canopen-dump to avoid "Resource temporarily unavailable" error (or handle it) #10

Open
follower opened this issue Feb 9, 2019 · 0 comments

Comments

@follower
Copy link

follower commented Feb 9, 2019

When using rs-canopen-dump I frequently encountered these errors and the tool would exit:

read: can raw socket read: Resource temporarily unavailable

This is because a non-blocking socket is opened when using can_socket_open():

/* Create the socket */
if ((sock = can_socket_open(argv[1])) < 0)

An alternate approach is to use can_socket_open_timeout() with a timeout of 0 instead:

diff --git a/bin/rs-canopen-dump.c b/bin/rs-canopen-dump.c
index 7b68661..7439799 100644
--- a/bin/rs-canopen-dump.c
+++ b/bin/rs-canopen-dump.c
@@ -47,7 +47,7 @@ main(int argc, char **argv)
     }
 
     /* Create the socket */
-    if ((sock = can_socket_open(argv[1])) < 0)
+    if ((sock = can_socket_open_timeout(argv[1], 0)) < 0)
     {
         fprintf(stderr, "Error: Failed to create socket.\n");
         return -1;

This is consistent with how rs-canopen-monitor operates:

/* Create the socket */
if ((sock = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
{
fprintf(stderr, "Error: Failed to create socket.\n");
return -1;
}
/* Locate the interface you wish to use */
strcpy(ifr.ifr_name, argv[1]);
ioctl(sock, SIOCGIFINDEX, &ifr); /* ifr.ifr_ifindex gets filled
* with that device's index */
// XXX add check
/* Select that CAN interface, and bind the socket to it. */
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
bind(sock, (struct sockaddr*)&addr, sizeof(addr)); // XXX Add check

Thanks for your work on libcanopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant