static VOID WINAPI ServiceEntry(DWORD argc, wchar_t **argv) { wchar_t *wservice_name; wservice_name = zbx_utf8_to_unicode(ZABBIX_SERVICE_NAME); serviceHandle = RegisterServiceCtrlHandler(wservice_name, ServiceCtrlHandler); zbx_free(wservice_name); /* start service initialization */ serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; serviceStatus.dwCurrentState = SERVICE_START_PENDING; serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; serviceStatus.dwWin32ExitCode = 0; serviceStatus.dwServiceSpecificExitCode = 0; serviceStatus.dwCheckPoint = 0; serviceStatus.dwWaitHint = 2000; SetServiceStatus(serviceHandle, &serviceStatus); /* service is running */ serviceStatus.dwCurrentState = SERVICE_RUNNING; serviceStatus.dwWaitHint = 0; SetServiceStatus(serviceHandle, &serviceStatus); MAIN_ZABBIX_ENTRY(); }
void service_start() { int ret; static SERVICE_TABLE_ENTRY serviceTable[2]; serviceTable[0].lpServiceName = zbx_utf8_to_unicode(ZABBIX_SERVICE_NAME); serviceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceEntry; serviceTable[1].lpServiceName = NULL; serviceTable[1].lpServiceProc = NULL; ret = StartServiceCtrlDispatcher(serviceTable); zbx_free(serviceTable[0].lpServiceName); if (0 == ret) { if (ERROR_FAILED_SERVICE_CONTROLLER_CONNECT == GetLastError()) { zbx_error("\n\n\t!!!ATTENTION!!! Zabbix Agent started as a console application. !!!ATTENTION!!!\n"); MAIN_ZABBIX_ENTRY(); } else zbx_error("StartServiceCtrlDispatcher() failed: %s", strerror_from_system(GetLastError())); } }
/****************************************************************************** * * * Function: daemon_start * * * * Purpose: init process as daemon * * * * Parameters: allow_root - allow root permission for application * * user - user on the system to which to drop the * * privileges * * * * Author: Alexei Vladishev * * * * Comments: it doesn't allow running under 'root' if allow_root is zero * * * ******************************************************************************/ int daemon_start(int allow_root, const char *user) { pid_t pid; struct passwd *pwd; if (0 == allow_root && 0 == getuid()) /* running as root? */ { if (NULL == user) user = "******"; pwd = getpwnam(user); if (NULL == pwd) { zbx_error("user %s does not exist", user); zbx_error("cannot run as root!"); exit(EXIT_FAILURE); } if (0 == pwd->pw_uid) { zbx_error("User=%s contradicts AllowRoot=0", user); zbx_error("cannot run as root!"); exit(EXIT_FAILURE); } if (-1 == setgid(pwd->pw_gid)) { zbx_error("cannot setgid to %s: %s", user, zbx_strerror(errno)); exit(EXIT_FAILURE); } #ifdef HAVE_FUNCTION_INITGROUPS if (-1 == initgroups(user, pwd->pw_gid)) { zbx_error("cannot initgroups to %s: %s", user, zbx_strerror(errno)); exit(EXIT_FAILURE); } #endif if (-1 == setuid(pwd->pw_uid)) { zbx_error("cannot setuid to %s: %s", user, zbx_strerror(errno)); exit(EXIT_FAILURE); } #ifdef HAVE_FUNCTION_SETEUID if (-1 == setegid(pwd->pw_gid) || -1 == seteuid(pwd->pw_uid)) { zbx_error("cannot setegid or seteuid to %s: %s", user, zbx_strerror(errno)); exit(EXIT_FAILURE); } #endif } if (0 != (pid = zbx_fork())) exit(EXIT_SUCCESS); setsid(); signal(SIGHUP, SIG_IGN); if (0 != (pid = zbx_fork())) exit(EXIT_SUCCESS); if (-1 == chdir("/")) /* this is to eliminate warning: ignoring return value of chdir */ assert(0); umask(0002); redirect_std(CONFIG_LOG_FILE); if (FAIL == create_pid_file(CONFIG_PID_FILE)) exit(EXIT_FAILURE); atexit(daemon_stop); parent_pid = (int)getpid(); zbx_set_common_signal_handlers(); set_daemon_signal_handlers(); /* Set SIGCHLD now to avoid race conditions when a child process is created before */ /* sigaction() is called. To avoid problems when scripts exit in zbx_execute() and */ /* other cases, SIGCHLD is set to SIG_DFL in zbx_child_fork(). */ zbx_set_child_signal_handler(); return MAIN_ZABBIX_ENTRY(); }
int daemon_start(int allow_root, const char* user) { pid_t pid; struct passwd *pwd; struct sigaction phan; /* running as root ?*/ if((0 == allow_root) && (0 == getuid() || 0 == getgid())) { pwd = getpwnam(user); if (NULL == pwd) { zbx_error("User %s does not exist.", user); zbx_error("Cannot run as root !"); exit(FAIL); } if(setgid(pwd->pw_gid) ==-1) { zbx_error("Cannot setgid to %s [%s].", user, strerror(errno)); exit(FAIL); } #ifdef HAVE_FUNCTION_INITGROUPS if(initgroups(user, pwd->pw_gid) == -1) { zbx_error("Cannot initgroups to %s [%s].", user, strerror(errno)); exit(FAIL); } #endif /* HAVE_FUNCTION_INITGROUPS */ if(setuid(pwd->pw_uid) == -1) { zbx_error("Cannot setuid to %s [%s].", user, strerror(errno)); exit(FAIL); } #ifdef HAVE_FUNCTION_SETEUID if( (setegid(pwd->pw_gid) ==-1) || (seteuid(pwd->pw_uid) == -1) ) { zbx_error("Cannot setegid or seteuid to zabbix [%s].", strerror(errno)); exit(FAIL); } #endif /* HAVE_FUNCTION_SETEUID */ } if( (pid = zbx_fork()) != 0 ) { exit( 0 ); } setsid(); signal( SIGHUP, SIG_IGN ); if( (pid = zbx_fork()) !=0 ) { exit( 0 ); } /* This is to eliminate warning: ignoring return value of chdir */ if(-1 == chdir("/")) { assert(0); } umask(0002); redirect_std(CONFIG_LOG_FILE); #ifdef HAVE_SYS_RESOURCE_SETPRIORITY if(setpriority(PRIO_PROCESS,0,5)!=0) { zbx_error("Unable to set process priority to 5. Leaving default."); } #endif /* HAVE_SYS_RESOURCE_SETPRIORITY */ /*------------------------------------------------*/ if( FAIL == create_pid_file(APP_PID_FILE)) { exit(FAIL); } phan.sa_handler = child_signal_handler; sigemptyset(&phan.sa_mask); phan.sa_flags = 0; sigaction(SIGINT, &phan, NULL); sigaction(SIGQUIT, &phan, NULL); sigaction(SIGTERM, &phan, NULL); sigaction(SIGPIPE, &phan, NULL); zbx_setproctitle("main process"); return MAIN_ZABBIX_ENTRY(); }
/****************************************************************************** * * * Function: daemon_start * * * * Purpose: init process as daemon * * * * Parameters: allow_root - allow root permission for application * * * * Author: Alexei Vladishev * * * * Comments: it doesn't allow running under 'root' if allow_root is zero * * * ******************************************************************************/ int daemon_start(int allow_root) { pid_t pid; struct passwd *pwd; struct sigaction phan; char user[7] = "zabbix"; /* running as root ? */ if (0 == allow_root && (0 == getuid() || 0 == getgid())) { pwd = getpwnam(user); if (NULL == pwd) { zbx_error("user %s does not exist", user); zbx_error("Cannot run as root!"); exit(FAIL); } if (-1 == setgid(pwd->pw_gid)) { zbx_error("cannot setgid to %s: %s", user, zbx_strerror(errno)); exit(FAIL); } #ifdef HAVE_FUNCTION_INITGROUPS if (-1 == initgroups(user, pwd->pw_gid)) { zbx_error("cannot initgroups to %s: %s", user, zbx_strerror(errno)); exit(FAIL); } #endif if (-1 == setuid(pwd->pw_uid)) { zbx_error("cannot setuid to %s: %s", user, zbx_strerror(errno)); exit(FAIL); } #ifdef HAVE_FUNCTION_SETEUID if (-1 == setegid(pwd->pw_gid) || -1 == seteuid(pwd->pw_uid)) { zbx_error("cannot setegid or seteuid to %s: %s", user, zbx_strerror(errno)); exit(FAIL); } #endif } if (0 != (pid = zbx_fork())) exit(0); setsid(); signal(SIGHUP, SIG_IGN); if (0 != (pid = zbx_fork())) exit(0); /* this is to eliminate warning: ignoring return value of chdir */ if (-1 == chdir("/")) assert(0); umask(0002); redirect_std(CONFIG_LOG_FILE); #ifdef HAVE_SYS_RESOURCE_SETPRIORITY if (0 != setpriority(PRIO_PROCESS, 0, 5)) zbx_error("Unable to set process priority to 5. Leaving default."); #endif /*------------------------------------------------*/ if (FAIL == create_pid_file(CONFIG_PID_FILE)) exit(FAIL); parent_pid = (int)getpid(); phan.sa_sigaction = child_signal_handler; sigemptyset(&phan.sa_mask); phan.sa_flags = SA_SIGINFO; sigaction(SIGINT, &phan, NULL); sigaction(SIGQUIT, &phan, NULL); sigaction(SIGTERM, &phan, NULL); sigaction(SIGPIPE, &phan, NULL); sigaction(SIGILL, &phan, NULL); sigaction(SIGFPE, &phan, NULL); sigaction(SIGSEGV, &phan, NULL); sigaction(SIGBUS, &phan, NULL); sigaction(SIGALRM, &phan, NULL); sigaction(SIGUSR1, &phan, NULL); /* Set SIGCHLD now to avoid race conditions when a child process is created before */ /* sigaction() is called. To avoid problems when scripts exit in zbx_execute() and */ /* other cases, SIGCHLD is set to SIG_IGN in zbx_child_fork(). */ phan.sa_sigaction = parent_signal_handler; sigaction(SIGCHLD, &phan, NULL); zbx_setproctitle("main process"); return MAIN_ZABBIX_ENTRY(); }