From d5721b974a58ed5a26f33346dae728d93a7c1803 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Fri, 8 Oct 2010 07:01:44 +0300 Subject: boost threads and mutexes, better error handling (thread's timeouts) --- commonheaders.h | 22 +++++++--- globals.h | 1 + gpg_wrapper.cpp | 9 ++-- log.cpp | 8 ++++ log.h | 1 + main.cpp | 133 +++++++++++++++++++++++++++++--------------------------- messages.cpp | 45 ++++++++++--------- new_gpg.vcproj | 8 ++-- options.cpp | 33 +++++++------- utilities.cpp | 21 +++++---- 10 files changed, 154 insertions(+), 127 deletions(-) diff --git a/commonheaders.h b/commonheaders.h index d76cc58..49a1b4d 100644 --- a/commonheaders.h +++ b/commonheaders.h @@ -16,15 +16,17 @@ #ifndef COMMONHEADERS_H #define COMMONHEADERS_H #define MIRANDA_VER 0x0901 +//windows #include -#include #include #include -#include -#include #include #include - +//c +#include +#include +#include +//c++ #include #include using std::map; @@ -39,6 +41,11 @@ using std::wstring; using std::wfstream; using std::fstream; +//boost +#include +#include + +//miranda #include #include #include @@ -58,13 +65,14 @@ using std::fstream; #include "m_metacontacts.h" #include "resource.h" + +//internal #include "constants.h" -#include "utilities.h" +#include "log.h" #include "globals.h" +#include "utilities.h" #include "main.h" #include "gpg_wrapper.h" #include "jabber_account.h" #include "metacontacts.h" -#include "log.h" -extern logtofile debuglog; #endif diff --git a/globals.h b/globals.h index 2f0d5a5..4b6533c 100644 --- a/globals.h +++ b/globals.h @@ -18,4 +18,5 @@ #define GLOBALS_H extern bool bAppendTags; extern TCHAR *inopentag, *inclosetag, *outopentag, *outclosetag; +extern logtofile debuglog; #endif diff --git a/gpg_wrapper.cpp b/gpg_wrapper.cpp index 226fd5e..a6c2723 100644 --- a/gpg_wrapper.cpp +++ b/gpg_wrapper.cpp @@ -18,12 +18,12 @@ //thx gpg module from Harald Treder, Zakhar V. Bardymov -HANDLE gpg_mutex = NULL; +boost::mutex gpg_mutex; pxResult pxExecute(wstring *acommandline, char *ainput, string *aoutput, LPDWORD aexitcode, pxResult *result) { - WaitForSingleObject(gpg_mutex, INFINITE); + gpg_mutex.lock(); extern logtofile debuglog; BOOL success; STARTUPINFO sinfo = {0}; @@ -99,7 +99,6 @@ pxResult pxExecute(wstring *acommandline, char *ainput, string *aoutput, LPDWORD debuglog<<"gpg in: "<timed_join(boost::posix_time::seconds(10))) { - TerminateThread(gpg_thread, 0); + delete gpg_thread; MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + break; } if(result == pxNotFound) { @@ -349,11 +353,12 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM cmd += _T("--batch --delete-secret-and-public-key --fingerprint "); cmd += fp; mir_free(fp); - gpg_thread = mir_forkthread(pxEexcute_thread, (void*)¶ms); - if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT) + gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) { - TerminateThread(gpg_thread, 0); + delete gpg_thread; MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + break; } if(result == pxNotFound) { @@ -445,6 +450,10 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szGpgBinPath", (SHGetValue(HKEY_CURRENT_USER, _T("Software\\GNU\\GnuPG"), _T("gpgProgram"), 0, path, &len) == ERROR_SUCCESS)?path:_T("")); if(tmp[0]) { + char *mir_path = new char [MAX_PATH]; + CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); + SetCurrentDirectoryA(mir_path); + delete [] mir_path; if(_waccess(tmp, 0) == -1) { if(errno == ENOENT) @@ -472,16 +481,9 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP 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); - } + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + gpg_thread.~thread(); DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath"); string::size_type p1 = out.find("(GnuPG) "); if(p1 != string::npos) @@ -556,6 +558,10 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP GetDlgItemText(hwndDlg, IDC_BIN_PATH, tmp, 512); if(tmp[0]) { + char *mir_path = new char [MAX_PATH]; + CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); + SetCurrentDirectoryA(mir_path); + delete [] mir_path; if(_waccess(tmp, 0) == -1) { if(errno == ENOENT) @@ -583,16 +589,9 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP 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); - } + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + gpg_thread.~thread(); DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath"); string::size_type p1 = out.find("(GnuPG) "); if(p1 != string::npos) @@ -651,9 +650,9 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP static BOOL CALLBACK DlgProcNewKeyDialog(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { extern HANDLE new_key_hcnt; - extern HANDLE new_key_hcnt_mutex; + extern boost::mutex new_key_hcnt_mutex; HANDLE hContact = new_key_hcnt; - ReleaseMutex(new_key_hcnt_mutex); + new_key_hcnt_mutex.unlock(); void ImportKey(); TCHAR *tmp = NULL; switch (msg) @@ -817,12 +816,12 @@ static BOOL CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wParam, tmp = new TCHAR [5]; GetDlgItemText(hwndDlg, IDC_KEY_TYPE, tmp, 5); tmp2 = mir_t2a(tmp); + delete [] tmp; char *subkeytype = new char [6]; if(strstr(tmp2, "RSA")) strcpy(subkeytype, "RSA"); else if(strstr(tmp2, "DSA")) //this is useless check for now, but it will be required if someone add another key types support strcpy(subkeytype, "ELG-E"); - delete [] tmp; f<timed_join(boost::posix_time::seconds(10))) { - TerminateThread(gpg_thread, 0); + delete gpg_thread; MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + return CallService(MS_PROTO_CHAINRECV, w, l); } if(result == pxNotFound) { @@ -224,8 +224,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) DBWriteContactSettingString(hContact, szGPGModuleName, "InKeyID", out.substr(s, s2-s).c_str()); } void ShowLoadKeyPasswordWindow(); - WaitForSingleObject(new_key_hcnt_mutex, INFINITE); - new_key_hcnt_mutex = CreateMutex(NULL, FALSE, NULL); + new_key_hcnt_mutex.lock(); new_key_hcnt = hContact; ShowLoadKeyPasswordWindow(); wstring cmd2 = cmd; @@ -245,11 +244,12 @@ int RecvMsgSvc(WPARAM w, LPARAM l) 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) + gpg_thread = gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) { - TerminateThread(gpg_thread, 0); + delete gpg_thread; MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + return CallService(MS_PROTO_CHAINRECV, w, l); } if(result == pxNotFound) { @@ -259,11 +259,12 @@ int RecvMsgSvc(WPARAM w, LPARAM l) } } out.clear(); - gpg_thread = mir_forkthread(pxEexcute_thread, (void*)¶ms); - if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT) + gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) { - TerminateThread(gpg_thread, 0); + delete gpg_thread; MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + return CallService(MS_PROTO_CHAINRECV, w, l); } if(result == pxNotFound) { @@ -423,11 +424,12 @@ int SendMsgSvc(WPARAM w, LPARAM l) 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) + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) { - TerminateThread(gpg_thread, 0); + gpg_thread.~thread(); MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + return CallService(MS_PROTO_CHAINSEND, w, l); } if(result == pxNotFound) { @@ -448,11 +450,12 @@ int SendMsgSvc(WPARAM w, LPARAM l) 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) + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) { - TerminateThread(gpg_thread, 0); + gpg_thread.~thread(); MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + return CallService(MS_PROTO_CHAINSEND, w, l); } if(result == pxNotFound) { @@ -536,7 +539,7 @@ int TestHook(WPARAM w, LPARAM l) static BOOL CALLBACK DlgProcKeyPassword(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { char *inkeyid = UniGetContactSettingUtf(new_key_hcnt, szGPGModuleName, "InKeyID", ""); - ReleaseMutex(new_key_hcnt_mutex); + new_key_hcnt_mutex.unlock(); TCHAR *tmp = NULL; switch (msg) diff --git a/new_gpg.vcproj b/new_gpg.vcproj index 35370df..b6731ba 100644 --- a/new_gpg.vcproj +++ b/new_gpg.vcproj @@ -151,7 +151,7 @@