2015-05-07 05:59:55 +02:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2015
|
|
|
|
* KO Myung-Hun <komh@chollian.net>
|
|
|
|
*
|
|
|
|
* Provided that these terms and disclaimer and all copyright notices
|
|
|
|
* are retained or reproduced in an accompanying document, permission
|
|
|
|
* is granted to deal in this work without restriction, including un-
|
|
|
|
* limited rights to use, publicly perform, distribute, sell, modify,
|
|
|
|
* merge, give away, or sublicence.
|
|
|
|
*
|
|
|
|
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
|
|
|
* the utmost extent permitted by applicable law, neither express nor
|
|
|
|
* implied; without malicious intent or gross negligence. In no event
|
|
|
|
* may a licensor, author or contributor be held liable for indirect,
|
|
|
|
* direct, other damage, loss, or other issues arising in any way out
|
|
|
|
* of dealing in the work, even if advised of the possibility of such
|
|
|
|
* damage or existence of a defect, except proven that it results out
|
|
|
|
* of said person's immediate fault when using the work as intended.
|
|
|
|
*/
|
|
|
|
|
2015-06-03 05:20:05 +02:00
|
|
|
#define INCL_DOS
|
|
|
|
#include <os2.h>
|
|
|
|
|
2015-05-07 05:59:55 +02:00
|
|
|
#include "sh.h"
|
|
|
|
|
2015-05-11 09:06:22 +02:00
|
|
|
#include <klibc/startup.h>
|
2015-05-12 09:15:25 +02:00
|
|
|
#include <io.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <process.h>
|
2015-05-11 09:06:22 +02:00
|
|
|
|
2017-04-02 17:00:45 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/os2.c,v 1.1 2017/04/02 15:00:44 tg Exp $");
|
|
|
|
|
2015-05-07 05:59:55 +02:00
|
|
|
static char *remove_trailing_dots(char *);
|
|
|
|
static int access_stat_ex(int (*)(), const char *, void *);
|
|
|
|
static int test_exec_exist(const char *, char *);
|
2015-05-11 09:06:22 +02:00
|
|
|
static void response(int *, const char ***);
|
2015-05-12 09:15:25 +02:00
|
|
|
static char *make_response_file(char * const *);
|
2015-05-13 06:34:21 +02:00
|
|
|
static void env_slashify(void);
|
2015-05-19 06:14:57 +02:00
|
|
|
static void add_temp(const char *);
|
|
|
|
static void cleanup_temps(void);
|
|
|
|
static void cleanup(void);
|
2015-05-11 09:06:22 +02:00
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
#define RPUT(x) do { \
|
|
|
|
if (new_argc >= new_alloc) { \
|
|
|
|
new_alloc += 20; \
|
|
|
|
if (!(new_argv = realloc(new_argv, \
|
|
|
|
new_alloc * sizeof(char *)))) \
|
|
|
|
goto exit_out_of_memory; \
|
|
|
|
} \
|
|
|
|
new_argv[new_argc++] = (x); \
|
|
|
|
} while (/* CONSTCOND */ 0)
|
2015-05-11 09:06:22 +02:00
|
|
|
|
|
|
|
#define KLIBC_ARG_RESPONSE_EXCLUDE \
|
|
|
|
(__KLIBC_ARG_DQUOTE | __KLIBC_ARG_WILDCARD | __KLIBC_ARG_SHELL)
|
|
|
|
|
|
|
|
static void
|
|
|
|
response(int *argcp, const char ***argvp)
|
|
|
|
{
|
|
|
|
int i, old_argc, new_argc, new_alloc = 0;
|
|
|
|
const char **old_argv, **new_argv;
|
|
|
|
char *line, *l, *p;
|
|
|
|
FILE *f;
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
old_argc = *argcp;
|
|
|
|
old_argv = *argvp;
|
2015-05-11 09:06:22 +02:00
|
|
|
for (i = 1; i < old_argc; ++i)
|
|
|
|
if (old_argv[i] &&
|
|
|
|
!(old_argv[i][-1] & KLIBC_ARG_RESPONSE_EXCLUDE) &&
|
|
|
|
old_argv[i][0] == '@')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i >= old_argc)
|
2017-03-25 23:56:09 +01:00
|
|
|
/* do nothing */
|
|
|
|
return;
|
2015-05-11 09:06:22 +02:00
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
new_argv = NULL;
|
|
|
|
new_argc = 0;
|
2015-05-11 09:06:22 +02:00
|
|
|
for (i = 0; i < old_argc; ++i) {
|
|
|
|
if (i == 0 || !old_argv[i] ||
|
|
|
|
(old_argv[i][-1] & KLIBC_ARG_RESPONSE_EXCLUDE) ||
|
|
|
|
old_argv[i][0] != '@' ||
|
|
|
|
!(f = fopen(old_argv[i] + 1, "rt")))
|
|
|
|
RPUT(old_argv[i]);
|
|
|
|
else {
|
|
|
|
long filesize;
|
|
|
|
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
filesize = ftell(f);
|
|
|
|
fseek(f, 0, SEEK_SET);
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
line = malloc(filesize + /* type */ 1 + /* NUL */ 1);
|
|
|
|
if (!line) {
|
|
|
|
exit_out_of_memory:
|
|
|
|
fputs("Out of memory while reading response file\n", stderr);
|
|
|
|
exit(255);
|
|
|
|
}
|
2015-05-11 09:06:22 +02:00
|
|
|
|
|
|
|
line[0] = __KLIBC_ARG_NONZERO | __KLIBC_ARG_RESPONSE;
|
|
|
|
l = line + 1;
|
2016-03-20 03:25:31 +01:00
|
|
|
while (fgets(l, (filesize + 1) - (l - (line + 1)), f)) {
|
2015-05-11 09:06:22 +02:00
|
|
|
p = strchr(l, '\n');
|
|
|
|
if (p) {
|
2017-03-25 23:56:09 +01:00
|
|
|
/*
|
|
|
|
* if a line ends with a backslash,
|
|
|
|
* concatenate with the next line
|
2015-05-11 09:06:22 +02:00
|
|
|
*/
|
|
|
|
if (p > l && p[-1] == '\\') {
|
|
|
|
char *p1;
|
|
|
|
int count = 0;
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
for (p1 = p - 1; p1 >= l &&
|
|
|
|
*p1 == '\\'; p1--)
|
2015-05-11 09:06:22 +02:00
|
|
|
count++;
|
|
|
|
|
|
|
|
if (count & 1) {
|
|
|
|
l = p + 1;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = 0;
|
|
|
|
}
|
|
|
|
p = strdup(line);
|
|
|
|
if (!p)
|
|
|
|
goto exit_out_of_memory;
|
|
|
|
|
|
|
|
RPUT(p + 1);
|
|
|
|
|
|
|
|
l = line + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(line);
|
|
|
|
|
|
|
|
if (ferror(f)) {
|
|
|
|
fputs("Cannot read response file\n", stderr);
|
|
|
|
exit(255);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
RPUT(NULL);
|
|
|
|
--new_argc;
|
2015-05-11 09:06:22 +02:00
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
*argcp = new_argc;
|
|
|
|
*argvp = new_argv;
|
2015-05-11 09:06:22 +02:00
|
|
|
}
|
|
|
|
|
2015-06-03 05:20:05 +02:00
|
|
|
static void
|
|
|
|
init_extlibpath(void)
|
|
|
|
{
|
2017-03-25 23:56:09 +01:00
|
|
|
const char *vars[] = {
|
|
|
|
"BEGINLIBPATH",
|
|
|
|
"ENDLIBPATH",
|
|
|
|
"LIBPATHSTRICT",
|
|
|
|
NULL
|
|
|
|
};
|
2015-06-03 05:20:05 +02:00
|
|
|
char val[512];
|
|
|
|
int flag;
|
|
|
|
|
|
|
|
for (flag = 0; vars[flag]; flag++) {
|
|
|
|
DosQueryExtLIBPATH(val, flag + 1);
|
|
|
|
if (val[0])
|
|
|
|
setenv(vars[flag], val, 1);
|
2017-03-25 23:56:09 +01:00
|
|
|
}
|
2015-06-03 05:20:05 +02:00
|
|
|
}
|
|
|
|
|
2015-05-13 06:34:21 +02:00
|
|
|
/*
|
|
|
|
* Convert backslashes of environmental variables to forward slahes.
|
2017-03-25 23:56:09 +01:00
|
|
|
* A backslash may be used as an escaped character when doing 'echo'.
|
|
|
|
* This leads to an unexpected behavior.
|
2015-05-13 06:34:21 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
env_slashify(void)
|
|
|
|
{
|
|
|
|
/*
|
2017-03-25 23:56:09 +01:00
|
|
|
* PATH and TMPDIR are used by OS/2 as well. That is, they may
|
|
|
|
* have backslashes as a directory separator.
|
2015-06-03 05:20:05 +02:00
|
|
|
* BEGINLIBPATH and ENDLIBPATH are special variables on OS/2.
|
2015-05-13 06:34:21 +02:00
|
|
|
*/
|
2017-03-25 23:56:09 +01:00
|
|
|
const char *var_list[] = {
|
|
|
|
"PATH",
|
|
|
|
"TMPDIR",
|
|
|
|
"BEGINLIBPATH",
|
|
|
|
"ENDLIBPATH",
|
|
|
|
NULL
|
|
|
|
};
|
2015-05-13 06:34:21 +02:00
|
|
|
const char **var;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
for (var = var_list; *var; var++) {
|
|
|
|
value = getenv(*var);
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
_fnslashify(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
void
|
|
|
|
os2_init(int *argcp, const char ***argvp)
|
2015-05-11 09:06:22 +02:00
|
|
|
{
|
|
|
|
response(argcp, argvp);
|
2015-05-13 06:34:21 +02:00
|
|
|
|
2015-06-03 05:20:05 +02:00
|
|
|
init_extlibpath();
|
2015-05-13 06:34:21 +02:00
|
|
|
env_slashify();
|
2015-05-16 08:27:18 +02:00
|
|
|
|
2016-12-17 09:04:37 +01:00
|
|
|
if (!isatty(STDIN_FILENO))
|
|
|
|
setmode(STDIN_FILENO, O_BINARY);
|
2015-05-16 08:27:18 +02:00
|
|
|
if (!isatty(STDOUT_FILENO))
|
|
|
|
setmode(STDOUT_FILENO, O_BINARY);
|
|
|
|
if (!isatty(STDERR_FILENO))
|
|
|
|
setmode(STDERR_FILENO, O_BINARY);
|
2015-05-19 06:14:57 +02:00
|
|
|
|
|
|
|
atexit(cleanup);
|
2015-05-11 09:06:22 +02:00
|
|
|
}
|
2015-05-07 05:59:55 +02:00
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
void
|
|
|
|
setextlibpath(const char *name, const char *val)
|
2015-06-03 05:20:05 +02:00
|
|
|
{
|
|
|
|
int flag;
|
2017-03-25 23:56:09 +01:00
|
|
|
char *p, *cp;
|
2015-06-03 05:20:05 +02:00
|
|
|
|
|
|
|
if (!strcmp(name, "BEGINLIBPATH"))
|
|
|
|
flag = BEGIN_LIBPATH;
|
|
|
|
else if (!strcmp(name, "ENDLIBPATH"))
|
|
|
|
flag = END_LIBPATH;
|
|
|
|
else if (!strcmp(name, "LIBPATHSTRICT"))
|
|
|
|
flag = LIBPATHSTRICT;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
/* convert slashes to backslashes */
|
|
|
|
strdupx(cp, val, ATEMP);
|
|
|
|
for (p = cp; *p; p++) {
|
2015-06-03 05:20:05 +02:00
|
|
|
if (*p == '/')
|
|
|
|
*p = '\\';
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
DosSetExtLIBPATH(cp, flag);
|
2015-06-03 05:20:05 +02:00
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
afree(cp, ATEMP);
|
2015-06-03 05:20:05 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
/* remove trailing dots */
|
2015-05-07 05:59:55 +02:00
|
|
|
static char *
|
|
|
|
remove_trailing_dots(char *name)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
for (p = name + strlen(name); --p > name && *p == '.'; )
|
|
|
|
/* nothing */;
|
|
|
|
|
|
|
|
if (*p != '.' && *p != '/' && *p != '\\' && *p != ':')
|
|
|
|
p[1] = '\0';
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
return (name);
|
2015-05-07 05:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define REMOVE_TRAILING_DOTS(name) \
|
2017-03-26 00:05:25 +01:00
|
|
|
remove_trailing_dots(memcpy(alloca(strlen(name) + 1), name, strlen(name) + 1))
|
2015-05-07 05:59:55 +02:00
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
/* alias of stat() */
|
|
|
|
extern int _std_stat(const char *, struct stat *);
|
2015-05-07 05:59:55 +02:00
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
/* replacement for stat() of kLIBC which fails if there are trailing dots */
|
2015-05-07 05:59:55 +02:00
|
|
|
int
|
|
|
|
stat(const char *name, struct stat *buffer)
|
|
|
|
{
|
2017-03-25 23:56:09 +01:00
|
|
|
return (_std_stat(REMOVE_TRAILING_DOTS(name), buffer));
|
2015-05-07 05:59:55 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
/* alias of access() */
|
|
|
|
extern int _std_access(const char *, int);
|
2015-05-07 05:59:55 +02:00
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
/* replacement for access() of kLIBC which fails if there are trailing dots */
|
2015-05-07 05:59:55 +02:00
|
|
|
int
|
|
|
|
access(const char *name, int mode)
|
|
|
|
{
|
|
|
|
/*
|
2017-03-25 23:56:09 +01:00
|
|
|
* On OS/2 kLIBC, X_OK is set only for executable files.
|
|
|
|
* This prevents scripts from being executed.
|
2015-05-07 05:59:55 +02:00
|
|
|
*/
|
|
|
|
if (mode & X_OK)
|
|
|
|
mode = (mode & ~X_OK) | R_OK;
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
return (_std_access(REMOVE_TRAILING_DOTS(name), mode));
|
2015-05-07 05:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_X_SUFFIX_LEN 4
|
|
|
|
|
|
|
|
static const char *x_suffix_list[] =
|
2017-03-25 23:56:09 +01:00
|
|
|
{ "", ".ksh", ".exe", ".sh", ".cmd", ".com", ".bat", NULL };
|
2015-05-07 05:59:55 +02:00
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
/* call fn() by appending executable extensions */
|
2015-05-07 05:59:55 +02:00
|
|
|
static int
|
|
|
|
access_stat_ex(int (*fn)(), const char *name, void *arg)
|
|
|
|
{
|
|
|
|
char *x_name;
|
|
|
|
const char **x_suffix;
|
|
|
|
int rc = -1;
|
2017-03-26 00:05:25 +01:00
|
|
|
size_t x_namelen = strlen(name) + MAX_X_SUFFIX_LEN + 1;
|
2015-05-07 05:59:55 +02:00
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
/* otherwise, try to append executable suffixes */
|
2017-03-26 00:05:25 +01:00
|
|
|
x_name = alloc(x_namelen, ATEMP);
|
2015-05-07 05:59:55 +02:00
|
|
|
|
|
|
|
for (x_suffix = x_suffix_list; rc && *x_suffix; x_suffix++) {
|
2017-03-26 00:05:25 +01:00
|
|
|
strlcpy(x_name, name, x_namelen);
|
|
|
|
strlcat(x_name, *x_suffix, x_namelen);
|
2015-05-07 05:59:55 +02:00
|
|
|
|
|
|
|
rc = fn(x_name, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
afree(x_name, ATEMP);
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
return (rc);
|
2015-05-07 05:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* access()/search_access() version */
|
|
|
|
int
|
|
|
|
access_ex(int (*fn)(const char *, int), const char *name, int mode)
|
|
|
|
{
|
2017-03-26 00:05:25 +01:00
|
|
|
/*XXX this smells fishy --mirabilos */
|
2017-03-25 23:56:09 +01:00
|
|
|
return (access_stat_ex(fn, name, (void *)mode));
|
2015-05-07 05:59:55 +02:00
|
|
|
}
|
|
|
|
|
2015-05-11 06:43:56 +02:00
|
|
|
/* stat() version */
|
|
|
|
int
|
|
|
|
stat_ex(const char *name, struct stat *buffer)
|
|
|
|
{
|
2017-03-25 23:56:09 +01:00
|
|
|
return (access_stat_ex(stat, name, buffer));
|
2015-05-11 06:43:56 +02:00
|
|
|
}
|
|
|
|
|
2015-05-07 05:59:55 +02:00
|
|
|
static int
|
|
|
|
test_exec_exist(const char *name, char *real_name)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (stat(name, &sb) < 0 || !S_ISREG(sb.st_mode))
|
2017-03-25 23:56:09 +01:00
|
|
|
return (-1);
|
2015-05-07 05:59:55 +02:00
|
|
|
|
2017-03-26 00:05:25 +01:00
|
|
|
/* safe due to calculations in real_exec_name() */
|
|
|
|
memcpy(real_name, name, strlen(name) + 1);
|
2015-05-07 05:59:55 +02:00
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
return (0);
|
2015-05-07 05:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
real_exec_name(const char *name)
|
|
|
|
{
|
|
|
|
char x_name[strlen(name) + MAX_X_SUFFIX_LEN + 1];
|
|
|
|
const char *real_name = name;
|
|
|
|
|
|
|
|
if (access_stat_ex(test_exec_exist, real_name, x_name) != -1)
|
2017-03-26 00:05:25 +01:00
|
|
|
/*XXX memory leak */
|
2015-05-07 05:59:55 +02:00
|
|
|
strdupx(real_name, x_name, ATEMP);
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
return (real_name);
|
2015-05-07 05:59:55 +02:00
|
|
|
}
|
2017-03-25 23:56:09 +01:00
|
|
|
|
|
|
|
/* OS/2 can process a command line up to 32 KiB */
|
2015-05-12 09:15:25 +02:00
|
|
|
#define MAX_CMD_LINE_LEN 32768
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
/* make a response file to pass a very long command line */
|
2015-05-12 09:15:25 +02:00
|
|
|
static char *
|
|
|
|
make_response_file(char * const *argv)
|
|
|
|
{
|
2017-03-25 23:56:09 +01:00
|
|
|
char rsp_name_arg[] = "@mksh-rsp-XXXXXX";
|
2015-05-12 09:15:25 +02:00
|
|
|
char *rsp_name = &rsp_name_arg[1];
|
2017-03-25 23:56:09 +01:00
|
|
|
int arg_len = 0;
|
|
|
|
int i;
|
2015-05-12 09:15:25 +02:00
|
|
|
|
|
|
|
for (i = 0; argv[i]; i++)
|
|
|
|
arg_len += strlen(argv[i]) + 1;
|
|
|
|
|
|
|
|
/*
|
2017-03-25 23:56:09 +01:00
|
|
|
* If a length of command line is longer than MAX_CMD_LINE_LEN, then
|
|
|
|
* use a response file. OS/2 cannot process a command line longer
|
2017-04-02 17:00:45 +02:00
|
|
|
* than 32K. Of course, a response file cannot be recognised by a
|
2017-03-25 23:56:09 +01:00
|
|
|
* normal OS/2 program, that is, neither non-EMX or non-kLIBC. But
|
|
|
|
* it cannot accept a command line longer than 32K in itself. So
|
|
|
|
* using a response file in this case, is an acceptable solution.
|
2015-05-12 09:15:25 +02:00
|
|
|
*/
|
|
|
|
if (arg_len > MAX_CMD_LINE_LEN) {
|
|
|
|
int fd;
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
if ((fd = mkstemp(rsp_name)) == -1)
|
2017-03-25 23:56:09 +01:00
|
|
|
return (NULL);
|
2015-05-12 09:15:25 +02:00
|
|
|
|
|
|
|
/* write all the arguments except a 0th program name */
|
|
|
|
for (i = 1; argv[i]; i++) {
|
|
|
|
write(fd, argv[i], strlen(argv[i]));
|
2017-03-25 23:56:09 +01:00
|
|
|
write(fd, "\n", 1);
|
2015-05-12 09:15:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
2015-05-19 06:14:57 +02:00
|
|
|
add_temp(rsp_name);
|
2015-05-12 09:15:25 +02:00
|
|
|
strdupx(result, rsp_name_arg, ATEMP);
|
2017-03-25 23:56:09 +01:00
|
|
|
return (result);
|
2015-05-12 09:15:25 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
return (NULL);
|
2015-05-12 09:15:25 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
/* alias of execve() */
|
|
|
|
extern int _std_execve(const char *, char * const *, char * const *);
|
2015-05-12 09:15:25 +02:00
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
/* replacement for execve() of kLIBC */
|
|
|
|
int
|
|
|
|
execve(const char *name, char * const *argv, char * const *envp)
|
2015-05-12 09:15:25 +02:00
|
|
|
{
|
|
|
|
const char *exec_name;
|
|
|
|
FILE *fp;
|
|
|
|
char sign[2];
|
2015-05-19 06:14:57 +02:00
|
|
|
char *rsp_argv[3];
|
2015-05-12 09:15:25 +02:00
|
|
|
char *rsp_name_arg;
|
2015-05-19 06:14:57 +02:00
|
|
|
int pid;
|
|
|
|
int status;
|
|
|
|
int fd;
|
2015-05-12 09:15:25 +02:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* #! /bin/sh : append .exe
|
|
|
|
* extproc sh : search sh.exe in PATH
|
|
|
|
*/
|
|
|
|
exec_name = search_path(name, path, X_OK, NULL);
|
|
|
|
if (!exec_name) {
|
|
|
|
errno = ENOENT;
|
2017-03-25 23:56:09 +01:00
|
|
|
return (-1);
|
2015-05-12 09:15:25 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
/*-
|
2015-05-12 09:15:25 +02:00
|
|
|
* kLIBC execve() has problems when executing scripts.
|
|
|
|
* 1. it fails to execute a script if a directory whose name
|
2017-03-25 23:56:09 +01:00
|
|
|
* is same as an interpreter exists in a current directory.
|
2015-05-12 09:15:25 +02:00
|
|
|
* 2. it fails to execute a script not starting with sharpbang.
|
|
|
|
* 3. it fails to execute a batch file if COMSPEC is set to a shell
|
2017-03-25 23:56:09 +01:00
|
|
|
* incompatible with cmd.exe, such as /bin/sh.
|
2015-05-12 09:15:25 +02:00
|
|
|
* And ksh process scripts more well, so let ksh process scripts.
|
|
|
|
*/
|
|
|
|
errno = 0;
|
|
|
|
if (!(fp = fopen(exec_name, "rb")))
|
|
|
|
errno = ENOEXEC;
|
|
|
|
|
|
|
|
if (!errno && fread(sign, 1, sizeof(sign), fp) != sizeof(sign))
|
|
|
|
errno = ENOEXEC;
|
|
|
|
|
|
|
|
if (fp && fclose(fp))
|
|
|
|
errno = ENOEXEC;
|
|
|
|
|
|
|
|
if (!errno &&
|
|
|
|
!((sign[0] == 'M' && sign[1] == 'Z') ||
|
|
|
|
(sign[0] == 'N' && sign[1] == 'E') ||
|
|
|
|
(sign[0] == 'L' && sign[1] == 'X')))
|
|
|
|
errno = ENOEXEC;
|
|
|
|
|
|
|
|
if (errno == ENOEXEC)
|
2017-03-25 23:56:09 +01:00
|
|
|
return (-1);
|
2015-05-12 09:15:25 +02:00
|
|
|
|
2015-05-19 06:14:57 +02:00
|
|
|
rsp_name_arg = make_response_file(argv);
|
|
|
|
|
2015-05-12 09:15:25 +02:00
|
|
|
if (rsp_name_arg) {
|
2015-05-19 06:14:57 +02:00
|
|
|
rsp_argv[0] = argv[0];
|
|
|
|
rsp_argv[1] = rsp_name_arg;
|
|
|
|
rsp_argv[2] = NULL;
|
2015-05-12 09:15:25 +02:00
|
|
|
|
2015-05-19 06:14:57 +02:00
|
|
|
argv = rsp_argv;
|
|
|
|
}
|
2015-05-12 09:15:25 +02:00
|
|
|
|
2015-05-19 06:14:57 +02:00
|
|
|
pid = spawnve(P_NOWAIT, exec_name, argv, envp);
|
2015-05-12 09:15:25 +02:00
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
afree(rsp_name_arg, ATEMP);
|
2015-05-12 09:15:25 +02:00
|
|
|
|
2015-05-19 06:14:57 +02:00
|
|
|
if (pid == -1) {
|
|
|
|
cleanup_temps();
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
return (-1);
|
2015-05-19 06:14:57 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
/* close all opened handles */
|
2015-05-19 06:14:57 +02:00
|
|
|
for (fd = 0; fd < NUFILE; fd++) {
|
|
|
|
if (fcntl(fd, F_GETFD) == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((rc = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
|
2017-03-25 23:56:09 +01:00
|
|
|
/* nothing */;
|
2015-05-19 06:14:57 +02:00
|
|
|
|
|
|
|
cleanup_temps();
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
/* Is this possible? And is this right? */
|
2015-05-19 06:14:57 +02:00
|
|
|
if (rc == -1)
|
2017-03-25 23:56:09 +01:00
|
|
|
return (-1);
|
2015-05-19 06:14:57 +02:00
|
|
|
|
2016-12-21 12:02:56 +01:00
|
|
|
if (WIFSIGNALED(status))
|
|
|
|
_exit(ksh_sigmask(WTERMSIG(status)));
|
|
|
|
|
2015-05-19 06:14:57 +02:00
|
|
|
_exit(WEXITSTATUS(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct temp *templist = NULL;
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
static void
|
|
|
|
add_temp(const char *name)
|
2015-05-19 06:14:57 +02:00
|
|
|
{
|
|
|
|
struct temp *tp;
|
|
|
|
|
|
|
|
tp = alloc(offsetof(struct temp, tffn[0]) + strlen(name) + 1, APERM);
|
2017-03-26 00:05:25 +01:00
|
|
|
memcpy(tp->tffn, name, strlen(name) + 1);
|
2015-05-19 06:14:57 +02:00
|
|
|
tp->next = templist;
|
|
|
|
templist = tp;
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
/* alias of unlink() */
|
|
|
|
extern int _std_unlink(const char *);
|
2015-05-19 06:14:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Replacement for unlink() of kLIBC not supporting to remove files used by
|
|
|
|
* another processes.
|
|
|
|
*/
|
2017-03-25 23:56:09 +01:00
|
|
|
int
|
|
|
|
unlink(const char *name)
|
2015-05-19 06:14:57 +02:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = _std_unlink(name);
|
|
|
|
if (rc == -1 && errno != ENOENT)
|
|
|
|
add_temp(name);
|
|
|
|
|
2017-03-25 23:56:09 +01:00
|
|
|
return (rc);
|
2015-05-12 09:15:25 +02:00
|
|
|
}
|
2015-05-19 06:14:57 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup_temps(void)
|
|
|
|
{
|
|
|
|
struct temp *tp;
|
|
|
|
struct temp **tpnext;
|
|
|
|
|
|
|
|
for (tpnext = &templist, tp = templist; tp; tp = *tpnext) {
|
|
|
|
if (_std_unlink(tp->tffn) == 0 || errno == ENOENT) {
|
|
|
|
*tpnext = tp->next;
|
|
|
|
afree(tp, APERM);
|
|
|
|
} else {
|
|
|
|
tpnext = &tp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup(void)
|
|
|
|
{
|
|
|
|
cleanup_temps();
|
|
|
|
}
|