2020-12-16 13:25:36 +00:00
|
|
|
/* radare2 - LGPL - Copyright 2015-2020 pancake */
|
2015-03-19 01:48:44 +00:00
|
|
|
|
|
|
|
#include "r_lib.h"
|
|
|
|
#include "r_core.h"
|
|
|
|
#include "r_lang.h"
|
2015-04-03 00:01:11 +00:00
|
|
|
#if __WINDOWS__
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2017-05-10 20:12:49 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <process.h>
|
|
|
|
#endif
|
2015-03-19 01:48:44 +00:00
|
|
|
|
2015-04-03 00:01:11 +00:00
|
|
|
#if __WINDOWS__
|
2021-12-21 18:52:17 +00:00
|
|
|
static HANDLE myCreateChildProcess(const char *szCmdline) {
|
2017-10-30 11:28:59 +00:00
|
|
|
PROCESS_INFORMATION piProcInfo = {0};
|
|
|
|
STARTUPINFO siStartInfo = {0};
|
2015-04-03 00:01:11 +00:00
|
|
|
BOOL bSuccess = FALSE;
|
2015-08-19 21:11:53 +00:00
|
|
|
siStartInfo.cb = sizeof (STARTUPINFO);
|
2019-05-05 09:11:59 +00:00
|
|
|
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
|
2020-06-25 00:42:58 +00:00
|
|
|
siStartInfo.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
|
|
|
|
siStartInfo.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
|
|
siStartInfo.hStdError = GetStdHandle (STD_ERROR_HANDLE);
|
2019-05-05 09:11:59 +00:00
|
|
|
|
2019-03-18 08:05:38 +00:00
|
|
|
LPTSTR cmdline_ = r_sys_conv_utf8_to_win (szCmdline);
|
2017-10-30 11:28:59 +00:00
|
|
|
bSuccess = CreateProcess (NULL, cmdline_, NULL, NULL,
|
2015-05-13 10:39:51 +00:00
|
|
|
TRUE, 0, NULL, NULL, &siStartInfo, &piProcInfo);
|
2017-10-30 11:28:59 +00:00
|
|
|
free (cmdline_);
|
2019-05-05 09:11:59 +00:00
|
|
|
return bSuccess ? piProcInfo.hProcess : NULL;
|
2015-10-25 22:51:52 +00:00
|
|
|
}
|
2018-03-23 14:03:17 +00:00
|
|
|
|
2015-10-27 09:34:48 +00:00
|
|
|
static HANDLE hPipeInOut = NULL;
|
|
|
|
static HANDLE hproc = NULL;
|
2019-10-10 02:42:32 +00:00
|
|
|
#define PIPE_BUF_SIZE 8192
|
2018-03-23 14:03:17 +00:00
|
|
|
|
2017-09-02 15:36:14 +00:00
|
|
|
static void lang_pipe_run_win(RLang *lang) {
|
2015-10-27 09:34:48 +00:00
|
|
|
CHAR buf[PIPE_BUF_SIZE];
|
2019-05-05 09:11:59 +00:00
|
|
|
BOOL bSuccess = TRUE;
|
2015-10-27 09:34:48 +00:00
|
|
|
int i, res = 0;
|
2020-07-29 01:35:01 +00:00
|
|
|
DWORD dwRead = 0, dwWritten = 0, dwEvent;
|
|
|
|
HANDLE hRead = CreateEvent (NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!hRead) {
|
|
|
|
r_sys_perror ("lang_pipe_run_win/CreateEvent hRead");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
HANDLE hWritten = CreateEvent (NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!hWritten) {
|
|
|
|
r_sys_perror ("lang_pipe_run_win/CreateEvent hWritten");
|
|
|
|
CloseHandle (hRead);
|
|
|
|
return;
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_push (NULL, NULL);
|
2015-10-27 09:34:48 +00:00
|
|
|
do {
|
2016-11-20 18:20:14 +00:00
|
|
|
if (r_cons_is_breaked ()) {
|
2019-05-05 09:11:59 +00:00
|
|
|
TerminateProcess (hproc, 0);
|
2015-10-27 09:34:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-03-12 01:48:37 +00:00
|
|
|
OVERLAPPED oRead = {0};
|
2020-07-29 01:35:01 +00:00
|
|
|
oRead.hEvent = hRead;
|
2015-10-27 09:34:48 +00:00
|
|
|
memset (buf, 0, PIPE_BUF_SIZE);
|
2020-07-29 01:35:01 +00:00
|
|
|
ReadFile (hPipeInOut, buf, PIPE_BUF_SIZE, NULL, &oRead);
|
|
|
|
HANDLE hReadEvents[] = { hRead, hproc };
|
|
|
|
dwEvent = WaitForMultipleObjects (R_ARRAY_SIZE (hReadEvents), hReadEvents,
|
2021-10-28 17:59:10 +00:00
|
|
|
FALSE, INFINITE);
|
2020-07-29 01:35:01 +00:00
|
|
|
if (dwEvent == WAIT_OBJECT_0 + 1) { // hproc
|
|
|
|
break;
|
|
|
|
} else if (dwEvent == WAIT_FAILED) {
|
|
|
|
r_sys_perror ("lang_pipe_run_win/WaitForMultipleObjects read");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bSuccess = GetOverlappedResult (hPipeInOut, &oRead, &dwRead, TRUE);
|
|
|
|
if (!bSuccess) {
|
|
|
|
break;
|
2018-03-23 14:03:17 +00:00
|
|
|
}
|
|
|
|
if (bSuccess && dwRead > 0) {
|
2019-05-05 09:11:59 +00:00
|
|
|
buf[sizeof (buf) - 1] = 0;
|
2022-03-12 01:48:37 +00:00
|
|
|
OVERLAPPED oWrite = {0};
|
2020-07-29 01:35:01 +00:00
|
|
|
oWrite.hEvent = hWritten;
|
2015-10-27 09:34:48 +00:00
|
|
|
char *res = lang->cmd_str ((RCore*)lang->user, buf);
|
|
|
|
if (res) {
|
|
|
|
int res_len = strlen (res) + 1;
|
|
|
|
for (i = 0; i < res_len; i++) {
|
|
|
|
memset (buf, 0, PIPE_BUF_SIZE);
|
|
|
|
dwWritten = 0;
|
2019-05-05 09:11:59 +00:00
|
|
|
int writelen = res_len - i;
|
2020-07-29 01:35:01 +00:00
|
|
|
WriteFile (hPipeInOut, res + i,
|
2021-10-28 17:59:10 +00:00
|
|
|
writelen > PIPE_BUF_SIZE? PIPE_BUF_SIZE: writelen,
|
|
|
|
NULL, &oWrite);
|
2020-07-29 01:35:01 +00:00
|
|
|
HANDLE hWriteEvents[] = { hWritten, hproc };
|
|
|
|
dwEvent = WaitForMultipleObjects (R_ARRAY_SIZE (hWriteEvents), hWriteEvents,
|
2021-10-28 17:59:10 +00:00
|
|
|
FALSE, INFINITE);
|
2020-07-29 01:35:01 +00:00
|
|
|
if (dwEvent == WAIT_OBJECT_0 + 1) { // hproc
|
2015-10-27 09:34:48 +00:00
|
|
|
break;
|
2020-07-29 01:35:01 +00:00
|
|
|
} else if (dwEvent == WAIT_FAILED) {
|
|
|
|
r_sys_perror ("lang_pipe_run_win/WaitForMultipleObjects write");
|
2015-10-27 09:34:48 +00:00
|
|
|
}
|
2020-07-29 01:35:01 +00:00
|
|
|
BOOL rc = GetOverlappedResult (hPipeInOut, &oWrite, &dwWritten, TRUE);
|
2015-10-27 09:34:48 +00:00
|
|
|
if (!rc) {
|
2020-07-29 01:35:01 +00:00
|
|
|
r_sys_perror ("lang_pipe_run_win/WriteFile res");
|
2015-10-27 09:34:48 +00:00
|
|
|
}
|
|
|
|
if (dwWritten > 0) {
|
|
|
|
i += dwWritten - 1;
|
|
|
|
} else {
|
2020-07-29 01:35:01 +00:00
|
|
|
// send null termination // chop
|
|
|
|
r_sys_perror ("lang_pipe_run_win/dwWritten");
|
2015-10-27 09:34:48 +00:00
|
|
|
//WriteFile (hPipeInOut, "", 1, &dwWritten, NULL);
|
|
|
|
//break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (res);
|
|
|
|
} else {
|
2020-07-29 01:35:01 +00:00
|
|
|
WriteFile (hPipeInOut, "", 1, NULL, &oWrite);
|
|
|
|
if (!GetOverlappedResult (hPipeInOut, &oWrite, &dwWritten, TRUE)) {
|
|
|
|
r_sys_perror ("lang_pipe_run_win/WriteFile nul");
|
|
|
|
}
|
2015-10-27 09:34:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-29 01:35:01 +00:00
|
|
|
} while (true);
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_pop ();
|
2020-07-29 01:35:01 +00:00
|
|
|
CloseHandle (hWritten);
|
|
|
|
CloseHandle (hRead);
|
2015-10-27 09:34:48 +00:00
|
|
|
}
|
2015-10-25 22:51:52 +00:00
|
|
|
#else
|
|
|
|
static void env(const char *s, int f) {
|
|
|
|
char *a = r_str_newf ("%d", f);
|
|
|
|
r_sys_setenv (s, a);
|
|
|
|
// eprintf ("%s %s\n", s, a);
|
|
|
|
free (a);
|
2015-04-03 00:01:11 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-12-16 13:25:36 +00:00
|
|
|
static bool lang_pipe_run(RLang *lang, const char *code, int len) {
|
2015-07-14 11:55:58 +00:00
|
|
|
#if __UNIX__
|
2015-03-19 01:48:44 +00:00
|
|
|
int safe_in = dup (0);
|
|
|
|
int child, ret;
|
|
|
|
int input[2];
|
|
|
|
int output[2];
|
2015-08-19 21:11:53 +00:00
|
|
|
|
2018-11-25 00:46:57 +00:00
|
|
|
if (pipe (input) != 0) {
|
|
|
|
eprintf ("r_lang_pipe: pipe failed on input\n");
|
2018-12-22 09:01:03 +00:00
|
|
|
if (safe_in != -1) {
|
|
|
|
close (safe_in);
|
|
|
|
}
|
2018-11-25 00:46:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (pipe (output) != 0) {
|
|
|
|
eprintf ("r_lang_pipe: pipe failed on output\n");
|
2018-12-22 09:01:03 +00:00
|
|
|
if (safe_in != -1) {
|
|
|
|
close (safe_in);
|
|
|
|
}
|
2018-11-25 00:46:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-19 01:48:44 +00:00
|
|
|
env ("R2PIPE_IN", input[0]);
|
|
|
|
env ("R2PIPE_OUT", output[1]);
|
|
|
|
|
2015-04-11 03:00:05 +00:00
|
|
|
child = r_sys_fork ();
|
2015-03-19 01:48:44 +00:00
|
|
|
if (child == -1) {
|
|
|
|
/* error */
|
2022-03-15 18:27:34 +00:00
|
|
|
r_sys_perror ("pipe run");
|
2016-11-20 18:20:14 +00:00
|
|
|
} else if (!child) {
|
2015-03-19 01:48:44 +00:00
|
|
|
/* children */
|
2022-03-22 10:12:43 +00:00
|
|
|
int rc = 0;
|
2015-03-19 22:23:29 +00:00
|
|
|
r_sandbox_system (code, 1);
|
2022-03-22 10:12:43 +00:00
|
|
|
if (write (input[1], "", 1) != 1) {
|
|
|
|
rc = 1;
|
|
|
|
}
|
2015-03-19 01:48:44 +00:00
|
|
|
close (input[0]);
|
|
|
|
close (input[1]);
|
|
|
|
close (output[0]);
|
|
|
|
close (output[1]);
|
2019-06-02 15:28:17 +00:00
|
|
|
fflush (stdout);
|
|
|
|
fflush (stderr);
|
2022-03-22 10:12:43 +00:00
|
|
|
r_sys_exit (rc, true);
|
2015-09-14 00:08:31 +00:00
|
|
|
return false;
|
2015-03-19 01:48:44 +00:00
|
|
|
} else {
|
|
|
|
/* parent */
|
2019-07-10 11:22:57 +00:00
|
|
|
char *res, buf[8192]; // TODO: use the heap?
|
2015-05-18 16:22:47 +00:00
|
|
|
/* Close pipe ends not required in the parent */
|
2015-08-19 21:11:53 +00:00
|
|
|
close (output[1]);
|
|
|
|
close (input[0]);
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_push (NULL, NULL);
|
2015-03-19 01:48:44 +00:00
|
|
|
for (;;) {
|
2016-11-20 18:20:14 +00:00
|
|
|
if (r_cons_is_breaked ()) {
|
2015-03-19 01:48:44 +00:00
|
|
|
break;
|
2015-03-21 00:27:54 +00:00
|
|
|
}
|
2015-03-19 01:48:44 +00:00
|
|
|
memset (buf, 0, sizeof (buf));
|
2018-09-22 18:11:24 +00:00
|
|
|
void *bed = r_cons_sleep_begin ();
|
2018-03-23 14:03:17 +00:00
|
|
|
ret = read (output[0], buf, sizeof (buf) - 1);
|
2018-09-22 18:11:24 +00:00
|
|
|
r_cons_sleep_end (bed);
|
2019-07-10 11:22:57 +00:00
|
|
|
if (ret < 1) {
|
2015-03-19 01:48:44 +00:00
|
|
|
break;
|
2015-03-21 00:27:54 +00:00
|
|
|
}
|
2019-07-10 11:22:57 +00:00
|
|
|
if (!buf[0]) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
buf[sizeof (buf) - 1] = 0;
|
2015-03-19 01:48:44 +00:00
|
|
|
res = lang->cmd_str ((RCore*)lang->user, buf);
|
|
|
|
if (res) {
|
2020-12-16 13:25:36 +00:00
|
|
|
// r_cons_print (res);
|
2022-03-22 10:12:43 +00:00
|
|
|
size_t res_len = strlen (res) + 1;
|
|
|
|
if (write (input[1], res, res_len) != res_len) {
|
|
|
|
break;
|
|
|
|
}
|
2015-03-19 01:48:44 +00:00
|
|
|
free (res);
|
|
|
|
} else {
|
|
|
|
eprintf ("r_lang_pipe: NULL reply for (%s)\n", buf);
|
2022-03-22 10:12:43 +00:00
|
|
|
if (write (input[1], "", 1) != 1) {
|
|
|
|
break;
|
|
|
|
}
|
2015-03-19 01:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_pop ();
|
2015-03-19 01:48:44 +00:00
|
|
|
/* workaround to avoid stdin closed */
|
2016-11-20 18:20:14 +00:00
|
|
|
if (safe_in != -1) {
|
2015-03-19 01:48:44 +00:00
|
|
|
close (safe_in);
|
2016-11-20 18:20:14 +00:00
|
|
|
}
|
2020-09-11 10:47:27 +00:00
|
|
|
safe_in = -1;
|
|
|
|
char *term_in = ttyname (0);
|
|
|
|
if (term_in) {
|
|
|
|
safe_in = open (term_in, O_RDONLY);
|
|
|
|
if (safe_in != -1) {
|
|
|
|
dup2 (safe_in, 0);
|
|
|
|
} else {
|
|
|
|
eprintf ("Cannot open ttyname(0) %s\n", term_in);
|
|
|
|
}
|
2016-11-20 18:20:14 +00:00
|
|
|
}
|
2015-03-19 01:48:44 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 16:30:43 +00:00
|
|
|
close (input[0]);
|
2015-03-19 01:48:44 +00:00
|
|
|
close (input[1]);
|
|
|
|
close (output[0]);
|
2015-05-18 16:30:43 +00:00
|
|
|
close (output[1]);
|
2016-11-20 18:20:14 +00:00
|
|
|
if (safe_in != -1) {
|
2015-07-25 00:10:31 +00:00
|
|
|
close (safe_in);
|
2016-11-20 18:20:14 +00:00
|
|
|
}
|
2021-06-20 15:14:43 +00:00
|
|
|
#ifndef __wasi__
|
2018-08-28 08:35:50 +00:00
|
|
|
waitpid (child, NULL, WNOHANG);
|
2021-06-20 15:14:43 +00:00
|
|
|
#endif
|
2015-09-14 00:08:31 +00:00
|
|
|
return true;
|
2015-03-19 01:48:44 +00:00
|
|
|
#else
|
2015-04-03 00:01:11 +00:00
|
|
|
#if __WINDOWS__
|
2016-06-06 12:40:58 +00:00
|
|
|
char *r2pipe_var = r_str_newf ("R2PIPE_IN%x", _getpid ());
|
|
|
|
char *r2pipe_paz = r_str_newf ("\\\\.\\pipe\\%s", r2pipe_var);
|
2019-03-18 08:05:38 +00:00
|
|
|
LPTSTR r2pipe_paz_ = r_sys_conv_utf8_to_win (r2pipe_paz);
|
2017-10-30 11:28:59 +00:00
|
|
|
|
2019-05-05 09:11:59 +00:00
|
|
|
SetEnvironmentVariable (TEXT ("R2PIPE_PATH"), r2pipe_paz_);
|
2017-10-30 11:28:59 +00:00
|
|
|
hPipeInOut = CreateNamedPipe (r2pipe_paz_,
|
2020-07-29 01:35:01 +00:00
|
|
|
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
|
2015-10-27 03:29:30 +00:00
|
|
|
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES,
|
2015-10-27 09:34:48 +00:00
|
|
|
PIPE_BUF_SIZE,
|
|
|
|
PIPE_BUF_SIZE,
|
2020-06-25 00:42:58 +00:00
|
|
|
0, NULL);
|
|
|
|
if (hPipeInOut == INVALID_HANDLE_VALUE) {
|
2020-07-29 01:35:01 +00:00
|
|
|
r_sys_perror ("lang_pipe_run/CreateNamedPipe");
|
2020-06-25 00:42:58 +00:00
|
|
|
goto beach;
|
|
|
|
}
|
2020-07-29 01:35:01 +00:00
|
|
|
HANDLE hConnected = CreateEvent (NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!hConnected) {
|
|
|
|
r_sys_perror ("lang_pipe_run/CreateEvent hConnected");
|
|
|
|
goto pipe_cleanup;
|
|
|
|
}
|
2022-03-12 01:48:37 +00:00
|
|
|
OVERLAPPED oConnect = {0};
|
2020-07-29 01:35:01 +00:00
|
|
|
oConnect.hEvent = hConnected;
|
2015-10-25 22:51:52 +00:00
|
|
|
hproc = myCreateChildProcess (code);
|
2020-07-29 01:35:01 +00:00
|
|
|
BOOL connected = FALSE;
|
2016-06-06 12:40:58 +00:00
|
|
|
if (hproc) {
|
2020-07-29 01:35:01 +00:00
|
|
|
connected = ConnectNamedPipe (hPipeInOut, &oConnect);
|
|
|
|
DWORD err = GetLastError ();
|
|
|
|
if (!connected && err != ERROR_PIPE_CONNECTED) {
|
|
|
|
if (err == ERROR_IO_PENDING) {
|
|
|
|
HANDLE hEvents[] = { hConnected, hproc };
|
|
|
|
DWORD dwEvent = WaitForMultipleObjects (R_ARRAY_SIZE (hEvents), hEvents,
|
2021-10-28 17:59:10 +00:00
|
|
|
FALSE, INFINITE);
|
2020-07-29 01:35:01 +00:00
|
|
|
if (dwEvent == WAIT_OBJECT_0 + 1) { // hproc
|
|
|
|
goto cleanup;
|
|
|
|
} else if (dwEvent == WAIT_FAILED) {
|
|
|
|
r_sys_perror ("lang_pipe_run/WaitForMultipleObjects connect");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
DWORD dummy;
|
|
|
|
connected = GetOverlappedResult (hPipeInOut, &oConnect, &dummy, TRUE);
|
|
|
|
err = GetLastError ();
|
|
|
|
}
|
|
|
|
if (!connected && err != ERROR_PIPE_CONNECTED) {
|
|
|
|
r_sys_perror ("lang_pipe_run/ConnectNamedPipe");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
2017-09-02 15:36:14 +00:00
|
|
|
lang_pipe_run_win (lang);
|
2015-04-03 00:01:11 +00:00
|
|
|
}
|
2020-07-29 01:35:01 +00:00
|
|
|
cleanup:
|
|
|
|
CloseHandle (hConnected);
|
|
|
|
pipe_cleanup:
|
2019-10-10 02:42:32 +00:00
|
|
|
DeleteFile (r2pipe_paz_);
|
|
|
|
CloseHandle (hPipeInOut);
|
2020-06-25 00:42:58 +00:00
|
|
|
beach:
|
2016-06-06 12:40:58 +00:00
|
|
|
free (r2pipe_var);
|
|
|
|
free (r2pipe_paz);
|
2017-10-30 11:28:59 +00:00
|
|
|
free (r2pipe_paz_);
|
2022-03-15 18:54:04 +00:00
|
|
|
return hproc;
|
2015-03-19 01:48:44 +00:00
|
|
|
#endif
|
2015-04-03 00:01:11 +00:00
|
|
|
#endif
|
2015-03-19 01:48:44 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 13:25:36 +00:00
|
|
|
static bool lang_pipe_file(RLang *lang, const char *file) {
|
|
|
|
return lang_pipe_run (lang, file, -1);
|
|
|
|
}
|
|
|
|
|
2018-03-23 14:03:17 +00:00
|
|
|
static RLangPlugin r_lang_plugin_pipe = {
|
2015-03-19 01:48:44 +00:00
|
|
|
.name = "pipe",
|
|
|
|
.ext = "pipe",
|
2016-05-31 08:39:34 +00:00
|
|
|
.license = "LGPL",
|
2015-03-19 01:48:44 +00:00
|
|
|
.desc = "Use #!pipe node script.js",
|
|
|
|
.run = lang_pipe_run,
|
|
|
|
.run_file = (void*)lang_pipe_file,
|
|
|
|
};
|