1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
#ifdef _WIN32
#include <stdio.h>
#include "ntservice.h"
//#include <strsafe.h>
#include <wtsapi32.h>
/* Handle to SCM for updating service status */
static SERVICE_STATUS_HANDLE hServiceStatus = 0;
static BOOL foreground = FALSE;
static char ConsoleTitle[128];
static int glb_argc;
static char **glb_argv;
HANDLE hServDoneEvent = NULL;
HANDLE WaitHandles[3];
extern volatile int debug;
char *progname;
int real_main(int argc, char *argv[]);
/*
* Forward declarations
*/
void ServiceControl(DWORD dwCtrlCode);
void ntservice_init(void);
void ntservice_exit(void);
void main_shutdown();
void log_text(char * text);
/*
void svcReportError(LPTSTR szText) {
HANDLE hEventSource;
LPCTSTR lpszStrings[2];
TCHAR Buffer[80];
hEventSource = RegisterEventSource(NULL, TEXT(NTSERVICE_DISPLAY_NAME));
if (NULL != hEventSource) {
StringCchPrintf(Buffer, 80, TEXT("%s"), szText);
lpszStrings[0] = TEXT(NTSERVICE_DISPLAY_NAME);
lpszStrings[1] = Buffer;
ReportEvent(hEventSource,
EVENTLOG_ERROR_TYPE,
0,
SVC_ERROR,
NULL,
2,
0,
lpszStrings,
NULL);
DeregisterEventSource(hEventSource);
}
}
*/
void launchUserProgram(char * cmd) {
DWORD csid = WTSGetActiveConsoleSessionId();
HANDLE handle, handle2;
if (WTSQueryUserToken(csid, &handle)) {
if (!DuplicateTokenEx(handle, TOKEN_ALL_ACCESS, NULL, SecurityDelegation, TokenPrimary, &handle2)) {
CloseHandle(handle);
log_text("#1");
return;
}
CloseHandle(handle);
log_text("#2");
STARTUPINFO si;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
si.lpDesktop = TEXT("winsta0\\default");
PROCESS_INFORMATION pi;
memset(&pi, 0, sizeof(pi));
if (!CreateProcessAsUser(handle2, NULL, cmd, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
log_text("#3");
}
CloseHandle(handle2);
}
}
void WINAPI service_main( DWORD argc, LPTSTR *argv )
{
ntservice_init();
/* pass the global command line options on to the service */
real_main( glb_argc, glb_argv );
ntservice_exit();
}
/*
* This is the entry point for the executable
* We can call ntpdmain() explicitly or via StartServiceCtrlDispatcher()
* as we need to.
*/
int main( int argc, char *argv[] )
{
int rc;
int i = 1;
/* Save the command line parameters */
glb_argc = argc;
glb_argv = argv;
/* Command line users should put -f in the options */
while (argv[i]) {
if (!_strnicmp(argv[i], "-c", 2) ||
!strcmp(argv[i], "-q") ||
!strcmp(argv[i], "--help") ||
!strcmp(argv[i], "-n")) {
foreground = TRUE;
break;
}
i++;
}
if (foreground) {
/* run in console window */
exit(real_main(argc, argv));
} else {
/* Start up as service */
SERVICE_TABLE_ENTRY dispatchTable[] = {
{ TEXT(NTSERVICE_DISPLAY_NAME), (LPSERVICE_MAIN_FUNCTION) service_main },
{ NULL, NULL }
};
rc = StartServiceCtrlDispatcher(dispatchTable);
if (!rc) {
char buffer[200];
progname = argv[0];
rc = GetLastError();
sprintf(buffer, "%s: unable to start as service, rc: %i", progname, rc);
MessageBox( NULL,
(LPCTSTR)buffer,
(LPCTSTR)"ERROR",
MB_ICONINFORMATION | MB_OK
);
//#ifdef DEBUG
fprintf(stderr, "%s: unable to start as service, rc: %i\n\n", progname, rc);
//#endif
fprintf(stderr, "\nUse -d, -q, --help or -n to run from the command line.\n");
exit(rc);
}
}
exit(0);
}
/*
* Initialize the Service by registering it.
*/
void
ntservice_init() {
if (!foreground) {
/* Register handler with the SCM */
hServiceStatus = RegisterServiceCtrlHandler(NTSERVICE_DISPLAY_NAME,
(LPHANDLER_FUNCTION)ServiceControl);
if (!hServiceStatus) {
// NTReportError(NTSERVICE_NAME,
// "could not register service control handler");
UpdateSCM(SERVICE_STOPPED);
exit(1);
}
UpdateSCM(SERVICE_RUNNING);
} else {
strcpy(ConsoleTitle, "NTP Version ");
strcat(ConsoleTitle, "1.0");
SetConsoleTitle(ConsoleTitle);
}
//atexit( ntservice_exit );
}
/*
* Routine to check if this is a service or a foreground program
*/
BOOL
ntservice_isservice() {
return(!foreground);
}
/* service_ctrl - control handler for NTP service
* signals the service_main routine of start/stop requests
* from the control panel or other applications making
* win32API calls
*/
void
ntservice_exit( void )
{
if (!foreground) { /* did not become a service, simply exit */
/* service mode, need to have the service_main routine
* register with the service control manager that the
* service has stopped running, before exiting
*/
UpdateSCM(SERVICE_STOPPED);
}
// uninit_io_completion_port();
Sleep( 2 ); //##++
# ifdef DEBUG
_CrtDumpMemoryLeaks();
# endif
}
/*
* ServiceControl(): Handles requests from the SCM and passes them on
* to the service.
*/
void
ServiceControl(DWORD dwCtrlCode) {
/* Handle the requested control code */
switch(dwCtrlCode) {
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
UpdateSCM(SERVICE_STOP_PENDING);
main_shutdown();
return;
case SERVICE_CONTROL_PAUSE:
case SERVICE_CONTROL_CONTINUE:
case SERVICE_CONTROL_INTERROGATE:
default:
break;
}
UpdateSCM(SERVICE_RUNNING);
}
/*
* Tell the Service Control Manager the state of the service.
*/
void UpdateSCM(DWORD state) {
SERVICE_STATUS ss;
static DWORD dwState = SERVICE_STOPPED;
if (hServiceStatus) {
if (state)
dwState = state;
memset(&ss, 0, sizeof(SERVICE_STATUS));
ss.dwServiceType |= SERVICE_WIN32_OWN_PROCESS;
ss.dwCurrentState = dwState;
ss.dwControlsAccepted = SERVICE_ACCEPT_STOP |
SERVICE_ACCEPT_SHUTDOWN;
ss.dwCheckPoint = 0;
ss.dwServiceSpecificExitCode = 0;
ss.dwWin32ExitCode = NO_ERROR;
ss.dwWaitHint = dwState == SERVICE_STOP_PENDING ? 5000 : 1000;
if (!SetServiceStatus(hServiceStatus, &ss)) {
ss.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(hServiceStatus, &ss);
}
}
}
BOOL WINAPI
OnConsoleEvent(
DWORD dwCtrlType
)
{
switch (dwCtrlType) {
#ifdef DEBUG
case CTRL_BREAK_EVENT :
if (debug > 0) {
debug <<= 1;
}
else {
debug = 1;
}
if (debug > 8) {
debug = 0;
}
printf("debug level %d\n", debug);
break ;
#endif
case CTRL_C_EVENT :
case CTRL_CLOSE_EVENT :
case CTRL_SHUTDOWN_EVENT :
if (WaitHandles[0] != NULL) {
SetEvent(WaitHandles[0]);
Sleep( 100 ); //##++
}
break;
default :
return FALSE;
}
return TRUE;;
}
#endif /* _WIN32 */
|