~ubuntu-dev/ubuntu/lucid/zabbix/lucid-201002110857

« back to all changes in this revision

Viewing changes to src/libs/zbxnix/daemon.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Ablassmeier
  • Date: 2007-07-02 09:06:51 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070702090651-8l6fl3fjw9rh6l2u
Tags: 1:1.4.1-2
Add patch from SVN in order to fix Incorrect processing of character '%'
in user parameters and remote commands.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
** ZABBIX
 
3
** Copyright (C) 2000-2005 SIA Zabbix
 
4
**
 
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.
 
9
**
 
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.
 
14
**
 
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.
 
18
**/
 
19
 
 
20
#include "common.h"
 
21
#include "daemon.h"
 
22
 
 
23
#include "mutexs.h"
 
24
#include "pid.h"
 
25
#include "cfg.h"
 
26
#include "log.h"
 
27
 
 
28
char    *APP_PID_FILE   = NULL;
 
29
 
 
30
static int      parent = 0;
 
31
 
 
32
#define uninit() { if(parent == 1) zbx_on_exit(); }
 
33
 
 
34
void    child_signal_handler(int sig)
 
35
{
 
36
        switch(sig)
 
37
        {
 
38
        case SIGALRM:
 
39
                signal(SIGALRM , child_signal_handler);
 
40
                zabbix_log( LOG_LEVEL_WARNING, "Timeout while answering request");
 
41
                break;
 
42
        case SIGQUIT:
 
43
        case SIGINT:
 
44
        case SIGTERM:
 
45
                zabbix_log( LOG_LEVEL_DEBUG, "Got signal. Exiting ...");
 
46
                uninit();
 
47
                exit( FAIL );
 
48
                break;
 
49
        case SIGPIPE:
 
50
                zabbix_log( LOG_LEVEL_WARNING, "Got SIGPIPE. Where it came from???");
 
51
                break;
 
52
        default:
 
53
                zabbix_log( LOG_LEVEL_WARNING, "Got signal [%d]. Ignoring ...", sig);
 
54
        }
 
55
}
 
56
 
 
57
static void     parent_signal_handler(int sig)
 
58
{
 
59
        switch(sig)
 
60
        {
 
61
        case SIGCHLD:
 
62
                zabbix_log( LOG_LEVEL_WARNING, "One child process died. Exiting ...");
 
63
                uninit();
 
64
                exit( FAIL );
 
65
                break;
 
66
        default:
 
67
                child_signal_handler(sig);
 
68
        }
 
69
}
 
70
 
 
71
 
 
72
/******************************************************************************
 
73
 *                                                                            *
 
74
 * Function: daemon_start                                                     *
 
75
 *                                                                            *
 
76
 * Purpose: init process as daemon                                            *
 
77
 *                                                                            *
 
78
 * Parameters: allow_root - allow root permision for application              *
 
79
 *                                                                            *
 
80
 * Return value:                                                              *
 
81
 *                                                                            *
 
82
 * Author: Alexei Vladishev                                                   *
 
83
 *                                                                            *
 
84
 * Comments: it doesn't allow running under 'root' if allow_root is zerro     *
 
85
 *                                                                            *
 
86
 ******************************************************************************/
 
87
 
 
88
int     daemon_start(int allow_root)
 
89
{
 
90
        int                     i;
 
91
        pid_t                   pid;
 
92
        struct passwd           *pwd;
 
93
        struct sigaction        phan;
 
94
 
 
95
        /* running as root ?*/
 
96
        if((0 == allow_root) && (0 == getuid() || 0 == getgid()))
 
97
        {
 
98
                pwd = getpwnam("zabbix");
 
99
                if (NULL == pwd)
 
100
                {
 
101
                        zbx_error("User zabbix does not exist.");
 
102
                        zbx_error("Cannot run as root !");
 
103
                        exit(FAIL);
 
104
                }
 
105
                if( (setgid(pwd->pw_gid) ==-1) || (setuid(pwd->pw_uid) == -1) )
 
106
                {
 
107
                        zbx_error("Cannot setgid or setuid to zabbix [%s].", strerror(errno));
 
108
                        exit(FAIL);
 
109
                }
 
110
 
 
111
#ifdef HAVE_FUNCTION_SETEUID
 
112
                if( (setegid(pwd->pw_gid) ==-1) || (seteuid(pwd->pw_uid) == -1) )
 
113
                {
 
114
                        zbx_error("Cannot setegid or seteuid to zabbix [%s].", strerror(errno));
 
115
                        exit(FAIL);
 
116
                }
 
117
#endif /* HAVE_FUNCTION_SETEUID */
 
118
 
 
119
        }
 
120
 
 
121
        if( (pid = zbx_fork()) != 0 )   
 
122
        {                               
 
123
                exit( 0 );              
 
124
        }                               
 
125
 
 
126
        setsid();
 
127
        
 
128
        signal( SIGHUP, SIG_IGN );
 
129
 
 
130
        if( (pid = zbx_fork()) !=0 )    
 
131
        {                               
 
132
                exit( 0 );              
 
133
        }                               
 
134
 
 
135
        chdir("/");
 
136
        umask(0002);
 
137
 
 
138
        for(i=0; i<MAXFD; i++)  close(i);
 
139
 
 
140
        redirect_std(CONFIG_LOG_FILE);
 
141
 
 
142
#ifdef HAVE_SYS_RESOURCE_SETPRIORITY
 
143
 
 
144
        if(setpriority(PRIO_PROCESS,0,5)!=0)
 
145
        {
 
146
                zbx_error("Unable to set process priority to 5. Leaving default.");
 
147
        }
 
148
 
 
149
#endif /* HAVE_SYS_RESOURCE_SETPRIORITY */
 
150
 
 
151
/*------------------------------------------------*/
 
152
 
 
153
        if( FAIL == create_pid_file(APP_PID_FILE))
 
154
        {
 
155
                exit(FAIL);
 
156
        }
 
157
 
 
158
        phan.sa_handler = child_signal_handler;
 
159
        sigemptyset(&phan.sa_mask);
 
160
        phan.sa_flags = 0;
 
161
 
 
162
        sigaction(SIGINT,       &phan, NULL);
 
163
        sigaction(SIGQUIT,      &phan, NULL);
 
164
        sigaction(SIGTERM,      &phan, NULL);
 
165
        sigaction(SIGPIPE,      &phan, NULL);
 
166
 
 
167
        zbx_setproctitle("main process");
 
168
 
 
169
        return MAIN_ZABBIX_ENTRY();
 
170
}
 
171
 
 
172
void    daemon_stop(void)
 
173
{       
 
174
        drop_pid_file(APP_PID_FILE);
 
175
}
 
176
 
 
177
void    init_main_process(void)
 
178
{
 
179
        struct sigaction        phan;
 
180
        
 
181
        parent = 1; /* signalize signal_handler what this process isi a PARENT process */
 
182
        
 
183
        phan.sa_handler = parent_signal_handler;
 
184
        sigemptyset(&phan.sa_mask);
 
185
        phan.sa_flags = 0;
 
186
 
 
187
        /* For parent only. To avoid problems with EXECUTE_INT */
 
188
        sigaction(SIGCHLD,      &phan, NULL);
 
189
}
 
190