2009-01-15 03:58:27 +00:00
|
|
|
/*
|
2012-08-16 03:47:55 +00:00
|
|
|
* $Id$
|
2009-01-15 03:58:27 +00:00
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2014-11-19 14:42:24 +00:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2014-11-19 14:42:24 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2009-01-15 03:58:27 +00:00
|
|
|
*/
|
|
|
|
|
2016-04-28 14:33:10 +00:00
|
|
|
#include <qse/si/pio.h>
|
2017-09-26 13:49:16 +00:00
|
|
|
#include <qse/si/sinfo.h>
|
2011-12-31 15:24:48 +00:00
|
|
|
#include <qse/cmn/mbwc.h>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include "../cmn/mem-prv.h"
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2011-03-15 21:40:39 +00:00
|
|
|
#if defined(_WIN32)
|
2009-01-15 03:58:27 +00:00
|
|
|
# include <windows.h>
|
2011-03-17 02:37:06 +00:00
|
|
|
# include <tchar.h>
|
|
|
|
#elif defined(__OS2__)
|
2011-03-19 09:07:21 +00:00
|
|
|
# define INCL_DOSQUEUES
|
2011-03-17 02:37:06 +00:00
|
|
|
# define INCL_DOSPROCESS
|
|
|
|
# define INCL_DOSERRORS
|
2011-03-15 21:40:39 +00:00
|
|
|
# include <os2.h>
|
2011-05-07 09:28:27 +00:00
|
|
|
#elif defined(__DOS__)
|
|
|
|
# include <io.h>
|
2012-02-20 14:45:49 +00:00
|
|
|
# include <errno.h>
|
2009-01-15 03:58:27 +00:00
|
|
|
#else
|
2016-04-27 14:57:50 +00:00
|
|
|
# include "../cmn/syscall.h"
|
2012-02-01 14:32:34 +00:00
|
|
|
# if defined(HAVE_SPAWN_H)
|
|
|
|
# include <spawn.h>
|
|
|
|
# endif
|
2013-01-17 14:13:48 +00:00
|
|
|
# if defined(HAVE_CRT_EXTERNS_H)
|
|
|
|
# include <crt_externs.h> /* MacOSX/darwin. _NSGetEnviron() */
|
|
|
|
# endif
|
2009-01-15 03:58:27 +00:00
|
|
|
#endif
|
|
|
|
|
2012-02-12 13:20:39 +00:00
|
|
|
static qse_ssize_t pio_input (
|
|
|
|
qse_tio_t* tio, qse_tio_cmd_t cmd, void* buf, qse_size_t size);
|
|
|
|
static qse_ssize_t pio_output (
|
|
|
|
qse_tio_t* tio, qse_tio_cmd_t cmd, void* buf, qse_size_t size);
|
|
|
|
|
2016-04-27 14:57:50 +00:00
|
|
|
#include "../cmn/syserr.h"
|
2012-12-25 14:10:02 +00:00
|
|
|
IMPLEMENT_SYSERR_TO_ERRNUM (pio, PIO)
|
2012-02-12 13:20:39 +00:00
|
|
|
|
|
|
|
static qse_pio_errnum_t tio_errnum_to_pio_errnum (qse_tio_t* tio)
|
|
|
|
{
|
|
|
|
/* i only translate error codes that's relevant
|
|
|
|
* to pio. all other errors becom QSE_PIO_EOTHER */
|
|
|
|
switch (tio->errnum)
|
|
|
|
{
|
|
|
|
case QSE_TIO_ENOMEM:
|
|
|
|
return QSE_PIO_ENOMEM;
|
|
|
|
case QSE_TIO_EINVAL:
|
|
|
|
return QSE_PIO_EINVAL;
|
|
|
|
case QSE_TIO_ENOENT:
|
|
|
|
return QSE_PIO_ENOENT;
|
|
|
|
case QSE_TIO_EACCES:
|
|
|
|
return QSE_PIO_EACCES;
|
|
|
|
case QSE_TIO_EILSEQ:
|
|
|
|
return QSE_PIO_EILSEQ;
|
|
|
|
case QSE_TIO_EICSEQ:
|
|
|
|
return QSE_PIO_EICSEQ;
|
|
|
|
case QSE_TIO_EILCHR:
|
|
|
|
return QSE_PIO_EILCHR;
|
|
|
|
default:
|
|
|
|
return QSE_PIO_EOTHER;
|
|
|
|
}
|
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
qse_pio_t* qse_pio_open (
|
2012-12-01 13:13:13 +00:00
|
|
|
qse_mmgr_t* mmgr, qse_size_t xtnsize,
|
2011-12-28 14:26:02 +00:00
|
|
|
const qse_char_t* cmd, qse_env_t* env, int flags)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2009-01-29 08:50:30 +00:00
|
|
|
qse_pio_t* pio;
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2012-12-01 13:13:13 +00:00
|
|
|
pio = QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(qse_pio_t) + xtnsize);
|
|
|
|
if (pio)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2012-12-01 13:13:13 +00:00
|
|
|
if (qse_pio_init (pio, mmgr, cmd, env, flags) <= -1)
|
|
|
|
{
|
|
|
|
QSE_MMGR_FREE (mmgr, pio);
|
|
|
|
pio = QSE_NULL;
|
|
|
|
}
|
2014-07-11 14:17:00 +00:00
|
|
|
else QSE_MEMSET (QSE_XTN(pio), 0, xtnsize);
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
return pio;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
void qse_pio_close (qse_pio_t* pio)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2009-01-29 08:50:30 +00:00
|
|
|
qse_pio_fini (pio);
|
|
|
|
QSE_MMGR_FREE (pio->mmgr, pio);
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2012-02-01 14:32:34 +00:00
|
|
|
#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__)
|
|
|
|
struct param_t
|
|
|
|
{
|
|
|
|
qse_mchar_t* mcmd;
|
|
|
|
qse_mchar_t* fixed_argv[4];
|
|
|
|
qse_mchar_t** argv;
|
|
|
|
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
2012-02-19 14:38:22 +00:00
|
|
|
/* nothing extra */
|
2012-02-01 14:32:34 +00:00
|
|
|
#else
|
|
|
|
qse_mchar_t fixed_mbuf[64];
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
typedef struct param_t param_t;
|
|
|
|
|
|
|
|
static void free_param (qse_pio_t* pio, param_t* param)
|
|
|
|
{
|
|
|
|
if (param->argv && param->argv != param->fixed_argv)
|
|
|
|
QSE_MMGR_FREE (pio->mmgr, param->argv);
|
|
|
|
if (param->mcmd) QSE_MMGR_FREE (pio->mmgr, param->mcmd);
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
static int make_param (qse_pio_t* pio, const qse_char_t* cmd, int flags, param_t* param)
|
2012-02-01 14:32:34 +00:00
|
|
|
{
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
|
|
|
qse_mchar_t* mcmd = QSE_NULL;
|
|
|
|
#else
|
|
|
|
qse_mchar_t* mcmd = QSE_NULL;
|
|
|
|
qse_char_t* wcmd = QSE_NULL;
|
|
|
|
#endif
|
|
|
|
int fcnt = 0;
|
|
|
|
|
|
|
|
QSE_MEMSET (param, 0, QSE_SIZEOF(*param));
|
|
|
|
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
|
|
|
if (flags & QSE_PIO_SHELL) mcmd = (qse_char_t*)cmd;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mcmd = qse_strdup (cmd, pio->mmgr);
|
|
|
|
if (mcmd == QSE_NULL)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_ENOMEM;
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
fcnt = qse_strspl (mcmd, QSE_T(""),
|
|
|
|
QSE_T('\"'), QSE_T('\"'), QSE_T('\\'));
|
|
|
|
if (fcnt <= 0)
|
|
|
|
{
|
|
|
|
/* no field or an error */
|
|
|
|
pio->errnum = QSE_PIO_EINVAL;
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
}
|
2014-12-14 15:55:03 +00:00
|
|
|
#else
|
2012-02-01 14:32:34 +00:00
|
|
|
if (flags & QSE_PIO_MBSCMD)
|
|
|
|
{
|
|
|
|
/* the cmd is flagged to be of qse_mchar_t
|
|
|
|
* while the default character type is qse_wchar_t. */
|
|
|
|
|
|
|
|
if (flags & QSE_PIO_SHELL) mcmd = (qse_mchar_t*)cmd;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mcmd = qse_mbsdup ((const qse_mchar_t*)cmd, pio->mmgr);
|
|
|
|
if (mcmd == QSE_NULL)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_ENOMEM;
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
fcnt = qse_mbsspl (mcmd, QSE_MT(""),
|
|
|
|
QSE_MT('\"'), QSE_MT('\"'), QSE_MT('\\'));
|
|
|
|
if (fcnt <= 0)
|
|
|
|
{
|
|
|
|
/* no field or an error */
|
|
|
|
pio->errnum = QSE_PIO_EINVAL;
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qse_size_t n, mn, wl;
|
|
|
|
|
|
|
|
if (flags & QSE_PIO_SHELL)
|
|
|
|
{
|
|
|
|
if (qse_wcstombs (cmd, &wl, QSE_NULL, &mn) <= -1)
|
|
|
|
{
|
|
|
|
/* cmd has illegal sequence */
|
|
|
|
pio->errnum = QSE_PIO_EINVAL;
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wcmd = qse_strdup (cmd, pio->mmgr);
|
|
|
|
if (wcmd == QSE_NULL)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_ENOMEM;
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
fcnt = qse_strspl (wcmd, QSE_T(""),
|
|
|
|
QSE_T('\"'), QSE_T('\"'), QSE_T('\\'));
|
|
|
|
if (fcnt <= 0)
|
|
|
|
{
|
|
|
|
/* no field or an error */
|
|
|
|
pio->errnum = QSE_PIO_EINVAL;
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate the length of the string after splitting */
|
|
|
|
for (wl = 0, n = fcnt; n > 0; )
|
|
|
|
{
|
|
|
|
if (wcmd[wl++] == QSE_T('\0')) n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qse_wcsntombsn (wcmd, &wl, QSE_NULL, &mn) <= -1)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_EINVAL;
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prepare to reserve 1 more slot for the terminating '\0'
|
|
|
|
* by incrementing mn by 1. */
|
|
|
|
mn = mn + 1;
|
|
|
|
|
|
|
|
if (mn <= QSE_COUNTOF(param->fixed_mbuf))
|
|
|
|
{
|
|
|
|
mcmd = param->fixed_mbuf;
|
|
|
|
mn = QSE_COUNTOF(param->fixed_mbuf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mcmd = QSE_MMGR_ALLOC (pio->mmgr, mn * QSE_SIZEOF(*mcmd));
|
|
|
|
if (mcmd == QSE_NULL)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_ENOMEM;
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & QSE_PIO_SHELL)
|
|
|
|
{
|
|
|
|
QSE_ASSERT (wcmd == QSE_NULL);
|
|
|
|
/* qse_wcstombs() should succeed as
|
|
|
|
* it was successful above */
|
|
|
|
qse_wcstombs (cmd, &wl, mcmd, &mn);
|
|
|
|
/* qse_wcstombs() null-terminate mcmd */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QSE_ASSERT (wcmd != QSE_NULL);
|
|
|
|
/* qse_wcsntombsn() should succeed as
|
|
|
|
* it was was successful above */
|
|
|
|
qse_wcsntombsn (wcmd, &wl, mcmd, &mn);
|
|
|
|
/* qse_wcsntombsn() doesn't null-terminate mcmd */
|
|
|
|
mcmd[mn] = QSE_MT('\0');
|
|
|
|
|
|
|
|
QSE_MMGR_FREE (pio->mmgr, wcmd);
|
|
|
|
wcmd = QSE_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (flags & QSE_PIO_SHELL)
|
|
|
|
{
|
|
|
|
param->argv = param->fixed_argv;
|
|
|
|
param->argv[0] = QSE_MT("/bin/sh");
|
|
|
|
param->argv[1] = QSE_MT("-c");
|
|
|
|
param->argv[2] = mcmd;
|
|
|
|
param->argv[3] = QSE_NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
qse_mchar_t** argv;
|
|
|
|
qse_mchar_t* mcmdptr;
|
|
|
|
|
2016-04-11 14:32:27 +00:00
|
|
|
if (fcnt < QSE_COUNTOF(param->fixed_argv))
|
2012-02-01 14:32:34 +00:00
|
|
|
{
|
2016-04-11 14:32:27 +00:00
|
|
|
param->argv = param->fixed_argv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
param->argv = QSE_MMGR_ALLOC (pio->mmgr, (fcnt + 1) * QSE_SIZEOF(argv[0]));
|
|
|
|
if (param->argv == QSE_NULL)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_ENOMEM;
|
|
|
|
goto oops;
|
|
|
|
}
|
2012-02-01 14:32:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mcmdptr = mcmd;
|
|
|
|
for (i = 0; i < fcnt; i++)
|
|
|
|
{
|
|
|
|
param->argv[i] = mcmdptr;
|
|
|
|
while (*mcmdptr != QSE_MT('\0')) mcmdptr++;
|
|
|
|
mcmdptr++;
|
|
|
|
}
|
|
|
|
param->argv[i] = QSE_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
2012-02-02 14:48:06 +00:00
|
|
|
if (mcmd && mcmd != (qse_mchar_t*)cmd) param->mcmd = mcmd;
|
2012-02-01 14:32:34 +00:00
|
|
|
#else
|
2012-02-02 14:48:06 +00:00
|
|
|
if (mcmd && mcmd != (qse_mchar_t*)cmd &&
|
|
|
|
mcmd != param->fixed_mbuf) param->mcmd = mcmd;
|
2012-02-01 14:32:34 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
oops:
|
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
|
|
|
if (mcmd && mcmd != cmd) QSE_MMGR_FREE (pio->mmgr, mcmd);
|
|
|
|
#else
|
2012-02-02 14:48:06 +00:00
|
|
|
if (mcmd && mcmd != (qse_mchar_t*)cmd &&
|
|
|
|
mcmd != param->fixed_mbuf) QSE_MMGR_FREE (pio->mmgr, mcmd);
|
2012-02-01 14:32:34 +00:00
|
|
|
if (wcmd) QSE_MMGR_FREE (pio->mmgr, wcmd);
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-02-10 13:58:23 +00:00
|
|
|
static int assert_executable (qse_pio_t* pio, const qse_mchar_t* path)
|
|
|
|
{
|
|
|
|
qse_lstat_t st;
|
|
|
|
|
|
|
|
if (QSE_ACCESS(path, X_OK) <= -1)
|
|
|
|
{
|
2012-02-12 13:20:39 +00:00
|
|
|
pio->errnum = syserr_to_errnum (errno);
|
2012-02-10 13:58:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-02-12 13:20:39 +00:00
|
|
|
/*if (QSE_LSTAT(path, &st) <= -1)*/
|
|
|
|
if (QSE_STAT(path, &st) <= -1)
|
2012-02-10 13:58:23 +00:00
|
|
|
{
|
2012-02-12 13:20:39 +00:00
|
|
|
pio->errnum = syserr_to_errnum (errno);
|
2012-02-10 13:58:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISREG(st.st_mode))
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_EACCES;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-01 14:32:34 +00:00
|
|
|
static QSE_INLINE int is_fd_valid (int fd)
|
|
|
|
{
|
2013-09-11 09:14:29 +00:00
|
|
|
return QSE_FCNTL (fd, F_GETFD, 0) != -1 || errno != EBADF;
|
2012-02-02 14:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static QSE_INLINE int is_fd_valid_and_nocloexec (int fd)
|
|
|
|
{
|
2013-09-11 09:14:29 +00:00
|
|
|
int flags = QSE_FCNTL (fd, F_GETFD, 0);
|
2012-02-02 14:48:06 +00:00
|
|
|
if (flags == -1)
|
|
|
|
{
|
|
|
|
if (errno == EBADF) return 0; /* invalid. return false */
|
|
|
|
return -1; /* unknown. true but negative to indicate unknown */
|
|
|
|
}
|
|
|
|
return !(flags & FD_CLOEXEC)? 1: 0;
|
2012-02-01 14:32:34 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
static qse_pio_pid_t standard_fork_and_exec (qse_pio_t* pio, int pipes[], param_t* param, qse_env_t* env)
|
|
|
|
{
|
|
|
|
qse_pio_pid_t pid;
|
|
|
|
|
|
|
|
#if defined(HAVE_CRT_EXTERNS_H)
|
|
|
|
# define environ (*(_NSGetEnviron()))
|
|
|
|
#else
|
|
|
|
extern char** environ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pid = QSE_FORK();
|
|
|
|
if (pid <= -1)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
/* child */
|
|
|
|
qse_pio_hnd_t devnull = -1;
|
|
|
|
|
|
|
|
if (!(pio->flags & QSE_PIO_NOCLOEXEC))
|
|
|
|
{
|
2017-09-26 13:49:16 +00:00
|
|
|
if (qse_close_open_fds_using_proc (pipes, 6) <= -1)
|
2014-09-19 14:05:02 +00:00
|
|
|
{
|
2017-09-26 13:49:16 +00:00
|
|
|
int fd = qse_get_highest_fd ();
|
2014-09-19 14:05:02 +00:00
|
|
|
|
|
|
|
/* close all other unknown open handles except
|
|
|
|
* stdin/out/err and the pipes. */
|
|
|
|
while (fd > 2)
|
|
|
|
{
|
|
|
|
if (fd != pipes[0] && fd != pipes[1] &&
|
2017-09-26 13:49:16 +00:00
|
|
|
fd != pipes[2] && fd != pipes[3] &&
|
|
|
|
fd != pipes[4] && fd != pipes[5])
|
2014-09-19 14:05:02 +00:00
|
|
|
{
|
|
|
|
QSE_CLOSE (fd);
|
|
|
|
}
|
|
|
|
fd--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pio->flags & QSE_PIO_WRITEIN)
|
|
|
|
{
|
|
|
|
/* child should read */
|
|
|
|
QSE_CLOSE (pipes[1]);
|
|
|
|
pipes[1] = QSE_PIO_HND_NIL;
|
|
|
|
if (QSE_DUP2 (pipes[0], 0) <= -1) goto child_oops;
|
|
|
|
QSE_CLOSE (pipes[0]);
|
|
|
|
pipes[0] = QSE_PIO_HND_NIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pio->flags & QSE_PIO_READOUT)
|
|
|
|
{
|
|
|
|
/* child should write */
|
|
|
|
QSE_CLOSE (pipes[2]);
|
|
|
|
pipes[2] = QSE_PIO_HND_NIL;
|
|
|
|
if (QSE_DUP2 (pipes[3], 1) <= -1) goto child_oops;
|
|
|
|
|
|
|
|
if (pio->flags & QSE_PIO_ERRTOOUT)
|
|
|
|
{
|
|
|
|
if (QSE_DUP2 (pipes[3], 2) <= -1) goto child_oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSE_CLOSE (pipes[3]);
|
|
|
|
pipes[3] = QSE_PIO_HND_NIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pio->flags & QSE_PIO_READERR)
|
|
|
|
{
|
|
|
|
/* child should write */
|
|
|
|
QSE_CLOSE (pipes[4]);
|
|
|
|
pipes[4] = QSE_PIO_HND_NIL;
|
|
|
|
if (QSE_DUP2 (pipes[5], 2) <= -1) goto child_oops;
|
|
|
|
|
|
|
|
if (pio->flags & QSE_PIO_OUTTOERR)
|
|
|
|
{
|
|
|
|
if (QSE_DUP2 (pipes[5], 1) <= -1) goto child_oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSE_CLOSE (pipes[5]);
|
|
|
|
pipes[5] = QSE_PIO_HND_NIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pio->flags & QSE_PIO_INTONUL) ||
|
|
|
|
(pio->flags & QSE_PIO_OUTTONUL) ||
|
|
|
|
(pio->flags & QSE_PIO_ERRTONUL))
|
|
|
|
{
|
|
|
|
#if defined(O_LARGEFILE)
|
|
|
|
devnull = QSE_OPEN (QSE_MT("/dev/null"), O_RDWR|O_LARGEFILE, 0);
|
|
|
|
#else
|
|
|
|
devnull = QSE_OPEN (QSE_MT("/dev/null"), O_RDWR, 0);
|
|
|
|
#endif
|
|
|
|
if (devnull <= -1) goto child_oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pio->flags & QSE_PIO_INTONUL) &&
|
|
|
|
QSE_DUP2(devnull,0) <= -1) goto child_oops;
|
|
|
|
if ((pio->flags & QSE_PIO_OUTTONUL) &&
|
|
|
|
QSE_DUP2(devnull,1) <= -1) goto child_oops;
|
|
|
|
if ((pio->flags & QSE_PIO_ERRTONUL) &&
|
|
|
|
QSE_DUP2(devnull,2) <= -1) goto child_oops;
|
|
|
|
|
|
|
|
if ((pio->flags & QSE_PIO_INTONUL) ||
|
|
|
|
(pio->flags & QSE_PIO_OUTTONUL) ||
|
|
|
|
(pio->flags & QSE_PIO_ERRTONUL))
|
|
|
|
{
|
|
|
|
QSE_CLOSE (devnull);
|
|
|
|
devnull = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pio->flags & QSE_PIO_DROPIN) QSE_CLOSE(0);
|
|
|
|
if (pio->flags & QSE_PIO_DROPOUT) QSE_CLOSE(1);
|
|
|
|
if (pio->flags & QSE_PIO_DROPERR) QSE_CLOSE(2);
|
|
|
|
|
|
|
|
|
|
|
|
if (pio->flags & QSE_PIO_FNCCMD)
|
|
|
|
{
|
|
|
|
/* -----------------------------------------------
|
|
|
|
* the function pointer to execute has been given.
|
|
|
|
* -----------------------------------------------*/
|
|
|
|
qse_pio_fnc_t* fnc = (qse_pio_fnc_t*)param;
|
|
|
|
int retx;
|
|
|
|
|
|
|
|
retx = fnc->ptr (fnc->ctx, (env? qse_env_getarr(env): environ));
|
|
|
|
if (devnull >= 0) QSE_CLOSE (devnull);
|
|
|
|
QSE_EXIT (retx);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QSE_EXECVE (param->argv[0], param->argv, (env? qse_env_getarr(env): environ));
|
|
|
|
|
|
|
|
/* if exec fails, free 'param' parameter which is an inherited pointer */
|
|
|
|
free_param (pio, param);
|
|
|
|
}
|
|
|
|
|
|
|
|
child_oops:
|
|
|
|
if (devnull >= 0) QSE_CLOSE (devnull);
|
|
|
|
QSE_EXIT (128);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2012-02-01 14:32:34 +00:00
|
|
|
#endif
|
|
|
|
|
2014-07-25 17:28:20 +00:00
|
|
|
static int set_pipe_nonblock (qse_pio_t* pio, qse_pio_hnd_t fd, int enabled)
|
|
|
|
{
|
|
|
|
#if defined(O_NONBLOCK)
|
|
|
|
|
|
|
|
int flag = QSE_FCNTL (fd, F_GETFL, 0);
|
|
|
|
if (flag >= 0) flag = QSE_FCNTL (fd, F_SETFL, (enabled? (flag | O_NONBLOCK): (flag & ~O_NONBLOCK)));
|
|
|
|
if (flag <= -1) pio->errnum = syserr_to_errnum (errno);
|
|
|
|
return flag;
|
|
|
|
|
|
|
|
#else
|
|
|
|
pio->errnum = QSE_PIO_ENOIMPL;
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-01 09:43:46 +00:00
|
|
|
int qse_pio_init (
|
2011-08-10 10:08:26 +00:00
|
|
|
qse_pio_t* pio, qse_mmgr_t* mmgr, const qse_char_t* cmd,
|
2011-12-28 14:26:02 +00:00
|
|
|
qse_env_t* env, int flags)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2014-11-17 17:18:11 +00:00
|
|
|
qse_pio_hnd_t handle[6] /*=
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2009-01-29 08:50:30 +00:00
|
|
|
QSE_PIO_HND_NIL,
|
|
|
|
QSE_PIO_HND_NIL,
|
|
|
|
QSE_PIO_HND_NIL,
|
|
|
|
QSE_PIO_HND_NIL,
|
|
|
|
QSE_PIO_HND_NIL,
|
|
|
|
QSE_PIO_HND_NIL
|
2014-11-17 17:18:11 +00:00
|
|
|
}*/;
|
2009-07-23 06:47:13 +00:00
|
|
|
|
2014-11-17 17:18:11 +00:00
|
|
|
qse_tio_t* tio[3] /*=
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
|
|
|
QSE_NULL,
|
|
|
|
QSE_NULL,
|
|
|
|
QSE_NULL
|
2014-11-17 17:18:11 +00:00
|
|
|
}*/;
|
2009-07-23 06:47:13 +00:00
|
|
|
|
2009-01-15 03:58:27 +00:00
|
|
|
int i, minidx = -1, maxidx = -1;
|
|
|
|
|
2011-03-16 09:20:03 +00:00
|
|
|
#if defined(_WIN32)
|
2009-06-09 07:09:01 +00:00
|
|
|
SECURITY_ATTRIBUTES secattr;
|
2009-07-23 06:47:13 +00:00
|
|
|
PROCESS_INFORMATION procinfo;
|
|
|
|
STARTUPINFO startup;
|
2009-07-24 07:01:52 +00:00
|
|
|
HANDLE windevnul = INVALID_HANDLE_VALUE;
|
2012-02-19 14:38:22 +00:00
|
|
|
BOOL apiret;
|
|
|
|
qse_char_t* dupcmd;
|
|
|
|
int create_retried;
|
2011-05-10 10:11:13 +00:00
|
|
|
|
2011-03-17 02:37:06 +00:00
|
|
|
#elif defined(__OS2__)
|
2011-03-19 09:07:21 +00:00
|
|
|
APIRET rc;
|
|
|
|
ULONG pipe_size = 4096;
|
2011-03-21 08:16:54 +00:00
|
|
|
UCHAR load_error[CCHMAXPATH];
|
2011-03-19 09:07:21 +00:00
|
|
|
RESULTCODES child_rc;
|
2011-03-21 08:16:54 +00:00
|
|
|
HFILE old_in = QSE_PIO_HND_NIL;
|
|
|
|
HFILE old_out = QSE_PIO_HND_NIL;
|
|
|
|
HFILE old_err = QSE_PIO_HND_NIL;
|
|
|
|
HFILE std_in = 0, std_out = 1, std_err = 2;
|
|
|
|
qse_mchar_t* cmd_line = QSE_NULL;
|
|
|
|
qse_mchar_t* cmd_file;
|
|
|
|
HFILE os2devnul = (HFILE)-1;
|
2011-05-10 10:11:13 +00:00
|
|
|
|
2011-05-07 09:28:27 +00:00
|
|
|
#elif defined(__DOS__)
|
2011-08-05 09:43:28 +00:00
|
|
|
|
|
|
|
/* DOS not multi-processed. can't support pio */
|
2011-05-10 10:11:13 +00:00
|
|
|
|
2013-09-11 09:14:29 +00:00
|
|
|
#elif defined(HAVE_POSIX_SPAWN) && !(defined(QSE_SYSCALL0) && defined(SYS_vfork))
|
2012-02-01 14:32:34 +00:00
|
|
|
posix_spawn_file_actions_t fa;
|
|
|
|
int fa_inited = 0;
|
2012-02-19 14:38:22 +00:00
|
|
|
int pserr;
|
2012-03-26 14:11:11 +00:00
|
|
|
posix_spawnattr_t psattr;
|
|
|
|
qse_pio_pid_t pid;
|
|
|
|
param_t param;
|
2013-01-17 14:13:48 +00:00
|
|
|
#if defined(HAVE_CRT_EXTERNS_H)
|
|
|
|
#define environ (*(_NSGetEnviron()))
|
|
|
|
#else
|
2012-03-26 14:11:11 +00:00
|
|
|
extern char** environ;
|
2013-01-17 14:13:48 +00:00
|
|
|
#endif
|
2012-03-26 14:11:11 +00:00
|
|
|
#elif defined(QSE_SYSCALL0) && defined(SYS_vfork)
|
2012-02-01 14:32:34 +00:00
|
|
|
qse_pio_pid_t pid;
|
|
|
|
param_t param;
|
2013-01-17 14:13:48 +00:00
|
|
|
#if defined(HAVE_CRT_EXTERNS_H)
|
|
|
|
#define environ (*(_NSGetEnviron()))
|
|
|
|
#else
|
2012-02-01 14:32:34 +00:00
|
|
|
extern char** environ;
|
2013-01-17 14:13:48 +00:00
|
|
|
#endif
|
2012-03-26 14:11:11 +00:00
|
|
|
char** envarr;
|
|
|
|
int highest_fd;
|
|
|
|
int dummy;
|
2009-07-24 07:01:52 +00:00
|
|
|
#else
|
|
|
|
qse_pio_pid_t pid;
|
2012-02-01 14:32:34 +00:00
|
|
|
param_t param;
|
2013-01-17 14:13:48 +00:00
|
|
|
#if defined(HAVE_CRT_EXTERNS_H)
|
|
|
|
#define environ (*(_NSGetEnviron()))
|
|
|
|
#else
|
2012-02-01 14:32:34 +00:00
|
|
|
extern char** environ;
|
2013-01-17 14:13:48 +00:00
|
|
|
#endif
|
2009-06-09 07:09:01 +00:00
|
|
|
#endif
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
QSE_MEMSET (pio, 0, QSE_SIZEOF(*pio));
|
|
|
|
pio->mmgr = mmgr;
|
2012-04-27 14:33:14 +00:00
|
|
|
pio->flags = flags;
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2014-11-17 17:18:11 +00:00
|
|
|
handle[0] = QSE_PIO_HND_NIL;
|
|
|
|
handle[1] = QSE_PIO_HND_NIL;
|
|
|
|
handle[2] = QSE_PIO_HND_NIL;
|
|
|
|
handle[3] = QSE_PIO_HND_NIL;
|
|
|
|
handle[4] = QSE_PIO_HND_NIL;
|
|
|
|
handle[5] = QSE_PIO_HND_NIL;
|
|
|
|
|
|
|
|
tio[0] = QSE_NULL;
|
|
|
|
tio[1] = QSE_NULL;
|
|
|
|
tio[2] = QSE_NULL;
|
|
|
|
|
2011-03-19 09:07:21 +00:00
|
|
|
#if defined(_WIN32)
|
2009-06-09 07:09:01 +00:00
|
|
|
/* http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx */
|
|
|
|
|
|
|
|
secattr.nLength = QSE_SIZEOF(secattr);
|
|
|
|
secattr.bInheritHandle = TRUE;
|
2009-07-24 07:01:52 +00:00
|
|
|
secattr.lpSecurityDescriptor = QSE_NULL;
|
2009-06-09 07:09:01 +00:00
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_WRITEIN)
|
2009-06-09 07:09:01 +00:00
|
|
|
{
|
2009-07-23 06:47:13 +00:00
|
|
|
/* child reads, parent writes */
|
2012-02-13 14:43:50 +00:00
|
|
|
if (CreatePipe (&handle[0], &handle[1], &secattr, 0) == FALSE)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (GetLastError());
|
|
|
|
goto oops;
|
|
|
|
}
|
2009-06-09 07:09:01 +00:00
|
|
|
|
2009-07-23 06:47:13 +00:00
|
|
|
/* don't inherit write handle */
|
2012-02-13 14:43:50 +00:00
|
|
|
if (SetHandleInformation (handle[1], HANDLE_FLAG_INHERIT, 0) == FALSE)
|
|
|
|
{
|
2012-02-19 14:38:22 +00:00
|
|
|
DWORD e = GetLastError();
|
|
|
|
if (e != ERROR_CALL_NOT_IMPLEMENTED)
|
|
|
|
{
|
|
|
|
/* SetHandleInformation() is not implemented on win9x.
|
|
|
|
* so let's care only if it is implemented */
|
|
|
|
pio->errnum = syserr_to_errnum (e);
|
|
|
|
goto oops;
|
|
|
|
}
|
2012-02-13 14:43:50 +00:00
|
|
|
}
|
2009-06-09 07:09:01 +00:00
|
|
|
|
|
|
|
minidx = 0; maxidx = 1;
|
|
|
|
}
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_READOUT)
|
2009-06-09 07:09:01 +00:00
|
|
|
{
|
2009-07-23 06:47:13 +00:00
|
|
|
/* child writes, parent reads */
|
2012-02-13 14:43:50 +00:00
|
|
|
if (CreatePipe (&handle[2], &handle[3], &secattr, 0) == FALSE)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (GetLastError());
|
|
|
|
goto oops;
|
|
|
|
}
|
2009-06-09 07:09:01 +00:00
|
|
|
|
2009-07-23 06:47:13 +00:00
|
|
|
/* don't inherit read handle */
|
2012-02-13 14:43:50 +00:00
|
|
|
if (SetHandleInformation (handle[2], HANDLE_FLAG_INHERIT, 0) == FALSE)
|
|
|
|
{
|
2012-02-19 14:38:22 +00:00
|
|
|
DWORD e = GetLastError();
|
|
|
|
if (e != ERROR_CALL_NOT_IMPLEMENTED)
|
|
|
|
{
|
|
|
|
/* SetHandleInformation() is not implemented on win9x.
|
|
|
|
* so let's care only if it is implemented */
|
|
|
|
pio->errnum = syserr_to_errnum (e);
|
|
|
|
goto oops;
|
|
|
|
}
|
2012-02-13 14:43:50 +00:00
|
|
|
}
|
2009-06-09 07:09:01 +00:00
|
|
|
|
|
|
|
if (minidx == -1) minidx = 2;
|
|
|
|
maxidx = 3;
|
|
|
|
}
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_READERR)
|
2009-06-09 07:09:01 +00:00
|
|
|
{
|
2009-07-23 06:47:13 +00:00
|
|
|
/* child writes, parent reads */
|
2012-02-13 14:43:50 +00:00
|
|
|
if (CreatePipe (&handle[4], &handle[5], &secattr, 0) == FALSE)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (GetLastError());
|
|
|
|
goto oops;
|
|
|
|
}
|
2009-06-09 07:09:01 +00:00
|
|
|
|
2009-07-23 06:47:13 +00:00
|
|
|
/* don't inherit read handle */
|
2012-02-13 14:43:50 +00:00
|
|
|
if (SetHandleInformation (handle[4], HANDLE_FLAG_INHERIT, 0) == FALSE)
|
|
|
|
{
|
2012-02-19 14:38:22 +00:00
|
|
|
DWORD e = GetLastError();
|
|
|
|
if (e != ERROR_CALL_NOT_IMPLEMENTED)
|
|
|
|
{
|
|
|
|
/* SetHandleInformation() is not implemented on win9x.
|
|
|
|
* so let's care only if it is implemented */
|
|
|
|
pio->errnum = syserr_to_errnum (e);
|
|
|
|
goto oops;
|
|
|
|
}
|
2012-02-13 14:43:50 +00:00
|
|
|
}
|
2009-06-09 07:09:01 +00:00
|
|
|
|
|
|
|
if (minidx == -1) minidx = 4;
|
|
|
|
maxidx = 5;
|
|
|
|
}
|
|
|
|
|
2011-09-01 09:43:46 +00:00
|
|
|
if (maxidx == -1)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_EINVAL;
|
|
|
|
goto oops;
|
|
|
|
}
|
2011-03-19 09:07:21 +00:00
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if ((flags & QSE_PIO_INTONUL) ||
|
|
|
|
(flags & QSE_PIO_OUTTONUL) ||
|
|
|
|
(flags & QSE_PIO_ERRTONUL))
|
2009-07-24 07:01:52 +00:00
|
|
|
{
|
|
|
|
windevnul = CreateFile(
|
|
|
|
QSE_T("NUL"), GENERIC_READ | GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
&secattr, OPEN_EXISTING, 0, NULL
|
|
|
|
);
|
2012-02-13 14:43:50 +00:00
|
|
|
if (windevnul == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (GetLastError());
|
|
|
|
goto oops;
|
|
|
|
}
|
2009-07-24 07:01:52 +00:00
|
|
|
}
|
2009-07-23 06:47:13 +00:00
|
|
|
|
|
|
|
QSE_MEMSET (&procinfo, 0, QSE_SIZEOF(procinfo));
|
|
|
|
QSE_MEMSET (&startup, 0, QSE_SIZEOF(startup));
|
|
|
|
|
|
|
|
startup.cb = QSE_SIZEOF(startup);
|
2009-07-24 07:01:52 +00:00
|
|
|
|
2009-12-15 06:50:11 +00:00
|
|
|
/*
|
2009-07-24 07:01:52 +00:00
|
|
|
startup.hStdInput = INVALID_HANDLE_VALUE;
|
|
|
|
startup.hStdOutput = INVALID_HANDLE_VALUE;
|
|
|
|
startup.hStdOutput = INVALID_HANDLE_VALUE;
|
2009-12-15 06:50:11 +00:00
|
|
|
*/
|
|
|
|
|
2011-03-16 09:20:03 +00:00
|
|
|
startup.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
|
|
|
|
startup.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
|
|
startup.hStdOutput = GetStdHandle (STD_ERROR_HANDLE);
|
2009-12-15 06:50:11 +00:00
|
|
|
if (startup.hStdInput == INVALID_HANDLE_VALUE ||
|
|
|
|
startup.hStdOutput == INVALID_HANDLE_VALUE ||
|
2012-02-19 14:38:22 +00:00
|
|
|
startup.hStdError == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (GetLastError());
|
|
|
|
goto oops;
|
|
|
|
}
|
2009-07-24 07:01:52 +00:00
|
|
|
|
2012-02-19 14:38:22 +00:00
|
|
|
if (flags & QSE_PIO_WRITEIN)
|
|
|
|
{
|
|
|
|
startup.hStdInput = handle[0];
|
|
|
|
}
|
2009-07-24 07:01:52 +00:00
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_READOUT)
|
2009-07-24 07:01:52 +00:00
|
|
|
{
|
|
|
|
startup.hStdOutput = handle[3];
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_ERRTOOUT) startup.hStdError = handle[3];
|
2009-07-24 07:01:52 +00:00
|
|
|
}
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_READERR)
|
2009-07-24 07:01:52 +00:00
|
|
|
{
|
|
|
|
startup.hStdError = handle[5];
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_OUTTOERR) startup.hStdOutput = handle[5];
|
2009-07-24 07:01:52 +00:00
|
|
|
}
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_INTONUL) startup.hStdInput = windevnul;
|
|
|
|
if (flags & QSE_PIO_OUTTONUL) startup.hStdOutput = windevnul;
|
|
|
|
if (flags & QSE_PIO_ERRTONUL) startup.hStdError = windevnul;
|
2009-07-24 07:01:52 +00:00
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_DROPIN) startup.hStdInput = INVALID_HANDLE_VALUE;
|
|
|
|
if (flags & QSE_PIO_DROPOUT) startup.hStdOutput = INVALID_HANDLE_VALUE;
|
|
|
|
if (flags & QSE_PIO_DROPERR) startup.hStdError = INVALID_HANDLE_VALUE;
|
2009-07-24 07:01:52 +00:00
|
|
|
|
2009-07-23 06:47:13 +00:00
|
|
|
startup.dwFlags |= STARTF_USESTDHANDLES;
|
|
|
|
|
|
|
|
/* there is nothing to do for QSE_PIO_SHELL as CreateProcess
|
|
|
|
* takes the entire command line */
|
|
|
|
|
2012-02-19 14:38:22 +00:00
|
|
|
create_retried = 0;
|
|
|
|
|
|
|
|
create_process:
|
|
|
|
if (flags & QSE_PIO_SHELL)
|
2009-07-24 07:01:52 +00:00
|
|
|
{
|
2012-02-19 14:38:22 +00:00
|
|
|
static const qse_char_t* cmdname[] =
|
|
|
|
{
|
|
|
|
QSE_T("cmd.exe /c "),
|
|
|
|
QSE_T("command.com /c ")
|
|
|
|
};
|
|
|
|
static const qse_mchar_t* mcmdname[] =
|
|
|
|
{
|
|
|
|
QSE_MT("cmd.exe /c "),
|
|
|
|
QSE_MT("command.com /c ")
|
|
|
|
};
|
2009-07-24 07:01:52 +00:00
|
|
|
|
2012-02-19 14:38:22 +00:00
|
|
|
#if defined(QSE_CHAR_IS_WCHAR)
|
|
|
|
if (flags & QSE_PIO_MBSCMD)
|
2011-08-11 10:18:35 +00:00
|
|
|
{
|
2012-02-19 14:38:22 +00:00
|
|
|
const qse_mchar_t* x[3];
|
|
|
|
x[0] = mcmdname[create_retried];
|
|
|
|
x[1] = (const qse_mchar_t*)cmd;
|
|
|
|
x[2] = QSE_NULL;
|
2012-10-18 06:52:03 +00:00
|
|
|
dupcmd = qse_mbsatowcsdup (x, QSE_NULL, mmgr);
|
2011-08-12 09:11:02 +00:00
|
|
|
}
|
2012-02-19 14:38:22 +00:00
|
|
|
else
|
2011-08-12 09:11:02 +00:00
|
|
|
{
|
2012-02-19 14:38:22 +00:00
|
|
|
#endif
|
|
|
|
dupcmd = qse_strdup2 (cmdname[create_retried], cmd, mmgr);
|
|
|
|
#if defined(QSE_CHAR_IS_WCHAR)
|
2011-12-21 02:02:05 +00:00
|
|
|
}
|
2012-02-19 14:38:22 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(QSE_CHAR_IS_WCHAR)
|
|
|
|
if (flags & QSE_PIO_MBSCMD)
|
2011-12-21 02:02:05 +00:00
|
|
|
{
|
2012-10-18 06:52:03 +00:00
|
|
|
dupcmd = qse_mbstowcsdup ((const qse_mchar_t*)cmd, QSE_NULL, mmgr);
|
2011-08-11 10:18:35 +00:00
|
|
|
}
|
2012-02-19 14:38:22 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
/* CreateProcess requires command buffer to be read-write. */
|
|
|
|
dupcmd = qse_strdup (cmd, mmgr);
|
|
|
|
#if defined(QSE_CHAR_IS_WCHAR)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2011-08-11 10:18:35 +00:00
|
|
|
|
2012-02-19 14:38:22 +00:00
|
|
|
if (dupcmd == QSE_NULL)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_ENOMEM;
|
|
|
|
goto oops;
|
|
|
|
}
|
2011-08-11 10:18:35 +00:00
|
|
|
|
2012-02-19 14:38:22 +00:00
|
|
|
apiret = CreateProcess (
|
|
|
|
QSE_NULL, /* LPCTSTR lpApplicationName */
|
|
|
|
dupcmd, /* LPTSTR lpCommandLine */
|
|
|
|
QSE_NULL, /* LPSECURITY_ATTRIBUTES lpProcessAttributes */
|
|
|
|
QSE_NULL, /* LPSECURITY_ATTRIBUTES lpThreadAttributes */
|
|
|
|
TRUE, /* BOOL bInheritHandles */
|
2014-11-14 02:44:20 +00:00
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
2012-02-19 14:38:22 +00:00
|
|
|
0, /* DWORD dwCreationFlags */
|
|
|
|
#else
|
|
|
|
CREATE_UNICODE_ENVIRONMENT, /* DWORD dwCreationFlags */
|
|
|
|
#endif
|
|
|
|
(env? qse_env_getstr(env): QSE_NULL), /* LPVOID lpEnvironment */
|
|
|
|
QSE_NULL, /* LPCTSTR lpCurrentDirectory */
|
|
|
|
&startup, /* LPSTARTUPINFO lpStartupInfo */
|
|
|
|
&procinfo /* LPPROCESS_INFORMATION lpProcessInformation */
|
|
|
|
);
|
|
|
|
|
|
|
|
QSE_MMGR_FREE (mmgr, dupcmd);
|
|
|
|
if (apiret == FALSE)
|
|
|
|
{
|
|
|
|
DWORD e = GetLastError();
|
|
|
|
if (create_retried == 0 && (flags & QSE_PIO_SHELL) &&
|
|
|
|
e == ERROR_FILE_NOT_FOUND)
|
|
|
|
{
|
|
|
|
/* if it failed to exeucte cmd.exe,
|
|
|
|
* attempt to execute command.com.
|
|
|
|
* this is provision for old windows platforms */
|
|
|
|
create_retried = 1;
|
|
|
|
goto create_process;
|
|
|
|
}
|
|
|
|
|
|
|
|
pio->errnum = syserr_to_errnum (e);
|
|
|
|
goto oops;
|
2011-08-11 10:18:35 +00:00
|
|
|
}
|
2009-07-23 06:47:13 +00:00
|
|
|
|
2011-03-21 08:16:54 +00:00
|
|
|
if (windevnul != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
CloseHandle (windevnul);
|
|
|
|
windevnul = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
2009-07-24 07:01:52 +00:00
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_WRITEIN)
|
2009-07-23 06:47:13 +00:00
|
|
|
{
|
|
|
|
CloseHandle (handle[0]);
|
|
|
|
handle[0] = QSE_PIO_HND_NIL;
|
|
|
|
}
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_READOUT)
|
2009-07-23 06:47:13 +00:00
|
|
|
{
|
|
|
|
CloseHandle (handle[3]);
|
|
|
|
handle[3] = QSE_PIO_HND_NIL;
|
|
|
|
}
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_READERR)
|
2009-07-23 06:47:13 +00:00
|
|
|
{
|
|
|
|
CloseHandle (handle[5]);
|
|
|
|
handle[5] = QSE_PIO_HND_NIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle (procinfo.hThread);
|
2011-03-17 02:37:06 +00:00
|
|
|
pio->child = procinfo.hProcess;
|
2011-03-19 09:07:21 +00:00
|
|
|
|
2011-03-17 02:37:06 +00:00
|
|
|
#elif defined(__OS2__)
|
2011-03-19 09:07:21 +00:00
|
|
|
|
2011-03-22 08:01:10 +00:00
|
|
|
#define DOS_DUP_HANDLE(x,y) QSE_BLOCK ( \
|
2012-02-13 14:43:50 +00:00
|
|
|
rc = DosDupHandle(x,y); \
|
|
|
|
if (rc != NO_ERROR) \
|
|
|
|
{ \
|
|
|
|
pio->errnum = syserr_to_errnum (rc); \
|
|
|
|
goto oops; \
|
|
|
|
} \
|
2011-03-22 08:01:10 +00:00
|
|
|
)
|
2011-03-21 08:16:54 +00:00
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_WRITEIN)
|
2011-03-19 09:07:21 +00:00
|
|
|
{
|
|
|
|
/* child reads, parent writes */
|
2012-02-13 14:43:50 +00:00
|
|
|
rc = DosCreatePipe (&handle[0], &handle[1], pipe_size);
|
|
|
|
if (rc != NO_ERROR)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (rc);
|
|
|
|
goto oops;
|
|
|
|
}
|
2011-03-19 09:07:21 +00:00
|
|
|
|
2011-03-21 08:16:54 +00:00
|
|
|
/* the parent writes to handle[1] and the child reads from
|
|
|
|
* handle[0] inherited. set the flag not to inherit handle[1]. */
|
2012-02-13 14:43:50 +00:00
|
|
|
rc = DosSetFHState (handle[1], OPEN_FLAGS_NOINHERIT);
|
|
|
|
if (rc != NO_ERROR)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (rc);
|
|
|
|
goto oops;
|
|
|
|
}
|
2011-03-21 08:16:54 +00:00
|
|
|
|
2011-03-23 20:21:14 +00:00
|
|
|
/* Need to do somthing like this to set the flag instead?
|
2011-03-22 08:01:10 +00:00
|
|
|
ULONG state;
|
2011-03-21 08:16:54 +00:00
|
|
|
DosQueryFHState (handle[1], &state);
|
|
|
|
DosSetFHState (handle[1], state | OPEN_FLAGS_NOINHERIT); */
|
2011-03-19 09:07:21 +00:00
|
|
|
|
|
|
|
minidx = 0; maxidx = 1;
|
|
|
|
}
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_READOUT)
|
2011-03-19 09:07:21 +00:00
|
|
|
{
|
|
|
|
/* child writes, parent reads */
|
2012-02-13 14:43:50 +00:00
|
|
|
rc = DosCreatePipe (&handle[2], &handle[3], pipe_size);
|
|
|
|
if (rc != NO_ERROR)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (rc);
|
|
|
|
goto oops;
|
|
|
|
}
|
2011-03-19 09:07:21 +00:00
|
|
|
|
2011-03-21 08:16:54 +00:00
|
|
|
/* the parent reads from handle[2] and the child writes to
|
|
|
|
* handle[3] inherited. set the flag not to inherit handle[2] */
|
2012-02-13 14:43:50 +00:00
|
|
|
rc = DosSetFHState (handle[2], OPEN_FLAGS_NOINHERIT);
|
|
|
|
if (rc != NO_ERROR)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (rc);
|
|
|
|
goto oops;
|
|
|
|
}
|
2011-03-19 09:07:21 +00:00
|
|
|
|
|
|
|
if (minidx == -1) minidx = 2;
|
|
|
|
maxidx = 3;
|
|
|
|
}
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_READERR)
|
2011-03-19 09:07:21 +00:00
|
|
|
{
|
|
|
|
/* child writes, parent reads */
|
2012-02-13 14:43:50 +00:00
|
|
|
rc = DosCreatePipe (&handle[4], &handle[5], pipe_size);
|
|
|
|
if (rc != NO_ERROR)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (rc);
|
|
|
|
goto oops;
|
|
|
|
}
|
2011-03-19 09:07:21 +00:00
|
|
|
|
2011-03-21 08:16:54 +00:00
|
|
|
/* the parent reads from handle[4] and the child writes to
|
|
|
|
* handle[5] inherited. set the flag not to inherit handle[4] */
|
2012-02-13 14:43:50 +00:00
|
|
|
rc = DosSetFHState (handle[4], OPEN_FLAGS_NOINHERIT);
|
|
|
|
if (rc != NO_ERROR)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (rc);
|
|
|
|
goto oops;
|
|
|
|
}
|
2011-03-19 09:07:21 +00:00
|
|
|
|
|
|
|
if (minidx == -1) minidx = 4;
|
|
|
|
maxidx = 5;
|
|
|
|
}
|
|
|
|
|
2011-09-01 09:43:46 +00:00
|
|
|
if (maxidx == -1)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_EINVAL;
|
|
|
|
goto oops;
|
|
|
|
}
|
2011-03-19 09:07:21 +00:00
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if ((flags & QSE_PIO_INTONUL) ||
|
|
|
|
(flags & QSE_PIO_OUTTONUL) ||
|
|
|
|
(flags & QSE_PIO_ERRTONUL))
|
2011-03-21 08:16:54 +00:00
|
|
|
{
|
|
|
|
ULONG action_taken;
|
2012-12-18 15:12:53 +00:00
|
|
|
/*
|
2011-03-21 08:16:54 +00:00
|
|
|
LONGLONG zero;
|
|
|
|
|
|
|
|
zero.ulLo = 0;
|
|
|
|
zero.ulHi = 0;
|
2012-12-18 15:12:53 +00:00
|
|
|
*/
|
2011-03-21 08:16:54 +00:00
|
|
|
|
2012-12-18 08:12:15 +00:00
|
|
|
/* TODO: selective between DosOpenL and DosOpen */
|
2012-12-18 15:12:53 +00:00
|
|
|
rc = DosOpen /*DosOpenL*/ (
|
2011-03-21 08:16:54 +00:00
|
|
|
QSE_MT("NUL"),
|
|
|
|
&os2devnul,
|
|
|
|
&action_taken,
|
2012-12-18 15:12:53 +00:00
|
|
|
0, /*zero,*/
|
2011-03-21 08:16:54 +00:00
|
|
|
FILE_NORMAL,
|
|
|
|
OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
|
|
|
|
OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE,
|
|
|
|
0L
|
|
|
|
);
|
2012-02-13 14:43:50 +00:00
|
|
|
if (rc != NO_ERROR)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (rc);
|
|
|
|
goto oops;
|
|
|
|
}
|
2011-03-21 08:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* duplicate the current stdin/out/err to old_in/out/err as a new handle */
|
|
|
|
|
2012-02-13 14:43:50 +00:00
|
|
|
rc = DosDupHandle (std_in, &old_in);
|
|
|
|
if (rc != NO_ERROR)
|
2011-03-21 08:16:54 +00:00
|
|
|
{
|
2012-02-13 14:43:50 +00:00
|
|
|
pio->errnum = syserr_to_errnum (rc);
|
2011-03-21 08:16:54 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
2012-02-13 14:43:50 +00:00
|
|
|
rc = DosDupHandle (std_out, &old_out);
|
|
|
|
if (rc != NO_ERROR)
|
2011-03-21 08:16:54 +00:00
|
|
|
{
|
2012-02-13 14:43:50 +00:00
|
|
|
pio->errnum = syserr_to_errnum (rc);
|
2011-03-21 08:16:54 +00:00
|
|
|
DosClose (old_in); old_in = QSE_PIO_HND_NIL;
|
|
|
|
goto oops;
|
|
|
|
}
|
2012-02-13 14:43:50 +00:00
|
|
|
rc = DosDupHandle (std_err, &old_err);
|
|
|
|
if (rc != NO_ERROR)
|
2011-03-21 08:16:54 +00:00
|
|
|
{
|
2012-02-13 14:43:50 +00:00
|
|
|
pio->errnum = syserr_to_errnum (rc);
|
2011-03-21 08:16:54 +00:00
|
|
|
DosClose (old_out); old_out = QSE_PIO_HND_NIL;
|
|
|
|
DosClose (old_in); old_in = QSE_PIO_HND_NIL;
|
|
|
|
goto oops;
|
2014-09-19 14:05:02 +00:00
|
|
|
}
|
2011-03-21 08:16:54 +00:00
|
|
|
|
|
|
|
/* we must not let our own stdin/out/err duplicated
|
|
|
|
* into old_in/out/err be inherited */
|
|
|
|
DosSetFHState (old_in, OPEN_FLAGS_NOINHERIT);
|
|
|
|
DosSetFHState (old_out, OPEN_FLAGS_NOINHERIT);
|
|
|
|
DosSetFHState (old_err, OPEN_FLAGS_NOINHERIT);
|
2011-03-19 09:07:21 +00:00
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_WRITEIN)
|
2011-03-19 09:07:21 +00:00
|
|
|
{
|
2011-03-21 08:16:54 +00:00
|
|
|
/* the child reads from handle[0] inherited and expects it to
|
|
|
|
* be stdin(0). so we duplicate handle[0] to stdin */
|
|
|
|
DOS_DUP_HANDLE (handle[0], &std_in);
|
|
|
|
|
|
|
|
/* the parent writes to handle[1] but does not read from handle[0].
|
|
|
|
* so we close it */
|
|
|
|
DosClose (handle[0]); handle[0] = QSE_PIO_HND_NIL;
|
2011-03-19 09:07:21 +00:00
|
|
|
}
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_READOUT)
|
2011-03-19 09:07:21 +00:00
|
|
|
{
|
2011-03-21 08:16:54 +00:00
|
|
|
/* the child writes to handle[3] inherited and expects it to
|
|
|
|
* be stdout(1). so we duplicate handle[3] to stdout. */
|
|
|
|
DOS_DUP_HANDLE (handle[3], &std_out);
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_ERRTOOUT) DOS_DUP_HANDLE (handle[3], &std_err);
|
2011-03-21 08:16:54 +00:00
|
|
|
/* the parent reads from handle[2] but does not write to handle[3].
|
|
|
|
* so we close it */
|
|
|
|
DosClose (handle[3]); handle[3] = QSE_PIO_HND_NIL;
|
2011-03-19 09:07:21 +00:00
|
|
|
}
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_READERR)
|
2011-03-19 09:07:21 +00:00
|
|
|
{
|
2011-03-21 08:16:54 +00:00
|
|
|
DOS_DUP_HANDLE (handle[5], &std_err);
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_OUTTOERR) DOS_DUP_HANDLE (handle[5], &std_out);
|
2011-03-21 08:16:54 +00:00
|
|
|
DosClose (handle[5]); handle[5] = QSE_PIO_HND_NIL;
|
2011-03-19 09:07:21 +00:00
|
|
|
}
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_INTONUL) DOS_DUP_HANDLE (os2devnul, &std_in);
|
|
|
|
if (flags & QSE_PIO_OUTTONUL) DOS_DUP_HANDLE (os2devnul, &std_out);
|
|
|
|
if (flags & QSE_PIO_ERRTONUL) DOS_DUP_HANDLE (os2devnul, &std_err);
|
2011-03-19 09:07:21 +00:00
|
|
|
|
2011-03-21 08:16:54 +00:00
|
|
|
if (os2devnul != QSE_PIO_HND_NIL)
|
|
|
|
{
|
|
|
|
/* close NUL early as we've duplicated it already */
|
|
|
|
DosClose (os2devnul);
|
|
|
|
os2devnul = QSE_PIO_HND_NIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* at this moment, stdin/out/err are already redirected to pipes
|
|
|
|
* if proper flags have been set. we close them selectively if
|
|
|
|
* dropping is requested */
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_DROPIN) DosClose (std_in);
|
|
|
|
if (flags & QSE_PIO_DROPOUT) DosClose (std_out);
|
|
|
|
if (flags & QSE_PIO_DROPERR) DosClose (std_err);
|
2011-03-22 08:01:10 +00:00
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_SHELL)
|
2011-03-21 08:16:54 +00:00
|
|
|
{
|
|
|
|
qse_size_t n, mn;
|
|
|
|
|
2014-11-14 02:44:20 +00:00
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
2011-03-21 08:16:54 +00:00
|
|
|
mn = qse_strlen(cmd);
|
|
|
|
#else
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_MBSCMD)
|
2011-08-12 09:11:02 +00:00
|
|
|
{
|
|
|
|
mn = qse_mbslen((const qse_mchar_t*)cmd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-21 02:02:05 +00:00
|
|
|
if (qse_wcstombs (cmd, &n, QSE_NULL, &mn) <= -1)
|
2011-09-01 09:43:46 +00:00
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_EINVAL;
|
|
|
|
goto oops; /* illegal sequence found */
|
|
|
|
}
|
2011-08-12 09:11:02 +00:00
|
|
|
}
|
2011-03-21 08:16:54 +00:00
|
|
|
#endif
|
|
|
|
cmd_line = QSE_MMGR_ALLOC (
|
2011-08-12 09:11:02 +00:00
|
|
|
mmgr, ((11+mn+1+1) * QSE_SIZEOF(*cmd_line)));
|
2011-09-01 09:43:46 +00:00
|
|
|
if (cmd_line == QSE_NULL)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_ENOMEM;
|
|
|
|
goto oops;
|
|
|
|
}
|
2011-03-19 09:07:21 +00:00
|
|
|
|
2011-03-21 08:16:54 +00:00
|
|
|
qse_mbscpy (cmd_line, QSE_MT("cmd.exe")); /* cmd.exe\0/c */
|
|
|
|
qse_mbscpy (&cmd_line[8], QSE_MT("/c "));
|
2014-11-14 02:44:20 +00:00
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
2011-03-21 08:16:54 +00:00
|
|
|
qse_mbscpy (&cmd_line[11], cmd);
|
|
|
|
#else
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_MBSCMD)
|
2011-08-12 09:11:02 +00:00
|
|
|
{
|
|
|
|
qse_mbscpy (&cmd_line[11], (const qse_mchar_t*)cmd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mn = mn + 1; /* update the buffer size */
|
2011-12-21 02:02:05 +00:00
|
|
|
qse_wcstombs (cmd, &n, &cmd_line[11], &mn);
|
2011-08-12 09:11:02 +00:00
|
|
|
}
|
2011-03-21 08:16:54 +00:00
|
|
|
#endif
|
|
|
|
cmd_line[11+mn+1] = QSE_MT('\0'); /* additional \0 after \0 */
|
|
|
|
|
|
|
|
cmd_file = QSE_MT("cmd.exe");
|
|
|
|
}
|
|
|
|
else
|
2011-03-23 20:21:14 +00:00
|
|
|
{
|
|
|
|
qse_mchar_t* mptr;
|
2011-08-13 08:39:18 +00:00
|
|
|
qse_size_t mn;
|
2011-03-23 20:21:14 +00:00
|
|
|
|
2014-11-14 02:44:20 +00:00
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
2011-08-13 08:39:18 +00:00
|
|
|
mn = qse_strlen(cmd);
|
2011-03-23 20:21:14 +00:00
|
|
|
cmd_line = qse_strdup2 (cmd, QSE_T(" "), pio->mmgr);
|
2011-09-01 09:43:46 +00:00
|
|
|
if (cmd_line == QSE_NULL)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_ENOMEM;
|
|
|
|
goto oops;
|
|
|
|
}
|
2011-03-23 20:21:14 +00:00
|
|
|
#else
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_MBSCMD)
|
2011-08-12 09:11:02 +00:00
|
|
|
{
|
2011-08-13 08:39:18 +00:00
|
|
|
mn = qse_mbslen((const qse_mchar_t*)cmd);
|
2011-08-12 09:11:02 +00:00
|
|
|
cmd_line = qse_mbsdup2 ((const qse_mchar_t*)cmd, QSE_MT(" "), pio->mmgr);
|
2011-09-01 09:43:46 +00:00
|
|
|
if (cmd_line == QSE_NULL)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_ENOMEM;
|
|
|
|
goto oops;
|
|
|
|
}
|
2011-08-12 09:11:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-13 08:39:18 +00:00
|
|
|
qse_size_t n;
|
|
|
|
|
2011-12-21 02:02:05 +00:00
|
|
|
if (qse_wcstombs (cmd, &n, QSE_NULL, &mn) <= -1)
|
2011-09-01 09:43:46 +00:00
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_EINVAL;
|
|
|
|
goto oops; /* illegal sequence in cmd */
|
|
|
|
}
|
2011-08-12 09:11:02 +00:00
|
|
|
|
|
|
|
mn = mn + 1;
|
|
|
|
cmd_line = QSE_MMGR_ALLOC (pio->mmgr, mn * QSE_SIZEOF(qse_char_t));
|
2011-09-01 09:43:46 +00:00
|
|
|
if (cmd_line == QSE_NULL)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_ENOMEM;
|
|
|
|
goto oops;
|
|
|
|
}
|
2011-03-23 20:21:14 +00:00
|
|
|
|
2011-12-21 02:02:05 +00:00
|
|
|
qse_wcstombs (cmd, &n, cmd_line, &mn);
|
2011-08-12 09:11:02 +00:00
|
|
|
}
|
2011-03-23 20:21:14 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* TODO: enhance this part by:
|
|
|
|
* supporting file names containing whitespaces.
|
|
|
|
* detecting the end of the file name better.
|
|
|
|
* doing better parsing of the command line.
|
|
|
|
*/
|
|
|
|
|
2011-03-24 09:07:24 +00:00
|
|
|
/* NOTE: you must separate the command name and the parameters
|
|
|
|
* with a space. "pstat.exe /c" is ok while "pstat.exe/c"
|
|
|
|
* is not. */
|
|
|
|
mptr = qse_mbspbrk (cmd_line, QSE_MT(" \t"));
|
2011-03-23 20:21:14 +00:00
|
|
|
if (mptr) *mptr = QSE_MT('\0');
|
|
|
|
cmd_line[mn+1] = QSE_MT('\0'); /* the second '\0' at the end */
|
|
|
|
cmd_file = cmd_line;
|
2011-03-21 08:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* execute the command line */
|
2011-03-19 09:07:21 +00:00
|
|
|
rc = DosExecPgm (
|
|
|
|
&load_error,
|
|
|
|
QSE_SIZEOF(load_error),
|
|
|
|
EXEC_ASYNCRESULT,
|
2011-03-21 08:16:54 +00:00
|
|
|
cmd_line,
|
2011-08-13 10:35:34 +00:00
|
|
|
(env? qse_env_getstr(env): QSE_NULL),
|
2011-03-19 09:07:21 +00:00
|
|
|
&child_rc,
|
2011-03-21 08:16:54 +00:00
|
|
|
cmd_file
|
2011-03-19 09:07:21 +00:00
|
|
|
);
|
2011-03-21 08:16:54 +00:00
|
|
|
|
2011-12-21 02:02:05 +00:00
|
|
|
QSE_MMGR_FREE (mmgr, cmd_line);
|
|
|
|
cmd_line = QSE_NULL;
|
|
|
|
|
2011-03-21 08:16:54 +00:00
|
|
|
/* Once execution is completed regardless of success or failure,
|
|
|
|
* Restore stdin/out/err using handles duplicated into old_in/out/err */
|
|
|
|
DosDupHandle (old_in, &std_in); /* I can't do much if this fails */
|
|
|
|
DosClose (old_in); old_in = QSE_PIO_HND_NIL;
|
2011-03-19 09:07:21 +00:00
|
|
|
DosDupHandle (old_out, &std_out);
|
2011-03-21 08:16:54 +00:00
|
|
|
DosClose (old_out); old_out = QSE_PIO_HND_NIL;
|
2011-03-19 09:07:21 +00:00
|
|
|
DosDupHandle (old_err, &std_err);
|
2011-03-21 08:16:54 +00:00
|
|
|
DosClose (old_err); old_err = QSE_PIO_HND_NIL;
|
2011-03-19 09:07:21 +00:00
|
|
|
|
2012-02-13 14:43:50 +00:00
|
|
|
if (rc != NO_ERROR)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (rc);
|
|
|
|
goto oops;
|
|
|
|
}
|
2011-03-19 09:07:21 +00:00
|
|
|
pio->child = child_rc.codeTerminate;
|
2011-05-10 10:11:13 +00:00
|
|
|
|
|
|
|
#elif defined(__DOS__)
|
2014-09-19 14:05:02 +00:00
|
|
|
|
2011-08-05 09:43:28 +00:00
|
|
|
/* DOS not multi-processed. can't support pio */
|
2012-02-19 14:38:22 +00:00
|
|
|
pio->errnum = QSE_PIO_ENOIMPL;
|
2011-09-01 09:43:46 +00:00
|
|
|
return -1;
|
2011-05-10 10:11:13 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
#else
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_WRITEIN)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2012-02-13 14:43:50 +00:00
|
|
|
if (QSE_PIPE(&handle[0]) <= -1)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (errno);
|
|
|
|
goto oops;
|
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
minidx = 0; maxidx = 1;
|
|
|
|
}
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_READOUT)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2012-02-13 14:43:50 +00:00
|
|
|
if (QSE_PIPE(&handle[2]) <= -1)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (errno);
|
|
|
|
goto oops;
|
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
if (minidx == -1) minidx = 2;
|
|
|
|
maxidx = 3;
|
|
|
|
}
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_READERR)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2012-02-13 14:43:50 +00:00
|
|
|
if (QSE_PIPE(&handle[4]) <= -1)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (errno);
|
|
|
|
goto oops;
|
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
if (minidx == -1) minidx = 4;
|
|
|
|
maxidx = 5;
|
|
|
|
}
|
|
|
|
|
2011-09-01 09:43:46 +00:00
|
|
|
if (maxidx == -1)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_EINVAL;
|
|
|
|
goto oops;
|
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (pio->flags & QSE_PIO_FNCCMD)
|
2012-02-19 14:38:22 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
/* i know i'm abusing typecasting here.
|
|
|
|
* cmd is supposed to be qse_pio_fnc_t*, anyway */
|
|
|
|
pid = standard_fork_and_exec (pio, handle, (param_t*)cmd, env);
|
|
|
|
if (pid <= -1) goto oops;
|
|
|
|
pio->child = pid;
|
2012-02-19 14:38:22 +00:00
|
|
|
}
|
2014-09-19 14:05:02 +00:00
|
|
|
else
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
#if defined(HAVE_POSIX_SPAWN) && !(defined(QSE_SYSCALL0) && defined(SYS_vfork))
|
2012-02-01 14:32:34 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if ((pserr = posix_spawn_file_actions_init (&fa)) != 0)
|
2012-02-19 14:38:22 +00:00
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
|
|
|
}
|
2014-09-19 14:05:02 +00:00
|
|
|
fa_inited = 1;
|
2012-02-01 14:32:34 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (flags & QSE_PIO_WRITEIN)
|
2012-02-19 14:38:22 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
/* child should read */
|
|
|
|
if ((pserr = posix_spawn_file_actions_addclose (&fa, handle[1])) != 0)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
if ((pserr = posix_spawn_file_actions_adddup2 (&fa, handle[0], 0)) != 0)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
if ((pserr = posix_spawn_file_actions_addclose (&fa, handle[0])) != 0)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
|
|
|
}
|
2012-02-19 14:38:22 +00:00
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (flags & QSE_PIO_READOUT)
|
2012-02-19 14:38:22 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
/* child should write */
|
|
|
|
if ((pserr = posix_spawn_file_actions_addclose (&fa, handle[2])) != 0)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
if ((pserr = posix_spawn_file_actions_adddup2 (&fa, handle[3], 1)) != 0)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
if ((flags & QSE_PIO_ERRTOOUT) &&
|
|
|
|
(pserr = posix_spawn_file_actions_adddup2 (&fa, handle[3], 2)) != 0)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
if ((pserr = posix_spawn_file_actions_addclose (&fa, handle[3])) != 0)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
|
|
|
}
|
2012-02-19 14:38:22 +00:00
|
|
|
}
|
2009-06-12 01:44:44 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (flags & QSE_PIO_READERR)
|
2009-06-12 01:44:44 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
/* child should write */
|
|
|
|
if ((pserr = posix_spawn_file_actions_addclose (&fa, handle[4])) != 0)
|
2012-02-19 14:38:22 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
if ((pserr = posix_spawn_file_actions_adddup2 (&fa, handle[5], 2)) != 0)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
if ((flags & QSE_PIO_OUTTOERR) &&
|
|
|
|
(pserr = posix_spawn_file_actions_adddup2 (&fa, handle[5], 1)) != 0)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
if ((pserr = posix_spawn_file_actions_addclose (&fa, handle[5])) != 0)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
2012-02-19 14:38:22 +00:00
|
|
|
}
|
2009-06-12 01:44:44 +00:00
|
|
|
}
|
2012-02-01 14:32:34 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
{
|
|
|
|
int oflags = O_RDWR;
|
|
|
|
#if defined(O_LARGEFILE)
|
|
|
|
oflags |= O_LARGEFILE;
|
|
|
|
#endif
|
2012-02-01 14:32:34 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if ((flags & QSE_PIO_INTONUL) &&
|
|
|
|
(pserr = posix_spawn_file_actions_addopen (&fa, 0, QSE_MT("/dev/null"), oflags, 0)) != 0)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
if ((flags & QSE_PIO_OUTTONUL) &&
|
|
|
|
(pserr = posix_spawn_file_actions_addopen (&fa, 1, QSE_MT("/dev/null"), oflags, 0)) != 0)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
if ((flags & QSE_PIO_ERRTONUL) &&
|
|
|
|
(pserr = posix_spawn_file_actions_addopen (&fa, 2, QSE_MT("/dev/null"), oflags, 0)) != 0)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
}
|
2012-02-01 14:32:34 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
/* there remains the chance of race condition that
|
|
|
|
* 0, 1, 2 can be closed between addclose() and posix_spawn().
|
|
|
|
* so checking the file descriptors with is_fd_valid() is
|
|
|
|
* just on the best-effort basis.
|
|
|
|
*/
|
|
|
|
if ((flags & QSE_PIO_DROPIN) && is_fd_valid(0) &&
|
|
|
|
(pserr = posix_spawn_file_actions_addclose (&fa, 0)) != 0)
|
2012-03-26 14:11:11 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
2012-03-26 14:11:11 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
2014-09-19 14:05:02 +00:00
|
|
|
if ((flags & QSE_PIO_DROPOUT) && is_fd_valid(1) &&
|
|
|
|
(pserr = posix_spawn_file_actions_addclose (&fa, 1)) != 0)
|
2012-03-26 14:11:11 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
2012-03-26 14:11:11 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
2014-09-19 14:05:02 +00:00
|
|
|
if ((flags & QSE_PIO_DROPERR) && is_fd_valid(2) &&
|
|
|
|
(pserr = posix_spawn_file_actions_addclose (&fa, 2)) != 0)
|
2012-03-26 14:11:11 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
2012-03-26 14:11:11 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flags & QSE_PIO_NOCLOEXEC))
|
|
|
|
{
|
2017-09-26 13:49:16 +00:00
|
|
|
int fd = qse_get_highest_fd ();
|
2013-01-29 03:43:32 +00:00
|
|
|
while (fd > 2)
|
2012-03-26 14:11:11 +00:00
|
|
|
{
|
|
|
|
if (fd != handle[0] && fd != handle[1] &&
|
|
|
|
fd != handle[2] && fd != handle[3] &&
|
|
|
|
fd != handle[4] && fd != handle[5])
|
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
/* closing attempt on a best-effort basis.
|
|
|
|
* posix_spawn() fails if the file descriptor added
|
|
|
|
* with addclose() is closed before posix_spawn().
|
|
|
|
* addclose() if no FD_CLOEXEC is set or it's unknown. */
|
|
|
|
if (is_fd_valid_and_nocloexec(fd) &&
|
|
|
|
(pserr = posix_spawn_file_actions_addclose (&fa, fd)) != 0)
|
|
|
|
{
|
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
|
|
|
}
|
2012-03-26 14:11:11 +00:00
|
|
|
}
|
2013-01-29 03:43:32 +00:00
|
|
|
fd--;
|
2012-03-26 14:11:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (make_param (pio, cmd, flags, ¶m) <= -1) goto oops;
|
2012-03-26 14:11:11 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
/* check if the command(the command requested or /bin/sh) is
|
|
|
|
* exectuable to return an error without trying to execute it
|
|
|
|
* though this check alone isn't sufficient */
|
|
|
|
if (assert_executable (pio, param.argv[0]) <= -1)
|
2012-03-26 14:11:11 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
free_param (pio, ¶m);
|
|
|
|
goto oops;
|
2012-03-26 14:11:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
posix_spawnattr_init (&psattr);
|
2012-03-26 14:11:11 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
#if defined(__linux)
|
|
|
|
#if !defined(POSIX_SPAWN_USEVFORK)
|
|
|
|
# define POSIX_SPAWN_USEVFORK 0x40
|
|
|
|
#endif
|
|
|
|
posix_spawnattr_setflags (&psattr, POSIX_SPAWN_USEVFORK);
|
|
|
|
#endif
|
2012-03-26 14:11:11 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
pserr = posix_spawn(
|
|
|
|
&pid, param.argv[0], &fa, &psattr, param.argv,
|
|
|
|
(env? qse_env_getarr(env): environ));
|
2012-03-26 14:11:11 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
#if defined(__linux)
|
|
|
|
posix_spawnattr_destroy (&psattr);
|
2012-03-26 14:11:11 +00:00
|
|
|
#endif
|
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
free_param (pio, ¶m);
|
|
|
|
if (fa_inited)
|
2012-03-26 14:11:11 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
posix_spawn_file_actions_destroy (&fa);
|
|
|
|
fa_inited = 0;
|
2012-03-26 14:11:11 +00:00
|
|
|
}
|
2014-09-19 14:05:02 +00:00
|
|
|
if (pserr != 0)
|
2012-03-26 14:11:11 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
pio->errnum = syserr_to_errnum (pserr);
|
|
|
|
goto oops;
|
2012-03-26 14:11:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
pio->child = pid;
|
2012-03-26 14:11:11 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
#elif defined(QSE_SYSCALL0) && defined(SYS_vfork)
|
2012-03-26 14:11:11 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (make_param (pio, cmd, flags, ¶m) <= -1) goto oops;
|
2012-03-26 14:11:11 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
/* check if the command(the command requested or /bin/sh) is
|
|
|
|
* exectuable to return an error without trying to execute it
|
|
|
|
* though this check alone isn't sufficient */
|
|
|
|
if (assert_executable (pio, param.argv[0]) <= -1)
|
2012-02-19 14:38:22 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
free_param (pio, ¶m);
|
2012-02-19 14:38:22 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
2012-02-01 14:32:34 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
/* prepare some data before vforking for vfork limitation.
|
|
|
|
* the child in vfork should not make function calls or
|
|
|
|
* change data shared with the parent. */
|
2017-09-26 13:49:16 +00:00
|
|
|
if (!(flags & QSE_PIO_NOCLOEXEC)) highest_fd = qse_get_highest_fd ();
|
2014-09-19 14:05:02 +00:00
|
|
|
envarr = env? qse_env_getarr(env): environ;
|
2012-02-01 14:32:34 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
QSE_SYSCALL0 (pid, SYS_vfork);
|
|
|
|
if (pid <= -1)
|
2012-02-19 14:38:22 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
pio->errnum = QSE_PIO_EINVAL;
|
|
|
|
free_param (pio, ¶m);
|
2012-02-19 14:38:22 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
2012-02-10 13:58:23 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
/* the child after vfork should not make function calls.
|
|
|
|
* since the system call like close() are also normal
|
|
|
|
* functions, i have to use assembly macros to make
|
|
|
|
* system calls. */
|
2012-02-10 13:58:23 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
qse_pio_hnd_t devnull = -1;
|
2012-02-01 14:32:34 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (!(flags & QSE_PIO_NOCLOEXEC))
|
2012-02-02 14:48:06 +00:00
|
|
|
{
|
2017-09-26 13:49:16 +00:00
|
|
|
/* cannot call qse_close_open_fds_using_proc() in the vfork() context */
|
2014-09-19 14:05:02 +00:00
|
|
|
|
|
|
|
int fd = highest_fd;
|
2014-09-17 15:02:14 +00:00
|
|
|
|
|
|
|
/* close all other unknown open handles except
|
|
|
|
* stdin/out/err and the pipes. */
|
|
|
|
while (fd > 2)
|
2012-03-26 14:11:11 +00:00
|
|
|
{
|
2014-09-17 15:02:14 +00:00
|
|
|
if (fd != handle[0] && fd != handle[1] &&
|
2014-09-19 14:05:02 +00:00
|
|
|
fd != handle[2] && fd != handle[3] &&
|
|
|
|
fd != handle[4] && fd != handle[5])
|
2014-09-17 15:02:14 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
QSE_SYSCALL1 (dummy, SYS_close, fd);
|
2014-09-17 15:02:14 +00:00
|
|
|
}
|
|
|
|
fd--;
|
2012-03-26 14:11:11 +00:00
|
|
|
}
|
2012-02-02 14:48:06 +00:00
|
|
|
}
|
2009-06-12 01:44:44 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (flags & QSE_PIO_WRITEIN)
|
|
|
|
{
|
|
|
|
/* child should read */
|
|
|
|
QSE_SYSCALL1 (dummy, SYS_close, handle[1]);
|
|
|
|
QSE_SYSCALL2 (dummy, SYS_dup2, handle[0], 0);
|
|
|
|
if (dummy <= -1) goto child_oops;
|
|
|
|
QSE_SYSCALL1 (dummy, SYS_close, handle[0]);
|
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (flags & QSE_PIO_READOUT)
|
|
|
|
{
|
|
|
|
/* child should write */
|
|
|
|
QSE_SYSCALL1 (dummy, SYS_close, handle[2]);
|
|
|
|
QSE_SYSCALL2 (dummy, SYS_dup2, handle[3], 1);
|
|
|
|
if (dummy <= -1) goto child_oops;
|
|
|
|
|
|
|
|
if (flags & QSE_PIO_ERRTOOUT)
|
|
|
|
{
|
|
|
|
QSE_SYSCALL2 (dummy, SYS_dup2, handle[3], 2);
|
|
|
|
if (dummy <= -1) goto child_oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSE_SYSCALL1 (dummy, SYS_close, handle[3]);
|
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (flags & QSE_PIO_READERR)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
/* child should write */
|
|
|
|
QSE_SYSCALL1 (dummy, SYS_close, handle[4]);
|
|
|
|
QSE_SYSCALL2 (dummy, SYS_dup2, handle[5], 2);
|
|
|
|
if (dummy <= -1) goto child_oops;
|
|
|
|
|
|
|
|
if (flags & QSE_PIO_OUTTOERR)
|
|
|
|
{
|
|
|
|
QSE_SYSCALL2 (dummy, SYS_dup2, handle[5], 1);
|
|
|
|
if (dummy <= -1) goto child_oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSE_SYSCALL1 (dummy, SYS_close, handle[5]);
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (flags & (QSE_PIO_INTONUL | QSE_PIO_OUTTONUL | QSE_PIO_ERRTONUL))
|
|
|
|
{
|
|
|
|
#if defined(O_LARGEFILE)
|
|
|
|
QSE_SYSCALL3 (devnull, SYS_open, QSE_MT("/dev/null"), O_RDWR|O_LARGEFILE, 0);
|
|
|
|
#else
|
|
|
|
QSE_SYSCALL3 (devnull, SYS_open, QSE_MT("/dev/null"), O_RDWR, 0);
|
|
|
|
#endif
|
|
|
|
if (devnull <= -1) goto child_oops;
|
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (flags & QSE_PIO_INTONUL)
|
|
|
|
{
|
|
|
|
QSE_SYSCALL2 (dummy, SYS_dup2, devnull, 0);
|
|
|
|
if (dummy <= -1) goto child_oops;
|
|
|
|
}
|
|
|
|
if (flags & QSE_PIO_OUTTONUL)
|
|
|
|
{
|
|
|
|
QSE_SYSCALL2 (dummy, SYS_dup2, devnull, 1);
|
|
|
|
if (dummy <= -1) goto child_oops;
|
|
|
|
}
|
|
|
|
if (flags & QSE_PIO_ERRTONUL)
|
|
|
|
{
|
|
|
|
QSE_SYSCALL2 (dummy, SYS_dup2, devnull, 2);
|
|
|
|
if (dummy <= -1) goto child_oops;
|
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (flags & (QSE_PIO_INTONUL | QSE_PIO_OUTTONUL | QSE_PIO_ERRTONUL))
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
QSE_SYSCALL1 (dummy, SYS_close, devnull);
|
|
|
|
devnull = -1;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (flags & QSE_PIO_DROPIN) QSE_SYSCALL1 (dummy, SYS_close, 0);
|
|
|
|
if (flags & QSE_PIO_DROPOUT) QSE_SYSCALL1 (dummy, SYS_close, 1);
|
|
|
|
if (flags & QSE_PIO_DROPERR) QSE_SYSCALL1 (dummy, SYS_close, 2);
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
QSE_SYSCALL3 (dummy, SYS_execve, param.argv[0], param.argv, envarr);
|
|
|
|
/*free_param (pio, ¶m); don't free this in the vfork version */
|
|
|
|
|
|
|
|
child_oops:
|
|
|
|
if (devnull >= 0) QSE_SYSCALL1 (dummy, SYS_close, devnull);
|
|
|
|
QSE_SYSCALL1 (dummy, SYS_exit, 128);
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
/* parent */
|
|
|
|
free_param (pio, ¶m);
|
|
|
|
pio->child = pid;
|
|
|
|
|
|
|
|
#else
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
if (make_param (pio, cmd, flags, ¶m) <= -1) goto oops;
|
|
|
|
|
|
|
|
/* check if the command(the command requested or /bin/sh) is
|
|
|
|
* exectuable to return an error without trying to execute it
|
|
|
|
* though this check alone isn't sufficient */
|
|
|
|
if (assert_executable (pio, param.argv[0]) <= -1)
|
2012-02-01 14:32:34 +00:00
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
free_param (pio, ¶m);
|
|
|
|
goto oops;
|
2012-02-01 14:32:34 +00:00
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
pid = standard_fork_and_exec (pio, handle, ¶m, env);
|
|
|
|
if (pid <= -1)
|
|
|
|
{
|
|
|
|
free_param (pio, ¶m);
|
|
|
|
goto oops;
|
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2014-09-19 14:05:02 +00:00
|
|
|
/* parent */
|
|
|
|
free_param (pio, ¶m);
|
|
|
|
pio->child = pid;
|
|
|
|
#endif
|
2009-01-15 03:58:27 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_WRITEIN)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 012345
|
|
|
|
* rw----
|
|
|
|
* X
|
|
|
|
* WRITE => 1
|
|
|
|
*/
|
2011-08-05 09:43:28 +00:00
|
|
|
QSE_CLOSE (handle[0]);
|
|
|
|
handle[0] = QSE_PIO_HND_NIL;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_READOUT)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 012345
|
|
|
|
* --rw--
|
|
|
|
* X
|
|
|
|
* READ => 2
|
|
|
|
*/
|
2011-08-05 09:43:28 +00:00
|
|
|
QSE_CLOSE (handle[3]);
|
|
|
|
handle[3] = QSE_PIO_HND_NIL;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_READERR)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 012345
|
|
|
|
* ----rw
|
|
|
|
* X
|
|
|
|
* READ => 4
|
|
|
|
*/
|
2011-08-05 09:43:28 +00:00
|
|
|
QSE_CLOSE (handle[5]);
|
|
|
|
handle[5] = QSE_PIO_HND_NIL;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-25 17:28:20 +00:00
|
|
|
if (((flags & QSE_PIO_INNOBLOCK) && set_pipe_nonblock(pio, handle[1], 1) <= -1) ||
|
|
|
|
((flags & QSE_PIO_OUTNOBLOCK) && set_pipe_nonblock(pio, handle[2], 1) <= -1) ||
|
2014-07-25 18:10:56 +00:00
|
|
|
((flags & QSE_PIO_ERRNOBLOCK) && set_pipe_nonblock(pio, handle[4], 1) <= -1))
|
2014-07-25 17:28:20 +00:00
|
|
|
{
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2009-01-15 03:58:27 +00:00
|
|
|
/* store back references */
|
2009-01-29 08:50:30 +00:00
|
|
|
pio->pin[QSE_PIO_IN].self = pio;
|
|
|
|
pio->pin[QSE_PIO_OUT].self = pio;
|
|
|
|
pio->pin[QSE_PIO_ERR].self = pio;
|
2009-01-15 03:58:27 +00:00
|
|
|
|
|
|
|
/* store actual pipe handles */
|
2009-01-29 08:50:30 +00:00
|
|
|
pio->pin[QSE_PIO_IN].handle = handle[1];
|
|
|
|
pio->pin[QSE_PIO_OUT].handle = handle[2];
|
|
|
|
pio->pin[QSE_PIO_ERR].handle = handle[4];
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2014-07-25 17:28:20 +00:00
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_TEXT)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2011-09-11 20:48:21 +00:00
|
|
|
int topt = 0;
|
|
|
|
|
2011-12-28 14:26:02 +00:00
|
|
|
if (flags & QSE_PIO_IGNOREMBWCERR) topt |= QSE_TIO_IGNOREMBWCERR;
|
|
|
|
if (flags & QSE_PIO_NOAUTOFLUSH) topt |= QSE_TIO_NOAUTOFLUSH;
|
2011-09-11 20:48:21 +00:00
|
|
|
|
2011-12-30 14:44:21 +00:00
|
|
|
QSE_ASSERT (QSE_COUNTOF(tio) == QSE_COUNTOF(pio->pin));
|
2009-01-15 03:58:27 +00:00
|
|
|
for (i = 0; i < QSE_COUNTOF(tio); i++)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
2012-02-12 13:20:39 +00:00
|
|
|
tio[i] = qse_tio_open (pio->mmgr, QSE_SIZEOF(&pio->pin[i]), topt);
|
2011-09-01 09:43:46 +00:00
|
|
|
if (tio[i] == QSE_NULL)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_ENOMEM;
|
|
|
|
goto oops;
|
|
|
|
}
|
2012-02-12 13:20:39 +00:00
|
|
|
|
|
|
|
/**(qse_pio_pin_t**)qse_tio_getxtn(tio[i]) = &pio->pin[i]; */
|
|
|
|
*(qse_pio_pin_t**)QSE_XTN(tio[i]) = &pio->pin[i];
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
r = (i == QSE_PIO_IN)?
|
2012-02-12 13:20:39 +00:00
|
|
|
qse_tio_attachout (tio[i], pio_output, QSE_NULL, 4096):
|
|
|
|
qse_tio_attachin (tio[i], pio_input, QSE_NULL, 4096);
|
|
|
|
if (r <= -1)
|
|
|
|
{
|
|
|
|
if (pio->errnum == QSE_PIO_ENOERR)
|
|
|
|
pio->errnum = tio_errnum_to_pio_errnum (tio[i]);
|
|
|
|
goto oops;
|
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
pio->pin[i].tio = tio[i];
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-01 09:43:46 +00:00
|
|
|
return 0;
|
2009-01-15 03:58:27 +00:00
|
|
|
|
|
|
|
oops:
|
2012-02-21 09:23:51 +00:00
|
|
|
if (pio->errnum == QSE_PIO_ENOERR) pio->errnum = QSE_PIO_ESYSERR;
|
2011-05-10 10:11:13 +00:00
|
|
|
|
2011-03-19 09:07:21 +00:00
|
|
|
#if defined(_WIN32)
|
2009-07-24 07:01:52 +00:00
|
|
|
if (windevnul != INVALID_HANDLE_VALUE) CloseHandle (windevnul);
|
2011-05-10 10:11:13 +00:00
|
|
|
|
2011-03-21 08:16:54 +00:00
|
|
|
#elif defined(__OS2__)
|
|
|
|
if (cmd_line) QSE_MMGR_FREE (mmgr, cmd_line);
|
|
|
|
if (old_in != QSE_PIO_HND_NIL)
|
|
|
|
{
|
|
|
|
DosDupHandle (old_in, &std_in);
|
|
|
|
DosClose (old_in);
|
|
|
|
}
|
|
|
|
if (old_out != QSE_PIO_HND_NIL)
|
|
|
|
{
|
|
|
|
DosDupHandle (old_out, &std_out);
|
|
|
|
DosClose (old_out);
|
|
|
|
}
|
|
|
|
if (old_err != QSE_PIO_HND_NIL)
|
|
|
|
{
|
|
|
|
DosDupHandle (old_err, &std_err);
|
|
|
|
DosClose (old_err);
|
|
|
|
}
|
|
|
|
if (os2devnul != QSE_PIO_HND_NIL) DosClose (os2devnul);
|
2009-07-23 06:47:13 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
for (i = 0; i < QSE_COUNTOF(tio); i++)
|
|
|
|
{
|
2011-08-05 09:43:28 +00:00
|
|
|
if (tio[i]) qse_tio_close (tio[i]);
|
2009-07-23 06:47:13 +00:00
|
|
|
}
|
2011-03-19 09:07:21 +00:00
|
|
|
|
2011-03-16 09:20:03 +00:00
|
|
|
#if defined(_WIN32)
|
2011-03-17 02:37:06 +00:00
|
|
|
for (i = minidx; i < maxidx; i++) CloseHandle (handle[i]);
|
|
|
|
#elif defined(__OS2__)
|
2011-03-21 08:16:54 +00:00
|
|
|
for (i = minidx; i < maxidx; i++)
|
|
|
|
{
|
2014-09-19 14:05:02 +00:00
|
|
|
if (handle[i] != QSE_PIO_HND_NIL) DosClose (handle[i]);
|
2011-03-21 08:16:54 +00:00
|
|
|
}
|
2011-05-10 10:11:13 +00:00
|
|
|
#elif defined(__DOS__)
|
2011-08-05 09:43:28 +00:00
|
|
|
|
|
|
|
/* DOS not multi-processed. can't support pio */
|
2012-03-26 14:11:11 +00:00
|
|
|
#elif defined(HAVE_POSIX_SPAWN) && !(defined(QSE_SYSCALL0) && defined(SYS_vfork))
|
2012-02-01 14:32:34 +00:00
|
|
|
if (fa_inited)
|
|
|
|
{
|
|
|
|
posix_spawn_file_actions_destroy (&fa);
|
|
|
|
fa_inited = 0;
|
|
|
|
}
|
|
|
|
for (i = minidx; i < maxidx; i++)
|
|
|
|
{
|
2014-07-25 17:28:20 +00:00
|
|
|
if (handle[i] != QSE_PIO_HND_NIL) QSE_CLOSE (handle[i]);
|
2012-02-01 14:32:34 +00:00
|
|
|
}
|
2012-03-26 14:11:11 +00:00
|
|
|
#elif defined(QSE_SYSCALL0) && defined(SYS_vfork)
|
|
|
|
for (i = minidx; i < maxidx; i++)
|
|
|
|
{
|
2014-07-25 17:28:20 +00:00
|
|
|
if (handle[i] != QSE_PIO_HND_NIL) QSE_CLOSE (handle[i]);
|
2012-03-26 14:11:11 +00:00
|
|
|
}
|
2009-06-09 07:09:01 +00:00
|
|
|
#else
|
2011-03-21 08:16:54 +00:00
|
|
|
for (i = minidx; i < maxidx; i++)
|
|
|
|
{
|
2014-07-25 17:28:20 +00:00
|
|
|
if (handle[i] != QSE_PIO_HND_NIL) QSE_CLOSE (handle[i]);
|
2011-03-21 08:16:54 +00:00
|
|
|
}
|
2009-06-09 07:09:01 +00:00
|
|
|
#endif
|
2011-03-19 09:07:21 +00:00
|
|
|
|
2011-09-01 09:43:46 +00:00
|
|
|
return -1;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
void qse_pio_fini (qse_pio_t* pio)
|
|
|
|
{
|
|
|
|
qse_pio_end (pio, QSE_PIO_ERR);
|
2009-06-12 01:44:44 +00:00
|
|
|
qse_pio_end (pio, QSE_PIO_OUT);
|
|
|
|
qse_pio_end (pio, QSE_PIO_IN);
|
2009-01-29 08:50:30 +00:00
|
|
|
|
2012-04-27 14:33:14 +00:00
|
|
|
/* when closing, enable blocking and retrying */
|
|
|
|
pio->flags &= ~QSE_PIO_WAITNOBLOCK;
|
|
|
|
pio->flags &= ~QSE_PIO_WAITNORETRY;
|
2009-01-29 08:50:30 +00:00
|
|
|
qse_pio_wait (pio);
|
|
|
|
}
|
|
|
|
|
2012-12-01 13:13:13 +00:00
|
|
|
qse_mmgr_t* qse_pio_getmmgr (qse_pio_t* pio)
|
|
|
|
{
|
|
|
|
return pio->mmgr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* qse_pio_getxtn (qse_pio_t* pio)
|
|
|
|
{
|
|
|
|
return QSE_XTN (pio);
|
|
|
|
}
|
|
|
|
|
2012-02-25 14:52:26 +00:00
|
|
|
qse_pio_errnum_t qse_pio_geterrnum (const qse_pio_t* pio)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2012-02-25 14:52:26 +00:00
|
|
|
return pio->errnum;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2011-12-30 14:44:21 +00:00
|
|
|
qse_cmgr_t* qse_pio_getcmgr (qse_pio_t* pio, qse_pio_hid_t hid)
|
|
|
|
{
|
|
|
|
return pio->pin[hid].tio?
|
|
|
|
qse_tio_getcmgr(pio->pin[hid].tio): QSE_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qse_pio_setcmgr (qse_pio_t* pio, qse_pio_hid_t hid, qse_cmgr_t* cmgr)
|
|
|
|
{
|
|
|
|
if (pio->pin[hid].tio) qse_tio_setcmgr (pio->pin[hid].tio, cmgr);
|
|
|
|
}
|
|
|
|
|
2019-05-13 05:22:40 +00:00
|
|
|
qse_pio_hnd_t qse_pio_gethnd (const qse_pio_t* pio, qse_pio_hid_t hid)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2009-01-29 08:50:30 +00:00
|
|
|
return pio->pin[hid].handle;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2012-02-25 14:52:26 +00:00
|
|
|
qse_pio_pid_t qse_pio_getchild (const qse_pio_t* pio)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2009-01-29 08:50:30 +00:00
|
|
|
return pio->child;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
static qse_ssize_t pio_read (qse_pio_t* pio, void* buf, qse_size_t size, qse_pio_hnd_t hnd)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2011-03-16 09:20:03 +00:00
|
|
|
#if defined(_WIN32)
|
2011-03-17 02:37:06 +00:00
|
|
|
DWORD count;
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
ULONG count;
|
2011-03-16 09:20:03 +00:00
|
|
|
APIRET rc;
|
2011-05-10 10:11:13 +00:00
|
|
|
#elif defined(__DOS__)
|
|
|
|
int n;
|
2009-01-15 03:58:27 +00:00
|
|
|
#else
|
|
|
|
qse_ssize_t n;
|
|
|
|
#endif
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
if (hnd == QSE_PIO_HND_NIL)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
|
|
|
/* the stream is already closed */
|
2009-01-29 08:50:30 +00:00
|
|
|
pio->errnum = QSE_PIO_ENOHND;
|
2009-01-15 03:58:27 +00:00
|
|
|
return (qse_ssize_t)-1;
|
|
|
|
}
|
|
|
|
|
2011-03-16 09:20:03 +00:00
|
|
|
#if defined(_WIN32)
|
2011-12-18 17:40:37 +00:00
|
|
|
|
|
|
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(DWORD)))
|
|
|
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(DWORD);
|
|
|
|
|
2011-03-16 09:20:03 +00:00
|
|
|
if (ReadFile(hnd, buf, (DWORD)size, &count, QSE_NULL) == FALSE)
|
2009-07-23 06:47:13 +00:00
|
|
|
{
|
2009-07-24 07:01:52 +00:00
|
|
|
/* ReadFile receives ERROR_BROKEN_PIPE when the write end
|
|
|
|
* is closed in the child process */
|
|
|
|
if (GetLastError() == ERROR_BROKEN_PIPE) return 0;
|
2012-02-12 13:20:39 +00:00
|
|
|
pio->errnum = syserr_to_errnum(GetLastError());
|
2009-07-23 06:47:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-03-17 02:37:06 +00:00
|
|
|
return (qse_ssize_t)count;
|
2011-05-10 10:11:13 +00:00
|
|
|
|
2011-03-17 02:37:06 +00:00
|
|
|
#elif defined(__OS2__)
|
2011-12-18 17:40:37 +00:00
|
|
|
|
|
|
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(ULONG)))
|
|
|
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(ULONG);
|
|
|
|
|
2011-03-17 02:37:06 +00:00
|
|
|
rc = DosRead (hnd, buf, (ULONG)size, &count);
|
|
|
|
if (rc != NO_ERROR)
|
|
|
|
{
|
|
|
|
if (rc == ERROR_BROKEN_PIPE) return 0; /* TODO: check this */
|
2012-02-12 13:20:39 +00:00
|
|
|
pio->errnum = syserr_to_errnum(rc);
|
2011-03-17 02:37:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
return (qse_ssize_t)count;
|
2011-05-10 10:11:13 +00:00
|
|
|
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
/* TODO: verify this */
|
2011-12-18 17:40:37 +00:00
|
|
|
|
|
|
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(unsigned int)))
|
|
|
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(unsigned int);
|
2011-05-10 10:11:13 +00:00
|
|
|
|
|
|
|
n = read (hnd, buf, size);
|
2012-02-12 13:20:39 +00:00
|
|
|
if (n <= -1) pio->errnum = syserr_to_errnum(errno);
|
2011-05-10 10:11:13 +00:00
|
|
|
return n;
|
|
|
|
|
2009-01-15 03:58:27 +00:00
|
|
|
#else
|
|
|
|
|
2011-12-18 17:40:37 +00:00
|
|
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t)))
|
|
|
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t);
|
2009-01-29 08:50:30 +00:00
|
|
|
|
|
|
|
reread:
|
2019-04-21 05:44:53 +00:00
|
|
|
n = QSE_READ(hnd, buf, size);
|
2012-02-12 13:20:39 +00:00
|
|
|
if (n <= -1)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2009-01-29 08:50:30 +00:00
|
|
|
if (errno == EINTR)
|
|
|
|
{
|
2012-04-27 14:33:14 +00:00
|
|
|
if (pio->flags & QSE_PIO_READNORETRY)
|
2009-01-29 08:50:30 +00:00
|
|
|
pio->errnum = QSE_PIO_EINTR;
|
|
|
|
else goto reread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-12 13:20:39 +00:00
|
|
|
pio->errnum = syserr_to_errnum (errno);
|
2009-01-29 08:50:30 +00:00
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
qse_ssize_t qse_pio_read (qse_pio_t* pio, qse_pio_hid_t hid, void* buf, qse_size_t size)
|
2009-01-29 08:50:30 +00:00
|
|
|
{
|
|
|
|
if (pio->pin[hid].tio == QSE_NULL)
|
2019-04-21 05:44:53 +00:00
|
|
|
return pio_read(pio, buf, size, pio->pin[hid].handle);
|
2009-01-29 08:50:30 +00:00
|
|
|
else
|
2012-02-12 13:20:39 +00:00
|
|
|
{
|
|
|
|
qse_ssize_t n;
|
|
|
|
|
|
|
|
pio->errnum = QSE_PIO_ENOERR;
|
2019-04-21 05:44:53 +00:00
|
|
|
n = qse_tio_read(pio->pin[hid].tio, buf, size);
|
2012-02-12 13:20:39 +00:00
|
|
|
if (n <= -1 && pio->errnum == QSE_PIO_ENOERR)
|
|
|
|
pio->errnum = tio_errnum_to_pio_errnum (pio->pin[hid].tio);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
2009-01-29 08:50:30 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
static qse_ssize_t pio_write (qse_pio_t* pio, const void* data, qse_size_t size, qse_pio_hnd_t hnd)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2011-03-16 09:20:03 +00:00
|
|
|
#if defined(_WIN32)
|
2011-03-17 02:37:06 +00:00
|
|
|
DWORD count;
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
ULONG count;
|
2011-03-16 09:20:03 +00:00
|
|
|
APIRET rc;
|
2011-05-10 10:11:13 +00:00
|
|
|
#elif defined(__DOS__)
|
|
|
|
int n;
|
2009-01-15 03:58:27 +00:00
|
|
|
#else
|
|
|
|
qse_ssize_t n;
|
|
|
|
#endif
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
if (hnd == QSE_PIO_HND_NIL)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
|
|
|
/* the stream is already closed */
|
2009-01-29 08:50:30 +00:00
|
|
|
pio->errnum = QSE_PIO_ENOHND;
|
2012-02-12 13:20:39 +00:00
|
|
|
return (qse_ssize_t)-1;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2011-03-16 09:20:03 +00:00
|
|
|
#if defined(_WIN32)
|
2011-03-21 08:16:54 +00:00
|
|
|
|
2011-12-18 17:40:37 +00:00
|
|
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(DWORD)))
|
|
|
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(DWORD);
|
|
|
|
|
2011-03-16 09:20:03 +00:00
|
|
|
if (WriteFile (hnd, data, (DWORD)size, &count, QSE_NULL) == FALSE)
|
2009-07-23 06:47:13 +00:00
|
|
|
{
|
2012-02-12 13:20:39 +00:00
|
|
|
pio->errnum = syserr_to_errnum(GetLastError());
|
2009-07-23 06:47:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-03-17 02:37:06 +00:00
|
|
|
return (qse_ssize_t)count;
|
2011-03-21 08:16:54 +00:00
|
|
|
|
2011-03-17 02:37:06 +00:00
|
|
|
#elif defined(__OS2__)
|
2011-03-21 08:16:54 +00:00
|
|
|
|
2011-12-18 17:40:37 +00:00
|
|
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(ULONG)))
|
|
|
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(ULONG);
|
|
|
|
|
2011-03-17 02:37:06 +00:00
|
|
|
rc = DosWrite (hnd, (PVOID)data, (ULONG)size, &count);
|
|
|
|
if (rc != NO_ERROR)
|
|
|
|
{
|
2012-02-12 13:20:39 +00:00
|
|
|
pio->errnum = syserr_to_errnum(rc);
|
2011-03-17 02:37:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return (qse_ssize_t)count;
|
2011-03-16 09:20:03 +00:00
|
|
|
|
2011-05-10 10:11:13 +00:00
|
|
|
#elif defined(__DOS__)
|
2011-12-18 17:40:37 +00:00
|
|
|
|
|
|
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(unsigned int)))
|
|
|
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(unsigned int);
|
2011-05-10 10:11:13 +00:00
|
|
|
|
|
|
|
n = write (hnd, data, size);
|
2012-02-20 14:45:49 +00:00
|
|
|
if (n <= -1) pio->errnum = syserr_to_errnum (errno);
|
2011-05-10 10:11:13 +00:00
|
|
|
return n;
|
|
|
|
|
2009-01-15 03:58:27 +00:00
|
|
|
#else
|
2011-03-21 08:16:54 +00:00
|
|
|
|
2011-12-18 17:40:37 +00:00
|
|
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t)))
|
|
|
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t);
|
2009-01-29 08:50:30 +00:00
|
|
|
|
|
|
|
rewrite:
|
2009-01-15 03:58:27 +00:00
|
|
|
n = QSE_WRITE (hnd, data, size);
|
2012-02-12 13:20:39 +00:00
|
|
|
if (n <= -1)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2009-01-29 08:50:30 +00:00
|
|
|
if (errno == EINTR)
|
|
|
|
{
|
2012-04-27 14:33:14 +00:00
|
|
|
if (pio->flags & QSE_PIO_WRITENORETRY)
|
2009-01-29 08:50:30 +00:00
|
|
|
pio->errnum = QSE_PIO_EINTR;
|
|
|
|
else goto rewrite;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-12 13:20:39 +00:00
|
|
|
pio->errnum = syserr_to_errnum (errno);
|
2009-01-29 08:50:30 +00:00
|
|
|
}
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
return n;
|
2011-03-21 08:16:54 +00:00
|
|
|
|
2009-01-15 03:58:27 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
qse_ssize_t qse_pio_write (qse_pio_t* pio, qse_pio_hid_t hid, const void* data, qse_size_t size)
|
2009-01-29 08:50:30 +00:00
|
|
|
{
|
|
|
|
if (pio->pin[hid].tio == QSE_NULL)
|
2019-04-21 05:44:53 +00:00
|
|
|
return pio_write(pio, data, size, pio->pin[hid].handle);
|
2009-01-29 08:50:30 +00:00
|
|
|
else
|
2012-02-12 13:20:39 +00:00
|
|
|
{
|
|
|
|
qse_ssize_t n;
|
|
|
|
|
|
|
|
pio->errnum = QSE_PIO_ENOERR;
|
2019-04-21 05:44:53 +00:00
|
|
|
n = qse_tio_write(pio->pin[hid].tio, data, size);
|
2012-02-12 13:20:39 +00:00
|
|
|
if (n <= -1 && pio->errnum == QSE_PIO_ENOERR)
|
|
|
|
pio->errnum = tio_errnum_to_pio_errnum (pio->pin[hid].tio);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
2009-01-29 08:50:30 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
qse_ssize_t qse_pio_writebytes (qse_pio_t* pio, qse_pio_hid_t hid, const void* data, qse_size_t size)
|
|
|
|
{
|
|
|
|
if (pio->pin[hid].tio == QSE_NULL)
|
|
|
|
return pio_write(pio, data, size, pio->pin[hid].handle);
|
|
|
|
else
|
|
|
|
return qse_tio_writembs(pio->pin[hid].tio, data, size);
|
|
|
|
}
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
qse_ssize_t qse_pio_flush (qse_pio_t* pio, qse_pio_hid_t hid)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2012-02-12 13:20:39 +00:00
|
|
|
qse_ssize_t n;
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
if (pio->pin[hid].tio == QSE_NULL) return 0;
|
2012-02-12 13:20:39 +00:00
|
|
|
|
|
|
|
pio->errnum = QSE_PIO_ENOERR;
|
2019-04-21 05:44:53 +00:00
|
|
|
n = qse_tio_flush(pio->pin[hid].tio);
|
2012-02-12 13:20:39 +00:00
|
|
|
if (n <= -1 && pio->errnum == QSE_PIO_ENOERR)
|
2019-04-21 05:44:53 +00:00
|
|
|
pio->errnum = tio_errnum_to_pio_errnum(pio->pin[hid].tio);
|
2012-02-12 13:20:39 +00:00
|
|
|
|
|
|
|
return n;
|
2009-01-29 08:50:30 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 14:18:06 +00:00
|
|
|
void qse_pio_drain (qse_pio_t* pio, qse_pio_hid_t hid)
|
2012-04-27 14:33:14 +00:00
|
|
|
{
|
2014-04-28 14:18:06 +00:00
|
|
|
if (pio->pin[hid].tio) qse_tio_drain (pio->pin[hid].tio);
|
2012-04-27 14:33:14 +00:00
|
|
|
}
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
void qse_pio_end (qse_pio_t* pio, qse_pio_hid_t hid)
|
|
|
|
{
|
2012-04-27 14:33:14 +00:00
|
|
|
if (pio->pin[hid].tio)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2009-01-29 08:50:30 +00:00
|
|
|
qse_tio_close (pio->pin[hid].tio);
|
|
|
|
pio->pin[hid].tio = QSE_NULL;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
if (pio->pin[hid].handle != QSE_PIO_HND_NIL)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2011-03-16 09:20:03 +00:00
|
|
|
#if defined(_WIN32)
|
2011-03-17 02:37:06 +00:00
|
|
|
CloseHandle (pio->pin[hid].handle);
|
|
|
|
#elif defined(__OS2__)
|
2011-03-16 09:20:03 +00:00
|
|
|
DosClose (pio->pin[hid].handle);
|
2011-05-10 10:11:13 +00:00
|
|
|
#elif defined(__DOS__)
|
|
|
|
close (pio->pin[hid].handle);
|
2009-06-09 07:09:01 +00:00
|
|
|
#else
|
2009-01-29 08:50:30 +00:00
|
|
|
QSE_CLOSE (pio->pin[hid].handle);
|
2009-06-09 07:09:01 +00:00
|
|
|
#endif
|
2009-01-29 08:50:30 +00:00
|
|
|
pio->pin[hid].handle = QSE_PIO_HND_NIL;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
int qse_pio_wait (qse_pio_t* pio)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2011-03-16 09:20:03 +00:00
|
|
|
#if defined(_WIN32)
|
2011-03-21 08:16:54 +00:00
|
|
|
|
2009-06-09 07:09:01 +00:00
|
|
|
DWORD ecode, w;
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
if (pio->child == QSE_PIO_PID_NIL)
|
2011-03-19 09:07:21 +00:00
|
|
|
{
|
2009-01-29 08:50:30 +00:00
|
|
|
pio->errnum = QSE_PIO_ECHILD;
|
2009-01-15 03:58:27 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-06-09 07:09:01 +00:00
|
|
|
w = WaitForSingleObject (pio->child,
|
2012-04-27 14:33:14 +00:00
|
|
|
((pio->flags & QSE_PIO_WAITNOBLOCK)? 0: INFINITE)
|
2009-06-09 07:09:01 +00:00
|
|
|
);
|
|
|
|
if (w == WAIT_TIMEOUT)
|
|
|
|
{
|
|
|
|
/* the child process is still alive */
|
|
|
|
return 255 + 1;
|
|
|
|
}
|
|
|
|
if (w != WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
/* WAIT_FAILED, WAIT_ABANDONED */
|
2012-02-21 09:23:51 +00:00
|
|
|
pio->errnum = QSE_PIO_ESYSERR;
|
2009-06-09 07:09:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSE_ASSERT (w == WAIT_OBJECT_0);
|
|
|
|
|
|
|
|
if (GetExitCodeProcess (pio->child, &ecode) == FALSE)
|
|
|
|
{
|
|
|
|
/* close the handle anyway to prevent further
|
|
|
|
* errors when this function is called again */
|
|
|
|
CloseHandle (pio->child);
|
|
|
|
pio->child = QSE_PIO_PID_NIL;
|
|
|
|
|
2012-02-21 09:23:51 +00:00
|
|
|
pio->errnum = QSE_PIO_ESYSERR;
|
2009-06-09 07:09:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-01-15 03:58:27 +00:00
|
|
|
/* close handle here to emulate waitpid() as much as possible. */
|
2009-01-29 08:50:30 +00:00
|
|
|
CloseHandle (pio->child);
|
|
|
|
pio->child = QSE_PIO_PID_NIL;
|
2009-01-15 03:58:27 +00:00
|
|
|
|
2009-06-09 07:09:01 +00:00
|
|
|
if (ecode == STILL_ACTIVE)
|
|
|
|
{
|
|
|
|
/* this should not happen as the control reaches here
|
|
|
|
* only when WaitforSingleObject() is successful.
|
|
|
|
* if it happends, close the handle and return an error */
|
2012-02-21 09:23:51 +00:00
|
|
|
pio->errnum = QSE_PIO_ESYSERR;
|
2009-06-09 07:09:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-17 02:37:06 +00:00
|
|
|
return ecode;
|
2011-03-21 08:16:54 +00:00
|
|
|
|
2011-03-17 02:37:06 +00:00
|
|
|
#elif defined(__OS2__)
|
2011-03-21 08:16:54 +00:00
|
|
|
|
2011-03-19 09:07:21 +00:00
|
|
|
APIRET rc;
|
|
|
|
RESULTCODES child_rc;
|
|
|
|
PID ppid;
|
|
|
|
|
|
|
|
if (pio->child == QSE_PIO_PID_NIL)
|
|
|
|
{
|
|
|
|
pio->errnum = QSE_PIO_ECHILD;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = DosWaitChild (
|
2011-03-21 08:16:54 +00:00
|
|
|
DCWA_PROCESSTREE,
|
2012-04-27 14:33:14 +00:00
|
|
|
((pio->flags & QSE_PIO_WAITNOBLOCK)? DCWW_NOWAIT: DCWW_WAIT),
|
2011-03-19 09:07:21 +00:00
|
|
|
&child_rc,
|
|
|
|
&ppid,
|
|
|
|
pio->child
|
|
|
|
);
|
|
|
|
if (rc == ERROR_CHILD_NOT_COMPLETE)
|
|
|
|
{
|
|
|
|
/* the child process is still alive */
|
|
|
|
return 255 + 1;
|
|
|
|
}
|
|
|
|
if (rc != NO_ERROR)
|
|
|
|
{
|
|
|
|
/* WAIT_FAILED, WAIT_ABANDONED */
|
2012-02-21 09:23:51 +00:00
|
|
|
pio->errnum = QSE_PIO_ESYSERR;
|
2011-03-19 09:07:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close handle here to emulate waitpid() as much as possible. */
|
2011-03-21 08:16:54 +00:00
|
|
|
/*DosClose (pio->child);*/
|
2011-03-19 09:07:21 +00:00
|
|
|
pio->child = QSE_PIO_PID_NIL;
|
|
|
|
|
2011-03-21 08:16:54 +00:00
|
|
|
return (child_rc.codeTerminate == TC_EXIT)?
|
|
|
|
child_rc.codeResult: (255 + 1 + child_rc.codeTerminate);
|
2011-03-19 09:07:21 +00:00
|
|
|
|
2011-05-10 10:11:13 +00:00
|
|
|
#elif defined(__DOS__)
|
2012-02-19 14:38:22 +00:00
|
|
|
|
|
|
|
pio->errnum = QSE_PIO_ENOIMPL;
|
2011-05-10 10:11:13 +00:00
|
|
|
return -1;
|
|
|
|
|
2009-01-15 03:58:27 +00:00
|
|
|
#else
|
2011-03-21 08:16:54 +00:00
|
|
|
|
2009-01-15 03:58:27 +00:00
|
|
|
int opt = 0;
|
|
|
|
int ret = -1;
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
if (pio->child == QSE_PIO_PID_NIL)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2009-01-29 08:50:30 +00:00
|
|
|
pio->errnum = QSE_PIO_ECHILD;
|
2009-01-15 03:58:27 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-27 14:33:14 +00:00
|
|
|
if (pio->flags & QSE_PIO_WAITNOBLOCK) opt |= WNOHANG;
|
2009-01-15 03:58:27 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int status, n;
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
n = QSE_WAITPID (pio->child, &status, opt);
|
2009-07-25 06:22:00 +00:00
|
|
|
if (n <= -1)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
|
|
|
if (errno == ECHILD)
|
|
|
|
{
|
|
|
|
/* most likely, the process has already been
|
|
|
|
* waitpid()ed on. */
|
2009-01-29 08:50:30 +00:00
|
|
|
pio->child = QSE_PIO_PID_NIL;
|
|
|
|
pio->errnum = QSE_PIO_ECHILD;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
else if (errno == EINTR)
|
|
|
|
{
|
2012-04-27 14:33:14 +00:00
|
|
|
if (pio->flags & QSE_PIO_WAITNORETRY)
|
2009-01-29 08:50:30 +00:00
|
|
|
pio->errnum = QSE_PIO_EINTR;
|
|
|
|
else continue;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
2012-02-12 13:20:39 +00:00
|
|
|
else pio->errnum = syserr_to_errnum (errno);
|
2009-01-15 03:58:27 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
/* when WNOHANG is not specified, 0 can't be returned */
|
2012-04-27 14:33:14 +00:00
|
|
|
QSE_ASSERT (pio->flags & QSE_PIO_WAITNOBLOCK);
|
2009-01-15 03:58:27 +00:00
|
|
|
|
|
|
|
ret = 255 + 1;
|
|
|
|
/* the child process is still alive */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
if (n == pio->child)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
|
|
|
if (WIFEXITED(status))
|
|
|
|
{
|
|
|
|
/* the child process ended normally */
|
|
|
|
ret = WEXITSTATUS(status);
|
|
|
|
}
|
|
|
|
else if (WIFSIGNALED(status))
|
|
|
|
{
|
|
|
|
/* the child process was killed by a signal */
|
|
|
|
ret = 255 + 1 + WTERMSIG (status);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* not interested in WIFSTOPPED & WIFCONTINUED.
|
2009-06-09 07:09:01 +00:00
|
|
|
* in fact, this else-block should not be reached
|
2009-01-15 03:58:27 +00:00
|
|
|
* as WIFEXITED or WIFSIGNALED must be true.
|
|
|
|
* anyhow, just set the return value to 0. */
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
pio->child = QSE_PIO_PID_NIL;
|
2009-01-15 03:58:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
int qse_pio_kill (qse_pio_t* pio)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2011-03-16 09:20:03 +00:00
|
|
|
#if defined(_WIN32)
|
2011-03-17 02:37:06 +00:00
|
|
|
DWORD n;
|
|
|
|
#elif defined(__OS2__)
|
2011-03-21 08:16:54 +00:00
|
|
|
APIRET rc;
|
2011-05-10 10:11:13 +00:00
|
|
|
#elif defined(__DOS__)
|
|
|
|
/* TODO: implement this */
|
2009-01-15 03:58:27 +00:00
|
|
|
#else
|
|
|
|
int n;
|
|
|
|
#endif
|
|
|
|
|
2009-01-29 08:50:30 +00:00
|
|
|
if (pio->child == QSE_PIO_PID_NIL)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2009-01-29 08:50:30 +00:00
|
|
|
pio->errnum = QSE_PIO_ECHILD;
|
2009-01-15 03:58:27 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-16 09:20:03 +00:00
|
|
|
#if defined(_WIN32)
|
2009-01-15 03:58:27 +00:00
|
|
|
/* 9 was chosen below to treat TerminateProcess as kill -KILL. */
|
2009-01-29 08:50:30 +00:00
|
|
|
n = TerminateProcess (pio->child, 255 + 1 + 9);
|
2009-01-15 03:58:27 +00:00
|
|
|
if (n == FALSE)
|
|
|
|
{
|
2012-02-21 09:23:51 +00:00
|
|
|
pio->errnum = QSE_PIO_ESYSERR;
|
2009-01-15 03:58:27 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-03-17 02:37:06 +00:00
|
|
|
return 0;
|
2011-05-10 10:11:13 +00:00
|
|
|
|
2011-03-17 02:37:06 +00:00
|
|
|
#elif defined(__OS2__)
|
2011-03-21 08:16:54 +00:00
|
|
|
/*TODO: must use DKP_PROCESS? */
|
|
|
|
rc = DosKillProcess (pio->child, DKP_PROCESSTREE);
|
|
|
|
if (rc != NO_ERROR)
|
2011-03-17 02:37:06 +00:00
|
|
|
{
|
2012-02-21 09:23:51 +00:00
|
|
|
pio->errnum = QSE_PIO_ESYSERR;
|
2011-03-17 02:37:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-03-16 09:20:03 +00:00
|
|
|
return 0;
|
2011-05-10 10:11:13 +00:00
|
|
|
|
|
|
|
#elif defined(__DOS__)
|
2012-02-19 14:38:22 +00:00
|
|
|
|
|
|
|
pio->errnum = QSE_PIO_ENOIMPL;
|
2011-05-10 10:11:13 +00:00
|
|
|
return -1;
|
|
|
|
|
2009-01-15 03:58:27 +00:00
|
|
|
#else
|
2009-01-29 08:50:30 +00:00
|
|
|
n = QSE_KILL (pio->child, SIGKILL);
|
2012-02-21 09:23:51 +00:00
|
|
|
if (n <= -1) pio->errnum = QSE_PIO_ESYSERR;
|
2009-01-15 03:58:27 +00:00
|
|
|
return n;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
static qse_ssize_t pio_input (qse_tio_t* tio, qse_tio_cmd_t cmd, void* buf, qse_size_t size)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2011-12-25 15:25:28 +00:00
|
|
|
if (cmd == QSE_TIO_DATA)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2012-02-12 13:20:39 +00:00
|
|
|
/*qse_pio_pin_t* pin = (qse_pio_pin_t*)qse_tio_getxtn(tio);*/
|
|
|
|
qse_pio_pin_t* pin = *(qse_pio_pin_t**)QSE_XTN(tio);
|
|
|
|
QSE_ASSERT (pin != QSE_NULL);
|
2009-01-29 08:50:30 +00:00
|
|
|
QSE_ASSERT (pin->self != QSE_NULL);
|
|
|
|
return pio_read (pin->self, buf, size, pin->handle);
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* take no actions for OPEN and CLOSE as they are handled
|
2009-01-29 08:50:30 +00:00
|
|
|
* by pio */
|
2011-03-15 21:40:39 +00:00
|
|
|
return 0;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
static qse_ssize_t pio_output (qse_tio_t* tio, qse_tio_cmd_t cmd, void* buf, qse_size_t size)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2011-12-25 15:25:28 +00:00
|
|
|
if (cmd == QSE_TIO_DATA)
|
2009-01-15 03:58:27 +00:00
|
|
|
{
|
2012-02-12 13:20:39 +00:00
|
|
|
/*qse_pio_pin_t* pin = (qse_pio_pin_t*)qse_tio_getxtn(tio);*/
|
|
|
|
qse_pio_pin_t* pin = *(qse_pio_pin_t**)QSE_XTN(tio);
|
|
|
|
QSE_ASSERT (pin != QSE_NULL);
|
2009-01-29 08:50:30 +00:00
|
|
|
QSE_ASSERT (pin->self != QSE_NULL);
|
|
|
|
return pio_write (pin->self, buf, size, pin->handle);
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* take no actions for OPEN and CLOSE as they are handled
|
2009-01-29 08:50:30 +00:00
|
|
|
* by pio */
|
2011-03-15 21:40:39 +00:00
|
|
|
return 0;
|
2009-01-15 03:58:27 +00:00
|
|
|
}
|