VOID ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv) { pthread_t lprobeThread; TCHAR szAppParameters[8192]; // Let the service control manager know that the service is // initializing. if (!ReportStatus(SERVICE_START_PENDING, NO_ERROR, 3000)) //goto cleanup; return; // Create a Stop Event hServerStopEvent = CreateEvent( NULL, TRUE, FALSE, NULL); if ( hServerStopEvent == NULL) goto cleanup; if(dwArgc > 0) _dwArgc = dwArgc, _lpszArgv = lpszArgv; else { char *progName = SZSERVICENAME; _dwArgc = 1, _lpszArgv = &progName; } if (!ReportStatus(SERVICE_RUNNING,NO_ERROR,0)){ goto cleanup; } // createThread(&lprobeThread, invokelprobe, NULL); // J. R. Duarte: Create an argument string from the argument list convertArgListToArgString((LPTSTR)szAppParameters,0, dwArgc, lpszArgv); if(NULL == szAppParameters){ _tprintf(TEXT("Could not create AppParameters string.\n")); } AddToMessageLog(TEXT("About to start lprobe")); pthread_create(&lprobeThread, NULL, invokelprobe, (void*)strdup(szAppParameters)); AddToMessageLog(TEXT("lprobe started")); // Wait for the stop event to be signalled. WaitForSingleObject(hServerStopEvent,INFINITE); AddToMessageLog(TEXT("lprobe terminated")); cleanup: if (hServerStopEvent) CloseHandle(hServerStopEvent); }
void installService(int argc, char **argv) { SC_HANDLE schService; SC_HANDLE schSCManager; TCHAR szPath[512], szDescr[256]; TCHAR szAppParameters[8192]; char szParamKey[1025], szParamKey2[1025]; sprintf(szParamKey,"SYSTEM\\CurrentControlSet\\Services\\%s\\Parameters",SZSERVICENAME); // Get the full path and filename of this program if ( GetModuleFileName( NULL, szPath, 512 ) == 0 ){ _tprintf(TEXT("Unable to install %s - %s\n"), TEXT(SZSERVICEDISPLAYNAME), GetLastErrorText(szErr, 256)); return; } // Next, get a handle to the service control manager schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if ( schSCManager ) { schService = CreateService(schSCManager, // SCManager database TEXT(SZSERVICENAME), // name of service TEXT(SZSERVICEDISPLAYNAME), // name to display SERVICE_ALL_ACCESS, // desired access SERVICE_WIN32_OWN_PROCESS, // service type SERVICESTARTTYPE, // start type SERVICE_ERROR_NORMAL, // error control type szPath, // service's binary NULL, // no load ordering group NULL, // no tag identifier TEXT(SZDEPENDENCIES), // dependencies NULL, // LocalSystem account NULL); // no password if (schService){ _tprintf(TEXT("%s installed.\n"), TEXT(SZSERVICEDISPLAYNAME) ); // Close the handle to this service object CloseServiceHandle(schService); /* ****************************************** */ // Set the service name. Courtesy of Yuri Francalacci <*****@*****.**> sprintf(szParamKey2, "SYSTEM\\CurrentControlSet\\Services\\%s",SZSERVICENAME); strcpy(szDescr, "ntopng: Web-based network traffic monitor"); // Set the file value (where the message resources are located.... in this case, our runfile.) if(0 != setStringValue((const unsigned char *)szDescr, strlen(szDescr) + 1,HKEY_LOCAL_MACHINE, szParamKey2,TEXT("Description"))) { _tprintf(TEXT("The Message File value could\nnot be assigned.\n")); } /* ********************************************** */ //Make a registry key to support logging messages using the service name. sprintf(szParamKey2, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",SZSERVICENAME); if(0 != makeNewKey(HKEY_LOCAL_MACHINE, szParamKey2)){ _tprintf(TEXT("The EventLog subkey could not be created.\n")); } // Set the file value (where the message resources are located.... in this case, our runfile.) if(0 != setStringValue((const unsigned char *) szPath, strlen(szPath) + 1,HKEY_LOCAL_MACHINE, szParamKey2,TEXT("EventMessageFile"))) { _tprintf(TEXT("The Message File value could\nnot be assigned.\n")); } // Set the supported types flags. if(0 != setDwordValue(EVENTLOG_INFORMATION_TYPE,HKEY_LOCAL_MACHINE, szParamKey2,TEXT("TypesSupported"))){ _tprintf(TEXT("The Types Supported value could\nnot be assigned.\n")); } // Try to create a subkey to hold the runtime args for the JavaVM and // Java application if(0 != makeNewKey(HKEY_LOCAL_MACHINE, szParamKey)){ _tprintf(TEXT("Could not create Parameters subkey.\n")); } else { //Create an argument string from the argument list // J. R. Duarte: modified it to store the full command line convertArgListToArgString((LPTSTR) szAppParameters,0, argc, argv); if(NULL == szAppParameters){ _tprintf(TEXT("Could not create AppParameters string.\n")); } else { HKEY hkey; DWORD disposition; _TCHAR data[] = "redis\0\0"; // Try to save the argument string under the new subkey if(0 != setStringValue(szAppParameters, strlen(szAppParameters)+1, HKEY_LOCAL_MACHINE, szParamKey, SZAPPPARAMS)){ _tprintf(TEXT("Could not save AppParameters value.\n")); } sprintf(szParamKey,"SYSTEM\\CurrentControlSet\\Services\\%s",SZSERVICENAME); if( RegCreateKeyEx( HKEY_LOCAL_MACHINE, szParamKey, 0, "", 0, KEY_ALL_ACCESS, NULL, &hkey, &disposition) != ERROR_SUCCESS) { _tprintf(TEXT("Could not create service registry key")); return; } strcpy(szAppParameters, "redis"); // Try to save the argument string under the new subkey if(RegSetValueEx (hkey, TEXT("DependOnService"), 0, REG_MULTI_SZ, (LPBYTE)data, strlen(data)+2) != 0) { _tprintf(TEXT("Could not save DependOnService value.\n")); } RegCloseKey(hkey); } } } else{ _tprintf(TEXT("CreateService failed - %s\n"), GetLastErrorText(szErr, 256)); } // Close the handle to the service control manager database CloseServiceHandle(schSCManager); } else{ _tprintf(TEXT(SZSCMGRFAILURE), GetLastErrorText(szErr,256)); } }
void main(int argc, char **argv) { // The StartServiceCtrlDispatcher requires this table to specify // the ServiceMain function to run in the calling process. The first // member in this example is actually ignored, since we will install // our service as a SERVICE_WIN32_OWN_PROCESS service type. The NULL // members of the last entry are necessary to indicate the end of // the table; SERVICE_TABLE_ENTRY serviceTable[] = { { TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)serviceMain }, { NULL, NULL } }; TCHAR szAppParameters[8192]; if(!isWinNT()) { convertArgListToArgString((LPTSTR) szAppParameters,0, argc, argv); if(NULL == szAppParameters){ _tprintf(TEXT("Could not create AppParameters string.\n")); } invokeNtop(szAppParameters); return; } isNtopAservice = 0; // This app may be started with one of three arguments, /i, /r, and // /c, or /?, followed by actual program arguments. These arguments // indicate if the program is to be installed, removed, run as a // console application, or to display a usage message. if(argc > 1){ if(!stricmp(argv[1],"/i")){ installService(argc,argv); printf("NOTE: the default password for the 'admin' user has been set to 'admin'."); } else if(!stricmp(argv[1],"/r")){ removeService(); } else if(!stricmp(argv[1],"/c")){ bConsole = TRUE; runService(argc, argv); } else { printf("\nUnrecognized option: %s\n", argv[1]); printf("Available options:\n"); printf("/i [ntopng options] - Install ntopng as service\n"); printf("/c - Run ntopng on a console\n"); printf("/r - Deinstall the service\n"); printf("/h - Prints this help\n\n"); usage(); } exit(0); } // If main is called without any arguments, it will probably be by the // service control manager, in which case StartServiceCtrlDispatcher // must be called here. A message will be printed just in case this // happens from the console. printf("\nNOTE:\nUnder your version of Windows, ntopng is started as a service.\n"); printf("Please open the services control panel to start/stop ntop,\n"); printf("or type ntop /h to see all the available options.\n"); isNtopAservice = 1; if(!StartServiceCtrlDispatcher(serviceTable)) { printf("\n%s\n", SZFAILURE); AddToMessageLog(TEXT(SZFAILURE)); } }
void main(int argc, char **argv) { // The StartServiceCtrlDispatcher requires this table to specify // the ServiceMain function to run in the calling process. The first // member in this example is actually ignored, since we will install // our service as a SERVICE_WIN32_OWN_PROCESS service type. The NULL // members of the last entry are necessary to indicate the end of // the table; SERVICE_TABLE_ENTRY serviceTable[] = { { TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)serviceMain }, { NULL, NULL } }; TCHAR szAppParameters[8192]; // pthread_win32_process_attach_np(); if(!isWinNT()) { convertArgListToArgString((LPTSTR) szAppParameters,0, argc, argv); if(NULL == szAppParameters){ _tprintf(TEXT("Could not create AppParameters string.\n")); } invokelprobe(szAppParameters); return; } thisIsAservice = 0; // This app may be started with one of three arguments, /i, /r, and // /c, or /?, followed by actual program arguments. These arguments // indicate if the program is to be installed, removed, run as a // console application, or to display a usage message. if(argc > 1){ char *service_name = "lprobe for Win32"; if(!stricmp(argv[1],"/i")){ if(argc >2) installService(argv[2], argc, argv); else _tprintf(TEXT("/i requires the service name as parameter\n")); } else if(!stricmp(argv[1],"/r")){ if(argc >1) removeService(argv[2]); else _tprintf(TEXT("/r requires the service name as parameter\n")); } else if(!stricmp(argv[1],"/c")){ bConsole = TRUE; runService(argc,argv); } else{ if(stricmp(argv[1],"/h")) printf("\nUnrecognized option: %s\n", argv[1]); printf("Available options:\n"); printf("/i <service name> [lprobe options] - Install lprobe as service\n"); printf("/c [lprobe options] - Run lprobe on a console\n"); printf("/r <service name> - Deinstall the service\n\n"); printf("Example:\n" "Install lprobe as a service: 'lprobe /i my_lprobe -i 0 -n 192.168.0.1:2055'\n" "Remove the lprobe service: 'lprobe /r my_lprobe'\n\n"); printf("Notes:\n" "1. Type 'lprobe /c -h' to see all options\n" "1. In order to reinstall a service with new options\n" " it is necessary to first remove the service, then add it\n" " again with the new options.\n" "2. Services are started/stopped using the Services\n" " control panel item.\n" "3. You can install the lprobe service multiple times\n" " as long as you use different service names.\n\n"); } exit(0); } thisIsAservice = 1; // If main is called without any arguments, it will probably be by the // service control manager, in which case StartServiceCtrlDispatcher // must be called here. A message will be printed just in case this // happens from the console. printf("\nNOTE:\nUnder your version of Windows, lprobe is started as a service.\n"); printf("Please open the services control panel to start/stop lprobe,\n"); printf("or type lprobe /h to see all the available options.\n"); if(!StartServiceCtrlDispatcher(serviceTable)) { printf("\n%s\n", SZFAILURE); AddToMessageLog(TEXT(SZFAILURE)); } }
void installService(char *service_name, int argc, char **argv) { SC_HANDLE schService; SC_HANDLE schSCManager; TCHAR szPath[512], szDescr[256]; TCHAR szAppParameters[8192]; SERVICE_DESCRIPTION sdBuf; char szParamKey[1025], szParamKey2[1025]; #if 0 thisIsAservice = 1; bConsole = 0; if(argc >=1) traceEvent(TRACE_ERROR, "argv[1] = %s", argv[1]); if(argc >=2) traceEvent(TRACE_ERROR, "argv[2] = %s", argv[2]); if(argc >=3) traceEvent(TRACE_ERROR, "argv[3] = %s", argv[3]); #endif sprintf(szParamKey, "SYSTEM\\CurrentControlSet\\Services\\%s\\Parameters", service_name); // Get the full path and filename of this program if ( GetModuleFileName( NULL, szPath, 512 ) == 0 ){ _tprintf(TEXT("Unable to install %s - %s\n"), TEXT(service_name), GetLastErrorText(szErr, 256)); return; } // Next, get a handle to the service control manager schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if ( schSCManager ) { schService = CreateService(schSCManager, // SCManager database TEXT(service_name), // name of service TEXT(service_name), // name to display SERVICE_ALL_ACCESS, // desired access SERVICE_WIN32_OWN_PROCESS, // service type SERVICESTARTTYPE, // start type SERVICE_ERROR_NORMAL, // error control type szPath, // service's binary NULL, // no load ordering group NULL, // no tag identifier TEXT(SZDEPENDENCIES), // dependencies NULL, // LocalSystem account NULL); // no password if (schService){ _tprintf(TEXT("%s installed.\n"), TEXT(service_name) ); //Create an argument string from the argument list // J. R. Duarte: modified it to store the full command line convertArgListToArgString((LPTSTR) szAppParameters, 0, argc, argv); /* Modify the service description string */ if(szAppParameters != NULL) { sdBuf.lpDescription = szAppParameters; if( !ChangeServiceConfig2( schService, // handle to service SERVICE_CONFIG_DESCRIPTION, // change: description &sdBuf) ) // value: new description { ; /* Failed */ } } // Close the handle to this service object CloseServiceHandle(schService); /* ****************************************** */ // Set the service name. Courtesy of Yuri Francalacci <*****@*****.**> sprintf(szParamKey2, "SYSTEM\\CurrentControlSet\\Services\\%s",service_name); snprintf(szDescr, sizeof(szDescr), "lprobe v.%s - NetFlow/IPFIX Probe. http://www.ltop.org/", version); // Set the file value (where the message resources are located.... in this case, our runfile.) if(0 != setStringValue((const unsigned char *)szDescr, strlen(szDescr) + 1,HKEY_LOCAL_MACHINE, szParamKey2,TEXT("Description"))) { _tprintf(TEXT("Unable to set service description .\n")); } /* ********************************************** */ //Make a registry key to support logging messages using the service name. sprintf(szParamKey2, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s", service_name); if(0 != makeNewKey(HKEY_LOCAL_MACHINE, szParamKey2)){ _tprintf(TEXT("The EventLog subkey could not be created.\n")); } // Set the file value (where the message resources are located.... in this case, our runfile.) if(0 != setStringValue((const unsigned char *) szPath, strlen(szPath) + 1,HKEY_LOCAL_MACHINE, szParamKey2,TEXT("EventMessageFile"))) { _tprintf(TEXT("The Message File value could\nnot be assigned.\n")); } // Set the supported types flags. if(0 != setDwordValue(EVENTLOG_INFORMATION_TYPE,HKEY_LOCAL_MACHINE, szParamKey2,TEXT("TypesSupported"))){ _tprintf(TEXT("The Types Supported value could\nnot be assigned.\n")); } // Try to create a subkey to hold the runtime args for the JavaVM and // Java application if(0 != makeNewKey(HKEY_LOCAL_MACHINE, szParamKey)){ _tprintf(TEXT("Could not create Parameters subkey.\n")); } else { if(NULL == szAppParameters){ _tprintf(TEXT("Could not create AppParameters string.\n")); } else{ // Try to save the argument string under the new subkey if(0 != setStringValue(szAppParameters, strlen(szAppParameters)+1, HKEY_LOCAL_MACHINE, szParamKey, SZAPPPARAMS)){ _tprintf(TEXT("Could not save AppParameters value.\n")); } } } } else{ _tprintf(TEXT("CreateService failed - %s\n"), GetLastErrorText(szErr, 256)); } // Close the handle to the service control manager database CloseServiceHandle(schSCManager); } else{ _tprintf(TEXT(SZSCMGRFAILURE), GetLastErrorText(szErr,256)); } }