moveit2
The MoveIt Motion Planning Framework for ROS 2.
warehouse_connector.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2012, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: E. Gil Jones */
36 
37 #include <sys/types.h>
38 #include <signal.h>
40 #include <rclcpp/logger.hpp>
41 #include <rclcpp/logging.hpp>
42 #include <rclcpp/utilities.hpp>
43 
44 static const rclcpp::Logger LOGGER = rclcpp::get_logger("moveit.ros.warehouse.warehouse_connector");
45 
46 #ifdef _WIN32
47 void kill(int, int)
48 {
49  RCLCPP_ERROR(LOGGER, "Warehouse connector not supported on Windows");
50 } // Should never be called
51 int fork()
52 {
53  RCLCPP_ERROR(LOGGER, "Warehouse connector not supported on Windows");
54  return -1;
55 }
56 #else
57 #include <unistd.h>
58 #endif
59 
60 namespace moveit_warehouse
61 {
62 WarehouseConnector::WarehouseConnector(const std::string& dbexec) : dbexec_(dbexec), child_pid_(0)
63 {
64 }
65 
67 {
68  if (child_pid_ != 0)
69  kill(child_pid_, SIGTERM);
70 }
71 
72 bool WarehouseConnector::connectToDatabase(const std::string& dirname)
73 {
74  if (child_pid_ != 0)
75  kill(child_pid_, SIGTERM);
76 
77  child_pid_ = fork();
78  if (child_pid_ == -1)
79  {
80  RCLCPP_ERROR(LOGGER, "Error forking process.");
81  child_pid_ = 0;
82  return false;
83  }
84 
85  if (child_pid_ == 0)
86  {
87  std::size_t exec_file_pos = dbexec_.find_last_of("/\\");
88  if (exec_file_pos != std::string::npos)
89  {
90  char** argv = new char*[4];
91  std::size_t exec_length = 1 + dbexec_.length() - exec_file_pos;
92  argv[0] = new char[1 + exec_length];
93  snprintf(argv[0], exec_length, "%s", dbexec_.substr(exec_file_pos + 1).c_str());
94 
95  argv[1] = new char[16];
96  snprintf(argv[1], 15, "--dbpath");
97 
98  argv[2] = new char[1024];
99  snprintf(argv[2], 1023, "%s", dirname.c_str());
100 
101  argv[3] = nullptr;
102 
103  int code = execv(dbexec_.c_str(), argv);
104  delete[] argv[0];
105  delete[] argv[1];
106  delete[] argv[2];
107  delete[] argv;
108  RCLCPP_ERROR_STREAM(LOGGER,
109  "execv() returned " << code << ", errno=" << errno << " string errno = " << strerror(errno));
110  }
111  return false;
112  }
113  else
114  {
115  // sleep so mongod has time to come up
116  using namespace std::chrono_literals;
117  rclcpp::sleep_for(1s);
118  }
119  return true;
120 }
121 } // namespace moveit_warehouse
WarehouseConnector(const std::string &dbexec)
bool connectToDatabase(const std::string &db_dirname)
const rclcpp::Logger LOGGER
Definition: async_test.h:31