3

I am trying to start / stop CAN boards or to update their baudrate using SocketCAN from userspace. My tests are performed on PeakSystem and IXXAT USB-to-CAN V1/V2 boards.

My first attempt was to use visudo and to enable NOPASSWD to "ip link set ...", and then to call "sudo ip link set ..." in my C++ code. Complete visudo line is:

%sudo  ALL=(ALL:ALL) NOPASSWD: /bin/ip link set can[0123456789]* type can bitrate [0123456789]*, /bin/ip link set can[0123456789]* up, /bin/ip link set can[0123456789]* down

Then, I tried with Linux capabilities by adding capabilities to /bin/ip. That allows me to call "ip link set ..." from my C++ code which was even better. Add capabilities command:

sudo setcap cap_net_raw,cap_net_admin+ep /bin/ip

But then I discovered libsocketcan which is a far better approach than calling command lines from C++. However when calling "can_set_bitrate" or "can_do_start", I have an error "RTNETLINK: Operation not permitted". But things are working fine when my program is launched as root. Other functions like can_get_state are working fine in userspace (actually, they are returning : 4 -> CAN_STATE_STOPPED). I tried to adding capabilities to my program without any success ""sudo setcap cap_net_raw,cap_net_admin+ep ./myprogram".

How can I allow my program to use libsocketcan in userspace?

Thanks for your help!

Fylhan
  • 677
  • 6
  • 10
  • `sudo setcap cap_net_raw,cap_net_admin+ep /usr/lib/x86_64-linux-gnu/libsocketcan.so.2.2.1` does not help either... I really don't know how I can make it work. – Fylhan Jan 22 '16 at 08:34
  • `sudo setcap cap_net_raw,cap_net_admin+ep /bin/ip` is not working anymore, I cannot call "ip link set ..." from CLI or C++ code without root access anymore. No solution until now. Keep searching... – Fylhan Oct 15 '18 at 07:43

1 Answers1

1

sudo setcap cap_net_raw,cap_net_admin+ep your_executable is the solution. You may need to compile with -Wl,-rpath so your program finds its shared libraries (also see this answer)

Community
  • 1
  • 1
Laurenz
  • 1,810
  • 12
  • 25