diff options
author | Alexander Gluzsky <sss123next@list.ru> | 2013-02-08 14:27:01 +0000 |
---|---|---|
committer | Alexander Gluzsky <sss123next@list.ru> | 2013-02-08 14:27:01 +0000 |
commit | 4d20cb679ac6ca7efd23bc9ad99d33fc8794f693 (patch) | |
tree | c9279611b2ec59e03102a0520654e0b482e74c64 | |
parent | 8ba05a620bebb8f1400b20148be7012243e2ff9f (diff) |
"boosted" process management
misc process management optimizations (should be little faster now)
git-svn-id: http://svn.miranda-ng.org/main/trunk@3479 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
-rwxr-xr-x | plugins/New_GPG/src/commonheaders.h | 2 | ||||
-rwxr-xr-x | plugins/New_GPG/src/gpg_wrapper.cpp | 195 | ||||
-rwxr-xr-x | plugins/New_GPG/src/gpg_wrapper.h | 30 | ||||
-rwxr-xr-x | plugins/New_GPG/src/main.cpp | 401 | ||||
-rwxr-xr-x | plugins/New_GPG/src/messages.cpp | 147 | ||||
-rwxr-xr-x | plugins/New_GPG/src/options.cpp | 101 | ||||
-rwxr-xr-x | plugins/New_GPG/src/utilities.cpp | 286 |
7 files changed, 395 insertions, 767 deletions
diff --git a/plugins/New_GPG/src/commonheaders.h b/plugins/New_GPG/src/commonheaders.h index 6476f127cf..eb5f6d2a64 100755 --- a/plugins/New_GPG/src/commonheaders.h +++ b/plugins/New_GPG/src/commonheaders.h @@ -19,6 +19,7 @@ #define _CRT_SECURE_NO_WARNINGS #define MIRANDA_VER 0x0A00 + //windows #include <windows.h> #include <shlobj.h> @@ -47,6 +48,7 @@ using std::fstream; //boost + #include <boost/thread/thread.hpp> #include <boost/thread/mutex.hpp> #include <boost/nondet_random.hpp> diff --git a/plugins/New_GPG/src/gpg_wrapper.cpp b/plugins/New_GPG/src/gpg_wrapper.cpp index e110f74c73..5f9df7dfd1 100755 --- a/plugins/New_GPG/src/gpg_wrapper.cpp +++ b/plugins/New_GPG/src/gpg_wrapper.cpp @@ -15,64 +15,16 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "commonheaders.h" - //thx gpg module from Harald Treder, Zakhar V. Bardymov -//boost::mutex gpg_mutex; - -pxResult pxExecute(wstring *acommandline, char *ainput, string *aoutput, LPDWORD aexitcode, pxResult *result, HANDLE hProcess, PROCESS_INFORMATION *pr) +pxResult pxExecute(std::vector<std::wstring> &aargv, string *aoutput, LPDWORD aexitcode, pxResult *result, boost::process::child *_child) { -// gpg_mutex.lock(); if(!gpg_valid) return pxNotConfigured; extern logtofile debuglog; - BOOL success; - STARTUPINFO sinfo = {0}; - SECURITY_ATTRIBUTES sattrs = {0}; - SECURITY_DESCRIPTOR sdesc = {0}; - PROCESS_INFORMATION pri = {0}; - HANDLE newstdin, newstdout, readstdout, writestdin; - char *inputpos; - unsigned long transfered; - int size; - - wstring commandline; - - sattrs.nLength=sizeof(SECURITY_ATTRIBUTES); - sattrs.bInheritHandle=TRUE; - InitializeSecurityDescriptor(&sdesc,SECURITY_DESCRIPTOR_REVISION); - SetSecurityDescriptorDacl(&sdesc,TRUE,NULL,FALSE); - sattrs.lpSecurityDescriptor=&sdesc; - - success=CreatePipe(&newstdin,&writestdin,&sattrs,0); - if (!success) - { - *result = pxCreatePipeFailed; - return pxCreatePipeFailed; - } - - success=CreatePipe(&readstdout,&newstdout,&sattrs,0); - if (!success) - { - CloseHandle(newstdin); - CloseHandle(writestdin); - *result = pxCreatePipeFailed; - return pxCreatePipeFailed; - } - - GetStartupInfo(&sinfo); - sinfo.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; - sinfo.wShowWindow=SW_HIDE; - sinfo.hStdOutput=newstdout; - sinfo.hStdError=newstdout; - sinfo.hStdInput=newstdin; - - char *mir_path = new char [MAX_PATH]; - CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); - SetCurrentDirectoryA(mir_path); - delete [] mir_path; + TCHAR *bin_path = UniGetContactSettingUtf(NULL, szGPGModuleName, "szGpgBinPath", _T("")); { if(!boost::filesystem::exists(bin_path)) @@ -84,98 +36,114 @@ pxResult pxExecute(wstring *acommandline, char *ainput, string *aoutput, LPDWORD return pxNotFound; } } - TCHAR *home_dir = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); - { //form initial command - commandline += _T("\""); - commandline += bin_path; - commandline += _T("\" --homedir \""); - commandline += home_dir; - commandline += _T("\" "); - commandline += _T("--display-charset utf-8 "); - commandline += _T("-z 9 "); - commandline += *acommandline; - mir_free(bin_path); - mir_free(home_dir); - } - if(bDebugLog) - debuglog<<std::string(time_str()+": gpg in: "+toUTF8(commandline)); + using namespace boost::process; + using namespace boost::process::initializers; + using namespace boost::iostreams; - success = CreateProcess(NULL, (TCHAR*)commandline.c_str(), NULL, NULL, TRUE, CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT, (void*)_T("LANGUAGE=en@quot\0LC_ALL=English\0"), NULL, &sinfo, &pri); - if (!success) + std::vector<std::wstring> argv; + std::vector<std::wstring> env; + env.push_back(L"LANGUAGE=en@quot"); + env.push_back(L"LC_ALL=English"); + argv.push_back(bin_path); + argv.push_back(L"--homedir"); + TCHAR *home_dir = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); + argv.push_back(home_dir); + mir_free(home_dir); + argv.push_back(L"--display-charset"); + argv.push_back(L"utf-8"); + argv.push_back(L"-z9"); + argv.insert(argv.end(), aargv.begin(), aargv.end()); + + if(bDebugLog) { - CloseHandle(newstdin); - CloseHandle(writestdin); - CloseHandle(newstdout); - CloseHandle(readstdout); - if(bDebugLog) - debuglog<<time_str()<<": Failed to create process\n"; -// gpg_mutex.unlock(); - *result = pxCreateProcessFailed; - return pxCreateProcessFailed; + std::wstring args; + for(int i = 0; i < argv.size(); ++i) + { + args += argv[i]; + args += L" "; + } + args.erase(args.size()-1, 1); + debuglog<<std::string(time_str()+": gpg in: "+toUTF8(args)); } - hProcess = pri.hProcess; - - inputpos=ainput; - while (true) + pipe pout = create_pipe(); { - if(!pri.hProcess) - break; - success=GetExitCodeProcess(pri.hProcess,aexitcode); - if (success && *aexitcode!=STILL_ACTIVE) - break; + file_descriptor_sink sout(pout.sink, close_handle); - storeOutput(readstdout,aoutput); + char *mir_path = new char [MAX_PATH]; + CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); - if (*inputpos!='\0') size=1; - else size=0; + child c = execute(set_args(argv), bind_stdout(sout), /*bind_stdin(sin),*/ show_window(SW_HIDE), hide_console(), inherit_env(), set_env(env), start_in_dir(toUTF16(mir_path))); + _child = &c; - success=WriteFile(writestdin,inputpos,size,&transfered,NULL); - inputpos+=transfered; - boost::this_thread::sleep(boost::posix_time::milliseconds(50)); + delete [] mir_path; + auto ec = wait_for_exit(*_child); + *aexitcode = ec; + _child = nullptr; } + + file_descriptor_source source(pout.source, close_handle); - storeOutput(readstdout,aoutput); + stream<file_descriptor_source> is(source); + try{ + std::string s; + while(std::getline(is, s)) + { + aoutput->append(s); + aoutput->append("\n"); + } + } + catch(const std::exception &e) + { + if(bDebugLog) + debuglog<<std::string(time_str()+": failed to read from stream with error: " + e.what() + "\n\tSuccesfully read : " + *aoutput); + } fix_line_term(*aoutput); if(bDebugLog) debuglog<<std::string(time_str()+": gpg out: "+*aoutput); - WaitForSingleObject(pri.hProcess,INFINITE); - - CloseHandle(pri.hThread); - CloseHandle(pri.hProcess); - CloseHandle(newstdin); - CloseHandle(newstdout); - CloseHandle(readstdout); - CloseHandle(writestdin); - *result = pxSuccess; -// gpg_mutex.unlock(); if(*aexitcode) { if(bDebugLog) debuglog<<std::string(time_str()+": warning: wrong gpg exit status, gpg output: "+*aoutput); return pxSuccessExitCodeInvalid; } + + return pxSuccess; } -void pxEexcute_thread(void *param) +void pxEexcute_thread(gpg_execution_params ¶ms) +{ + pxExecute(params.aargv, params.out, params.code, params.result, params.child); +} + +bool gpg_launcher(gpg_execution_params ¶ms, boost::posix_time::time_duration t) { - gpg_execution_params *params = (gpg_execution_params*)param; - pxResult result = pxExecute(params->cmd, params->useless, params->out, params->code, params->result, params->hProcess, params->proc); + bool ret = true; + boost::thread *gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, params)); + if(!gpg_thread->timed_join(t)) + { + ret = false; + delete gpg_thread; + if(params.child) + boost::process::terminate(*(params.child)); + if(bDebugLog) + debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); + } + return ret; } -pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, char *ainput, string *aoutput, LPDWORD aexitcode, pxResult *result, HANDLE hProcess, PROCESS_INFORMATION *pr, string &old_pass, string &new_pass) +pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, string *aoutput, LPDWORD aexitcode, pxResult *result, boost::process::child *_child, string &old_pass, string &new_pass) { -// gpg_mutex.lock(); if(!gpg_valid) return pxNotConfigured; extern logtofile debuglog; @@ -212,16 +180,16 @@ pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, char *ainput, argv.insert(argv.end(), aargv.begin(), aargv.end()); // pipe pout = create_pipe(); - pipe pin = create_pipe(); +// pipe pin = create_pipe(); // file_descriptor_sink sout(pout.sink, close_handle); - file_descriptor_source sin(pin.source, close_handle); +// file_descriptor_source sin(pin.source, close_handle); char *mir_path = new char [MAX_PATH]; CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); //execute(set_args(argv), bind_stdout(sout), bind_stdin(sin), show_window(SW_HIDE), hide_console(), inherit_env(), set_env(env), start_in_dir(toUTF16(mir_path))); - child c = execute(set_args(argv), bind_stdin(sin), inherit_env(), set_env(env), start_in_dir(toUTF16(mir_path))); - //child c = execute(run_exe("c:\\windows\\system32\\cmd.exe"), bind_stdin(sin), inherit_env(), set_env(env), start_in_dir(toUTF16(mir_path))); + child c = execute(set_args(argv), /*bind_stdin(sin), */inherit_env(), set_env(env), start_in_dir(toUTF16(mir_path))); + _child = &c; delete [] mir_path; @@ -241,7 +209,8 @@ pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, char *ainput, // out<<toUTF8(cmd)<<std::endl; //fucked gpg does not want to give us stdin/stdout - wait_for_exit(c); + wait_for_exit(*_child); + _child = nullptr; /* out<<old_pass<<std::endl; out<<new_pass<<std::endl; @@ -342,5 +311,5 @@ pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, char *ainput, void pxEexcute_passwd_change_thread(void *param) { gpg_execution_params_pass *params = (gpg_execution_params_pass*)param; - pxResult result = pxExecute_passwd_change(params->args, params->useless, params->out, params->code, params->result, params->hProcess, params->proc, params->old_pass, params->new_pass); + pxResult result = pxExecute_passwd_change(params->args, params->out, params->code, params->result, params->child, params->old_pass, params->new_pass); } diff --git a/plugins/New_GPG/src/gpg_wrapper.h b/plugins/New_GPG/src/gpg_wrapper.h index 44b2f4f493..9ff09578b5 100755 --- a/plugins/New_GPG/src/gpg_wrapper.h +++ b/plugins/New_GPG/src/gpg_wrapper.h @@ -32,36 +32,44 @@ typedef enum { } pxResult; -pxResult pxExecute(wstring *acommandline, char *ainput, string *aoutput, LPDWORD aexitcode, pxResult *result, HANDLE hProcess, PROCESS_INFORMATION *pr); -pxResult pxExecute_passwd_change(std::vector<std::string> &aargv, char *ainput, string *aoutput, LPDWORD aexitcode, pxResult *result, HANDLE hProcess, PROCESS_INFORMATION *pr, string &old_pass, string &new_pass); +pxResult pxExecute(std::vector<std::string> &aargv, string *aoutput, LPDWORD aexitcode, pxResult *result); +pxResult pxExecute_passwd_change(std::vector<std::string> &aargv, string *aoutput, LPDWORD aexitcode, pxResult *result, string &old_pass, string &new_pass); struct gpg_execution_params { - wstring *cmd; - char *useless; + std::vector<std::wstring> &aargv; +// char *useless; string *out; LPDWORD code; pxResult *result; - HANDLE hProcess; - PROCESS_INFORMATION *proc; + boost::process::child *child; +// HANDLE hProcess; +// PROCESS_INFORMATION *proc; + gpg_execution_params(std::vector<std::wstring> &a): aargv(a) + { + child = nullptr; + } }; struct gpg_execution_params_pass { std::vector<std::wstring> &args; string &old_pass, &new_pass; - char *useless; string *out; LPDWORD code; pxResult *result; - HANDLE hProcess; - PROCESS_INFORMATION *proc; + boost::process::child *child; +// HANDLE hProcess; +// PROCESS_INFORMATION *proc; gpg_execution_params_pass(std::vector<std::wstring> &a, std::string &o, std::string &n): args(a), old_pass(o), new_pass(n) - {} + { + child = nullptr; + } }; -void pxEexcute_thread(void *param); +void pxEexcute_thread(gpg_execution_params ¶ms); +bool gpg_launcher(gpg_execution_params ¶ms, boost::posix_time::time_duration t = boost::posix_time::seconds(10)); void pxEexcute_passwd_change_thread(void *param); #endif
\ No newline at end of file diff --git a/plugins/New_GPG/src/main.cpp b/plugins/New_GPG/src/main.cpp index 17822d3668..c70022a8d9 100755 --- a/plugins/New_GPG/src/main.cpp +++ b/plugins/New_GPG/src/main.cpp @@ -101,21 +101,15 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR pxResult result;
wstring::size_type p = 0, p2 = 0, stop = 0;
{
- gpg_execution_params params;
- wstring cmd = _T("--batch --list-secret-keys");
- params.cmd = &cmd;
- params.useless = "";
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--list-secret-keys");
+ gpg_execution_params params(cmd);
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
+ if(!gpg_launcher(params))
{
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break;
}
if(result == pxNotFound)
@@ -306,24 +300,18 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR }
string out;
DWORD code;
- wstring cmd = _T("--batch -a --export ");
- cmd += fp;
-// cmd += _T("\"");
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-a");
+ cmd.push_back(L"--export");
+ cmd.push_back(fp);
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
+ if(!gpg_launcher(params))
{
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break;
}
if(result == pxNotFound)
@@ -398,22 +386,16 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR DWORD code;
wstring::size_type p = 0, p2 = 0, stop = 0;
{
- wstring cmd = _T("--batch --list-secret-keys");
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--list-secret-keys");
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
+ if(!gpg_launcher(params))
{
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break;
}
if(result == pxNotFound)
@@ -471,23 +453,17 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR {
string out;
DWORD code;
- wstring cmd = _T("--batch --fingerprint ");
- cmd += fp;
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--fingerprint");
+ cmd.push_back(fp);
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread *gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread->timed_join(boost::posix_time::seconds(10)))
+ if(!gpg_launcher(params))
{
- delete gpg_thread;
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break;
}
if(result == pxNotFound)
@@ -507,17 +483,13 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR }
cmd.clear();
out.clear();
- cmd += _T("--batch --delete-secret-and-public-key --fingerprint ");
- cmd += fp;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--delete-secret-and-public-key");
+ cmd.push_back(L"--fingerprint");
+ cmd.push_back(fp);
mir_free(fp);
- gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread->timed_join(boost::posix_time::seconds(10)))
+ if(!gpg_launcher(params))
{
- delete gpg_thread;
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break;
}
if(result == pxNotFound)
@@ -594,14 +566,13 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR { //gpg execution
DWORD code;
string out;
- wstring cmd;
- cmd += _T("--batch --yes --gen-key \"");
- cmd += path;
- cmd += _T("\"");
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--yes");
+ cmd.push_back(L"--gen-key");
+ cmd.push_back(path);
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
@@ -613,14 +584,8 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR EnableWindow(GetDlgItem(hwndDlg, IDC_DELETE_KEY), 0);
EnableWindow(GetDlgItem(hwndDlg, IDC_KEY_LIST), 0);
EnableWindow(GetDlgItem(hwndDlg, IDC_GENERATE_RANDOM), 0);
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::minutes(10)))
+ if(!gpg_launcher(params, boost::posix_time::minutes(10)))
{
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break;
}
if(result == pxNotFound)
@@ -637,23 +602,18 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR {
string out;
DWORD code;
- wstring cmd = _T("--batch -a --export ");
- cmd += path;
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-a");
+ cmd.push_back(L"--export");
+ cmd.push_back(path);
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
+ if(!gpg_launcher(params))
{
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+"GPG execution timed out, aborted");
break;
}
if(result == pxNotFound)
@@ -720,23 +680,18 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR ListView_GetItemText(hwndList, itemnum, 0, fp, 16);
string out;
DWORD code;
- wstring cmd = _T("--batch -a --export ");
- cmd += fp;
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-a");
+ cmd.push_back(L"--export");
+ cmd.push_back(fp);
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
+ if(!gpg_launcher(params))
{
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break;
}
if(result == pxNotFound)
@@ -790,23 +745,18 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR ListView_GetItemText(hwndList, itemnum, 0, fp, 16);
string out;
DWORD code;
- wstring cmd = _T("--batch -a --export-secret-keys ");
- cmd += fp;
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-a");
+ cmd.push_back(L"--export-secret-keys");
+ cmd.push_back(fp);
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
+ if(!gpg_launcher(params))
{
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break;
}
if(result == pxNotFound)
@@ -832,7 +782,6 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR cmd.push_back(L"passwd");
gpg_execution_params_pass params(cmd, old_pass, new_pass);
pxResult result;
- params.useless = "";
params.out = &output;
params.code = &exitcode;
params.result = &result;
@@ -840,8 +789,8 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR if(!gpg_thread.timed_join(boost::posix_time::minutes(10)))
{
gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
+ if(params.child)
+ boost::process::terminate(*(params.child));
if(bDebugLog)
debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
DestroyWindow(hwndDlg);
@@ -960,24 +909,15 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp);
string out;
DWORD code;
- wstring cmd = _T("--version");
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--version");
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
gpg_valid = true;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
- {
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
- }
+ gpg_launcher(params);
gpg_valid = false;
DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath");
string::size_type p1 = out.find("(GnuPG) ");
@@ -1094,24 +1034,15 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp);
string out;
DWORD code;
- wstring cmd = _T("--version");
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--version");
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
gpg_valid = true;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
- {
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- gpg_thread.~thread();
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
- }
+ gpg_launcher(params);
gpg_valid = false;
DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath");
string::size_type p1 = out.find("(GnuPG) ");
@@ -1181,24 +1112,15 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp);
string out;
DWORD code;
- wstring cmd = _T("--version");
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--version");
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
gpg_valid = true;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
- {
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- gpg_thread.~thread();
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
- }
+ gpg_launcher(params);
gpg_valid = false;
DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath");
string::size_type p1 = out.find("(GnuPG) ");
@@ -1271,26 +1193,19 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, { //gpg execution
DWORD code;
string out;
- wstring cmd;
- cmd += _T("--batch --yes --gen-key \"");
- cmd += path;
- cmd += _T("\"");
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--yes");
+ cmd.push_back(L"--gen-key");
+ cmd.push_back(path);
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
gpg_valid = true;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::minutes(10)))
+ if(!gpg_launcher(params, boost::posix_time::minutes(10)))
{
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
gpg_valid = false;
break;
}
@@ -1308,24 +1223,19 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, {
string out;
DWORD code;
- wstring cmd = _T("--batch -a --export ");
- cmd += path;
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-a");
+ cmd.push_back(L"--export");
+ cmd.push_back(path);
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
gpg_valid = true;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
+ if(!gpg_launcher(params))
{
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+"GPG execution timed out, aborted");
gpg_valid = false;
break;
}
@@ -1630,14 +1540,13 @@ static INT_PTR CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wPara { //gpg execution
DWORD code;
string out;
- wstring cmd;
- cmd += _T("--batch --yes --gen-key \"");
- cmd += path;
- cmd += _T("\"");
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--yes");
+ cmd.push_back(L"--gen-key");
+ cmd.push_back(path);
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
@@ -1653,16 +1562,8 @@ static INT_PTR CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wPara EnableWindow(GetDlgItem(hwndDlg, IDC_KEY_EMAIL), 0);
EnableWindow(GetDlgItem(hwndDlg, IDC_KEY_COMMENT), 0);
EnableWindow(GetDlgItem(hwndDlg, IDC_KEY_EXPIRE_DATE), 0);
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::minutes(10)))
- {
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params, boost::posix_time::minutes(10)))
break;
- }
if(result == pxNotFound)
break;
}
@@ -1679,24 +1580,15 @@ static INT_PTR CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wPara DWORD code;
string::size_type p = 0, p2 = 0, stop = 0;
{
- wstring cmd = _T("--list-secret-keys");
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--list-secret-keys");
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
- {
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params))
break;
- }
if(result == pxNotFound)
break;
}
@@ -1842,24 +1734,16 @@ static INT_PTR CALLBACK DlgProcLoadExistingKey(HWND hwndDlg,UINT msg,WPARAM wPar DWORD code;
string::size_type p = 0, p2 = 0, stop = 0;
{
- wstring cmd = _T("--batch --list-keys");
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--list-keys");
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
- {
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params))
break;
- }
if(result == pxNotFound)
break;
}
@@ -1951,25 +1835,18 @@ static INT_PTR CALLBACK DlgProcLoadExistingKey(HWND hwndDlg,UINT msg,WPARAM wPar extern HWND hPubKeyEdit;
string out;
DWORD code;
- wstring cmd = _T("--batch -a --export ");
- cmd += id;
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-a");
+ cmd.push_back(L"--export");
+ cmd.push_back(id);
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
- {
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params))
break;
- }
if(result == pxNotFound)
break;
string::size_type s = 0;
@@ -2070,33 +1947,24 @@ static INT_PTR CALLBACK DlgProcImportKeyDialog(HWND hwndDlg, UINT msg, WPARAM wP {
string out;
DWORD code;
- wstring cmd = _T(" --keyserver \"");
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--keyserver");
TCHAR *server= new TCHAR [128];
GetDlgItemText(hwndDlg, IDC_KEYSERVER, server, 128);
- cmd += server;
+ cmd.push_back(server);
delete [] server;
- cmd += _T("\" --recv-keys ");
+ cmd.push_back(L"--recv-keys");
// char *tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyID_Prescense", "");
// TCHAR *tmp2 = mir_a2t(tmp);
// mir_free(tmp);
- cmd += toUTF16(hcontact_data[hContact].key_in_prescense);
+ cmd.push_back(toUTF16(hcontact_data[hContact].key_in_prescense));
// mir_free(tmp2);
- gpg_execution_params params;
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
- {
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
- }
+ gpg_launcher(params);
MessageBoxA(0, out.c_str(), "GPG output", MB_OK);
}
break;
@@ -2244,23 +2112,15 @@ void InitCheck() pxResult result;
wstring::size_type p = 0, p2 = 0, stop = 0;
{
- gpg_execution_params params;
- wstring cmd = _T("--batch --list-secret-keys");
- params.cmd = &cmd;
- params.useless = "";
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--list-secret-keys");
+ gpg_execution_params params(cmd);
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
- {
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params))
return;
- }
if(result == pxNotFound)
return;
}
@@ -2495,7 +2355,7 @@ void ImportKey() DBWriteContactSettingTString(hContact, szGPGModuleName, "GPGPubKey", new_key.c_str());
new_key.clear();
{ //gpg execute block
- wstring cmd;
+ std::vector<wstring> cmd;
TCHAR tmp2[MAX_PATH] = {0};
TCHAR *ptmp;
string output;
@@ -2516,28 +2376,17 @@ void ImportKey() mir_free(ptmp);
f<<new_key.c_str();
f.close();
- cmd += _T(" --batch ");
- cmd += _T(" --import \"");
- cmd += tmp2;
- cmd += _T("\"");
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--import");
+ cmd.push_back(tmp2);
}
- gpg_execution_params params;
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &output;
params.code = &exitcode;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
- {
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params))
return;
- }
if(result == pxNotFound)
return;
{
diff --git a/plugins/New_GPG/src/messages.cpp b/plugins/New_GPG/src/messages.cpp index 25519d1abc..262aa2abfa 100755 --- a/plugins/New_GPG/src/messages.cpp +++ b/plugins/New_GPG/src/messages.cpp @@ -97,8 +97,8 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, extern TCHAR *password; string out; DWORD code; - wstring cmd; - cmd += _T("--batch "); + std::vector<wstring> cmd; + cmd.push_back(L"--batch"); { char *inkeyid = UniGetContactSettingUtf(metaIsProtoMetaContacts(hContact)?metaGetMostOnline(hContact):hContact, szGPGModuleName, "InKeyID", ""); TCHAR *pass = NULL; @@ -119,17 +119,15 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, } if(pass && pass[0]) { - cmd += _T("--passphrase \""); - cmd += pass; - cmd += _T("\" "); + cmd.push_back(L"--passphrase"); + cmd.push_back(pass); } else if(password && password[0]) { if(bDebugLog) debuglog<<std::string(time_str()+": info: found password in memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))+" with password"); - cmd += _T("--passphrase \""); - cmd += password; - cmd += _T("\" "); + cmd.push_back(L"--passphrase"); + cmd.push_back(password); } else if (bDebugLog) debuglog<<std::string(time_str()+": info: passwords not found in database or memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))+" with out password"); @@ -142,29 +140,18 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, path += decfile; boost::filesystem::remove(path); } - cmd += _T("--output \""); - cmd += tmp2; - cmd += _T("\\tmp\\"); - cmd += decfile; - cmd += _T("\""); - cmd += _T(" -d -a \""); - cmd += path; - cmd += _T("\""); - gpg_execution_params params; + cmd.push_back(L"--output"); + cmd.push_back(std::wstring(tmp2) + L"\\tmp\\" + decfile); + cmd.push_back(L"-d"); + cmd.push_back(L"-a"); + cmd.push_back(path); + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &out; params.code = &code; params.result = &result; - boost::thread *gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params)) { - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); boost::filesystem::remove(path); HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); BYTE enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); @@ -214,32 +201,24 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, new_key_hcnt_mutex.lock(); new_key_hcnt = hContact; ShowLoadKeyPasswordWindow(); - wstring cmd2 = cmd; + std::vector<wstring> cmd2 = cmd; if(password) { if(bDebugLog) debuglog<<std::string(time_str()+": info: found password in memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))); - wstring tmp = _T("--passphrase \""); - tmp += password; - tmp += _T("\" "); - cmd2.insert(0, tmp); + std::vector<wstring> tmp; + tmp.push_back(L"--passphrase"); + tmp.push_back(password); + cmd2.insert(cmd2.begin(), tmp.begin(), tmp.end()); } out.clear(); - gpg_execution_params params; + gpg_execution_params params(cmd2); pxResult result; - params.cmd = &cmd2; - params.useless = ""; params.out = &out; params.code = &code; params.result = &result; - gpg_thread = gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params)) { - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); boost::filesystem::remove(path); HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); BYTE enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); @@ -257,14 +236,8 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, } } out.clear(); - gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params)) { - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); boost::filesystem::remove(path); HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); BYTE enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); @@ -421,7 +394,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) s2 += _tcslen(_T("-----END PGP PUBLIC KEY BLOCK-----")); DBWriteContactSettingTString(ccs->hContact, szGPGModuleName, "GPGPubKey", str.substr(s1,s2-s1).c_str()); { //gpg execute block - wstring cmd; + std::vector<wstring> cmd; TCHAR tmp2[MAX_PATH] = {0}; TCHAR *ptmp; string output; @@ -445,28 +418,17 @@ int RecvMsgSvc(WPARAM w, LPARAM l) mir_free(ptmp); f<<new_key.c_str(); f.close(); - cmd += _T(" --batch "); - cmd += _T(" --import \""); - cmd += tmp2; - cmd += _T("\""); + cmd.push_back(L"--batch"); + cmd.push_back(L"--import"); + cmd.push_back(tmp2); } - gpg_execution_params params; + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &output; params.code = &exitcode; params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) - { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); + if(!gpg_launcher(params)) return 1; - } boost::filesystem::remove(tmp2); if(result == pxNotFound) return 1; @@ -664,7 +626,8 @@ void SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) str.replace(i, 2, _T("\n")); */ string out; DWORD code; - wstring file = toUTF16(get_random(10)), path, cmd; + wstring file = toUTF16(get_random(10)), path; + std::vector<std::wstring> cmd; extern bool bJabberAPI, bIsMiranda09; char *tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyID", ""); if(!tmp[0]) @@ -677,24 +640,29 @@ void SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) return; } if(!bJabberAPI || !bIsMiranda09) //force jabber to handle encrypted message by itself - cmd += _T("--comment \"\" --no-version "); + { + cmd.push_back(L"--comment"); + cmd.push_back(L"\"\""); + cmd.push_back(L"--no-version"); + } if(DBGetContactSettingByte(hContact, szGPGModuleName, "bAlwaysTrust", 0)) - cmd += _T("--trust-model always "); - cmd += _T("--batch --yes -e -a -t -r "); + { + cmd.push_back(L"--trust-model"); + cmd.push_back(L"always"); + } + cmd.push_back(L"--batch"); + cmd.push_back(L"--yes"); + cmd.push_back(L"-eatr"); TCHAR *tmp2 = mir_a2t(tmp); mir_free(tmp); - cmd += tmp2; + cmd.push_back(tmp2); mir_free(tmp2); - cmd += _T(" \""); tmp2 = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); path = tmp2; - cmd += tmp2; + cmd.push_back(std::wstring(tmp2) + L"\\tmp\\" + file); mir_free(tmp2); - cmd += _T("\\tmp\\"); - cmd += file; path += L"\\tmp\\"; path += file; - cmd += _T("\""); { fstream f(path.c_str(), std::ios::out); while(!f.is_open()) @@ -706,21 +674,13 @@ void SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) f.write(tmp.c_str(), tmp.size()); f.close(); } - gpg_execution_params params; + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &out; params.code = &code; params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params)) { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); //mir_free(msg); CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); return; @@ -737,22 +697,17 @@ void SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) if(MessageBox(0, TranslateT("We trying to encrypt with untrusted key, do you want to trust this key permanently ?"), TranslateT("Warning"), MB_YESNO) == IDYES) { DBWriteContactSettingByte(hContact, szGPGModuleName, "bAlwaysTrust", 1); - cmd.insert(0, _T("--trust-model always ")); - gpg_execution_params params; + std::vector<std::wstring> tmp; + tmp.push_back(L"--trust-model"); + tmp.push_back(L"always"); + cmd.insert(cmd.begin(), tmp.begin(), tmp.end()); + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &out; params.code = &code; params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params)) { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); //mir_free(msg); CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); return; diff --git a/plugins/New_GPG/src/options.cpp b/plugins/New_GPG/src/options.cpp index ddc6323479..4e63d6f17d 100755 --- a/plugins/New_GPG/src/options.cpp +++ b/plugins/New_GPG/src/options.cpp @@ -258,28 +258,22 @@ static INT_PTR CALLBACK DlgProcGpgOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP if(!keep) if(MessageBox(0, TranslateT("This key not used by any contact, do you want to remove it from public keyring ?"), TranslateT("Key info"), MB_YESNO) == IDYES) { - wstring cmd; + std::vector<wstring> cmd; string output; DWORD exitcode; - cmd += _T(" --batch --yes --delete-key "); + cmd.push_back(L"--batch"); + cmd.push_back(L"--yes"); + cmd.push_back(L"--delete-key"); ptmp = mir_a2t(tmp); - cmd += ptmp; + cmd.push_back(ptmp); mir_free(ptmp); - gpg_execution_params params; + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &output; params.code = &exitcode; params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params)) { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); mir_free(tmp); break; } @@ -525,21 +519,14 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp); string out; DWORD code; - wstring cmd = _T("--version"); - gpg_execution_params params; + std::vector<wstring> cmd; + cmd.push_back(L"--version"); + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &out; params.code = &code; params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) - { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - } + gpg_launcher(params); DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp_path); mir_free(tmp_path); string::size_type p1 = out.find("(GnuPG) "); @@ -844,26 +831,16 @@ static INT_PTR CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam { string out; DWORD code; - wstring cmd = _T(" --export -a "); -// TCHAR *tmp3 = mir_a2t(tmp); - cmd += toUTF16(hcontact_data[hcnt].key_in_prescense); -// mir_free(tmp3); - gpg_execution_params params; + std::vector<wstring> cmd; + cmd.push_back(L"--export"); + cmd.push_back(L"-a"); + cmd.push_back(toUTF16(hcontact_data[hcnt].key_in_prescense)); + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &out; params.code = &code; params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) - { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); - } + gpg_launcher(params); //TODO: handle errors if((out.find("-----BEGIN PGP PUBLIC KEY BLOCK-----") != string::npos) && (out.find("-----END PGP PUBLIC KEY BLOCK-----") != string::npos)) { string::size_type p = 0, stop = 0; @@ -971,7 +948,7 @@ static INT_PTR CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam tmp = (TCHAR*)mir_alloc(sizeof( TCHAR) * (key_buf.length()+1)); _tcscpy(tmp, key_buf.substr(ws1,ws2-ws1).c_str()); { //gpg execute block - wstring cmd; + std::vector<wstring> cmd; TCHAR tmp2[MAX_PATH] = {0}; TCHAR *ptmp; string output; @@ -997,28 +974,17 @@ static INT_PTR CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam } f<<str.c_str(); f.close(); - cmd += _T(" --batch "); - cmd += _T(" --import \""); - cmd += tmp2; - cmd += _T("\""); + cmd.push_back(L"--batch"); + cmd.push_back(L"--import"); + cmd.push_back(tmp2); } - gpg_execution_params params; + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &output; params.code = &exitcode; params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) - { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); + if(!gpg_launcher(params)) break; - } if(result == pxNotFound) break; mir_free(begin); @@ -1247,26 +1213,19 @@ static INT_PTR CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam { string out; DWORD code; - wstring cmd = _T("--batch -a --export "); - cmd += fp; + std::vector<wstring> cmd; + cmd.push_back(L"--batch"); + cmd.push_back(L"-a"); + cmd.push_back(L"--export"); + cmd.push_back(fp); mir_free(fp); - gpg_execution_params params; + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &out; params.code = &code; params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) - { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); + if(!gpg_launcher(params)) break; - } if(result == pxNotFound) break; string::size_type s = 0; diff --git a/plugins/New_GPG/src/utilities.cpp b/plugins/New_GPG/src/utilities.cpp index 558e7f6875..8e56388172 100755 --- a/plugins/New_GPG/src/utilities.cpp +++ b/plugins/New_GPG/src/utilities.cpp @@ -330,7 +330,8 @@ int onProtoAck(WPARAM w, LPARAM l) string out;
DWORD code;
pxResult result;
- wstring cmd = _T(" -o ");
+ std::vector<wstring> cmd;
+ cmd.push_back(L"-o");
wstring file = filename;
wstring::size_type p1 = file.rfind(_T(".gpg"));
file.erase(p1, _tcslen(_T(".gpg")));
@@ -339,10 +340,8 @@ int onProtoAck(WPARAM w, LPARAM l) if(MessageBox(0, TranslateT("Target file exists, do you want to replace it ?"), TranslateT("Warning"), MB_YESNO) == IDNO)
return 0;
}
+ cmd.push_back(file);
boost::filesystem::remove(file);
- file.insert(0, _T("\""));
- file.insert(file.length(), _T("\" "));
- cmd += file;
extern TCHAR *password;
{ // password
TCHAR *pass = NULL;
@@ -364,42 +363,29 @@ int onProtoAck(WPARAM w, LPARAM l) }
if(_tcslen(pass) > 0)
{
- cmd += _T("--passphrase \"");
- cmd += pass;
- cmd += _T("\" ");
+ cmd.push_back(L"--passphrase");
+ cmd.push_back(pass);
}
else if(password)
{
if(bDebugLog)
debuglog<<std::string(time_str()+": info: found password in memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR))+" with password");
- cmd += _T("--passphrase \"");
- cmd += password;
- cmd += _T("\" ");
+ cmd.push_back(L"--passphrase");
+ cmd.push_back(password);
}
else if (bDebugLog)
debuglog<<std::string(time_str()+": info: passwords not found in database or memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR))+" with out password");
mir_free(pass);
mir_free(keyid);
}
- cmd += _T(" -d \"");
- cmd += filename;
- cmd += _T("\"");
- gpg_execution_params params;
- params.cmd = &cmd;
- params.useless = "";
+ cmd.push_back(L"-d");
+ cmd.push_back(filename);
+ gpg_execution_params params(cmd);
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread *gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread->timed_join(boost::posix_time::minutes(15)))
- {
- delete gpg_thread;
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params, boost::posix_time::minutes(15)))
return 0;
- }
while(out.find("public key decryption failed: bad passphrase") != string::npos)
{
extern bool _terminate;
@@ -423,32 +409,24 @@ int onProtoAck(WPARAM w, LPARAM l) new_key_hcnt_mutex.lock();
new_key_hcnt = ack->hContact;
ShowLoadKeyPasswordWindow();
- wstring cmd2 = cmd;
+ std::vector<wstring> cmd2 = cmd;
if(password)
{
if(bDebugLog)
debuglog<<std::string(time_str()+": info: found password in memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)));
- wstring tmp = _T("--passphrase \"");
- tmp += password;
- tmp += _T("\" ");
- cmd2.insert(0, tmp);
+ std::vector<wstring> tmp;
+ tmp.push_back(L"--passphrase");
+ tmp.push_back(password);
+ cmd2.insert(cmd2.begin(), tmp.begin(), tmp.end());
}
out.clear();
- gpg_execution_params params;
+ gpg_execution_params params(cmd2);
//pxResult result;
- params.cmd = &cmd2;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
- gpg_thread = gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread->timed_join(boost::posix_time::seconds(15)))
+ if(!gpg_launcher(params, boost::posix_time::seconds(15)))
{
- delete gpg_thread;
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
//boost::filesystem::remove(filename);
return 0;
}
@@ -498,7 +476,10 @@ std::wstring encrypt_file(HANDLE hContact, TCHAR *filename) DWORD code;
pxResult result;
HANDLE hcnt = metaIsProtoMetaContacts(hContact)?metaGetMostOnline(hContact):hContact;
- wstring cmd = _T("--batch --yes -r ");
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--tes");
+ cmd.push_back(L"-r");
char *keyid = UniGetContactSettingUtf(hcnt, szGPGModuleName, "KeyID", "");
TCHAR *szKeyid = mir_a2t(keyid);
TCHAR *name = _tcsrchr(filename,_T('\\'));
@@ -508,58 +489,42 @@ std::wstring encrypt_file(HANDLE hContact, TCHAR *filename) name++;
TCHAR *file_out = new TCHAR [_tcslen(filename)+4];
mir_sntprintf(file_out, _tcslen(name)+7, _T("%s.gpg"), name);
- cmd += szKeyid;
+ cmd.push_back(szKeyid);
if(DBGetContactSettingByte(hcnt, szGPGModuleName, "bAlwaysTrust", 0))
- cmd += _T(" --trust-model always ");
+ {
+ cmd.push_back(L"--trust-model");
+ cmd.push_back(L"always");
+ }
mir_free(szKeyid);
mir_free(keyid);
- cmd += _T(" -o \"");
+ cmd.push_back(L"-o");
TCHAR *temp = _tgetenv(_T("TEMP"));
- cmd += temp;
- cmd += _T("\\");
- cmd += file_out;
+ cmd.push_back(wstring(temp) + L"\\" + file_out);
wstring path_out = temp;
path_out += _T("\\");
path_out += file_out;
boost::filesystem::remove(path_out);
- cmd += _T("\" ");
- cmd += _T(" -e \"");
- cmd += filename;
- cmd += _T("\" ");
- gpg_execution_params params;
- params.cmd = &cmd;
- params.useless = "";
+ cmd.push_back(L"-e");
+ cmd.push_back(filename);
+ gpg_execution_params params(cmd);
params.out = &out;
params.code = &code;
params.result = &result;
delete [] file_out;
- boost::thread *gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread->timed_join(boost::posix_time::seconds(180)))
- {
- delete gpg_thread;
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params, boost::posix_time::minutes(3)))
return 0;
- }
if(out.find("There is no assurance this key belongs to the named user") != string::npos)
{
out.clear();
if(MessageBox(0, TranslateT("We trying to encrypt with untrusted key, do you want to trust this key permanently ?"), TranslateT("Warning"), MB_YESNO) == IDYES)
{
DBWriteContactSettingByte(hcnt, szGPGModuleName, "bAlwaysTrust", 1);
- cmd.insert(0, _T("--trust-model always "));
- gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread->timed_join(boost::posix_time::seconds(180)))
- {
- delete gpg_thread;
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ std::vector<std::wstring> tmp;
+ tmp.push_back(L"--trust-model");
+ tmp.push_back(L"always");
+ cmd.insert(cmd.begin(), tmp.begin(), tmp.end());
+ if(!gpg_launcher(params, boost::posix_time::minutes(3)))
return 0;
- }
}
else
return 0;
@@ -672,23 +637,6 @@ int onSendFile(WPARAM w, LPARAM l) return CallService(MS_PROTO_CHAINSEND, w, l);
}
-void storeOutput(HANDLE ahandle, string *output)
-{
- BOOL success;
- char *readbuffer = NULL;
- unsigned long transfered, available;
-
- do {
- PeekNamedPipe(ahandle,NULL,0,NULL,&available,NULL);
- if (!available)
- continue;
- readbuffer = (char*)mir_alloc(available);
- success=ReadFile(ahandle,readbuffer,available,&transfered,NULL);
- if (success && transfered)
- output->append(readbuffer, available);
- mir_free(readbuffer);
- } while (available>0);
-}
void HistoryLog(HANDLE hContact, db_event evt)
{
@@ -832,7 +780,7 @@ static JABBER_HANDLER_FUNC SendHandler(IJabberInterface *ji, HXML node, void *pU extern TCHAR *password;
string out;
DWORD code;
- wstring cmd;
+ std::vector<wstring> cmd;
{
char *inkeyid;
{
@@ -865,48 +813,37 @@ static JABBER_HANDLER_FUNC SendHandler(IJabberInterface *ji, HXML node, void *pU }
if(pass[0])
{
- cmd += _T("--passphrase \"");
- cmd += pass;
- cmd += _T("\" ");
+ cmd.push_back(L"--passphrase");
+ cmd.push_back(pass);
}
else if(password)
{
if(bDebugLog)
debuglog<<std::string(time_str()+": info: found password in memory, trying to encrypt message from self with password");
- cmd += _T("--passphrase \"");
- cmd += password;
- cmd += _T("\" ");
+ cmd.push_back(L"--passphrase");
+ cmd.push_back(password);
}
else if (bDebugLog)
debuglog<<std::string(time_str()+": info: passwords not found in database or memory, trying to encrypt message from self with out password");
mir_free(pass);
mir_free(inkeyid);
}
- cmd += _T("--local-user ");
+ cmd.push_back(L"--local-user");
path_c = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", _T(""));
- cmd += path_c;
- cmd += _T(" --default-key ");
- cmd += path_c;
+ cmd.push_back(path_c);
+ cmd.push_back(L"--default-key");
+ cmd.push_back(path_c);
mir_free(path_c);
- cmd += _T(" --batch --yes -a -b -s \"");
- cmd += path_out;
- cmd += _T("\" ");
- gpg_execution_params params;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--yes");
+ cmd.push_back(L"-abs");
+ cmd.push_back(path_out);
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(15)))
- {
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+"GPG execution timed out, aborted");
- }
+ gpg_launcher(params, boost::posix_time::seconds(15)); // TODO: handle errors
boost::filesystem::remove(path_out);
path_out += _T(".asc");
f.open(path_out.c_str(), std::ios::in | std::ios::ate | std::ios::binary);
@@ -1046,27 +983,18 @@ static JABBER_HANDLER_FUNC PrescenseHandler(IJabberInterface *ji, HXML node, voi { //gpg
string out;
DWORD code;
- wstring cmd = L" --verify -a \"";
- cmd += path_out;
- cmd += L"\"";
- cmd += L" \"";
- cmd += status_file_out;
- cmd += L"\"";
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--verify");
+ cmd.push_back(L"-a");
+ cmd.push_back(path_out);
+ cmd.push_back(status_file_out);
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(15)))
+ if(!gpg_launcher(params, boost::posix_time::seconds(15)))
{
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
return FALSE;
}
if(result == pxNotFound)
@@ -1231,24 +1159,15 @@ bool isGPGValid() DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp);
string out;
DWORD code;
- wstring cmd = _T("--version");
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--version");
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &out;
params.code = &code;
params.result = &result;
gpg_valid = true;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
- {
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
- }
+ gpg_launcher(params);
gpg_valid = false;
string::size_type p1 = out.find("(GnuPG) ");
if(p1 == string::npos)
@@ -1619,26 +1538,15 @@ void ExportGpGKeysFunc(int type) string out;
DWORD code;
pxResult result;
- gpg_execution_params params;
- wstring cmd = _T("--batch --export-secret-keys -a");
- params.cmd = &cmd;
- params.useless = "";
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-export-secret-keys");
+ cmd.push_back(L"-a");
+ gpg_execution_params params(cmd);
params.out = &out;
params.code = &code;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
- {
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
- //break;
- }
- else if(result == pxNotFound)
- ;//break;
- else
+ gpg_launcher(params); //TODO: handle errors
{
file<<out;
file<<std::endl;
@@ -1857,7 +1765,8 @@ INT_PTR ImportGpGKeys(WPARAM w, LPARAM l) }
if(found)
{
- wstring cmd, path;
+ wstring path;
+ std::vector<std::wstring> cmd;
TCHAR *ptmp;
string output;
DWORD exitcode;
@@ -1874,28 +1783,17 @@ INT_PTR ImportGpGKeys(WPARAM w, LPARAM l) wfstream f(path, std::ios::out);
f<<toUTF16(key).c_str();
f.close();
- cmd += _T(" --batch ");
- cmd += _T(" --import \"");
- cmd += path;
- cmd += _T("\"");
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--import");
+ cmd.push_back(path);
}
- gpg_execution_params params;
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &output;
params.code = &exitcode;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
- {
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params))
break;
- }
if(result == pxNotFound)
break;
if(result == pxSuccess)
@@ -1981,7 +1879,7 @@ INT_PTR ImportGpGKeys(WPARAM w, LPARAM l) }
if(strstr(line, "-----END PGP PRIVATE KEY BLOCK-----"))
{
- wstring cmd;
+ std::vector<wstring> cmd;
TCHAR tmp2[MAX_PATH] = {0};
TCHAR *ptmp;
string output;
@@ -1996,28 +1894,17 @@ INT_PTR ImportGpGKeys(WPARAM w, LPARAM l) wfstream f(tmp2, std::ios::out);
f<<toUTF16(key).c_str();
f.close();
- cmd += _T(" --batch ");
- cmd += _T(" --import \"");
- cmd += tmp2;
- cmd += _T("\"");
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--import");
+ cmd.push_back(tmp2);
}
- gpg_execution_params params;
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &output;
params.code = &exitcode;
params.result = &result;
- boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(10)))
- {
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params))
break;
- }
if(result == pxNotFound)
break;
if(result == pxSuccess)
@@ -2260,16 +2147,15 @@ static INT_PTR CALLBACK DlgProcChangePasswd(HWND hwndDlg, UINT msg, WPARAM wPara cmd.push_back(L"passwd");
gpg_execution_params_pass params(cmd, old_pass, new_pass);
pxResult result;
- params.useless = "";
params.out = &output;
params.code = &exitcode;
params.result = &result;
boost::thread gpg_thread(boost::bind(&pxEexcute_passwd_change_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(100)))
+ if(!gpg_thread.timed_join(boost::posix_time::minutes(10)))
{
gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
+ if(params.child)
+ boost::process::terminate(*(params.child));
if(bDebugLog)
debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
DestroyWindow(hwndDlg);
|