bool CBOINCClientManager::AutoRestart() { double timeNow, timeDiff; if (IsBOINCCoreRunning()) return true; #if ! (defined(__WXMAC__) || defined(__WXMSW__)) // Mac and Windows can restart Client as a daemon, but // Linux may not know Client's location if it didn't start the Client if (!m_bBOINCStartedByManager) return false; #endif // Alert user if Client crashes 3 times in CLIENT_3_CRASH_MAX_TIME timeNow = dtime(); timeDiff = timeNow - m_fAutoRestart1Time; if ((timeDiff) < (CLIENT_3_CRASH_MAX_TIME * 60)) { int response; ClientCrashDlg *dlg = new ClientCrashDlg(timeDiff); if (dlg) { CBOINCBaseFrame* pFrame = wxGetApp().GetFrame(); if (!pFrame->IsShown()) { pFrame->Show(); } response = dlg->ShowModal(); dlg->Destroy(); if (response == wxID_CANCEL) return false; timeNow = 0; m_fAutoRestart1Time = 0; m_fAutoRestart2Time = 0; } } m_lBOINCCoreProcessId = 0; m_fAutoRestart1Time = m_fAutoRestart2Time; m_fAutoRestart2Time = timeNow; StartupBOINCCore(); return true; }
bool CBOINCClientManager::AutoRestart() { if (IsBOINCCoreRunning()) return true; #ifndef __WXMAC__ // Mac can restart Client as a daemon if (! m_bBOINCStartedByManager) return false; #endif m_lBOINCCoreProcessId = 0; StartupBOINCCore(); return true; }
bool CBOINCClientManager::StartupBOINCCore() { wxLogTrace(wxT("Function Start/End"), wxT("CMainDocument::StartupBOINCCore - Function Begin")); bool bReturnValue = false; wxString strExecute = wxEmptyString; if (IsBOINCCoreRunning()) return true; #if defined(__WXMSW__) LPTSTR szExecute = NULL; LPTSTR szDataDirectory = NULL; if (IsBOINCConfiguredAsDaemon()) { return (!!StartBOINCService()); } // Append synecd.exe to the end of the strExecute string strExecute.Printf( wxT("\"%ssynecd.exe\" --redirectio --launched_by_manager %s"), wxGetApp().GetRootDirectory().c_str(), wxGetApp().GetArguments().c_str() ); PROCESS_INFORMATION pi; STARTUPINFO si; BOOL bProcessStarted; memset(&pi, 0, sizeof(pi)); memset(&si, 0, sizeof(si)); si.cb = sizeof(si); si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE; szExecute = (LPTSTR)strExecute.c_str(); if (wxGetApp().GetDataDirectory().empty()) { szDataDirectory = NULL; } else { szDataDirectory = (LPTSTR)wxGetApp().GetDataDirectory().c_str(); } wxLogTrace(wxT("Function Status"), wxT("CMainDocument::StartupBOINCCore - szExecute '%s'\n"), szExecute); wxLogTrace(wxT("Function Status"), wxT("CMainDocument::StartupBOINCCore - szDataDirectory '%s'\n"), szDataDirectory); bProcessStarted = CreateProcess( NULL, szExecute, NULL, NULL, FALSE, CREATE_NEW_PROCESS_GROUP|CREATE_NO_WINDOW, NULL, szDataDirectory, &si, &pi ); if (bProcessStarted) { m_lBOINCCoreProcessId = pi.dwProcessId; m_hBOINCCoreProcess = pi.hProcess; } #elif defined(__WXMAC__) char buf[1024]; char *argv[5]; ProcessSerialNumber ourPSN; FSRef ourFSRef; OSErr err; if (IsBOINCConfiguredAsDaemon() == NewStyleDaemon) { system ("launchctl load /Library/LaunchDaemons/com.googlecode.synecdoche.plist"); system ("launchctl start com.googleccode.synecdoche"); bReturnValue = IsBOINCCoreRunning(); } else { // Get the full path to core client inside this application's bundle err = GetCurrentProcess (&ourPSN); if (err == noErr) { err = GetProcessBundleLocation(&ourPSN, &ourFSRef); } if (err == noErr) { err = FSRefMakePath (&ourFSRef, (UInt8*)buf, sizeof(buf)); } if (err == noErr) { #if 0 // The Mac version of wxExecute(wxString& ...) crashes if there is a space in the path strExecute = wxT("\""); strExecute += wxT(buf); strExecute += wxT("/Contents/Resources/synecd\" --redirectio --launched_by_manager"); m_lBOINCCoreProcessId = ::wxExecute(strExecute); #else // Use wxExecute(wxChar **argv ...) instead of wxExecute(wxString& ...) strcat(buf, "/Contents/Resources/synecd"); argv[0] = buf; argv[1] = "--redirectio"; argv[2] = "--launched_by_manager"; argv[3] = NULL; #ifdef SANDBOX if (!g_use_sandbox) { argv[3] = "--insecure"; argv[4] = NULL; } #endif // Under wxMac-2.8.0, wxExecute starts a separate thread // to wait for child's termination. // That wxProcessTerminationThread uses a huge amount of processor // time (about 11% of one CPU on 2GHz Intel Dual-Core Mac). // m_lBOINCCoreProcessId = ::wxExecute(argv); run_program( "/Library/Application Support/Synecdoche Data", buf, argv[3] ? 4 : 3, argv, 0.0, m_lBOINCCoreProcessId ); #endif } else { buf[0] = '\0'; } } #else // Unix based systems // Append boinc.exe to the end of the strExecute string and get ready to rock strExecute = ::wxGetCwd() + wxT("/boinc --redirectio --launched_by_manager"); #ifdef SANDBOX if (!g_use_sandbox) { strExecute += wxT(" --insecure"); } #endif // SANDBOX wxLogTrace(wxT("Function Status"), wxT("CMainDocument::StartupBOINCCore - szExecute '%s'\n"), strExecute.c_str()); wxLogTrace(wxT("Function Status"), wxT("CMainDocument::StartupBOINCCore - szDataDirectory '%s'\n"), ::wxGetCwd().c_str()); m_lBOINCCoreProcessId = ::wxExecute(strExecute); #endif if (0 != m_lBOINCCoreProcessId) { m_bBOINCStartedByManager = true; bReturnValue = true; // Allow time for daemon to start up so we don't keep relaunching it for (int i = 0; i < 100; ++i) { // Wait up to 4 seconds in 40ms increments if (IsBOINCCoreRunning()) { break; } boinc_sleep(0.04); } } wxLogTrace(wxT("Function Start/End"), wxT("CMainDocument::StartupBOINCCore - Function End")); return bReturnValue; }
void CBOINCClientManager::ShutdownBOINCCore(bool ShuttingDownManager) { wxLogTrace(wxT("Function Start/End"), wxT("CBOINCClientManager::ShutdownBOINCCore - Function Begin")); CMainDocument* pDoc = wxGetApp().GetDocument(); wxInt32 iCount = 0; bool bClientQuit = false; wxString strConnectedCompter = wxEmptyString; wxString strPassword = wxEmptyString; double startTime = 0; wxDateTime zeroTime = wxDateTime((time_t)0); wxDateTime rpcCompletionTime = zeroTime; ASYNC_RPC_REQUEST request; int quit_result; wxASSERT(pDoc); wxASSERT(wxDynamicCast(pDoc, CMainDocument)); #ifdef __WXMAC__ // Mac Manager shuts down client only if Manager started client if (!m_bBOINCStartedByManager) return; #endif #ifdef __WXMSW__ if (IsBOINCConfiguredAsDaemon()) { stop_daemon_via_daemonctrl(); bClientQuit = true; } else #endif { pDoc->GetConnectedComputerName(strConnectedCompter); if (!pDoc->IsComputerNameLocal(strConnectedCompter)) { RPC_CLIENT rpc; if (!rpc.init("localhost")) { pDoc->m_pNetworkConnection->GetLocalPassword(strPassword); rpc.authorize((const char*)strPassword.mb_str()); if (IsBOINCCoreRunning()) { rpc.quit(); for (iCount = 0; iCount <= 10; iCount++) { if (!bClientQuit && !IsBOINCCoreRunning()) { wxLogTrace(wxT("Function Status"), wxT("CBOINCClientManager::ShutdownBOINCCore - (localhost) Application Exit Detected")); bClientQuit = true; break; } wxLogTrace(wxT("Function Status"), wxT("CBOINCClientManager::ShutdownBOINCCore - (localhost) Application Exit NOT Detected, Sleeping...")); ::wxSleep(1); } } else { bClientQuit = true; } } rpc.close(); } else { if (IsBOINCCoreRunning()) { if (ShuttingDownManager) { // Set event filtering to allow RPC completion // events but not events which start new RPCs wxGetApp().SetEventFiltering(true); } quit_result = -1; request.clear(); request.which_rpc = RPC_QUIT; request.rpcType = RPC_TYPE_ASYNC_NO_REFRESH; request.completionTime = &rpcCompletionTime; request.resultPtr = &quit_result; pDoc->RequestRPC(request); // Issue an asynchronous Quit RPC // Client needs time to shut down project applications, so don't wait // for it to shut down; assume it will exit OK if Quit RPC succeeds. startTime = dtime(); while ((dtime() - startTime) < 10.0) { // Allow 10 seconds boinc_sleep(0.25); // Check 4 times per second wxSafeYield(NULL, true); // To allow handling RPC completion events if (!bClientQuit && (rpcCompletionTime != zeroTime)) { // If Quit RPC finished, check its returned value if (quit_result) { break; // Quit RPC returned an error } wxLogTrace(wxT("Function Status"), wxT("CBOINCClientManager::ShutdownBOINCCore - Application Exit Detected")); bClientQuit = true; break; } wxLogTrace(wxT("Function Status"), wxT("CBOINCClientManager::ShutdownBOINCCore - Application Exit NOT Detected, Sleeping...")); } } else { bClientQuit = true; } } } if (!bClientQuit) { KillClient(); } m_lBOINCCoreProcessId = 0; wxLogTrace(wxT("Function Start/End"), wxT("CBOINCClientManager::ShutdownBOINCCore - Function End")); }