Skip to content

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

@follower

Description

@follower

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions