2014-11-22 06:21:33 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2014-11-22 06:21:33 +00:00
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2014-11-22 06:21:33 +00:00
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2014-11-22 06:21:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-11-22 13:38:58 +00:00
|
|
|
#include "osdep/subprocess.h"
|
|
|
|
|
2014-11-22 06:21:33 +00:00
|
|
|
#include "osdep/io.h"
|
2020-07-19 13:50:25 +00:00
|
|
|
#include "osdep/windows_utils.h"
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2014-11-22 06:21:33 +00:00
|
|
|
#include "common/common.h"
|
2014-11-22 13:38:58 +00:00
|
|
|
#include "stream/stream.h"
|
2014-11-22 06:21:33 +00:00
|
|
|
#include "misc/bstr.h"
|
2018-05-17 18:58:49 +00:00
|
|
|
#include "misc/thread_tools.h"
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
// Internal CRT FD flags
|
|
|
|
#define FOPEN (0x01)
|
|
|
|
#define FPIPE (0x08)
|
|
|
|
#define FDEV (0x40)
|
|
|
|
|
2014-11-22 06:21:33 +00:00
|
|
|
static void write_arg(bstr *cmdline, char *arg)
|
|
|
|
{
|
2015-03-24 04:40:01 +00:00
|
|
|
// Empty args must be represented as an empty quoted string
|
|
|
|
if (!arg[0]) {
|
|
|
|
bstr_xappend(NULL, cmdline, bstr0("\"\""));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-22 06:21:33 +00:00
|
|
|
// If the string doesn't have characters that need to be escaped, it's best
|
|
|
|
// to leave it alone for the sake of Windows programs that don't process
|
|
|
|
// quoted args correctly.
|
|
|
|
if (!strpbrk(arg, " \t\"")) {
|
|
|
|
bstr_xappend(NULL, cmdline, bstr0(arg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are characters that need to be escaped, write a quoted string
|
|
|
|
bstr_xappend(NULL, cmdline, bstr0("\""));
|
|
|
|
|
|
|
|
// Escape the argument. To match the behavior of CommandLineToArgvW,
|
|
|
|
// backslashes are only escaped if they appear before a quote or the end of
|
|
|
|
// the string.
|
|
|
|
int num_slashes = 0;
|
|
|
|
for (int pos = 0; arg[pos]; pos++) {
|
|
|
|
switch (arg[pos]) {
|
|
|
|
case '\\':
|
2015-03-24 04:40:35 +00:00
|
|
|
// Count consecutive backslashes
|
2014-11-22 06:21:33 +00:00
|
|
|
num_slashes++;
|
|
|
|
break;
|
|
|
|
case '"':
|
2015-03-24 04:40:35 +00:00
|
|
|
// Write the argument up to the point before the quote
|
2014-11-22 06:21:33 +00:00
|
|
|
bstr_xappend(NULL, cmdline, (struct bstr){arg, pos});
|
2015-03-24 04:40:35 +00:00
|
|
|
arg += pos;
|
|
|
|
pos = 0;
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2015-03-24 04:40:35 +00:00
|
|
|
// Double backslashes preceding the quote
|
2014-11-22 06:21:33 +00:00
|
|
|
for (int i = 0; i < num_slashes; i++)
|
|
|
|
bstr_xappend(NULL, cmdline, bstr0("\\"));
|
2015-03-24 04:40:35 +00:00
|
|
|
num_slashes = 0;
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2015-03-24 04:40:35 +00:00
|
|
|
// Escape the quote itself
|
2014-11-22 06:21:33 +00:00
|
|
|
bstr_xappend(NULL, cmdline, bstr0("\\"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
num_slashes = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the rest of the argument
|
|
|
|
bstr_xappend(NULL, cmdline, bstr0(arg));
|
|
|
|
|
2015-03-24 04:40:35 +00:00
|
|
|
// Double backslashes at the end of the argument
|
2014-11-22 06:21:33 +00:00
|
|
|
for (int i = 0; i < num_slashes; i++)
|
|
|
|
bstr_xappend(NULL, cmdline, bstr0("\\"));
|
|
|
|
|
|
|
|
bstr_xappend(NULL, cmdline, bstr0("\""));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert an array of arguments to a properly escaped command-line string
|
2020-07-19 13:50:25 +00:00
|
|
|
static wchar_t *write_cmdline(void *ctx, char *argv0, char **args)
|
2014-11-22 06:21:33 +00:00
|
|
|
{
|
2020-07-19 13:50:25 +00:00
|
|
|
// argv0 should always be quoted. Otherwise, arguments may be interpreted as
|
|
|
|
// part of the program name. Also, it can't contain escape sequences.
|
2014-11-22 06:21:33 +00:00
|
|
|
bstr cmdline = {0};
|
2020-07-19 13:50:25 +00:00
|
|
|
bstr_xappend_asprintf(NULL, &cmdline, "\"%s\"", argv0);
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
if (args) {
|
|
|
|
for (int i = 0; args[i]; i++) {
|
|
|
|
bstr_xappend(NULL, &cmdline, bstr0(" "));
|
|
|
|
write_arg(&cmdline, args[i]);
|
|
|
|
}
|
2014-11-22 06:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t *wcmdline = mp_from_utf8(ctx, cmdline.start);
|
|
|
|
talloc_free(cmdline.start);
|
|
|
|
return wcmdline;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delete_handle_list(void *p)
|
|
|
|
{
|
|
|
|
LPPROC_THREAD_ATTRIBUTE_LIST list = p;
|
2015-12-06 00:40:41 +00:00
|
|
|
DeleteProcThreadAttributeList(list);
|
2014-11-22 06:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a PROC_THREAD_ATTRIBUTE_LIST that specifies exactly which handles are
|
|
|
|
// inherited by the subprocess
|
|
|
|
static LPPROC_THREAD_ATTRIBUTE_LIST create_handle_list(void *ctx,
|
|
|
|
HANDLE *handles, int num)
|
|
|
|
{
|
|
|
|
// Get required attribute list size
|
|
|
|
SIZE_T size = 0;
|
2015-12-06 00:40:41 +00:00
|
|
|
if (!InitializeProcThreadAttributeList(NULL, 1, 0, &size)) {
|
2014-11-22 06:21:33 +00:00
|
|
|
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate attribute list
|
|
|
|
LPPROC_THREAD_ATTRIBUTE_LIST list = talloc_size(ctx, size);
|
2015-12-06 00:40:41 +00:00
|
|
|
if (!InitializeProcThreadAttributeList(list, 1, 0, &size))
|
2014-11-22 06:21:33 +00:00
|
|
|
goto error;
|
|
|
|
talloc_set_destructor(list, delete_handle_list);
|
|
|
|
|
2015-12-06 00:40:41 +00:00
|
|
|
if (!UpdateProcThreadAttribute(list, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
|
|
|
|
handles, num * sizeof(HANDLE), NULL, NULL))
|
2014-11-22 06:21:33 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
return list;
|
|
|
|
error:
|
|
|
|
talloc_free(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method similar to sparse_poll, skips NULL handles
|
|
|
|
static int sparse_wait(HANDLE *handles, unsigned num_handles)
|
|
|
|
{
|
|
|
|
unsigned w_num_handles = 0;
|
2020-07-19 13:50:25 +00:00
|
|
|
HANDLE w_handles[MP_SUBPROCESS_MAX_FDS + 2];
|
|
|
|
int map[MP_SUBPROCESS_MAX_FDS + 2];
|
2017-07-25 14:19:35 +00:00
|
|
|
if (num_handles > MP_ARRAY_SIZE(w_handles))
|
|
|
|
return -1;
|
2014-11-22 06:21:33 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < num_handles; i++) {
|
|
|
|
if (!handles[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
w_handles[w_num_handles] = handles[i];
|
|
|
|
map[w_num_handles] = i;
|
|
|
|
w_num_handles++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (w_num_handles == 0)
|
|
|
|
return -1;
|
|
|
|
DWORD i = WaitForMultipleObjects(w_num_handles, w_handles, FALSE, INFINITE);
|
|
|
|
i -= WAIT_OBJECT_0;
|
|
|
|
|
|
|
|
if (i >= w_num_handles)
|
|
|
|
return -1;
|
|
|
|
return map[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper for ReadFile that treats ERROR_IO_PENDING as success
|
|
|
|
static int async_read(HANDLE file, void *buf, unsigned size, OVERLAPPED* ol)
|
|
|
|
{
|
|
|
|
if (!ReadFile(file, buf, size, NULL, ol))
|
|
|
|
return (GetLastError() == ERROR_IO_PENDING) ? 0 : -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
static bool is_valid_handle(HANDLE h)
|
2015-01-01 19:04:38 +00:00
|
|
|
{
|
2020-07-19 13:50:25 +00:00
|
|
|
// _get_osfhandle can return -2 "when the file descriptor is not associated
|
|
|
|
// with a stream"
|
|
|
|
return h && h != INVALID_HANDLE_VALUE && (intptr_t)h != -2;
|
2015-01-01 19:04:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
static wchar_t *convert_environ(void *ctx, char **env)
|
|
|
|
{
|
|
|
|
// Environment size in wchar_ts, including the trailing NUL
|
|
|
|
size_t env_size = 1;
|
|
|
|
|
|
|
|
for (int i = 0; env[i]; i++) {
|
|
|
|
int count = MultiByteToWideChar(CP_UTF8, 0, env[i], -1, NULL, 0);
|
|
|
|
if (count <= 0)
|
|
|
|
abort();
|
|
|
|
env_size += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t *ret = talloc_array(ctx, wchar_t, env_size);
|
|
|
|
size_t pos = 0;
|
|
|
|
|
|
|
|
for (int i = 0; env[i]; i++) {
|
|
|
|
int count = MultiByteToWideChar(CP_UTF8, 0, env[i], -1,
|
|
|
|
ret + pos, env_size - pos);
|
|
|
|
if (count <= 0)
|
|
|
|
abort();
|
|
|
|
pos += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_subprocess2(struct mp_subprocess_opts *opts,
|
|
|
|
struct mp_subprocess_result *res)
|
2014-11-22 06:21:33 +00:00
|
|
|
{
|
|
|
|
wchar_t *tmp = talloc_new(NULL);
|
2020-07-19 13:50:25 +00:00
|
|
|
DWORD r;
|
|
|
|
|
|
|
|
HANDLE share_hndls[MP_SUBPROCESS_MAX_FDS] = {0};
|
|
|
|
int share_hndl_count = 0;
|
|
|
|
HANDLE wait_hndls[MP_SUBPROCESS_MAX_FDS + 2] = {0};
|
|
|
|
int wait_hndl_count = 0;
|
|
|
|
|
2014-11-22 06:21:33 +00:00
|
|
|
struct {
|
2020-07-19 13:50:25 +00:00
|
|
|
HANDLE handle;
|
|
|
|
bool handle_close;
|
|
|
|
char crt_flags;
|
|
|
|
|
2014-11-22 06:21:33 +00:00
|
|
|
HANDLE read;
|
2020-07-19 13:50:25 +00:00
|
|
|
OVERLAPPED read_ol;
|
|
|
|
char *read_buf;
|
|
|
|
} fd_data[MP_SUBPROCESS_MAX_FDS] = {0};
|
|
|
|
|
|
|
|
// The maximum target FD is limited because FDs have to fit in two sparse
|
|
|
|
// arrays in STARTUPINFO.lpReserved2, which has a maximum size of 65535
|
|
|
|
// bytes. The first four bytes are the handle count, followed by one byte
|
|
|
|
// per handle for flags, and an intptr_t per handle for the HANDLE itself.
|
|
|
|
static const int crt_fd_max = (65535 - sizeof(int)) / (1 + sizeof(intptr_t));
|
|
|
|
int crt_fd_count = 0;
|
2014-11-22 06:21:33 +00:00
|
|
|
|
|
|
|
// If the function exits before CreateProcess, there was an init error
|
2020-07-19 13:50:25 +00:00
|
|
|
*res = (struct mp_subprocess_result){ .error = MP_SUBPROCESS_EINIT };
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
STARTUPINFOEXW si = {
|
|
|
|
.StartupInfo = {
|
|
|
|
.cb = sizeof si,
|
|
|
|
.dwFlags = STARTF_USESTDHANDLES | STARTF_FORCEOFFFEEDBACK,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-11-18 22:31:16 +00:00
|
|
|
PROCESS_INFORMATION pi = {0};
|
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
for (int n = 0; n < opts->num_fds; n++) {
|
|
|
|
if (opts->fds[n].fd >= crt_fd_max) {
|
|
|
|
// Target FD is too big to fit in the CRT FD array
|
|
|
|
res->error = MP_SUBPROCESS_EUNSUPPORTED;
|
2014-11-22 06:21:33 +00:00
|
|
|
goto done;
|
2020-07-19 13:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts->fds[n].fd >= crt_fd_count)
|
|
|
|
crt_fd_count = opts->fds[n].fd + 1;
|
|
|
|
|
|
|
|
if (opts->fds[n].src_fd >= 0) {
|
|
|
|
HANDLE src_handle = (HANDLE)_get_osfhandle(opts->fds[n].src_fd);
|
|
|
|
|
|
|
|
// Invalid handles are just ignored. This is because sometimes the
|
|
|
|
// standard handles are invalid in Windows, like in GUI processes.
|
|
|
|
// In this case mp_subprocess2 callers should still be able to
|
|
|
|
// blindly forward the standard FDs.
|
|
|
|
if (!is_valid_handle(src_handle))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DWORD type = GetFileType(src_handle);
|
|
|
|
bool is_console_handle = false;
|
|
|
|
switch (type & 0xff) {
|
|
|
|
case FILE_TYPE_DISK:
|
|
|
|
fd_data[n].crt_flags = FOPEN;
|
|
|
|
break;
|
|
|
|
case FILE_TYPE_CHAR:
|
|
|
|
fd_data[n].crt_flags = FOPEN | FDEV;
|
|
|
|
is_console_handle = GetConsoleMode(src_handle, &(DWORD){0});
|
|
|
|
break;
|
|
|
|
case FILE_TYPE_PIPE:
|
|
|
|
fd_data[n].crt_flags = FOPEN | FPIPE;
|
|
|
|
break;
|
|
|
|
case FILE_TYPE_UNKNOWN:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_console_handle) {
|
|
|
|
// Some Windows versions have bugs when duplicating console
|
|
|
|
// handles, or when adding console handles to the CreateProcess
|
|
|
|
// handle list, so just use the handle directly for now. Console
|
|
|
|
// handles treat inheritance weirdly, so this should still work.
|
|
|
|
fd_data[n].handle = src_handle;
|
|
|
|
} else {
|
|
|
|
// Instead of making the source handle inheritable, just
|
|
|
|
// duplicate it to an inheritable handle
|
|
|
|
if (!DuplicateHandle(GetCurrentProcess(), src_handle,
|
|
|
|
GetCurrentProcess(), &fd_data[n].handle, 0,
|
|
|
|
TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
|
goto done;
|
|
|
|
fd_data[n].handle_close = true;
|
|
|
|
|
|
|
|
share_hndls[share_hndl_count++] = fd_data[n].handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (opts->fds[n].on_read && !opts->detach) {
|
|
|
|
fd_data[n].read_ol.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!fd_data[n].read_ol.hEvent)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
struct w32_create_anon_pipe_opts o = {
|
|
|
|
.server_flags = PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
|
|
|
|
.client_inheritable = true,
|
|
|
|
};
|
|
|
|
if (!mp_w32_create_anon_pipe(&fd_data[n].read, &fd_data[n].handle, &o))
|
|
|
|
goto done;
|
|
|
|
fd_data[n].handle_close = true;
|
|
|
|
|
|
|
|
wait_hndls[n] = fd_data[n].read_ol.hEvent;
|
|
|
|
wait_hndl_count++;
|
|
|
|
|
|
|
|
fd_data[n].crt_flags = FOPEN | FPIPE;
|
|
|
|
fd_data[n].read_buf = talloc_size(tmp, 4096);
|
|
|
|
|
|
|
|
share_hndls[share_hndl_count++] = fd_data[n].handle;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
DWORD access;
|
|
|
|
if (opts->fds[n].fd == 0) {
|
|
|
|
access = FILE_GENERIC_READ;
|
|
|
|
} else if (opts->fds[n].fd <= 2) {
|
|
|
|
access = FILE_GENERIC_WRITE | FILE_READ_ATTRIBUTES;
|
|
|
|
} else {
|
|
|
|
access = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECURITY_ATTRIBUTES sa = {
|
|
|
|
.nLength = sizeof sa,
|
|
|
|
.bInheritHandle = TRUE,
|
|
|
|
};
|
|
|
|
fd_data[n].crt_flags = FOPEN | FDEV;
|
|
|
|
fd_data[n].handle = CreateFileW(L"NUL", access,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
&sa, OPEN_EXISTING, 0, NULL);
|
|
|
|
fd_data[n].handle_close = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (opts->fds[n].fd) {
|
|
|
|
case 0:
|
|
|
|
si.StartupInfo.hStdInput = fd_data[n].handle;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
si.StartupInfo.hStdOutput = fd_data[n].handle;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
si.StartupInfo.hStdError = fd_data[n].handle;
|
|
|
|
break;
|
|
|
|
}
|
2014-11-22 06:21:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
// Convert the UTF-8 environment into a UTF-16 Windows environment block
|
|
|
|
wchar_t *env = NULL;
|
|
|
|
if (opts->env)
|
|
|
|
env = convert_environ(tmp, opts->env);
|
|
|
|
|
2014-11-22 06:21:33 +00:00
|
|
|
// Convert the args array to a UTF-16 Windows command-line string
|
2020-07-19 13:50:25 +00:00
|
|
|
char **args = opts->args && opts->args[0] ? &opts->args[1] : 0;
|
|
|
|
wchar_t *cmdline = write_cmdline(tmp, opts->exe, args);
|
|
|
|
|
|
|
|
// Get pointers to the arrays in lpReserved2. This is an undocumented data
|
|
|
|
// structure used by MSVCRT (and other frameworks and runtimes) to emulate
|
|
|
|
// FD inheritance. The format is unofficially documented here:
|
2024-02-08 09:51:33 +00:00
|
|
|
// <https://web.archive.org/web/20221014190010/
|
|
|
|
// https://www.catch22.net/tuts/undocumented-createprocess>
|
2020-07-19 13:50:25 +00:00
|
|
|
si.StartupInfo.cbReserved2 = sizeof(int) + crt_fd_count * (1 + sizeof(intptr_t));
|
|
|
|
si.StartupInfo.lpReserved2 = talloc_size(tmp, si.StartupInfo.cbReserved2);
|
|
|
|
char *crt_buf_flags = si.StartupInfo.lpReserved2 + sizeof(int);
|
|
|
|
char *crt_buf_hndls = crt_buf_flags + crt_fd_count;
|
|
|
|
|
|
|
|
memcpy(si.StartupInfo.lpReserved2, &crt_fd_count, sizeof(int));
|
|
|
|
|
|
|
|
// Fill the handle array with INVALID_HANDLE_VALUE, for unassigned handles
|
|
|
|
for (int n = 0; n < crt_fd_count; n++) {
|
|
|
|
HANDLE h = INVALID_HANDLE_VALUE;
|
|
|
|
memcpy(crt_buf_hndls + n * sizeof(intptr_t), &h, sizeof(intptr_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int n = 0; n < opts->num_fds; n++) {
|
|
|
|
crt_buf_flags[opts->fds[n].fd] = fd_data[n].crt_flags;
|
|
|
|
memcpy(crt_buf_hndls + opts->fds[n].fd * sizeof(intptr_t),
|
|
|
|
&fd_data[n].handle, sizeof(intptr_t));
|
|
|
|
}
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2015-12-06 00:40:41 +00:00
|
|
|
DWORD flags = CREATE_UNICODE_ENVIRONMENT | EXTENDED_STARTUPINFO_PRESENT;
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
// Specify which handles are inherited by the subprocess. If this isn't
|
|
|
|
// specified, the subprocess inherits all inheritable handles, which could
|
|
|
|
// include handles created by other threads. See:
|
2024-02-08 09:51:33 +00:00
|
|
|
// <https://web.archive.org/web/20121127222200/
|
|
|
|
// http://blogs.msdn.com/b/oldnewthing/archive/2011/12/16/10248328.aspx>
|
2020-07-19 13:50:25 +00:00
|
|
|
si.lpAttributeList = create_handle_list(tmp, share_hndls, share_hndl_count);
|
2014-11-22 06:21:33 +00:00
|
|
|
|
|
|
|
// If we have a console, the subprocess will automatically attach to it so
|
|
|
|
// it can receive Ctrl+C events. If we don't have a console, prevent the
|
|
|
|
// subprocess from creating its own console window by specifying
|
|
|
|
// CREATE_NO_WINDOW. GetConsoleCP() can be used to reliably determine if we
|
|
|
|
// have a console or not (Cygwin uses it too.)
|
|
|
|
if (!GetConsoleCP())
|
|
|
|
flags |= CREATE_NO_WINDOW;
|
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
if (!CreateProcessW(NULL, cmdline, NULL, NULL, TRUE, flags, env, NULL,
|
2014-11-22 06:21:33 +00:00
|
|
|
&si.StartupInfo, &pi))
|
|
|
|
goto done;
|
|
|
|
talloc_free(cmdline);
|
2020-07-19 13:50:25 +00:00
|
|
|
talloc_free(env);
|
|
|
|
talloc_free(si.StartupInfo.lpReserved2);
|
2014-11-22 06:21:33 +00:00
|
|
|
talloc_free(si.lpAttributeList);
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
for (int n = 0; n < opts->num_fds; n++) {
|
|
|
|
if (fd_data[n].handle_close && is_valid_handle(fd_data[n].handle))
|
|
|
|
CloseHandle(fd_data[n].handle);
|
|
|
|
fd_data[n].handle = NULL;
|
|
|
|
|
|
|
|
if (fd_data[n].read) {
|
|
|
|
// Do the first read operation on each pipe
|
|
|
|
if (async_read(fd_data[n].read, fd_data[n].read_buf, 4096,
|
|
|
|
&fd_data[n].read_ol))
|
|
|
|
{
|
|
|
|
CloseHandle(fd_data[n].read);
|
|
|
|
wait_hndls[n] = fd_data[n].read = NULL;
|
|
|
|
wait_hndl_count--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
if (opts->detach) {
|
|
|
|
res->error = MP_SUBPROCESS_OK;
|
|
|
|
goto done;
|
|
|
|
}
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
res->error = MP_SUBPROCESS_EGENERIC;
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
wait_hndls[MP_SUBPROCESS_MAX_FDS] = pi.hProcess;
|
|
|
|
wait_hndl_count++;
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
if (opts->cancel)
|
|
|
|
wait_hndls[MP_SUBPROCESS_MAX_FDS + 1] = mp_cancel_get_event(opts->cancel);
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
DWORD exit_code;
|
|
|
|
while (wait_hndl_count) {
|
|
|
|
int n = sparse_wait(wait_hndls, MP_ARRAY_SIZE(wait_hndls));
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
if (n >= 0 && n < MP_SUBPROCESS_MAX_FDS) {
|
|
|
|
// Complete the read operation on the pipe
|
|
|
|
if (!GetOverlappedResult(fd_data[n].read, &fd_data[n].read_ol, &r, TRUE)) {
|
|
|
|
CloseHandle(fd_data[n].read);
|
|
|
|
wait_hndls[n] = fd_data[n].read = NULL;
|
|
|
|
wait_hndl_count--;
|
|
|
|
} else {
|
|
|
|
opts->fds[n].on_read(opts->fds[n].on_read_ctx,
|
|
|
|
fd_data[n].read_buf, r);
|
|
|
|
|
|
|
|
// Begin the next read operation on the pipe
|
|
|
|
if (async_read(fd_data[n].read, fd_data[n].read_buf, 4096,
|
|
|
|
&fd_data[n].read_ol))
|
|
|
|
{
|
|
|
|
CloseHandle(fd_data[n].read);
|
|
|
|
wait_hndls[n] = fd_data[n].read = NULL;
|
|
|
|
wait_hndl_count--;
|
|
|
|
}
|
2014-11-22 06:21:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
} else if (n == MP_SUBPROCESS_MAX_FDS) { // pi.hProcess
|
2014-11-22 06:21:33 +00:00
|
|
|
GetExitCodeProcess(pi.hProcess, &exit_code);
|
2020-07-19 13:50:25 +00:00
|
|
|
res->exit_status = exit_code;
|
2014-11-22 06:21:33 +00:00
|
|
|
|
|
|
|
CloseHandle(pi.hProcess);
|
2020-07-19 13:50:25 +00:00
|
|
|
wait_hndls[n] = pi.hProcess = NULL;
|
|
|
|
wait_hndl_count--;
|
|
|
|
|
|
|
|
} else if (n == MP_SUBPROCESS_MAX_FDS + 1) { // opts.cancel
|
2014-11-22 06:21:33 +00:00
|
|
|
if (pi.hProcess) {
|
|
|
|
TerminateProcess(pi.hProcess, 1);
|
2020-07-19 13:50:25 +00:00
|
|
|
res->error = MP_SUBPROCESS_EKILLED_BY_US;
|
2014-11-22 06:21:33 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2020-07-19 13:50:25 +00:00
|
|
|
} else {
|
2014-11-22 06:21:33 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:50:25 +00:00
|
|
|
res->error = MP_SUBPROCESS_OK;
|
|
|
|
|
2014-11-22 06:21:33 +00:00
|
|
|
done:
|
2020-07-19 13:50:25 +00:00
|
|
|
for (int n = 0; n < opts->num_fds; n++) {
|
|
|
|
if (is_valid_handle(fd_data[n].read)) {
|
2014-11-22 06:21:33 +00:00
|
|
|
// Cancel any pending I/O (if the process was killed)
|
2020-07-19 13:50:25 +00:00
|
|
|
CancelIo(fd_data[n].read);
|
|
|
|
GetOverlappedResult(fd_data[n].read, &fd_data[n].read_ol, &r, TRUE);
|
|
|
|
CloseHandle(fd_data[n].read);
|
2014-11-22 06:21:33 +00:00
|
|
|
}
|
2020-07-19 13:50:25 +00:00
|
|
|
if (fd_data[n].handle_close && is_valid_handle(fd_data[n].handle))
|
|
|
|
CloseHandle(fd_data[n].handle);
|
|
|
|
if (fd_data[n].read_ol.hEvent)
|
|
|
|
CloseHandle(fd_data[n].read_ol.hEvent);
|
2014-11-22 06:21:33 +00:00
|
|
|
}
|
2020-07-19 13:50:25 +00:00
|
|
|
if (pi.hProcess)
|
|
|
|
CloseHandle(pi.hProcess);
|
2014-11-22 06:21:33 +00:00
|
|
|
talloc_free(tmp);
|
|
|
|
}
|