How to access ethernet port within ros node without using sudo ?

asked 2021-02-12 05:34:37 -0500

Shiva_uchiha gravatar image

updated 2021-02-12 05:51:59 -0500

Hi I am currently writing a ros2 wrapper for masterboard_sdk(link text). It initialises a constructor using an ethernet port access. Thus If This sdk was used in standalone c++ code, user have to run the executable with sudo permission. However since I am using ROS2 minimal publisher class . I cant run the node in sudo as the env variable changes , I get error stating that its not able access certain libraries related to ROS. How can I solve this issue ?

Below is the error

[INFO] [launch]: All log files can be found below /root/.ros/log/2021-02-12-17-20-57-003084-NHHYDl-00392-29555 [INFO] [launch]: Default logging verbosity is set to INFO [INFO] [imu_reader-1]: process started with pid [29558] [imu_reader-1] /home/shyamsrinivasan/ros2_tut/install/IMU_DEMO/lib/IMU_DEMO/imu_reader: error while loading shared libraries: liblibstatistics_collector.so: cannot open shared object file: No such file or directory [ERROR] [imu_reader-1]: process has died [pid 29558, exit code 127, cmd 'sudo /home/shyamsrinivasan/ros2_tut/install/IMU_DEMO/lib/IMU_DEMO/imu_reader --ros-args'].

Below is my code

#include <chrono>
#include <functional>
#include <memory>
#include <string>
#include "master_board_sdk/master_board_interface.h"
#include "master_board_sdk/defines.h"
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "sensor_msgs/msg/imu.hpp"
#include <math.h>
#include <stdio.h>
#include <sys/stat.h>
#include <assert.h>
#include <unistd.h>
#define N_SLAVES_CONTROLED 6
using namespace std::chrono_literals;

#include <typeinfo>
/* This example creates a subclass of Node and uses std::bind() to register a
* member function as a callback from the timer. */



class MinimalPublisher : public rclcpp::Node
{
  public:
    MinimalPublisher()
    : Node("minimal_publisher"), count_(0)
    { //std::string interface_name = "enp3s0";
      //std::string &if_name = interface_name ;
      MasterBoardInterface robot_if("enp3s0");
      robot_if.Init();

      for (int i = 0; i < N_SLAVES_CONTROLED; i++)
        {
            robot_if.motor_drivers[i].motor1->SetCurrentReference(0);
            robot_if.motor_drivers[i].motor2->SetCurrentReference(0);
            robot_if.motor_drivers[i].motor1->Enable();
            robot_if.motor_drivers[i].motor2->Enable();
            robot_if.motor_drivers[i].EnablePositionRolloverError();
            robot_if.motor_drivers[i].SetTimeout(5);
            robot_if.motor_drivers[i].Enable();
        }
      publisher_ = this->create_publisher<sensor_msgs::msg::Imu>("/imu", 10);
      timer_ = this->create_wall_timer(
      1ms, std::bind(&MinimalPublisher::timer_callback, this));
    }

  private:
    void timer_callback()
    {
      auto message = sensor_msgs::msg::Imu();
      //message.data = "Hello, world! " + std::to_string(count_++);
      //RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
      publisher_->publish(message);
    }
    rclcpp::TimerBase::SharedPtr timer_;
    rclcpp::Publisher<sensor_msgs::msg::Imu>::SharedPtr publisher_;



    size_t count_;
  };

  int main(int argc, char * argv[])
  {
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<MinimalPublisher>());
    rclcpp::shutdown();
    return 0;
  }
edit retag flag offensive close merge delete

Comments

If this requires the capability to transmit raw frames, then you should be able to just grant CAP_NET_RAW to the executable. See capabilities(7).

If it requires more capabilities, you could grant those as well.

Or grant them to the user which runs the binary.

gvdhoorn gravatar image gvdhoorn  ( 2021-02-12 06:25:56 -0500 )edit

Hi! gvdhoorn , I tried the following command

sudo setcap cap_net_admin,cap_net_raw+ep EXECUTABLE_NAME

I get the same error. is it same ?

I will look into the documentation .

Shiva_uchiha gravatar image Shiva_uchiha  ( 2021-02-12 06:36:56 -0500 )edit
1

well it wasn't meant to be a complete solution. I merely wanted to note the fact that depending on what exactly the library/binary needs, it's not necessary to run it with sudo. Instead, grant the required capabilities.

You may want to read up a bit on how that works, and what the limitations are.

I also don't know whether the library you're trying to use only needs those capabilities, or whether there are others.

gvdhoorn gravatar image gvdhoorn  ( 2021-02-12 06:42:42 -0500 )edit

yes gvhoorn , I am looking into it now.

Shiva_uchiha gravatar image Shiva_uchiha  ( 2021-02-12 06:44:15 -0500 )edit