ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Ros::ok() return true with dead roscore in node started as root

asked 2018-07-04 16:21:13 -0500

GuillaumeB gravatar image

Hello,

I am trying to kill my node started as root. In my case, my node need to be started as root to access some hardware (gpio). So I added sudo -E as a launch-prefix to my node.

When I ctrl+c my launchfile, the roscore died but node my node (probably due to the fact that the signal SIGTERM is not forwarded to program started as root). But I was expecting ros::ok() to return false or even ros::isShuttingDown() to return true, but no.

In this case as a normal user or as root rosnode list return ERROR: Unable to communicate with master!, so the master is dead but my node doesn't know about this.

<launch>
  <node name="test_node" pkg="test_pkg" type="test_node" launch-prefix="sudo -E" />
</launch>

and a simple node to test it:

#include <ros/ros.h>

int main(int argc, char** argv) {
    ros::init(argc, argv, "test_node");
    ros::NodeHandle nh;
    ros::NodeHandle nh_p("~");
    ros::Rate rate(10);
    while (ros::ok()) {
        ros::spinOnce();
        // do stuff
        rate.sleep();
        std::cerr<<"ros is ok"<<std::endl;
        if(ros::isShuttingDown())
          std::cerr<<"shutdown is called"<<std::endl;
    }
    std::cerr<<"program exit properly"<<std::endl;
    //free acess to hardware
    return 0;

Maybe this way is not the good one to kill a node started as root, I am open to any suggestion. Thanks for your help.

edit retag flag offensive close merge delete

Comments

my suggestion would be to allow non-root users to acces the GPIOs. What platform are you using? You can do this either via udev, by adding yourself to a specific group (e.g. dialout) or by making the GPIO world-read-writeable. So I'd suggest one or two. About your problem, I cannot help there.

mgruhler gravatar image mgruhler  ( 2018-07-05 01:08:18 -0500 )edit

But I was expecting ros::ok() to return false or even ros::isShuttingDown() to return true

ros::ok() returns whether the process local ROS objects are in an OK (enough) state. Not whether the master is still up.

isShuttingDown() returns whether the local node has been requested ..

gvdhoorn gravatar image gvdhoorn  ( 2018-07-05 02:15:46 -0500 )edit

.. to stop (using rosnode kill .. fi, or by a roslaunch server instance after ctrl+c).

The problem here is that a regular user cannot ask processes from other users to terminate using signals, as it doesn't have the right permissions.

Does the node shutdown if you use rosnode kill?

gvdhoorn gravatar image gvdhoorn  ( 2018-07-05 02:18:27 -0500 )edit

@mgruhler I am using a raspberry pi 3 (Ubuntu ARM) and pigpio. And I haven't found any way to use pigpio without root.

GuillaumeB gravatar image GuillaumeB  ( 2018-07-05 03:12:12 -0500 )edit

@gvdhoorn thanks for the explaination I didn't knew about this. rosnode kill return ERROR: Unable to communicate with master! as normal user or as root. The only way I found to kill the process was to send a SIGTERM signal as root. Maybe I could check if the master is still alive.

GuillaumeB gravatar image GuillaumeB  ( 2018-07-05 03:14:46 -0500 )edit

@GuillaumeB there are many questions about GPIO and RPi on this site. Check out #q278090 and the link therein, or #q229435

mgruhler gravatar image mgruhler  ( 2018-07-05 04:08:51 -0500 )edit

rosnode kill return ERROR: Unable to communicate with master!

did you do this after you ctrl+c-ed everything already? If so, that would make sense.

gvdhoorn gravatar image gvdhoorn  ( 2018-07-05 04:09:58 -0500 )edit

Yes, after the ctrl+c. I just tried the other way around and it worked. I can use rosnode kill before the ctr+c

GuillaumeB gravatar image GuillaumeB  ( 2018-07-05 16:52:19 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-07-12 06:24:41 -0500

GuillaumeB gravatar image

updated 2018-07-20 02:31:37 -0500

As the node was started as root it was not possible for it to catch ctrl+c I could use rosnode kill to kill the node but in my case I wanted the node to die after ctrl+ced my Launchfile.

So I had two options:

  • Find a way to run Pigpio with a non-root user

  • Find a way to kill the node started as root with the ctrl+c of the launchfile

I didn't find any way to use Pigpio without root, so I went for the second option.

I created a second node in the same package that was publishing a topic (with an empty msg) and added it to the launchfile (without sudo for this one). In the node started as Root I added a subscriber for this topic. And if I am not receiving anything for 1 second I just exit the node, thus releasing the access to GPIO.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-07-04 16:21:13 -0500

Seen: 1,066 times

Last updated: Jul 20 '18