// // FUNCTION: main // // PURPOSE: entrypoint for service // // PARAMETERS: // argc - number of command line arguments // argv - array of command line arguments // // RETURN VALUE: // none // // COMMENTS: // main() either performs the command line task, or // call StartServiceCtrlDispatcher to register the // main service thread. When the this call returns, // the service has stopped, so exit. // void main(int argc, char **argv) { char *Account; char *Password; SERVICE_TABLE_ENTRY dispatchTable[] = { { TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main }, { NULL, NULL } }; if ( (argc > 1) && ((*argv[1] == '-') || (*argv[1] == '/')) ) { if ( _stricmp( "install", argv[1]+1 ) == 0 ) { /*check the account*/ /*WCG. Install it with domain or without domain*/ if(argc<3) Account=NULL; else Account=argv[2]; /*check the account*/ if(argc<4) Password=NULL; else Password=argv[3]; CmdInstallService(Account,Password); } else if ( _stricmp( "remove", argv[1]+1 ) == 0 ) { CmdRemoveService(); } else if ( _stricmp( "debug", argv[1]+1 ) == 0 ) { bDebug = TRUE; CmdDebugService(argc, argv); } else { goto dispatch; } exit(0); } // if it doesn't match any of the above parameters // the service control manager may be starting the service // so we must call StartServiceCtrlDispatcher dispatch: // this is just to be friendly printf( "%s -install administrator_account password to install the service\n", SZAPPNAME ); printf( "%s -remove to remove the service\n", SZAPPNAME ); printf( "%s -debug <params> to run as a console app for debugging\n", SZAPPNAME ); printf( "\nStartServiceCtrlDispatcher being called.\n" ); printf( "This may take several seconds. Please wait.\n" ); if (!StartServiceCtrlDispatcher(dispatchTable)) AddToMessageLog(NULL,0,EVENTLOG_ERROR_TYPE,SC_SYSTEM, EVLG_STD_STARTSERVICECTRLDISPATCHER_FAILED); }
/** * @brief main entry */ int main(int argc, char **argv) { // open syslog openlog("kshell", LOG_PID, LOG_USER); // signal handlers signal(SIGINT, ctrlC); signal(SIGKILL, ctrlC); signal(SIGTERM, ctrlC); signal(SIGQUIT, ctrlC); if ((argc > 1) && ((*argv[1] == '-') || (*argv[1] == '/'))) { if (_stricmp( "start", argv[1]+1)) { return CmdStartService(); } else if (_stricmp( "stop", argv[1]+1)) { return CmdStopService(); } else if (_stricmp( "debug", argv[1]+1)) { CmdDebugService(argc, argv); } else { goto dispatch; } return 0; } // if it doesn't match any of the above parameters // print helper string dispatch: // this is just to be friendly printf( "%s -start to start the service\n", SZAPPNAME ); printf( "%s -stop to start the service\n", SZAPPNAME ); printf( "%s -debug <params> to run as a console app for debugging\n", SZAPPNAME ); return 0; }
// // FUNCTION: main // // PURPOSE: entrypoint for service // // PARAMETERS: // argc - number of command line arguments // argv - array of command line arguments // // RETURN VALUE: // none // // COMMENTS: // main() either performs the command line task, or // call StartServiceCtrlDispatcher to register the // main service thread. When the this call returns, // the service has stopped, so exit. // int __cdecl main(int argc, char **argv) { SERVICE_TABLE_ENTRY dispatchTable[] = { { TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main}, { NULL, NULL} }; if ( (argc > 1) && ((*argv[1] == '-') || (*argv[1] == '/')) ) { if ( _stricmp( "install", argv[1]+1 ) == 0 ) { return CmdInstallService(); } else if ( _stricmp( "remove", argv[1]+1 ) == 0 ) { return CmdRemoveService(); } else if ( _stricmp( "start", argv[1]+1 ) == 0) { return CmdStartService(); } else if ( _stricmp( "stop", argv[1]+1 ) == 0) { return CmdStopService(); } else if ( _stricmp( "debug", argv[1]+1 ) == 0 ) { bDebug = TRUE; CmdDebugService(argc, argv); } else { goto dispatch; } return 0; } // if it doesn't match any of the above parameters // the service control manager may be starting the service // so we must call StartServiceCtrlDispatcher dispatch: // this is just to be friendly printf( "%s -install to install the service\n", SZAPPNAME ); printf( "%s -start to start the service\n", SZAPPNAME ); printf( "%s -stop to start the service\n", SZAPPNAME ); printf( "%s -remove to remove the service\n", SZAPPNAME ); printf( "%s -debug <params> to run as a console app for debugging\n", SZAPPNAME ); printf( "\nStartServiceCtrlDispatcher being called.\n" ); printf( "This may take several seconds. Please wait.\n" ); if (!StartServiceCtrlDispatcher(dispatchTable)) AddToMessageLog(MSG_FLAGS_ERROR, TEXT("StartServiceCtrlDispatcher failed.")); return 0; }
// // FUNCTION: main // // PURPOSE: entrypoint for service // // PARAMETERS: // argc - number of command line arguments // argv - array of command line arguments // // RETURN VALUE: // none // // COMMENTS: // main() either performs the command line task, or // call StartServiceCtrlDispatcher to register the // main service thread. When the this call returns, // the service has stopped, so exit. // void _CRTAPI1 main(int argc, char **argv) { SERVICE_TABLE_ENTRY dispatchTable[] = { { NULL, (LPSERVICE_MAIN_FUNCTION)service_main }, { NULL, NULL } }; if (!InitPrologService()) { AddToMessageLog(TEXT("Prolog Service Initilization failed.")); exit(0); } dispatchTable[0].lpServiceName = szServiceName; if ( (argc > 1) && ((*argv[1] == '-') || (*argv[1] == '/')) ) { if ( _stricmp( "install", argv[1]+1 ) == 0 ) { CmdInstallService(); } else if ( _stricmp( "remove", argv[1]+1 ) == 0 ) { CmdRemoveService(); } else if ( _stricmp( "debug", argv[1]+1 ) == 0 ) { bDebug = TRUE; CmdDebugService(argc, argv); } else { goto dispatch; } exit(0); } // if it doesn't match any of the above parameters // the service control manager may be starting the service // so we must call StartServiceCtrlDispatcher dispatch: // this is just to be friendly printf( "%s -install to install the service\n", szAppName ); printf( "%s -remove to remove the service\n", szAppName ); printf( "%s -debug <params> to run as a console app for debugging\n", szAppName ); printf( "\nStartServiceCtrlDispatcher being called.\n" ); printf( "This may take several seconds. Please wait.\n" ); if (!StartServiceCtrlDispatcher(dispatchTable)) AddToMessageLog(TEXT("StartServiceCtrlDispatcher failed.")); }
int _tmain(int argc, TCHAR * argv[]) { if (GetModuleFileName(NULL, szServicePath, CountOf(szServicePath)) == 0) { _tprintf(_T("Unable to get module name - %s\n"), GetLastErrorText(szErr, CountOf(szErr))); return 1; } GetServiceNameFromModule(szServicePath, szServiceName, CountOf(szServiceName)); _stprintf(szServiceDispName, _T("%s Server"), szServiceName); SERVICE_TABLE_ENTRY DispTable[] = { { szServiceName, (LPSERVICE_MAIN_FUNCTION) ServiceMain }, { NULL, NULL } }; if (argc > 1) { if (_tcsicmp(_T("--install"), argv[1]) == 0) { CmdInstallService(SERVICE_DEMAND_START); return 0; } if (_tcsicmp(_T("--install-auto"), argv[1]) == 0) { CmdInstallService(SERVICE_AUTO_START); return 0; } else if (_tcsicmp(_T("--remove"), argv[1]) == 0) { CmdRemoveService(); return 0; } else if (_tcsicmp(_T("--debug"), argv[1]) == 0) { bDebug = TRUE; CmdDebugService(argc, argv); return 0; } } _tprintf(_T("%s --install = Install the service\n"), argv[0]); _tprintf(_T("%s --remove = Remove the service\n"), argv[0]); _tprintf(_T("%s --debug [params] = Run as a console app for debugging\n"), argv[0]); _tprintf(_T("\nStartServiceCtrlDispatcher being called.\n")); _tprintf(_T("This may take several seconds. Please wait.\n")); /* Setup std handles */ if (MnSetupStdHandles() < 0) return 1; /* Service loop */ if (!StartServiceCtrlDispatcher(DispTable)) AddToMessageLog(_T("StartServiceCtrlDispatcher")); return 0; }
// parseCommandLine /////////////////////////////////////////////////////////// // // check for command line parameters and set various flags // void parseCommandLine (int *argc, char** argv[]) { /* // A little snipet of code to test the update feature. // Uncomment this code, compile, run mpd -update, then run mpd -loser to see if this new functionality exists if (GetOpt(*argc, *argv, "-loser")) { printf("you are a winner\n"); ExitProcess(0); } //*/ if (GetOpt(*argc, *argv, "-norestart")) bSetupRestart = false; if (GetOpt(*argc, *argv, "-interact")) { interact = true; } if (GetOpt(*argc, *argv, "-remove") || GetOpt(*argc, *argv, "-unregserver") || GetOpt(*argc, *argv, "-uninstall")) { RegDeleteKey(HKEY_CURRENT_USER, MPICHKEY); CmdRemoveService(TRUE); ExitProcess(0); } if (GetOpt(*argc, *argv, "-install") || GetOpt(*argc, *argv, "-regserver")) { bool bMPDUserCapable = false; char account[100]="", password[100]="", phrase[MPD_PASSPHRASE_MAX_LENGTH+1]="", port[10]=""; char version[100]=""; if (CmdRemoveService(FALSE) == FALSE) { printf("Unable to remove the previous installation, install failed.\n"); ExitProcess(0); } easy_socket_init(); CreateMPDRegistry(); bMPDUserCapable = GetOpt(*argc, *argv, "-mpduser"); if (GetOpt(*argc, *argv, "-phrase", phrase)) WriteMPDRegistry("phrase", phrase); if (GetOpt(*argc, *argv, "-getphrase")) { GetPassword("passphrase for mpd: ", NULL, phrase); WriteMPDRegistry("phrase", phrase); } if (GetOpt(*argc, *argv, "-port", port)) WriteMPDRegistry("port", port); if (GetOpt(*argc, *argv, "-account", account)) { if (!GetOpt(*argc, *argv, "-password", password)) GetPassword(NULL, account, password); WriteMPDRegistry("SingleUser", "yes"); ParseRegistry(true); CmdInstallService(account, password, bMPDUserCapable); } else { WriteMPDRegistry("SingleUser", "no"); ParseRegistry(true); CmdInstallService(NULL, NULL, bMPDUserCapable); } GetMPDVersion(version, 100); WriteMPDRegistry("version", version); easy_socket_finalize(); ExitProcess(0); } if (GetOpt(*argc, *argv, "-update")) { char account[100]="", password[100]="", phrase[MPD_PASSPHRASE_MAX_LENGTH+1]="", port[10]=""; int nPort; char pszHost[MAX_HOST_LENGTH], pszHostFile[MAX_PATH]; char pszFileName[MAX_PATH]; char pszError[256]; if (!GetOpt(*argc, *argv, "-mpd", pszFileName)) { HMODULE hModule = GetModuleHandle(NULL); if (!GetModuleFileName(hModule, pszFileName, MAX_PATH)) { printf("Please specify the location of the new mpd.exe with the -mpd option, (-mpd c:\\some\\path\\mpd.exe)\n"); ExitProcess(0); } printf("updating mpd to '%s'\n", pszFileName); } if (!GetOpt(*argc, *argv, "-singleuser")) { if (GetOpt(*argc, *argv, "-account", account)) { if (!GetOpt(*argc, *argv, "-password", password)) GetPassword(NULL, account, password); } else { printf("Enter a user to connect to the remote machines as.\naccount: ");fflush(stdout); gets(account); GetPassword(NULL, account, password); } } easy_socket_init(); nPort = MPD_DEFAULT_PORT; if (!ReadMPDRegistry("phrase", phrase, false)) strcpy(phrase, MPD_DEFAULT_PASSPHRASE); GetOpt(*argc, *argv, "-phrase", phrase); if (GetOpt(*argc, *argv, "-getphrase")) { GetPassword("passphrase for mpd: ", NULL, phrase); } if (GetOpt(*argc, *argv, "-port", port)) nPort = atoi(port); if (GetOpt(*argc, *argv, "-hostfile", pszHostFile)) { FILE *fin = fopen(pszHostFile, "r"); if (fin == NULL) { char pszStr[1024]; Translate_Error(GetLastError(), pszStr); printf("Unable to open the host file '%s', %s\n", pszHostFile, pszStr); easy_socket_finalize(); ExitProcess(0); } while (GetNextHost(fin, pszHost)) { pszError[0] = '\0'; if (!UpdateMPD(pszHost, account, password, nPort, phrase, pszFileName, pszError, 256)) { printf("Failed to update mpd on %s:\n%s\n", pszHost, pszError); } } fclose(fin); } else { if (!GetOpt(*argc, *argv, "-host", pszHost)) { printf("Enter the hostname where the mpd that you wish to update is running.\nhost: ");fflush(stdout); gets(pszHost); } pszError[0] = '\0'; if (!UpdateMPD(pszHost, account, password, nPort, phrase, pszFileName, pszError, 256)) { printf("Failed to update mpd on %s:\n%s\n", pszHost, pszError); } } easy_socket_finalize(); printf("Finished.\n"); ExitProcess(0); } if (GetOpt(*argc, *argv, "-iupdate")) { // This option is used internally by the update feature char pszOldFileName[MAX_PATH], pszNewFileName[MAX_PATH]; char pszPid[10]; if (GetOpt(*argc, *argv, "-pid", pszPid) && GetOpt(*argc, *argv, "-old", pszOldFileName) && GetOpt(*argc, *argv, "-new", pszNewFileName)) { UpdateMPD(pszOldFileName, pszNewFileName, atoi(pszPid)); } ExitProcess(0); } char host[100]; if (GetOpt(*argc, *argv, "-console", host)) { char phrase[MPD_PASSPHRASE_MAX_LENGTH+1]; int port = -1; GetOpt(*argc, *argv, "-port", &port); /* DoConsole destroys phrase after using it */ DoConsole( host, port, GetOpt(*argc, *argv, "-getphrase"), GetOpt(*argc, *argv, "-phrase", phrase) ? phrase : NULL); easy_socket_finalize(); ExitProcess(0); } if (GetOpt(*argc, *argv, "-console")) { char phrase[MPD_PASSPHRASE_MAX_LENGTH+1]; int port = -1; GetOpt(*argc, *argv, "-port", &port); /* DoConsole destroys phrase after using it */ DoConsole( NULL, port, GetOpt(*argc, *argv, "-getphrase"), GetOpt(*argc, *argv, "-phrase", phrase) ? phrase : NULL); easy_socket_finalize(); ExitProcess(0); } if (GetOpt(*argc, *argv, "-start")) { CmdStartService(); ExitProcess(0); } char pszFileName[MAX_PATH]; if (GetOpt(*argc, *argv, "-startdelete", pszFileName)) { // This option is used by the update feature to start the new service and delete the old one. char version[100]; // update the version GetMPDVersion(version, 100); WriteMPDRegistry("version", version); // start the new mpd CmdStartService(); // Give the temporary mpd time to exit Sleep(1000); // Then delete it. DeleteFile(pszFileName); ExitProcess(0); } if (GetOpt(*argc, *argv, "-stop")) { CmdStopService(); ExitProcess(0); } if (GetOpt(*argc, *argv, "-restart", host)) { ConnectAndRestart(argc, argv, host); ExitProcess(0); } if (GetOpt(*argc, *argv, "-restart")) { CmdStopService(); Sleep(1000); CmdStartService(); ExitProcess(0); } if (GetOpt(*argc, *argv, "-clean")) { CleanMPDRegistry(); ExitProcess(0); } if (GetOpt(*argc, *argv, "-d")) { char account[100]="", password[100]="", phrase[MPD_PASSPHRASE_MAX_LENGTH+1]="", pszPort[20]=""; char str_temp[10]; easy_socket_init(); CreateMPDRegistry(); if (GetOpt(*argc, *argv, "-phrase", phrase)) WriteMPDRegistry("phrase", phrase); if (GetOpt(*argc, *argv, "-getphrase")) { GetPassword("passphrase for mpd: ", NULL, phrase); WriteMPDRegistry("phrase", phrase); } if (GetOpt(*argc, *argv, "-port", pszPort)) { int g_nSavedPort = g_nPort; g_nPort = atoi(pszPort); if (g_nPort > 0) { sprintf(pszPort, "%d", g_nPort); WriteMPDRegistry("port", pszPort); //printf("using port %d\n", g_nPort); } else g_nPort = g_nSavedPort; } g_bSingleUser = true; g_bStartAlone = GetOpt(*argc, *argv, "-startalone"); if (ReadMPDRegistry("SingleUser", str_temp, false)) { if (stricmp(str_temp, "no") == 0) { WriteMPDRegistry("RevertToMultiUser", "yes"); } } WriteMPDRegistry("SingleUser", "yes"); ParseRegistry(true); CmdDebugService(*argc, *argv); easy_socket_finalize(); ExitProcess(0); } if (GetOpt(*argc, *argv, "-v") || GetOpt(*argc, *argv, "-version")) { /*#define USE_BAD_NONREDIRECTABLE_VERSION*/ #ifdef USE_BAD_NONREDIRECTABLE_VERSION /* stdin, stdout, stderr don't get redirected for some reason when I launch this application with pipes for redirecting output. If the HANDLE version of stderr is used like the code after the else, then everything works fine. */ char str[100]; GetMPDVersion(str, 100); fprintf(stderr, "\nMPD - mpich daemon for Microsoft Windows, version %s\n%s\n", str, COPYRIGHT); fflush(stderr); ExitProcess(0); #else HANDLE hErr = GetStdHandle(STD_ERROR_HANDLE); char str[100], out[200]; DWORD num_written; GetMPDVersion(str, 100); sprintf(out, "\nMPD - mpich daemon for Microsoft Windows, version %s\n%s\n", str, COPYRIGHT); WriteFile(hErr, out, strlen(out), &num_written, NULL); ExitProcess(0); #endif } if (GetOpt(*argc, *argv, "-h") || GetOpt(*argc, *argv, "-?") || GetOpt(*argc, *argv, "-help")) { char str[100]; GetMPDVersion(str, 100); fprintf(stderr, "\nMPD - mpich daemon for Windows NT, version %s\n%s\n", str, COPYRIGHT); fprintf(stderr, "Usage:\n mpd [ -v -h -install -remove -console ]\n\nCommand line options:\n"); fprintf(stderr, " -install \t:install the service\n -install -interact :allows the mpd to interact with the desktop\n"); fprintf(stderr, " -install -mpduser\t:install the service with single user commands enabled.\n"); fprintf(stderr, " -remove\t:remove the service\n"); fprintf(stderr, " -v\t\t:display version\n"); fprintf(stderr, " -h\t\t:this help screen\n"); fprintf(stderr, " -console\t:start a console session with the mpd on the current host\n"); fprintf(stderr, " -console host [-port x] :start a console session with the mpd on 'host:port'\n"); fprintf(stderr, " -d\t\t:run the mpd from the console\n"); ExitProcess(0); } }