2000-02-17 20:38:33 +01:00
|
|
|
/* spawn.cc
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-08-02 18:28:18 +02:00
|
|
|
#include "winsup.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2012-02-07 18:15:07 +01:00
|
|
|
#include <process.h>
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <sys/wait.h>
|
2008-03-07 12:24:51 +01:00
|
|
|
#include <wchar.h>
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <ctype.h>
|
2000-09-08 04:56:55 +02:00
|
|
|
#include <sys/cygwin.h>
|
2012-01-11 20:07:11 +01:00
|
|
|
#include "cygerrno.h"
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "security.h"
|
2012-01-11 20:07:11 +01:00
|
|
|
#include "sigproc.h"
|
|
|
|
#include "pinfo.h"
|
2003-09-20 02:43:33 +02:00
|
|
|
#include "path.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
#include "fhandler.h"
|
2000-08-12 07:35:42 +02:00
|
|
|
#include "dtable.h"
|
2000-09-06 23:03:10 +02:00
|
|
|
#include "cygheap.h"
|
2000-09-01 22:54:22 +02:00
|
|
|
#include "child_info.h"
|
2000-09-08 04:56:55 +02:00
|
|
|
#include "environ.h"
|
2005-07-17 02:51:03 +02:00
|
|
|
#include "cygtls.h"
|
2008-03-07 12:24:51 +01:00
|
|
|
#include "tls_pbuf.h"
|
2006-04-12 17:53:22 +02:00
|
|
|
#include "winf.h"
|
2007-07-19 10:36:32 +02:00
|
|
|
#include "ntdll.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
static const suffix_info exe_suffixes[] =
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2006-02-03 22:33:09 +01:00
|
|
|
suffix_info ("", 1),
|
|
|
|
suffix_info (".exe", 1),
|
2006-01-31 22:49:39 +01:00
|
|
|
suffix_info (".com"),
|
|
|
|
suffix_info (NULL)
|
|
|
|
};
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Add .exe to PROG if not already present and see if that exists.
|
|
|
|
If not, return PROG (converted from posix to win32 rules if necessary).
|
|
|
|
The result is always BUF.
|
|
|
|
|
|
|
|
Returns (possibly NULL) suffix */
|
|
|
|
|
|
|
|
static const char *
|
2006-01-31 22:49:39 +01:00
|
|
|
perhaps_suffix (const char *prog, path_conv& buf, int& err, unsigned opt)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2009-08-01 21:52:46 +02:00
|
|
|
const char *ext;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-08-24 20:26:14 +02:00
|
|
|
err = 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
debug_printf ("prog '%s'", prog);
|
2015-02-11 14:15:59 +01:00
|
|
|
buf.check (prog, PC_SYM_FOLLOW | PC_NULLEMPTY | PC_POSIX,
|
2009-11-05 10:59:18 +01:00
|
|
|
(opt & FE_DLL) ? stat_suffixes : exe_suffixes);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-08-24 20:26:14 +02:00
|
|
|
if (buf.isdir ())
|
|
|
|
{
|
|
|
|
err = EACCES;
|
|
|
|
ext = NULL;
|
|
|
|
}
|
|
|
|
else if (!buf.exists ())
|
|
|
|
{
|
|
|
|
err = ENOENT;
|
|
|
|
ext = NULL;
|
|
|
|
}
|
2015-02-15 09:59:55 +01:00
|
|
|
else if (buf.known_suffix ())
|
|
|
|
ext = buf.get_win32 () + (buf.known_suffix () - buf.get_win32 ());
|
2000-02-17 20:38:33 +01:00
|
|
|
else
|
2007-08-16 17:07:42 +02:00
|
|
|
ext = strchr (buf.get_win32 (), '\0');
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2007-08-16 17:07:42 +02:00
|
|
|
debug_printf ("buf %s, suffix found '%s'", (char *) buf.get_win32 (), ext);
|
2000-02-17 20:38:33 +01:00
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
2015-02-11 14:15:59 +01:00
|
|
|
/* Find an executable name, possibly by appending known executable suffixes
|
|
|
|
to it. The path_conv struct 'buf' is filled and contains both, win32 and
|
2015-02-16 12:49:33 +01:00
|
|
|
posix path of the target file. Any found suffix is returned in known_suffix.
|
|
|
|
Eventually the posix path in buf is overwritten with the exact path as it
|
|
|
|
gets constructed for the path search. The reason is that the path is used
|
|
|
|
to create argv[0] in av::setup, and this requires that the filename stays
|
|
|
|
intact, instead of being resolved if the file is a symlink.
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2015-02-15 09:59:55 +01:00
|
|
|
If the file is not found and !FE_NNF then the POSIX version of name is
|
2015-02-11 14:15:59 +01:00
|
|
|
placed in buf and returned. Otherwise the contents of buf is undefined
|
|
|
|
and NULL is returned. */
|
2013-05-01 03:20:37 +02:00
|
|
|
const char * __reg3
|
2015-02-11 14:15:59 +01:00
|
|
|
find_exec (const char *name, path_conv& buf, const char *search,
|
2001-10-31 01:55:32 +01:00
|
|
|
unsigned opt, const char **known_suffix)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
const char *suffix = "";
|
2015-02-11 14:15:59 +01:00
|
|
|
const char *retval = NULL;
|
2008-03-11 14:26:40 +01:00
|
|
|
tmp_pathbuf tp;
|
2015-02-11 14:15:59 +01:00
|
|
|
char *tmp_path;
|
2008-03-11 14:26:40 +01:00
|
|
|
char *tmp = tp.c_get ();
|
2010-03-29 19:15:51 +02:00
|
|
|
bool has_slash = !!strpbrk (name, "/\\");
|
2014-06-23 21:05:15 +02:00
|
|
|
int err = 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2015-02-11 14:15:59 +01:00
|
|
|
debug_printf ("find_exec (%s)", name);
|
|
|
|
|
|
|
|
/* Check to see if file can be opened as is first. */
|
2001-10-31 01:55:32 +01:00
|
|
|
if ((has_slash || opt & FE_CWD)
|
2006-01-31 22:49:39 +01:00
|
|
|
&& (suffix = perhaps_suffix (name, buf, err, opt)) != NULL)
|
2001-10-31 01:55:32 +01:00
|
|
|
{
|
2015-03-17 11:40:12 +01:00
|
|
|
/* Overwrite potential symlink target with original path.
|
|
|
|
See comment preceeding this method. */
|
|
|
|
tmp_path = tmp;
|
2015-02-16 12:49:33 +01:00
|
|
|
if (!has_slash)
|
2015-03-17 11:40:12 +01:00
|
|
|
tmp_path = stpcpy (tmp, "./");
|
|
|
|
stpcpy (tmp_path, name);
|
|
|
|
buf.set_posix (tmp);
|
2015-02-15 09:59:55 +01:00
|
|
|
retval = buf.get_posix ();
|
2001-10-31 01:55:32 +01:00
|
|
|
goto out;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
const char *path;
|
2015-02-11 14:15:59 +01:00
|
|
|
/* If it starts with a slash, it's a PATH-like pathlist. Otherwise it's
|
|
|
|
the name of an environment variable. */
|
|
|
|
if (strchr (search, '/'))
|
|
|
|
*stpncpy (tmp, search, NT_MAX_PATH - 1) = '\0';
|
|
|
|
else if (has_slash || isdrive (name) || !(path = getenv (search)) || !*path)
|
2000-02-17 20:38:33 +01:00
|
|
|
goto errout;
|
2005-05-22 05:54:29 +02:00
|
|
|
else
|
2015-02-11 14:15:59 +01:00
|
|
|
*stpncpy (tmp, path, NT_MAX_PATH - 1) = '\0';
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2015-02-11 14:15:59 +01:00
|
|
|
path = tmp;
|
|
|
|
debug_printf ("searchpath %s", path);
|
|
|
|
|
|
|
|
tmp_path = tp.c_get ();
|
2000-02-17 20:38:33 +01:00
|
|
|
do
|
|
|
|
{
|
2015-02-11 14:15:59 +01:00
|
|
|
char *eotmp = strccpy (tmp_path, &path, ':');
|
2000-02-17 20:38:33 +01:00
|
|
|
/* An empty path or '.' means the current directory, but we've
|
|
|
|
already tried that. */
|
2015-02-11 14:15:59 +01:00
|
|
|
if ((opt & FE_CWD) && (tmp_path[0] == '\0'
|
|
|
|
|| (tmp_path[0] == '.' && tmp_path[1] == '\0')))
|
2000-02-17 20:38:33 +01:00
|
|
|
continue;
|
|
|
|
|
2015-02-11 14:15:59 +01:00
|
|
|
*eotmp++ = '/';
|
2015-02-16 12:49:33 +01:00
|
|
|
stpcpy (eotmp, name);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2015-02-11 14:15:59 +01:00
|
|
|
debug_printf ("trying %s", tmp_path);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2010-04-28 01:06:48 +02:00
|
|
|
int err1;
|
|
|
|
|
2015-02-11 14:15:59 +01:00
|
|
|
if ((suffix = perhaps_suffix (tmp_path, buf, err1, opt)) != NULL)
|
2001-10-31 01:55:32 +01:00
|
|
|
{
|
2009-09-25 15:44:45 +02:00
|
|
|
if (buf.has_acls () && check_file_access (buf, X_OK, true))
|
2006-01-31 22:09:43 +01:00
|
|
|
continue;
|
2015-02-16 12:49:33 +01:00
|
|
|
/* Overwrite potential symlink target with original path.
|
|
|
|
See comment preceeding this method. */
|
|
|
|
buf.set_posix (tmp_path);
|
2015-02-15 09:59:55 +01:00
|
|
|
retval = buf.get_posix ();
|
2001-10-31 01:55:32 +01:00
|
|
|
goto out;
|
|
|
|
}
|
2015-02-11 14:15:59 +01:00
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2015-02-11 14:15:59 +01:00
|
|
|
while (*path && *++path);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-08-22 19:50:22 +02:00
|
|
|
errout:
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Couldn't find anything in the given path.
|
2015-02-11 14:15:59 +01:00
|
|
|
Take the appropriate action based on FE_NNF. */
|
|
|
|
if (!(opt & FE_NNF))
|
2009-08-01 05:27:51 +02:00
|
|
|
{
|
2015-02-11 14:15:59 +01:00
|
|
|
buf.check (name, PC_SYM_FOLLOW | PC_POSIX);
|
2015-02-15 09:59:55 +01:00
|
|
|
retval = buf.get_posix ();
|
2009-08-01 05:27:51 +02:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-08-22 19:50:22 +02:00
|
|
|
out:
|
2015-02-15 09:59:55 +01:00
|
|
|
debug_printf ("%s = find_exec (%s)", (char *) buf.get_posix (), name);
|
2000-02-17 20:38:33 +01:00
|
|
|
if (known_suffix)
|
2007-08-16 17:07:42 +02:00
|
|
|
*known_suffix = suffix ?: strchr (buf.get_win32 (), '\0');
|
2005-08-24 20:26:14 +02:00
|
|
|
if (!retval && err)
|
|
|
|
set_errno (err);
|
2000-02-17 20:38:33 +01:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2011-11-14 02:29:49 +01:00
|
|
|
/* Utility for child_info_spawn::worker. */
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
static HANDLE
|
2006-12-11 19:55:29 +01:00
|
|
|
handle (int fd, bool writing)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2005-12-19 05:34:13 +01:00
|
|
|
HANDLE h;
|
|
|
|
cygheap_fdget cfd (fd);
|
|
|
|
|
|
|
|
if (cfd < 0)
|
|
|
|
h = INVALID_HANDLE_VALUE;
|
|
|
|
else if (cfd->close_on_exec ())
|
|
|
|
h = INVALID_HANDLE_VALUE;
|
2006-12-11 19:55:29 +01:00
|
|
|
else if (!writing)
|
2005-12-19 05:34:13 +01:00
|
|
|
h = cfd->get_handle ();
|
|
|
|
else
|
|
|
|
h = cfd->get_output_handle ();
|
2006-12-11 19:55:29 +01:00
|
|
|
|
2005-12-19 05:34:13 +01:00
|
|
|
return h;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
iscmd (const char *argv0, const char *what)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
n = strlen (argv0) - strlen (what);
|
|
|
|
if (n >= 2 && argv0[1] != ':')
|
|
|
|
return 0;
|
2000-07-01 19:30:35 +02:00
|
|
|
return n >= 0 && strcasematch (argv0 + n, what) &&
|
2000-02-17 20:38:33 +01:00
|
|
|
(n == 0 || isdirsep (argv0[n - 1]));
|
|
|
|
}
|
|
|
|
|
2013-05-03 21:39:01 +02:00
|
|
|
#define ILLEGAL_SIG_FUNC_PTR ((_sig_func_ptr) (-2))
|
|
|
|
struct system_call_handle
|
2003-09-20 21:51:48 +02:00
|
|
|
{
|
|
|
|
_sig_func_ptr oldint;
|
|
|
|
_sig_func_ptr oldquit;
|
|
|
|
sigset_t oldmask;
|
2013-05-03 21:39:01 +02:00
|
|
|
bool is_system_call ()
|
|
|
|
{
|
|
|
|
return oldint != ILLEGAL_SIG_FUNC_PTR;
|
|
|
|
}
|
|
|
|
system_call_handle (bool issystem)
|
2013-05-01 01:51:08 +02:00
|
|
|
{
|
|
|
|
if (!issystem)
|
2013-05-03 21:39:01 +02:00
|
|
|
oldint = ILLEGAL_SIG_FUNC_PTR;
|
2013-05-01 01:51:08 +02:00
|
|
|
else
|
2013-05-03 21:39:01 +02:00
|
|
|
{
|
|
|
|
sig_send (NULL, __SIGHOLD);
|
|
|
|
oldint = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void arm()
|
|
|
|
{
|
|
|
|
if (is_system_call ())
|
2013-05-01 01:51:08 +02:00
|
|
|
{
|
|
|
|
sigset_t child_block;
|
|
|
|
oldint = signal (SIGINT, SIG_IGN);
|
|
|
|
oldquit = signal (SIGQUIT, SIG_IGN);
|
|
|
|
sigemptyset (&child_block);
|
|
|
|
sigaddset (&child_block, SIGCHLD);
|
|
|
|
sigprocmask (SIG_BLOCK, &child_block, &oldmask);
|
|
|
|
sig_send (NULL, __SIGNOHOLD);
|
|
|
|
}
|
|
|
|
}
|
2013-05-03 21:39:01 +02:00
|
|
|
~system_call_handle ()
|
2013-05-01 01:51:08 +02:00
|
|
|
{
|
2013-05-03 21:39:01 +02:00
|
|
|
if (is_system_call ())
|
2013-05-01 01:51:08 +02:00
|
|
|
{
|
|
|
|
signal (SIGINT, oldint);
|
|
|
|
signal (SIGQUIT, oldquit);
|
|
|
|
sigprocmask (SIG_SETMASK, &oldmask, NULL);
|
|
|
|
}
|
|
|
|
}
|
2003-09-20 21:51:48 +02:00
|
|
|
# undef cleanup
|
2013-05-01 01:51:08 +02:00
|
|
|
};
|
2003-09-20 21:51:48 +02:00
|
|
|
|
2012-01-08 07:24:17 +01:00
|
|
|
child_info_spawn NO_COPY ch_spawn;
|
2003-09-20 21:51:48 +02:00
|
|
|
|
2020-08-01 21:35:58 +02:00
|
|
|
extern "C" void __posix_spawn_sem_release (void *sem, int error);
|
|
|
|
|
2009-01-03 06:12:22 +01:00
|
|
|
int
|
2011-11-14 02:29:49 +01:00
|
|
|
child_info_spawn::worker (const char *prog_arg, const char *const *argv,
|
|
|
|
const char *const envp[], int mode,
|
|
|
|
int in__stdin, int in__stdout)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2003-12-07 23:37:12 +01:00
|
|
|
bool rc;
|
2005-08-11 18:13:30 +02:00
|
|
|
int res = -1;
|
2019-11-16 00:27:24 +01:00
|
|
|
DWORD pid_restore = 0;
|
2019-09-13 23:48:14 +02:00
|
|
|
bool attach_to_console = false;
|
2019-09-08 14:58:35 +02:00
|
|
|
pid_t ctty_pgid = 0;
|
|
|
|
|
|
|
|
/* Search for CTTY and retrieve its PGID */
|
|
|
|
cygheap_fdenum cfd (false);
|
|
|
|
while (cfd.next () >= 0)
|
|
|
|
if (cfd->get_major () == DEV_PTYS_MAJOR ||
|
|
|
|
cfd->get_major () == DEV_CONS_MAJOR)
|
|
|
|
{
|
|
|
|
fhandler_termios *fh = (fhandler_termios *) (fhandler_base *) cfd;
|
|
|
|
if (fh->tc ()->ntty == myself->ctty)
|
|
|
|
{
|
|
|
|
ctty_pgid = fh->tc ()->getpgid ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2011-01-19 10:15:17 +01:00
|
|
|
/* Check if we have been called from exec{lv}p or spawn{lv}p and mask
|
|
|
|
mode to keep only the spawn mode. */
|
|
|
|
bool p_type_exec = !!(mode & _P_PATH_TYPE_EXEC);
|
|
|
|
mode = _P_MODE (mode);
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
if (prog_arg == NULL)
|
|
|
|
{
|
|
|
|
syscall_printf ("prog_arg is NULL");
|
2009-12-21 16:16:28 +01:00
|
|
|
set_errno (EFAULT); /* As on Linux. */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!prog_arg[0])
|
|
|
|
{
|
|
|
|
syscall_printf ("prog_arg is empty");
|
|
|
|
set_errno (ENOENT); /* Per POSIX */
|
2000-02-17 20:38:33 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-11-14 02:29:49 +01:00
|
|
|
syscall_printf ("mode = %d, prog_arg = %.9500s", mode, prog_arg);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2009-12-21 16:16:28 +01:00
|
|
|
/* FIXME: This is no error condition on Linux. */
|
2000-02-17 20:38:33 +01:00
|
|
|
if (argv == NULL)
|
|
|
|
{
|
|
|
|
syscall_printf ("argv is NULL");
|
2001-03-18 21:58:18 +01:00
|
|
|
set_errno (EINVAL);
|
2002-10-09 06:08:05 +02:00
|
|
|
return -1;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2005-08-11 18:13:30 +02:00
|
|
|
av newargv;
|
2013-07-19 19:28:34 +02:00
|
|
|
linebuf cmd;
|
2008-03-07 12:24:51 +01:00
|
|
|
PWCHAR envblock = NULL;
|
2005-08-11 18:13:30 +02:00
|
|
|
path_conv real_path;
|
|
|
|
bool reset_sendsig = false;
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2008-03-07 12:24:51 +01:00
|
|
|
tmp_pathbuf tp;
|
|
|
|
PWCHAR runpath = tp.w_get ();
|
2006-07-17 21:30:30 +02:00
|
|
|
int c_flags;
|
|
|
|
|
2005-08-11 18:13:30 +02:00
|
|
|
bool null_app_name = false;
|
2009-01-09 06:18:02 +01:00
|
|
|
STARTUPINFOW si = {};
|
2006-03-18 20:17:21 +01:00
|
|
|
int looped = 0;
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2013-05-03 21:39:01 +02:00
|
|
|
system_call_handle system_call (mode == _P_SYSTEM);
|
2014-08-22 11:21:33 +02:00
|
|
|
|
|
|
|
__try
|
2005-08-11 18:13:30 +02:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
child_info_types chtype;
|
|
|
|
if (mode == _P_OVERLAY)
|
|
|
|
chtype = _CH_EXEC;
|
2005-09-15 01:03:44 +02:00
|
|
|
else
|
2014-08-22 11:21:33 +02:00
|
|
|
chtype = _CH_SPAWN;
|
2000-10-14 07:52:38 +02:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
moreinfo = cygheap_exec_info::alloc ();
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
/* CreateProcess takes one long string that is the command line (sigh).
|
|
|
|
We need to quote any argument that has whitespace or embedded "'s. */
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
int ac;
|
|
|
|
for (ac = 0; argv[ac]; ac++)
|
|
|
|
/* nothing */;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
int err;
|
|
|
|
const char *ext;
|
|
|
|
if ((ext = perhaps_suffix (prog_arg, real_path, err, FE_NADA)) == NULL)
|
|
|
|
{
|
|
|
|
set_errno (err);
|
|
|
|
res = -1;
|
|
|
|
__leave;
|
|
|
|
}
|
2008-11-26 18:21:04 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
res = newargv.setup (prog_arg, real_path, ext, ac, argv, p_type_exec);
|
2009-06-22 17:40:59 +02:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
if (res)
|
|
|
|
__leave;
|
2006-01-10 03:39:22 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
if (!real_path.iscygexec () && ::cygheap->cwd.get_error ())
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
small_printf ("Error: Current working directory %s.\n"
|
|
|
|
"Can't start native Windows application from here.\n\n",
|
|
|
|
::cygheap->cwd.get_error_desc ());
|
|
|
|
set_errno (::cygheap->cwd.get_error ());
|
2006-04-12 17:53:22 +02:00
|
|
|
res = -1;
|
2014-08-22 11:21:33 +02:00
|
|
|
__leave;
|
2003-09-26 10:45:24 +02:00
|
|
|
}
|
2000-09-13 21:57:00 +02:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
if (ac == 3 && argv[1][0] == '/' && tolower (argv[1][1]) == 'c' &&
|
|
|
|
(iscmd (argv[0], "command.com") || iscmd (argv[0], "cmd.exe")))
|
|
|
|
{
|
|
|
|
real_path.check (prog_arg);
|
|
|
|
cmd.add ("\"");
|
|
|
|
if (!real_path.error)
|
|
|
|
cmd.add (real_path.get_win32 ());
|
|
|
|
else
|
|
|
|
cmd.add (argv[0]);
|
|
|
|
cmd.add ("\"");
|
|
|
|
cmd.add (" ");
|
|
|
|
cmd.add (argv[1]);
|
|
|
|
cmd.add (" ");
|
|
|
|
cmd.add (argv[2]);
|
|
|
|
real_path.set_path (argv[0]);
|
|
|
|
null_app_name = true;
|
|
|
|
}
|
2006-01-10 03:39:22 +01:00
|
|
|
else
|
2014-08-22 11:21:33 +02:00
|
|
|
{
|
|
|
|
if (real_path.iscygexec ())
|
|
|
|
{
|
|
|
|
moreinfo->argc = newargv.argc;
|
|
|
|
moreinfo->argv = newargv;
|
|
|
|
}
|
|
|
|
if ((wincmdln || !real_path.iscygexec ())
|
|
|
|
&& !cmd.fromargv (newargv, real_path.get_win32 (),
|
|
|
|
real_path.iscygexec ()))
|
|
|
|
{
|
|
|
|
res = -1;
|
|
|
|
__leave;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2006-12-11 19:55:29 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
if (mode != _P_OVERLAY || !real_path.iscygexec ()
|
|
|
|
|| !DuplicateHandle (GetCurrentProcess (), myself.shared_handle (),
|
|
|
|
GetCurrentProcess (), &moreinfo->myself_pinfo,
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
|
moreinfo->myself_pinfo = NULL;
|
|
|
|
else
|
|
|
|
VerifyHandle (moreinfo->myself_pinfo);
|
|
|
|
}
|
2006-12-11 19:55:29 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
pi.hProcess = pi.hThread = NULL;
|
|
|
|
pi.dwProcessId = pi.dwThreadId = 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
c_flags = GetPriorityClass (GetCurrentProcess ());
|
|
|
|
sigproc_printf ("priority class %d", c_flags);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
c_flags |= CREATE_SEPARATE_WOW_VDM | CREATE_UNICODE_ENVIRONMENT;
|
2004-11-26 05:15:10 +01:00
|
|
|
|
2015-12-15 15:53:16 +01:00
|
|
|
/* We're adding the CREATE_BREAKAWAY_FROM_JOB flag here to workaround
|
|
|
|
issues with the "Program Compatibility Assistant (PCA) Service".
|
|
|
|
For some reason, when starting long running sessions from mintty(*),
|
|
|
|
the affected svchost.exe process takes more and more memory and at one
|
|
|
|
point takes over the CPU. At this point the machine becomes
|
|
|
|
unresponsive. The only way to get back to normal is to stop the
|
|
|
|
entire mintty session, or to stop the PCA service. However, a process
|
|
|
|
which is controlled by PCA is part of a compatibility job, which
|
|
|
|
allows child processes to break away from the job. This helps to
|
|
|
|
avoid this issue.
|
|
|
|
|
|
|
|
First we call IsProcessInJob. It fetches the information whether or
|
|
|
|
not we're part of a job 20 times faster than QueryInformationJobObject.
|
|
|
|
|
|
|
|
(*) Note that this is not mintty's fault. It has just been observed
|
|
|
|
with mintty in the first place. See the archives for more info:
|
|
|
|
http://cygwin.com/ml/cygwin-developers/2012-02/msg00018.html */
|
|
|
|
JOBOBJECT_BASIC_LIMIT_INFORMATION jobinfo;
|
|
|
|
BOOL is_in_job;
|
|
|
|
|
|
|
|
if (IsProcessInJob (GetCurrentProcess (), NULL, &is_in_job)
|
|
|
|
&& is_in_job
|
|
|
|
&& QueryInformationJobObject (NULL, JobObjectBasicLimitInformation,
|
|
|
|
&jobinfo, sizeof jobinfo, NULL)
|
|
|
|
&& (jobinfo.LimitFlags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK
|
|
|
|
| JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
|
2004-12-05 20:41:26 +01:00
|
|
|
{
|
2015-12-15 15:53:16 +01:00
|
|
|
debug_printf ("Add CREATE_BREAKAWAY_FROM_JOB");
|
|
|
|
c_flags |= CREATE_BREAKAWAY_FROM_JOB;
|
2004-12-05 20:41:26 +01:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
if (mode == _P_DETACH)
|
|
|
|
c_flags |= DETACHED_PROCESS;
|
|
|
|
else
|
|
|
|
fhandler_console::need_invisible ();
|
|
|
|
|
|
|
|
if (mode != _P_OVERLAY)
|
|
|
|
myself->exec_sendsig = NULL;
|
|
|
|
else
|
2010-01-25 12:21:56 +01:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
/* Reset sendsig so that any process which wants to send a signal
|
|
|
|
to this pid will wait for the new process to become active.
|
|
|
|
Save the old value in case the exec fails. */
|
|
|
|
if (!myself->exec_sendsig)
|
2011-02-15 16:25:59 +01:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
myself->exec_sendsig = myself->sendsig;
|
|
|
|
myself->exec_dwProcessId = myself->dwProcessId;
|
|
|
|
myself->sendsig = NULL;
|
|
|
|
reset_sendsig = true;
|
2011-02-15 16:25:59 +01:00
|
|
|
}
|
2010-01-25 12:21:56 +01:00
|
|
|
}
|
2000-10-20 06:20:21 +02:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
if (null_app_name)
|
|
|
|
runpath = NULL;
|
2011-02-15 16:25:59 +01:00
|
|
|
else
|
2011-06-06 07:02:13 +02:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
USHORT len = real_path.get_nt_native_path ()->Length / sizeof (WCHAR);
|
|
|
|
if (RtlEqualUnicodePathPrefix (real_path.get_nt_native_path (),
|
|
|
|
&ro_u_natp, FALSE))
|
|
|
|
{
|
|
|
|
runpath = real_path.get_wide_win32_path (runpath);
|
|
|
|
/* If the executable path length is < MAX_PATH, make sure the long
|
|
|
|
path win32 prefix is removed from the path to make subsequent
|
|
|
|
not long path aware native Win32 child processes happy. */
|
|
|
|
if (len < MAX_PATH + 4)
|
|
|
|
{
|
|
|
|
if (runpath[5] == ':')
|
|
|
|
runpath += 4;
|
|
|
|
else if (len < MAX_PATH + 6)
|
|
|
|
*(runpath += 6) = L'\\';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (len < NT_MAX_PATH - ro_u_globalroot.Length / sizeof (WCHAR))
|
|
|
|
{
|
|
|
|
UNICODE_STRING rpath;
|
2004-09-12 05:47:57 +02:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
RtlInitEmptyUnicodeString (&rpath, runpath,
|
|
|
|
(NT_MAX_PATH - 1) * sizeof (WCHAR));
|
|
|
|
RtlCopyUnicodeString (&rpath, &ro_u_globalroot);
|
|
|
|
RtlAppendUnicodeStringToString (&rpath,
|
|
|
|
real_path.get_nt_native_path ());
|
|
|
|
}
|
2007-08-17 21:58:57 +02:00
|
|
|
else
|
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
set_errno (ENAMETOOLONG);
|
|
|
|
res = -1;
|
|
|
|
__leave;
|
2007-08-17 21:58:57 +02:00
|
|
|
}
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
cygbench ("spawn-worker");
|
|
|
|
|
|
|
|
if (!real_path.iscygexec())
|
|
|
|
::cygheap->fdtab.set_file_pointers_for_exec ();
|
|
|
|
|
2014-12-02 11:16:03 +01:00
|
|
|
/* If we switch the user, merge the user's Windows environment. */
|
|
|
|
bool switch_user = ::cygheap->user.issetuid ()
|
|
|
|
&& (::cygheap->user.saved_uid
|
|
|
|
!= ::cygheap->user.real_uid);
|
2014-08-22 11:21:33 +02:00
|
|
|
moreinfo->envp = build_env (envp, envblock, moreinfo->envc,
|
2014-12-02 11:16:03 +01:00
|
|
|
real_path.iscygexec (),
|
|
|
|
switch_user ? ::cygheap->user.primary_token ()
|
|
|
|
: NULL);
|
2014-08-22 11:21:33 +02:00
|
|
|
if (!moreinfo->envp || !envblock)
|
|
|
|
{
|
|
|
|
set_errno (E2BIG);
|
|
|
|
res = -1;
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
set (chtype, real_path.iscygexec ());
|
|
|
|
__stdin = in__stdin;
|
|
|
|
__stdout = in__stdout;
|
|
|
|
record_children ();
|
|
|
|
|
|
|
|
si.lpReserved2 = (LPBYTE) this;
|
|
|
|
si.cbReserved2 = sizeof (*this);
|
|
|
|
|
|
|
|
/* Depends on set call above.
|
|
|
|
Some file types might need extra effort in the parent after CreateProcess
|
|
|
|
and before copying the datastructures to the child. So we have to start
|
|
|
|
the child in suspend state, unfortunately, to avoid a race condition. */
|
|
|
|
if (!newargv.win16_exe
|
|
|
|
&& (!iscygwin () || mode != _P_OVERLAY
|
|
|
|
|| ::cygheap->fdtab.need_fixup_before ()))
|
|
|
|
c_flags |= CREATE_SUSPENDED;
|
|
|
|
/* If a native application should be spawned, we test here if the spawning
|
|
|
|
process is running in a console and, if so, if it's a foreground or
|
|
|
|
background process. If it's a background process, we start the native
|
|
|
|
process with the CREATE_NEW_PROCESS_GROUP flag set. This lets the native
|
|
|
|
process ignore Ctrl-C by default. If we don't do that, pressing Ctrl-C
|
|
|
|
in a console will break native processes running in the background,
|
|
|
|
because the Ctrl-C event is sent to all processes in the console, unless
|
|
|
|
they ignore it explicitely. CREATE_NEW_PROCESS_GROUP does that for us. */
|
2019-09-08 14:58:35 +02:00
|
|
|
if (!iscygwin () && ctty_pgid && ctty_pgid != myself->pgid)
|
2014-08-22 11:21:33 +02:00
|
|
|
c_flags |= CREATE_NEW_PROCESS_GROUP;
|
|
|
|
refresh_cygheap ();
|
|
|
|
|
|
|
|
if (mode == _P_DETACH)
|
|
|
|
/* all set */;
|
|
|
|
else if (mode != _P_OVERLAY || !my_wr_proc_pipe)
|
|
|
|
prefork ();
|
|
|
|
else
|
|
|
|
wr_proc_pipe = my_wr_proc_pipe;
|
|
|
|
|
|
|
|
/* Don't allow child to inherit these handles if it's not a Cygwin program.
|
|
|
|
wr_proc_pipe will be injected later. parent won't be used by the child
|
|
|
|
so there is no reason for the child to have it open as it can confuse
|
|
|
|
ps into thinking that children of windows processes are all part of
|
|
|
|
the same "execed" process.
|
|
|
|
FIXME: Someday, make it so that parent is never created when starting
|
|
|
|
non-Cygwin processes. */
|
|
|
|
if (!iscygwin ())
|
2007-08-17 21:58:57 +02:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
SetHandleInformation (wr_proc_pipe, HANDLE_FLAG_INHERIT, 0);
|
|
|
|
SetHandleInformation (parent, HANDLE_FLAG_INHERIT, 0);
|
2007-08-17 21:58:57 +02:00
|
|
|
}
|
2014-08-22 11:21:33 +02:00
|
|
|
/* FIXME: racy */
|
|
|
|
if (mode != _P_OVERLAY)
|
|
|
|
SetHandleInformation (my_wr_proc_pipe, HANDLE_FLAG_INHERIT, 0);
|
|
|
|
parent_winpid = GetCurrentProcessId ();
|
|
|
|
|
2019-03-12 11:41:35 +01:00
|
|
|
PSECURITY_ATTRIBUTES sa = (PSECURITY_ATTRIBUTES) tp.w_get ();
|
|
|
|
if (!sec_user_nih (sa, cygheap->user.sid (),
|
|
|
|
well_known_authenticated_users_sid,
|
|
|
|
PROCESS_QUERY_LIMITED_INFORMATION))
|
|
|
|
sa = &sec_none_nih;
|
|
|
|
|
2019-08-27 20:04:02 +02:00
|
|
|
/* Attach to pseudo console if pty salve is used */
|
2019-11-16 00:27:24 +01:00
|
|
|
pid_restore = fhandler_console::get_console_process_id
|
2019-08-27 20:04:02 +02:00
|
|
|
(GetCurrentProcessId (), false);
|
2019-09-04 03:45:35 +02:00
|
|
|
for (int i = 0; i < 3; i ++)
|
2019-08-27 20:04:02 +02:00
|
|
|
{
|
2019-09-13 23:48:14 +02:00
|
|
|
const int chk_order[] = {1, 0, 2};
|
2019-09-04 03:45:35 +02:00
|
|
|
int fd = chk_order[i];
|
2019-08-27 20:04:02 +02:00
|
|
|
fhandler_base *fh = ::cygheap->fdtab[fd];
|
|
|
|
if (fh && fh->get_major () == DEV_PTYS_MAJOR)
|
|
|
|
{
|
2019-09-13 23:48:14 +02:00
|
|
|
fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
|
2019-11-16 00:27:24 +01:00
|
|
|
if (ptys->get_pseudo_console ())
|
2019-08-27 20:04:02 +02:00
|
|
|
{
|
2019-11-16 00:27:24 +01:00
|
|
|
DWORD helper_process_id = ptys->get_helper_process_id ();
|
2019-08-27 20:04:02 +02:00
|
|
|
debug_printf ("found a PTY slave %d: helper_PID=%d",
|
2019-11-16 00:27:24 +01:00
|
|
|
fh->get_minor (), helper_process_id);
|
2019-09-04 03:45:35 +02:00
|
|
|
if (fhandler_console::get_console_process_id
|
2019-11-16 00:27:24 +01:00
|
|
|
(helper_process_id, true))
|
2019-09-13 23:48:14 +02:00
|
|
|
/* Already attached */
|
|
|
|
attach_to_console = true;
|
|
|
|
else if (!attach_to_console)
|
2019-09-04 03:45:35 +02:00
|
|
|
{
|
|
|
|
FreeConsole ();
|
2019-11-16 00:27:24 +01:00
|
|
|
if (AttachConsole (helper_process_id))
|
2019-09-13 23:48:14 +02:00
|
|
|
attach_to_console = true;
|
2019-08-27 20:04:02 +02:00
|
|
|
}
|
2019-09-13 23:48:14 +02:00
|
|
|
ptys->fixup_after_attach (!iscygwin (), fd);
|
2020-01-10 12:46:26 +01:00
|
|
|
if (mode == _P_OVERLAY)
|
|
|
|
ptys->set_freeconsole_on_close (iscygwin ());
|
2019-08-27 20:04:02 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-13 23:48:14 +02:00
|
|
|
else if (fh && fh->get_major () == DEV_CONS_MAJOR)
|
2020-01-14 03:09:42 +01:00
|
|
|
{
|
|
|
|
attach_to_console = true;
|
2020-02-18 10:12:54 +01:00
|
|
|
fhandler_console *cons = (fhandler_console *) fh;
|
2020-01-14 03:09:42 +01:00
|
|
|
if (wincap.has_con_24bit_colors () && !iscygwin ())
|
2020-02-17 11:29:07 +01:00
|
|
|
if (fd == 1 || fd == 2)
|
2020-02-18 10:12:54 +01:00
|
|
|
cons->request_xterm_mode_output (false);
|
2020-01-14 03:09:42 +01:00
|
|
|
}
|
2019-08-27 20:04:02 +02:00
|
|
|
}
|
2019-09-13 23:48:14 +02:00
|
|
|
|
|
|
|
/* Set up needed handles for stdio */
|
|
|
|
si.dwFlags = STARTF_USESTDHANDLES;
|
|
|
|
si.hStdInput = handle ((in__stdin < 0 ? 0 : in__stdin), false);
|
|
|
|
si.hStdOutput = handle ((in__stdout < 0 ? 1 : in__stdout), true);
|
|
|
|
si.hStdError = handle (2, true);
|
|
|
|
|
|
|
|
si.cb = sizeof (si);
|
2019-08-27 20:04:02 +02:00
|
|
|
|
2019-09-04 15:47:42 +02:00
|
|
|
if (!iscygwin ())
|
2019-09-20 05:04:36 +02:00
|
|
|
init_console_handler (myself->ctty > 0);
|
2019-09-04 15:47:42 +02:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
loop:
|
|
|
|
/* When ruid != euid we create the new process under the current original
|
|
|
|
account and impersonate in child, this way maintaining the different
|
|
|
|
effective vs. real ids.
|
|
|
|
FIXME: If ruid != euid and ruid != saved_uid we currently give
|
|
|
|
up on ruid. The new process will have ruid == euid. */
|
|
|
|
::cygheap->user.deimpersonate ();
|
|
|
|
|
|
|
|
if (!real_path.iscygexec () && mode == _P_OVERLAY)
|
|
|
|
myself->process_state |= PID_NOTCYGWIN;
|
|
|
|
|
2019-02-08 15:49:47 +01:00
|
|
|
cygpid = (mode != _P_OVERLAY) ? create_cygwin_pid () : myself->pid;
|
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
wchar_t wcmd[(size_t) cmd];
|
|
|
|
if (!::cygheap->user.issetuid ()
|
|
|
|
|| (::cygheap->user.saved_uid == ::cygheap->user.real_uid
|
|
|
|
&& ::cygheap->user.saved_gid == ::cygheap->user.real_gid
|
|
|
|
&& !::cygheap->user.groups.issetgroups ()
|
|
|
|
&& !::cygheap->user.setuid_to_restricted))
|
2007-08-17 21:58:57 +02:00
|
|
|
{
|
2019-03-12 11:41:35 +01:00
|
|
|
rc = CreateProcessW (runpath, /* image name w/ full path */
|
|
|
|
cmd.wcs (wcmd), /* what was passed to exec */
|
|
|
|
sa, /* process security attrs */
|
|
|
|
sa, /* thread security attrs */
|
|
|
|
TRUE, /* inherit handles */
|
2014-08-22 11:21:33 +02:00
|
|
|
c_flags,
|
2019-03-12 11:41:35 +01:00
|
|
|
envblock, /* environment */
|
2014-08-22 11:21:33 +02:00
|
|
|
NULL,
|
|
|
|
&si,
|
|
|
|
&pi);
|
2007-08-17 21:58:57 +02:00
|
|
|
}
|
2014-08-22 11:21:33 +02:00
|
|
|
else
|
2004-12-05 20:41:26 +01:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
/* Give access to myself */
|
|
|
|
if (mode == _P_OVERLAY)
|
|
|
|
myself.set_acl();
|
|
|
|
|
2016-06-25 00:42:36 +02:00
|
|
|
HWINSTA hwst = NULL;
|
|
|
|
HWINSTA hwst_orig = GetProcessWindowStation ();
|
|
|
|
HDESK hdsk = NULL;
|
|
|
|
HDESK hdsk_orig = GetThreadDesktop (GetCurrentThreadId ());
|
|
|
|
/* Don't create WindowStation and Desktop for restricted child. */
|
|
|
|
if (!::cygheap->user.setuid_to_restricted)
|
2014-08-22 11:21:33 +02:00
|
|
|
{
|
2016-06-25 00:42:36 +02:00
|
|
|
PSECURITY_ATTRIBUTES sa;
|
2014-08-22 11:21:33 +02:00
|
|
|
WCHAR sid[128];
|
2016-06-25 00:42:36 +02:00
|
|
|
WCHAR wstname[1024] = { L'\0' };
|
2014-08-22 11:21:33 +02:00
|
|
|
|
|
|
|
sa = sec_user ((PSECURITY_ATTRIBUTES) alloca (1024),
|
|
|
|
::cygheap->user.sid ());
|
2016-06-25 00:42:36 +02:00
|
|
|
/* We're creating a window station per user, not per logon
|
|
|
|
session First of all we might not have a valid logon session
|
|
|
|
for the user (logon by create_token), and second, it doesn't
|
2014-08-22 11:21:33 +02:00
|
|
|
make sense in terms of security to create a new window
|
|
|
|
station for every logon of the same user. It just fills up
|
|
|
|
the system with window stations for no good reason. */
|
2014-12-02 11:09:13 +01:00
|
|
|
hwst = CreateWindowStationW (::cygheap->user.get_windows_id (sid),
|
|
|
|
0, GENERIC_READ | GENERIC_WRITE, sa);
|
2014-08-22 11:21:33 +02:00
|
|
|
if (!hwst)
|
|
|
|
system_printf ("CreateWindowStation failed, %E");
|
|
|
|
else if (!SetProcessWindowStation (hwst))
|
|
|
|
system_printf ("SetProcessWindowStation failed, %E");
|
|
|
|
else if (!(hdsk = CreateDesktopW (L"Default", NULL, NULL, 0,
|
|
|
|
GENERIC_ALL, sa)))
|
|
|
|
system_printf ("CreateDesktop failed, %E");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wcpcpy (wcpcpy (wstname, sid), L"\\Default");
|
|
|
|
si.lpDesktop = wstname;
|
|
|
|
debug_printf ("Desktop: %W", si.lpDesktop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = CreateProcessAsUserW (::cygheap->user.primary_token (),
|
2019-03-12 11:41:35 +01:00
|
|
|
runpath, /* image name w/ full path */
|
|
|
|
cmd.wcs (wcmd), /* what was passed to exec */
|
|
|
|
sa, /* process security attrs */
|
|
|
|
sa, /* thread security attrs */
|
|
|
|
TRUE, /* inherit handles */
|
2014-08-22 11:21:33 +02:00
|
|
|
c_flags,
|
2019-03-12 11:41:35 +01:00
|
|
|
envblock, /* environment */
|
2014-08-22 11:21:33 +02:00
|
|
|
NULL,
|
|
|
|
&si,
|
|
|
|
&pi);
|
|
|
|
if (hwst)
|
|
|
|
{
|
|
|
|
SetProcessWindowStation (hwst_orig);
|
|
|
|
CloseWindowStation (hwst);
|
|
|
|
}
|
|
|
|
if (hdsk)
|
|
|
|
{
|
|
|
|
SetThreadDesktop (hdsk_orig);
|
|
|
|
CloseDesktop (hdsk);
|
|
|
|
}
|
2004-12-05 20:41:26 +01:00
|
|
|
}
|
2013-03-09 01:11:31 +01:00
|
|
|
|
|
|
|
if (mode != _P_OVERLAY)
|
2014-08-22 11:21:33 +02:00
|
|
|
SetHandleInformation (my_wr_proc_pipe, HANDLE_FLAG_INHERIT,
|
|
|
|
HANDLE_FLAG_INHERIT);
|
2013-03-09 01:11:31 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
/* Set errno now so that debugging messages from it appear before our
|
|
|
|
final debugging message [this is a general rule for debugging
|
|
|
|
messages]. */
|
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
syscall_printf ("CreateProcess failed, %E");
|
|
|
|
/* If this was a failed exec, restore the saved sendsig. */
|
|
|
|
if (reset_sendsig)
|
|
|
|
{
|
|
|
|
myself->sendsig = myself->exec_sendsig;
|
|
|
|
myself->exec_sendsig = NULL;
|
|
|
|
}
|
|
|
|
myself->process_state &= ~PID_NOTCYGWIN;
|
2016-06-25 00:42:36 +02:00
|
|
|
/* Reset handle inheritance to default when the execution of a'
|
|
|
|
non-Cygwin process fails. Only need to do this for _P_OVERLAY
|
|
|
|
since the handle will be closed otherwise. Don't need to do
|
|
|
|
this for 'parent' since it will be closed in every case.
|
|
|
|
See FIXME above. */
|
2014-08-22 11:21:33 +02:00
|
|
|
if (!iscygwin () && mode == _P_OVERLAY)
|
|
|
|
SetHandleInformation (wr_proc_pipe, HANDLE_FLAG_INHERIT,
|
|
|
|
HANDLE_FLAG_INHERIT);
|
|
|
|
if (wr_proc_pipe == my_wr_proc_pipe)
|
2016-06-25 00:42:36 +02:00
|
|
|
wr_proc_pipe = NULL; /* We still own it: don't nuke in destructor */
|
2014-08-22 11:21:33 +02:00
|
|
|
|
|
|
|
/* Restore impersonation. In case of _P_OVERLAY this isn't
|
|
|
|
allowed since it would overwrite child data. */
|
|
|
|
if (mode != _P_OVERLAY)
|
|
|
|
::cygheap->user.reimpersonate ();
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
res = -1;
|
|
|
|
__leave;
|
|
|
|
}
|
2005-12-29 21:46:34 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
/* The CREATE_SUSPENDED case is handled below */
|
|
|
|
if (iscygwin () && !(c_flags & CREATE_SUSPENDED))
|
|
|
|
strace.write_childpid (pi.dwProcessId);
|
2009-11-17 11:43:01 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
/* Fixup the parent data structures if needed and resume the child's
|
|
|
|
main thread. */
|
|
|
|
if (::cygheap->fdtab.need_fixup_before ())
|
|
|
|
::cygheap->fdtab.fixup_before_exec (pi.dwProcessId);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2016-06-25 00:42:36 +02:00
|
|
|
/* Print the original program name here so the user can see that too. */
|
2014-08-22 11:21:33 +02:00
|
|
|
syscall_printf ("pid %d, prog_arg %s, cmd line %.9500s)",
|
|
|
|
rc ? cygpid : (unsigned int) -1, prog_arg, (const char *) cmd);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
/* Name the handle similarly to proc_subproc. */
|
|
|
|
ProtectHandle1 (pi.hProcess, childhProc);
|
|
|
|
|
|
|
|
if (mode == _P_OVERLAY)
|
2000-07-29 18:24:59 +02:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
myself->dwProcessId = pi.dwProcessId;
|
|
|
|
strace.execing = 1;
|
|
|
|
myself.hProcess = hExeced = pi.hProcess;
|
2019-04-02 13:00:22 +02:00
|
|
|
HANDLE old_winpid_hdl = myself.shared_winpid_handle ();
|
2019-02-02 20:01:41 +01:00
|
|
|
if (!real_path.iscygexec ())
|
|
|
|
{
|
|
|
|
/* If the child process is not a Cygwin process, we have to
|
2019-04-02 13:00:22 +02:00
|
|
|
create a new winpid symlink on behalf of the child process
|
|
|
|
not being able to do this by itself. */
|
2019-02-02 20:01:41 +01:00
|
|
|
myself.create_winpid_symlink ();
|
|
|
|
}
|
2019-04-02 13:00:22 +02:00
|
|
|
NtClose (old_winpid_hdl);
|
2014-08-22 11:21:33 +02:00
|
|
|
real_path.get_wide_win32_path (myself->progname); // FIXME: race?
|
|
|
|
sigproc_printf ("new process name %W", myself->progname);
|
2020-01-10 12:46:26 +01:00
|
|
|
if (!iscygwin ())
|
|
|
|
close_all_files ();
|
2000-07-29 18:24:59 +02:00
|
|
|
}
|
2014-08-22 11:21:33 +02:00
|
|
|
else
|
2004-12-23 15:57:08 +01:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
myself->set_has_pgid_children ();
|
|
|
|
ProtectHandle (pi.hThread);
|
|
|
|
pinfo child (cygpid,
|
|
|
|
PID_IN_USE | (real_path.iscygexec () ? 0 : PID_NOTCYGWIN));
|
|
|
|
if (!child)
|
|
|
|
{
|
|
|
|
syscall_printf ("pinfo failed");
|
|
|
|
if (get_errno () != ENOMEM)
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
res = -1;
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
child->dwProcessId = pi.dwProcessId;
|
|
|
|
child.hProcess = pi.hProcess;
|
|
|
|
|
|
|
|
real_path.get_wide_win32_path (child->progname);
|
2019-02-02 20:01:41 +01:00
|
|
|
/* This introduces an unreferenced, open handle into the child.
|
|
|
|
The purpose is to keep the pid shared memory open so that all
|
|
|
|
of the fields filled out by child.remember do not disappear
|
|
|
|
and so there is not a brief period during which the pid is
|
|
|
|
not available. */
|
2014-08-22 11:21:33 +02:00
|
|
|
DuplicateHandle (GetCurrentProcess (), child.shared_handle (),
|
|
|
|
pi.hProcess, NULL, 0, 0, DUPLICATE_SAME_ACCESS);
|
2019-02-02 20:01:41 +01:00
|
|
|
if (!real_path.iscygexec ())
|
|
|
|
{
|
|
|
|
/* If the child process is not a Cygwin process, we have to
|
|
|
|
create a new winpid symlink and induce it into the child
|
|
|
|
process as well to keep it over the lifetime of the child. */
|
|
|
|
child.create_winpid_symlink ();
|
|
|
|
DuplicateHandle (GetCurrentProcess (),
|
|
|
|
child.shared_winpid_handle (),
|
|
|
|
pi.hProcess, NULL, 0, 0, DUPLICATE_SAME_ACCESS);
|
|
|
|
}
|
2014-08-22 11:21:33 +02:00
|
|
|
child->start_time = time (NULL); /* Register child's starting time. */
|
|
|
|
child->nice = myself->nice;
|
|
|
|
postfork (child);
|
2019-07-31 12:35:31 +02:00
|
|
|
if (mode == _P_DETACH
|
|
|
|
? !child.remember (true)
|
|
|
|
: !(child.remember (false) && child.reattach ()))
|
2014-08-22 11:21:33 +02:00
|
|
|
{
|
|
|
|
/* FIXME: Child in strange state now */
|
|
|
|
CloseHandle (pi.hProcess);
|
|
|
|
ForceCloseHandle (pi.hThread);
|
|
|
|
res = -1;
|
|
|
|
__leave;
|
|
|
|
}
|
2004-12-23 15:57:08 +01:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
/* Start the child running */
|
|
|
|
if (c_flags & CREATE_SUSPENDED)
|
|
|
|
{
|
|
|
|
/* Inject a non-inheritable wr_proc_pipe handle into child so that we
|
|
|
|
can accurately track when the child exits without keeping this
|
|
|
|
process waiting around for it to exit. */
|
|
|
|
if (!iscygwin ())
|
|
|
|
DuplicateHandle (GetCurrentProcess (), wr_proc_pipe, pi.hProcess, NULL,
|
|
|
|
0, false, DUPLICATE_SAME_ACCESS);
|
|
|
|
ResumeThread (pi.hThread);
|
|
|
|
if (iscygwin ())
|
|
|
|
strace.write_childpid (pi.dwProcessId);
|
|
|
|
}
|
|
|
|
ForceCloseHandle (pi.hThread);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
sigproc_printf ("spawned windows pid %d", pi.dwProcessId);
|
2000-10-17 01:55:58 +02:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
bool synced;
|
|
|
|
if ((mode == _P_DETACH || mode == _P_NOWAIT) && !iscygwin ())
|
|
|
|
synced = false;
|
|
|
|
else
|
|
|
|
/* Just mark a non-cygwin process as 'synced'. We will still eventually
|
|
|
|
wait for it to exit in maybe_set_exit_code_from_windows(). */
|
|
|
|
synced = iscygwin () ? sync (pi.dwProcessId, pi.hProcess, INFINITE) : true;
|
2001-06-26 23:03:08 +02:00
|
|
|
|
2014-08-22 11:21:33 +02:00
|
|
|
switch (mode)
|
2006-03-18 20:17:21 +01:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
case _P_OVERLAY:
|
|
|
|
myself.hProcess = pi.hProcess;
|
|
|
|
if (!synced)
|
|
|
|
{
|
|
|
|
if (!proc_retry (pi.hProcess))
|
|
|
|
{
|
|
|
|
looped++;
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
close_all_files (true);
|
|
|
|
}
|
|
|
|
else
|
2006-03-18 20:17:21 +01:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
if (iscygwin ())
|
|
|
|
close_all_files (true);
|
|
|
|
if (!my_wr_proc_pipe
|
|
|
|
&& WaitForSingleObject (pi.hProcess, 0) == WAIT_TIMEOUT)
|
|
|
|
wait_for_myself ();
|
2006-03-18 20:17:21 +01:00
|
|
|
}
|
2020-08-01 21:35:58 +02:00
|
|
|
if (sem)
|
|
|
|
__posix_spawn_sem_release (sem, 0);
|
2014-08-22 11:21:33 +02:00
|
|
|
myself.exit (EXITCODE_NOSET);
|
|
|
|
break;
|
|
|
|
case _P_WAIT:
|
|
|
|
case _P_SYSTEM:
|
|
|
|
system_call.arm ();
|
|
|
|
if (waitpid (cygpid, &res, 0) != cygpid)
|
|
|
|
res = -1;
|
|
|
|
break;
|
|
|
|
case _P_DETACH:
|
|
|
|
res = 0; /* Lost all memory of this child. */
|
|
|
|
break;
|
|
|
|
case _P_NOWAIT:
|
|
|
|
case _P_NOWAITO:
|
|
|
|
case _P_VFORK:
|
|
|
|
res = cygpid;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2006-03-18 20:17:21 +01:00
|
|
|
}
|
2014-08-22 11:21:33 +02:00
|
|
|
}
|
|
|
|
__except (NO_ERROR)
|
|
|
|
{
|
|
|
|
if (get_errno () == ENOMEM)
|
|
|
|
set_errno (E2BIG);
|
2006-03-22 04:20:28 +01:00
|
|
|
else
|
2014-08-22 11:21:33 +02:00
|
|
|
set_errno (EFAULT);
|
|
|
|
res = -1;
|
2005-01-11 16:31:04 +01:00
|
|
|
}
|
2014-08-22 11:21:33 +02:00
|
|
|
__endtry
|
2011-11-14 02:29:49 +01:00
|
|
|
this->cleanup ();
|
2006-03-18 20:17:21 +01:00
|
|
|
if (envblock)
|
|
|
|
free (envblock);
|
2019-08-27 20:04:02 +02:00
|
|
|
|
2019-11-16 00:27:24 +01:00
|
|
|
if (attach_to_console && pid_restore)
|
2019-08-27 20:04:02 +02:00
|
|
|
{
|
|
|
|
FreeConsole ();
|
2019-11-16 00:27:24 +01:00
|
|
|
AttachConsole (pid_restore);
|
2020-01-16 12:04:47 +01:00
|
|
|
cygheap_fdenum cfd (false);
|
|
|
|
int fd;
|
|
|
|
while ((fd = cfd.next ()) >= 0)
|
|
|
|
if (cfd->get_major () == DEV_PTYS_MAJOR)
|
|
|
|
{
|
|
|
|
fhandler_pty_slave *ptys =
|
|
|
|
(fhandler_pty_slave *) (fhandler_base *) cfd;
|
|
|
|
ptys->fixup_after_attach (false, fd);
|
|
|
|
}
|
2019-08-27 20:04:02 +02:00
|
|
|
}
|
|
|
|
|
2005-01-11 16:31:04 +01:00
|
|
|
return (int) res;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2001-08-22 19:50:22 +02:00
|
|
|
extern "C" int
|
2000-02-17 20:38:33 +01:00
|
|
|
cwait (int *result, int pid, int)
|
|
|
|
{
|
2005-07-17 00:01:50 +02:00
|
|
|
return waitpid (pid, result, 0);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-12-05 20:41:26 +01:00
|
|
|
* Helper function for spawn runtime calls.
|
|
|
|
* Doesn't search the path.
|
|
|
|
*/
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
extern "C" int
|
2002-06-11 04:08:00 +02:00
|
|
|
spawnve (int mode, const char *path, const char *const *argv,
|
2004-12-05 20:41:26 +01:00
|
|
|
const char *const *envp)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2011-01-19 11:28:39 +01:00
|
|
|
static char *const empty_env[] = { NULL };
|
|
|
|
|
2005-07-17 00:01:50 +02:00
|
|
|
int ret;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
syscall_printf ("spawnve (%s, %s, %p)", path, argv[0], envp);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2011-01-19 11:28:39 +01:00
|
|
|
if (!envp)
|
|
|
|
envp = empty_env;
|
|
|
|
|
2011-01-19 10:15:17 +01:00
|
|
|
switch (_P_MODE (mode))
|
2005-07-17 00:01:50 +02:00
|
|
|
{
|
|
|
|
case _P_OVERLAY:
|
2011-11-14 02:29:49 +01:00
|
|
|
ch_spawn.worker (path, argv, envp, mode);
|
|
|
|
/* Errno should be set by worker. */
|
2005-07-17 00:01:50 +02:00
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
case _P_VFORK:
|
|
|
|
case _P_NOWAIT:
|
|
|
|
case _P_NOWAITO:
|
|
|
|
case _P_WAIT:
|
|
|
|
case _P_DETACH:
|
|
|
|
case _P_SYSTEM:
|
2011-11-14 02:29:49 +01:00
|
|
|
ret = ch_spawn.worker (path, argv, envp, mode);
|
2005-07-17 00:01:50 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_errno (EINVAL);
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-12-05 20:41:26 +01:00
|
|
|
* spawn functions as implemented in the MS runtime library.
|
|
|
|
* Most of these based on (and copied from) newlib/libc/posix/execXX.c
|
|
|
|
*/
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-08-22 19:50:22 +02:00
|
|
|
extern "C" int
|
2000-02-17 20:38:33 +01:00
|
|
|
spawnl (int mode, const char *path, const char *arg0, ...)
|
|
|
|
{
|
2005-07-17 00:01:50 +02:00
|
|
|
int i;
|
|
|
|
va_list args;
|
|
|
|
const char *argv[256];
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-07-17 00:01:50 +02:00
|
|
|
va_start (args, arg0);
|
|
|
|
argv[0] = arg0;
|
|
|
|
i = 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-07-17 00:01:50 +02:00
|
|
|
do
|
|
|
|
argv[i] = va_arg (args, const char *);
|
|
|
|
while (argv[i++] != NULL);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-07-17 00:01:50 +02:00
|
|
|
va_end (args);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-07-17 00:01:50 +02:00
|
|
|
return spawnve (mode, path, (char * const *) argv, cur_environ ());
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2001-08-22 19:50:22 +02:00
|
|
|
extern "C" int
|
2000-02-17 20:38:33 +01:00
|
|
|
spawnle (int mode, const char *path, const char *arg0, ...)
|
|
|
|
{
|
2005-07-17 00:01:50 +02:00
|
|
|
int i;
|
|
|
|
va_list args;
|
|
|
|
const char * const *envp;
|
|
|
|
const char *argv[256];
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-07-17 00:01:50 +02:00
|
|
|
va_start (args, arg0);
|
|
|
|
argv[0] = arg0;
|
|
|
|
i = 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-07-17 00:01:50 +02:00
|
|
|
do
|
|
|
|
argv[i] = va_arg (args, const char *);
|
|
|
|
while (argv[i++] != NULL);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-07-17 00:01:50 +02:00
|
|
|
envp = va_arg (args, const char * const *);
|
|
|
|
va_end (args);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-07-17 00:01:50 +02:00
|
|
|
return spawnve (mode, path, (char * const *) argv, (char * const *) envp);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2001-08-22 19:50:22 +02:00
|
|
|
extern "C" int
|
2011-01-19 11:28:39 +01:00
|
|
|
spawnlp (int mode, const char *file, const char *arg0, ...)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2005-07-17 00:01:50 +02:00
|
|
|
int i;
|
|
|
|
va_list args;
|
|
|
|
const char *argv[256];
|
2011-01-19 11:28:39 +01:00
|
|
|
path_conv buf;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-07-17 00:01:50 +02:00
|
|
|
va_start (args, arg0);
|
|
|
|
argv[0] = arg0;
|
|
|
|
i = 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-07-17 00:01:50 +02:00
|
|
|
do
|
|
|
|
argv[i] = va_arg (args, const char *);
|
|
|
|
while (argv[i++] != NULL);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-07-17 00:01:50 +02:00
|
|
|
va_end (args);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2011-01-20 12:09:21 +01:00
|
|
|
return spawnve (mode | _P_PATH_TYPE_EXEC, find_exec (file, buf),
|
|
|
|
(char * const *) argv, cur_environ ());
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2001-08-22 19:50:22 +02:00
|
|
|
extern "C" int
|
2011-01-19 11:28:39 +01:00
|
|
|
spawnlpe (int mode, const char *file, const char *arg0, ...)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2005-07-17 00:01:50 +02:00
|
|
|
int i;
|
|
|
|
va_list args;
|
|
|
|
const char * const *envp;
|
|
|
|
const char *argv[256];
|
2011-01-19 11:28:39 +01:00
|
|
|
path_conv buf;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-07-17 00:01:50 +02:00
|
|
|
va_start (args, arg0);
|
|
|
|
argv[0] = arg0;
|
|
|
|
i = 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-07-17 00:01:50 +02:00
|
|
|
do
|
|
|
|
argv[i] = va_arg (args, const char *);
|
|
|
|
while (argv[i++] != NULL);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-07-17 00:01:50 +02:00
|
|
|
envp = va_arg (args, const char * const *);
|
|
|
|
va_end (args);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2011-01-20 12:09:21 +01:00
|
|
|
return spawnve (mode | _P_PATH_TYPE_EXEC, find_exec (file, buf),
|
|
|
|
(char * const *) argv, envp);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2001-08-22 19:50:22 +02:00
|
|
|
extern "C" int
|
2000-02-17 20:38:33 +01:00
|
|
|
spawnv (int mode, const char *path, const char * const *argv)
|
|
|
|
{
|
2005-07-17 00:01:50 +02:00
|
|
|
return spawnve (mode, path, argv, cur_environ ());
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2001-08-22 19:50:22 +02:00
|
|
|
extern "C" int
|
2011-01-19 11:28:39 +01:00
|
|
|
spawnvp (int mode, const char *file, const char * const *argv)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2011-01-19 11:28:39 +01:00
|
|
|
path_conv buf;
|
2019-10-09 22:06:02 +02:00
|
|
|
return spawnve (mode | _P_PATH_TYPE_EXEC,
|
|
|
|
find_exec (file, buf, "PATH", FE_NNF) ?: "",
|
|
|
|
argv, cur_environ ());
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2001-08-22 19:50:22 +02:00
|
|
|
extern "C" int
|
2000-02-17 20:38:33 +01:00
|
|
|
spawnvpe (int mode, const char *file, const char * const *argv,
|
2011-01-19 11:28:39 +01:00
|
|
|
const char * const *envp)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2005-07-17 00:01:50 +02:00
|
|
|
path_conv buf;
|
2019-10-09 22:06:02 +02:00
|
|
|
return spawnve (mode | _P_PATH_TYPE_EXEC,
|
|
|
|
find_exec (file, buf, "PATH", FE_NNF) ?: "",
|
|
|
|
argv, envp);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2005-07-17 02:51:03 +02:00
|
|
|
|
|
|
|
int
|
2013-06-19 18:00:43 +02:00
|
|
|
av::setup (const char *prog_arg, path_conv& real_path, const char *ext,
|
|
|
|
int argc, const char *const *argv, bool p_type_exec)
|
2005-07-17 02:51:03 +02:00
|
|
|
{
|
2006-02-20 17:15:51 +01:00
|
|
|
const char *p;
|
2008-02-01 13:37:51 +01:00
|
|
|
bool exeext = ascii_strcasematch (ext, ".exe");
|
2013-06-19 18:00:43 +02:00
|
|
|
new (this) av (argc, argv);
|
|
|
|
if ((exeext && real_path.iscygexec ()) || ascii_strcasematch (ext, ".bat")
|
|
|
|
|| (!*ext && ((p = ext - 4) > real_path.get_win32 ())
|
|
|
|
&& (ascii_strcasematch (p, ".bat") || ascii_strcasematch (p, ".cmd")
|
|
|
|
|| ascii_strcasematch (p, ".btm"))))
|
|
|
|
/* no extra checks needed */;
|
|
|
|
else
|
|
|
|
while (1)
|
2005-09-15 02:31:42 +02:00
|
|
|
{
|
2013-06-19 18:00:43 +02:00
|
|
|
char *pgm = NULL;
|
|
|
|
char *arg1 = NULL;
|
|
|
|
char *ptr, *buf;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
HANDLE h;
|
|
|
|
NTSTATUS status;
|
|
|
|
LARGE_INTEGER size;
|
|
|
|
|
|
|
|
status = NtOpenFile (&h, SYNCHRONIZE | GENERIC_READ,
|
|
|
|
real_path.get_object_attr (attr, sec_none_nih),
|
|
|
|
&io, FILE_SHARE_VALID_FLAGS,
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT
|
|
|
|
| FILE_OPEN_FOR_BACKUP_INTENT
|
|
|
|
| FILE_NON_DIRECTORY_FILE);
|
|
|
|
if (!NT_SUCCESS (status))
|
2005-09-15 02:31:42 +02:00
|
|
|
{
|
2013-06-19 18:00:43 +02:00
|
|
|
/* File is not readable? Doesn't mean it's not executable.
|
|
|
|
Test for executability and if so, just assume the file is
|
|
|
|
a cygwin executable and go ahead. */
|
|
|
|
if (status == STATUS_ACCESS_DENIED && real_path.has_acls ()
|
|
|
|
&& check_file_access (real_path, X_OK, true) == 0)
|
|
|
|
{
|
|
|
|
real_path.set_cygexec (true);
|
|
|
|
break;
|
|
|
|
}
|
2016-03-17 17:46:11 +01:00
|
|
|
SetLastError (RtlNtStatusToDosError (status));
|
2013-06-19 18:00:43 +02:00
|
|
|
goto err;
|
2005-09-15 02:31:42 +02:00
|
|
|
}
|
2013-06-19 18:00:43 +02:00
|
|
|
if (!GetFileSizeEx (h, &size))
|
2005-09-15 02:31:42 +02:00
|
|
|
{
|
2013-06-19 18:00:43 +02:00
|
|
|
NtClose (h);
|
|
|
|
goto err;
|
2005-09-15 02:31:42 +02:00
|
|
|
}
|
2013-06-19 18:00:43 +02:00
|
|
|
if (size.QuadPart > (LONGLONG) wincap.allocation_granularity ())
|
|
|
|
size.LowPart = wincap.allocation_granularity ();
|
2005-07-17 02:51:03 +02:00
|
|
|
|
2013-06-19 18:00:43 +02:00
|
|
|
HANDLE hm = CreateFileMapping (h, &sec_none_nih, PAGE_READONLY,
|
|
|
|
0, 0, NULL);
|
|
|
|
NtClose (h);
|
|
|
|
if (!hm)
|
|
|
|
{
|
|
|
|
/* ERROR_FILE_INVALID indicates very likely an empty file. */
|
|
|
|
if (GetLastError () == ERROR_FILE_INVALID)
|
|
|
|
{
|
|
|
|
debug_printf ("zero length file, treat as script.");
|
|
|
|
goto just_shell;
|
|
|
|
}
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
/* Try to map the first 64K of the image. That's enough for the local
|
|
|
|
tests, and it's enough for hook_or_detect_cygwin to compute the IAT
|
|
|
|
address. */
|
|
|
|
buf = (char *) MapViewOfFile (hm, FILE_MAP_READ, 0, 0, size.LowPart);
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
CloseHandle (hm);
|
|
|
|
goto err;
|
|
|
|
}
|
2005-07-17 02:51:03 +02:00
|
|
|
|
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
__try
|
2005-07-17 02:51:03 +02:00
|
|
|
{
|
2014-08-22 11:21:33 +02:00
|
|
|
if (buf[0] == 'M' && buf[1] == 'Z')
|
|
|
|
{
|
|
|
|
WORD subsys;
|
|
|
|
unsigned off = (unsigned char) buf[0x18] | (((unsigned char) buf[0x19]) << 8);
|
|
|
|
win16_exe = off < sizeof (IMAGE_DOS_HEADER);
|
|
|
|
if (!win16_exe)
|
|
|
|
real_path.set_cygexec (hook_or_detect_cygwin (buf, NULL,
|
|
|
|
subsys, hm));
|
|
|
|
else
|
|
|
|
real_path.set_cygexec (false);
|
|
|
|
UnmapViewOfFile (buf);
|
|
|
|
CloseHandle (hm);
|
|
|
|
break;
|
|
|
|
}
|
2005-07-17 02:51:03 +02:00
|
|
|
}
|
2014-08-22 11:21:33 +02:00
|
|
|
__except (NO_ERROR)
|
2011-01-19 15:59:10 +01:00
|
|
|
{
|
2013-06-19 18:00:43 +02:00
|
|
|
UnmapViewOfFile (buf);
|
|
|
|
CloseHandle (hm);
|
2014-08-22 11:21:33 +02:00
|
|
|
real_path.set_cygexec (false);
|
2013-06-19 18:00:43 +02:00
|
|
|
break;
|
2011-01-19 15:59:10 +01:00
|
|
|
}
|
2014-08-22 11:21:33 +02:00
|
|
|
__endtry
|
2005-07-17 02:51:03 +02:00
|
|
|
}
|
2013-06-19 18:00:43 +02:00
|
|
|
CloseHandle (hm);
|
2005-07-17 02:51:03 +02:00
|
|
|
|
2013-06-19 18:00:43 +02:00
|
|
|
debug_printf ("%s is possibly a script", real_path.get_win32 ());
|
|
|
|
|
|
|
|
ptr = buf;
|
|
|
|
if (*ptr++ == '#' && *ptr++ == '!')
|
|
|
|
{
|
|
|
|
ptr += strspn (ptr, " \t");
|
|
|
|
size_t len = strcspn (ptr, "\r\n");
|
2014-05-03 21:58:20 +02:00
|
|
|
while (ptr[len - 1] == ' ' || ptr[len - 1] == '\t')
|
|
|
|
len--;
|
2013-06-19 18:00:43 +02:00
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
char *namebuf = (char *) alloca (len + 1);
|
|
|
|
memcpy (namebuf, ptr, len);
|
|
|
|
namebuf[len] = '\0';
|
|
|
|
for (ptr = pgm = namebuf; *ptr; ptr++)
|
|
|
|
if (!arg1 && (*ptr == ' ' || *ptr == '\t'))
|
|
|
|
{
|
|
|
|
/* Null terminate the initial command and step over any
|
|
|
|
additional white space. If we've hit the end of the
|
|
|
|
line, exit the loop. Otherwise, we've found the first
|
|
|
|
argument. Position the current pointer on the last known
|
|
|
|
white space. */
|
|
|
|
*ptr = '\0';
|
|
|
|
char *newptr = ptr + 1;
|
|
|
|
newptr += strspn (newptr, " \t");
|
|
|
|
if (!*newptr)
|
|
|
|
break;
|
|
|
|
arg1 = newptr;
|
|
|
|
ptr = newptr - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UnmapViewOfFile (buf);
|
|
|
|
just_shell:
|
2019-08-06 10:46:31 +02:00
|
|
|
/* Check if script is executable. Otherwise we start non-executable
|
|
|
|
scripts successfully, which is incorrect behaviour. */
|
|
|
|
if (real_path.has_acls ()
|
|
|
|
&& check_file_access (real_path, X_OK, true) < 0)
|
|
|
|
return -1; /* errno is already set. */
|
|
|
|
|
2013-06-19 18:00:43 +02:00
|
|
|
if (!pgm)
|
|
|
|
{
|
|
|
|
if (!p_type_exec)
|
|
|
|
{
|
|
|
|
/* Not called from exec[lv]p. Don't try to treat as script. */
|
|
|
|
debug_printf ("%s is not a valid executable",
|
|
|
|
real_path.get_win32 ());
|
|
|
|
set_errno (ENOEXEC);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ascii_strcasematch (ext, ".com"))
|
|
|
|
break;
|
|
|
|
pgm = (char *) "/bin/sh";
|
|
|
|
arg1 = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace argv[0] with the full path to the script if this is the
|
|
|
|
first time through the loop. */
|
|
|
|
replace0_maybe (prog_arg);
|
|
|
|
|
|
|
|
/* pointers:
|
|
|
|
* pgm interpreter name
|
|
|
|
* arg1 optional string
|
|
|
|
*/
|
|
|
|
if (arg1)
|
|
|
|
unshift (arg1);
|
|
|
|
|
2015-02-11 14:15:59 +01:00
|
|
|
find_exec (pgm, real_path, "PATH", FE_NADA, &ext);
|
2015-02-15 09:59:55 +01:00
|
|
|
unshift (real_path.get_posix ());
|
2013-06-19 18:00:43 +02:00
|
|
|
}
|
|
|
|
if (real_path.iscygexec ())
|
|
|
|
dup_all ();
|
2005-07-17 02:51:03 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
2020-08-01 21:35:58 +02:00
|
|
|
|
|
|
|
/* The following __posix_spawn_* functions are called from newlib's posix_spawn
|
|
|
|
implementation. The original code in newlib has been taken from FreeBSD,
|
|
|
|
and the core code relies on specific, non-portable behaviour of vfork(2).
|
|
|
|
Our replacement implementation uses a semaphore to synchronize parent and
|
|
|
|
child process. Note: __posix_spawn_fork in fork.cc is part of the set. */
|
|
|
|
|
|
|
|
/* Create an inheritable semaphore. Set it to 0 (== non-signalled), so the
|
|
|
|
parent can wait on the semaphore immediately. */
|
|
|
|
extern "C" int
|
|
|
|
__posix_spawn_sem_create (void **semp)
|
|
|
|
{
|
|
|
|
HANDLE sem;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
if (!semp)
|
|
|
|
return EINVAL;
|
|
|
|
InitializeObjectAttributes (&attr, NULL, OBJ_INHERIT, NULL, NULL);
|
|
|
|
status = NtCreateSemaphore (&sem, SEMAPHORE_ALL_ACCESS, &attr, 0, INT_MAX);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
return geterrno_from_nt_status (status);
|
|
|
|
*semp = sem;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Signal the semaphore. "error" should be 0 if all went fine and the
|
|
|
|
exec'd child process is up and running, a useful POSIX error code otherwise.
|
|
|
|
After releasing the semaphore, the value of the semaphore reflects
|
|
|
|
the error code + 1. Thus, after WFMO in__posix_spawn_sem_wait_and_close,
|
|
|
|
querying the value of the semaphore returns either 0 if all went well,
|
|
|
|
or a value > 0 equivalent to the POSIX error code. */
|
|
|
|
extern "C" void
|
|
|
|
__posix_spawn_sem_release (void *sem, int error)
|
|
|
|
{
|
|
|
|
ReleaseSemaphore (sem, error + 1, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper to check the semaphore value. */
|
|
|
|
static inline int
|
|
|
|
__posix_spawn_sem_query (void *sem)
|
|
|
|
{
|
|
|
|
SEMAPHORE_BASIC_INFORMATION sbi;
|
|
|
|
|
|
|
|
NtQuerySemaphore (sem, SemaphoreBasicInformation, &sbi, sizeof sbi, NULL);
|
|
|
|
return sbi.CurrentCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called from parent to wait for fork/exec completion. We're waiting for
|
|
|
|
the semaphore as well as the child's process handle, so even if the
|
|
|
|
child crashes without signalling the semaphore, we won't wait infinitely. */
|
|
|
|
extern "C" int
|
|
|
|
__posix_spawn_sem_wait_and_close (void *sem, void *proc)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
HANDLE w4[2] = { sem, proc };
|
|
|
|
|
|
|
|
switch (WaitForMultipleObjects (2, w4, FALSE, INFINITE))
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
ret = __posix_spawn_sem_query (sem);
|
|
|
|
break;
|
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
/* If we return here due to the child process dying, the semaphore is
|
|
|
|
very likely not signalled. Check this here and return a valid error
|
|
|
|
code. */
|
|
|
|
ret = __posix_spawn_sem_query (sem);
|
|
|
|
if (ret == 0)
|
|
|
|
ret = ECHILD;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = geterrno_from_win_error ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle (sem);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replacement for execve/execvpe, called from forked child in newlib's
|
|
|
|
posix_spawn. The relevant difference is the additional semaphore
|
|
|
|
so the worker method (which is not supposed to return on success)
|
|
|
|
can signal the semaphore after sync'ing with the exec'd child. */
|
|
|
|
extern "C" int
|
|
|
|
__posix_spawn_execvpe (const char *path, char * const *argv, char *const *envp,
|
|
|
|
HANDLE sem, int use_env_path)
|
|
|
|
{
|
|
|
|
path_conv buf;
|
|
|
|
|
|
|
|
static char *const empty_env[] = { NULL };
|
|
|
|
if (!envp)
|
|
|
|
envp = empty_env;
|
|
|
|
ch_spawn.set_sem (sem);
|
|
|
|
ch_spawn.worker (use_env_path ? (find_exec (path, buf, "PATH", FE_NNF) ?: "")
|
|
|
|
: path,
|
|
|
|
argv, envp,
|
|
|
|
_P_OVERLAY | (use_env_path ? _P_PATH_TYPE_EXEC : 0));
|
|
|
|
__posix_spawn_sem_release (sem, errno);
|
|
|
|
return -1;
|
|
|
|
}
|