How to enable CAN bus using C++ code

HI, I am running on a Verdin dev board with a imx5m-plus module. I am using two can bus lines talking to 2 separate devices.
Everything is working, once I manually go in and turn on both CAN buses using ip link commands. However, I’ve tried using the system call to do those exact commands, and they fail. Yet I can open a shell in the container, and run the commands and they work just fine. I don’t understand the difference. Doesn’t the code run at the same level as the shell login? The system commands all return with error code 15, which I haven’t found explanation of what it means.

Any help would be appreciated.

Hi @Evets ,

Are you using TorizonCore? Which version?

From what I understood, you’re running the commands inside of a container, and they work if you manually open a shell inside of it and execute them.

What do you mean exactly by ‘system call’? You mean running the commands as soon as the container is initialized e.g. via ENTRYPOINT or CMD?

Best regards,
Lucas Akira

Torizoncore 6 latest, yes. In C or C++, you can execute system calls like you typed them from a keyboard. They fork to a command shell and run the command. So, using cantools I am doing:
int ret = system(“ip link set can0 type can bitrate 1000000”);
ret = system(“ip link set can0 up”);
Both of which fail with error code 15. If I open a shell into the container and check, both calls have failed, can0 is not set up and not “up”. But I can manually do the same commands in the shell and have it work.

I do this after some other setup code has been done, and also, the containers is running at that point as I am running the debugger in it. I can do the commands from the shell as soon as the container is showing running, as I originally did it before I ran my program, so the debugger was sitting at the first line of code. It is just when I try to run those commands from the system call shown above that it fails.


Hi @Evets ,

I created a sample container image with a simple C++ program that runs the system() function with the ip link set commands, as you described.

I was only able to successfully run the program as user root: Running the command as torizon gave me the following permission error:

RTNETLINK answers: Operation not permitted

In this situation the output of system() wasn’t 15: I was not able to reproduce this particular error number.

As described in our Torizon Best Practices article, configuring CAN inside of a container as root is an easier workaround for permission issues.

What user in the container is executing your application that has system()? Make sure that it is root.

You said both commands fail with error code 15. Did you receive anything else, like an error message?

Are you running your container with --net=host and --cap-add="NET_ADMIN", as described here:

Are you using our IDE extension 2 to develop/run the container, or are you directly using the Docker commands on the terminal?

Best regards,
Lucas Akira

Hi @lucas_a.tx ,
I can’t run the container as root, as before a certain version (don’t remember what it was), the root user had an empty pw, but now it doesn’t and I don’t find the pw anywhere on your web. I run it as torizon as the user, with a different pw. However, if I right click on the container in VSC, I can attach a shell, and run the commands (or commands in a bash shell file) and it works just fine. I’ve tried running the commands in the bash shell file, via system(“bash”; and the command succeeds but most of the time it doesn’t work, but sometimes it does, but rarely.

I am using the Apollo extension to run the container and build the project.


Hi @Evets ,

The root user in our containers doesn’t have a password by default. I’m not sure if this happened to you, but If you’re in the container as the torizon user and run su to try changing to root, it will always ask for a password, regardless if it is set or not.

If you’re using the Torizon IDE Extension 2 and you’re trying to debug your application in it, you can change the user running the application on debug with torizon_run_as in <your-project-dir>/.vscode/settings.json, as detailed here:

Try doing the above and see if you can manage to run the app as root.

Best regards,
Lucas Akira