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>
39 #include <unistd.h>
40 #include <ros/ros.h>
42 
43 namespace moveit_warehouse
44 {
45 WarehouseConnector::WarehouseConnector(const std::string& dbexec) : dbexec_(dbexec), child_pid_(0)
46 {
47 }
48 
50 {
51  if (child_pid_ != 0)
52  kill(child_pid_, SIGTERM);
53 }
54 
55 bool WarehouseConnector::connectToDatabase(const std::string& dirname)
56 {
57  if (child_pid_ != 0)
58  kill(child_pid_, SIGTERM);
59 
60  child_pid_ = fork();
61  if (child_pid_ == -1)
62  {
63  ROS_ERROR("Error forking process.");
64  child_pid_ = 0;
65  return false;
66  }
67 
68  if (child_pid_ == 0)
69  {
70  std::size_t exec_file_pos = dbexec_.find_last_of("/\\");
71  if (exec_file_pos != std::string::npos)
72  {
73  char** argv = new char*[4];
74  std::size_t exec_length = 1 + dbexec_.length() - exec_file_pos;
75  argv[0] = new char[1 + exec_length];
76  snprintf(argv[0], exec_length, "%s", dbexec_.substr(exec_file_pos + 1).c_str());
77 
78  argv[1] = new char[16];
79  snprintf(argv[1], 15, "--dbpath");
80 
81  argv[2] = new char[1024];
82  snprintf(argv[2], 1023, "%s", dirname.c_str());
83 
84  argv[3] = NULL;
85 
86  int code = execv(dbexec_.c_str(), argv);
87  delete[] argv[0];
88  delete[] argv[1];
89  delete[] argv[2];
90  delete[] argv;
91  ROS_ERROR_STREAM("execv() returned " << code << ", errno=" << errno << " string errno = " << strerror(errno));
92  }
93  return false;
94  }
95  else
96  {
97  // sleep so mongod has time to come up
98  ros::WallDuration(1.0).sleep();
99  }
100  return true;
101 }
102 }
bool sleep() const
bool connectToDatabase(const std::string &db_dirname)
#define ROS_ERROR_STREAM(args)
#define ROS_ERROR(...)
WarehouseConnector(const std::string &dbexec)


warehouse
Author(s): Ioan Sucan
autogenerated on Wed Jul 10 2019 04:04:05