int main (int argc, char **argv) { #ifdef WIN32 /* Check if started as Service ? */ int argv0len = strlen (argv[0]); int extlen = (sizeof (WISVC_EXE_EXTENSION_FOR_SERVICE) - 1); /* 4 */ #ifdef MALLOC_DEBUG dbg_malloc_enable(); #endif if ((argv0len > extlen) && !strcmp ((argv[0] + argv0len - extlen), WISVC_EXE_EXTENSION_FOR_SERVICE)) /* if(strstr(argv[0],WISVC_EXE_EXTENSION_FOR_SERVICE)) */ { wisvc_Main_G_argc = argc; wisvc_Main_G_argv = argv; wisvc_start_kubl_service_dispatcher (argc, argv); } else #endif { #ifdef MALLOC_DEBUG dbg_malloc_enable(); #endif return (kubl_main (argc, argv, 0, NULL)); } }
VOID wisvc_KublServiceStart (DWORD argc, LPTSTR * argv) { DWORD status; DWORD specificError = 0; char *service_name = find_service_name_from (argv); char *cutpnt; /* First chdir to the same directory where the executable is (got from argv[0] of original main arguments), so that further error messages printed with wisvc_err_printf (which calls log_error in turn) will be appended to wi.err file in more appropriate place than \WINNT\SYSTEM32 directory. */ if (wisvc_Main_G_argv && wisvc_Main_G_argv[0] && (NULL != (cutpnt = strrchr (wisvc_Main_G_argv[0], '\\')))) { /* Search the last backslash. */ ptrlong len; char *work_dir; len = (cutpnt - wisvc_Main_G_argv[0]); if (NULL == (work_dir = ((char *) malloc (len + 1)))) { exit (7); /* Failed beyond all reason, best to exit. */ } else { strncpy (work_dir, wisvc_Main_G_argv[0], len); /* Leave backslash there */ work_dir[len] = '\0'; } if (chdir (work_dir)) /* Is not zero, i.e. -1, an error. */ { /* However, we do not exit yet. */ /* DWORD erhe = GetLastError(); */ wisvc_err_printf ("%s: Cannot chdir to \"%s\" because: %s", argv[0], work_dir, strerror (errno)); } free (work_dir); } wisvc_KublServiceStatus.dwServiceType = SERVICE_WIN32; wisvc_KublServiceStatus.dwCurrentState = SERVICE_START_PENDING; wisvc_KublServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; /* | SERVICE_ACCEPT_PAUSE_CONTINUE; (Not for wisvc_Kubl) */ wisvc_KublServiceStatus.dwWin32ExitCode = 0; wisvc_KublServiceStatus.dwServiceSpecificExitCode = 0; wisvc_KublServiceStatus.dwCheckPoint = 0; wisvc_KublServiceStatus.dwWaitHint = 2; wisvc_KublServiceStatusHandle = RegisterServiceCtrlHandler (TEXT (service_name), ((LPHANDLER_FUNCTION) wisvc_KublServiceCtrlHandler)); if (wisvc_KublServiceStatusHandle == (SERVICE_STATUS_HANDLE) 0) { wisvc_err_printf ("%s: (%s) RegisterServiceCtrlHandler failed %ld\n", wisvc_Main_G_argv[0], service_name, GetLastError ()); return; } /* Now we are calling kubl_main second time. */ status = kubl_main (argc, argv, 1, &specificError); if (status != NO_ERROR) /* Handle error condition */ { wisvc_KublServiceStatus.dwCurrentState = SERVICE_STOPPED; wisvc_KublServiceStatus.dwCheckPoint = 0; wisvc_KublServiceStatus.dwWaitHint = 0; wisvc_KublServiceStatus.dwWin32ExitCode = status; wisvc_KublServiceStatus.dwServiceSpecificExitCode = specificError; SetServiceStatus (wisvc_KublServiceStatusHandle, &wisvc_KublServiceStatus); return; } wisvc_send_service_running_status (); /* This is where the service either does few checkpoints now and then or does nothing: */ main_the_rest (); /* In chil.c */ return; }