├── CHANGELOG.rst ├── LICENSE ├── README.md ├── linux_isolate_process ├── __init__.py ├── __main__.py └── isolate.py ├── package.xml ├── resource └── linux_isolate_process ├── setup.cfg ├── setup.py └── test ├── test_copyright.py ├── test_flake8.py └── test_pep257.py /CHANGELOG.rst: -------------------------------------------------------------------------------- 1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2 | Changelog for package linux_isolate_process 3 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 4 | 5 | 0.0.2 (2024-01-29) 6 | ------------------ 7 | * 0.0.1 8 | * Removed entry points 9 | * Added main 10 | * Updated readme 11 | * Update README.md 12 | * Updated package.xml 13 | * Changed to a ROS package 14 | * Updated name 15 | * Updated project name 16 | * Updated toml 17 | * Update pyproject.toml 18 | * Update setup.py 19 | * Update README.md 20 | * Update README.md 21 | * Update README.md 22 | * Added toml file 23 | * Create LICENSE 24 | * Changed name of the tool to linux_isolate_process 25 | * Update README.md 26 | * Update README.md 27 | * Update README.md 28 | * Can use as a module 29 | * Check os before execution 30 | * Update isolate.py 31 | * Update README.md 32 | * Update README.md 33 | * Added the basic script 34 | * Initial commit 35 | * Contributors: Aditya Pande 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # linux_isolate_process 2 | Isolates any given process using the ``unshare()`` system call. Suited for ROS, though can work for any process. 3 | Since the system call is linux specific, this would not work on Windows. 4 | This can be really useful for running a large number of tests in parallel wihtout having them interacting with each other. 5 | 6 | This is similar to ROS domain IDs, though domain IDs are limited by the number of ports you have, and this approach is more scalable. 7 | 8 | Originally added in [drake-ros](https://github.com/RobotLocomotion/drake-ros) 9 | 10 | # Installation 11 | 12 | This is a python ROS2 package, and requires colcon to be installed. 13 | 14 | ### 1. From source 15 | ``` 16 | mkdir ~/my_ws 17 | cd ~/my_ws ; mkdir src ; cd src 18 | git clone https://github.com/adityapande-1995/linux_isolate_process 19 | cd ~/my_ws 20 | colcon build 21 | ``` 22 | 23 | # Usage 24 | 25 | ### 1. This command can now be used as a commandline tool: 26 | ``` 27 | source ~/my_ws/install/setup.bash 28 | python3 -m linux_isolate_process 29 | 30 | # For e.g., to get a bash shell that is isolated 31 | python3 -m linux_isolate_process /bin/bash 32 | ``` 33 | 34 | For example, to run an isolated talker (Note : any ros nodes inside the ``bash -c "()"`` will be able to talk to each other) : 35 | ``` 36 | python3 -m linux_isolate_process /bin/bash -c "ros2 run demo_nodes_cpp talker" 37 | ``` 38 | If you open another terminal and run a listener normally, using 39 | ``` 40 | ros2 run demo_nodes_cpp listener 41 | ``` 42 | it will not receive any of the messages published by the talker. 43 | However, if you run the talker and listener in the same process, they will be able to talk to each other. 44 | That is, this should work perfectly fine : 45 | ``` 46 | python3 -m linux_isolate_process /bin/bash -c "ros2 run demo_nodes_cpp talker & ros2 run demo_nodes_cpp listener" 47 | ``` 48 | 49 | The talker and listener in this case are "isolated together", and hence can talk to each other. 50 | 51 | ### 2. This can also be used as a module, to isolate the current process: 52 | ``` 53 | Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux 54 | Type "help", "copyright", "credits" or "license" for more information. 55 | >>> import linux_isolate_process as i 56 | >>> i.create_linux_namespaces() 57 | True 58 | >>> # Any ros2 nodes run here would be isolated 59 | ``` 60 | 61 | -------------------------------------------------------------------------------- /linux_isolate_process/__init__.py: -------------------------------------------------------------------------------- 1 | # flake8: noqa 2 | from .isolate import create_linux_namespaces 3 | -------------------------------------------------------------------------------- /linux_isolate_process/__main__.py: -------------------------------------------------------------------------------- 1 | from .isolate import main 2 | main() 3 | -------------------------------------------------------------------------------- /linux_isolate_process/isolate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | import ctypes 3 | import os 4 | import sys 5 | 6 | # Constants 7 | CLONE_NEWUSER = 0x10000000 8 | CLONE_NEWNET = 0x40000000 9 | CLONE_NEWIPC = 0x08000000 10 | IFNAMSIZ = 16 11 | SIOCGIFFLAGS = 0x8913 12 | SIOCSIFFLAGS = 0x8914 13 | IFF_LOOPBACK = 0x8 14 | IFF_MULTICAST = 0x1000 15 | IFF_UP = 0x1 16 | AF_INET = 2 17 | SOCK_DGRAM = 2 18 | 19 | 20 | # Structures 21 | class sockaddr(ctypes.Structure): 22 | pass 23 | 24 | 25 | sockaddr._fields_ = [('sa_family', ctypes.c_ushort), ('sa_data', ctypes.c_char * 14)] 26 | 27 | 28 | class ifreq(ctypes.Structure): 29 | _fields_ = [('ifr_name', ctypes.c_char * IFNAMSIZ), ('ifr_flags', ctypes.c_short)] 30 | 31 | 32 | class ifaddrs(ctypes.Structure): 33 | pass 34 | 35 | 36 | ifaddrs._fields_ = [ 37 | ('ifa_next', ctypes.POINTER(ifaddrs)), 38 | ('ifa_name', ctypes.c_char_p), 39 | ('ifa_flags', ctypes.c_uint), 40 | ('ifa_addr', ctypes.POINTER(sockaddr)), 41 | ('ifa_netmask', ctypes.POINTER(sockaddr)), 42 | ('ifa_ifu', ctypes.c_uint * 4), # Union for ifa_broadaddr and ifa_dstaddr 43 | ('ifa_data', ctypes.c_void_p), 44 | ] 45 | 46 | # Functions 47 | libc = ctypes.CDLL('libc.so.6') 48 | 49 | 50 | def error(message): 51 | print('create_linux_namespaces:', message, ':', os.strerror(ctypes.get_errno())) 52 | 53 | 54 | def create_linux_namespaces(): 55 | result = libc.unshare(CLONE_NEWUSER | CLONE_NEWNET | CLONE_NEWIPC) 56 | if result != 0: 57 | error('failed to call unshare') 58 | return False 59 | 60 | # Assert there is exactly one network interface 61 | ifaddr = ctypes.POINTER(ifaddrs)() 62 | if libc.getifaddrs(ctypes.byref(ifaddr)) == -1: 63 | error('could not get network interfaces') 64 | return False 65 | 66 | current_ifaddr = ifaddr.contents 67 | 68 | if current_ifaddr is None: 69 | error('there are no network interfaces') 70 | return False 71 | 72 | if bool(current_ifaddr.ifa_next): 73 | error('there are multiple network interfaces') 74 | return False 75 | 76 | # Need a socket to do ioctl stuff on 77 | fd = libc.socket(AF_INET, SOCK_DGRAM, 0) 78 | if fd < 0: 79 | error('could not open a socket') 80 | libc.freeifaddrs(ifaddr) 81 | return False 82 | 83 | ioctl_request = ifreq() 84 | 85 | # Check what flags are set on the interface 86 | ioctl_request.ifr_name = current_ifaddr.ifa_name 87 | err = libc.ioctl(fd, SIOCGIFFLAGS, ctypes.byref(ioctl_request)) 88 | if err != 0: 89 | libc.freeifaddrs(ifaddr) 90 | error('failed to get interface flags') 91 | return False 92 | 93 | # Expecting a loopback interface. 94 | if not (ioctl_request.ifr_flags & IFF_LOOPBACK): 95 | error('the only interface is not a loopback interface') 96 | libc.freeifaddrs(ifaddr) 97 | return False 98 | 99 | # Enable multicast 100 | ioctl_request.ifr_flags |= IFF_MULTICAST 101 | # Bring up interface 102 | ioctl_request.ifr_flags |= IFF_UP 103 | 104 | err = libc.ioctl(fd, SIOCSIFFLAGS, ctypes.byref(ioctl_request)) 105 | if err != 0: 106 | error('failed to set interface flags') 107 | libc.freeifaddrs(ifaddr) 108 | return False 109 | 110 | libc.freeifaddrs(ifaddr) 111 | return True 112 | 113 | 114 | def main(): 115 | if os.name != 'posix': 116 | error('Not suported on non posix systems') 117 | 118 | if len(sys.argv) < 2: 119 | print('shim must be given a command to execute') 120 | exit(-1) 121 | 122 | if not create_linux_namespaces(): 123 | error('Failed to fully create isolated environment') 124 | 125 | os.execv(sys.argv[1], sys.argv[1:]) 126 | -------------------------------------------------------------------------------- /package.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | linux_isolate_process 5 | 0.0.2 6 | A tool to isolate ros2 nodes 7 | adityapande 8 | Apache-2.0 9 | 10 | ament_copyright 11 | ament_flake8 12 | ament_pep257 13 | python3-pytest 14 | 15 | 16 | ament_python 17 | 18 | 19 | -------------------------------------------------------------------------------- /resource/linux_isolate_process: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adityapande-1995/linux_isolate_process/63d76fdc9a0c351fc95432489ba223cd633ed26b/resource/linux_isolate_process -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [develop] 2 | script_dir=$base/lib/linux_isolate_process 3 | [install] 4 | install_scripts=$base/lib/linux_isolate_process 5 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import find_packages, setup 2 | 3 | package_name = 'linux_isolate_process' 4 | 5 | setup( 6 | name=package_name, 7 | version='0.0.2', 8 | packages=find_packages(exclude=['test']), 9 | data_files=[ 10 | ('share/ament_index/resource_index/packages', 11 | ['resource/' + package_name]), 12 | ('share/' + package_name, ['package.xml']), 13 | ], 14 | install_requires=['setuptools'], 15 | zip_safe=True, 16 | maintainer='adityapande', 17 | maintainer_email='adityapande@intrinsic.ai', 18 | description='A package isolate ROS2 nodes', 19 | license='Apache-2.0', 20 | tests_require=['pytest'], 21 | entry_points={ 22 | 'console_scripts': [ 23 | ], 24 | }, 25 | ) 26 | -------------------------------------------------------------------------------- /test/test_copyright.py: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Open Source Robotics Foundation, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from ament_copyright.main import main 16 | import pytest 17 | 18 | 19 | # Remove the `skip` decorator once the source file(s) have a copyright header 20 | @pytest.mark.skip(reason='No copyright header has been placed in the generated source file.') 21 | @pytest.mark.copyright 22 | @pytest.mark.linter 23 | def test_copyright(): 24 | rc = main(argv=['.', 'test']) 25 | assert rc == 0, 'Found errors' 26 | -------------------------------------------------------------------------------- /test/test_flake8.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 Open Source Robotics Foundation, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from ament_flake8.main import main_with_errors 16 | import pytest 17 | 18 | 19 | @pytest.mark.flake8 20 | @pytest.mark.linter 21 | def test_flake8(): 22 | rc, errors = main_with_errors(argv=[]) 23 | assert rc == 0, \ 24 | 'Found %d code style errors / warnings:\n' % len(errors) + \ 25 | '\n'.join(errors) 26 | -------------------------------------------------------------------------------- /test/test_pep257.py: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Open Source Robotics Foundation, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from ament_pep257.main import main 16 | import pytest 17 | 18 | 19 | @pytest.mark.linter 20 | @pytest.mark.pep257 21 | def test_pep257(): 22 | rc = main(argv=['.', 'test']) 23 | assert rc == 0, 'Found code style errors / warnings' 24 | --------------------------------------------------------------------------------