scriptindex: Fix weird error cases
[xapian.git] / xapian-core / net / progclient.cc
blob62d595da0b1a98c7a6f1462fb163b8128913856c
1 /** @file
2 * @brief implementation of NetClient which spawns a program.
3 */
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
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.
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.
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
24 #include <config.h>
26 #include "safefcntl.h"
28 #include "progclient.h"
29 #include <xapian/error.h>
30 #include "closefrom.h"
31 #include "debuglog.h"
33 #include <cerrno>
34 #include <string>
35 #include <vector>
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
46 using namespace std;
48 #ifndef __WIN32__
49 /** Split a string into a vector of strings, using a given separator
50 * character (default space)
52 static void
53 split_words(const string &text, vector<string> &words, char ws = ' ')
55 size_t i = 0;
56 if (i < text.length() && text[0] == ws) {
57 i = text.find_first_not_of(ws, i);
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);
65 #endif
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)
73 LOGCALL_CTOR(DB, "ProgClient", progname | args | timeout_ | writable | flags);
76 string
77 ProgClient::get_progcontext(const string &progname, const string &args)
79 LOGCALL_STATIC(DB, string, "ProgClient::get_progcontext", progname | args);
80 RETURN("remote:prog(" + progname + " " + args + ")");
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
92 LOGCALL_STATIC(DB, int, "ProgClient::run_program", progname | args | Literal("[&child]"));
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.
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 child = fork();
106 if (child < 0) {
107 throw Xapian::NetworkError(string("fork failed"), get_progcontext(progname, args), errno);
110 if (child != 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 static unsigned int pipecount = 0;
181 char pipename[256];
182 sprintf(pipename, "\\\\.\\pipe\\xapian-remote-%lx-%lx-%x",
183 static_cast<unsigned long>(GetCurrentProcessId()),
184 static_cast<unsigned long>(GetCurrentThreadId()), pipecount++);
185 // Create a pipe so we can read stdout from the child process.
186 HANDLE hPipe = CreateNamedPipe(pipename,
187 PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,
189 1, 4096, 4096, NMPWAIT_USE_DEFAULT_WAIT,
190 NULL);
192 if (hPipe == INVALID_HANDLE_VALUE) {
193 throw Xapian::NetworkError("CreateNamedPipe failed",
194 get_progcontext(progname, args),
195 -int(GetLastError()));
198 HANDLE hClient = CreateFile(pipename,
199 GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
200 FILE_FLAG_OVERLAPPED, NULL);
202 if (hClient == INVALID_HANDLE_VALUE) {
203 throw Xapian::NetworkError("CreateFile failed",
204 get_progcontext(progname, args),
205 -int(GetLastError()));
208 if (!ConnectNamedPipe(hPipe, NULL) && GetLastError() != ERROR_PIPE_CONNECTED) {
209 throw Xapian::NetworkError("ConnectNamedPipe failed",
210 get_progcontext(progname, args),
211 -int(GetLastError()));
214 // Set the appropriate handles to be inherited by the child process.
215 SetHandleInformation(hClient, HANDLE_FLAG_INHERIT, 1);
217 // Create the child process.
218 PROCESS_INFORMATION procinfo;
219 memset(&procinfo, 0, sizeof(PROCESS_INFORMATION));
221 STARTUPINFO startupinfo;
222 memset(&startupinfo, 0, sizeof(STARTUPINFO));
223 startupinfo.cb = sizeof(STARTUPINFO);
224 startupinfo.hStdError = hClient;
225 startupinfo.hStdOutput = hClient;
226 startupinfo.hStdInput = hClient;
227 startupinfo.dwFlags |= STARTF_USESTDHANDLES;
229 // For some reason Windows wants a modifiable command line!
230 string cmdline = progname + ' ' + args;
231 BOOL ok = CreateProcess(0, &cmdline[0], 0, 0, TRUE, 0, 0, 0,
232 &startupinfo, &procinfo);
233 if (!ok) {
234 throw Xapian::NetworkError("CreateProcess failed",
235 get_progcontext(progname, args),
236 -int(GetLastError()));
239 CloseHandle(hClient);
240 CloseHandle(procinfo.hThread);
241 child = procinfo.hProcess;
242 RETURN(_open_osfhandle(intptr_t(hPipe), O_RDWR|O_BINARY));
243 #endif
246 ProgClient::~ProgClient()
248 try {
249 // Close the socket and reap the child.
250 do_close();
251 } catch (...) {
253 #ifndef __WIN32__
254 waitpid(child, 0, 0);
255 #else
256 WaitForSingleObject(child, INFINITE);
257 #endif