2000-02-17 20:38:33 +01:00
|
|
|
/* spawn.cc
|
|
|
|
|
2004-02-09 05:04:24 +01:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Red Hat, Inc.
|
2000-02-17 20:38:33 +01: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-02 18:28:18 +02:00
|
|
|
#include "winsup.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <process.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <limits.h>
|
2000-07-27 19:30:51 +02:00
|
|
|
#include <wingdi.h>
|
|
|
|
#include <winuser.h>
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <ctype.h>
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "cygerrno.h"
|
2000-09-08 04:56:55 +02:00
|
|
|
#include <sys/cygwin.h>
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "security.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-08-22 07:10:20 +02:00
|
|
|
#include "sigproc.h"
|
2000-09-06 23:03:10 +02:00
|
|
|
#include "cygheap.h"
|
2000-09-01 22:54:22 +02:00
|
|
|
#include "child_info.h"
|
2001-01-28 06:51:15 +01:00
|
|
|
#include "shared_info.h"
|
2000-08-12 07:35:42 +02:00
|
|
|
#include "pinfo.h"
|
2000-09-08 04:56:55 +02:00
|
|
|
#define NEED_VFORK
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "perthread.h"
|
2000-09-08 04:56:55 +02:00
|
|
|
#include "registry.h"
|
|
|
|
#include "environ.h"
|
2002-10-13 20:16:33 +02:00
|
|
|
#include "cygthread.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-11-15 00:40:06 +01:00
|
|
|
#define LINE_BUF_CHUNK (CYG_MAX_PATH * 2)
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-24 04:25:27 +02:00
|
|
|
static suffix_info std_suffixes[] =
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
suffix_info (".exe", 1), suffix_info ("", 1),
|
|
|
|
suffix_info (".com"), suffix_info (".cmd"),
|
|
|
|
suffix_info (".bat"), suffix_info (".dll"),
|
|
|
|
suffix_info (NULL)
|
|
|
|
};
|
|
|
|
|
2000-10-24 04:25:27 +02:00
|
|
|
HANDLE hExeced;
|
2002-01-10 04:21:27 +01:00
|
|
|
DWORD dwExeced;
|
2000-10-24 04:25:27 +02:00
|
|
|
|
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 *
|
2002-01-10 04:21:27 +01:00
|
|
|
perhaps_suffix (const char *prog, path_conv& buf)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
char *ext;
|
|
|
|
|
|
|
|
debug_printf ("prog '%s'", prog);
|
2000-07-17 21:18:21 +02:00
|
|
|
buf.check (prog, PC_SYM_FOLLOW | PC_FULL, std_suffixes);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-10-05 03:39:08 +02:00
|
|
|
if (!buf.exists () || buf.isdir ())
|
2000-02-17 20:38:33 +01:00
|
|
|
ext = NULL;
|
2000-04-26 07:13:32 +02:00
|
|
|
else if (buf.known_suffix)
|
2001-10-01 06:10:07 +02:00
|
|
|
ext = (char *) buf + (buf.known_suffix - buf.get_win32 ());
|
2000-02-17 20:38:33 +01:00
|
|
|
else
|
|
|
|
ext = strchr (buf, '\0');
|
|
|
|
|
2000-04-26 07:13:32 +02:00
|
|
|
debug_printf ("buf %s, suffix found '%s'", (char *) buf, ext);
|
2000-02-17 20:38:33 +01: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 07:13:32 +02:00
|
|
|
find_exec (const char *name, path_conv& buf, const char *mywinenv,
|
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 = "";
|
|
|
|
debug_printf ("find_exec (%s)", name);
|
2002-03-22 04:24:30 +01:00
|
|
|
const char *retval = buf;
|
2003-11-15 00:40:06 +01:00
|
|
|
char tmp[CYG_MAX_PATH];
|
2001-10-31 01:55:32 +01:00
|
|
|
const char *posix = (opt & FE_NATIVE) ? NULL : name;
|
|
|
|
bool has_slash = strchr (name, '/');
|
2000-02-17 20:38:33 +01: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 01:55:32 +01:00
|
|
|
if ((has_slash || opt & FE_CWD)
|
|
|
|
&& (suffix = perhaps_suffix (name, buf)) != NULL)
|
|
|
|
{
|
|
|
|
if (posix && !has_slash)
|
|
|
|
{
|
|
|
|
tmp[0] = '.';
|
|
|
|
tmp[1] = '/';
|
|
|
|
strcpy (tmp + 2, name);
|
|
|
|
posix = tmp;
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
win_env *winpath;
|
|
|
|
const char *path;
|
2001-10-31 01:55:32 +01:00
|
|
|
const char *posix_path;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Return the error condition if this is an absolute path or if there
|
|
|
|
is no PATH to search. */
|
2004-06-02 23:20:54 +02:00
|
|
|
if (has_slash || strchr (name, '\\') || isdrive (name)
|
|
|
|
|| !(winpath = getwinenv (mywinenv))
|
|
|
|
|| !(path = winpath->get_native ()) || *path == '\0')
|
2000-02-17 20:38:33 +01:00
|
|
|
goto errout;
|
|
|
|
|
|
|
|
debug_printf ("%s%s", mywinenv, path);
|
|
|
|
|
2001-10-31 01:55:32 +01:00
|
|
|
posix = (opt & FE_NATIVE) ? NULL : tmp;
|
|
|
|
posix_path = winpath->get_posix () - 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Iterate over the specified path, looking for the file with and
|
|
|
|
without executable extensions. */
|
|
|
|
do
|
|
|
|
{
|
2001-10-31 01:55:32 +01:00
|
|
|
posix_path++;
|
2000-02-17 20:38:33 +01:00
|
|
|
char *eotmp = strccpy (tmp, &path, ';');
|
|
|
|
/* An empty path or '.' means the current directory, but we've
|
|
|
|
already tried that. */
|
2001-10-31 01:55:32 +01:00
|
|
|
if (opt & FE_CWD && (tmp[0] == '\0' || (tmp[0] == '.' && tmp[1] == '\0')))
|
2000-02-17 20:38:33 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
*eotmp++ = '\\';
|
|
|
|
strcpy (eotmp, name);
|
|
|
|
|
|
|
|
debug_printf ("trying %s", tmp);
|
|
|
|
|
|
|
|
if ((suffix = perhaps_suffix (tmp, buf)) != NULL)
|
2001-10-31 01:55:32 +01:00
|
|
|
{
|
|
|
|
if (posix == tmp)
|
|
|
|
{
|
|
|
|
eotmp = strccpy (tmp, &posix_path, ':');
|
|
|
|
if (eotmp == tmp)
|
|
|
|
*eotmp++ = '.';
|
|
|
|
*eotmp++ = '/';
|
|
|
|
strcpy (eotmp, name);
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2001-10-31 01:55:32 +01:00
|
|
|
while (*path && *++path && (posix_path = strchr (posix_path, ':')));
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-08-22 19:50:22 +02:00
|
|
|
errout:
|
2001-10-31 01:55:32 +01:00
|
|
|
posix = NULL;
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Couldn't find anything in the given path.
|
|
|
|
Take the appropriate action based on null_if_not_found. */
|
2001-10-31 01:55:32 +01:00
|
|
|
if (opt & FE_NNF)
|
2000-02-17 20:38:33 +01:00
|
|
|
retval = NULL;
|
2001-10-31 01:55:32 +01:00
|
|
|
else if (opt & FE_NATIVE)
|
2000-04-26 07:13:32 +02:00
|
|
|
buf.check (name);
|
2002-03-22 04:24:30 +01:00
|
|
|
else
|
|
|
|
retval = name;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-08-22 19:50:22 +02:00
|
|
|
out:
|
2001-10-31 01:55:32 +01:00
|
|
|
if (posix)
|
|
|
|
buf.set_path (posix);
|
2000-04-26 07:13:32 +02:00
|
|
|
debug_printf ("%s = find_exec (%s)", (char *) buf, name);
|
2000-02-17 20:38:33 +01:00
|
|
|
if (known_suffix)
|
|
|
|
*known_suffix = suffix ?: strchr (buf, '\0');
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Utility for spawn_guts. */
|
|
|
|
|
|
|
|
static HANDLE
|
|
|
|
handle (int n, int direction)
|
|
|
|
{
|
2001-04-18 23:10:15 +02:00
|
|
|
fhandler_base *fh = cygheap->fdtab[n];
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
if (!fh)
|
|
|
|
return INVALID_HANDLE_VALUE;
|
2004-04-10 15:45:10 +02:00
|
|
|
if (fh->close_on_exec ())
|
2000-02-17 20:38:33 +01:00
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
if (direction == 0)
|
|
|
|
return fh->get_handle ();
|
|
|
|
return fh->get_output_handle ();
|
|
|
|
}
|
|
|
|
|
|
|
|
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]));
|
|
|
|
}
|
|
|
|
|
|
|
|
class linebuf
|
|
|
|
{
|
2001-08-22 19:50:22 +02:00
|
|
|
public:
|
2000-02-17 20:38:33 +01:00
|
|
|
size_t ix;
|
|
|
|
char *buf;
|
|
|
|
size_t alloced;
|
2000-10-20 06:20:21 +02:00
|
|
|
linebuf () : ix (0), buf (NULL), alloced (0) {}
|
2003-02-13 03:52:41 +01:00
|
|
|
~linebuf () {if (buf) free (buf);}
|
2000-02-17 20:38:33 +01:00
|
|
|
void add (const char *what, int len);
|
|
|
|
void add (const char *what) {add (what, strlen (what));}
|
|
|
|
void prepend (const char *what, int len);
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
linebuf::add (const char *what, int len)
|
|
|
|
{
|
|
|
|
size_t newix;
|
|
|
|
if ((newix = ix + len) >= alloced || !buf)
|
|
|
|
{
|
|
|
|
alloced += LINE_BUF_CHUNK + newix;
|
|
|
|
buf = (char *) realloc (buf, alloced + 1);
|
|
|
|
}
|
|
|
|
memcpy (buf + ix, what, len);
|
|
|
|
ix = newix;
|
|
|
|
buf[ix] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
linebuf::prepend (const char *what, int len)
|
|
|
|
{
|
|
|
|
int buflen;
|
|
|
|
size_t newix;
|
|
|
|
if ((newix = ix + len) >= alloced)
|
|
|
|
{
|
|
|
|
alloced += LINE_BUF_CHUNK + newix;
|
|
|
|
buf = (char *) realloc (buf, alloced + 1);
|
|
|
|
buf[ix] = '\0';
|
|
|
|
}
|
|
|
|
if ((buflen = strlen (buf)))
|
|
|
|
memmove (buf + len, buf, buflen + 1);
|
|
|
|
else
|
|
|
|
buf[newix] = '\0';
|
|
|
|
memcpy (buf, what, len);
|
|
|
|
ix = newix;
|
|
|
|
}
|
|
|
|
|
2000-09-13 21:57:00 +02:00
|
|
|
class av
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
|
|
|
char **argv;
|
2000-09-13 21:57:00 +02:00
|
|
|
int calloced;
|
2001-08-22 19:50:22 +02:00
|
|
|
public:
|
2002-10-09 06:08:05 +02:00
|
|
|
int error;
|
2000-09-13 21:57:00 +02:00
|
|
|
int argc;
|
2002-10-09 06:08:05 +02:00
|
|
|
av (int ac, const char * const *av) : calloced (0), error (false), argc (ac)
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
2000-10-14 07:52:38 +02:00
|
|
|
argv = (char **) cmalloc (HEAP_1_ARGV, (argc + 5) * sizeof (char *));
|
2000-09-03 06:16:35 +02:00
|
|
|
memcpy (argv, av, (argc + 1) * sizeof (char *));
|
|
|
|
}
|
|
|
|
~av ()
|
|
|
|
{
|
2002-10-09 06:08:05 +02:00
|
|
|
if (argv)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < calloced; i++)
|
|
|
|
if (argv[i])
|
|
|
|
cfree (argv[i]);
|
|
|
|
cfree (argv);
|
|
|
|
}
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
|
|
|
int unshift (const char *what, int conv = 0);
|
|
|
|
operator char **() {return argv;}
|
2000-09-13 21:57:00 +02:00
|
|
|
void all_calloced () {calloced = argc;}
|
|
|
|
void replace0_maybe (const char *arg0)
|
|
|
|
{
|
|
|
|
/* Note: Assumes that argv array has not yet been "unshifted" */
|
2002-10-09 06:08:05 +02:00
|
|
|
if (!calloced
|
|
|
|
&& (argv[0] = cstrdup1 (arg0)))
|
|
|
|
calloced = true;
|
|
|
|
else
|
|
|
|
error = errno;
|
2000-09-13 21:57:00 +02:00
|
|
|
}
|
2000-09-25 18:36:12 +02:00
|
|
|
void dup_maybe (int i)
|
2000-09-13 21:57:00 +02:00
|
|
|
{
|
2002-10-09 06:08:05 +02:00
|
|
|
if (i >= calloced
|
|
|
|
&& !(argv[i] = cstrdup1 (argv[i])))
|
|
|
|
error = errno;
|
2000-09-13 21:57:00 +02:00
|
|
|
}
|
|
|
|
void dup_all ()
|
|
|
|
{
|
|
|
|
for (int i = calloced; i < argc; i++)
|
2002-10-09 06:08:05 +02:00
|
|
|
if (!(argv[i] = cstrdup1 (argv[i])))
|
|
|
|
error = errno;
|
2000-09-13 21:57:00 +02:00
|
|
|
}
|
2000-09-03 06:16:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
av::unshift (const char *what, int conv)
|
|
|
|
{
|
|
|
|
char **av;
|
|
|
|
av = (char **) crealloc (argv, (argc + 2) * sizeof (char *));
|
|
|
|
if (!av)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
argv = av;
|
|
|
|
memmove (argv + 1, argv, (argc + 1) * sizeof (char *));
|
2003-11-15 00:40:06 +01:00
|
|
|
char buf[CYG_MAX_PATH + 1];
|
2000-09-03 06:16:35 +02:00
|
|
|
if (conv)
|
|
|
|
{
|
|
|
|
cygwin_conv_to_posix_path (what, buf);
|
|
|
|
char *p = strchr (buf, '\0') - 4;
|
|
|
|
if (p > buf && strcasematch (p, ".exe"))
|
|
|
|
*p = '\0';
|
|
|
|
what = buf;
|
|
|
|
}
|
2002-10-09 06:08:05 +02:00
|
|
|
if (!(*argv = cstrdup1 (what)))
|
|
|
|
error = errno;
|
2000-09-03 06:16:35 +02:00
|
|
|
argc++;
|
|
|
|
calloced++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-09-20 21:51:48 +02:00
|
|
|
struct pthread_cleanup
|
|
|
|
{
|
|
|
|
_sig_func_ptr oldint;
|
|
|
|
_sig_func_ptr oldquit;
|
|
|
|
sigset_t oldmask;
|
2004-10-07 18:49:30 +02:00
|
|
|
pthread_cleanup (): oldint (NULL), oldquit (NULL), oldmask ((sigset_t) -1) {}
|
2003-09-20 21:51:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
do_cleanup (void *args)
|
|
|
|
{
|
|
|
|
# define cleanup ((pthread_cleanup *) args)
|
|
|
|
if (cleanup->oldint)
|
|
|
|
signal (SIGINT, cleanup->oldint);
|
|
|
|
if (cleanup->oldquit)
|
|
|
|
signal (SIGQUIT, cleanup->oldquit);
|
2004-10-07 18:49:30 +02:00
|
|
|
if (cleanup->oldmask != (sigset_t) -1)
|
2003-09-20 21:51:48 +02:00
|
|
|
sigprocmask (SIG_SETMASK, &(cleanup->oldmask), NULL);
|
|
|
|
# undef cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-08 01:21:17 +01:00
|
|
|
static int __stdcall
|
2002-06-11 04:08:00 +02:00
|
|
|
spawn_guts (const char * prog_arg, const char *const *argv,
|
2000-07-29 18:24:59 +02:00
|
|
|
const char *const envp[], int mode)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2003-12-07 23:37:12 +01:00
|
|
|
bool rc;
|
2000-07-29 18:24:59 +02:00
|
|
|
pid_t cygpid;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
MALLOC_CHECK;
|
|
|
|
|
|
|
|
if (prog_arg == NULL)
|
|
|
|
{
|
|
|
|
syscall_printf ("prog_arg is NULL");
|
2001-03-18 21:58:18 +01:00
|
|
|
set_errno (EINVAL);
|
2000-02-17 20:38:33 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-05-19 03:43:31 +02:00
|
|
|
syscall_printf ("spawn_guts (%d, %.9500s)", mode, prog_arg);
|
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
|
|
|
}
|
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
path_conv real_path;
|
|
|
|
|
|
|
|
linebuf one_line;
|
|
|
|
|
|
|
|
STARTUPINFO si = {0, NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL};
|
|
|
|
|
|
|
|
child_info_spawn ciresrv;
|
|
|
|
si.lpReserved2 = (LPBYTE) &ciresrv;
|
|
|
|
si.cbReserved2 = sizeof (ciresrv);
|
|
|
|
|
|
|
|
DWORD chtype;
|
2001-10-04 04:34:20 +02:00
|
|
|
if (mode != _P_OVERLAY)
|
2000-09-03 06:16:35 +02:00
|
|
|
chtype = PROC_SPAWN;
|
|
|
|
else
|
2000-10-14 07:52:38 +02:00
|
|
|
chtype = PROC_EXEC;
|
|
|
|
|
2002-07-13 22:00:27 +02:00
|
|
|
HANDLE subproc_ready;
|
2001-10-04 04:34:20 +02:00
|
|
|
if (chtype != PROC_EXEC)
|
2002-07-13 22:00:27 +02:00
|
|
|
subproc_ready = NULL;
|
2001-07-17 05:41:52 +02:00
|
|
|
else
|
|
|
|
{
|
2002-07-13 22:00:27 +02:00
|
|
|
subproc_ready = CreateEvent (&sec_all, TRUE, FALSE, NULL);
|
|
|
|
ProtectHandleINH (subproc_ready);
|
2001-07-17 05:41:52 +02:00
|
|
|
}
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2004-09-12 05:47:57 +02:00
|
|
|
init_child_info (chtype, &ciresrv, subproc_ready);
|
2004-02-02 22:00:07 +01:00
|
|
|
|
2000-09-27 07:12:09 +02:00
|
|
|
ciresrv.moreinfo = (cygheap_exec_info *) ccalloc (HEAP_1_EXEC, 1, sizeof (cygheap_exec_info));
|
2000-10-15 03:37:07 +02:00
|
|
|
ciresrv.moreinfo->old_title = NULL;
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2000-02-17 20:38:33 +01: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 06:16:35 +02:00
|
|
|
int ac;
|
|
|
|
for (ac = 0; argv[ac]; ac++)
|
2000-02-17 20:38:33 +01:00
|
|
|
/* nothing */;
|
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
av newargv (ac, argv);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-20 06:20:21 +02:00
|
|
|
int null_app_name = 0;
|
2000-09-03 06:16:35 +02:00
|
|
|
if (ac == 3 && argv[1][0] == '/' && argv[1][1] == 'c' &&
|
2000-02-17 20:38:33 +01:00
|
|
|
(iscmd (argv[0], "command.com") || iscmd (argv[0], "cmd.exe")))
|
|
|
|
{
|
2000-10-20 06:20:21 +02:00
|
|
|
real_path.check (prog_arg);
|
2000-10-21 06:53:49 +02:00
|
|
|
one_line.add ("\"");
|
2000-10-20 06:20:21 +02:00
|
|
|
if (!real_path.error)
|
|
|
|
one_line.add (real_path);
|
|
|
|
else
|
|
|
|
one_line.add (argv[0]);
|
2000-10-21 06:53:49 +02:00
|
|
|
one_line.add ("\"");
|
2000-02-17 20:38:33 +01:00
|
|
|
one_line.add (" ");
|
|
|
|
one_line.add (argv[1]);
|
|
|
|
one_line.add (" ");
|
|
|
|
one_line.add (argv[2]);
|
2000-09-03 06:16:35 +02:00
|
|
|
strcpy (real_path, argv[0]);
|
2000-10-20 06:20:21 +02:00
|
|
|
null_app_name = 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
goto skip_arg_parsing;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ext;
|
2000-09-03 06:16:35 +02:00
|
|
|
if ((ext = perhaps_suffix (prog_arg, real_path)) == NULL)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
set_errno (ENOENT);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
MALLOC_CHECK;
|
|
|
|
|
|
|
|
/* If the file name ends in either .exe, .com, .bat, or .cmd we assume
|
|
|
|
that it is NOT a script file */
|
|
|
|
while (*ext == '\0')
|
|
|
|
{
|
2002-09-19 17:12:48 +02:00
|
|
|
HANDLE hnd = CreateFile (real_path, GENERIC_READ,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
&sec_none_nih, OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL, 0);
|
2000-02-17 20:38:33 +01:00
|
|
|
if (hnd == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD done;
|
|
|
|
|
2003-11-15 00:40:06 +01:00
|
|
|
char buf[2 * CYG_MAX_PATH + 1];
|
2001-03-18 21:58:18 +01:00
|
|
|
buf[0] = buf[1] = buf[2] = buf[sizeof (buf) - 1] = '\0';
|
2000-09-30 03:56:40 +02:00
|
|
|
if (!ReadFile (hnd, buf, sizeof (buf) - 1, &done, 0))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
CloseHandle (hnd);
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle (hnd);
|
|
|
|
|
|
|
|
if (buf[0] == 'M' && buf[1] == 'Z')
|
|
|
|
break;
|
|
|
|
|
2000-09-13 21:57:00 +02:00
|
|
|
debug_printf ("%s is a script", (char *) real_path);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-07-08 11:18:29 +02:00
|
|
|
if (real_path.has_acls () && allow_ntsec
|
|
|
|
&& check_file_access (real_path, X_OK))
|
|
|
|
{
|
|
|
|
debug_printf ("... but not executable");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-09-13 21:57:00 +02:00
|
|
|
char *pgm, *arg1;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
if (buf[0] != '#' || buf[1] != '!')
|
|
|
|
{
|
2000-09-03 06:16:35 +02:00
|
|
|
pgm = (char *) "/bin/sh";
|
2000-02-17 20:38:33 +01:00
|
|
|
arg1 = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-09-13 21:57:00 +02:00
|
|
|
char *ptr;
|
2000-02-17 20:38:33 +01:00
|
|
|
pgm = buf + 2;
|
|
|
|
pgm += strspn (pgm, " \t");
|
|
|
|
for (ptr = pgm, arg1 = NULL;
|
|
|
|
*ptr && *ptr != '\r' && *ptr != '\n';
|
|
|
|
ptr++)
|
|
|
|
if (!arg1 && (*ptr == ' ' || *ptr == '\t'))
|
|
|
|
{
|
|
|
|
/* Null terminate the initial command and step over
|
|
|
|
any additional white space. If we've hit the
|
2000-09-30 03:56:40 +02:00
|
|
|
end of the line, exit the loop. Otherwise, we've
|
|
|
|
found the first argument. Position the current
|
2000-02-17 20:38:33 +01:00
|
|
|
pointer on the last known white space. */
|
|
|
|
*ptr = '\0';
|
|
|
|
char *newptr = ptr + 1;
|
|
|
|
newptr += strspn (newptr, " \t");
|
|
|
|
if (!*newptr || *newptr == '\r' || *newptr == '\n')
|
|
|
|
break;
|
|
|
|
arg1 = newptr;
|
|
|
|
ptr = newptr - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
|
2000-09-13 21:57:00 +02:00
|
|
|
/* Replace argv[0] with the full path to the script if this is the
|
|
|
|
first time through the loop. */
|
2000-09-19 15:48:52 +02:00
|
|
|
newargv.replace0_maybe (prog_arg);
|
2000-10-28 07:41:44 +02:00
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* pointers:
|
|
|
|
* pgm interpreter name
|
|
|
|
* arg1 optional string
|
|
|
|
*/
|
2000-09-03 06:16:35 +02:00
|
|
|
if (arg1)
|
|
|
|
newargv.unshift (arg1);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-10-31 01:55:32 +01:00
|
|
|
/* FIXME: This should not be using FE_NATIVE. It should be putting
|
2001-11-05 07:09:15 +01:00
|
|
|
the posix path on the argv list. */
|
2001-10-31 01:55:32 +01:00
|
|
|
find_exec (pgm, real_path, "PATH=", FE_NATIVE, &ext);
|
2000-09-03 06:16:35 +02:00
|
|
|
newargv.unshift (real_path, 1);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
if (real_path.iscygexec ())
|
2000-09-13 21:57:00 +02:00
|
|
|
newargv.dup_all ();
|
2000-09-03 06:16:35 +02:00
|
|
|
else
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-09-03 06:16:35 +02:00
|
|
|
for (int i = 0; i < newargv.argc; i++)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-09-03 06:16:35 +02:00
|
|
|
char *p = NULL;
|
|
|
|
const char *a;
|
|
|
|
|
2000-09-13 21:57:00 +02:00
|
|
|
newargv.dup_maybe (i);
|
2000-09-26 02:52:21 +02:00
|
|
|
a = i ? newargv[i] : (char *) real_path;
|
2000-09-03 06:16:35 +02:00
|
|
|
int len = strlen (a);
|
|
|
|
if (len != 0 && !strpbrk (a, " \t\n\r\""))
|
|
|
|
one_line.add (a, len);
|
|
|
|
else
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-09-03 06:16:35 +02:00
|
|
|
one_line.add ("\"", 1);
|
2000-11-20 23:10:13 +01:00
|
|
|
/* Handle embedded special characters " and \.
|
|
|
|
A " is always preceded by a \.
|
|
|
|
A \ is not special unless it precedes a ". If it does,
|
|
|
|
then all preceding \'s must be doubled to avoid having
|
|
|
|
the Windows command line parser interpret the \ as quoting
|
|
|
|
the ". This rule applies to a string of \'s before the end
|
|
|
|
of the string, since cygwin/windows uses a " to delimit the
|
|
|
|
argument. */
|
2000-09-03 06:16:35 +02:00
|
|
|
for (; (p = strpbrk (a, "\"\\")); a = ++p)
|
|
|
|
{
|
|
|
|
one_line.add (a, p - a);
|
2000-11-20 23:10:13 +01:00
|
|
|
/* Find length of string of backslashes */
|
|
|
|
int n = strspn (p, "\\");
|
|
|
|
if (!n)
|
|
|
|
one_line.add ("\\\"", 2); /* No backslashes, so it must be a ".
|
|
|
|
The " has to be protected with a backslash. */
|
|
|
|
else
|
2000-11-20 20:35:45 +01:00
|
|
|
{
|
2000-11-20 23:10:13 +01:00
|
|
|
one_line.add (p, n); /* Add the run of backslashes */
|
|
|
|
/* Need to double up all of the preceding
|
|
|
|
backslashes if they precede a quote or EOS. */
|
|
|
|
if (!p[n] || p[n] == '"')
|
|
|
|
one_line.add (p, n);
|
|
|
|
p += n - 1; /* Point to last backslash */
|
2000-11-20 20:35:45 +01:00
|
|
|
}
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
|
|
|
if (*a)
|
|
|
|
one_line.add (a);
|
|
|
|
one_line.add ("\"", 1);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2000-09-03 06:16:35 +02:00
|
|
|
MALLOC_CHECK;
|
|
|
|
one_line.add (" ", 1);
|
|
|
|
MALLOC_CHECK;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
if (one_line.ix)
|
|
|
|
one_line.buf[one_line.ix - 1] = '\0';
|
|
|
|
else
|
|
|
|
one_line.add ("", 1);
|
2000-02-17 20:38:33 +01:00
|
|
|
MALLOC_CHECK;
|
2003-09-26 10:45:24 +02:00
|
|
|
|
|
|
|
if (one_line.ix > 32767)
|
|
|
|
{
|
|
|
|
debug_printf ("Command line too long (>32K), return E2BIG");
|
|
|
|
set_errno (E2BIG);
|
|
|
|
return -1;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2000-09-13 21:57:00 +02:00
|
|
|
|
2002-06-12 07:13:54 +02:00
|
|
|
char *envblock;
|
2000-09-13 21:57:00 +02:00
|
|
|
newargv.all_calloced ();
|
2002-10-09 06:08:05 +02:00
|
|
|
if (newargv.error)
|
|
|
|
{
|
|
|
|
set_errno (newargv.error);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
ciresrv.moreinfo->argc = newargv.argc;
|
|
|
|
ciresrv.moreinfo->argv = newargv;
|
2000-10-17 01:55:58 +02:00
|
|
|
ciresrv.hexec_proc = hexec_proc;
|
2002-06-12 07:13:54 +02:00
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
if (mode != _P_OVERLAY ||
|
2001-07-18 19:05:34 +02:00
|
|
|
!DuplicateHandle (hMainProc, myself.shared_handle (), hMainProc,
|
|
|
|
&ciresrv.moreinfo->myself_pinfo, 0,
|
2000-09-03 06:16:35 +02:00
|
|
|
TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
|
ciresrv.moreinfo->myself_pinfo = NULL;
|
2004-01-24 00:05:33 +01:00
|
|
|
else
|
|
|
|
VerifyHandle (ciresrv.moreinfo->myself_pinfo);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-08-22 19:50:22 +02:00
|
|
|
skip_arg_parsing:
|
2000-02-23 05:07:13 +01:00
|
|
|
PROCESS_INFORMATION pi = {NULL, 0, 0, 0};
|
2000-02-17 20:38:33 +01:00
|
|
|
si.lpReserved = NULL;
|
|
|
|
si.lpDesktop = NULL;
|
|
|
|
si.dwFlags = STARTF_USESTDHANDLES;
|
|
|
|
si.hStdInput = handle (0, 0); /* Get input handle */
|
|
|
|
si.hStdOutput = handle (1, 1); /* Get output handle */
|
|
|
|
si.hStdError = handle (2, 1); /* Get output handle */
|
|
|
|
si.cb = sizeof (si);
|
|
|
|
|
2000-10-17 01:55:58 +02:00
|
|
|
int flags = CREATE_DEFAULT_ERROR_MODE | GetPriorityClass (hMainProc);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
if (mode == _P_DETACH || !set_console_state_for_spawn ())
|
|
|
|
flags |= DETACHED_PROCESS;
|
2000-10-17 01:55:58 +02:00
|
|
|
if (mode != _P_OVERLAY)
|
|
|
|
flags |= CREATE_SUSPENDED;
|
2004-09-12 05:47:57 +02:00
|
|
|
#if 0 //someday
|
|
|
|
else
|
|
|
|
myself->dwProcessId = 0;
|
|
|
|
#endif
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-26 12:13:41 +02:00
|
|
|
/* Some file types (currently only sockets) 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. */
|
2001-04-18 23:10:15 +02:00
|
|
|
if (cygheap->fdtab.need_fixup_before ())
|
2000-10-26 12:13:41 +02:00
|
|
|
flags |= CREATE_SUSPENDED;
|
|
|
|
|
2000-10-20 06:20:21 +02:00
|
|
|
const char *runpath = null_app_name ? NULL : (const char *) real_path;
|
|
|
|
|
2003-05-19 03:43:31 +02:00
|
|
|
syscall_printf ("null_app_name %d (%s, %.9500s)", null_app_name, runpath, one_line.buf);
|
2000-10-20 06:20:21 +02:00
|
|
|
|
2001-09-14 02:49:00 +02:00
|
|
|
void *newheap;
|
2002-06-11 04:08:00 +02:00
|
|
|
|
2000-10-20 06:20:21 +02:00
|
|
|
cygbench ("spawn-guts");
|
2002-06-14 20:01:21 +02:00
|
|
|
|
2002-10-17 19:45:09 +02:00
|
|
|
cygheap->fdtab.set_file_pointers_for_exec ();
|
2003-06-30 15:07:36 +02:00
|
|
|
cygheap->user.deimpersonate ();
|
2004-09-12 05:47:57 +02:00
|
|
|
|
2003-06-09 15:29:12 +02: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 11:24:52 +02:00
|
|
|
FIXME: If ruid != euid and ruid != saved_uid we currently give
|
2003-06-09 15:29:12 +02:00
|
|
|
up on ruid. The new process will have ruid == euid. */
|
|
|
|
if (!cygheap->user.issetuid ()
|
2003-09-16 11:24:52 +02:00
|
|
|
|| (cygheap->user.saved_uid == cygheap->user.real_uid
|
|
|
|
&& cygheap->user.saved_gid == cygheap->user.real_gid
|
2003-06-09 15:29:12 +02:00
|
|
|
&& !cygheap->user.groups.issetgroups ()))
|
2000-10-17 01:55:58 +02:00
|
|
|
{
|
2002-06-14 20:01:21 +02:00
|
|
|
ciresrv.moreinfo->envp = build_env (envp, envblock, ciresrv.moreinfo->envc,
|
|
|
|
real_path.iscygexec ());
|
2002-06-14 23:46:19 +02:00
|
|
|
newheap = cygheap_setup_for_child (&ciresrv, cygheap->fdtab.need_fixup_before ());
|
2000-10-20 06:20:21 +02:00
|
|
|
rc = CreateProcess (runpath, /* image name - with full path */
|
2000-10-17 01:55:58 +02:00
|
|
|
one_line.buf, /* what was passed to exec */
|
2004-02-06 11:37:37 +01:00
|
|
|
&sec_none_nih,/* process security attrs */
|
|
|
|
&sec_none_nih,/* thread security attrs */
|
2002-06-11 04:22:02 +02:00
|
|
|
TRUE, /* inherit handles from parent */
|
2000-10-17 01:55:58 +02:00
|
|
|
flags,
|
2002-06-11 04:22:02 +02:00
|
|
|
envblock, /* environment */
|
|
|
|
0, /* use current drive/directory */
|
2000-10-17 01:55:58 +02:00
|
|
|
&si,
|
|
|
|
&pi);
|
|
|
|
}
|
|
|
|
else
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2003-09-27 03:58:23 +02:00
|
|
|
/* Give access to myself */
|
|
|
|
if (mode == _P_OVERLAY)
|
|
|
|
myself.set_acl();
|
2002-06-14 20:01:21 +02:00
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* allow the child to interact with our window station/desktop */
|
|
|
|
HANDLE hwst, hdsk;
|
|
|
|
SECURITY_INFORMATION dsi = DACL_SECURITY_INFORMATION;
|
|
|
|
DWORD n;
|
|
|
|
char wstname[1024];
|
|
|
|
char dskname[1024];
|
|
|
|
|
2001-03-18 21:58:18 +01:00
|
|
|
hwst = GetProcessWindowStation ();
|
|
|
|
SetUserObjectSecurity (hwst, &dsi, get_null_sd ());
|
|
|
|
GetUserObjectInformation (hwst, UOI_NAME, wstname, 1024, &n);
|
|
|
|
hdsk = GetThreadDesktop (GetCurrentThreadId ());
|
|
|
|
SetUserObjectSecurity (hdsk, &dsi, get_null_sd ());
|
|
|
|
GetUserObjectInformation (hdsk, UOI_NAME, dskname, 1024, &n);
|
2000-02-17 20:38:33 +01:00
|
|
|
strcat (wstname, "\\");
|
|
|
|
strcat (wstname, dskname);
|
|
|
|
si.lpDesktop = wstname;
|
|
|
|
|
2002-06-14 20:01:21 +02:00
|
|
|
ciresrv.moreinfo->envp = build_env (envp, envblock, ciresrv.moreinfo->envc,
|
|
|
|
real_path.iscygexec ());
|
2002-06-14 23:46:19 +02:00
|
|
|
newheap = cygheap_setup_for_child (&ciresrv, cygheap->fdtab.need_fixup_before ());
|
2003-06-30 15:07:36 +02:00
|
|
|
rc = CreateProcessAsUser (cygheap->user.token (),
|
2000-10-20 06:20:21 +02:00
|
|
|
runpath, /* image name - with full path */
|
2000-02-17 20:38:33 +01:00
|
|
|
one_line.buf, /* what was passed to exec */
|
2004-02-06 11:37:37 +01:00
|
|
|
&sec_none_nih, /* process security attrs */
|
|
|
|
&sec_none_nih, /* thread security attrs */
|
2002-06-11 04:22:02 +02:00
|
|
|
TRUE, /* inherit handles from parent */
|
2000-02-17 20:38:33 +01:00
|
|
|
flags,
|
2002-06-11 04:22:02 +02:00
|
|
|
envblock, /* environment */
|
|
|
|
0, /* use current drive/directory */
|
2000-02-17 20:38:33 +01:00
|
|
|
&si,
|
|
|
|
&pi);
|
|
|
|
}
|
2003-09-20 02:31:13 +02:00
|
|
|
|
2003-06-09 15:29:12 +02:00
|
|
|
/* Restore impersonation. In case of _P_OVERLAY this isn't
|
|
|
|
allowed since it would overwrite child data. */
|
2003-09-26 04:23:34 +02:00
|
|
|
if (mode != _P_OVERLAY || !rc)
|
2003-06-30 15:07:36 +02:00
|
|
|
cygheap->user.reimpersonate ();
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
if (envblock)
|
|
|
|
free (envblock);
|
2000-02-17 20:38:33 +01:00
|
|
|
MALLOC_CHECK;
|
|
|
|
|
|
|
|
/* 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 05:02:41 +02:00
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
syscall_printf ("CreateProcess failed, %E");
|
2004-09-12 05:47:57 +02:00
|
|
|
#if 0 // someday
|
|
|
|
if (mode == _P_OVERLAY)
|
|
|
|
myself->dwProcessId = GetCurrentProcessId ();
|
|
|
|
#endif
|
2002-07-13 22:00:27 +02:00
|
|
|
if (subproc_ready)
|
|
|
|
ForceCloseHandle (subproc_ready);
|
2001-09-14 02:49:00 +02:00
|
|
|
cygheap_setup_for_child_cleanup (newheap, &ciresrv, 0);
|
2000-08-03 05:02:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-09-25 15:49:21 +02:00
|
|
|
/* FIXME: There is a small race here */
|
|
|
|
|
2003-09-26 04:23:34 +02:00
|
|
|
int res;
|
2003-09-25 15:49:21 +02:00
|
|
|
pthread_cleanup cleanup;
|
|
|
|
pthread_cleanup_push (do_cleanup, (void *) &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);
|
|
|
|
(void) sigprocmask (SIG_BLOCK, &child_block, &cleanup.oldmask);
|
|
|
|
}
|
|
|
|
|
2004-02-13 20:34:32 +01:00
|
|
|
/* Fixup the parent data structures if needed and resume the child's
|
2000-10-26 12:13:41 +02:00
|
|
|
main thread. */
|
2001-09-14 02:49:00 +02:00
|
|
|
if (!cygheap->fdtab.need_fixup_before ())
|
|
|
|
cygheap_setup_for_child_cleanup (newheap, &ciresrv, 0);
|
|
|
|
else
|
2000-10-26 12:13:41 +02:00
|
|
|
{
|
2001-04-18 23:10:15 +02:00
|
|
|
cygheap->fdtab.fixup_before_exec (pi.dwProcessId);
|
2001-09-14 02:49:00 +02:00
|
|
|
cygheap_setup_for_child_cleanup (newheap, &ciresrv, 1);
|
2000-10-26 12:13:41 +02:00
|
|
|
if (mode == _P_OVERLAY)
|
2002-10-14 05:51:44 +02:00
|
|
|
{
|
|
|
|
ResumeThread (pi.hThread);
|
|
|
|
cygthread::terminate ();
|
|
|
|
}
|
2000-10-26 12:13:41 +02:00
|
|
|
}
|
|
|
|
|
2002-10-13 20:16:33 +02:00
|
|
|
if (mode != _P_OVERLAY)
|
2000-07-29 18:24:59 +02:00
|
|
|
cygpid = cygwin_pid (pi.dwProcessId);
|
2002-10-13 20:16:33 +02:00
|
|
|
else
|
|
|
|
cygpid = myself->pid;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* We print the original program name here so the user can see that too. */
|
2003-05-19 03:43:31 +02:00
|
|
|
syscall_printf ("%d = spawn_guts (%s, %.9500s)",
|
2000-10-09 04:53:44 +02:00
|
|
|
rc ? cygpid : (unsigned int) -1, prog_arg, one_line.buf);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-07-29 18:24:59 +02:00
|
|
|
/* Name the handle similarly to proc_subproc. */
|
|
|
|
ProtectHandle1 (pi.hProcess, childhProc);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
if (mode == _P_OVERLAY)
|
|
|
|
{
|
2000-10-17 01:55:58 +02:00
|
|
|
/* These are both duplicated in the child code. We do this here,
|
|
|
|
primarily for strace. */
|
2000-10-24 04:25:27 +02:00
|
|
|
strace.execing = 1;
|
|
|
|
hExeced = pi.hProcess;
|
2002-01-10 04:21:27 +01:00
|
|
|
dwExeced = pi.dwProcessId;
|
2001-06-24 23:57:50 +02:00
|
|
|
strcpy (myself->progname, real_path);
|
2001-08-04 23:10:52 +02:00
|
|
|
close_all_files ();
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-10-25 05:54:50 +02:00
|
|
|
myself->set_has_pgid_children ();
|
2000-10-17 01:55:58 +02:00
|
|
|
ProtectHandle (pi.hThread);
|
2003-09-26 04:23:34 +02:00
|
|
|
pinfo child (cygpid, PID_IN_USE);
|
2000-07-29 18:24:59 +02:00
|
|
|
if (!child)
|
|
|
|
{
|
2003-09-26 04:23:34 +02:00
|
|
|
syscall_printf ("pinfo failed");
|
2003-09-26 05:20:30 +02:00
|
|
|
if (get_errno () != ENOMEM)
|
|
|
|
set_errno (EAGAIN);
|
2003-09-26 04:23:34 +02:00
|
|
|
res = -1;
|
|
|
|
goto out;
|
2000-07-29 18:24:59 +02:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
child->dwProcessId = pi.dwProcessId;
|
|
|
|
child->hProcess = pi.hProcess;
|
2003-09-26 04:23:34 +02:00
|
|
|
if (!child.remember ())
|
|
|
|
{
|
|
|
|
syscall_printf ("process table full");
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
res = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2001-06-24 23:57:50 +02:00
|
|
|
strcpy (child->progname, real_path);
|
2001-10-04 04:34:20 +02:00
|
|
|
/* FIXME: This introduces an unreferenced, open handle into the child.
|
2001-11-05 07:09:15 +01:00
|
|
|
The purpose is to keep the pid shared memory open so that all of
|
2001-10-04 04:34:20 +02: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. */
|
|
|
|
(void) DuplicateHandle (hMainProc, child.shared_handle (), pi.hProcess,
|
|
|
|
NULL, 0, 0, DUPLICATE_SAME_ACCESS);
|
2000-10-17 01:55:58 +02:00
|
|
|
/* Start the child running */
|
|
|
|
ResumeThread (pi.hThread);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ForceCloseHandle (pi.hThread);
|
|
|
|
|
2000-10-17 01:55:58 +02:00
|
|
|
sigproc_printf ("spawned windows pid %d", pi.dwProcessId);
|
|
|
|
|
2003-12-07 23:37:12 +01:00
|
|
|
bool exited;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-14 07:52:38 +02:00
|
|
|
res = 0;
|
2003-12-07 23:37:12 +01:00
|
|
|
exited = false;
|
2000-10-14 07:52:38 +02:00
|
|
|
MALLOC_CHECK;
|
2001-07-17 05:41:52 +02:00
|
|
|
if (mode == _P_OVERLAY)
|
2000-10-14 07:52:38 +02:00
|
|
|
{
|
2001-07-17 05:41:52 +02:00
|
|
|
int nwait = 3;
|
2002-07-13 22:00:27 +02:00
|
|
|
HANDLE waitbuf[3] = {pi.hProcess, signal_arrived, subproc_ready};
|
2001-07-17 05:41:52 +02:00
|
|
|
for (int i = 0; i < 100; i++)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-07-17 05:41:52 +02:00
|
|
|
switch (WaitForMultipleObjects (nwait, waitbuf, FALSE, INFINITE))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-07-17 05:41:52 +02:00
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
sigproc_printf ("subprocess exited");
|
|
|
|
DWORD exitcode;
|
|
|
|
if (!GetExitCodeProcess (pi.hProcess, &exitcode))
|
|
|
|
exitcode = 1;
|
|
|
|
res |= exitcode;
|
2003-12-07 23:37:12 +01:00
|
|
|
exited = true;
|
2001-07-17 05:41:52 +02:00
|
|
|
break;
|
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
sigproc_printf ("signal arrived");
|
|
|
|
reset_signal_arrived ();
|
|
|
|
continue;
|
|
|
|
case WAIT_OBJECT_0 + 2:
|
2003-06-03 04:32:49 +02:00
|
|
|
if (my_parent_is_alive ())
|
2001-03-12 15:49:29 +01:00
|
|
|
res |= EXIT_REPARENTING;
|
2003-06-03 04:32:49 +02:00
|
|
|
else if (!myself->ppid_handle)
|
2000-03-15 05:49:36 +01:00
|
|
|
{
|
2001-07-17 05:41:52 +02:00
|
|
|
nwait = 2;
|
2000-10-14 07:52:38 +02:00
|
|
|
sigproc_terminate ();
|
|
|
|
continue;
|
2000-03-15 05:49:36 +01:00
|
|
|
}
|
2001-07-17 05:41:52 +02:00
|
|
|
break;
|
|
|
|
case WAIT_FAILED:
|
|
|
|
system_printf ("wait failed: nwait %d, pid %d, winpid %d, %E",
|
|
|
|
nwait, myself->pid, myself->dwProcessId);
|
|
|
|
system_printf ("waitbuf[0] %p %d", waitbuf[0],
|
|
|
|
WaitForSingleObject (waitbuf[0], 0));
|
2004-09-03 03:53:12 +02:00
|
|
|
system_printf ("waitbuf[1] %p %d", waitbuf[1],
|
2001-07-17 05:41:52 +02:00
|
|
|
WaitForSingleObject (waitbuf[1], 0));
|
2004-09-03 03:53:12 +02:00
|
|
|
system_printf ("waitbuf[w] %p %d", waitbuf[2],
|
2001-07-17 05:41:52 +02:00
|
|
|
WaitForSingleObject (waitbuf[2], 0));
|
|
|
|
set_errno (ECHILD);
|
|
|
|
try_to_debug ();
|
|
|
|
return -1;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-07-13 22:00:27 +02:00
|
|
|
ForceCloseHandle (subproc_ready);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-09-03 03:53:12 +02:00
|
|
|
sigproc_printf ("res %p", res);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-07-17 05:41:52 +02:00
|
|
|
if (res & EXIT_REPARENTING)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-07-17 05:41:52 +02:00
|
|
|
/* Try to reparent child process.
|
|
|
|
* Make handles to child available to parent process and exit with
|
|
|
|
* EXIT_REPARENTING status. Wait() syscall in parent will then wait
|
|
|
|
* for newly created child.
|
|
|
|
*/
|
|
|
|
HANDLE oldh = myself->hProcess;
|
|
|
|
HANDLE h = myself->ppid_handle;
|
|
|
|
sigproc_printf ("parent handle %p", h);
|
|
|
|
int rc = DuplicateHandle (hMainProc, pi.hProcess, h, &myself->hProcess,
|
|
|
|
0, FALSE, DUPLICATE_SAME_ACCESS);
|
|
|
|
sigproc_printf ("%d = DuplicateHandle, oldh %p, newh %p",
|
|
|
|
rc, oldh, myself->hProcess);
|
2004-01-24 00:05:33 +01:00
|
|
|
VerifyHandle (myself->hProcess);
|
2001-07-17 05:41:52 +02:00
|
|
|
if (!rc && my_parent_is_alive ())
|
|
|
|
{
|
|
|
|
system_printf ("Reparent failed, parent handle %p, %E", h);
|
|
|
|
system_printf ("my dwProcessId %d, myself->dwProcessId %d",
|
|
|
|
GetCurrentProcessId (), myself->dwProcessId);
|
|
|
|
system_printf ("old hProcess %p, hProcess %p", oldh, myself->hProcess);
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2000-10-14 07:52:38 +02:00
|
|
|
|
2001-07-17 05:41:52 +02:00
|
|
|
}
|
2001-06-26 23:03:08 +02:00
|
|
|
|
2000-10-14 07:52:38 +02:00
|
|
|
MALLOC_CHECK;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
switch (mode)
|
|
|
|
{
|
2000-10-14 07:52:38 +02:00
|
|
|
case _P_OVERLAY:
|
2000-10-17 01:55:58 +02:00
|
|
|
ForceCloseHandle1 (pi.hProcess, childhProc);
|
2000-10-21 07:53:43 +02:00
|
|
|
myself->exit (res, 1);
|
2000-10-14 07:52:38 +02:00
|
|
|
break;
|
2000-09-03 06:16:35 +02:00
|
|
|
case _P_WAIT:
|
2003-09-20 02:31:13 +02:00
|
|
|
case _P_SYSTEM:
|
2004-03-14 07:34:05 +01:00
|
|
|
if (waitpid (cygpid, (int *) &res, 0) != cygpid)
|
|
|
|
res = -1;
|
2000-09-03 06:16:35 +02:00
|
|
|
break;
|
|
|
|
case _P_DETACH:
|
|
|
|
res = 0; /* Lose all memory of this child. */
|
|
|
|
break;
|
|
|
|
case _P_NOWAIT:
|
|
|
|
case _P_NOWAITO:
|
|
|
|
case _P_VFORK:
|
|
|
|
res = cygpid;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-09-26 04:23:34 +02:00
|
|
|
out:
|
2003-09-20 21:51:48 +02:00
|
|
|
pthread_cleanup_pop (1);
|
2000-02-17 20:38:33 +01:00
|
|
|
return (int) res;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return waitpid (pid, result, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper function for spawn runtime calls.
|
|
|
|
* Doesn't search the path.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern "C" int
|
2002-06-11 04:08:00 +02:00
|
|
|
spawnve (int mode, const char *path, const char *const *argv,
|
|
|
|
const char *const *envp)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2004-01-24 00:05:33 +01:00
|
|
|
#ifdef NEWVFORK
|
2000-02-17 20:38:33 +01:00
|
|
|
vfork_save *vf = vfork_storage.val ();
|
|
|
|
|
|
|
|
if (vf != NULL && (vf->pid < 0) && mode == _P_OVERLAY)
|
|
|
|
mode = _P_NOWAIT;
|
|
|
|
else
|
|
|
|
vf = NULL;
|
2004-01-24 00:05:33 +01:00
|
|
|
#endif
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-06-11 04:08:00 +02:00
|
|
|
syscall_printf ("spawnve (%s, %s, %x)", path, argv[0], envp);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
switch (mode)
|
|
|
|
{
|
2000-09-03 06:16:35 +02:00
|
|
|
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. */
|
2002-06-11 04:08:00 +02:00
|
|
|
spawn_guts (path, argv, envp, mode);
|
2000-09-03 06:16:35 +02:00
|
|
|
/* 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:
|
2003-09-20 02:31:13 +02:00
|
|
|
case _P_SYSTEM:
|
2000-09-03 06:16:35 +02:00
|
|
|
subproc_init ();
|
2002-06-11 04:08:00 +02:00
|
|
|
ret = spawn_guts (path, argv, envp, mode);
|
2004-01-24 00:05:33 +01:00
|
|
|
#ifdef NEWVFORK
|
2002-08-18 07:49:26 +02:00
|
|
|
if (vf)
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
2003-09-26 05:09:42 +02:00
|
|
|
if (ret > 0)
|
2003-11-28 21:55:59 +01:00
|
|
|
{
|
|
|
|
debug_printf ("longjmping due to vfork");
|
|
|
|
vf->restore_pid (ret);
|
|
|
|
}
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
2004-01-24 00:05:33 +01:00
|
|
|
#endif
|
2000-09-03 06:16:35 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_errno (EINVAL);
|
|
|
|
ret = -1;
|
|
|
|
break;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* spawn functions as implemented in the MS runtime library.
|
|
|
|
* Most of these based on (and copied from) newlib/libc/posix/execXX.c
|
|
|
|
*/
|
|
|
|
|
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, ...)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
va_list args;
|
|
|
|
const char *argv[256];
|
|
|
|
|
|
|
|
va_start (args, arg0);
|
|
|
|
argv[0] = arg0;
|
|
|
|
i = 1;
|
|
|
|
|
|
|
|
do
|
|
|
|
argv[i] = va_arg (args, const char *);
|
|
|
|
while (argv[i++] != NULL);
|
|
|
|
|
|
|
|
va_end (args);
|
|
|
|
|
2002-06-11 04:08:00 +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, ...)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
va_list args;
|
|
|
|
const char * const *envp;
|
|
|
|
const char *argv[256];
|
|
|
|
|
|
|
|
va_start (args, arg0);
|
|
|
|
argv[0] = arg0;
|
|
|
|
i = 1;
|
|
|
|
|
|
|
|
do
|
|
|
|
argv[i] = va_arg (args, const char *);
|
|
|
|
while (argv[i++] != NULL);
|
|
|
|
|
|
|
|
envp = va_arg (args, const char * const *);
|
|
|
|
va_end (args);
|
|
|
|
|
2002-06-11 04:08:00 +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
|
2000-02-17 20:38:33 +01:00
|
|
|
spawnlp (int mode, const char *path, const char *arg0, ...)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
va_list args;
|
|
|
|
const char *argv[256];
|
|
|
|
|
|
|
|
va_start (args, arg0);
|
|
|
|
argv[0] = arg0;
|
|
|
|
i = 1;
|
|
|
|
|
|
|
|
do
|
|
|
|
argv[i] = va_arg (args, const char *);
|
|
|
|
while (argv[i++] != NULL);
|
|
|
|
|
|
|
|
va_end (args);
|
|
|
|
|
2000-08-02 18:28:18 +02:00
|
|
|
return spawnvpe (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
|
|
|
spawnlpe (int mode, const char *path, const char *arg0, ...)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
va_list args;
|
|
|
|
const char * const *envp;
|
|
|
|
const char *argv[256];
|
|
|
|
|
|
|
|
va_start (args, arg0);
|
|
|
|
argv[0] = arg0;
|
|
|
|
i = 1;
|
|
|
|
|
|
|
|
do
|
|
|
|
argv[i] = va_arg (args, const char *);
|
|
|
|
while (argv[i++] != NULL);
|
|
|
|
|
|
|
|
envp = va_arg (args, const char * const *);
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
return spawnvpe (mode, path, (char * const *) argv, envp);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2002-06-11 04:08:00 +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
|
2000-02-17 20:38:33 +01:00
|
|
|
spawnvp (int mode, const char *path, const char * const *argv)
|
|
|
|
{
|
2000-08-02 18:28:18 +02:00
|
|
|
return spawnvpe (mode, path, 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,
|
|
|
|
const char * const *envp)
|
|
|
|
{
|
2000-04-26 07:13:32 +02:00
|
|
|
path_conv buf;
|
2002-06-11 04:08:00 +02:00
|
|
|
return spawnve (mode, find_exec (file, buf), argv, envp);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|