2021-06-05 09:27:46 +00:00
|
|
|
/* radare - LGPL - Copyright 2014-2021 - pancake */
|
2014-07-09 00:24:31 +00:00
|
|
|
|
|
|
|
/* this helper api is here because it depends on r_util and r_socket */
|
|
|
|
/* we should find a better place for it. r_io? */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <r_socket.h>
|
2017-05-09 12:25:57 +00:00
|
|
|
#include <r_util.h>
|
2016-04-04 18:34:26 +00:00
|
|
|
#include <r_lib.h>
|
2020-07-03 16:59:24 +00:00
|
|
|
#include <r_cons.h>
|
2015-09-23 07:48:30 +00:00
|
|
|
#include <sys/stat.h>
|
2015-10-14 21:50:16 +00:00
|
|
|
#include <sys/types.h>
|
2017-12-31 11:01:25 +00:00
|
|
|
|
|
|
|
#if __APPLE__ && LIBC_HAVE_FORK
|
2016-04-26 23:52:35 +00:00
|
|
|
#if !__POWERPC__
|
2014-07-09 00:24:31 +00:00
|
|
|
#include <spawn.h>
|
2016-04-26 23:52:35 +00:00
|
|
|
#endif
|
2014-07-09 00:24:31 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <mach/exception_types.h>
|
|
|
|
#include <mach/mach_init.h>
|
|
|
|
#include <mach/mach_port.h>
|
|
|
|
#include <mach/mach_traps.h>
|
|
|
|
#include <mach/task.h>
|
|
|
|
#include <mach/task_info.h>
|
|
|
|
#include <mach/thread_act.h>
|
|
|
|
#include <mach/thread_info.h>
|
|
|
|
#include <mach/vm_map.h>
|
|
|
|
#include <mach-o/loader.h>
|
|
|
|
#include <mach-o/nlist.h>
|
|
|
|
#endif
|
2017-12-31 11:01:25 +00:00
|
|
|
|
2014-07-09 00:24:31 +00:00
|
|
|
#if __UNIX__
|
2015-09-23 07:48:30 +00:00
|
|
|
#include <sys/ioctl.h>
|
2014-07-09 00:24:31 +00:00
|
|
|
#include <sys/resource.h>
|
2021-06-20 15:14:43 +00:00
|
|
|
#ifndef __wasi__
|
2014-11-10 02:06:41 +00:00
|
|
|
#include <grp.h>
|
2021-06-20 15:14:43 +00:00
|
|
|
#endif
|
2015-03-26 14:19:12 +00:00
|
|
|
#include <errno.h>
|
2017-01-17 23:47:46 +00:00
|
|
|
#if defined(__sun)
|
|
|
|
#include <sys/filio.h>
|
|
|
|
#endif
|
2014-07-09 00:25:43 +00:00
|
|
|
#if __linux__ && !__ANDROID__
|
2014-07-09 00:24:31 +00:00
|
|
|
#include <sys/personality.h>
|
2015-09-23 07:48:30 +00:00
|
|
|
#include <pty.h>
|
2016-12-29 19:30:05 +00:00
|
|
|
#include <utmp.h>
|
2014-07-09 00:24:31 +00:00
|
|
|
#endif
|
2015-10-02 00:40:52 +00:00
|
|
|
#if defined(__APPLE__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
#include <util.h>
|
2018-11-06 23:31:00 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined(__DragonFly__)
|
2019-02-21 01:29:31 +00:00
|
|
|
#include <sys/sysctl.h>
|
2018-05-28 16:47:49 +00:00
|
|
|
#include <libutil.h>
|
2015-10-02 00:40:52 +00:00
|
|
|
#endif
|
2014-07-09 00:24:31 +00:00
|
|
|
#endif
|
2017-05-09 21:53:20 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <direct.h> // to compile chdir in msvc windows
|
|
|
|
#include <process.h> // to compile execv in msvc windows
|
2020-09-25 11:51:23 +00:00
|
|
|
#define pid_t int
|
2017-05-09 21:53:20 +00:00
|
|
|
#endif
|
2014-07-09 00:24:31 +00:00
|
|
|
|
2016-12-15 14:06:06 +00:00
|
|
|
|
2020-04-19 10:38:55 +00:00
|
|
|
#if HAVE_PTY
|
|
|
|
static int (*dyn_openpty)(int *amaster, int *aslave, char *name, struct termios *termp, struct winsize *winp) = NULL;
|
|
|
|
static int (*dyn_login_tty)(int fd) = NULL;
|
|
|
|
static id_t (*dyn_forkpty)(int *amaster, char *name, struct termios *termp, struct winsize *winp) = NULL;
|
2020-04-20 15:04:30 +00:00
|
|
|
static void dyn_init(void) {
|
|
|
|
if (!dyn_openpty) {
|
|
|
|
dyn_openpty = r_lib_dl_sym (NULL, "openpty");
|
|
|
|
}
|
|
|
|
if (!dyn_login_tty) {
|
2021-03-29 09:11:38 +00:00
|
|
|
dyn_login_tty = r_lib_dl_sym (NULL, "login_tty");
|
2020-04-20 15:04:30 +00:00
|
|
|
}
|
|
|
|
if (!dyn_forkpty) {
|
2021-03-29 09:11:38 +00:00
|
|
|
dyn_forkpty = r_lib_dl_sym (NULL, "forkpty");
|
2020-04-20 15:04:30 +00:00
|
|
|
}
|
2021-03-29 09:11:38 +00:00
|
|
|
#if __UNIX__
|
|
|
|
// attempt to fall back on libutil if we failed to load anything
|
|
|
|
if (!(dyn_openpty && dyn_login_tty && dyn_forkpty)) {
|
|
|
|
void *libutil;
|
|
|
|
if (!(libutil = r_lib_dl_open ("libutil." R_LIB_EXT))) {
|
|
|
|
eprintf ("[ERROR] rarun2: Could not find PTY utils, failed to load %s\n", "libutil." R_LIB_EXT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!dyn_openpty) {
|
|
|
|
dyn_openpty = r_lib_dl_sym (libutil, "openpty");
|
|
|
|
}
|
|
|
|
if (!dyn_login_tty) {
|
|
|
|
dyn_login_tty = r_lib_dl_sym (libutil, "login_tty");
|
|
|
|
}
|
|
|
|
if (!dyn_forkpty) {
|
|
|
|
dyn_forkpty = r_lib_dl_sym (libutil, "forkpty");
|
|
|
|
}
|
2021-04-03 01:50:08 +00:00
|
|
|
r_lib_dl_close (libutil);
|
2021-03-29 09:11:38 +00:00
|
|
|
}
|
|
|
|
#endif
|
2020-04-20 15:04:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 10:38:55 +00:00
|
|
|
#endif
|
|
|
|
|
2014-07-09 00:24:31 +00:00
|
|
|
R_API RRunProfile *r_run_new(const char *str) {
|
2017-04-26 17:35:37 +00:00
|
|
|
RRunProfile *p = R_NEW0 (RRunProfile);
|
2014-07-09 01:48:47 +00:00
|
|
|
if (p) {
|
|
|
|
r_run_reset (p);
|
2020-07-01 01:33:38 +00:00
|
|
|
if (str) {
|
|
|
|
r_run_parsefile (p, str);
|
|
|
|
}
|
2014-07-09 01:48:47 +00:00
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2014-07-09 01:48:47 +00:00
|
|
|
R_API void r_run_reset(RRunProfile *p) {
|
2020-07-01 01:33:38 +00:00
|
|
|
r_return_if_fail (p);
|
2014-07-09 01:48:47 +00:00
|
|
|
memset (p, 0, sizeof (RRunProfile));
|
|
|
|
p->_aslr = -1;
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:35:37 +00:00
|
|
|
R_API bool r_run_parse(RRunProfile *pf, const char *profile) {
|
2020-07-01 01:33:38 +00:00
|
|
|
r_return_val_if_fail (pf && profile, false);
|
2017-04-15 11:29:27 +00:00
|
|
|
char *p, *o, *str = strdup (profile);
|
2017-04-12 15:56:19 +00:00
|
|
|
if (!str) {
|
2017-04-26 17:35:37 +00:00
|
|
|
return false;
|
2017-04-12 15:56:19 +00:00
|
|
|
}
|
|
|
|
r_str_replace_char (str, '\r',0);
|
2020-07-24 08:25:55 +00:00
|
|
|
p = str;
|
|
|
|
while (p) {
|
|
|
|
if ((o = strchr (p, '\n'))) {
|
|
|
|
*o++ = 0;
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
r_run_parseline (pf, p);
|
2020-07-24 08:25:55 +00:00
|
|
|
p = o;
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
free (str);
|
2017-04-26 17:35:37 +00:00
|
|
|
return true;
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 01:33:38 +00:00
|
|
|
R_API void r_run_free(RRunProfile *r) {
|
|
|
|
if (r) {
|
|
|
|
free (r->_system);
|
|
|
|
free (r->_program);
|
|
|
|
free (r->_runlib);
|
|
|
|
free (r->_runlib_fcn);
|
|
|
|
free (r->_stdio);
|
|
|
|
free (r->_stdin);
|
|
|
|
free (r->_stdout);
|
|
|
|
free (r->_stderr);
|
|
|
|
free (r->_chgdir);
|
|
|
|
free (r->_chroot);
|
|
|
|
free (r->_libpath);
|
|
|
|
free (r->_preload);
|
|
|
|
free (r);
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if __UNIX__
|
|
|
|
static void set_limit(int n, int a, ut64 b) {
|
2021-06-20 15:14:43 +00:00
|
|
|
#ifndef __wasi__
|
2014-07-09 00:24:31 +00:00
|
|
|
if (n) {
|
|
|
|
struct rlimit cl = {b, b};
|
|
|
|
setrlimit (RLIMIT_CORE, &cl);
|
|
|
|
} else {
|
|
|
|
struct rlimit cl = {0, 0};
|
|
|
|
setrlimit (a, &cl);
|
|
|
|
}
|
2021-06-20 15:14:43 +00:00
|
|
|
#endif
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char *getstr(const char *src) {
|
|
|
|
int len;
|
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
switch (*src) {
|
|
|
|
case '\'':
|
|
|
|
ret = strdup (src+1);
|
|
|
|
if (ret) {
|
|
|
|
len = strlen (ret);
|
2017-05-18 22:39:33 +00:00
|
|
|
if (len > 0) {
|
2014-07-09 00:24:31 +00:00
|
|
|
len--;
|
2017-05-18 22:39:33 +00:00
|
|
|
if (ret[len] == '\'') {
|
2014-07-09 00:24:31 +00:00
|
|
|
ret[len] = 0;
|
|
|
|
return ret;
|
2017-05-18 22:39:33 +00:00
|
|
|
}
|
|
|
|
eprintf ("Missing \"\n");
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
free (ret);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
case '"':
|
2016-11-04 10:26:47 +00:00
|
|
|
ret = strdup (src + 1);
|
2014-07-09 00:24:31 +00:00
|
|
|
if (ret) {
|
|
|
|
len = strlen (ret);
|
2016-11-04 10:26:47 +00:00
|
|
|
if (len > 0) {
|
2014-07-09 00:24:31 +00:00
|
|
|
len--;
|
2016-11-04 10:26:47 +00:00
|
|
|
if (ret[len] == '"') {
|
2014-07-09 00:24:31 +00:00
|
|
|
ret[len] = 0;
|
|
|
|
r_str_unescape (ret);
|
|
|
|
return ret;
|
2016-11-04 10:26:47 +00:00
|
|
|
}
|
|
|
|
eprintf ("Missing \"\n");
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
free (ret);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
case '@':
|
2014-10-23 01:56:10 +00:00
|
|
|
{
|
2016-11-04 10:26:47 +00:00
|
|
|
char *pat = strchr (src + 1, '@');
|
2014-10-23 01:56:10 +00:00
|
|
|
if (pat) {
|
2020-04-12 18:45:24 +00:00
|
|
|
size_t len;
|
|
|
|
long i, rep;
|
2014-10-23 01:56:10 +00:00
|
|
|
*pat++ = 0;
|
2020-04-12 18:45:24 +00:00
|
|
|
rep = strtol (src + 1, NULL, 10);
|
2015-12-05 18:57:06 +00:00
|
|
|
len = strlen (pat);
|
2016-11-04 10:26:47 +00:00
|
|
|
if (rep > 0) {
|
2014-10-23 02:45:01 +00:00
|
|
|
char *buf = malloc (rep);
|
2016-05-24 20:22:15 +00:00
|
|
|
if (buf) {
|
|
|
|
for (i = 0; i < rep; i++) {
|
2020-04-12 18:45:24 +00:00
|
|
|
buf[i] = pat[i % len];
|
2016-05-24 20:22:15 +00:00
|
|
|
}
|
2014-10-23 02:45:01 +00:00
|
|
|
}
|
|
|
|
return buf;
|
2014-10-23 01:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// slurp file
|
2016-05-24 20:22:15 +00:00
|
|
|
return r_file_slurp (src + 1, NULL);
|
2014-10-23 01:56:10 +00:00
|
|
|
}
|
2016-12-18 00:01:16 +00:00
|
|
|
case '`':
|
|
|
|
{
|
|
|
|
char *msg = strdup (src + 1);
|
|
|
|
int msg_len = strlen (msg);
|
|
|
|
if (msg_len > 0) {
|
|
|
|
msg [msg_len - 1] = 0;
|
2020-03-02 20:39:37 +00:00
|
|
|
char *ret = r_sys_cmd_str (msg, NULL, NULL);
|
|
|
|
r_str_trim_tail (ret);
|
2016-12-18 00:01:16 +00:00
|
|
|
free (msg);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-12-21 20:36:14 +00:00
|
|
|
free (msg);
|
2016-12-18 00:01:16 +00:00
|
|
|
return strdup ("");
|
|
|
|
}
|
2014-10-20 11:39:37 +00:00
|
|
|
case '!':
|
2020-03-02 20:39:37 +00:00
|
|
|
{
|
|
|
|
char *a = r_sys_cmd_str (src + 1, NULL, NULL);
|
|
|
|
r_str_trim_tail (a);
|
|
|
|
return a;
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
case ':':
|
2016-11-04 10:26:47 +00:00
|
|
|
if (src[1] == '!') {
|
2020-03-02 20:39:37 +00:00
|
|
|
ret = r_sys_cmd_str (src + 1, NULL, NULL);
|
|
|
|
r_str_trim_tail (ret); // why no head :?
|
2014-10-28 15:45:19 +00:00
|
|
|
} else {
|
|
|
|
ret = strdup (src);
|
|
|
|
}
|
2016-11-04 10:26:47 +00:00
|
|
|
len = r_hex_str2bin (src + 1, (ut8*)ret);
|
2016-05-24 20:22:15 +00:00
|
|
|
if (len > 0) {
|
2014-07-09 00:24:31 +00:00
|
|
|
ret[len] = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
2016-11-04 10:26:47 +00:00
|
|
|
eprintf ("Invalid hexpair string\n");
|
|
|
|
free (ret);
|
|
|
|
return NULL;
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
r_str_unescape ((ret = strdup (src)));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-02-14 21:08:51 +00:00
|
|
|
static int parseBool(const char *e) {
|
2014-07-09 00:24:31 +00:00
|
|
|
return (strcmp (e, "yes")?
|
2018-04-14 08:46:08 +00:00
|
|
|
(strcmp (e, "on")?
|
2014-07-09 00:24:31 +00:00
|
|
|
(strcmp (e, "true")?
|
|
|
|
(strcmp (e, "1")?
|
2018-04-14 08:46:08 +00:00
|
|
|
0: 1): 1): 1): 1);
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: move into r_util? r_run_... ? with the rest of funcs?
|
2018-01-28 21:56:46 +00:00
|
|
|
static void setASLR(RRunProfile *r, int enabled) {
|
2014-07-09 00:24:31 +00:00
|
|
|
#if __linux__
|
2019-02-21 22:03:20 +00:00
|
|
|
r_sys_aslr (enabled);
|
2019-03-03 13:10:44 +00:00
|
|
|
#if HAVE_DECL_ADDR_NO_RANDOMIZE && !__ANDROID__
|
2019-02-21 22:03:20 +00:00
|
|
|
if (personality (ADDR_NO_RANDOMIZE) == -1) {
|
2014-07-09 00:25:43 +00:00
|
|
|
#endif
|
2019-02-21 22:03:20 +00:00
|
|
|
r_sys_aslr (0);
|
2019-03-03 13:10:44 +00:00
|
|
|
#if HAVE_DECL_ADDR_NO_RANDOMIZE && !__ANDROID__
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
2019-03-03 13:10:44 +00:00
|
|
|
#endif
|
2014-07-09 00:24:31 +00:00
|
|
|
#elif __APPLE__
|
2015-08-16 15:45:23 +00:00
|
|
|
// TOO OLD setenv ("DYLD_NO_PIE", "1", 1);
|
2015-09-30 11:10:49 +00:00
|
|
|
// disable this because its
|
2018-01-28 21:56:46 +00:00
|
|
|
const char *argv0 = r->_system ? r->_system
|
|
|
|
: r->_program ? r->_program
|
|
|
|
: r->_args[0] ? r->_args[0]
|
|
|
|
: "/path/to/exec";
|
|
|
|
eprintf ("To disable aslr patch mach0.hdr.flags with:\n"
|
|
|
|
"r2 -qwnc 'wx 000000 @ 0x18' %s\n", argv0);
|
|
|
|
// f MH_PIE=0x00200000; wB-MH_PIE @ 24\n");
|
2014-07-09 00:24:31 +00:00
|
|
|
// for osxver>=10.7
|
|
|
|
// "unset the MH_PIE bit in an already linked executable" with --no-pie flag of the script
|
2015-08-16 15:45:23 +00:00
|
|
|
// the right way is to disable the aslr bit in the spawn call
|
2020-10-22 08:26:53 +00:00
|
|
|
#elif __FreeBSD__ || __NetBSD__ || __DragonFly__
|
2019-02-21 22:03:20 +00:00
|
|
|
r_sys_aslr (enabled);
|
2014-07-09 00:24:31 +00:00
|
|
|
#else
|
|
|
|
// not supported for this platform
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-10-13 10:42:57 +00:00
|
|
|
#if __APPLE__ && !__POWERPC__
|
|
|
|
#else
|
2018-05-15 10:11:34 +00:00
|
|
|
#if HAVE_PTY
|
2018-02-14 21:08:51 +00:00
|
|
|
static void restore_saved_fd(int saved, bool restore, int fd) {
|
2017-01-22 08:58:51 +00:00
|
|
|
if (saved == -1) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-14 21:08:51 +00:00
|
|
|
if (restore) {
|
2017-01-22 08:58:51 +00:00
|
|
|
dup2 (saved, fd);
|
|
|
|
}
|
|
|
|
close (saved);
|
|
|
|
}
|
2018-05-15 10:11:34 +00:00
|
|
|
#endif
|
2017-01-22 08:58:51 +00:00
|
|
|
|
2018-02-14 21:08:51 +00:00
|
|
|
static int handle_redirection_proc(const char *cmd, bool in, bool out, bool err) {
|
2016-12-15 14:06:06 +00:00
|
|
|
#if HAVE_PTY
|
2020-04-19 10:38:55 +00:00
|
|
|
if (!dyn_forkpty) {
|
|
|
|
// No forkpty api found, maybe we should fallback to just fork without any pty allocated
|
|
|
|
return -1;
|
|
|
|
}
|
2015-09-23 07:48:30 +00:00
|
|
|
// use PTY to redirect I/O because pipes can be problematic in
|
|
|
|
// case of interactive programs.
|
|
|
|
int saved_stdin = dup (STDIN_FILENO);
|
2017-07-07 14:58:52 +00:00
|
|
|
if (saved_stdin == -1) {
|
|
|
|
return -1;
|
2017-01-22 08:58:51 +00:00
|
|
|
}
|
2017-07-07 14:58:52 +00:00
|
|
|
int saved_stdout = dup (STDOUT_FILENO);
|
2020-04-19 10:38:55 +00:00
|
|
|
if (saved_stdout == -1) {
|
2017-07-07 14:58:52 +00:00
|
|
|
close (saved_stdin);
|
|
|
|
return -1;
|
2017-01-22 08:58:51 +00:00
|
|
|
}
|
2020-04-19 10:38:55 +00:00
|
|
|
|
|
|
|
int fdm, pid = dyn_forkpty (&fdm, NULL, NULL, NULL);
|
2018-02-16 05:31:05 +00:00
|
|
|
if (pid == -1) {
|
|
|
|
close (saved_stdin);
|
2018-02-19 12:17:21 +00:00
|
|
|
close (saved_stdout);
|
2018-02-16 05:31:05 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const char *tn = ttyname (fdm);
|
|
|
|
if (!tn) {
|
|
|
|
close (saved_stdin);
|
2018-02-19 12:17:21 +00:00
|
|
|
close (saved_stdout);
|
2018-02-16 05:31:05 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int fds = open (tn, O_RDWR);
|
|
|
|
if (fds == -1) {
|
|
|
|
close (saved_stdin);
|
2018-02-19 12:17:21 +00:00
|
|
|
close (saved_stdout);
|
2018-02-16 05:31:05 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-01-22 08:58:51 +00:00
|
|
|
if (pid == 0) {
|
2018-02-14 21:08:51 +00:00
|
|
|
close (fdm);
|
2017-07-07 14:58:52 +00:00
|
|
|
// child process
|
|
|
|
if (in) {
|
2018-02-14 21:08:51 +00:00
|
|
|
dup2 (fds, STDIN_FILENO);
|
2017-07-07 14:58:52 +00:00
|
|
|
}
|
|
|
|
if (out) {
|
2018-02-14 21:08:51 +00:00
|
|
|
dup2 (fds, STDOUT_FILENO);
|
2017-07-07 14:58:52 +00:00
|
|
|
}
|
2015-09-23 07:48:30 +00:00
|
|
|
// child - program to run
|
|
|
|
|
|
|
|
// necessary because otherwise you can read the same thing you
|
|
|
|
// wrote on fdm.
|
2017-01-22 08:58:51 +00:00
|
|
|
struct termios t;
|
2018-02-14 21:08:51 +00:00
|
|
|
tcgetattr (fds, &t);
|
2016-06-14 01:44:26 +00:00
|
|
|
cfmakeraw (&t);
|
2018-02-14 21:08:51 +00:00
|
|
|
tcsetattr (fds, TCSANOW, &t);
|
2015-09-23 07:48:30 +00:00
|
|
|
|
2017-01-22 08:58:51 +00:00
|
|
|
int code = r_sys_cmd (cmd);
|
|
|
|
restore_saved_fd (saved_stdin, in, STDIN_FILENO);
|
|
|
|
restore_saved_fd (saved_stdout, out, STDOUT_FILENO);
|
|
|
|
exit (code);
|
2017-07-07 14:58:52 +00:00
|
|
|
} else {
|
2018-02-14 21:08:51 +00:00
|
|
|
close (fds);
|
|
|
|
if (in) {
|
|
|
|
dup2 (fdm, STDIN_FILENO);
|
|
|
|
}
|
|
|
|
if (out) {
|
|
|
|
dup2 (fdm, STDOUT_FILENO);
|
|
|
|
}
|
2017-07-07 14:58:52 +00:00
|
|
|
// parent process
|
|
|
|
int status;
|
|
|
|
waitpid (pid, &status, 0);
|
2016-06-14 01:44:26 +00:00
|
|
|
}
|
2017-01-22 08:58:51 +00:00
|
|
|
|
|
|
|
// parent
|
|
|
|
close (saved_stdin);
|
|
|
|
close (saved_stdout);
|
|
|
|
return 0;
|
2015-09-23 07:48:30 +00:00
|
|
|
#else
|
2017-05-09 12:25:57 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma message ("TODO: handle_redirection_proc: Not implemented for this platform")
|
|
|
|
#else
|
2015-09-23 07:48:30 +00:00
|
|
|
#warning handle_redirection_proc : unimplemented for this platform
|
2017-05-09 12:25:57 +00:00
|
|
|
#endif
|
2015-09-23 07:48:30 +00:00
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
2020-10-13 10:42:57 +00:00
|
|
|
#endif
|
2015-09-23 07:48:30 +00:00
|
|
|
|
|
|
|
static int handle_redirection(const char *cmd, bool in, bool out, bool err) {
|
2017-04-27 22:16:19 +00:00
|
|
|
#if __APPLE__ && !__POWERPC__
|
|
|
|
//XXX handle this in other layer since things changes a little bit
|
|
|
|
//this seems like a really good place to refactor stuff
|
|
|
|
return 0;
|
2020-07-01 01:33:38 +00:00
|
|
|
#else
|
2020-07-24 08:25:55 +00:00
|
|
|
if (!cmd || !*cmd) {
|
2020-07-01 01:33:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-01-13 21:14:56 +00:00
|
|
|
if (cmd[0] == '"') {
|
2021-06-20 15:14:43 +00:00
|
|
|
#ifdef __wasi__
|
|
|
|
eprintf ("[ERROR] rarun2: Cannot create pipe\n");
|
|
|
|
#elif __UNIX__
|
2016-01-13 21:14:56 +00:00
|
|
|
if (in) {
|
|
|
|
int pipes[2];
|
|
|
|
if (pipe (pipes) != -1) {
|
2018-12-12 21:04:11 +00:00
|
|
|
size_t cmdl = strlen (cmd)-2;
|
|
|
|
if (write (pipes[1], cmd + 1, cmdl) != cmdl) {
|
|
|
|
eprintf ("[ERROR] rarun2: Cannot write to the pipe\n");
|
|
|
|
close (0);
|
|
|
|
return 1;
|
|
|
|
}
|
2018-12-16 13:54:40 +00:00
|
|
|
if (write (pipes[1], "\n", 1) != 1) {
|
|
|
|
eprintf ("[ERROR] rarun2: Cannot write to the pipe\n");
|
|
|
|
close (0);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-01-13 21:14:56 +00:00
|
|
|
close (0);
|
|
|
|
dup2 (pipes[0], 0);
|
|
|
|
} else {
|
|
|
|
eprintf ("[ERROR] rarun2: Cannot create pipe\n");
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 03:29:45 +00:00
|
|
|
#else
|
2017-05-09 12:25:57 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma message ("string redirection handle not yet done")
|
|
|
|
#else
|
2016-01-20 03:29:45 +00:00
|
|
|
#warning quoted string redirection handle not yet done
|
2017-05-09 12:25:57 +00:00
|
|
|
#endif
|
2016-01-20 03:29:45 +00:00
|
|
|
#endif
|
2016-01-13 21:14:56 +00:00
|
|
|
} else if (cmd[0] == '!') {
|
2015-09-23 07:48:30 +00:00
|
|
|
// redirection to a process
|
|
|
|
return handle_redirection_proc (cmd + 1, in, out, err);
|
|
|
|
} else {
|
|
|
|
// redirection to a file
|
|
|
|
int f, flag = 0, mode = 0;
|
|
|
|
flag |= in ? O_RDONLY : 0;
|
|
|
|
flag |= out ? O_WRONLY | O_CREAT : 0;
|
|
|
|
flag |= err ? O_WRONLY | O_CREAT : 0;
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
mode = _S_IREAD | _S_IWRITE;
|
|
|
|
#else
|
|
|
|
mode = S_IRUSR | S_IWUSR;
|
|
|
|
#endif
|
|
|
|
f = open (cmd, flag, mode);
|
|
|
|
if (f < 0) {
|
2016-01-13 21:14:56 +00:00
|
|
|
eprintf ("[ERROR] rarun2: Cannot open: %s\n", cmd);
|
2015-09-23 07:48:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2021-06-20 15:14:43 +00:00
|
|
|
#ifndef __wasi__
|
2016-01-13 21:14:56 +00:00
|
|
|
#define DUP(x) { close(x); dup2(f,x); }
|
2017-04-27 22:16:19 +00:00
|
|
|
if (in) {
|
|
|
|
DUP(0);
|
|
|
|
}
|
|
|
|
if (out) {
|
|
|
|
DUP(1);
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
DUP(2);
|
|
|
|
}
|
2021-06-20 15:14:43 +00:00
|
|
|
#endif
|
2015-09-30 15:06:59 +00:00
|
|
|
close (f);
|
2015-09-23 07:48:30 +00:00
|
|
|
}
|
2020-07-01 01:33:38 +00:00
|
|
|
return 0;
|
|
|
|
#endif
|
2015-09-23 07:48:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 01:33:38 +00:00
|
|
|
R_API bool r_run_parsefile(RRunProfile *p, const char *b) {
|
|
|
|
r_return_val_if_fail (p && b, false);
|
2014-07-09 01:48:47 +00:00
|
|
|
char *s = r_file_slurp (b, NULL);
|
|
|
|
if (s) {
|
2020-07-01 01:33:38 +00:00
|
|
|
bool ret = r_run_parse (p, s);
|
2014-07-09 01:48:47 +00:00
|
|
|
free (s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-27 16:06:40 +00:00
|
|
|
R_API bool r_run_parseline(RRunProfile *p, const char *b) {
|
2015-09-14 00:08:31 +00:00
|
|
|
int must_free = false;
|
2014-07-09 00:24:31 +00:00
|
|
|
char *e = strchr (b, '=');
|
2017-04-26 17:35:37 +00:00
|
|
|
if (!e || *b == '#') {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
*e++ = 0;
|
2020-09-25 11:51:23 +00:00
|
|
|
if (*e == '$') {
|
2015-09-14 00:08:31 +00:00
|
|
|
must_free = true;
|
2014-07-09 00:24:31 +00:00
|
|
|
e = r_sys_getenv (e);
|
|
|
|
}
|
2017-04-26 17:35:37 +00:00
|
|
|
if (!e) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-13 08:17:26 +00:00
|
|
|
if (!strcmp (b, "program")) {
|
|
|
|
p->_args[0] = p->_program = strdup (e);
|
2020-09-25 11:51:23 +00:00
|
|
|
} else if (!strcmp (b, "daemon")) {
|
|
|
|
p->_daemon = true;
|
2018-09-13 08:17:26 +00:00
|
|
|
} else if (!strcmp (b, "system")) {
|
|
|
|
p->_system = strdup (e);
|
|
|
|
} else if (!strcmp (b, "runlib")) {
|
|
|
|
p->_runlib = strdup (e);
|
|
|
|
} else if (!strcmp (b, "runlib.fcn")) {
|
|
|
|
p->_runlib_fcn = strdup (e);
|
|
|
|
} else if (!strcmp (b, "aslr")) {
|
|
|
|
p->_aslr = parseBool (e);
|
|
|
|
} else if (!strcmp (b, "pid")) {
|
|
|
|
p->_pid = atoi (e);
|
|
|
|
} else if (!strcmp (b, "pidfile")) {
|
|
|
|
p->_pidfile = strdup (e);
|
|
|
|
} else if (!strcmp (b, "connect")) {
|
|
|
|
p->_connect = strdup (e);
|
|
|
|
} else if (!strcmp (b, "listen")) {
|
|
|
|
p->_listen = strdup (e);
|
|
|
|
} else if (!strcmp (b, "pty")) {
|
|
|
|
p->_pty = parseBool (e);
|
|
|
|
} else if (!strcmp (b, "stdio")) {
|
2015-09-23 07:48:30 +00:00
|
|
|
if (e[0] == '!') {
|
|
|
|
p->_stdio = strdup (e);
|
|
|
|
} else {
|
|
|
|
p->_stdout = strdup (e);
|
|
|
|
p->_stderr = strdup (e);
|
|
|
|
p->_stdin = strdup (e);
|
|
|
|
}
|
2018-09-13 08:17:26 +00:00
|
|
|
} else if (!strcmp (b, "stdout")) {
|
|
|
|
p->_stdout = strdup (e);
|
|
|
|
} else if (!strcmp (b, "stdin")) {
|
|
|
|
p->_stdin = strdup (e);
|
|
|
|
} else if (!strcmp (b, "stderr")) {
|
|
|
|
p->_stderr = strdup (e);
|
|
|
|
} else if (!strcmp (b, "input")) {
|
|
|
|
p->_input = strdup (e);
|
|
|
|
} else if (!strcmp (b, "chdir")) {
|
|
|
|
p->_chgdir = strdup (e);
|
|
|
|
} else if (!strcmp (b, "core")) {
|
|
|
|
p->_docore = parseBool (e);
|
|
|
|
} else if (!strcmp (b, "fork")) {
|
|
|
|
p->_dofork = parseBool (e);
|
|
|
|
} else if (!strcmp (b, "sleep")) {
|
|
|
|
p->_r2sleep = atoi (e);
|
|
|
|
} else if (!strcmp (b, "maxstack")) {
|
|
|
|
p->_maxstack = atoi (e);
|
|
|
|
} else if (!strcmp (b, "maxproc")) {
|
|
|
|
p->_maxproc = atoi (e);
|
|
|
|
} else if (!strcmp (b, "maxfd")) {
|
|
|
|
p->_maxfd = atoi (e);
|
|
|
|
} else if (!strcmp (b, "bits")) {
|
|
|
|
p->_bits = atoi (e);
|
|
|
|
} else if (!strcmp (b, "chroot")) {
|
|
|
|
p->_chroot = strdup (e);
|
|
|
|
} else if (!strcmp (b, "libpath")) {
|
|
|
|
p->_libpath = strdup (e);
|
|
|
|
} else if (!strcmp (b, "preload")) {
|
|
|
|
p->_preload = strdup (e);
|
|
|
|
} else if (!strcmp (b, "r2preload")) {
|
|
|
|
p->_r2preload = parseBool (e);
|
|
|
|
} else if (!strcmp (b, "r2preweb")) {
|
|
|
|
r_sys_setenv ("RARUN2_WEB", "yes");
|
|
|
|
} else if (!strcmp (b, "setuid")) {
|
|
|
|
p->_setuid = strdup (e);
|
|
|
|
} else if (!strcmp (b, "seteuid")) {
|
|
|
|
p->_seteuid = strdup (e);
|
|
|
|
} else if (!strcmp (b, "setgid")) {
|
|
|
|
p->_setgid = strdup (e);
|
|
|
|
} else if (!strcmp (b, "setegid")) {
|
|
|
|
p->_setegid = strdup (e);
|
|
|
|
} else if (!strcmp (b, "nice")) {
|
|
|
|
p->_nice = atoi (e);
|
|
|
|
} else if (!strcmp (b, "timeout")) {
|
|
|
|
p->_timeout = atoi (e);
|
|
|
|
} else if (!strcmp (b, "timeoutsig")) {
|
|
|
|
p->_timeout_sig = r_signal_from_string (e);
|
|
|
|
} else if (!memcmp (b, "arg", 3)) {
|
2015-09-30 15:06:59 +00:00
|
|
|
int n = atoi (b + 3);
|
|
|
|
if (n >= 0 && n < R_RUN_PROFILE_NARGS) {
|
2014-07-09 00:24:31 +00:00
|
|
|
p->_args[n] = getstr (e);
|
2017-07-31 13:03:11 +00:00
|
|
|
p->_argc++;
|
2017-02-02 13:33:52 +00:00
|
|
|
} else {
|
|
|
|
eprintf ("Out of bounds args index: %d\n", n);
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
} else if (!strcmp (b, "envfile")) {
|
|
|
|
char *p, buf[1024];
|
2017-06-26 06:35:43 +00:00
|
|
|
size_t len;
|
2019-08-18 15:19:53 +00:00
|
|
|
FILE *fd = r_sandbox_fopen (e, "r");
|
2014-07-09 00:24:31 +00:00
|
|
|
if (!fd) {
|
|
|
|
eprintf ("Cannot open '%s'\n", e);
|
2017-04-26 17:35:37 +00:00
|
|
|
if (must_free == true) {
|
|
|
|
free (e);
|
|
|
|
}
|
|
|
|
return false;
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
for (;;) {
|
2020-02-29 19:47:10 +00:00
|
|
|
if (!fgets (buf, sizeof (buf), fd)) {
|
2018-11-25 00:46:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-05-18 22:39:33 +00:00
|
|
|
if (feof (fd)) {
|
|
|
|
break;
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
p = strchr (buf, '=');
|
|
|
|
if (p) {
|
2017-06-26 06:35:43 +00:00
|
|
|
*p++ = 0;
|
2019-08-18 15:19:53 +00:00
|
|
|
len = strlen (p);
|
|
|
|
if (len > 0 && p[len - 1] == '\n') {
|
2018-09-13 08:17:26 +00:00
|
|
|
p[len - 1] = 0;
|
|
|
|
}
|
2019-08-18 15:19:53 +00:00
|
|
|
if (len > 1 && p[len - 2] == '\r') {
|
2018-09-13 08:17:26 +00:00
|
|
|
p[len - 2] = 0;
|
|
|
|
}
|
2017-06-26 06:35:43 +00:00
|
|
|
r_sys_setenv (buf, p);
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose (fd);
|
|
|
|
} else if (!strcmp (b, "unsetenv")) {
|
|
|
|
r_sys_setenv (e, NULL);
|
|
|
|
} else if (!strcmp (b, "setenv")) {
|
2016-06-04 08:04:14 +00:00
|
|
|
char *V, *v = strchr (e, '=');
|
2014-07-09 00:24:31 +00:00
|
|
|
if (v) {
|
|
|
|
*v++ = 0;
|
2016-06-04 08:04:14 +00:00
|
|
|
V = getstr (v);
|
|
|
|
r_sys_setenv (e, V);
|
|
|
|
free (V);
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
2015-08-28 22:15:41 +00:00
|
|
|
} else if (!strcmp(b, "clearenv")) {
|
|
|
|
r_sys_clearenv ();
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
2017-04-26 17:35:37 +00:00
|
|
|
if (must_free == true) {
|
2014-07-09 00:24:31 +00:00
|
|
|
free (e);
|
2017-04-26 17:35:37 +00:00
|
|
|
}
|
|
|
|
return true;
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 14:08:32 +00:00
|
|
|
R_API const char *r_run_help(void) {
|
2014-07-09 00:24:31 +00:00
|
|
|
return
|
|
|
|
"program=/bin/ls\n"
|
|
|
|
"arg1=/bin\n"
|
|
|
|
"# arg2=hello\n"
|
|
|
|
"# arg3=\"hello\\nworld\"\n"
|
|
|
|
"# arg4=:048490184058104849\n"
|
2014-10-28 15:45:19 +00:00
|
|
|
"# arg5=:!ragg2 -p n50 -d 10:0x8048123\n"
|
|
|
|
"# arg6=@arg.txt\n"
|
|
|
|
"# arg7=@300@ABCD # 300 chars filled with ABCD pattern\n"
|
2014-07-09 00:24:31 +00:00
|
|
|
"# system=r2 -\n"
|
2020-09-25 11:51:23 +00:00
|
|
|
"# daemon=false\n"
|
2014-07-09 00:24:31 +00:00
|
|
|
"# aslr=no\n"
|
|
|
|
"setenv=FOO=BAR\n"
|
|
|
|
"# unsetenv=FOO\n"
|
2015-08-28 22:15:41 +00:00
|
|
|
"# clearenv=true\n"
|
2014-07-09 00:24:31 +00:00
|
|
|
"# envfile=environ.txt\n"
|
|
|
|
"timeout=3\n"
|
2017-02-02 13:33:52 +00:00
|
|
|
"# timeoutsig=SIGTERM # or 15\n"
|
2014-07-09 00:24:31 +00:00
|
|
|
"# connect=localhost:8080\n"
|
|
|
|
"# listen=8080\n"
|
2016-12-15 14:06:06 +00:00
|
|
|
"# pty=false\n"
|
2015-09-12 17:36:15 +00:00
|
|
|
"# fork=true\n"
|
2014-07-09 00:24:31 +00:00
|
|
|
"# bits=32\n"
|
2014-11-22 02:00:31 +00:00
|
|
|
"# pid=0\n"
|
2015-02-18 00:10:19 +00:00
|
|
|
"# pidfile=/tmp/foo.pid\n"
|
2014-07-09 00:24:31 +00:00
|
|
|
"# #sleep=0\n"
|
|
|
|
"# #maxfd=0\n"
|
2016-03-27 12:17:39 +00:00
|
|
|
"# #execve=false\n"
|
2014-07-09 00:24:31 +00:00
|
|
|
"# #maxproc=0\n"
|
|
|
|
"# #maxstack=0\n"
|
|
|
|
"# #core=false\n"
|
|
|
|
"# #stdio=blah.txt\n"
|
|
|
|
"# #stderr=foo.txt\n"
|
|
|
|
"# stdout=foo.txt\n"
|
2018-02-14 21:08:51 +00:00
|
|
|
"# stdin=input.txt # or !program to redirect input from another program\n"
|
2014-07-09 00:24:31 +00:00
|
|
|
"# input=input.txt\n"
|
|
|
|
"# chdir=/\n"
|
|
|
|
"# chroot=/mnt/chroot\n"
|
|
|
|
"# libpath=$PWD:/tmp/lib\n"
|
|
|
|
"# r2preload=yes\n"
|
|
|
|
"# preload=/lib/libfoo.so\n"
|
|
|
|
"# setuid=2000\n"
|
|
|
|
"# seteuid=2000\n"
|
|
|
|
"# setgid=2001\n"
|
2015-03-26 14:19:12 +00:00
|
|
|
"# setegid=2001\n"
|
|
|
|
"# nice=5\n";
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 18:20:01 +00:00
|
|
|
#if HAVE_PTY
|
2016-12-15 14:06:06 +00:00
|
|
|
static int fd_forward(int in_fd, int out_fd, char **buff) {
|
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
if (ioctl (in_fd, FIONREAD, &size) == -1) {
|
|
|
|
perror ("ioctl");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-12-18 16:14:30 +00:00
|
|
|
if (!size) { // child process exited or socket is closed
|
2016-12-15 14:06:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *new_buff = realloc (*buff, size);
|
2016-12-18 16:14:30 +00:00
|
|
|
if (!new_buff) {
|
2016-12-15 14:06:06 +00:00
|
|
|
eprintf ("Failed to allocate buffer for redirection");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*buff = new_buff;
|
2018-06-11 09:48:22 +00:00
|
|
|
if (read (in_fd, *buff, size) != size) {
|
|
|
|
perror ("read");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-12-15 14:06:06 +00:00
|
|
|
if (write (out_fd, *buff, size) != size) {
|
|
|
|
perror ("write");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int redirect_socket_to_stdio(RSocket *sock) {
|
|
|
|
close (0);
|
|
|
|
close (1);
|
|
|
|
close (2);
|
2021-06-20 15:14:43 +00:00
|
|
|
#ifndef __wasi__
|
2016-12-15 14:06:06 +00:00
|
|
|
dup2 (sock->fd, 0);
|
|
|
|
dup2 (sock->fd, 1);
|
|
|
|
dup2 (sock->fd, 2);
|
2021-06-20 15:14:43 +00:00
|
|
|
#endif
|
2016-12-15 14:06:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-25 11:51:23 +00:00
|
|
|
#if __WINDOWS__
|
|
|
|
static RThreadFunctionRet exit_process(RThread *th) {
|
|
|
|
// eprintf ("\nrarun2: Interrupted by timeout\n");
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-12-15 14:06:06 +00:00
|
|
|
static int redirect_socket_to_pty(RSocket *sock) {
|
|
|
|
#if HAVE_PTY
|
|
|
|
// directly duplicating the fds using dup2() creates problems
|
|
|
|
// in case of interactive applications
|
2020-12-21 00:10:22 +00:00
|
|
|
int fdm = -1, fds = -1;
|
2016-12-15 14:06:06 +00:00
|
|
|
|
2020-04-19 10:38:55 +00:00
|
|
|
if (dyn_openpty && dyn_openpty (&fdm, &fds, NULL, NULL, NULL) == -1) {
|
2016-12-19 21:34:00 +00:00
|
|
|
perror ("opening pty");
|
2016-12-15 14:06:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-12-19 21:34:00 +00:00
|
|
|
pid_t child_pid = r_sys_fork ();
|
2016-12-15 14:06:06 +00:00
|
|
|
|
2016-12-19 21:34:00 +00:00
|
|
|
if (child_pid == -1) {
|
|
|
|
eprintf ("cannot fork\n");
|
2020-12-21 00:10:22 +00:00
|
|
|
if (fdm != -1) {
|
|
|
|
close (fdm);
|
|
|
|
}
|
|
|
|
if (fds != -1) {
|
|
|
|
close (fds);
|
|
|
|
}
|
2016-12-19 21:34:00 +00:00
|
|
|
return -1;
|
2016-12-15 14:06:06 +00:00
|
|
|
}
|
|
|
|
|
2016-12-19 21:34:00 +00:00
|
|
|
if (child_pid == 0) {
|
|
|
|
// child process
|
|
|
|
close (fds);
|
2016-12-15 14:06:06 +00:00
|
|
|
|
2016-12-19 21:34:00 +00:00
|
|
|
char *buff = NULL;
|
|
|
|
int sockfd = sock->fd;
|
|
|
|
int max_fd = fdm > sockfd ? fdm : sockfd;
|
2016-12-15 14:06:06 +00:00
|
|
|
|
2016-12-19 21:34:00 +00:00
|
|
|
while (true) {
|
|
|
|
fd_set readfds;
|
|
|
|
FD_ZERO (&readfds);
|
|
|
|
FD_SET (fdm, &readfds);
|
|
|
|
FD_SET (sockfd, &readfds);
|
2016-12-15 14:06:06 +00:00
|
|
|
|
2016-12-19 21:34:00 +00:00
|
|
|
if (select (max_fd + 1, &readfds, NULL, NULL, NULL) == -1) {
|
|
|
|
perror ("select error");
|
2016-12-15 14:06:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-12-19 21:34:00 +00:00
|
|
|
if (FD_ISSET (fdm, &readfds)) {
|
|
|
|
if (fd_forward (fdm, sockfd, &buff) != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET (sockfd, &readfds)) {
|
|
|
|
if (fd_forward (sockfd, fdm, &buff) != 0) {
|
|
|
|
break;
|
|
|
|
}
|
2016-12-15 14:06:06 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-19 21:34:00 +00:00
|
|
|
|
|
|
|
free (buff);
|
2020-12-21 00:10:22 +00:00
|
|
|
if (fdm != -1) {
|
|
|
|
close (fdm);
|
|
|
|
fdm = -1;
|
|
|
|
}
|
2016-12-19 21:34:00 +00:00
|
|
|
r_socket_free (sock);
|
|
|
|
exit (0);
|
2016-12-15 14:06:06 +00:00
|
|
|
}
|
|
|
|
|
2016-12-19 21:34:00 +00:00
|
|
|
// parent
|
|
|
|
r_socket_close_fd (sock);
|
2020-04-19 10:38:55 +00:00
|
|
|
if (dyn_login_tty) {
|
|
|
|
dyn_login_tty (fds);
|
|
|
|
}
|
2020-12-21 00:10:22 +00:00
|
|
|
if (fdm != -1) {
|
|
|
|
close (fdm);
|
|
|
|
}
|
2016-12-15 14:06:06 +00:00
|
|
|
|
2016-12-19 21:34:00 +00:00
|
|
|
// disable the echo on slave stdin
|
|
|
|
struct termios t;
|
|
|
|
tcgetattr (0, &t);
|
|
|
|
cfmakeraw (&t);
|
|
|
|
tcsetattr (0, TCSANOW, &t);
|
|
|
|
|
|
|
|
return 0;
|
2016-12-15 14:06:06 +00:00
|
|
|
#else
|
|
|
|
// Fallback to socket to I/O redirection
|
|
|
|
return redirect_socket_to_stdio (sock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-10-27 19:30:06 +00:00
|
|
|
R_API int r_run_config_env(RRunProfile *p) {
|
2014-07-09 00:24:31 +00:00
|
|
|
int ret;
|
2015-10-27 19:30:06 +00:00
|
|
|
|
2020-04-20 15:04:30 +00:00
|
|
|
#if HAVE_PTY
|
2020-04-19 10:38:55 +00:00
|
|
|
dyn_init ();
|
2020-04-20 15:04:30 +00:00
|
|
|
#endif
|
2020-04-19 10:38:55 +00:00
|
|
|
|
2017-07-31 13:03:11 +00:00
|
|
|
if (!p->_program && !p->_system && !p->_runlib) {
|
2020-09-25 11:51:23 +00:00
|
|
|
eprintf ("No program, system or runlib rule defined\n");
|
2014-07-09 00:24:31 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-09-23 07:48:30 +00:00
|
|
|
// when IO is redirected to a process, handle them together
|
|
|
|
if (handle_redirection (p->_stdio, true, true, false) != 0) {
|
|
|
|
return 1;
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
2015-09-23 07:48:30 +00:00
|
|
|
if (handle_redirection (p->_stdin, true, false, false) != 0) {
|
|
|
|
return 1;
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
2015-09-23 07:48:30 +00:00
|
|
|
if (handle_redirection (p->_stdout, false, true, false) != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (handle_redirection (p->_stderr, false, false, true) != 0) {
|
|
|
|
return 1;
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
2018-09-13 08:17:26 +00:00
|
|
|
if (p->_aslr != -1) {
|
2018-01-28 21:56:46 +00:00
|
|
|
setASLR (p, p->_aslr);
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2021-07-07 17:18:30 +00:00
|
|
|
#if __UNIX__ && !__wasi__ && !defined(serenity)
|
2014-07-09 00:24:31 +00:00
|
|
|
set_limit (p->_docore, RLIMIT_CORE, RLIM_INFINITY);
|
2018-09-13 08:17:26 +00:00
|
|
|
if (p->_maxfd) {
|
2014-07-09 00:24:31 +00:00
|
|
|
set_limit (p->_maxfd, RLIMIT_NOFILE, p->_maxfd);
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
#ifdef RLIMIT_NPROC
|
2018-09-13 08:17:26 +00:00
|
|
|
if (p->_maxproc) {
|
2014-07-09 00:24:31 +00:00
|
|
|
set_limit (p->_maxproc, RLIMIT_NPROC, p->_maxproc);
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
#endif
|
2018-09-13 08:17:26 +00:00
|
|
|
if (p->_maxstack) {
|
2014-07-09 00:24:31 +00:00
|
|
|
set_limit (p->_maxstack, RLIMIT_STACK, p->_maxstack);
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
#else
|
|
|
|
if (p->_docore || p->_maxfd || p->_maxproc || p->_maxstack)
|
|
|
|
eprintf ("Warning: setrlimits not supported for this platform\n");
|
|
|
|
#endif
|
|
|
|
if (p->_connect) {
|
|
|
|
char *q = strchr (p->_connect, ':');
|
|
|
|
if (q) {
|
|
|
|
RSocket *fd = r_socket_new (0);
|
|
|
|
*q = 0;
|
|
|
|
if (!r_socket_connect_tcp (fd, p->_connect, q+1, 30)) {
|
|
|
|
eprintf ("Cannot connect\n");
|
2021-04-03 01:50:08 +00:00
|
|
|
r_socket_free (fd);
|
2014-07-09 00:24:31 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-12-15 14:06:06 +00:00
|
|
|
if (p->_pty) {
|
|
|
|
if (redirect_socket_to_pty (fd) != 0) {
|
|
|
|
eprintf ("socket redirection failed\n");
|
|
|
|
r_socket_free (fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
redirect_socket_to_stdio (fd);
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
} else {
|
|
|
|
eprintf ("Invalid format for connect. missing ':'\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p->_listen) {
|
|
|
|
RSocket *child, *fd = r_socket_new (0);
|
2016-05-23 16:20:34 +00:00
|
|
|
bool is_child = false;
|
2014-07-09 00:24:31 +00:00
|
|
|
if (!r_socket_listen (fd, p->_listen, NULL)) {
|
|
|
|
eprintf ("rarun2: cannot listen\n");
|
|
|
|
r_socket_free (fd);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-09-14 00:08:31 +00:00
|
|
|
while (true) {
|
2015-09-12 17:36:15 +00:00
|
|
|
child = r_socket_accept (fd);
|
|
|
|
if (child) {
|
2016-05-23 16:20:34 +00:00
|
|
|
is_child = true;
|
2015-09-12 17:36:15 +00:00
|
|
|
|
|
|
|
if (p->_dofork && !p->_dodebug) {
|
|
|
|
pid_t child_pid = r_sys_fork ();
|
|
|
|
if (child_pid == -1) {
|
|
|
|
eprintf("rarun2: cannot fork\n");
|
|
|
|
r_socket_free (child);
|
|
|
|
r_socket_free (fd);
|
|
|
|
return 1;
|
|
|
|
} else if (child_pid != 0){
|
|
|
|
// parent code
|
2015-09-14 00:08:31 +00:00
|
|
|
is_child = false;
|
2015-09-12 17:36:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_child) {
|
|
|
|
r_socket_close_fd (fd);
|
|
|
|
eprintf ("connected\n");
|
2016-12-15 14:06:06 +00:00
|
|
|
if (p->_pty) {
|
|
|
|
if (redirect_socket_to_pty (child) != 0) {
|
|
|
|
eprintf ("socket redirection failed\n");
|
|
|
|
r_socket_free (child);
|
|
|
|
r_socket_free (fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
redirect_socket_to_stdio (child);
|
|
|
|
}
|
2015-09-12 17:36:15 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
r_socket_close_fd (child);
|
|
|
|
}
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
2017-07-27 20:57:18 +00:00
|
|
|
if (!is_child) {
|
|
|
|
r_socket_free (child);
|
|
|
|
}
|
2015-11-01 04:47:16 +00:00
|
|
|
r_socket_free (fd);
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
if (p->_r2sleep != 0) {
|
|
|
|
r_sys_sleep (p->_r2sleep);
|
|
|
|
}
|
2016-05-24 10:32:45 +00:00
|
|
|
#if __UNIX__
|
|
|
|
if (p->_chroot) {
|
|
|
|
if (chdir (p->_chroot) == -1) {
|
|
|
|
eprintf ("Cannot chdir to chroot in %s\n", p->_chroot);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
if (chroot (".") == -1) {
|
|
|
|
eprintf ("Cannot chroot to %s\n", p->_chroot);
|
|
|
|
return 1;
|
|
|
|
} else {
|
2018-12-12 21:04:11 +00:00
|
|
|
// Silenting pedantic meson flags...
|
|
|
|
if (chdir ("/") == -1) {
|
|
|
|
eprintf ("Cannot chdir to /\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2016-05-24 10:32:45 +00:00
|
|
|
if (p->_chgdir) {
|
|
|
|
if (chdir (p->_chgdir) == -1) {
|
|
|
|
eprintf ("Cannot chdir after chroot to %s\n", p->_chgdir);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (p->_chgdir) {
|
|
|
|
if (chdir (p->_chgdir) == -1) {
|
|
|
|
eprintf ("Cannot chdir after chroot to %s\n", p->_chgdir);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2016-11-10 10:43:07 +00:00
|
|
|
#else
|
2014-07-09 00:24:31 +00:00
|
|
|
if (p->_chgdir) {
|
|
|
|
ret = chdir (p->_chgdir);
|
2016-05-24 10:32:45 +00:00
|
|
|
if (ret < 0) {
|
2014-07-09 00:24:31 +00:00
|
|
|
return 1;
|
2016-05-24 10:32:45 +00:00
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
if (p->_chroot) {
|
|
|
|
ret = chdir (p->_chroot);
|
2016-05-24 10:32:45 +00:00
|
|
|
if (ret < 0) {
|
2014-07-09 00:24:31 +00:00
|
|
|
return 1;
|
2016-05-24 10:32:45 +00:00
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
2016-11-10 10:43:07 +00:00
|
|
|
#endif
|
2014-07-09 00:24:31 +00:00
|
|
|
#if __UNIX__
|
|
|
|
if (p->_setuid) {
|
2016-05-24 10:32:45 +00:00
|
|
|
ret = setgroups (0, NULL);
|
2016-11-10 10:43:07 +00:00
|
|
|
if (ret < 0) {
|
2014-11-10 02:06:41 +00:00
|
|
|
return 1;
|
2016-11-10 10:43:07 +00:00
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
ret = setuid (atoi (p->_setuid));
|
2016-11-10 10:43:07 +00:00
|
|
|
if (ret < 0) {
|
2014-07-09 00:24:31 +00:00
|
|
|
return 1;
|
2016-11-10 10:43:07 +00:00
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
if (p->_seteuid) {
|
|
|
|
ret = seteuid (atoi (p->_seteuid));
|
2016-11-10 10:43:07 +00:00
|
|
|
if (ret < 0) {
|
2014-07-09 00:24:31 +00:00
|
|
|
return 1;
|
2016-11-10 10:43:07 +00:00
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
if (p->_setgid) {
|
|
|
|
ret = setgid (atoi (p->_setgid));
|
2016-11-10 10:43:07 +00:00
|
|
|
if (ret < 0) {
|
2014-07-09 00:24:31 +00:00
|
|
|
return 1;
|
2016-11-10 10:43:07 +00:00
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
if (p->_input) {
|
2015-09-17 08:46:11 +00:00
|
|
|
char *inp;
|
2014-07-09 00:24:31 +00:00
|
|
|
int f2[2];
|
2018-11-25 00:46:57 +00:00
|
|
|
if (pipe (f2) != -1) {
|
|
|
|
close (0);
|
2021-06-20 15:14:43 +00:00
|
|
|
#if !__wasi__
|
2018-11-25 00:46:57 +00:00
|
|
|
dup2 (f2[0], 0);
|
2021-06-20 15:14:43 +00:00
|
|
|
#endif
|
2018-11-25 00:46:57 +00:00
|
|
|
} else {
|
|
|
|
eprintf ("[ERROR] rarun2: Cannot create pipe\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2015-09-17 08:46:11 +00:00
|
|
|
inp = getstr (p->_input);
|
|
|
|
if (inp) {
|
2018-12-12 21:04:11 +00:00
|
|
|
size_t inpl = strlen (inp);
|
|
|
|
if (write (f2[1], inp, inpl) != inpl) {
|
|
|
|
eprintf ("[ERROR] rarun2: Cannot write to the pipe\n");
|
|
|
|
}
|
2015-09-17 08:46:11 +00:00
|
|
|
close (f2[1]);
|
|
|
|
free (inp);
|
|
|
|
} else {
|
|
|
|
eprintf ("Invalid input\n");
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (p->_r2preload) {
|
|
|
|
if (p->_preload) {
|
2021-03-19 10:02:54 +00:00
|
|
|
eprintf ("Warning: Only one library can be opened at a time\n");
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
2019-09-22 08:42:49 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
p->_preload = r_str_r2_prefix (R_JOIN_2_PATHS (R2_LIBDIR, "libr2."R_LIB_EXT));
|
|
|
|
#else
|
|
|
|
p->_preload = strdup (R2_LIBDIR"/libr2."R_LIB_EXT);
|
|
|
|
#endif
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
if (p->_libpath) {
|
|
|
|
#if __WINDOWS__
|
|
|
|
eprintf ("rarun2: libpath unsupported for this platform\n");
|
|
|
|
#elif __HAIKU__
|
2021-07-27 17:50:31 +00:00
|
|
|
char *orig = r_sys_getenv ("LIBRARY_PATH");
|
|
|
|
char *newlib = r_str_newf ("%s:%s", p->_libpath, orig);
|
|
|
|
r_sys_setenv ("LIBRARY_PATH", newlib);
|
|
|
|
free (newlib);
|
|
|
|
free (orig);
|
2014-07-09 00:24:31 +00:00
|
|
|
#elif __APPLE__
|
|
|
|
r_sys_setenv ("DYLD_LIBRARY_PATH", p->_libpath);
|
|
|
|
#else
|
|
|
|
r_sys_setenv ("LD_LIBRARY_PATH", p->_libpath);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (p->_preload) {
|
|
|
|
#if __APPLE__
|
|
|
|
// 10.6
|
2017-09-23 00:31:12 +00:00
|
|
|
#ifndef __MAC_10_7
|
2014-07-09 00:24:31 +00:00
|
|
|
r_sys_setenv ("DYLD_PRELOAD", p->_preload);
|
2017-09-23 00:31:12 +00:00
|
|
|
#endif
|
2014-07-09 00:24:31 +00:00
|
|
|
r_sys_setenv ("DYLD_INSERT_LIBRARIES", p->_preload);
|
|
|
|
// 10.8
|
|
|
|
r_sys_setenv ("DYLD_FORCE_FLAT_NAMESPACE", "1");
|
|
|
|
#else
|
|
|
|
r_sys_setenv ("LD_PRELOAD", p->_preload);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (p->_timeout) {
|
|
|
|
#if __UNIX__
|
2021-06-07 15:32:37 +00:00
|
|
|
int mypid = r_sys_getpid ();
|
2015-04-11 03:00:05 +00:00
|
|
|
if (!r_sys_fork ()) {
|
2016-05-17 08:41:33 +00:00
|
|
|
int use_signal = p->_timeout_sig;
|
|
|
|
if (use_signal < 1) {
|
|
|
|
use_signal = SIGKILL;
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
sleep (p->_timeout);
|
2016-05-17 08:41:33 +00:00
|
|
|
if (!kill (mypid, 0)) {
|
2020-09-25 11:51:23 +00:00
|
|
|
// eprintf ("\nrarun2: Interrupted by timeout\n");
|
2016-05-17 08:41:33 +00:00
|
|
|
}
|
|
|
|
kill (mypid, use_signal);
|
2014-07-09 00:24:31 +00:00
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
#else
|
2020-09-25 11:51:23 +00:00
|
|
|
if (p->_timeout_sig < 1 || p->_timeout_sig == 9) {
|
|
|
|
r_th_new (exit_process, NULL, p->_timeout);
|
|
|
|
} else {
|
|
|
|
eprintf ("timeout with signal not supported for this platform\n");
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
#endif
|
|
|
|
}
|
2015-10-27 19:30:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-01 01:33:38 +00:00
|
|
|
// NOTE: return value is like in unix return code (0 = ok, 1 = not ok)
|
2015-10-27 19:30:06 +00:00
|
|
|
R_API int r_run_start(RRunProfile *p) {
|
2016-03-27 12:17:39 +00:00
|
|
|
#if LIBC_HAVE_FORK
|
|
|
|
if (p->_execve) {
|
|
|
|
exit (execv (p->_program, (char* const*)p->_args));
|
|
|
|
}
|
|
|
|
#endif
|
2016-04-26 23:52:35 +00:00
|
|
|
#if __APPLE__ && !__POWERPC__ && LIBC_HAVE_FORK
|
2015-10-27 19:30:06 +00:00
|
|
|
posix_spawnattr_t attr = {0};
|
|
|
|
pid_t pid = -1;
|
|
|
|
int ret;
|
2014-07-09 00:24:31 +00:00
|
|
|
posix_spawnattr_init (&attr);
|
|
|
|
if (p->_args[0]) {
|
2014-09-09 10:10:55 +00:00
|
|
|
char **envp = r_sys_get_environ();
|
2014-07-09 00:24:31 +00:00
|
|
|
ut32 spflags = 0; //POSIX_SPAWN_START_SUSPENDED;
|
|
|
|
spflags |= POSIX_SPAWN_SETEXEC;
|
|
|
|
if (p->_aslr == 0) {
|
|
|
|
#define _POSIX_SPAWN_DISABLE_ASLR 0x0100
|
|
|
|
spflags |= _POSIX_SPAWN_DISABLE_ASLR;
|
|
|
|
}
|
|
|
|
(void)posix_spawnattr_setflags (&attr, spflags);
|
2014-07-09 01:31:04 +00:00
|
|
|
if (p->_bits) {
|
|
|
|
size_t copied = 1;
|
|
|
|
cpu_type_t cpu;
|
|
|
|
#if __i386__ || __x86_64__
|
|
|
|
cpu = CPU_TYPE_I386;
|
2017-09-26 09:44:43 +00:00
|
|
|
if (p->_bits == 64) {
|
2014-07-09 01:31:04 +00:00
|
|
|
cpu |= CPU_ARCH_ABI64;
|
2017-09-26 09:44:43 +00:00
|
|
|
}
|
2014-07-09 01:31:04 +00:00
|
|
|
#else
|
|
|
|
cpu = CPU_TYPE_ANY;
|
|
|
|
#endif
|
|
|
|
posix_spawnattr_setbinpref_np (
|
|
|
|
&attr, 1, &cpu, &copied);
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
ret = posix_spawnp (&pid, p->_args[0],
|
2014-09-09 10:10:55 +00:00
|
|
|
NULL, &attr, p->_args, envp);
|
2014-07-09 00:24:31 +00:00
|
|
|
switch (ret) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 22:
|
|
|
|
eprintf ("posix_spawnp: Invalid argument\n");
|
|
|
|
break;
|
|
|
|
case 86:
|
|
|
|
eprintf ("posix_spawnp: Unsupported architecture\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
eprintf ("posix_spawnp: unknown error %d\n", ret);
|
|
|
|
perror ("posix_spawnp");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
exit (ret);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (p->_system) {
|
2014-11-22 02:00:31 +00:00
|
|
|
if (p->_pid) {
|
|
|
|
eprintf ("PID: Cannot determine pid with 'system' directive. Use 'program'.\n");
|
|
|
|
}
|
2020-09-25 11:51:23 +00:00
|
|
|
if (p->_daemon) {
|
|
|
|
#if __WINDOWS__
|
|
|
|
// eprintf ("PID: Cannot determine pid with 'system' directive. Use 'program'.\n");
|
|
|
|
#else
|
|
|
|
pid_t child = r_sys_fork ();
|
|
|
|
if (child == -1) {
|
|
|
|
perror ("fork");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
if (child) {
|
|
|
|
if (p->_pidfile) {
|
|
|
|
char pidstr[32];
|
|
|
|
snprintf (pidstr, sizeof (pidstr), "%d\n", child);
|
|
|
|
r_file_dump (p->_pidfile,
|
|
|
|
(const ut8*)pidstr,
|
|
|
|
strlen (pidstr), 0);
|
|
|
|
}
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
setsid ();
|
|
|
|
if (p->_timeout) {
|
|
|
|
#if __UNIX__
|
2021-06-07 15:32:37 +00:00
|
|
|
int mypid = r_sys_getpid ();
|
2020-09-25 11:51:23 +00:00
|
|
|
if (!r_sys_fork ()) {
|
|
|
|
int use_signal = p->_timeout_sig;
|
|
|
|
if (use_signal < 1) {
|
|
|
|
use_signal = SIGKILL;
|
|
|
|
}
|
|
|
|
sleep (p->_timeout);
|
|
|
|
if (!kill (mypid, 0)) {
|
|
|
|
// eprintf ("\nrarun2: Interrupted by timeout\n");
|
|
|
|
}
|
|
|
|
kill (mypid, use_signal);
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
eprintf ("timeout not supported for this platform\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if __UNIX__
|
2021-06-17 22:36:04 +00:00
|
|
|
close (0);
|
|
|
|
close (1);
|
2021-08-22 10:02:41 +00:00
|
|
|
char *bin_sh = r_file_binsh ();
|
|
|
|
if (bin_sh) {
|
2021-06-17 22:36:04 +00:00
|
|
|
exit (execl (bin_sh, bin_sh, "-c", p->_system, NULL));
|
|
|
|
} else {
|
|
|
|
exit (r_sys_cmd (p->_system));
|
|
|
|
}
|
|
|
|
free (bin_sh);
|
2020-09-25 11:51:23 +00:00
|
|
|
#else
|
|
|
|
exit (r_sys_cmd (p->_system));
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
if (p->_pidfile) {
|
|
|
|
eprintf ("Warning: pidfile doesnt work with 'system'.\n");
|
|
|
|
}
|
|
|
|
exit (r_sys_cmd (p->_system));
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
|
|
|
if (p->_program) {
|
|
|
|
if (!r_file_exists (p->_program)) {
|
2014-11-18 10:19:04 +00:00
|
|
|
char *progpath = r_file_path (p->_program);
|
|
|
|
if (progpath && *progpath) {
|
|
|
|
free (p->_program);
|
|
|
|
p->_program = progpath;
|
|
|
|
} else {
|
2014-11-19 09:54:08 +00:00
|
|
|
free (progpath);
|
2014-11-18 10:19:04 +00:00
|
|
|
eprintf ("rarun2: %s: file not found\n", p->_program);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
2014-11-23 10:07:29 +00:00
|
|
|
#if __UNIX__
|
2014-10-23 02:45:01 +00:00
|
|
|
// XXX HACK close all non-tty fds
|
2018-09-13 08:17:26 +00:00
|
|
|
{ int i;
|
2020-07-24 08:25:55 +00:00
|
|
|
for (i = 3; i < 1024; i++) {
|
2018-09-13 08:17:26 +00:00
|
|
|
close (i);
|
|
|
|
}
|
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
// TODO: use posix_spawn
|
2014-11-22 02:00:31 +00:00
|
|
|
if (p->_setgid) {
|
2015-11-23 01:36:02 +00:00
|
|
|
int ret = setgid (atoi (p->_setgid));
|
2018-09-13 08:17:26 +00:00
|
|
|
if (ret < 0) {
|
2014-11-22 02:00:31 +00:00
|
|
|
return 1;
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2014-11-22 02:00:31 +00:00
|
|
|
}
|
|
|
|
if (p->_pid) {
|
2021-06-07 15:32:37 +00:00
|
|
|
eprintf ("PID: %d\n", r_sys_getpid ());
|
2014-11-22 02:00:31 +00:00
|
|
|
}
|
2015-02-18 00:10:19 +00:00
|
|
|
if (p->_pidfile) {
|
|
|
|
char pidstr[32];
|
2021-06-07 15:32:37 +00:00
|
|
|
snprintf (pidstr, sizeof (pidstr), "%d\n", r_sys_getpid ());
|
2015-04-03 02:04:46 +00:00
|
|
|
r_file_dump (p->_pidfile,
|
|
|
|
(const ut8*)pidstr,
|
|
|
|
strlen (pidstr), 0);
|
2015-02-18 00:10:19 +00:00
|
|
|
}
|
2014-11-23 10:07:29 +00:00
|
|
|
#endif
|
2015-03-26 14:19:12 +00:00
|
|
|
|
2015-04-23 23:16:57 +00:00
|
|
|
if (p->_nice) {
|
2021-07-07 18:20:01 +00:00
|
|
|
#if __UNIX__ && !defined(__HAIKU__) && !defined(__serenity__)
|
2015-04-23 23:16:57 +00:00
|
|
|
if (nice (p->_nice) == -1) {
|
|
|
|
return 1;
|
|
|
|
}
|
2015-03-26 14:19:12 +00:00
|
|
|
#else
|
2015-04-23 23:16:57 +00:00
|
|
|
eprintf ("nice not supported for this platform\n");
|
2020-09-25 11:51:23 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (p->_daemon) {
|
|
|
|
#if __WINDOWS__
|
|
|
|
eprintf ("PID: Cannot determine pid with 'system' directive. Use 'program'.\n");
|
|
|
|
#else
|
|
|
|
pid_t child = r_sys_fork ();
|
|
|
|
if (child == -1) {
|
|
|
|
perror ("fork");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
if (child) {
|
|
|
|
if (p->_pidfile) {
|
|
|
|
char pidstr[32];
|
|
|
|
snprintf (pidstr, sizeof (pidstr), "%d\n", child);
|
|
|
|
r_file_dump (p->_pidfile,
|
|
|
|
(const ut8*)pidstr,
|
|
|
|
strlen (pidstr), 0);
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setsid ();
|
|
|
|
#if !LIBC_HAVE_FORK
|
|
|
|
exit (execv (p->_program, (char* const*)p->_args));
|
|
|
|
#endif
|
2015-03-26 14:19:12 +00:00
|
|
|
#endif
|
2015-04-23 23:16:57 +00:00
|
|
|
}
|
2015-12-28 19:40:18 +00:00
|
|
|
// TODO: must be HAVE_EXECVE
|
|
|
|
#if LIBC_HAVE_FORK
|
2014-07-21 22:24:37 +00:00
|
|
|
exit (execv (p->_program, (char* const*)p->_args));
|
2015-12-28 19:40:18 +00:00
|
|
|
#endif
|
2014-07-09 00:24:31 +00:00
|
|
|
}
|
2017-07-31 13:03:11 +00:00
|
|
|
if (p->_runlib) {
|
|
|
|
if (!p->_runlib_fcn) {
|
|
|
|
eprintf ("No function specified. Please set runlib.fcn\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
void *addr = r_lib_dl_open (p->_runlib);
|
|
|
|
if (!addr) {
|
|
|
|
eprintf ("Could not load the library '%s'\n", p->_runlib);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
void (*fcn)(void) = r_lib_dl_sym (addr, p->_runlib_fcn);
|
|
|
|
if (!fcn) {
|
|
|
|
eprintf ("Could not find the function '%s'\n", p->_runlib_fcn);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
switch (p->_argc) {
|
|
|
|
case 0:
|
|
|
|
fcn ();
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
r_run_call1 (fcn, p->_args[1]);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
r_run_call2 (fcn, p->_args[1], p->_args[2]);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
r_run_call3 (fcn, p->_args[1], p->_args[2], p->_args[3]);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
r_run_call4 (fcn, p->_args[1], p->_args[2], p->_args[3], p->_args[4]);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
r_run_call5 (fcn, p->_args[1], p->_args[2], p->_args[3], p->_args[4],
|
|
|
|
p->_args[5]);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
r_run_call6 (fcn, p->_args[1], p->_args[2], p->_args[3], p->_args[4],
|
|
|
|
p->_args[5], p->_args[6]);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
r_run_call7 (fcn, p->_args[1], p->_args[2], p->_args[3], p->_args[4],
|
|
|
|
p->_args[5], p->_args[6], p->_args[7]);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
r_run_call8 (fcn, p->_args[1], p->_args[2], p->_args[3], p->_args[4],
|
|
|
|
p->_args[5], p->_args[6], p->_args[7], p->_args[8]);
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
r_run_call9 (fcn, p->_args[1], p->_args[2], p->_args[3], p->_args[4],
|
|
|
|
p->_args[5], p->_args[6], p->_args[7], p->_args[8], p->_args[9]);
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
r_run_call10 (fcn, p->_args[1], p->_args[2], p->_args[3], p->_args[4],
|
|
|
|
p->_args[5], p->_args[6], p->_args[7], p->_args[8], p->_args[9], p->_args[10]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
eprintf ("Too many arguments.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2017-08-24 15:20:39 +00:00
|
|
|
r_lib_dl_close (addr);
|
2017-07-31 13:03:11 +00:00
|
|
|
}
|
2014-07-09 00:24:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-09-07 09:16:57 +00:00
|
|
|
|
|
|
|
R_API char *r_run_get_environ_profile(char **env) {
|
|
|
|
if (!env) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
RStrBuf *sb = r_strbuf_new (NULL);
|
|
|
|
while (*env) {
|
|
|
|
char *k = strdup (*env);
|
|
|
|
char *v = strchr (k, '=');
|
|
|
|
if (v) {
|
|
|
|
*v++ = 0;
|
|
|
|
v = r_str_escape_latin1 (v, false, true, true);
|
|
|
|
if (v) {
|
|
|
|
r_strbuf_appendf (sb, "setenv=%s=\"%s\"\n", k, v);
|
|
|
|
free (v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (k);
|
|
|
|
env++;
|
|
|
|
}
|
|
|
|
return r_strbuf_drain (sb);
|
|
|
|
}
|