3
** Copyright (C) 2000-2005 SIA Zabbix
5
** This program is free software; you can redistribute it and/or modify
6
** it under the terms of the GNU General Public License as published by
7
** the Free Software Foundation; either version 2 of the License, or
8
** (at your option) any later version.
10
** This program is distributed in the hope that it will be useful,
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
** GNU General Public License for more details.
15
** You should have received a copy of the GNU General Public License
16
** along with this program; if not, write to the Free Software
17
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28
char *APP_PID_FILE = NULL;
30
static int parent = 0;
32
#define uninit() { if(parent == 1) zbx_on_exit(); }
34
void child_signal_handler(int sig)
39
signal(SIGALRM , child_signal_handler);
40
zabbix_log( LOG_LEVEL_WARNING, "Timeout while answering request");
45
zabbix_log( LOG_LEVEL_DEBUG, "Got signal. Exiting ...");
50
zabbix_log( LOG_LEVEL_WARNING, "Got SIGPIPE. Where it came from???");
53
zabbix_log( LOG_LEVEL_WARNING, "Got signal [%d]. Ignoring ...", sig);
57
static void parent_signal_handler(int sig)
62
zabbix_log( LOG_LEVEL_WARNING, "One child process died. Exiting ...");
67
child_signal_handler(sig);
72
/******************************************************************************
74
* Function: daemon_start *
76
* Purpose: init process as daemon *
78
* Parameters: allow_root - allow root permision for application *
82
* Author: Alexei Vladishev *
84
* Comments: it doesn't allow running under 'root' if allow_root is zerro *
86
******************************************************************************/
88
int daemon_start(int allow_root)
93
struct sigaction phan;
95
/* running as root ?*/
96
if((0 == allow_root) && (0 == getuid() || 0 == getgid()))
98
pwd = getpwnam("zabbix");
101
zbx_error("User zabbix does not exist.");
102
zbx_error("Cannot run as root !");
105
if( (setgid(pwd->pw_gid) ==-1) || (setuid(pwd->pw_uid) == -1) )
107
zbx_error("Cannot setgid or setuid to zabbix [%s].", strerror(errno));
111
#ifdef HAVE_FUNCTION_SETEUID
112
if( (setegid(pwd->pw_gid) ==-1) || (seteuid(pwd->pw_uid) == -1) )
114
zbx_error("Cannot setegid or seteuid to zabbix [%s].", strerror(errno));
117
#endif /* HAVE_FUNCTION_SETEUID */
121
if( (pid = zbx_fork()) != 0 )
128
signal( SIGHUP, SIG_IGN );
130
if( (pid = zbx_fork()) !=0 )
138
for(i=0; i<MAXFD; i++) close(i);
140
redirect_std(CONFIG_LOG_FILE);
142
#ifdef HAVE_SYS_RESOURCE_SETPRIORITY
144
if(setpriority(PRIO_PROCESS,0,5)!=0)
146
zbx_error("Unable to set process priority to 5. Leaving default.");
149
#endif /* HAVE_SYS_RESOURCE_SETPRIORITY */
151
/*------------------------------------------------*/
153
if( FAIL == create_pid_file(APP_PID_FILE))
158
phan.sa_handler = child_signal_handler;
159
sigemptyset(&phan.sa_mask);
162
sigaction(SIGINT, &phan, NULL);
163
sigaction(SIGQUIT, &phan, NULL);
164
sigaction(SIGTERM, &phan, NULL);
165
sigaction(SIGPIPE, &phan, NULL);
167
zbx_setproctitle("main process");
169
return MAIN_ZABBIX_ENTRY();
172
void daemon_stop(void)
174
drop_pid_file(APP_PID_FILE);
177
void init_main_process(void)
179
struct sigaction phan;
181
parent = 1; /* signalize signal_handler what this process isi a PARENT process */
183
phan.sa_handler = parent_signal_handler;
184
sigemptyset(&phan.sa_mask);
187
/* For parent only. To avoid problems with EXECUTE_INT */
188
sigaction(SIGCHLD, &phan, NULL);