Electroneum
spawn.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #ifdef _WIN32
33 #include <boost/algorithm/string/join.hpp>
34 #include <boost/scope_exit.hpp>
35 #include <windows.h>
36 #else
37 #include <sys/wait.h>
38 #include <signal.h>
39 #endif
40 
41 #include "misc_log_ex.h"
42 #include "util.h"
43 #include "spawn.h"
44 
45 #undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
46 #define ELECTRONEUM_DEFAULT_LOG_CATEGORY "spawn"
47 
48 namespace tools
49 {
50 
51 int spawn(const char *filename, const std::vector<std::string>& args, bool wait)
52 {
53 #ifdef _WIN32
54  std::string joined = boost::algorithm::join(args, " ");
55  char *commandLine = !joined.empty() ? &joined[0] : nullptr;
56  STARTUPINFOA si = {};
57  si.cb = sizeof(si);
58  PROCESS_INFORMATION pi;
59  if (!CreateProcessA(filename, commandLine, nullptr, nullptr, false, 0, nullptr, nullptr, &si, &pi))
60  {
61  MERROR("CreateProcess failed. Error code " << GetLastError());
62  return -1;
63  }
64 
65  BOOST_SCOPE_EXIT(&pi)
66  {
67  CloseHandle(pi.hThread);
68  CloseHandle(pi.hProcess);
69  }
70  BOOST_SCOPE_EXIT_END
71 
72  if (!wait)
73  {
74  return 0;
75  }
76 
77  DWORD result = WaitForSingleObject(pi.hProcess, INFINITE);
78  if (result != WAIT_OBJECT_0)
79  {
80  MERROR("WaitForSingleObject failed. Result " << result << ", error code " << GetLastError());
81  return -1;
82  }
83 
84  DWORD exitCode;
85  if (!GetExitCodeProcess(pi.hProcess, &exitCode))
86  {
87  MERROR("GetExitCodeProcess failed. Error code " << GetLastError());
88  return -1;
89  }
90 
91  MINFO("Child exited with " << exitCode);
92  return static_cast<int>(exitCode);
93 #else
94  std::vector<char*> argv(args.size() + 1);
95  for (size_t n = 0; n < args.size(); ++n)
96  argv[n] = (char*)args[n].c_str();
97  argv[args.size()] = NULL;
98 
99  pid_t pid = fork();
100  if (pid < 0)
101  {
102  MERROR("Error forking: " << strerror(errno));
103  return -1;
104  }
105 
106  // child
107  if (pid == 0)
108  {
109  tools::closefrom(3);
110  close(0);
111  char *envp[] = {NULL};
112  execve(filename, argv.data(), envp);
113  MERROR("Failed to execve: " << strerror(errno));
114  return -1;
115  }
116 
117  // parent
118  if (pid > 0)
119  {
120  if (!wait)
121  {
122  signal(SIGCHLD, SIG_IGN);
123  return 0;
124  }
125 
126  while (1)
127  {
128  int wstatus = 0;
129  pid_t w = waitpid(pid, &wstatus, WUNTRACED | WCONTINUED);
130  if (w < 0) {
131  MERROR("Error waiting for child: " << strerror(errno));
132  return -1;
133  }
134  if (WIFEXITED(wstatus))
135  {
136  MINFO("Child exited with " << WEXITSTATUS(wstatus));
137  return WEXITSTATUS(wstatus);
138  }
139  if (WIFSIGNALED(wstatus))
140  {
141  MINFO("Child killed by " << WEXITSTATUS(wstatus));
142  return WEXITSTATUS(wstatus);
143  }
144  }
145  }
146  MERROR("Secret passage found");
147  return -1;
148 #endif
149 }
150 
151 }
#define MERROR(x)
Definition: misc_log_ex.h:73
#define MINFO(x)
Definition: misc_log_ex.h:75
::std::string string
Definition: gtest-port.h:1097
void fork(const std::string &pidfile)
Definition: posix_fork.cpp:30
Various Tools.
Definition: tools.cpp:31
int spawn(const char *filename, const std::vector< std::string > &args, bool wait)
Definition: spawn.cpp:51
void closefrom(int fd)
Definition: util.cpp:1058