2021-08-27 00:46:18 +00:00
|
|
|
/* radare2 - LGPL - Copyright 2021 - pancake */
|
|
|
|
|
2010-04-12 00:22:52 +00:00
|
|
|
#include <r_types.h>
|
|
|
|
#include <r_util.h>
|
|
|
|
|
2022-12-10 18:23:37 +00:00
|
|
|
#if R2__WINDOWS__
|
2017-02-17 03:31:20 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
2013-09-14 12:04:08 +00:00
|
|
|
#include <tchar.h>
|
2010-04-12 00:22:52 +00:00
|
|
|
|
|
|
|
#define BUFSIZE 1024
|
2016-05-10 19:56:13 +00:00
|
|
|
void r_sys_perror_str(const char *fun);
|
2010-04-12 00:22:52 +00:00
|
|
|
|
2019-05-04 22:11:12 +00:00
|
|
|
#define ErrorExit(x) { r_sys_perror(x); return false; }
|
2019-03-21 08:16:12 +00:00
|
|
|
char *ReadFromPipe(HANDLE fh, int *outlen);
|
2010-04-12 00:22:52 +00:00
|
|
|
|
2020-06-14 14:08:32 +00:00
|
|
|
R_API char *r_sys_get_src_dir_w32(void) {
|
2017-10-30 11:28:59 +00:00
|
|
|
TCHAR fullpath[MAX_PATH + 1];
|
|
|
|
TCHAR shortpath[MAX_PATH + 1];
|
2017-02-17 03:31:20 +00:00
|
|
|
|
2017-10-30 11:28:59 +00:00
|
|
|
if (!GetModuleFileName (NULL, fullpath, MAX_PATH + 1) ||
|
|
|
|
!GetShortPathName (fullpath, shortpath, MAX_PATH + 1)) {
|
2019-06-20 16:10:01 +00:00
|
|
|
return NULL;
|
2017-02-17 03:31:20 +00:00
|
|
|
}
|
2019-09-22 22:15:12 +00:00
|
|
|
char *path = r_sys_conv_win_to_utf8 (shortpath);
|
|
|
|
char *dir = r_file_dirname (path);
|
|
|
|
if (!r_sys_getenv_asbool ("R_ALT_SRC_DIR")) {
|
|
|
|
char *tmp = dir;
|
|
|
|
dir = r_file_dirname (tmp);
|
|
|
|
free (tmp);
|
|
|
|
}
|
2019-06-20 16:10:01 +00:00
|
|
|
return dir;
|
2017-02-17 03:31:20 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 19:26:34 +00:00
|
|
|
R_API bool r_sys_cmd_str_full_w32(const char *cmd, const char *input, int ilen, char **output, int *outlen, char **sterr) {
|
2019-02-02 21:25:06 +00:00
|
|
|
HANDLE in = NULL;
|
2010-04-12 00:22:52 +00:00
|
|
|
HANDLE out = NULL;
|
2019-02-02 21:25:06 +00:00
|
|
|
HANDLE err = NULL;
|
2017-02-17 03:31:20 +00:00
|
|
|
SECURITY_ATTRIBUTES saAttr;
|
2010-04-12 00:22:52 +00:00
|
|
|
|
2017-02-17 03:31:20 +00:00
|
|
|
// Set the bInheritPlugin flag so pipe handles are inherited.
|
|
|
|
saAttr.nLength = sizeof (SECURITY_ATTRIBUTES);
|
|
|
|
saAttr.bInheritHandle = TRUE;
|
|
|
|
saAttr.lpSecurityDescriptor = NULL;
|
2019-02-02 21:25:06 +00:00
|
|
|
HANDLE fi = NULL;
|
|
|
|
HANDLE fo = NULL;
|
|
|
|
HANDLE fe = NULL;
|
|
|
|
|
|
|
|
// Create a pipe for the child process's STDOUT and STDERR.
|
|
|
|
// Ensure the read handle to the pipe for STDOUT and SRDERR and write handle of STDIN is not inherited.
|
|
|
|
if (output) {
|
|
|
|
if (!CreatePipe (&fo, &out, &saAttr, 0)) {
|
|
|
|
ErrorExit ("StdOutRd CreatePipe");
|
|
|
|
}
|
|
|
|
if (!SetHandleInformation (fo, HANDLE_FLAG_INHERIT, 0)) {
|
|
|
|
ErrorExit ("StdOut SetHandleInformation");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sterr) {
|
|
|
|
if (!CreatePipe (&fe, &err, &saAttr, 0)) {
|
|
|
|
ErrorExit ("StdErrRd CreatePipe");
|
|
|
|
}
|
|
|
|
if (!SetHandleInformation (fe, HANDLE_FLAG_INHERIT, 0)) {
|
|
|
|
ErrorExit ("StdErr SetHandleInformation");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (input) {
|
|
|
|
if (!CreatePipe (&fi, &in, &saAttr, 0)) {
|
|
|
|
ErrorExit ("StdInRd CreatePipe");
|
|
|
|
}
|
2019-04-01 12:34:41 +00:00
|
|
|
DWORD nBytesWritten;
|
2021-04-10 19:26:34 +00:00
|
|
|
WriteFile (in, input, ilen >= 0 ? ilen: strlen (input) + 1, &nBytesWritten, NULL);
|
2019-02-02 21:25:06 +00:00
|
|
|
if (!SetHandleInformation (in, HANDLE_FLAG_INHERIT, 0)) {
|
|
|
|
ErrorExit ("StdIn SetHandleInformation");
|
|
|
|
}
|
|
|
|
}
|
2010-04-12 00:22:52 +00:00
|
|
|
|
2019-02-02 21:25:06 +00:00
|
|
|
if (!r_sys_create_child_proc_w32 (cmd, fi, out, err)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-12 00:22:52 +00:00
|
|
|
|
2017-02-17 03:31:20 +00:00
|
|
|
// Close the write end of the pipe before reading from the
|
2010-04-12 00:22:52 +00:00
|
|
|
// read end of the pipe, to control child process execution.
|
|
|
|
// The pipe is assumed to have enough buffer space to hold the
|
|
|
|
// data the child process has already written to it.
|
2019-02-02 21:25:06 +00:00
|
|
|
if (in && !CloseHandle (in)) {
|
|
|
|
ErrorExit ("StdInWr CloseHandle");
|
|
|
|
}
|
|
|
|
if (out && !CloseHandle (out)) {
|
2017-02-17 03:31:20 +00:00
|
|
|
ErrorExit ("StdOutWr CloseHandle");
|
2019-02-02 21:25:06 +00:00
|
|
|
}
|
|
|
|
if (err && !CloseHandle (err)) {
|
|
|
|
ErrorExit ("StdErrWr CloseHandle");
|
|
|
|
}
|
2010-04-12 00:22:52 +00:00
|
|
|
|
2019-02-02 21:25:06 +00:00
|
|
|
if (output) {
|
2019-03-21 08:16:12 +00:00
|
|
|
*output = ReadFromPipe (fo, outlen);
|
2019-02-02 21:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sterr) {
|
2019-03-21 08:16:12 +00:00
|
|
|
*sterr = ReadFromPipe (fe, NULL);
|
2019-02-02 21:25:06 +00:00
|
|
|
}
|
|
|
|
if (fi && !CloseHandle (fi)) {
|
|
|
|
ErrorExit ("PipeIn CloseHandle");
|
|
|
|
}
|
|
|
|
if (fo && !CloseHandle (fo)) {
|
|
|
|
ErrorExit ("PipeOut CloseHandle");
|
|
|
|
}
|
|
|
|
if (fe && !CloseHandle (fe)) {
|
|
|
|
ErrorExit ("PipeErr CloseHandle");
|
|
|
|
}
|
2019-09-15 21:13:59 +00:00
|
|
|
|
2019-02-02 21:25:06 +00:00
|
|
|
return true;
|
2017-02-17 03:31:20 +00:00
|
|
|
}
|
2010-04-12 00:22:52 +00:00
|
|
|
|
2019-02-02 21:25:06 +00:00
|
|
|
R_API bool r_sys_create_child_proc_w32(const char *cmdline, HANDLE in, HANDLE out, HANDLE err) {
|
2017-10-30 11:28:59 +00:00
|
|
|
PROCESS_INFORMATION pi = {0};
|
|
|
|
STARTUPINFO si = {0};
|
|
|
|
LPTSTR cmdline_;
|
2019-02-02 21:25:06 +00:00
|
|
|
bool ret = false;
|
2019-05-04 22:11:12 +00:00
|
|
|
const size_t max_length = 32768 * sizeof (TCHAR);
|
|
|
|
LPTSTR _cmdline_ = malloc (max_length);
|
2019-01-31 19:39:32 +00:00
|
|
|
|
|
|
|
if (!_cmdline_) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-12 00:22:52 +00:00
|
|
|
|
2017-02-17 03:31:20 +00:00
|
|
|
// Set up members of the STARTUPINFO structure.
|
2010-04-12 00:22:52 +00:00
|
|
|
// This structure specifies the STDIN and STDOUT handles for redirection.
|
2017-10-30 11:28:59 +00:00
|
|
|
si.cb = sizeof (STARTUPINFO);
|
2019-02-02 21:25:06 +00:00
|
|
|
si.hStdError = err;
|
2017-10-30 11:28:59 +00:00
|
|
|
si.hStdOutput = out;
|
2019-02-02 21:25:06 +00:00
|
|
|
si.hStdInput = in;
|
2017-10-30 11:28:59 +00:00
|
|
|
si.dwFlags |= STARTF_USESTDHANDLES;
|
2019-03-18 08:05:38 +00:00
|
|
|
cmdline_ = r_sys_conv_utf8_to_win (cmdline);
|
2019-01-31 19:39:32 +00:00
|
|
|
ExpandEnvironmentStrings (cmdline_, _cmdline_, max_length - 1);
|
2017-10-30 11:28:59 +00:00
|
|
|
if ((ret = CreateProcess (NULL,
|
2019-01-31 19:39:32 +00:00
|
|
|
_cmdline_, // command line
|
2017-02-17 03:31:20 +00:00
|
|
|
NULL, // process security attributes
|
|
|
|
NULL, // primary thread security attributes
|
|
|
|
TRUE, // handles are inherited
|
|
|
|
0, // creation flags
|
|
|
|
NULL, // use parent's environment
|
|
|
|
NULL, // use parent's current directory
|
2019-01-31 19:39:32 +00:00
|
|
|
&si, // STARTUPINFO pointer
|
2019-09-15 21:13:59 +00:00
|
|
|
&pi))) { // receives PROCESS_INFORMATION
|
2019-02-02 21:25:06 +00:00
|
|
|
ret = true;
|
2017-10-30 11:28:59 +00:00
|
|
|
CloseHandle (pi.hProcess);
|
|
|
|
CloseHandle (pi.hThread);
|
|
|
|
} else {
|
|
|
|
r_sys_perror ("CreateProcess");
|
|
|
|
}
|
|
|
|
free (cmdline_);
|
2019-02-02 21:25:06 +00:00
|
|
|
free (_cmdline_);
|
2017-10-30 11:28:59 +00:00
|
|
|
return ret;
|
2010-04-12 00:22:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 08:16:12 +00:00
|
|
|
char *ReadFromPipe(HANDLE fh, int *outlen) {
|
2010-04-12 00:22:52 +00:00
|
|
|
DWORD dwRead;
|
2017-02-17 03:31:20 +00:00
|
|
|
CHAR chBuf[BUFSIZE];
|
2010-04-12 00:22:52 +00:00
|
|
|
BOOL bSuccess = FALSE;
|
|
|
|
int strl = 0;
|
2021-08-27 00:46:18 +00:00
|
|
|
int strsz = BUFSIZE + 1;
|
2010-04-12 00:22:52 +00:00
|
|
|
|
2019-03-21 08:16:12 +00:00
|
|
|
if (outlen) {
|
|
|
|
*outlen = 0;
|
|
|
|
}
|
2021-08-27 00:46:18 +00:00
|
|
|
char *str = malloc (strsz);
|
2017-07-05 01:40:14 +00:00
|
|
|
if (!str) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-02-02 21:25:06 +00:00
|
|
|
while (true) {
|
2010-04-12 00:22:52 +00:00
|
|
|
bSuccess = ReadFile (fh, chBuf, BUFSIZE, &dwRead, NULL);
|
2017-06-21 16:27:02 +00:00
|
|
|
if (!bSuccess || dwRead == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2010-04-12 00:22:52 +00:00
|
|
|
if (strl+dwRead>strsz) {
|
2017-07-05 01:40:14 +00:00
|
|
|
char *str_tmp = str;
|
2010-04-12 00:22:52 +00:00
|
|
|
strsz += 4096;
|
|
|
|
str = realloc (str, strsz);
|
2017-06-21 16:27:02 +00:00
|
|
|
if (!str) {
|
2017-07-05 01:40:14 +00:00
|
|
|
free (str_tmp);
|
2010-04-12 00:22:52 +00:00
|
|
|
return NULL;
|
2017-06-21 16:27:02 +00:00
|
|
|
}
|
2010-04-12 00:22:52 +00:00
|
|
|
}
|
|
|
|
memcpy (str+strl, chBuf, dwRead);
|
|
|
|
strl += dwRead;
|
2017-02-17 03:31:20 +00:00
|
|
|
}
|
2010-04-12 00:22:52 +00:00
|
|
|
str[strl] = 0;
|
2019-03-21 08:16:12 +00:00
|
|
|
if (outlen) {
|
|
|
|
*outlen = strl;
|
|
|
|
}
|
2010-04-12 00:22:52 +00:00
|
|
|
return str;
|
2017-02-17 03:31:20 +00:00
|
|
|
}
|
2010-04-12 00:22:52 +00:00
|
|
|
#endif
|