From 59d11b71ce5666d6780b7bc8f1769f1c16b0d299 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Tue, 21 Dec 2021 06:54:00 +0100 Subject: [PATCH 1/9] Changed uint to ByteArray::size_type in StrUtil.cpp --- libretroshare/src/tor/StrUtil.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libretroshare/src/tor/StrUtil.cpp b/libretroshare/src/tor/StrUtil.cpp index 8e4ac9d20..558013fef 100644 --- a/libretroshare/src/tor/StrUtil.cpp +++ b/libretroshare/src/tor/StrUtil.cpp @@ -39,7 +39,7 @@ ByteArray quotedString(const ByteArray &string) out.push_back('"'); - for (uint i = 0; i < string.size(); ++i) + for (ByteArray::size_type i = 0; i < string.size(); ++i) { switch (string[i]) { @@ -67,7 +67,7 @@ ByteArray unquotedString(const ByteArray& string) ByteArray out; out.reserve(string.size() - 2); - for (uint i = 1; i < string.size(); ++i) + for (ByteArray::size_type i = 1; i < string.size(); ++i) { switch (string[i]) { @@ -89,9 +89,9 @@ std::list splitQuotedStrings(const ByteArray &input, char separator) { std::list out; bool inquote = false; - uint start = 0; + ByteArray::size_type start = 0; - for (uint i = 0; i < input.size(); ++i) + for (ByteArray::size_type i = 0; i < input.size(); ++i) { switch (input[i]) { From 8d020e7395191ee6de33227bf5f8d3707b2d5e4c Mon Sep 17 00:00:00 2001 From: thunder2 Date: Tue, 21 Dec 2021 07:49:01 +0100 Subject: [PATCH 2/9] Changed ifdef Q_OS_WIN to WINDOWS_SYS --- libretroshare/src/tor/TorManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libretroshare/src/tor/TorManager.cpp b/libretroshare/src/tor/TorManager.cpp index ff61acbfc..7b4a80363 100644 --- a/libretroshare/src/tor/TorManager.cpp +++ b/libretroshare/src/tor/TorManager.cpp @@ -623,7 +623,7 @@ std::string TorManagerPrivate::torExecutablePath() const return path; #endif -#ifdef Q_OS_WIN +#ifdef WINDOWS_SYS std::string filename("/tor/tor.exe"); #else std::string filename("/tor"); From 15d56f01ac6232640f2cf992b3c8276ed75ae477 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Tue, 21 Dec 2021 16:33:20 +0100 Subject: [PATCH 3/9] Added missing include to string in PendingOperation.h --- libretroshare/src/tor/PendingOperation.h | 1 + 1 file changed, 1 insertion(+) diff --git a/libretroshare/src/tor/PendingOperation.h b/libretroshare/src/tor/PendingOperation.h index 5115f3252..cec170f9e 100644 --- a/libretroshare/src/tor/PendingOperation.h +++ b/libretroshare/src/tor/PendingOperation.h @@ -33,6 +33,7 @@ #pragma once #include +#include /* Represents an asynchronous operation for reporting status * From 5b8ac7e49b1aae597ecf8f0f7969d4e2edb62fa6 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Tue, 21 Dec 2021 12:44:59 +0100 Subject: [PATCH 4/9] Added missing fclose to TorProcess::tryReadControlPort --- libretroshare/src/tor/TorProcess.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libretroshare/src/tor/TorProcess.cpp b/libretroshare/src/tor/TorProcess.cpp index 134e542db..908070fe8 100644 --- a/libretroshare/src/tor/TorProcess.cpp +++ b/libretroshare/src/tor/TorProcess.cpp @@ -412,6 +412,8 @@ bool TorProcess::tryReadControlPort() ByteArray data = ByteArray((unsigned char*)line,size).trimmed(); free(line); + fclose(file); + int p; if (data.startsWith("PORT=") && (p = data.lastIndexOf(':')) > 0) { mControlHost = data.mid(5, p - 5).toString(); From 085e102ce1234adf1a3ed02eae81bd7a8de88526 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Tue, 21 Dec 2021 07:34:26 +0100 Subject: [PATCH 5/9] Added RsFileUtil for os specific file functions Added RsFileUtil::set_fd_nonblock to set a file descriptor to non blocking --- libretroshare/src/CMakeLists.txt | 1 + libretroshare/src/libretroshare.pro | 2 ++ libretroshare/src/tor/TorProcess.cpp | 5 +-- libretroshare/src/util/rsfile.cc | 51 ++++++++++++++++++++++++++++ libretroshare/src/util/rsfile.h | 29 ++++++++++++++++ 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 libretroshare/src/util/rsfile.cc create mode 100644 libretroshare/src/util/rsfile.h diff --git a/libretroshare/src/CMakeLists.txt b/libretroshare/src/CMakeLists.txt index d3ceffecf..c33799d7c 100644 --- a/libretroshare/src/CMakeLists.txt +++ b/libretroshare/src/CMakeLists.txt @@ -347,6 +347,7 @@ list( util/rsurl.cc util/folderiterator.cc util/rsdir.cc + util/rsfile.cc util/dnsresolver.cc util/extaddrfinder.cc util/rsdebug.cc diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 5ebb64290..69dabca79 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -487,6 +487,7 @@ HEADERS += util/folderiterator.h \ util/rsmemory.h \ util/smallobject.h \ util/rsdir.h \ + util/rsfile.h \ util/argstream.h \ util/rsdiscspace.h \ util/rsnet.h \ @@ -641,6 +642,7 @@ SOURCES += util/folderiterator.cc \ util/rsexpr.cc \ util/smallobject.cc \ util/rsdir.cc \ + util/rsfile.cc \ util/rsdiscspace.cc \ util/rsnet.cc \ util/rsnet_ss.cc \ diff --git a/libretroshare/src/tor/TorProcess.cpp b/libretroshare/src/tor/TorProcess.cpp index 908070fe8..272ddb985 100644 --- a/libretroshare/src/tor/TorProcess.cpp +++ b/libretroshare/src/tor/TorProcess.cpp @@ -35,6 +35,7 @@ #include #include "util/rsdir.h" +#include "util/rsfile.h" #include "pqi/pqifdbin.h" #include "TorProcess.h" @@ -270,8 +271,8 @@ void TorProcess::start() return; // stop the control thread } - unix_fcntl_nonblock(fd[STDOUT_FILENO]); - unix_fcntl_nonblock(fd[STDERR_FILENO]); + RsFileUtil::set_fd_nonblock(fd[STDOUT_FILENO]); + RsFileUtil::set_fd_nonblock(fd[STDERR_FILENO]); mStdOutFD = new RsFdBinInterface(fd[STDOUT_FILENO]); mStdErrFD = new RsFdBinInterface(fd[STDERR_FILENO]); diff --git a/libretroshare/src/util/rsfile.cc b/libretroshare/src/util/rsfile.cc new file mode 100644 index 000000000..fc9f12c21 --- /dev/null +++ b/libretroshare/src/util/rsfile.cc @@ -0,0 +1,51 @@ +/******************************************************************************* + * libretroshare/src/util: rsfile.cc * + * * + * libretroshare: retroshare core library * + * * + * Copyright (C) 2021 Retroshare Team * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public License * + * along with this program. If not, see . * + * * + *******************************************************************************/ + +#include "util/rsfile.h" + +#ifdef WINDOWS_SYS +#include +#include +#include +#else +#include +#endif + +int RsFileUtil::set_fd_nonblock(int fd) +{ + int ret = 0; + +/******************* OS SPECIFIC PART ******************/ +#ifdef WINDOWS_SYS + DWORD mode = PIPE_NOWAIT; + WINBOOL result = SetNamedPipeHandleState((HANDLE) _get_osfhandle(fd), &mode, nullptr, nullptr); + + if (!result) { + ret = -1; + } +#else // ie UNIX + int flags = fcntl(fd, F_GETFL); + ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK); +#endif + + return ret; +} diff --git a/libretroshare/src/util/rsfile.h b/libretroshare/src/util/rsfile.h new file mode 100644 index 000000000..7569b5af5 --- /dev/null +++ b/libretroshare/src/util/rsfile.h @@ -0,0 +1,29 @@ +/******************************************************************************* + * libretroshare/src/util: rsfile.h * + * * + * libretroshare: retroshare core library * + * * + * Copyright (C) 2021 Retroshare Team * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public License * + * along with this program. If not, see . * + * * + *******************************************************************************/ + +#pragma once + +namespace RsFileUtil { + +int set_fd_nonblock(int fd); + +} From 057e872eca9439ce801bb2c4c67e172822cf7a3b Mon Sep 17 00:00:00 2001 From: thunder2 Date: Tue, 21 Dec 2021 18:12:59 +0100 Subject: [PATCH 6/9] Added missing initializations of members in constructor of TorControl --- libretroshare/src/tor/TorControl.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libretroshare/src/tor/TorControl.cpp b/libretroshare/src/tor/TorControl.cpp index 3ef879e35..5da312613 100644 --- a/libretroshare/src/tor/TorControl.cpp +++ b/libretroshare/src/tor/TorControl.cpp @@ -68,6 +68,11 @@ TorControl::TorControl() : mControlPort(0),mSocksPort(0),mStatus(NotConnected), mTorStatus(TorOffline),mHasOwnership(false) { mSocket = new TorControlSocket(this); + mControlPort = 0; + mSocksPort = 0; + mStatus = NotConnected; + mTorStatus = TorUnknown; + mHasOwnership = false; } static RsTorConnectivityStatus torConnectivityStatus(Tor::TorControl::Status t) From 38b4c5063568935f636c416d398b3bc6ee4e0aab Mon Sep 17 00:00:00 2001 From: thunder2 Date: Tue, 21 Dec 2021 07:43:44 +0100 Subject: [PATCH 7/9] Changed RsFdBinInterface to use recv/send for sockets and read/write for file descriptors on Windows --- libretroshare/src/pqi/pqifdbin.cc | 38 ++++++++++++++++++++++------ libretroshare/src/pqi/pqifdbin.h | 3 ++- libretroshare/src/pqi/rstcpsocket.cc | 4 +-- libretroshare/src/tor/TorProcess.cpp | 4 +-- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/libretroshare/src/pqi/pqifdbin.cc b/libretroshare/src/pqi/pqifdbin.cc index b413cf87b..20c2451f0 100644 --- a/libretroshare/src/pqi/pqifdbin.cc +++ b/libretroshare/src/pqi/pqifdbin.cc @@ -21,10 +21,11 @@ ******************************************************************************/ #include "util/rsprint.h" +#include "util/rsfile.h" #include "pqi/pqifdbin.h" -RsFdBinInterface::RsFdBinInterface(int file_descriptor) - : mCLintConnt(file_descriptor),mIsActive(false) +RsFdBinInterface::RsFdBinInterface(int file_descriptor, bool is_socket) + : mCLintConnt(file_descriptor),mIsSocket(is_socket),mIsActive(false) { mTotalReadBytes=0; mTotalInBufferBytes=0; @@ -53,8 +54,11 @@ void RsFdBinInterface::setSocket(int s) #else // On windows, there is no way to determine whether a socket is blocking or not, so we set it to non blocking whatsoever. - unsigned long int on = 1; - ioctlsocket(s, FIONBIO, &on); + if (mIsSocket) { + unix_fcntl_nonblock(s); + } else { + RsFileUtil::set_fd_nonblock(s); + } #endif mCLintConnt = s; @@ -82,8 +86,15 @@ int RsFdBinInterface::read_pending() char inBuffer[1025]; memset(inBuffer,0,1025); - ssize_t readbytes = read(mCLintConnt, inBuffer, sizeof(inBuffer)); // Needs read instead of recv which is only for sockets. - // Sockets should be set to non blocking by the client process. + ssize_t readbytes; +#if WINDOWS_SYS + if (mIsSocket) + // Windows needs recv for sockets + readbytes = recv(mCLintConnt, inBuffer, sizeof(inBuffer), 0); + else +#endif + readbytes = read(mCLintConnt, inBuffer, sizeof(inBuffer)); // Needs read instead of recv which is only for sockets. + // Sockets should be set to non blocking by the client process. if(readbytes == 0) { @@ -95,7 +106,11 @@ int RsFdBinInterface::read_pending() } if(readbytes < 0) { - if(errno != EWOULDBLOCK && errno != EAGAIN) + if(errno != 0 && errno != EWOULDBLOCK && errno != EAGAIN) +#ifdef WINDOWS_SYS + // A non blocking read to file descriptor gets ERROR_NO_DATA for empty data + if (mIsSocket == true || GetLastError() != ERROR_NO_DATA) +#endif RsErr() << "read() failed. Errno=" << errno ; return mTotalInBufferBytes; @@ -138,7 +153,14 @@ int RsFdBinInterface::write_pending() return mTotalOutBufferBytes; auto& p = out_buffer.front(); - int written = write(mCLintConnt, p.first, p.second); + int written; +#if WINDOWS_SYS + if (mIsSocket) + // Windows needs send for sockets + written = send(mCLintConnt, (char*) p.first, p.second, 0); + else +#endif + written = write(mCLintConnt, p.first, p.second); if(written < 0) { diff --git a/libretroshare/src/pqi/pqifdbin.h b/libretroshare/src/pqi/pqifdbin.h index df9af39a5..a3709c22e 100644 --- a/libretroshare/src/pqi/pqifdbin.h +++ b/libretroshare/src/pqi/pqifdbin.h @@ -25,7 +25,7 @@ class RsFdBinInterface: public BinInterface { public: - RsFdBinInterface(int file_descriptor); + RsFdBinInterface(int file_descriptor, bool is_socket); ~RsFdBinInterface(); // Implements BinInterface methods @@ -70,6 +70,7 @@ private: int write_pending(); int mCLintConnt; + bool mIsSocket; bool mIsActive; uint32_t mTotalReadBytes; uint32_t mTotalInBufferBytes; diff --git a/libretroshare/src/pqi/rstcpsocket.cc b/libretroshare/src/pqi/rstcpsocket.cc index ac91414b3..f42f100a2 100644 --- a/libretroshare/src/pqi/rstcpsocket.cc +++ b/libretroshare/src/pqi/rstcpsocket.cc @@ -18,12 +18,12 @@ #include "rstcpsocket.h" RsTcpSocket::RsTcpSocket(const std::string& tcp_address,uint16_t tcp_port) - :RsFdBinInterface(0),mState(DISCONNECTED),mConnectAddress(tcp_address),mConnectPort(tcp_port),mSocket(0) + :RsFdBinInterface(0, true),mState(DISCONNECTED),mConnectAddress(tcp_address),mConnectPort(tcp_port),mSocket(0) { } RsTcpSocket::RsTcpSocket() - :RsFdBinInterface(0),mState(DISCONNECTED),mConnectAddress("0.0.0.0"),mConnectPort(0),mSocket(0) + :RsFdBinInterface(0, true),mState(DISCONNECTED),mConnectAddress("0.0.0.0"),mConnectPort(0),mSocket(0) { } diff --git a/libretroshare/src/tor/TorProcess.cpp b/libretroshare/src/tor/TorProcess.cpp index 272ddb985..04f62a12a 100644 --- a/libretroshare/src/tor/TorProcess.cpp +++ b/libretroshare/src/tor/TorProcess.cpp @@ -274,8 +274,8 @@ void TorProcess::start() RsFileUtil::set_fd_nonblock(fd[STDOUT_FILENO]); RsFileUtil::set_fd_nonblock(fd[STDERR_FILENO]); - mStdOutFD = new RsFdBinInterface(fd[STDOUT_FILENO]); - mStdErrFD = new RsFdBinInterface(fd[STDERR_FILENO]); + mStdOutFD = new RsFdBinInterface(fd[STDOUT_FILENO], false); + mStdErrFD = new RsFdBinInterface(fd[STDERR_FILENO], false); } void TorProcess::tick() From 753250b4e8b05a22ecd72d2cacf092faba15562b Mon Sep 17 00:00:00 2001 From: thunder2 Date: Tue, 21 Dec 2021 09:03:56 +0100 Subject: [PATCH 8/9] Added os specific RsFileUtil::rs_getline --- libretroshare/src/tor/TorProcess.cpp | 2 +- libretroshare/src/util/rsfile.cc | 56 ++++++++++++++++++++++++++++ libretroshare/src/util/rsfile.h | 3 ++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/libretroshare/src/tor/TorProcess.cpp b/libretroshare/src/tor/TorProcess.cpp index 04f62a12a..1836c4f4e 100644 --- a/libretroshare/src/tor/TorProcess.cpp +++ b/libretroshare/src/tor/TorProcess.cpp @@ -409,7 +409,7 @@ bool TorProcess::tryReadControlPort() char *line = nullptr; size_t tmp_buffsize = 0; - size_t size = getline(&line,&tmp_buffsize,file); + size_t size = RsFileUtil::rs_getline(&line,&tmp_buffsize,file); ByteArray data = ByteArray((unsigned char*)line,size).trimmed(); free(line); diff --git a/libretroshare/src/util/rsfile.cc b/libretroshare/src/util/rsfile.cc index fc9f12c21..6d3f7b1ab 100644 --- a/libretroshare/src/util/rsfile.cc +++ b/libretroshare/src/util/rsfile.cc @@ -26,6 +26,7 @@ #include #include #include +#include #else #include #endif @@ -49,3 +50,58 @@ int RsFileUtil::set_fd_nonblock(int fd) return ret; } + +ssize_t RsFileUtil::rs_getline(char **lineptr, size_t *n, FILE *stream) +{ +/******************* OS SPECIFIC PART ******************/ +#ifdef WINDOWS_SYS + if (lineptr == nullptr || n == nullptr || stream == nullptr) { + errno = EINVAL; + return -1; + } + + if (*lineptr == nullptr || *n < 1) { + *n = BUFSIZ; + *lineptr = (char*) malloc(*n); + if (*lineptr == nullptr) { + *n = 0; + return -1; + } + } + + char *ptr = *lineptr; + while (true) { + int c = fgetc(stream); + if (c == -1) { + if (feof(stream)) { + *ptr = '\0'; + return (ssize_t) (ptr - *lineptr); + } + return -1; + } + + *ptr = c; + ++ptr; + + if (c == '\n') { + *ptr = '\0'; + return ptr - *lineptr; + } + if (ptr + 2 >= *lineptr + *n) { + size_t new_size = *n * 2; + ssize_t diff = ptr - *lineptr; + + char *new_lineptr = (char*) realloc(*lineptr, new_size); + if (new_lineptr == nullptr) { + return -1; + } + + *lineptr = new_lineptr; + *n = new_size; + ptr = new_lineptr + diff; + } + } +#else // ie UNIX + return getline(lineptr, n, stream); +#endif +} diff --git a/libretroshare/src/util/rsfile.h b/libretroshare/src/util/rsfile.h index 7569b5af5..9a23b6a4c 100644 --- a/libretroshare/src/util/rsfile.h +++ b/libretroshare/src/util/rsfile.h @@ -22,8 +22,11 @@ #pragma once +#include + namespace RsFileUtil { int set_fd_nonblock(int fd); +ssize_t rs_getline(char **lineptr, size_t *n, FILE *stream); } From 5dfbdc08692c3f4a6727fff81f9ba3594b436d41 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Tue, 21 Dec 2021 09:26:32 +0100 Subject: [PATCH 9/9] Fixed start and stop of tor process on Windows --- libretroshare/src/tor/TorProcess.cpp | 164 ++++++++++++++++++++------- libretroshare/src/tor/TorProcess.h | 8 +- 2 files changed, 128 insertions(+), 44 deletions(-) diff --git a/libretroshare/src/tor/TorProcess.cpp b/libretroshare/src/tor/TorProcess.cpp index 1836c4f4e..5f23a9512 100644 --- a/libretroshare/src/tor/TorProcess.cpp +++ b/libretroshare/src/tor/TorProcess.cpp @@ -41,6 +41,13 @@ #include "TorProcess.h" #include "CryptoKey.h" +#ifdef WINDOWS_SYS +#include "util/rsstring.h" + +#include +#define pipe(fds) _pipe(fds, 1024, _O_BINARY) +#endif + using namespace Tor; static const int INTERVAL_BETWEEN_CONTROL_PORT_READ_TRIES = 5; // try every 5 secs. @@ -111,7 +118,7 @@ std::string TorProcess::errorMessage() const // Does a popen, but dup all file descriptors (STDIN STDOUT and STDERR) to the // FDs supplied by the parent process -int popen3(int fd[3],const char **const cmd,pid_t& pid) +int popen3(int fd[3],const std::vector& args,TorProcessHandle& pid) { RsErr() << "Launching Tor in background..." ; @@ -124,42 +131,120 @@ int popen3(int fd[3],const char **const cmd,pid_t& pid) for(int i=0; i<3; i++) if(pipe(p[i])) goto error; - // and fork - pid = fork(); - if(-1 == pid) - goto error; - // in the parent? - if(pid) - { - // parent - fd[STDIN_FILENO] = p[STDIN_FILENO][1]; - close(p[STDIN_FILENO][0]); - fd[STDOUT_FILENO] = p[STDOUT_FILENO][0]; - close(p[STDOUT_FILENO][1]); - fd[STDERR_FILENO] = p[STDERR_FILENO][0]; - close(p[STDERR_FILENO][1]); - // success - return 0; + +#ifdef WINDOWS_SYS + // Set up members of the PROCESS_INFORMATION structure. + PROCESS_INFORMATION pi; + ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); + + // Set up members of the STARTUPINFO structure. + // This structure specifies the STDIN and STDOUT handles for redirection. + STARTUPINFO si; + ZeroMemory(&si, sizeof(STARTUPINFO)); + si.cb = sizeof(STARTUPINFO); + si.hStdInput = (HANDLE) _get_osfhandle(p[STDIN_FILENO][0]); + si.hStdOutput = (HANDLE) _get_osfhandle(p[STDOUT_FILENO][1]); + si.hStdError = (HANDLE) _get_osfhandle(p[STDERR_FILENO][1]); + si.dwFlags |= STARTF_USESTDHANDLES; + + if (si.hStdInput != INVALID_HANDLE_VALUE && + si.hStdOutput != INVALID_HANDLE_VALUE && + si.hStdError != INVALID_HANDLE_VALUE) { + // build commandline + std::string cmd; + for (std::vector::const_iterator it = args.begin(); it != args.end(); ++it) { + if (it != args.begin()) { + cmd += " "; + } + cmd += *it; + } + + std::wstring wcmd; + if (!librs::util::ConvertUtf8ToUtf16(cmd, wcmd)) { + goto error; + } + + WINBOOL success = CreateProcess(nullptr, + (LPWSTR) wcmd.c_str(), // command line + nullptr, // process security attributes + nullptr, // primary thread security attributes + TRUE, // handles are inherited + 0, // creation flags + nullptr, // use parent's environment + nullptr, // use parent's current directory + &si, // STARTUPINFO pointer + &pi); // receives PROCESS_INFORMATION + + if (success) { + pid = pi.hProcess; + + CloseHandle(pi.hThread); + + fd[STDIN_FILENO] = p[STDIN_FILENO][1]; + close(p[STDIN_FILENO][0]); + fd[STDOUT_FILENO] = p[STDOUT_FILENO][0]; + close(p[STDOUT_FILENO][1]); + fd[STDERR_FILENO] = p[STDERR_FILENO][0]; + close(p[STDERR_FILENO][1]); + + // success + return 0; + } } - else + + // fall through error + +#else { - RsErr() << "Launching sub-process..." ; - // child - dup2(p[STDIN_FILENO][0],STDIN_FILENO); - close(p[STDIN_FILENO][1]); - dup2(p[STDOUT_FILENO][1],STDOUT_FILENO); - close(p[STDOUT_FILENO][0]); - dup2(p[STDERR_FILENO][1],STDERR_FILENO); - close(p[STDERR_FILENO][0]); + const char *arguments[args.size()+1]; + int n=0; - // here we try and run it + // We first pushed everything into a vector of strings to save the pointers obtained from string returning methods + // by the time the process is launched. - execv(*cmd,const_cast(cmd)); + for(uint32_t i=0;i(arguments)); + + // if we are there, then we failed to launch our program + perror("Could not launch"); + fprintf(stderr," \"%s\"\n",*arguments); + } } +#endif error: e = errno; @@ -251,20 +336,9 @@ void TorProcess::start() for(auto s:mExtraSettings) args.push_back(s); - const char *arguments[args.size()+1]; - int n=0; - - // We first pushed everything into a vector of strings to save the pointers obtained from string returning methods - // by the time the process is launched. - - for(uint32_t i=0;i