2000-02-18 03:38:33 +08:00
|
|
|
/* spawn.cc
|
|
|
|
|
2005-02-20 12:25:33 +08:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
|
2009-01-03 13:12:22 +08:00
|
|
|
2005, 2006, 2007, 2008, 2009 Red Hat, Inc.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
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-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <process.h>
|
|
|
|
#include <sys/wait.h>
|
2000-07-28 01:30:51 +08:00
|
|
|
#include <wingdi.h>
|
|
|
|
#include <winuser.h>
|
2008-03-07 19:24:51 +08:00
|
|
|
#include <wchar.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <ctype.h>
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "cygerrno.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#include <sys/cygwin.h>
|
2001-07-27 03:22:24 +08:00
|
|
|
#include "security.h"
|
2003-09-20 08:43:33 +08: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 08:37:18 +08:00
|
|
|
#include "fhandler.h"
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "dtable.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "sigproc.h"
|
2000-09-07 05:03:10 +08:00
|
|
|
#include "cygheap.h"
|
2000-09-02 04:54:22 +08:00
|
|
|
#include "child_info.h"
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "pinfo.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#include "environ.h"
|
2005-07-17 08:51:03 +08:00
|
|
|
#include "cygtls.h"
|
2008-03-07 19:24:51 +08:00
|
|
|
#include "tls_pbuf.h"
|
2006-04-12 23:53:22 +08:00
|
|
|
#include "winf.h"
|
2007-07-19 16:36:32 +08:00
|
|
|
#include "ntdll.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2006-02-01 05:49:39 +08:00
|
|
|
static suffix_info exe_suffixes[] =
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2006-02-04 05:33:09 +08:00
|
|
|
suffix_info ("", 1),
|
|
|
|
suffix_info (".exe", 1),
|
2006-02-01 05:49:39 +08:00
|
|
|
suffix_info (".com"),
|
|
|
|
suffix_info (NULL)
|
|
|
|
};
|
|
|
|
|
2009-11-05 17:59:18 +08:00
|
|
|
#if 0
|
|
|
|
/* CV, 2009-11-05: Used to be used when searching for DLLs in calls to
|
|
|
|
dlopen(). However, dlopen() on other platforms never adds a suffix by
|
|
|
|
its own. Therefore we use stat_suffixes now, which only adds a .exe
|
|
|
|
suffix for symmetry. */
|
2006-02-01 05:49:39 +08:00
|
|
|
static suffix_info dll_suffixes[] =
|
|
|
|
{
|
|
|
|
suffix_info (".dll"),
|
2006-02-04 05:33:09 +08:00
|
|
|
suffix_info ("", 1),
|
|
|
|
suffix_info (".exe", 1),
|
2000-02-18 03:38:33 +08:00
|
|
|
suffix_info (NULL)
|
|
|
|
};
|
2009-11-05 17:59:18 +08:00
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2006-05-22 12:50:54 +08:00
|
|
|
child_info_spawn *chExeced;
|
2000-10-24 10:25:27 +08:00
|
|
|
|
2000-02-18 03:38:33 +08: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-02-01 05:49:39 +08:00
|
|
|
perhaps_suffix (const char *prog, path_conv& buf, int& err, unsigned opt)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2009-08-02 03:52:46 +08:00
|
|
|
const char *ext;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-08-25 02:26:14 +08:00
|
|
|
err = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
debug_printf ("prog '%s'", prog);
|
2006-02-01 05:49:39 +08:00
|
|
|
buf.check (prog, PC_SYM_FOLLOW | PC_NULLEMPTY,
|
2009-11-05 17:59:18 +08:00
|
|
|
(opt & FE_DLL) ? stat_suffixes : exe_suffixes);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-08-25 02:26:14 +08:00
|
|
|
if (buf.isdir ())
|
|
|
|
{
|
|
|
|
err = EACCES;
|
|
|
|
ext = NULL;
|
|
|
|
}
|
|
|
|
else if (!buf.exists ())
|
|
|
|
{
|
|
|
|
err = ENOENT;
|
|
|
|
ext = NULL;
|
|
|
|
}
|
2000-04-26 13:13:32 +08:00
|
|
|
else if (buf.known_suffix)
|
2007-08-16 23:07:42 +08:00
|
|
|
ext = buf.get_win32 () + (buf.known_suffix - buf.get_win32 ());
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
2007-08-16 23:07:42 +08:00
|
|
|
ext = strchr (buf.get_win32 (), '\0');
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2007-08-16 23:07:42 +08:00
|
|
|
debug_printf ("buf %s, suffix found '%s'", (char *) buf.get_win32 (), ext);
|
2000-02-18 03:38:33 +08:00
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find an executable name, possibly by appending known executable
|
|
|
|
suffixes to it. The win32-translated name is placed in 'buf'.
|
|
|
|
Any found suffix is returned in known_suffix.
|
|
|
|
|
|
|
|
If the file is not found and !null_if_not_found then the win32 version
|
|
|
|
of name is placed in buf and returned. Otherwise the contents of buf
|
|
|
|
is undefined and NULL is returned. */
|
|
|
|
|
|
|
|
const char * __stdcall
|
2000-04-26 13:13:32 +08:00
|
|
|
find_exec (const char *name, path_conv& buf, const char *mywinenv,
|
2001-10-31 08:55:32 +08:00
|
|
|
unsigned opt, const char **known_suffix)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
const char *suffix = "";
|
|
|
|
debug_printf ("find_exec (%s)", name);
|
2009-08-01 11:27:51 +08:00
|
|
|
const char *retval;
|
2008-03-11 21:26:40 +08:00
|
|
|
tmp_pathbuf tp;
|
|
|
|
char *tmp = tp.c_get ();
|
2001-10-31 08:55:32 +08:00
|
|
|
const char *posix = (opt & FE_NATIVE) ? NULL : name;
|
|
|
|
bool has_slash = strchr (name, '/');
|
2005-08-25 02:26:14 +08:00
|
|
|
int err;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Check to see if file can be opened as is first.
|
|
|
|
Win32 systems always check . first, but PATH may not be set up to
|
|
|
|
do this. */
|
2001-10-31 08:55:32 +08:00
|
|
|
if ((has_slash || opt & FE_CWD)
|
2006-02-01 05:49:39 +08:00
|
|
|
&& (suffix = perhaps_suffix (name, buf, err, opt)) != NULL)
|
2001-10-31 08:55:32 +08:00
|
|
|
{
|
|
|
|
if (posix && !has_slash)
|
|
|
|
{
|
|
|
|
tmp[0] = '.';
|
|
|
|
tmp[1] = '/';
|
|
|
|
strcpy (tmp + 2, name);
|
|
|
|
posix = tmp;
|
|
|
|
}
|
2009-08-01 11:27:51 +08:00
|
|
|
retval = buf.get_win32 ();
|
2001-10-31 08:55:32 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
win_env *winpath;
|
|
|
|
const char *path;
|
2001-10-31 08:55:32 +08:00
|
|
|
const char *posix_path;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-05-22 11:54:29 +08:00
|
|
|
posix = (opt & FE_NATIVE) ? NULL : tmp;
|
|
|
|
|
|
|
|
if (strchr (mywinenv, '/'))
|
|
|
|
{
|
|
|
|
/* it's not really an environment variable at all */
|
2008-03-12 20:41:50 +08:00
|
|
|
int n = cygwin_conv_path_list (CCP_POSIX_TO_WIN_A, mywinenv, NULL, 0);
|
|
|
|
char *s = (char *) alloca (n);
|
|
|
|
if (cygwin_conv_path_list (CCP_POSIX_TO_WIN_A, mywinenv, s, n))
|
2005-05-22 11:54:29 +08:00
|
|
|
goto errout;
|
|
|
|
path = s;
|
|
|
|
posix_path = mywinenv - 1;
|
|
|
|
}
|
|
|
|
else if (has_slash || strchr (name, '\\') || isdrive (name)
|
2004-06-03 05:20:54 +08:00
|
|
|
|| !(winpath = getwinenv (mywinenv))
|
|
|
|
|| !(path = winpath->get_native ()) || *path == '\0')
|
2005-05-22 11:54:29 +08:00
|
|
|
/* Return the error condition if this is an absolute path or if there
|
|
|
|
is no PATH to search. */
|
2000-02-18 03:38:33 +08:00
|
|
|
goto errout;
|
2005-05-22 11:54:29 +08:00
|
|
|
else
|
|
|
|
posix_path = winpath->get_posix () - 1;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
debug_printf ("%s%s", mywinenv, path);
|
2006-02-16 06:11:13 +08:00
|
|
|
/* Iterate over the specified path, looking for the file with and without
|
|
|
|
executable extensions. */
|
2000-02-18 03:38:33 +08:00
|
|
|
do
|
|
|
|
{
|
2001-10-31 08:55:32 +08:00
|
|
|
posix_path++;
|
2000-02-18 03:38:33 +08:00
|
|
|
char *eotmp = strccpy (tmp, &path, ';');
|
|
|
|
/* An empty path or '.' means the current directory, but we've
|
|
|
|
already tried that. */
|
2001-10-31 08:55:32 +08:00
|
|
|
if (opt & FE_CWD && (tmp[0] == '\0' || (tmp[0] == '.' && tmp[1] == '\0')))
|
2000-02-18 03:38:33 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
*eotmp++ = '\\';
|
|
|
|
strcpy (eotmp, name);
|
|
|
|
|
|
|
|
debug_printf ("trying %s", tmp);
|
|
|
|
|
2006-02-01 05:49:39 +08:00
|
|
|
if ((suffix = perhaps_suffix (tmp, buf, err, opt)) != NULL)
|
2001-10-31 08:55:32 +08:00
|
|
|
{
|
2009-09-25 21:44:45 +08:00
|
|
|
if (buf.has_acls () && check_file_access (buf, X_OK, true))
|
2006-02-01 05:09:43 +08:00
|
|
|
continue;
|
|
|
|
|
2001-10-31 08:55:32 +08:00
|
|
|
if (posix == tmp)
|
|
|
|
{
|
|
|
|
eotmp = strccpy (tmp, &posix_path, ':');
|
|
|
|
if (eotmp == tmp)
|
|
|
|
*eotmp++ = '.';
|
|
|
|
*eotmp++ = '/';
|
|
|
|
strcpy (eotmp, name);
|
|
|
|
}
|
2009-08-01 11:36:44 +08:00
|
|
|
retval = buf.get_win32 ();
|
2001-10-31 08:55:32 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2001-10-31 08:55:32 +08:00
|
|
|
while (*path && *++path && (posix_path = strchr (posix_path, ':')));
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-08-23 01:50:22 +08:00
|
|
|
errout:
|
2001-10-31 08:55:32 +08:00
|
|
|
posix = NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Couldn't find anything in the given path.
|
|
|
|
Take the appropriate action based on null_if_not_found. */
|
2001-10-31 08:55:32 +08:00
|
|
|
if (opt & FE_NNF)
|
2000-02-18 03:38:33 +08:00
|
|
|
retval = NULL;
|
2009-08-01 11:27:51 +08:00
|
|
|
else if (!(opt & FE_NATIVE))
|
2002-03-22 11:24:30 +08:00
|
|
|
retval = name;
|
2009-08-01 11:27:51 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
buf.check (name);
|
|
|
|
retval = buf.get_win32 ();
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-08-23 01:50:22 +08:00
|
|
|
out:
|
2001-10-31 08:55:32 +08:00
|
|
|
if (posix)
|
2009-08-02 01:55:58 +08:00
|
|
|
retval = buf.set_path (posix);
|
2007-08-16 23:07:42 +08:00
|
|
|
debug_printf ("%s = find_exec (%s)", (char *) buf.get_win32 (), name);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (known_suffix)
|
2007-08-16 23:07:42 +08:00
|
|
|
*known_suffix = suffix ?: strchr (buf.get_win32 (), '\0');
|
2005-08-25 02:26:14 +08:00
|
|
|
if (!retval && err)
|
|
|
|
set_errno (err);
|
2000-02-18 03:38:33 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Utility for spawn_guts. */
|
|
|
|
|
|
|
|
static HANDLE
|
2006-12-12 02:55:29 +08:00
|
|
|
handle (int fd, bool writing)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-12-19 12:34:13 +08: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-12 02:55:29 +08:00
|
|
|
else if (!writing)
|
2005-12-19 12:34:13 +08:00
|
|
|
h = cfd->get_handle ();
|
|
|
|
else
|
|
|
|
h = cfd->get_output_handle ();
|
2006-12-12 02:55:29 +08:00
|
|
|
|
2005-12-19 12:34:13 +08:00
|
|
|
return h;
|
2000-02-18 03:38:33 +08: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-02 01:30:35 +08:00
|
|
|
return n >= 0 && strcasematch (argv0 + n, what) &&
|
2000-02-18 03:38:33 +08:00
|
|
|
(n == 0 || isdirsep (argv0[n - 1]));
|
|
|
|
}
|
|
|
|
|
2003-09-21 03:51:48 +08:00
|
|
|
struct pthread_cleanup
|
|
|
|
{
|
|
|
|
_sig_func_ptr oldint;
|
|
|
|
_sig_func_ptr oldquit;
|
|
|
|
sigset_t oldmask;
|
2004-10-08 00:49:30 +08:00
|
|
|
pthread_cleanup (): oldint (NULL), oldquit (NULL), oldmask ((sigset_t) -1) {}
|
2003-09-21 03:51:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
do_cleanup (void *args)
|
|
|
|
{
|
|
|
|
# define cleanup ((pthread_cleanup *) args)
|
2004-10-08 00:49:30 +08:00
|
|
|
if (cleanup->oldmask != (sigset_t) -1)
|
2005-03-02 23:32:34 +08:00
|
|
|
{
|
|
|
|
signal (SIGINT, cleanup->oldint);
|
|
|
|
signal (SIGQUIT, cleanup->oldquit);
|
|
|
|
sigprocmask (SIG_SETMASK, &(cleanup->oldmask), NULL);
|
|
|
|
}
|
2003-09-21 03:51:48 +08:00
|
|
|
# undef cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-03 13:12:22 +08:00
|
|
|
int
|
2008-03-27 09:50:40 +08:00
|
|
|
spawn_guts (const char *prog_arg, const char *const *argv,
|
2006-12-12 02:55:29 +08:00
|
|
|
const char *const envp[], int mode, int __stdin, int __stdout)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-12-08 06:37:12 +08:00
|
|
|
bool rc;
|
2000-07-30 00:24:59 +08:00
|
|
|
pid_t cygpid;
|
2005-08-12 00:13:30 +08:00
|
|
|
int res = -1;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (prog_arg == NULL)
|
|
|
|
{
|
|
|
|
syscall_printf ("prog_arg is NULL");
|
2009-12-21 23:16:28 +08: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-18 03:38:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-05-19 09:43:31 +08:00
|
|
|
syscall_printf ("spawn_guts (%d, %.9500s)", mode, prog_arg);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2009-12-21 23:16:28 +08:00
|
|
|
/* FIXME: This is no error condition on Linux. */
|
2000-02-18 03:38:33 +08:00
|
|
|
if (argv == NULL)
|
|
|
|
{
|
|
|
|
syscall_printf ("argv is NULL");
|
2001-03-19 04:58:18 +08:00
|
|
|
set_errno (EINVAL);
|
2002-10-09 12:08:05 +08:00
|
|
|
return -1;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2005-08-12 00:13:30 +08:00
|
|
|
/* FIXME: There is a small race here and FIXME: not thread safe! */
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2005-08-12 00:13:30 +08:00
|
|
|
pthread_cleanup cleanup;
|
|
|
|
if (mode == _P_SYSTEM)
|
|
|
|
{
|
|
|
|
sigset_t child_block;
|
|
|
|
cleanup.oldint = signal (SIGINT, SIG_IGN);
|
|
|
|
cleanup.oldquit = signal (SIGQUIT, SIG_IGN);
|
|
|
|
sigemptyset (&child_block);
|
|
|
|
sigaddset (&child_block, SIGCHLD);
|
|
|
|
sigprocmask (SIG_BLOCK, &child_block, &cleanup.oldmask);
|
|
|
|
}
|
|
|
|
pthread_cleanup_push (do_cleanup, (void *) &cleanup);
|
|
|
|
av newargv;
|
2000-09-03 12:16:35 +08:00
|
|
|
linebuf one_line;
|
2006-12-12 02:55:29 +08:00
|
|
|
child_info_spawn ch;
|
2008-03-07 19:24:51 +08:00
|
|
|
PWCHAR envblock = NULL;
|
2005-08-12 00:13:30 +08:00
|
|
|
path_conv real_path;
|
|
|
|
bool reset_sendsig = false;
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2008-03-07 19:24:51 +08:00
|
|
|
tmp_pathbuf tp;
|
|
|
|
PWCHAR runpath = tp.w_get ();
|
2006-07-18 03:30:30 +08:00
|
|
|
int c_flags;
|
|
|
|
bool wascygexec;
|
|
|
|
cygheap_exec_info *moreinfo;
|
|
|
|
|
2005-08-12 00:13:30 +08:00
|
|
|
bool null_app_name = false;
|
2009-01-09 13:18:02 +08:00
|
|
|
STARTUPINFOW si = {};
|
2006-03-19 03:17:21 +08:00
|
|
|
int looped = 0;
|
2006-03-21 09:37:25 +08:00
|
|
|
HANDLE orig_wr_proc_pipe = NULL;
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2005-08-12 00:13:30 +08:00
|
|
|
myfault efault;
|
2005-09-15 07:03:44 +08:00
|
|
|
if (efault.faulted ())
|
2005-08-12 00:13:30 +08:00
|
|
|
{
|
2005-09-15 07:03:44 +08:00
|
|
|
if (get_errno () == ENOMEM)
|
|
|
|
set_errno (E2BIG);
|
|
|
|
else
|
|
|
|
set_errno (EFAULT);
|
2005-08-12 00:13:30 +08:00
|
|
|
res = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2004-12-06 03:41:26 +08:00
|
|
|
child_info_types chtype;
|
2001-10-04 10:34:20 +08:00
|
|
|
if (mode != _P_OVERLAY)
|
2000-09-03 12:16:35 +08:00
|
|
|
chtype = PROC_SPAWN;
|
|
|
|
else
|
2000-10-14 13:52:38 +08:00
|
|
|
chtype = PROC_EXEC;
|
|
|
|
|
2008-03-07 19:24:51 +08:00
|
|
|
moreinfo = (cygheap_exec_info *) ccalloc_abort (HEAP_1_EXEC, 1,
|
|
|
|
sizeof (cygheap_exec_info));
|
2005-06-01 11:46:56 +08:00
|
|
|
moreinfo->old_title = NULL;
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2000-02-18 03:38:33 +08: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-09-03 12:16:35 +08:00
|
|
|
int ac;
|
|
|
|
for (ac = 0; argv[ac]; ac++)
|
2000-02-18 03:38:33 +08:00
|
|
|
/* nothing */;
|
|
|
|
|
2005-08-12 00:13:30 +08:00
|
|
|
newargv.set (ac, argv);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2006-01-10 10:39:22 +08:00
|
|
|
int err;
|
|
|
|
const char *ext;
|
2006-02-01 05:49:39 +08:00
|
|
|
if ((ext = perhaps_suffix (prog_arg, real_path, err, FE_NADA)) == NULL)
|
2006-01-10 10:39:22 +08:00
|
|
|
{
|
|
|
|
set_errno (err);
|
|
|
|
res = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2006-07-18 03:30:30 +08:00
|
|
|
|
|
|
|
wascygexec = real_path.iscygexec ();
|
2006-02-01 05:23:57 +08:00
|
|
|
res = newargv.fixup (prog_arg, real_path, ext);
|
2008-11-27 01:21:04 +08:00
|
|
|
|
2009-06-22 23:40:59 +08:00
|
|
|
if (res)
|
|
|
|
goto out;
|
|
|
|
|
2008-03-11 20:34:08 +08:00
|
|
|
if (!real_path.iscygexec ()
|
|
|
|
&& (cygheap->cwd.drive_length == 0
|
|
|
|
|| cygheap->cwd.win32.Length >= MAX_PATH * sizeof (WCHAR)))
|
|
|
|
{
|
|
|
|
small_printf ("Error: Current working directory is a %s.\n"
|
|
|
|
"Can't start native Windows application from here.\n\n",
|
|
|
|
cygheap->cwd.drive_length == 0
|
|
|
|
? "virtual Cygwin directory"
|
|
|
|
: "path longer than allowed for a\n"
|
|
|
|
"Win32 working directory");
|
|
|
|
set_errno (ENAMETOOLONG);
|
|
|
|
res = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2006-01-10 10:39:22 +08:00
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
if (ac == 3 && argv[1][0] == '/' && argv[1][1] == 'c' &&
|
2000-02-18 03:38:33 +08:00
|
|
|
(iscmd (argv[0], "command.com") || iscmd (argv[0], "cmd.exe")))
|
|
|
|
{
|
2000-10-20 12:20:21 +08:00
|
|
|
real_path.check (prog_arg);
|
2000-10-21 12:53:49 +08:00
|
|
|
one_line.add ("\"");
|
2000-10-20 12:20:21 +08:00
|
|
|
if (!real_path.error)
|
2007-08-16 23:07:42 +08:00
|
|
|
one_line.add (real_path.get_win32 ());
|
2000-10-20 12:20:21 +08:00
|
|
|
else
|
|
|
|
one_line.add (argv[0]);
|
2000-10-21 12:53:49 +08:00
|
|
|
one_line.add ("\"");
|
2000-02-18 03:38:33 +08:00
|
|
|
one_line.add (" ");
|
|
|
|
one_line.add (argv[1]);
|
|
|
|
one_line.add (" ");
|
|
|
|
one_line.add (argv[2]);
|
2009-08-02 01:55:58 +08:00
|
|
|
real_path.set_path (argv[0]);
|
2005-08-12 00:13:30 +08:00
|
|
|
null_app_name = true;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
else
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2006-04-13 09:37:00 +08:00
|
|
|
if (wascygexec)
|
2006-01-10 10:39:22 +08:00
|
|
|
newargv.dup_all ();
|
2008-03-07 19:24:51 +08:00
|
|
|
else if (!one_line.fromargv (newargv, real_path.get_win32 (),
|
|
|
|
real_path.iscygexec ()))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2006-04-12 23:53:22 +08:00
|
|
|
res = -1;
|
|
|
|
goto out;
|
2003-09-26 16:45:24 +08:00
|
|
|
}
|
2000-09-14 03:57:00 +08:00
|
|
|
|
2006-04-12 23:53:22 +08:00
|
|
|
|
2006-01-10 10:39:22 +08:00
|
|
|
newargv.all_calloced ();
|
|
|
|
moreinfo->argc = newargv.argc;
|
|
|
|
moreinfo->argv = newargv;
|
|
|
|
|
|
|
|
if (mode != _P_OVERLAY ||
|
2009-12-19 04:32:04 +08:00
|
|
|
!DuplicateHandle (GetCurrentProcess (), myself.shared_handle (),
|
|
|
|
GetCurrentProcess (), &moreinfo->myself_pinfo,
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS))
|
2006-01-10 10:39:22 +08:00
|
|
|
moreinfo->myself_pinfo = NULL;
|
|
|
|
else
|
|
|
|
VerifyHandle (moreinfo->myself_pinfo);
|
|
|
|
}
|
2008-03-07 19:24:51 +08:00
|
|
|
WCHAR wone_line[one_line.ix + 1];
|
2009-04-05 12:23:50 +08:00
|
|
|
if (one_line.ix)
|
|
|
|
sys_mbstowcs (wone_line, one_line.ix + 1, one_line.buf);
|
|
|
|
else
|
|
|
|
wone_line[0] = L'\0';
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-08-12 00:13:30 +08:00
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
pi.hProcess = pi.hThread = NULL;
|
|
|
|
pi.dwProcessId = pi.dwThreadId = 0;
|
2006-12-12 02:55:29 +08:00
|
|
|
|
|
|
|
/* Set up needed handles for stdio */
|
2000-02-18 03:38:33 +08:00
|
|
|
si.dwFlags = STARTF_USESTDHANDLES;
|
2006-12-12 02:55:29 +08:00
|
|
|
si.hStdInput = handle ((__stdin < 0 ? 0 : __stdin), false);
|
|
|
|
si.hStdOutput = handle ((__stdout < 0 ? 1 : __stdout), true);
|
|
|
|
si.hStdError = handle (2, true);
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
si.cb = sizeof (si);
|
|
|
|
|
2009-12-19 04:32:04 +08:00
|
|
|
c_flags = GetPriorityClass (GetCurrentProcess ());
|
2006-03-27 11:52:24 +08:00
|
|
|
sigproc_printf ("priority class %d", c_flags);
|
2008-03-07 19:24:51 +08:00
|
|
|
c_flags |= CREATE_SEPARATE_WOW_VDM | CREATE_UNICODE_ENVIRONMENT;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-12-19 12:34:13 +08:00
|
|
|
if (mode == _P_DETACH)
|
2006-03-27 11:52:24 +08:00
|
|
|
c_flags |= DETACHED_PROCESS;
|
2008-06-17 23:38:17 +08:00
|
|
|
else
|
|
|
|
fhandler_console::need_invisible ();
|
2004-11-26 12:15:10 +08:00
|
|
|
|
2000-10-17 07:55:58 +08:00
|
|
|
if (mode != _P_OVERLAY)
|
2004-12-06 03:41:26 +08:00
|
|
|
myself->exec_sendsig = NULL;
|
2004-09-12 11:47:57 +08:00
|
|
|
else
|
2004-11-26 12:15:10 +08: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. */
|
2004-12-06 03:41:26 +08:00
|
|
|
if (!myself->exec_sendsig)
|
|
|
|
{
|
|
|
|
myself->exec_sendsig = myself->sendsig;
|
|
|
|
myself->exec_dwProcessId = myself->dwProcessId;
|
|
|
|
myself->sendsig = NULL;
|
|
|
|
reset_sendsig = true;
|
|
|
|
}
|
2004-11-26 12:15:10 +08:00
|
|
|
/* Save a copy of a handle to the current process around the first time we
|
|
|
|
exec so that the pid will not be reused. Why did I stop cygwin from
|
|
|
|
generating its own pids again? */
|
|
|
|
if (cygheap->pid_handle)
|
|
|
|
/* already done previously */;
|
2009-12-19 04:32:04 +08:00
|
|
|
else if (DuplicateHandle (GetCurrentProcess (), GetCurrentProcess (),
|
|
|
|
GetCurrentProcess (), &cygheap->pid_handle,
|
|
|
|
PROCESS_QUERY_INFORMATION, TRUE, 0))
|
2006-05-21 13:25:49 +08:00
|
|
|
ProtectHandleINH (cygheap->pid_handle);
|
2004-11-26 12:15:10 +08:00
|
|
|
else
|
|
|
|
system_printf ("duplicate to pid_handle failed, %E");
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2008-03-07 19:24:51 +08:00
|
|
|
runpath = null_app_name ? NULL : real_path.get_wide_win32_path (runpath);
|
2008-07-23 00:59:59 +08:00
|
|
|
{ /* If the executable path length is < MAX_PATH, make sure the long path
|
|
|
|
win32 prefix is removed from the path to make subsequent native Win32
|
|
|
|
child processes happy which are not long path aware. */
|
|
|
|
USHORT len = real_path.get_nt_native_path ()->Length;
|
|
|
|
if (len < (MAX_PATH + 4) * sizeof (WCHAR)
|
|
|
|
|| (runpath[5] != L':' /* UNC path */
|
|
|
|
&& len < (MAX_PATH + 6) * sizeof (WCHAR)))
|
|
|
|
{
|
2008-11-27 01:21:04 +08:00
|
|
|
PWCHAR r = runpath + 4;
|
2008-07-23 00:59:59 +08:00
|
|
|
if (r[1] != L':') /* UNC path */
|
2008-07-23 18:17:04 +08:00
|
|
|
*(r += 2) = L'\\';
|
2008-07-31 22:41:19 +08:00
|
|
|
if (!RtlIsDosDeviceName_U (r))
|
|
|
|
runpath = r;
|
|
|
|
else if (*r == L'\\')
|
|
|
|
*r = L'C';
|
2008-07-23 00:59:59 +08:00
|
|
|
}
|
|
|
|
}
|
2000-10-20 12:20:21 +08:00
|
|
|
|
2008-03-07 19:24:51 +08:00
|
|
|
syscall_printf ("null_app_name %d (%W, %.9500W)", null_app_name,
|
|
|
|
runpath, wone_line);
|
2000-10-20 12:20:21 +08:00
|
|
|
|
|
|
|
cygbench ("spawn-guts");
|
2002-06-15 02:01:21 +08:00
|
|
|
|
2007-12-14 19:32:40 +08:00
|
|
|
if (!real_path.iscygexec())
|
|
|
|
cygheap->fdtab.set_file_pointers_for_exec ();
|
2004-09-12 11:47:57 +08:00
|
|
|
|
2008-03-07 19:24:51 +08:00
|
|
|
moreinfo->envp = build_env (envp, envblock, moreinfo->envc,
|
|
|
|
real_path.iscygexec ());
|
2005-09-17 03:58:12 +08:00
|
|
|
if (!moreinfo->envp || !envblock)
|
|
|
|
{
|
|
|
|
set_errno (E2BIG);
|
2005-09-17 04:12:12 +08:00
|
|
|
res = -1;
|
2005-09-17 03:58:12 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2006-04-14 00:11:06 +08:00
|
|
|
ch.set (chtype, real_path.iscygexec ());
|
2005-12-30 04:46:34 +08:00
|
|
|
ch.moreinfo = moreinfo;
|
2006-12-12 02:55:29 +08:00
|
|
|
ch.__stdin = __stdin;
|
|
|
|
ch.__stdout = __stdout;
|
2005-06-01 11:46:56 +08:00
|
|
|
|
2005-12-30 04:46:34 +08:00
|
|
|
si.lpReserved2 = (LPBYTE) &ch;
|
|
|
|
si.cbReserved2 = sizeof (ch);
|
2005-06-01 11:46:56 +08:00
|
|
|
|
2009-02-24 10:11:14 +08:00
|
|
|
/* Depends on ch.set call above.
|
2008-04-27 02:01:42 +08:00
|
|
|
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
|
2009-11-17 18:43:01 +08:00
|
|
|
&& (!ch.iscygwin () || mode != _P_OVERLAY
|
|
|
|
|| cygheap->fdtab.need_fixup_before ()))
|
2008-04-27 02:01:42 +08:00
|
|
|
c_flags |= CREATE_SUSPENDED;
|
|
|
|
|
2003-06-09 21:29:12 +08:00
|
|
|
/* 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.
|
2003-09-16 17:24:52 +08:00
|
|
|
FIXME: If ruid != euid and ruid != saved_uid we currently give
|
2003-06-09 21:29:12 +08:00
|
|
|
up on ruid. The new process will have ruid == euid. */
|
2006-03-19 03:17:21 +08:00
|
|
|
loop:
|
|
|
|
cygheap->user.deimpersonate ();
|
|
|
|
|
2003-06-09 21:29:12 +08:00
|
|
|
if (!cygheap->user.issetuid ()
|
2003-09-16 17:24:52 +08:00
|
|
|
|| (cygheap->user.saved_uid == cygheap->user.real_uid
|
|
|
|
&& cygheap->user.saved_gid == cygheap->user.real_gid
|
2009-10-13 18:23:31 +08:00
|
|
|
&& !cygheap->user.groups.issetgroups ()
|
|
|
|
&& !cygheap->user.setuid_to_restricted))
|
2000-10-17 07:55:58 +08:00
|
|
|
{
|
2008-03-07 19:24:51 +08:00
|
|
|
rc = CreateProcessW (runpath, /* image name - with full path */
|
|
|
|
wone_line, /* what was passed to exec */
|
|
|
|
&sec_none_nih, /* process security attrs */
|
|
|
|
&sec_none_nih, /* thread security attrs */
|
|
|
|
TRUE, /* inherit handles from parent */
|
|
|
|
c_flags,
|
|
|
|
envblock, /* environment */
|
|
|
|
NULL,
|
|
|
|
&si,
|
|
|
|
&pi);
|
2000-10-17 07:55:58 +08:00
|
|
|
}
|
|
|
|
else
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-09-27 09:58:23 +08:00
|
|
|
/* Give access to myself */
|
|
|
|
if (mode == _P_OVERLAY)
|
|
|
|
myself.set_acl();
|
2002-06-15 02:01:21 +08:00
|
|
|
|
2008-03-07 19:24:51 +08:00
|
|
|
WCHAR wstname[1024] = { L'\0' };
|
2007-08-18 03:58:57 +08:00
|
|
|
HWINSTA hwst_orig = NULL, hwst = NULL;
|
|
|
|
HDESK hdsk_orig = NULL, hdsk = NULL;
|
|
|
|
PSECURITY_ATTRIBUTES sa;
|
2000-02-18 03:38:33 +08:00
|
|
|
DWORD n;
|
2007-08-18 03:58:57 +08:00
|
|
|
|
|
|
|
hwst_orig = GetProcessWindowStation ();
|
|
|
|
hdsk_orig = GetThreadDesktop (GetCurrentThreadId ());
|
2008-03-07 19:24:51 +08:00
|
|
|
GetUserObjectInformationW (hwst_orig, UOI_NAME, wstname, 1024, &n);
|
2007-08-18 03:58:57 +08:00
|
|
|
/* Prior to Vista it was possible to start a service with the
|
|
|
|
"Interact with desktop" flag. This started the service in the
|
|
|
|
interactive window station of the console. A big security
|
|
|
|
risk, but we don't want to disable this behaviour for older
|
|
|
|
OSes because it's still heavily used by some users. They have
|
|
|
|
been warned. */
|
2009-10-13 18:23:31 +08:00
|
|
|
if (!cygheap->user.setuid_to_restricted
|
|
|
|
&& wcscasecmp (wstname, L"WinSta0") != 0)
|
2007-08-18 03:58:57 +08:00
|
|
|
{
|
2008-03-07 19:24:51 +08:00
|
|
|
WCHAR sid[128];
|
2007-08-18 03:58:57 +08:00
|
|
|
|
|
|
|
sa = sec_user ((PSECURITY_ATTRIBUTES) alloca (1024),
|
|
|
|
cygheap->user.sid ());
|
2007-08-18 04:22:24 +08:00
|
|
|
/* We're creating a window station per user, not per logon session.
|
2007-08-18 03:58:57 +08:00
|
|
|
First of all we might not have a valid logon session for
|
|
|
|
the user (logon by create_token), and second, it doesn't
|
|
|
|
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. */
|
2008-03-07 19:24:51 +08:00
|
|
|
hwst = CreateWindowStationW (cygheap->user.get_windows_id (sid), 0,
|
2007-08-18 03:58:57 +08:00
|
|
|
GENERIC_READ | GENERIC_WRITE, sa);
|
|
|
|
if (!hwst)
|
|
|
|
system_printf ("CreateWindowStation failed, %E");
|
|
|
|
else if (!SetProcessWindowStation (hwst))
|
|
|
|
system_printf ("SetProcessWindowStation failed, %E");
|
2008-03-07 19:24:51 +08:00
|
|
|
else if (!(hdsk = CreateDesktopW (L"Default", NULL, NULL, 0,
|
2007-08-18 03:58:57 +08:00
|
|
|
GENERIC_ALL, sa)))
|
|
|
|
system_printf ("CreateDesktop failed, %E");
|
|
|
|
else
|
|
|
|
{
|
2008-03-07 19:24:51 +08:00
|
|
|
wcpcpy (wcpcpy (wstname, sid), L"\\Default");
|
2007-08-18 03:58:57 +08:00
|
|
|
si.lpDesktop = wstname;
|
2008-03-07 19:24:51 +08:00
|
|
|
debug_printf ("Desktop: %W", si.lpDesktop);
|
2007-08-18 03:58:57 +08:00
|
|
|
}
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2008-03-07 19:24:51 +08:00
|
|
|
rc = CreateProcessAsUserW (cygheap->user.primary_token (),
|
|
|
|
runpath, /* image name - with full path */
|
|
|
|
wone_line, /* what was passed to exec */
|
|
|
|
&sec_none_nih, /* process security attrs */
|
|
|
|
&sec_none_nih, /* thread security attrs */
|
|
|
|
TRUE, /* inherit handles from parent */
|
|
|
|
c_flags,
|
|
|
|
envblock, /* environment */
|
|
|
|
NULL,
|
|
|
|
&si,
|
|
|
|
&pi);
|
2007-08-18 03:58:57 +08:00
|
|
|
if (hwst)
|
|
|
|
{
|
|
|
|
SetProcessWindowStation (hwst_orig);
|
|
|
|
CloseWindowStation (hwst);
|
|
|
|
}
|
|
|
|
if (hdsk)
|
|
|
|
{
|
|
|
|
SetThreadDesktop (hdsk_orig);
|
|
|
|
CloseDesktop (hdsk);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2003-09-20 08:31:13 +08:00
|
|
|
|
2003-06-09 21:29:12 +08:00
|
|
|
/* Restore impersonation. In case of _P_OVERLAY this isn't
|
|
|
|
allowed since it would overwrite child data. */
|
2003-09-26 10:23:34 +08:00
|
|
|
if (mode != _P_OVERLAY || !rc)
|
2004-11-26 12:15:10 +08:00
|
|
|
cygheap->user.reimpersonate ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Set errno now so that debugging messages from it appear before our
|
|
|
|
final debugging message [this is a general rule for debugging
|
|
|
|
messages]. */
|
2000-08-03 11:02:41 +08:00
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
syscall_printf ("CreateProcess failed, %E");
|
2004-11-26 12:15:10 +08:00
|
|
|
/* If this was a failed exec, restore the saved sendsig. */
|
2004-12-06 03:41:26 +08:00
|
|
|
if (reset_sendsig)
|
|
|
|
{
|
|
|
|
myself->sendsig = myself->exec_sendsig;
|
|
|
|
myself->exec_sendsig = NULL;
|
|
|
|
}
|
2005-10-24 23:17:54 +08:00
|
|
|
res = -1;
|
|
|
|
goto out;
|
2000-08-03 11:02:41 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2006-03-27 11:52:24 +08:00
|
|
|
if (!(c_flags & CREATE_SUSPENDED))
|
2005-12-30 04:46:34 +08:00
|
|
|
strace.write_childpid (ch, pi.dwProcessId);
|
|
|
|
|
2009-11-17 18:43:01 +08: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);
|
|
|
|
|
2002-10-14 02:16:33 +08:00
|
|
|
if (mode != _P_OVERLAY)
|
2000-07-30 00:24:59 +08:00
|
|
|
cygpid = cygwin_pid (pi.dwProcessId);
|
2002-10-14 02:16:33 +08:00
|
|
|
else
|
|
|
|
cygpid = myself->pid;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* We print the original program name here so the user can see that too. */
|
2003-05-19 09:43:31 +08:00
|
|
|
syscall_printf ("%d = spawn_guts (%s, %.9500s)",
|
2000-10-09 10:53:44 +08:00
|
|
|
rc ? cygpid : (unsigned int) -1, prog_arg, one_line.buf);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
/* Name the handle similarly to proc_subproc. */
|
2004-12-06 08:29:41 +08:00
|
|
|
ProtectHandle1 (pi.hProcess, childhProc);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-01-11 23:31:04 +08:00
|
|
|
bool synced;
|
2005-07-17 08:51:03 +08:00
|
|
|
pid_t pid;
|
2000-02-18 03:38:33 +08:00
|
|
|
if (mode == _P_OVERLAY)
|
|
|
|
{
|
2006-05-22 12:50:54 +08:00
|
|
|
chExeced = &ch; /* FIXME: there's a race here if a user sneaks in CTRL-C */
|
|
|
|
myself->dwProcessId = pi.dwProcessId;
|
2000-10-24 10:25:27 +08:00
|
|
|
strace.execing = 1;
|
2004-12-06 03:41:26 +08:00
|
|
|
myself.hProcess = hExeced = pi.hProcess;
|
2007-08-16 23:07:42 +08:00
|
|
|
strcpy (myself->progname, real_path.get_win32 ()); // FIXME: race?
|
2004-12-06 03:41:26 +08:00
|
|
|
sigproc_printf ("new process name %s", myself->progname);
|
|
|
|
/* If wr_proc_pipe doesn't exist then this process was not started by a cygwin
|
2004-12-25 02:31:23 +08:00
|
|
|
process. So, we need to wait around until the process we've just "execed"
|
|
|
|
dies. Use our own wait facility to wait for our own pid to exit (there
|
|
|
|
is some minor special case code in proc_waiter and friends to accommodate
|
|
|
|
this).
|
|
|
|
|
|
|
|
If wr_proc_pipe exists, then it should be duplicated to the child.
|
|
|
|
If the child has exited already, that's ok. The parent will pick up
|
2005-02-11 23:24:15 +08:00
|
|
|
on this fact when we exit. dup_proc_pipe will close our end of the pipe.
|
2004-12-25 02:31:23 +08:00
|
|
|
Note that wr_proc_pipe may also be == INVALID_HANDLE_VALUE. That will make
|
|
|
|
dup_proc_pipe essentially a no-op. */
|
2005-07-17 08:51:03 +08:00
|
|
|
if (!newargv.win16_exe && myself->wr_proc_pipe)
|
2004-12-25 02:31:23 +08:00
|
|
|
{
|
2006-03-19 03:17:21 +08:00
|
|
|
if (!looped)
|
2007-03-28 22:34:24 +08:00
|
|
|
myself->sync_proc_pipe (); /* Make sure that we own wr_proc_pipe
|
|
|
|
just in case we've been previously
|
|
|
|
execed. */
|
2006-03-21 09:37:25 +08:00
|
|
|
orig_wr_proc_pipe = myself->dup_proc_pipe (pi.hProcess);
|
2004-12-25 02:31:23 +08:00
|
|
|
}
|
2005-07-17 08:51:03 +08:00
|
|
|
pid = myself->pid;
|
2006-03-27 11:52:24 +08:00
|
|
|
if (!ch.iscygwin ())
|
|
|
|
close_all_files ();
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-10-25 11:54:50 +08:00
|
|
|
myself->set_has_pgid_children ();
|
2000-10-17 07:55:58 +08:00
|
|
|
ProtectHandle (pi.hThread);
|
2003-09-26 10:23:34 +08:00
|
|
|
pinfo child (cygpid, PID_IN_USE);
|
2000-07-30 00:24:59 +08:00
|
|
|
if (!child)
|
|
|
|
{
|
2003-09-26 10:23:34 +08:00
|
|
|
syscall_printf ("pinfo failed");
|
2003-09-26 11:20:30 +08:00
|
|
|
if (get_errno () != ENOMEM)
|
|
|
|
set_errno (EAGAIN);
|
2003-09-26 10:23:34 +08:00
|
|
|
res = -1;
|
|
|
|
goto out;
|
2000-07-30 00:24:59 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
child->dwProcessId = pi.dwProcessId;
|
2004-11-26 12:15:10 +08:00
|
|
|
child.hProcess = pi.hProcess;
|
2003-09-26 10:23:34 +08:00
|
|
|
|
2007-08-16 23:07:42 +08:00
|
|
|
strcpy (child->progname, real_path.get_win32 ());
|
2001-10-04 10:34:20 +08:00
|
|
|
/* FIXME: This introduces an unreferenced, open handle into the child.
|
2001-11-05 14:09:15 +08:00
|
|
|
The purpose is to keep the pid shared memory open so that all of
|
2001-10-04 10:34:20 +08:00
|
|
|
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.
|
|
|
|
However, we should try to find another way to do this eventually. */
|
2009-12-19 04:32:04 +08:00
|
|
|
DuplicateHandle (GetCurrentProcess (), child.shared_handle (),
|
|
|
|
pi.hProcess, NULL, 0, 0, DUPLICATE_SAME_ACCESS);
|
2004-12-06 03:41:26 +08:00
|
|
|
child->start_time = time (NULL); /* Register child's starting time. */
|
2005-01-26 06:45:11 +08:00
|
|
|
child->nice = myself->nice;
|
2004-12-23 22:57:08 +08:00
|
|
|
if (!child.remember (mode == _P_DETACH))
|
|
|
|
{
|
|
|
|
/* FIXME: Child in strange state now */
|
|
|
|
CloseHandle (pi.hProcess);
|
|
|
|
ForceCloseHandle (pi.hThread);
|
|
|
|
res = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2005-07-17 08:51:03 +08:00
|
|
|
pid = child->pid;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2005-01-11 23:31:04 +08:00
|
|
|
/* Start the child running */
|
2006-03-27 11:52:24 +08:00
|
|
|
if (c_flags & CREATE_SUSPENDED)
|
2005-12-30 04:46:34 +08:00
|
|
|
{
|
|
|
|
ResumeThread (pi.hThread);
|
|
|
|
strace.write_childpid (ch, pi.dwProcessId);
|
|
|
|
}
|
2005-01-11 23:31:04 +08:00
|
|
|
ForceCloseHandle (pi.hThread);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-01-11 23:31:04 +08:00
|
|
|
sigproc_printf ("spawned windows pid %d", pi.dwProcessId);
|
2000-10-17 07:55:58 +08:00
|
|
|
|
2007-01-02 03:37:19 +08:00
|
|
|
if ((mode == _P_DETACH || mode == _P_NOWAIT) && !ch.iscygwin ())
|
|
|
|
synced = false;
|
|
|
|
else
|
|
|
|
synced = ch.sync (pi.dwProcessId, pi.hProcess, INFINITE);
|
2001-06-27 05:03:08 +08:00
|
|
|
|
2005-01-11 23:31:04 +08:00
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case _P_OVERLAY:
|
2005-10-18 23:13:13 +08:00
|
|
|
myself.hProcess = pi.hProcess;
|
2006-03-19 03:17:21 +08:00
|
|
|
if (!synced)
|
|
|
|
{
|
2006-03-21 09:37:25 +08:00
|
|
|
if (orig_wr_proc_pipe)
|
|
|
|
{
|
|
|
|
myself->wr_proc_pipe_owner = GetCurrentProcessId ();
|
|
|
|
myself->wr_proc_pipe = orig_wr_proc_pipe;
|
|
|
|
}
|
2008-03-27 09:50:40 +08:00
|
|
|
if (!ch.proc_retry (pi.hProcess))
|
2006-03-19 03:17:21 +08:00
|
|
|
{
|
|
|
|
looped++;
|
|
|
|
goto loop;
|
|
|
|
}
|
2006-03-22 11:20:28 +08:00
|
|
|
close_all_files (true);
|
2006-03-19 03:17:21 +08:00
|
|
|
}
|
2006-03-22 11:20:28 +08:00
|
|
|
else
|
2005-01-11 23:31:04 +08:00
|
|
|
{
|
2006-03-22 11:20:28 +08:00
|
|
|
close_all_files (true);
|
|
|
|
if (!myself->wr_proc_pipe
|
|
|
|
&& WaitForSingleObject (pi.hProcess, 0) == WAIT_TIMEOUT)
|
|
|
|
{
|
|
|
|
extern bool is_toplevel_proc;
|
|
|
|
is_toplevel_proc = true;
|
|
|
|
myself.remember (false);
|
|
|
|
waitpid (myself->pid, &res, 0);
|
|
|
|
}
|
2005-01-11 23:31:04 +08:00
|
|
|
}
|
2005-01-17 01:00:27 +08:00
|
|
|
myself.exit (EXITCODE_NOSET);
|
2005-01-11 23:31:04 +08:00
|
|
|
break;
|
|
|
|
case _P_WAIT:
|
|
|
|
case _P_SYSTEM:
|
|
|
|
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;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2003-09-26 10:23:34 +08:00
|
|
|
out:
|
2006-03-19 03:17:21 +08:00
|
|
|
if (envblock)
|
|
|
|
free (envblock);
|
2005-01-11 23:31:04 +08:00
|
|
|
pthread_cleanup_pop (1);
|
|
|
|
return (int) res;
|
2006-12-05 18:59:21 +08:00
|
|
|
#undef ch
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-08-23 01:50:22 +08:00
|
|
|
extern "C" int
|
2000-02-18 03:38:33 +08:00
|
|
|
cwait (int *result, int pid, int)
|
|
|
|
{
|
2005-07-17 06:01:50 +08:00
|
|
|
return waitpid (pid, result, 0);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-12-06 03:41:26 +08:00
|
|
|
* Helper function for spawn runtime calls.
|
|
|
|
* Doesn't search the path.
|
|
|
|
*/
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
extern "C" int
|
2002-06-11 10:08:00 +08:00
|
|
|
spawnve (int mode, const char *path, const char *const *argv,
|
2004-12-06 03:41:26 +08:00
|
|
|
const char *const *envp)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-07-17 06:01:50 +08:00
|
|
|
int ret;
|
2004-01-24 07:05:33 +08:00
|
|
|
#ifdef NEWVFORK
|
2005-07-17 06:01:50 +08:00
|
|
|
vfork_save *vf = vfork_storage.val ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
if (vf != NULL && (vf->pid < 0) && mode == _P_OVERLAY)
|
|
|
|
mode = _P_NOWAIT;
|
|
|
|
else
|
|
|
|
vf = NULL;
|
2004-01-24 07:05:33 +08:00
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
syscall_printf ("spawnve (%s, %s, %x)", path, argv[0], envp);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case _P_OVERLAY:
|
|
|
|
/* We do not pass _P_SEARCH_PATH here. execve doesn't search PATH.*/
|
|
|
|
/* Just act as an exec if _P_OVERLAY set. */
|
|
|
|
spawn_guts (path, argv, envp, mode);
|
|
|
|
/* Errno should be set by spawn_guts. */
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
case _P_VFORK:
|
|
|
|
case _P_NOWAIT:
|
|
|
|
case _P_NOWAITO:
|
|
|
|
case _P_WAIT:
|
|
|
|
case _P_DETACH:
|
|
|
|
case _P_SYSTEM:
|
|
|
|
ret = spawn_guts (path, argv, envp, mode);
|
2004-01-24 07:05:33 +08:00
|
|
|
#ifdef NEWVFORK
|
2005-07-17 06:01:50 +08:00
|
|
|
if (vf)
|
|
|
|
{
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
debug_printf ("longjmping due to vfork");
|
|
|
|
vf->restore_pid (ret);
|
|
|
|
}
|
|
|
|
}
|
2004-01-24 07:05:33 +08:00
|
|
|
#endif
|
2005-07-17 06:01:50 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_errno (EINVAL);
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-12-06 03:41:26 +08: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-18 03:38:33 +08:00
|
|
|
|
2001-08-23 01:50:22 +08:00
|
|
|
extern "C" int
|
2000-02-18 03:38:33 +08:00
|
|
|
spawnl (int mode, const char *path, const char *arg0, ...)
|
|
|
|
{
|
2005-07-17 06:01:50 +08:00
|
|
|
int i;
|
|
|
|
va_list args;
|
|
|
|
const char *argv[256];
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
va_start (args, arg0);
|
|
|
|
argv[0] = arg0;
|
|
|
|
i = 1;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
do
|
|
|
|
argv[i] = va_arg (args, const char *);
|
|
|
|
while (argv[i++] != NULL);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
va_end (args);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
return spawnve (mode, path, (char * const *) argv, cur_environ ());
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-08-23 01:50:22 +08:00
|
|
|
extern "C" int
|
2000-02-18 03:38:33 +08:00
|
|
|
spawnle (int mode, const char *path, const char *arg0, ...)
|
|
|
|
{
|
2005-07-17 06:01:50 +08:00
|
|
|
int i;
|
|
|
|
va_list args;
|
|
|
|
const char * const *envp;
|
|
|
|
const char *argv[256];
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
va_start (args, arg0);
|
|
|
|
argv[0] = arg0;
|
|
|
|
i = 1;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
do
|
|
|
|
argv[i] = va_arg (args, const char *);
|
|
|
|
while (argv[i++] != NULL);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
envp = va_arg (args, const char * const *);
|
|
|
|
va_end (args);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
return spawnve (mode, path, (char * const *) argv, (char * const *) envp);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-08-23 01:50:22 +08:00
|
|
|
extern "C" int
|
2000-02-18 03:38:33 +08:00
|
|
|
spawnlp (int mode, const char *path, const char *arg0, ...)
|
|
|
|
{
|
2005-07-17 06:01:50 +08:00
|
|
|
int i;
|
|
|
|
va_list args;
|
|
|
|
const char *argv[256];
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
va_start (args, arg0);
|
|
|
|
argv[0] = arg0;
|
|
|
|
i = 1;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
do
|
|
|
|
argv[i] = va_arg (args, const char *);
|
|
|
|
while (argv[i++] != NULL);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
va_end (args);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
return spawnvpe (mode, path, (char * const *) argv, cur_environ ());
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-08-23 01:50:22 +08:00
|
|
|
extern "C" int
|
2000-02-18 03:38:33 +08:00
|
|
|
spawnlpe (int mode, const char *path, const char *arg0, ...)
|
|
|
|
{
|
2005-07-17 06:01:50 +08:00
|
|
|
int i;
|
|
|
|
va_list args;
|
|
|
|
const char * const *envp;
|
|
|
|
const char *argv[256];
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
va_start (args, arg0);
|
|
|
|
argv[0] = arg0;
|
|
|
|
i = 1;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
do
|
|
|
|
argv[i] = va_arg (args, const char *);
|
|
|
|
while (argv[i++] != NULL);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
envp = va_arg (args, const char * const *);
|
|
|
|
va_end (args);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-17 06:01:50 +08:00
|
|
|
return spawnvpe (mode, path, (char * const *) argv, envp);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-08-23 01:50:22 +08:00
|
|
|
extern "C" int
|
2000-02-18 03:38:33 +08:00
|
|
|
spawnv (int mode, const char *path, const char * const *argv)
|
|
|
|
{
|
2005-07-17 06:01:50 +08:00
|
|
|
return spawnve (mode, path, argv, cur_environ ());
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-08-23 01:50:22 +08:00
|
|
|
extern "C" int
|
2000-02-18 03:38:33 +08:00
|
|
|
spawnvp (int mode, const char *path, const char * const *argv)
|
|
|
|
{
|
2005-07-17 06:01:50 +08:00
|
|
|
return spawnvpe (mode, path, argv, cur_environ ());
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-08-23 01:50:22 +08:00
|
|
|
extern "C" int
|
2000-02-18 03:38:33 +08:00
|
|
|
spawnvpe (int mode, const char *file, const char * const *argv,
|
2004-12-06 03:41:26 +08:00
|
|
|
const char * const *envp)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-07-17 06:01:50 +08:00
|
|
|
path_conv buf;
|
|
|
|
return spawnve (mode, find_exec (file, buf), argv, envp);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2005-07-17 08:51:03 +08:00
|
|
|
|
|
|
|
int
|
2006-02-01 05:23:57 +08:00
|
|
|
av::fixup (const char *prog_arg, path_conv& real_path, const char *ext)
|
2005-07-17 08:51:03 +08:00
|
|
|
{
|
2006-02-21 00:15:51 +08:00
|
|
|
const char *p;
|
2008-02-01 20:37:51 +08:00
|
|
|
bool exeext = ascii_strcasematch (ext, ".exe");
|
2008-09-11 12:34:24 +08:00
|
|
|
if ((exeext && real_path.iscygexec ()) || ascii_strcasematch (ext, ".bat"))
|
2006-02-21 00:15:51 +08:00
|
|
|
return 0;
|
2007-08-16 23:07:42 +08:00
|
|
|
if (!*ext && ((p = ext - 4) > real_path.get_win32 ())
|
2008-02-01 20:37:51 +08:00
|
|
|
&& (ascii_strcasematch (p, ".bat") || ascii_strcasematch (p, ".cmd")
|
|
|
|
|| ascii_strcasematch (p, ".btm")))
|
2005-09-08 08:57:12 +08:00
|
|
|
return 0;
|
2005-09-07 11:10:17 +08:00
|
|
|
while (1)
|
2005-07-17 08:51:03 +08:00
|
|
|
{
|
2006-02-01 05:09:43 +08:00
|
|
|
char *pgm = NULL;
|
|
|
|
char *arg1 = NULL;
|
2006-07-18 03:30:30 +08:00
|
|
|
char *ptr, *buf;
|
2008-03-07 19:24:51 +08:00
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
HANDLE h;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
status = NtOpenFile (&h, SYNCHRONIZE | GENERIC_READ,
|
|
|
|
real_path.get_object_attr (attr, sec_none_nih),
|
|
|
|
&io, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT
|
|
|
|
| FILE_OPEN_FOR_BACKUP_INTENT
|
|
|
|
| FILE_NON_DIRECTORY_FILE);
|
|
|
|
if (!NT_SUCCESS (status))
|
2009-10-19 16:33:00 +08:00
|
|
|
{
|
|
|
|
/* File is not readable? Doesn't mean it's not executable.
|
|
|
|
Test for executablility 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;
|
|
|
|
}
|
|
|
|
goto err;
|
|
|
|
}
|
2005-07-17 08:51:03 +08:00
|
|
|
|
|
|
|
HANDLE hm = CreateFileMapping (h, &sec_none_nih, PAGE_READONLY, 0, 0, NULL);
|
2008-03-07 19:24:51 +08:00
|
|
|
NtClose (h);
|
2005-07-17 08:51:03 +08:00
|
|
|
if (!hm)
|
2006-05-28 23:50:14 +08:00
|
|
|
{
|
2006-02-01 05:09:43 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
2006-07-18 03:30:30 +08:00
|
|
|
buf = (char *) MapViewOfFile(hm, FILE_MAP_READ, 0, 0, 0);
|
2005-07-17 08:51:03 +08:00
|
|
|
CloseHandle (hm);
|
|
|
|
if (!buf)
|
|
|
|
goto err;
|
|
|
|
|
2005-09-15 08:31:42 +08:00
|
|
|
{
|
|
|
|
myfault efault;
|
|
|
|
if (efault.faulted ())
|
|
|
|
{
|
|
|
|
UnmapViewOfFile (buf);
|
|
|
|
real_path.set_cygexec (false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (buf[0] == 'M' && buf[1] == 'Z')
|
|
|
|
{
|
2005-12-22 01:14:34 +08:00
|
|
|
WORD subsys;
|
2005-09-15 08:31:42 +08:00
|
|
|
unsigned off = (unsigned char) buf[0x18] | (((unsigned char) buf[0x19]) << 8);
|
|
|
|
win16_exe = off < sizeof (IMAGE_DOS_HEADER);
|
|
|
|
if (!win16_exe)
|
2005-12-22 01:14:34 +08:00
|
|
|
real_path.set_cygexec (!!hook_or_detect_cygwin (buf, NULL, subsys));
|
2006-01-10 10:39:22 +08:00
|
|
|
else
|
|
|
|
real_path.set_cygexec (false);
|
2005-09-15 08:31:42 +08:00
|
|
|
UnmapViewOfFile (buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-07-17 08:51:03 +08:00
|
|
|
|
2007-08-16 23:07:42 +08:00
|
|
|
debug_printf ("%s is possibly a script", real_path.get_win32 ());
|
2005-07-17 08:51:03 +08:00
|
|
|
|
2006-07-18 03:30:30 +08:00
|
|
|
ptr = buf;
|
2005-07-17 08:51:03 +08:00
|
|
|
if (*ptr++ == '#' && *ptr++ == '!')
|
|
|
|
{
|
|
|
|
ptr += strspn (ptr, " \t");
|
|
|
|
size_t len = strcspn (ptr, "\r\n");
|
|
|
|
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);
|
2006-02-01 05:09:43 +08:00
|
|
|
just_shell:
|
2005-07-17 08:51:03 +08:00
|
|
|
if (!pgm)
|
|
|
|
{
|
2008-02-01 20:37:51 +08:00
|
|
|
if (ascii_strcasematch (ext, ".com"))
|
2005-09-08 08:57:12 +08:00
|
|
|
break;
|
2005-07-17 08:51:03 +08:00
|
|
|
pgm = (char *) "/bin/sh";
|
|
|
|
arg1 = NULL;
|
|
|
|
}
|
|
|
|
|
2009-06-22 23:40:59 +08:00
|
|
|
/* Check if script is executable. Otherwise we start non-executable
|
|
|
|
scripts successfully, which is incorrect behaviour. */
|
2009-09-25 21:44:45 +08:00
|
|
|
if (real_path.has_acls ()
|
|
|
|
&& check_file_access (real_path, X_OK, true) < 0)
|
2009-06-22 23:40:59 +08:00
|
|
|
return -1; /* errno is already set. */
|
|
|
|
|
2005-07-17 08:51:03 +08:00
|
|
|
/* 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);
|
|
|
|
|
|
|
|
/* FIXME: This should not be using FE_NATIVE. It should be putting
|
|
|
|
the posix path on the argv list. */
|
|
|
|
find_exec (pgm, real_path, "PATH=", FE_NATIVE, &ext);
|
2007-08-16 23:07:42 +08:00
|
|
|
unshift (real_path.get_win32 (), 1);
|
2005-07-17 08:51:03 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|