- Timestamp:
- 11/10/09 18:25:49 (4 years ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- b4ab033
- Parents:
- 6672c56
- Location:
- service
- Files:
-
- 2 edited
-
NSClient++.cpp (modified) (11 diffs)
-
NSClient++.h (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
service/NSClient++.cpp
r6672c56 r773ad32 81 81 } 82 82 83 static bool start( DWORDdwSessionId) {83 static bool start(unsigned long dwSessionId) { 84 84 std::wstring program = mainClient.getBasePath() + _T("\\") + 85 85 SETTINGS_GET_STRING_CORE(settings_def::SYSTRAY_EXE); … … 88 88 } 89 89 90 static bool startTrayHelper(DWORD dwSessionId, std::wstring exe, std::wstring cmdline, bool startThread = true) { 91 HANDLE hToken = NULL; 92 if (!remote_processes::GetSessionUserToken(dwSessionId, &hToken)) { 93 LOG_ERROR_STD(_T("Failed to query user token: ") + error::lookup::last_error()); 94 return false; 95 } else { 96 STARTUPINFO StartUPInfo; 97 PROCESS_INFORMATION ProcessInfo; 98 99 ZeroMemory(&StartUPInfo,sizeof(STARTUPINFO)); 100 ZeroMemory(&ProcessInfo,sizeof(PROCESS_INFORMATION)); 101 StartUPInfo.wShowWindow = SW_HIDE; 102 StartUPInfo.lpDesktop = L"Winsta0\\Default"; 103 StartUPInfo.cb = sizeof(STARTUPINFO); 104 105 wchar_t *buffer = new wchar_t[cmdline.size()+10]; 106 wcscpy(buffer, cmdline.c_str()); 107 LOG_MESSAGE_STD(_T("Running: ") + exe); 108 LOG_MESSAGE_STD(_T("Running: ") + cmdline); 109 110 LPVOID pEnv =NULL; 111 DWORD dwCreationFlags = CREATE_NO_WINDOW; //0; //DETACHED_PROCESS 112 113 if(CreateEnvironmentBlock(&pEnv,hToken,TRUE)) { 114 dwCreationFlags|=CREATE_UNICODE_ENVIRONMENT; 115 } else { 116 LOG_ERROR_STD(_T("Failed to create enviornment: ") + error::lookup::last_error()); 117 pEnv=NULL; 118 } 119 /* 120 LOG_ERROR_STD(_T("Impersonating user: ")); 121 if (!ImpersonateLoggedOnUser(hToken)) { 122 LOG_ERROR_STD(_T("Failed to impersonate the user: ") + error::lookup::last_error()); 123 } 124 125 wchar_t pszUname[UNLEN + 1]; 126 ZeroMemory(pszUname,sizeof(pszUname)); 127 DWORD dwSize = UNLEN; 128 if (!GetUserName(pszUname,&dwSize)) { 129 DWORD dwErr = GetLastError(); 130 if (!RevertToSelf()) 131 LOG_ERROR_STD(_T("Failed to revert to self: ") + error::lookup::last_error()); 132 LOG_ERROR_STD(_T("Failed to get username: ") + error::format::from_system(dwErr)); 133 return false; 134 } 135 136 137 PROFILEINFO info; 138 info.dwSize = sizeof(PROFILEINFO); 139 info.lpUserName = pszUname; 140 if (!LoadUserProfile(hToken, &info)) { 141 DWORD dwErr = GetLastError(); 142 if (!RevertToSelf()) 143 LOG_ERROR_STD(_T("Failed to revert to self: ") + error::lookup::last_error()); 144 LOG_ERROR_STD(_T("Failed to get username: ") + error::format::from_system(dwErr)); 145 return false; 146 } 147 */ 148 if (!CreateProcessAsUser(hToken, exe.c_str(), buffer, NULL, NULL, FALSE, dwCreationFlags, pEnv, NULL, &StartUPInfo, &ProcessInfo)) { 149 DWORD dwErr = GetLastError(); 150 delete [] buffer; 151 /* 152 if (!RevertToSelf()) { 153 LOG_ERROR_STD(_T("Failed to revert to self: ") + error::lookup::last_error()); 154 } 155 */ 156 if (startThread && dwErr == ERROR_PIPE_NOT_CONNECTED) { 157 LOG_MESSAGE(_T("Failed to start trayhelper: starting a background thread to do it instead...")); 158 Thread<tray_starter> *pThread = new Thread<tray_starter>(_T("tray-starter-thread")); 159 pThread->createThread(tray_starter::init(dwSessionId, exe, cmdline)); 160 return false; 161 } else if (dwErr == ERROR_PIPE_NOT_CONNECTED) { 162 LOG_ERROR_STD(_T("Thread failed to start trayhelper (will try again): ") + error::format::from_system(dwErr)); 163 return false; 164 } else { 165 LOG_ERROR_STD(_T("Failed to start trayhelper: ") + error::format::from_system(dwErr)); 166 return true; 167 } 168 } else { 169 delete [] buffer; 170 /* 171 if (!RevertToSelf()) { 172 LOG_ERROR_STD(_T("Failed to revert to self: ") + error::lookup::last_error()); 173 } 174 */ 175 LOG_MESSAGE_STD(_T("Started tray in other user session: ") + strEx::itos(dwSessionId)); 176 } 177 178 179 CloseHandle(hToken); 180 return true; 181 } 90 static bool startTrayHelper(unsigned long dwSessionId, std::wstring exe, std::wstring cmdline, bool startThread = true) { 91 // HANDLE hToken = NULL; 92 // if (!remote_processes::GetSessionUserToken(dwSessionId, &hToken)) { 93 // LOG_ERROR_STD(_T("Failed to query user token: ") + error::lookup::last_error()); 94 // return false; 95 // } else { 96 // STARTUPINFO StartUPInfo; 97 // PROCESS_INFORMATION ProcessInfo; 98 // 99 // ZeroMemory(&StartUPInfo,sizeof(STARTUPINFO)); 100 // ZeroMemory(&ProcessInfo,sizeof(PROCESS_INFORMATION)); 101 // StartUPInfo.wShowWindow = SW_HIDE; 102 // StartUPInfo.lpDesktop = L"Winsta0\\Default"; 103 // StartUPInfo.cb = sizeof(STARTUPINFO); 104 // 105 // wchar_t *buffer = new wchar_t[cmdline.size()+10]; 106 // wcscpy(buffer, cmdline.c_str()); 107 // LOG_MESSAGE_STD(_T("Running: ") + exe); 108 // LOG_MESSAGE_STD(_T("Running: ") + cmdline); 109 // 110 // LPVOID pEnv =NULL; 111 // DWORD dwCreationFlags = CREATE_NO_WINDOW; //0; //DETACHED_PROCESS 112 // 113 // if(CreateEnvironmentBlock(&pEnv,hToken,TRUE)) { 114 // dwCreationFlags|=CREATE_UNICODE_ENVIRONMENT; 115 // } else { 116 // LOG_ERROR_STD(_T("Failed to create enviornment: ") + error::lookup::last_error()); 117 // pEnv=NULL; 118 // } 119 // /* 120 // LOG_ERROR_STD(_T("Impersonating user: ")); 121 // if (!ImpersonateLoggedOnUser(hToken)) { 122 // LOG_ERROR_STD(_T("Failed to impersonate the user: ") + error::lookup::last_error()); 123 // } 124 // 125 // wchar_t pszUname[UNLEN + 1]; 126 // ZeroMemory(pszUname,sizeof(pszUname)); 127 // DWORD dwSize = UNLEN; 128 // if (!GetUserName(pszUname,&dwSize)) { 129 // DWORD dwErr = GetLastError(); 130 // if (!RevertToSelf()) 131 // LOG_ERROR_STD(_T("Failed to revert to self: ") + error::lookup::last_error()); 132 // LOG_ERROR_STD(_T("Failed to get username: ") + error::format::from_system(dwErr)); 133 // return false; 134 // } 135 // 136 // 137 // PROFILEINFO info; 138 // info.dwSize = sizeof(PROFILEINFO); 139 // info.lpUserName = pszUname; 140 // if (!LoadUserProfile(hToken, &info)) { 141 // DWORD dwErr = GetLastError(); 142 // if (!RevertToSelf()) 143 // LOG_ERROR_STD(_T("Failed to revert to self: ") + error::lookup::last_error()); 144 // LOG_ERROR_STD(_T("Failed to get username: ") + error::format::from_system(dwErr)); 145 // return false; 146 // } 147 // */ 148 // if (!CreateProcessAsUser(hToken, exe.c_str(), buffer, NULL, NULL, FALSE, dwCreationFlags, pEnv, NULL, &StartUPInfo, &ProcessInfo)) { 149 // DWORD dwErr = GetLastError(); 150 // delete [] buffer; 151 // /* 152 // if (!RevertToSelf()) { 153 // LOG_ERROR_STD(_T("Failed to revert to self: ") + error::lookup::last_error()); 154 // } 155 // */ 156 // if (startThread && dwErr == ERROR_PIPE_NOT_CONNECTED) { 157 // LOG_MESSAGE(_T("Failed to start trayhelper: starting a background thread to do it instead...")); 158 // Thread<tray_starter> *pThread = new Thread<tray_starter>(_T("tray-starter-thread")); 159 // pThread->createThread(tray_starter::init(dwSessionId, exe, cmdline)); 160 // return false; 161 // } else if (dwErr == ERROR_PIPE_NOT_CONNECTED) { 162 // LOG_ERROR_STD(_T("Thread failed to start trayhelper (will try again): ") + error::format::from_system(dwErr)); 163 // return false; 164 // } else { 165 // LOG_ERROR_STD(_T("Failed to start trayhelper: ") + error::format::from_system(dwErr)); 166 // return true; 167 // } 168 // } else { 169 // delete [] buffer; 170 // /* 171 // if (!RevertToSelf()) { 172 // LOG_ERROR_STD(_T("Failed to revert to self: ") + error::lookup::last_error()); 173 // } 174 // */ 175 // LOG_MESSAGE_STD(_T("Started tray in other user session: ") + strEx::itos(dwSessionId)); 176 // } 177 // 178 // 179 // CloseHandle(hToken); 180 // return true; 181 // } 182 return false; 182 183 } 183 184 }; … … 746 747 if (boot) { 747 748 LOG_MESSAGE_STD(_T("Starting shared session...")); 748 try {749 shared_server_.reset(new nsclient_session::shared_server_session(this));750 if (!shared_server_->session_exists()) {751 shared_server_->create_new_session();752 } else {753 LOG_ERROR_STD(_T("Session already exists cant create a new one!"));754 }755 startTrayIcons();756 } catch (nsclient_session::session_exception e) {757 LOG_ERROR_STD(_T("Failed to create new session: ") + e.what());758 shared_server_.reset(NULL);759 } catch (...) {760 LOG_ERROR_STD(_T("Failed to create new session: Unknown exception"));761 shared_server_.reset(NULL);762 }749 // try { 750 // shared_server_.reset(new nsclient_session::shared_server_session(this)); 751 // if (!shared_server_->session_exists()) { 752 // shared_server_->create_new_session(); 753 // } else { 754 // LOG_ERROR_STD(_T("Session already exists cant create a new one!")); 755 // } 756 // startTrayIcons(); 757 // } catch (nsclient_session::session_exception e) { 758 // LOG_ERROR_STD(_T("Failed to create new session: ") + e.what()); 759 // shared_server_.reset(NULL); 760 // } catch (...) { 761 // LOG_ERROR_STD(_T("Failed to create new session: Unknown exception")); 762 // shared_server_.reset(NULL); 763 // } 763 764 } else { 764 765 LOG_MESSAGE_STD(_T("Attaching to shared session...")); 765 try {766 std::wstring id = _T("_attached_") + strEx::itos(GetCurrentProcessId()) + _T("_");767 shared_client_.reset(new nsclient_session::shared_client_session(id, this));768 if (shared_client_->session_exists()) {769 shared_client_->attach_to_session(id);770 } else {771 LOG_ERROR_STD(_T("No session was found cant attach!"));772 }773 LOG_ERROR_STD(_T("Session is: ") + shared_client_->get_client_id());774 } catch (nsclient_session::session_exception e) {775 LOG_ERROR_STD(_T("Failed to attach to session: ") + e.what());776 shared_client_.reset(NULL);777 } catch (...) {778 LOG_ERROR_STD(_T("Failed to attach to session: Unknown exception"));779 shared_client_.reset(NULL);780 }766 // try { 767 // std::wstring id = _T("_attached_") + strEx::itos(GetCurrentProcessId()) + _T("_"); 768 // shared_client_.reset(new nsclient_session::shared_client_session(id, this)); 769 // if (shared_client_->session_exists()) { 770 // shared_client_->attach_to_session(id); 771 // } else { 772 // LOG_ERROR_STD(_T("No session was found cant attach!")); 773 // } 774 // LOG_ERROR_STD(_T("Session is: ") + shared_client_->get_client_id()); 775 // } catch (nsclient_session::session_exception e) { 776 // LOG_ERROR_STD(_T("Failed to attach to session: ") + e.what()); 777 // shared_client_.reset(NULL); 778 // } catch (...) { 779 // LOG_ERROR_STD(_T("Failed to attach to session: Unknown exception")); 780 // shared_client_.reset(NULL); 781 // } 781 782 } 782 783 } … … 863 864 864 865 void NSClientT::startTrayIcons() { 865 if (shared_server_.get() == NULL) {866 LOG_MESSAGE_STD(_T("No master session so tray icons not started"));867 return;868 }869 remote_processes::PWTS_SESSION_INFO list;870 DWORD count;871 if (!remote_processes::_WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE , 0, 1, &list, &count)) {872 LOG_ERROR_STD(_T("Failed to enumerate sessions:" ) + error::lookup::last_error());873 } else {874 LOG_DEBUG_STD(_T("Found ") + strEx::itos(count) + _T(" sessions"));875 for (DWORD i=0;i<count;i++) {876 LOG_DEBUG_STD(_T("Found session: ") + strEx::itos(list[i].SessionId) + _T(" state: ") + strEx::itos(list[i].State));877 if (list[i].State == remote_processes::_WTS_CONNECTSTATE_CLASS::WTSActive) {878 startTrayIcon(list[i].SessionId);879 }880 }881 }866 // if (shared_server_.get() == NULL) { 867 // LOG_MESSAGE_STD(_T("No master session so tray icons not started")); 868 // return; 869 // } 870 // remote_processes::PWTS_SESSION_INFO list; 871 // DWORD count; 872 // if (!remote_processes::_WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE , 0, 1, &list, &count)) { 873 // LOG_ERROR_STD(_T("Failed to enumerate sessions:" ) + error::lookup::last_error()); 874 // } else { 875 // LOG_DEBUG_STD(_T("Found ") + strEx::itos(count) + _T(" sessions")); 876 // for (DWORD i=0;i<count;i++) { 877 // LOG_DEBUG_STD(_T("Found session: ") + strEx::itos(list[i].SessionId) + _T(" state: ") + strEx::itos(list[i].State)); 878 // if (list[i].State == remote_processes::_WTS_CONNECTSTATE_CLASS::WTSActive) { 879 // startTrayIcon(list[i].SessionId); 880 // } 881 // } 882 // } 882 883 } 883 884 void NSClientT::startTrayIcon(DWORD dwSessionId) { 884 if (shared_server_.get() == NULL) {885 LOG_MESSAGE_STD(_T("No master session so tray icons not started"));886 return;887 }888 if (!shared_server_->re_attach_client(dwSessionId)) {889 if (!tray_starter::start(dwSessionId)) {890 LOG_ERROR_STD(_T("Failed to start session (") + strEx::itos(dwSessionId) + _T("): " ) + error::lookup::last_error());891 }892 }885 // if (shared_server_.get() == NULL) { 886 // LOG_MESSAGE_STD(_T("No master session so tray icons not started")); 887 // return; 888 // } 889 // if (!shared_server_->re_attach_client(dwSessionId)) { 890 // if (!tray_starter::start(dwSessionId)) { 891 // LOG_ERROR_STD(_T("Failed to start session (") + strEx::itos(dwSessionId) + _T("): " ) + error::lookup::last_error()); 892 // } 893 // } 893 894 } 894 895 … … 926 927 LOG_DEBUG_STD(_T("Stopping: Settings instance")); 927 928 settings_manager::destroy_settings(); 929 // try { 930 // if (shared_client_.get() != NULL) { 931 // LOG_DEBUG_STD(_T("Stopping: shared client")); 932 // shared_client_->set_handler(NULL); 933 // shared_client_->close_session(); 934 // } 935 // } catch(nsclient_session::session_exception &e) { 936 // LOG_ERROR_STD(_T("Exception closing shared client session: ") + e.what()); 937 // } catch(...) { 938 // LOG_ERROR_STD(_T("Exception closing shared client session: Unknown exception!")); 939 // } 928 940 try { 929 if (shared_client_.get() != NULL) { 930 LOG_DEBUG_STD(_T("Stopping: shared client")); 931 shared_client_->set_handler(NULL); 932 shared_client_->close_session(); 933 } 934 } catch(nsclient_session::session_exception &e) { 935 LOG_ERROR_STD(_T("Exception closing shared client session: ") + e.what()); 936 } catch(...) { 937 LOG_ERROR_STD(_T("Exception closing shared client session: Unknown exception!")); 938 } 939 try { 940 if (shared_server_.get() != NULL) { 941 LOG_DEBUG_STD(_T("Stopping: shared server")); 942 shared_server_->set_handler(NULL); 943 shared_server_->close_session(); 944 } 941 // if (shared_server_.get() != NULL) { 942 // LOG_DEBUG_STD(_T("Stopping: shared server")); 943 // shared_server_->set_handler(NULL); 944 // shared_server_->close_session(); 945 // } 945 946 } catch(...) { 946 947 LOG_ERROR_STD(_T("UNknown exception raised: When closing shared session")); … … 988 989 * @param dwCtrlCode 989 990 */ 990 void WINAPI NSClientT::service_ctrl_dispatch( DWORDdwCtrlCode) {991 void WINAPI NSClientT::service_ctrl_dispatch(unsigned long dwCtrlCode) { 991 992 mainClient.service_ctrl_ex(dwCtrlCode, NULL, NULL, NULL); 992 993 } 993 994 994 995 995 void NSClientT::service_on_session_changed( DWORD dwSessionId, bool logon, DWORDdwEventType) {996 if (shared_server_.get() == NULL) {997 LOG_DEBUG_STD(_T("No shared session: ignoring change event!"));998 return;999 }996 void NSClientT::service_on_session_changed(unsigned long dwSessionId, bool logon, unsigned long dwEventType) { 997 // if (shared_server_.get() == NULL) { 998 // LOG_DEBUG_STD(_T("No shared session: ignoring change event!")); 999 // return; 1000 // } 1000 1001 LOG_DEBUG_STD(_T("Got session change: ") + strEx::itos(dwSessionId)); 1001 1002 if (!logon) { … … 1250 1251 1251 1252 NSCAPI::nagiosReturn NSClientT::inject(std::wstring command, std::wstring arguments, TCHAR splitter, bool escape, std::wstring &msg, std::wstring & perf) { 1252 if (shared_client_.get() != NULL && shared_client_->hasMaster()) {1253 /*if (shared_client_.get() != NULL && shared_client_->hasMaster()) { 1253 1254 try { 1254 1255 return shared_client_->inject(command, arguments, splitter, escape, msg, perf); … … 1260 1261 return NSCAPI::returnCRIT; 1261 1262 } 1262 } else {1263 } else */{ 1263 1264 unsigned int aLen = 0; 1264 1265 TCHAR ** aBuf = arrayBuffer::split2arrayBuffer(arguments, splitter, aLen, escape); … … 1297 1298 LOG_DEBUG_STD(_T("Injecting: ") + (std::wstring) command + _T(": ") + arrayBuffer::arrayBuffer2string(argument, argLen, _T(", "))); 1298 1299 } 1299 if (shared_client_.get() != NULL && shared_client_->hasMaster()) {1300 /*if (shared_client_.get() != NULL && shared_client_->hasMaster()) { 1300 1301 try { 1301 1302 std::wstring msg, perf; … … 1312 1313 return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, _T(""), returnCode); 1313 1314 } 1314 } else {1315 } else */{ 1315 1316 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 1316 1317 if (!readLock.owns_lock()) { … … 1406 1407 return; 1407 1408 } 1408 if (shared_server_.get() != NULL && shared_server_->hasClients()) {1409 try {1410 shared_server_->sendLogMessageToClients(msgType, file, line, message);1411 } catch (nsclient_session::session_exception e) {1412 log_broken_message(_T("Failed to send message to clients: ") + e.what());1413 }1414 }1409 // if (shared_server_.get() != NULL && shared_server_->hasClients()) { 1410 // try { 1411 // shared_server_->sendLogMessageToClients(msgType, file, line, message); 1412 // } catch (nsclient_session::session_exception e) { 1413 // log_broken_message(_T("Failed to send message to clients: ") + e.what()); 1414 // } 1415 // } 1415 1416 std::wstring file_stl = file; 1416 1417 std::wstring::size_type pos = file_stl.find_last_of(_T("\\")); -
service/NSClient++.h
r6672c56 r773ad32 29 29 #include <map> 30 30 #include <com_helpers.hpp> 31 #include <nsclient_session.hpp>31 //#include <nsclient_session.hpp> 32 32 #include <boost/thread/thread.hpp> 33 33 #include <boost/thread/locks.hpp> … … 60 60 * 61 61 */ 62 class NSClientT : public nsclient_session::session_handler_interface{62 class NSClientT /*: public nsclient_session::session_handler_interface*/ { 63 63 64 64 public: … … 109 109 log_status debug_; 110 110 com_helper::initialize_com com_helper_; 111 /* 111 112 std::auto_ptr<nsclient_session::shared_client_session> shared_client_; 112 113 std::auto_ptr<nsclient_session::shared_server_session> shared_server_; 114 */ 113 115 log_cache_type log_cache_; 114 116 bool plugins_loaded_;
Note: See TracChangeset
for help on using the changeset viewer.








