From 26f425f0153de537bc676c9c38a68ef3edd882c5 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Fri, 27 Aug 2010 00:00:01 +0300 Subject: using threads, gpg execution timeout, fixed high cpu usage when waiting gpg process --- messages.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 8 deletions(-) (limited to 'messages.cpp') diff --git a/messages.cpp b/messages.cpp index 78994e5..10be19f 100644 --- a/messages.cpp +++ b/messages.cpp @@ -154,29 +154,68 @@ int RecvMsgSvc(WPARAM w, LPARAM l) cmd += _T(" -d -a \""); cmd += path; cmd += _T("\""); - //MessageBox(0, cmd.c_str(), _T("in"), MB_OK); - if(pxExecute(&cmd, "", &out, &code) == pxNotFound) + gpg_execution_params params; + pxResult result; + params.cmd = &cmd; + params.useless = ""; + params.out = &out; + params.code = &code; + params.result = &result; + HANDLE gpg_thread = mir_forkthread(pxEexcute_thread, (void*)¶ms); + if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT) + { + TerminateThread(gpg_thread, 0); + MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + } + if(result == pxNotFound) { MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK); DeleteFile(path.c_str()); return CallService(MS_PROTO_CHAINRECV, w, l); } - if(out.find("public key decryption failed: bad passphrase") != string::npos) + while(out.find("public key decryption failed: bad passphrase") != string::npos) { void ShowLoadKeyPasswordWindow(); ShowLoadKeyPasswordWindow(); + wstring cmd2 = cmd; if(password) { wstring tmp = _T("--passphrase \""); tmp += password; tmp += _T("\" "); - cmd.insert(0, tmp); + cmd2.insert(0, tmp); + } + out.clear(); + gpg_execution_params params; + pxResult result; + params.cmd = &cmd2; + params.useless = ""; + params.out = &out; + params.code = &code; + params.result = &result; + HANDLE gpg_thread = mir_forkthread(pxEexcute_thread, (void*)¶ms); + if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT) + { + TerminateThread(gpg_thread, 0); + MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + } + if(result == pxNotFound) + { + MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK); + DeleteFile(path.c_str()); + return CallService(MS_PROTO_CHAINRECV, w, l); } } out.clear(); - if(pxExecute(&cmd, "", &out, &code) == pxNotFound) + gpg_thread = mir_forkthread(pxEexcute_thread, (void*)¶ms); + if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT) { - MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK); + TerminateThread(gpg_thread, 0); + MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + } + if(result == pxNotFound) + { + MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK); DeleteFile(path.c_str()); return CallService(MS_PROTO_CHAINRECV, w, l); } @@ -319,7 +358,20 @@ int SendMsgSvc(WPARAM w, LPARAM l) mir_free(tmp); f.close(); } - if(pxExecute(&cmd, "", &out, &code) == pxNotFound) + gpg_execution_params params; + pxResult result; + params.cmd = &cmd; + params.useless = ""; + params.out = &out; + params.code = &code; + params.result = &result; + HANDLE gpg_thread = mir_forkthread(pxEexcute_thread, (void*)¶ms); + if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT) + { + TerminateThread(gpg_thread, 0); + MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + } + if(result == pxNotFound) { MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK); return CallService(MS_PROTO_CHAINSEND, w, l); @@ -331,7 +383,20 @@ int SendMsgSvc(WPARAM w, LPARAM l) { DBWriteContactSettingByte(ccs->hContact, szGPGModuleName, "bAlwaysTrust", 1); cmd.insert(0, _T("--trust-model always ")); - if(pxExecute(&cmd, "", &out, &code) == pxNotFound) + gpg_execution_params params; + pxResult result; + params.cmd = &cmd; + params.useless = ""; + params.out = &out; + params.code = &code; + params.result = &result; + HANDLE gpg_thread = mir_forkthread(pxEexcute_thread, (void*)¶ms); + if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT) + { + TerminateThread(gpg_thread, 0); + MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + } + if(result == pxNotFound) { MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK); return CallService(MS_PROTO_CHAINSEND, w, l); -- cgit v1.2.3