[honey] Fix portability to systems without pread()
[xapian.git] / xapian-core / net / progclient.cc
blob0f804b3f683c2d3bd6cb70ab9fb8f8a16b50f74e
1 /* progclient.cc: implementation of NetClient which spawns a program.
3 * Copyright 1999,2000,2001 BrightStation PLC
4 * Copyright 2002 Ananova Ltd
5 * Copyright 2003,2004,2005,2006,2007,2010,2011,2014 Olly Betts
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20 * USA
23 #include <config.h>
25 #include "safeerrno.h"
26 #include "safefcntl.h"
28 #include "progclient.h"
29 #include <xapian/error.h>
30 #include "closefrom.h"
31 #include "debuglog.h"
33 #include <string>
34 #include <vector>
36 #include <sys/types.h>
37 #ifndef __WIN32__
38 # include "safesyssocket.h"
39 # include <sys/wait.h>
40 #else
41 # include <cstdio> // For sprintf().
42 # include <io.h>
43 #endif
45 using namespace std;
47 #ifndef __WIN32__
48 /** Split a string into a vector of strings, using a given separator
49 * character (default space)
51 static void
52 split_words(const string &text, vector<string> &words, char ws = ' ')
54 size_t i = 0;
55 if (i < text.length() && text[0] == ws) {
56 i = text.find_first_not_of(ws, i);
58 while (i < text.length()) {
59 size_t j = text.find_first_of(ws, i);
60 words.push_back(text.substr(i, j - i));
61 i = text.find_first_not_of(ws, j);
64 #endif
66 ProgClient::ProgClient(const string &progname, const string &args,
67 double timeout_, bool writable, int flags)
68 : RemoteDatabase(run_program(progname, args
69 #ifndef __WIN32__
70 , pid
71 #endif
73 timeout_, get_progcontext(progname, args), writable,
74 flags)
76 LOGCALL_CTOR(DB, "ProgClient", progname | args | timeout_ | writable | flags);
79 string
80 ProgClient::get_progcontext(const string &progname, const string &args)
82 LOGCALL_STATIC(DB, string, "ProgClient::get_progcontext", progname | args);
83 RETURN("remote:prog(" + progname + " " + args);
86 int
87 ProgClient::run_program(const string &progname, const string &args
88 #ifndef __WIN32__
89 , pid_t &pid
90 #endif
93 #if defined HAVE_SOCKETPAIR && defined HAVE_FORK
94 LOGCALL_STATIC(DB, int, "ProgClient::run_program", progname | args | Literal("[&pid]"));
95 /* socketpair() returns two sockets. We keep sv[0] and give
96 * sv[1] to the child process.
98 int sv[2];
100 if (socketpair(PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, sv) < 0) {
101 throw Xapian::NetworkError(string("socketpair failed"), get_progcontext(progname, args), errno);
104 pid = fork();
106 if (pid < 0) {
107 throw Xapian::NetworkError(string("fork failed"), get_progcontext(progname, args), errno);
110 if (pid != 0) {
111 // parent
112 // close the child's end of the socket
113 ::close(sv[1]);
114 RETURN(sv[0]);
117 /* child process:
118 * set up file descriptors and exec program
121 #if defined F_SETFD && defined FD_CLOEXEC
122 // Clear close-on-exec flag, if we set it when we called socketpair().
123 // Clearing it here means there's no window where another thread in the
124 // parent process could fork()+exec() and end up with this fd still
125 // open (assuming close-on-exec is supported).
127 // We can't use a preprocessor check on the *value* of SOCK_CLOEXEC as
128 // on Linux SOCK_CLOEXEC is an enum, with '#define SOCK_CLOEXEC
129 // SOCK_CLOEXEC' to allow '#ifdef SOCK_CLOEXEC' to work.
130 if (SOCK_CLOEXEC != 0)
131 (void)fcntl(sv[1], F_SETFD, 0);
132 #endif
134 // replace stdin and stdout with the socket
135 // FIXME: check return values from dup2.
136 if (sv[1] != 0) {
137 dup2(sv[1], 0);
139 if (sv[1] != 1) {
140 dup2(sv[1], 1);
143 // close unnecessary file descriptors
144 closefrom(2);
146 // Redirect stderr to /dev/null
147 int stderrfd = open("/dev/null", O_WRONLY);
148 if (stderrfd == -1) {
149 throw Xapian::NetworkError(string("Redirecting stderr to /dev/null failed"), get_progcontext(progname, args), errno);
151 if (stderrfd != 2) {
152 // Not sure why it wouldn't be 2, but handle the situation anyway.
153 dup2(stderrfd, 2);
154 ::close(stderrfd);
157 vector<string> argvec;
158 split_words(args, argvec);
160 // We never explicitly free this memory, but that's OK as we're about
161 // to either execvp() or _exit().
162 const char **new_argv = new const char *[argvec.size() + 2];
164 new_argv[0] = progname.c_str();
165 for (vector<string>::size_type i = 0; i < argvec.size(); ++i) {
166 new_argv[i + 1] = argvec[i].c_str();
168 new_argv[argvec.size() + 1] = 0;
169 execvp(progname.c_str(), const_cast<char *const *>(new_argv));
171 // if we get here, then execvp failed.
172 /* throwing an exception is a bad idea, since we're
173 * not the original process. */
174 _exit(-1);
175 #ifdef __xlC__
176 // Avoid "missing return statement" warning.
177 return 0;
178 #endif
179 #elif defined __WIN32__
180 LOGCALL_STATIC(DB, int, "ProgClient::run_program", progname | args);
182 static unsigned int pipecount = 0;
183 char pipename[256];
184 sprintf(pipename, "\\\\.\\pipe\\xapian-remote-%lx-%lx-%x",
185 static_cast<unsigned long>(GetCurrentProcessId()),
186 static_cast<unsigned long>(GetCurrentThreadId()), pipecount++);
187 // Create a pipe so we can read stdout from the child process.
188 HANDLE hPipe = CreateNamedPipe(pipename,
189 PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,
191 1, 4096, 4096, NMPWAIT_USE_DEFAULT_WAIT,
192 NULL);
194 if (hPipe == INVALID_HANDLE_VALUE) {
195 throw Xapian::NetworkError("CreateNamedPipe failed",
196 get_progcontext(progname, args),
197 -int(GetLastError()));
200 HANDLE hClient = CreateFile(pipename,
201 GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
202 FILE_FLAG_OVERLAPPED, NULL);
204 if (hClient == INVALID_HANDLE_VALUE) {
205 throw Xapian::NetworkError("CreateFile failed",
206 get_progcontext(progname, args),
207 -int(GetLastError()));
210 if (!ConnectNamedPipe(hPipe, NULL) && GetLastError() != ERROR_PIPE_CONNECTED) {
211 throw Xapian::NetworkError("ConnectNamedPipe failed",
212 get_progcontext(progname, args),
213 -int(GetLastError()));
216 // Set the appropriate handles to be inherited by the child process.
217 SetHandleInformation(hClient, HANDLE_FLAG_INHERIT, 1);
219 // Create the child process.
220 PROCESS_INFORMATION procinfo;
221 memset(&procinfo, 0, sizeof(PROCESS_INFORMATION));
223 STARTUPINFO startupinfo;
224 memset(&startupinfo, 0, sizeof(STARTUPINFO));
225 startupinfo.cb = sizeof(STARTUPINFO);
226 startupinfo.hStdError = hClient;
227 startupinfo.hStdOutput = hClient;
228 startupinfo.hStdInput = hClient;
229 startupinfo.dwFlags |= STARTF_USESTDHANDLES;
231 // For some reason Windows wants a modifiable copy!
232 BOOL ok;
233 char * cmdline = strdup((progname + ' ' + args).c_str());
234 ok = CreateProcess(0, cmdline, 0, 0, TRUE, 0, 0, 0, &startupinfo, &procinfo);
235 free(cmdline);
236 if (!ok) {
237 throw Xapian::NetworkError("CreateProcess failed",
238 get_progcontext(progname, args),
239 -int(GetLastError()));
242 CloseHandle(hClient);
243 CloseHandle(procinfo.hThread);
244 RETURN(_open_osfhandle(intptr_t(hPipe), O_RDWR|O_BINARY));
245 #endif
248 ProgClient::~ProgClient()
250 // Close the socket and reap the child.
251 do_close();
252 #ifndef __WIN32__
253 waitpid(pid, 0, 0);
254 #endif
257 #ifdef DISABLE_GPL_LIBXAPIAN
258 # error GPL source we cannot relicense included in libxapian
259 #endif