| 1 | /************************************************************************** |
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> * |
|---|
| 3 | * * |
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp * |
|---|
| 5 | * * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 7 | * it under the terms of the GNU General Public License as published by * |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 9 | * (at your option) any later version. * |
|---|
| 10 | * * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, * |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 14 | * GNU General Public License for more details. * |
|---|
| 15 | * * |
|---|
| 16 | * You should have received a copy of the GNU General Public License * |
|---|
| 17 | * along with this program; if not, write to the * |
|---|
| 18 | * Free Software Foundation, Inc., * |
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | #include "stdafx.h" |
|---|
| 22 | #include "SysTray.h" |
|---|
| 23 | #include "TrayIcon.h" |
|---|
| 24 | #include <ServiceCmd.h> |
|---|
| 25 | #include <config.h> |
|---|
| 26 | |
|---|
| 27 | SysTray gSysTray; |
|---|
| 28 | |
|---|
| 29 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) |
|---|
| 30 | { |
|---|
| 31 | NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call); |
|---|
| 32 | return TRUE; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | SysTray::SysTray() : icon(_T("SysTray")) {} |
|---|
| 36 | SysTray::~SysTray() {} |
|---|
| 37 | bool SysTray::loadModule() { |
|---|
| 38 | try { |
|---|
| 39 | if ((serviceControll::GetServiceType(SZSERVICENAME)&SERVICE_INTERACTIVE_PROCESS)!=SERVICE_INTERACTIVE_PROCESS) { |
|---|
| 40 | NSC_LOG_ERROR(_T("SysTray is not installed (or it cannot interact with the desktop) SysTray wont be loaded. Run ") SZAPPNAME _T(" SysTray install ti change this.")); |
|---|
| 41 | return true; |
|---|
| 42 | } |
|---|
| 43 | } catch (serviceControll::SCException e) { |
|---|
| 44 | NSC_LOG_ERROR(_T("SysTray is not installed (or it cannot interact with the desktop) SysTray wont be loaded. Run ") SZAPPNAME _T(" SysTray install ti change this.")); |
|---|
| 45 | return true; |
|---|
| 46 | } |
|---|
| 47 | icon.createThread(); |
|---|
| 48 | return true; |
|---|
| 49 | } |
|---|
| 50 | bool SysTray::unloadModule() { |
|---|
| 51 | if (!icon.exitThread(20000)) { |
|---|
| 52 | std::wcout << _T("MAJOR ERROR: Could not unload thread...") << std::endl; |
|---|
| 53 | NSC_LOG_ERROR(_T("Could not exit the thread, memory leak and potential corruption may be the result...")); |
|---|
| 54 | return false; |
|---|
| 55 | } |
|---|
| 56 | return true; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | int SysTray::commandLineExec(const TCHAR* command,const unsigned int argLen,TCHAR** args) { |
|---|
| 60 | if (_wcsicmp(command, _T("install")) == 0) { |
|---|
| 61 | try { |
|---|
| 62 | serviceControll::ModifyServiceType(SZSERVICENAME, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS); |
|---|
| 63 | NSC_LOG_MESSAGE_STD(_T(MODULE_NAME) _T(" is now able to run as the SERVICE_INTERACTIVE_PROCESS flag has been set.")); |
|---|
| 64 | } catch (const serviceControll::SCException& e) { |
|---|
| 65 | NSC_LOG_ERROR_STD(_T("Could not modify service: ") + e.error_); |
|---|
| 66 | return -1; |
|---|
| 67 | } |
|---|
| 68 | } else if (_wcsicmp(command, _T("uninstall")) == 0) { |
|---|
| 69 | try { |
|---|
| 70 | serviceControll::ModifyServiceType(SZSERVICENAME, SERVICE_WIN32_OWN_PROCESS); |
|---|
| 71 | NSC_LOG_MESSAGE_STD(_T(MODULE_NAME) _T(" is now not able to run as the SERVICE_INTERACTIVE_PROCESS flag has been reset.")); |
|---|
| 72 | } catch (const serviceControll::SCException& e) { |
|---|
| 73 | NSC_LOG_ERROR_STD(_T("Could not modify service: ") + e.error_); |
|---|
| 74 | return -1; |
|---|
| 75 | } |
|---|
| 76 | } else { |
|---|
| 77 | NSC_LOG_ERROR_STD(_T("Undefined command, usage: install or uninstall")); |
|---|
| 78 | return -1; |
|---|
| 79 | } |
|---|
| 80 | return 0; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | bool SysTray::hasCommandHandler() { |
|---|
| 84 | return false; |
|---|
| 85 | } |
|---|
| 86 | bool SysTray::hasMessageHandler() { |
|---|
| 87 | return true; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | void SysTray::setLogWindow(HWND hWnd) { |
|---|
| 91 | MutexLock lock(logLock); |
|---|
| 92 | if (lock.hasMutex()) { |
|---|
| 93 | hLogWnd = hWnd; |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | void SysTray::handleMessage(int msgType, TCHAR* file, int line, TCHAR* message) { |
|---|
| 98 | log_entry record(msgType, file, line, message); |
|---|
| 99 | HWND hWnd = NULL; |
|---|
| 100 | { |
|---|
| 101 | MutexLock lock(logLock); |
|---|
| 102 | if (lock.hasMutex()) { |
|---|
| 103 | log.push_back(record); |
|---|
| 104 | if (log.size() > 50) |
|---|
| 105 | log.pop_front(); |
|---|
| 106 | hWnd = hLogWnd; |
|---|
| 107 | } else { |
|---|
| 108 | std::cout << "Fuck!!!" << std::endl; |
|---|
| 109 | NSC_LOG_ERROR_STD(_T("Failed to get mutex in logger, message discarded")); |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | if (hWnd) { |
|---|
| 113 | SendMessage(hWnd, WM_USER+1, reinterpret_cast<WPARAM>(&record), NULL); |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | SysTray::log_type SysTray::getLog() { |
|---|
| 117 | log_type ret; |
|---|
| 118 | for (log_type::const_iterator cit = log.begin(); cit != log.end(); ++cit) |
|---|
| 119 | ret.push_back(*cit); |
|---|
| 120 | return ret; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | NSC_WRAPPERS_MAIN_DEF(gSysTray); |
|---|
| 127 | NSC_WRAPPERS_HANDLE_MSG_DEF(gSysTray); |
|---|
| 128 | NSC_WRAPPERS_IGNORE_CMD_DEF(); |
|---|
| 129 | NSC_WRAPPERS_CLI_DEF(gSysTray); |
|---|
| 130 | |
|---|