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

Prevent python scripts and cpp files to run directly, allow run only using roslaunch

asked 2017-10-25 09:59:03 -0500

ravijoshi gravatar image

I am designing a ROS (ROS Indigo on Ubuntu 14.04 LTS) package containing several python scripts and CPP files. I don't want any user to directly invoke python scripts or CPP code. For example, if any user invokes such files directly, it should exit by displaying an error message such as following:

$rosrun my_package my_script.py
[ERROR] Do not invoke this file directly. Instead, use 'roslaunch my_package my_script.launch'
$rosrun my_package my_cpp
[ERROR] Do not invoke this file directly. Instead, use 'roslaunch my_package my_cpp.launch'

Any suggestions to incorporate such behavior? Thank you very much.

edit retag flag offensive close merge delete

Comments

2

To avoid a possible xy-problem, could you explain why you'd want to do this?

gvdhoorn gravatar image gvdhoorn  ( 2017-10-25 10:14:45 -0500 )edit

@gvdhoorn: I am passing many parameters to script and cpp files from launch file. If these scipt and cpp files are executed with rosrun/directly, it terminates by thowring long long exceptions. Hence i would like to show some info to prevent it.

ravijoshi gravatar image ravijoshi  ( 2017-11-17 23:36:09 -0500 )edit

If 'parameters' are actual ROS parameters, would checking the return value of NodeHandle::hasParam(..) printing an appropriate error message and then return or exit(..)-ing not be possible?

gvdhoorn gravatar image gvdhoorn  ( 2017-11-18 01:56:24 -0500 )edit

Yeah. that's what I am doing till now. and just before exit, I put a print command in each file.

ravijoshi gravatar image ravijoshi  ( 2017-11-18 07:14:05 -0500 )edit

Then I would suggest you make the whole thing a bit more generic, and don't complain about starting things 'directly', but just error out on a missing parameter.

Launch files can be changed, there is no guarantee that all your parameters will always be there.

gvdhoorn gravatar image gvdhoorn  ( 2017-11-19 06:27:59 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-10-25 10:13:27 -0500

gvdhoorn gravatar image

I'm not sure how robust it is, but one way to approach this would be to check for certain command line arguments (CLAs) that indicate that the binary or the script has been started by roslaunch. This would be almost 'the reverse' of what is described in #q272267.

Examples of such arguments are __name and __log. Those should be present in the argv of any program started from a launch file. Users could of course add those CLAs when invoking your script or starting your binary, but abuse is probably always possible.

edit flag offensive delete link more

Comments

I'm not sure actually whether rosrun does not also set those arguments. That would be something to check.

gvdhoorn gravatar image gvdhoorn  ( 2017-10-25 10:14:15 -0500 )edit

rosrun does not set any additional command-line arguments.

ahendrix gravatar image ahendrix  ( 2017-10-25 11:25:22 -0500 )edit

While it's possible for the launch file that invokes these nodes to add arguments, parameters or environment variables, in the end the user can mimic all of these if they really want to run the node with rosrun or by directly invoking the binary.

ahendrix gravatar image ahendrix  ( 2017-10-25 11:27:17 -0500 )edit

Yes, I agree. And I added that as well at the end of my answer I believe:

Users could of course add those CLAs when invoking your script or starting your binary, but abuse is probably always possible.

gvdhoorn gravatar image gvdhoorn  ( 2017-10-25 11:44:55 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2017-10-25 09:59:03 -0500

Seen: 114 times

Last updated: Oct 25 '17