void TraceSvcUtil::startSession(TraceSession& session, bool /*interactive*/) { m_stop = false; HalfStaticArray<UCHAR, 1024> buff(*getDefaultMemoryPool()); UCHAR* p = NULL; long len = 0; FILE* file = NULL; try { const char* fileName = session.ses_config.c_str(); file = fopen(fileName, "rb"); if (!file) { (Arg::Gds(isc_io_error) << Arg::Str("fopen") << Arg::Str(fileName) << Arg::Gds(isc_io_open_err) << Arg::OsError()).raise(); } fseek(file, 0, SEEK_END); len = ftell(file); if (len == 0) { (Arg::Gds(isc_io_error) << Arg::Str("fread") << Arg::Str(fileName) << Arg::Gds(isc_io_read_err) << Arg::OsError()).raise(); } fseek(file, 0, SEEK_SET); p = buff.getBuffer(len); if (fread(p, 1, len, file) != size_t(len)) { (Arg::Gds(isc_io_error) << Arg::Str("fread") << Arg::Str(fileName) << Arg::Gds(isc_io_read_err) << Arg::OsError()).raise(); } fclose(file); } catch (const Exception&) { if (file) fclose(file); throw; } ClumpletWriter spb(ClumpletWriter::SpbStart, MAXBUF); spb.insertTag(isc_action_svc_trace_start); spb.insertBytes(isc_spb_trc_cfg, p, len); if (session.ses_name.hasData()) { spb.insertBytes(isc_spb_trc_name, reinterpret_cast<const UCHAR*> (session.ses_name.c_str()), session.ses_name.length()); } runService(spb.getBufferLength(), spb.getBuffer()); }
void NetworkService::run() { m_impl->service->reset(); m_impl->connectionsManager->enable(); // Inizializza il timer m_impl->timer = createAsioObject<boost::asio::deadline_timer>(*getService()); startTimer(); OS_ASSERT(m_impl->workerThreads.empty()); uint32 workerThreadsCount = getWorkerThreadsCount(); for(uint32 i = 0; i < workerThreadsCount; i++) { shared_ptr<WorkerThread> thread(OS_NEW WorkerThread(boost::bind(&NetworkService::runService, this))); // Assegna ai sotto threads la stessa priorità del thread principale thread->setPriority(getPriority()); // Accoda e avvia il sottothread m_impl->workerThreads.add(thread, true); } // Avvia il servizio runService(); // Attende la chiusura di tutti i threads m_impl->workerThreads.join(); m_impl->workerThreads.clear(); }
void TraceSvcUtil::listSessions() { ClumpletWriter spb(ClumpletWriter::SpbStart, MAXBUF); spb.insertTag(isc_action_svc_trace_list); runService(spb.getBufferLength(), spb.getBuffer()); }
void TraceSvcUtil::setActive(ULONG id, bool active) { ClumpletWriter spb(ClumpletWriter::SpbStart, MAXBUF); spb.insertTag(active ? isc_action_svc_trace_resume : isc_action_svc_trace_suspend); spb.insertInt(isc_spb_trc_id, id); runService(spb.getBufferLength(), spb.getBuffer()); }
void TraceSvcUtil::stopSession(ULONG id) { ClumpletWriter spb(ClumpletWriter::SpbStart, MAXBUF); spb.insertTag(isc_action_svc_trace_stop); spb.insertInt(isc_spb_trc_id, id); runService(spb.getBufferLength(), spb.getBuffer()); }
static bool process(cchar *operation, bool quiet) { cchar *name, *off, *path; int rc, launch, update, service, upstart; /* No systemd support yet */ rc = 1; name = app->serviceName; launch = upstart = update = service = 0; if (exists("/bin/launchctl") && exists("/Library/LaunchDaemons/com.%s.%s.plist", slower(BLD_COMPANY), name)) { launch++; } else if (exists("/sbin/start") && exists("/etc/init/rc.conf") && (exists("/etc/init/%s.conf", name) || exists("/etc/init/%s.off", name))) { upstart++; } else if (exists("/usr/sbin/update-rc.d") && exists("/etc/init.d/%s", name)) { update++; } else if (exists("/sbin/service") && exists("/etc/init.d/%s", name)) { service++; } else { mprError("Can't locate system tool to manage service"); return 0; } /* Operations */ if (smatch(operation, "install")) { if (launch) { ; } else if (service) { if (!run("/sbin/chkconfig --del %s", name)) { rc = 0; } else if (!run("/sbin/chkconfig --add %s", name)) { rc = 0; } else if (!run("/sbin/chkconfig --level 5 %s", name)) { rc = 0; } } else if (update) { ; } else if (upstart) { ; } } else if (smatch(operation, "uninstall")) { process("disable", 1); if (launch) { ; } else if (service) { rc = run("/sbin/chkconfig --del %s", name); } else if (update) { ; } else if (upstart) { ; } } else if (smatch(operation, "enable")) { /* Enable service (will start on reboot) */ if (launch) { path = sfmt("/Library/LaunchDaemons/com.%s.%s.plist", slower(BLD_COMPANY), name); /* Unfortunately, there is no launchctl command to do an enable without starting. So must do a stop below. */ if (!run("/bin/launchctl load -w %s", path)) { rc = 0; } else { rc = process("stop", 1); } } else if (update) { rc = run("/usr/sbin/update-rc.d %s defaults 90 10", name); /* Not supported on older versions rc = run("/usr/sbin/update-rc.d %s enable", name); */ } else if (service) { rc = run("/sbin/chkconfig %s on", name); } else if (upstart) { off = sfmt("/etc/init/%s.off", name); if (exists(off) && !run("mv %s /etc/init/%s.conf", off, name)) { rc = 0; } } } else if (smatch(operation, "disable")) { process("stop", 1); if (launch) { rc = run("/bin/launchctl unload -w /Library/LaunchDaemons/com.%s.%s.plist", slower(BLD_COMPANY), name); } else if (update) { /* Not supported on older versions rc = run("/usr/sbin/update-rc.d %s disable", name); */ rc = run("/usr/sbin/update-rc.d -f %s remove", name); } else if (service) { rc = run("/sbin/chkconfig %s off", name); } else if (upstart) { if (exists("/etc/init/%s.conf", name)) { rc = run("mv /etc/init/%s.conf /etc/init/%s.off", name, name); } } } else if (smatch(operation, "start")) { if (launch) { rc = run("/bin/launchctl load /Library/LaunchDaemons/com.%s.%s.plist", slower(BLD_COMPANY), name); } else if (service) { rc = run("/sbin/service %s start", name); } else if (update) { rc = run("/usr/sbin/invoke-rc.d --quiet %s start", name); } else if (upstart) { rc = run("/sbin/start %s", name); if (!rc) { if (scontains(app->error, "start: Job is already running", -1)) { rc = 0; } } } } else if (smatch(operation, "stop")) { if (launch) { rc = run("/bin/launchctl unload /Library/LaunchDaemons/com.%s.%s.plist", slower(BLD_COMPANY), name); } else if (service) { if (!run("/sbin/service %s stop", name)) { rc = killPid(); } } else if (update) { if (!run("/usr/sbin/invoke-rc.d --quiet %s stop", name)) { rc = killPid(); } } else if (upstart) { if (exists("/etc/init/%s.conf", name)) { rc = run("/sbin/stop %s", name); } } } else if (smatch(operation, "reload")) { rc = process("restart", 0); } else if (smatch(operation, "restart")) { process("stop", 1); rc = process("start", 0); } else if (smatch(operation, "run")) { runService(); } if (!quiet) { if (!rc && app->error && *app->error) { mprError("Can't run command: %s\nCommand output: %s\n", app->command, app->error); } /* Logging at level one will be visible if appman -v is used */ if (app->error && *app->error) { mprLog(1, "Error: %s", app->error); } if (app->output && *app->output) { mprLog(1, "Output: %s", app->output); } } return rc; }
int main (int argn, char **argv) { program_name = argv[0]; int runas = MYSERVER_RUNAS_CONSOLE; struct argp_input input; #ifndef WIN32 pid_t pid; pid_t sid; #endif string mainConf, mimeConf, vhostConf, externPath; registerSignals (); setlocale (LC_ALL, ""); setlocale (LC_TIME, "POSIX"); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); try { Server::createInstance (); } catch (exception & e) { cerr << e.what () << endl; return 1; } catch (...) { return 1; }; /* Move to a different working directory, if necessary. */ updateWorkingDirectory (argv[0]); /* Call the parser. */ argp_parse (&myserverArgp, argn, argv, 0, 0, &input); runas = input.runas; if (input.logFileName && Server::getInstance ()->setLogLocation (input.logFileName)) { cerr << "Error setting the location for the MyServer's main log" << endl; return 1; } /* If the version flag is up, show the version and exit. */ if (input.version) { cout << "GNU MyServer " << MYSERVER_VERSION << endl << "Copyright (C) 2002-2010 Free Software Foundation, Inc." << endl << "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>." << endl << "This is free software: you are free to change and redistribute it." << endl << "There is NO WARRANTY, to the extent permitted by law." << endl << endl << "http://www.gnu.org/software/myserver" << endl; return 0; } if (input.useForkServer) { FilesUtility::resetTmpPath (); Process::getForkServer ()->startForkServer (); } if (input.plugins) { PluginsManager *pluginsManager = Server::getInstance ()->getPluginsManager (); if (pluginsManager->quickLoad (Server::getInstance (), input.plugins)) { cerr << _("Cannot load specified plugins") << endl; return 1; } } /* Start here the MyServer execution. */ try { const char *confFileDir; #ifdef WIN32 confFileDir = "."; #else confFileDir = input.confFilesLocation; #endif if (loadConfFilesLocation (mainConf, mimeConf, vhostConf, externPath, confFileDir)) { cerr << _("Cannot find the configuration files, be sure they exist") << endl; return 1; } switch (runas) { case MYSERVER_RUNAS_SERVICE: #ifdef WIN32 runService (); #else /* Run the daemon. pid is the process ID for the forked process. Fork the process. */ pid = fork (); /* An error happened, return with errors. */ if (pid < 0) return 1; if (pid) { if (input.pidFileName) writePidfile (input.pidFileName); else writePidfile (); return 0; } /* Create a SID for the new process. */ sid = setsid (); if (sid < 0) return 1; #endif case MYSERVER_RUNAS_CONSOLE: consoleService (mainConf, mimeConf, vhostConf, externPath, genMainConf); } } catch (...) { if (input.useForkServer) Process::getForkServer ()->killServer (); return 1; }; if (input.useForkServer) Process::getForkServer ()->killServer (); return 0; }
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)); } }