xapian-core  1.4.25
progclient.cc
Go to the documentation of this file.
1 
4 /* Copyright 1999,2000,2001 BrightStation PLC
5  * Copyright 2002 Ananova Ltd
6  * Copyright 2003,2004,2005,2006,2007,2010,2011,2014,2019 Olly Betts
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21  * USA
22  */
23 
24 #include <config.h>
25 
26 #include "safefcntl.h"
27 
28 #include "progclient.h"
29 #include <xapian/error.h>
30 #include "closefrom.h"
31 #include "debuglog.h"
32 
33 #include <cerrno>
34 #include <string>
35 #include <vector>
36 
37 #include <sys/types.h>
38 #ifndef __WIN32__
39 # include "safesyssocket.h"
40 # include <sys/wait.h>
41 #else
42 # include <cstdio> // For sprintf().
43 # include <io.h>
44 #endif
45 
46 using namespace std;
47 
48 #ifndef __WIN32__
49 
52 static void
53 split_words(const string &text, vector<string> &words, char ws = ' ')
54 {
55  size_t i = 0;
56  if (i < text.length() && text[0] == ws) {
57  i = text.find_first_not_of(ws, i);
58  }
59  while (i < text.length()) {
60  size_t j = text.find_first_of(ws, i);
61  words.push_back(text.substr(i, j - i));
62  i = text.find_first_not_of(ws, j);
63  }
64 }
65 #endif
66 
67 ProgClient::ProgClient(const string &progname, const string &args,
68  double timeout_, bool writable, int flags)
69  : RemoteDatabase(run_program(progname, args, child),
70  timeout_, get_progcontext(progname, args), writable,
71  flags)
72 {
73  LOGCALL_CTOR(DB, "ProgClient", progname | args | timeout_ | writable | flags);
74 }
75 
76 string
77 ProgClient::get_progcontext(const string &progname, const string &args)
78 {
79  LOGCALL_STATIC(DB, string, "ProgClient::get_progcontext", progname | args);
80  RETURN("remote:prog(" + progname + " " + args + ")");
81 }
82 
83 int
84 ProgClient::run_program(const string &progname, const string &args,
85 #ifndef __WIN32__
86  pid_t& child
87 #else
88  HANDLE& child
89 #endif
90  )
91 {
92  LOGCALL_STATIC(DB, int, "ProgClient::run_program", progname | args | Literal("[&child]"));
93 
94 #if defined HAVE_SOCKETPAIR && defined HAVE_FORK
95  /* socketpair() returns two sockets. We keep sv[0] and give
96  * sv[1] to the child process.
97  */
98  int sv[2];
99 
100  if (socketpair(PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, sv) < 0) {
101  throw Xapian::NetworkError(string("socketpair failed"), get_progcontext(progname, args), errno);
102  }
103 
104  // Do the steps of splitting args into an argv[] array which need to
105  // allocate memory before we call fork() since in a multi-threaded program
106  // (which we might be used in) it's only safe to call async-signal-safe
107  // functions in the child process after fork() until exec, and malloc, etc
108  // aren't async-signal-safe.
109  vector<string> argvec;
110  split_words(args, argvec);
111  const char **new_argv = new const char *[argvec.size() + 2];
112 
113  child = fork();
114 
115  if (child < 0) {
116  delete [] new_argv;
117  throw Xapian::NetworkError(string("fork failed"), get_progcontext(progname, args), errno);
118  }
119 
120  if (child != 0) {
121  // parent
122  delete [] new_argv;
123  // close the child's end of the socket
124  ::close(sv[1]);
125  RETURN(sv[0]);
126  }
127 
128  /* child process:
129  * set up file descriptors and exec program
130  */
131 
132 #if defined F_SETFD && defined FD_CLOEXEC
133  // Clear close-on-exec flag, if we set it when we called socketpair().
134  // Clearing it here means there's no window where another thread in the
135  // parent process could fork()+exec() and end up with this fd still
136  // open (assuming close-on-exec is supported).
137  //
138  // We can't use a preprocessor check on the *value* of SOCK_CLOEXEC as
139  // on Linux SOCK_CLOEXEC is an enum, with '#define SOCK_CLOEXEC
140  // SOCK_CLOEXEC' to allow '#ifdef SOCK_CLOEXEC' to work.
141  if (SOCK_CLOEXEC != 0)
142  (void)fcntl(sv[1], F_SETFD, 0);
143 #endif
144 
145  // replace stdin and stdout with the socket
146  // FIXME: check return values from dup2.
147  if (sv[1] != 0) {
148  dup2(sv[1], 0);
149  }
150  if (sv[1] != 1) {
151  dup2(sv[1], 1);
152  }
153 
154  // close unnecessary file descriptors
155  closefrom(2);
156 
157  // Redirect stderr to /dev/null
158  int stderrfd = open("/dev/null", O_WRONLY);
159  if (stderrfd == -1) {
160  _exit(-1);
161  }
162  if (stderrfd != 2) {
163  // Not sure why it wouldn't be 2, but handle the situation anyway.
164  dup2(stderrfd, 2);
165  ::close(stderrfd);
166  }
167 
168  new_argv[0] = progname.c_str();
169  for (vector<string>::size_type i = 0; i < argvec.size(); ++i) {
170  new_argv[i + 1] = argvec[i].c_str();
171  }
172  new_argv[argvec.size() + 1] = 0;
173  execvp(progname.c_str(), const_cast<char *const *>(new_argv));
174 
175  // if we get here, then execvp failed.
176  /* throwing an exception is a bad idea, since we're
177  * not the original process. */
178  _exit(-1);
179 #ifdef __xlC__
180  // Avoid "missing return statement" warning.
181  return 0;
182 #endif
183 #elif defined __WIN32__
184  static unsigned int pipecount = 0;
185  char pipename[256];
186  sprintf(pipename, "\\\\.\\pipe\\xapian-remote-%lx-%lx-%x",
187  static_cast<unsigned long>(GetCurrentProcessId()),
188  static_cast<unsigned long>(GetCurrentThreadId()), pipecount++);
189  // Create a pipe so we can read stdout from the child process.
190  HANDLE hPipe = CreateNamedPipe(pipename,
191  PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,
192  0,
193  1, 4096, 4096, NMPWAIT_USE_DEFAULT_WAIT,
194  NULL);
195 
196  if (hPipe == INVALID_HANDLE_VALUE) {
197  throw Xapian::NetworkError("CreateNamedPipe failed",
198  get_progcontext(progname, args),
199  -int(GetLastError()));
200  }
201 
202  HANDLE hClient = CreateFile(pipename,
203  GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
204  FILE_FLAG_OVERLAPPED, NULL);
205 
206  if (hClient == INVALID_HANDLE_VALUE) {
207  throw Xapian::NetworkError("CreateFile failed",
208  get_progcontext(progname, args),
209  -int(GetLastError()));
210  }
211 
212  if (!ConnectNamedPipe(hPipe, NULL) && GetLastError() != ERROR_PIPE_CONNECTED) {
213  throw Xapian::NetworkError("ConnectNamedPipe failed",
214  get_progcontext(progname, args),
215  -int(GetLastError()));
216  }
217 
218  // Set the appropriate handles to be inherited by the child process.
219  SetHandleInformation(hClient, HANDLE_FLAG_INHERIT, 1);
220 
221  // Create the child process.
222  PROCESS_INFORMATION procinfo;
223  memset(&procinfo, 0, sizeof(PROCESS_INFORMATION));
224 
225  STARTUPINFO startupinfo;
226  memset(&startupinfo, 0, sizeof(STARTUPINFO));
227  startupinfo.cb = sizeof(STARTUPINFO);
228  startupinfo.hStdError = hClient;
229  startupinfo.hStdOutput = hClient;
230  startupinfo.hStdInput = hClient;
231  startupinfo.dwFlags |= STARTF_USESTDHANDLES;
232 
233  string cmdline{progname};
234  cmdline += ' ';
235  cmdline += args;
236  // For some reason Windows wants a modifiable command line so we
237  // pass `&cmdline[0]` rather than `cmdline.c_str()`.
238  BOOL ok = CreateProcess(progname.c_str(), &cmdline[0], 0, 0, TRUE, 0, 0, 0,
239  &startupinfo, &procinfo);
240  if (!ok) {
241  throw Xapian::NetworkError("CreateProcess failed",
242  get_progcontext(progname, args),
243  -int(GetLastError()));
244  }
245 
246  CloseHandle(hClient);
247  CloseHandle(procinfo.hThread);
248  child = procinfo.hProcess;
249  RETURN(_open_osfhandle(intptr_t(hPipe), O_RDWR|O_BINARY));
250 #endif
251 }
252 
254 {
255  try {
256  // Close the socket and reap the child.
257  do_close();
258  } catch (...) {
259  }
260 #ifndef __WIN32__
261  waitpid(child, 0, 0);
262 #else
263  WaitForSingleObject(child, INFINITE);
264 #endif
265 }
void close()
Close the database.
#define LOGCALL_STATIC(CATEGORY, TYPE, FUNC, PARAMS)
Definition: debuglog.h:491
#define RETURN(A)
Definition: debuglog.h:493
void closefrom(int fd)
Definition: closefrom.cc:89
include <sys/socket.h> with portability workarounds.
#define O_BINARY
Definition: safefcntl.h:81
Implementation of closefrom() function.
WritableDatabase open()
Construct a WritableDatabase object for a new, empty InMemory database.
Definition: dbfactory.h:104
STL namespace.
#define SOCK_CLOEXEC
Definition: safesyssocket.h:83
void do_close()
Close the socket.
~ProgClient()
Destructor.
Definition: progclient.cc:253
Hierarchy of classes which Xapian can throw as exceptions.
Definition: pretty.h:45
ProgClient(const ProgClient &)
Don&#39;t allow copying.
pid_t child
Process id of the child process.
Definition: progclient.h:43
static void split_words(const string &text, vector< string > &words, char ws=' ')
Split a string into a vector of strings, using a given separator character (default space) ...
Definition: progclient.cc:53
static std::string get_progcontext(const std::string &progname, const std::string &args)
Generate context string for Xapian::Error exception objects.
Definition: progclient.cc:77
RemoteDatabase is the baseclass for remote database implementations.
Implementation of RemoteDatabase using a spawned server.
#define LOGCALL_CTOR(CATEGORY, CLASS, PARAMS)
Definition: debuglog.h:489
Indicates a problem communicating with a remote database.
Definition: error.h:803
include <fcntl.h>, but working around broken platforms.
Debug logging macros.
static int run_program(const std::string &progname, const std::string &args, pid_t &child)
Start the child process.
Definition: progclient.cc:84