Imported Upstream version 2.7
[ossec-hids.git] / src / init / ossec-client.sh
1 #!/bin/sh
2 # ossec-control        This shell script takes care of starting
3 #                      or stopping ossec-hids
4 # Author: Daniel B. Cid <daniel.cid@gmail.com>
5
6
7 LOCAL=`dirname $0`;
8 cd ${LOCAL}
9 PWD=`pwd`
10 DIR=`dirname $PWD`;
11
12 ###  Do not modify bellow here ###
13 NAME="OSSEC HIDS"
14 VERSION="v2.7"
15 AUTHOR="Trend Micro Inc."
16 DAEMONS="ossec-logcollector ossec-syscheckd ossec-agentd ossec-execd"
17
18
19 ## Locking for the start/stop
20 LOCK="${DIR}/var/start-script-lock"
21 LOCK_PID="${LOCK}/pid"
22
23
24 # This number should be more than enough (even if it is
25 # started multiple times together). It will try for up
26 # to 10 attempts (or 10 seconds) to execute.
27 MAX_ITERATION="10"
28
29
30
31 # Check pid
32 checkpid()
33 {
34     for i in ${DAEMONS}; do
35         for j in `cat ${DIR}/var/run/${i}*.pid 2>/dev/null`; do
36             ps -p $j |grep ossec >/dev/null 2>&1
37             if [ ! $? = 0 ]; then
38                 echo "Deleting PID file '${DIR}/var/run/${i}-${j}.pid' not used..."
39                 rm ${DIR}/var/run/${i}-${j}.pid
40             fi    
41         done    
42     done    
43 }
44
45
46
47 # Lock function
48 lock()
49 {
50     i=0;
51     
52     # Providing a lock.
53     while [ 1 ]; do
54         mkdir ${LOCK} > /dev/null 2>&1
55         MSL=$?
56         if [ "${MSL}" = "0" ]; then
57             # Lock aquired (setting the pid)
58             echo "$$" > ${LOCK_PID}
59             return;
60         fi
61
62         # Waiting 1 second before trying again
63         sleep 1;
64         i=`expr $i + 1`;
65
66         # If PID is not present, speed things a bit.
67         kill -0 `cat ${LOCK_PID}` >/dev/null 2>&1
68         if [ ! $? = 0 ]; then
69             # Pid is not present.
70             i=`expr $i + 1`;
71         fi    
72
73         # We tried 10 times to acquire the lock.
74         if [ "$i" = "${MAX_ITERATION}" ]; then
75             # Unlocking and executing
76             unlock;
77             mkdir ${LOCK} > /dev/null 2>&1
78             echo "$$" > ${LOCK_PID}
79             return;
80         fi
81     done
82 }
83
84
85 # Unlock function
86 unlock()
87 {
88     rm -rf ${LOCK}
89 }
90
91     
92 # Help message
93 help()
94 {
95     # Help message
96     echo "Usage: $0 {start|stop|restart|status}";
97     exit 1;
98 }
99
100
101 # Status function
102 status()
103 {
104     for i in ${DAEMONS}; do
105         pstatus ${i};
106         if [ $? = 0 ]; then
107             echo "${i} not running..."
108         else
109             echo "${i} is running..."
110         fi
111     done             
112 }
113
114 testconfig()
115 {
116     # We first loop to check the config. 
117     for i in ${SDAEMONS}; do
118         ${DIR}/bin/${i} -t;
119         if [ $? != 0 ]; then
120             echo "${i}: Configuration error. Exiting"
121             unlock;
122             exit 1;
123         fi    
124     done
125 }
126
127 # Start function
128 start()
129 {
130     SDAEMONS="ossec-execd ossec-agentd ossec-logcollector ossec-syscheckd"
131     
132     echo "Starting $NAME $VERSION (by $AUTHOR)..."
133     lock;
134     checkpid;
135
136     
137     # We actually start them now.
138     for i in ${SDAEMONS}; do
139         pstatus ${i};
140         if [ $? = 0 ]; then
141             ${DIR}/bin/${i};
142             if [ $? != 0 ]; then
143                 unlock;
144                 exit 1;
145             fi 
146
147             echo "Started ${i}..."            
148         else
149             echo "${i} already running..."                
150         fi    
151     
152     done    
153
154     # After we start we give 2 seconds for the daemons
155     # to internally create their PID files.
156     sleep 2;
157     unlock;
158     echo "Completed."
159 }
160
161 # Process status
162 pstatus()
163 {
164     pfile=$1;
165     
166     # pfile must be set
167     if [ "X${pfile}" = "X" ]; then
168         return 0;
169     fi
170         
171     ls ${DIR}/var/run/${pfile}*.pid > /dev/null 2>&1
172     if [ $? = 0 ]; then
173         for j in `cat ${DIR}/var/run/${pfile}*.pid 2>/dev/null`; do
174             ps -p $j |grep ossec >/dev/null 2>&1
175             if [ ! $? = 0 ]; then
176                 echo "${pfile}: Process $j not used by ossec, removing .."
177                 rm -f ${DIR}/var/run/${pfile}-$j.pid
178                 continue;
179             fi
180                 
181             kill -0 $j > /dev/null 2>&1
182             if [ $? = 0 ]; then
183                 return 1;
184             fi    
185         done    
186     fi
187     
188     return 0;    
189 }
190
191
192 # Stop all
193 stopa()
194 {
195     lock;
196     checkpid;
197     for i in ${DAEMONS}; do
198         pstatus ${i};
199         if [ $? = 1 ]; then
200             echo "Killing ${i} .. ";
201             
202             kill `cat ${DIR}/var/run/${i}*.pid`;
203         else
204             echo "${i} not running .."; 
205         fi
206         
207         rm -f ${DIR}/var/run/${i}*.pid
208         
209      done    
210     
211     unlock;
212     echo "$NAME $VERSION Stopped"
213 }
214
215
216 ### MAIN HERE ###
217
218 case "$1" in
219   start)
220     testconfig
221         start
222         ;;
223   stop) 
224         stopa
225         ;;
226   restart)
227     testconfig
228         stopa
229         sleep 1;
230         start
231         ;;
232   reload)
233         DAEMONS="ossec-logcollector ossec-syscheckd ossec-agentd"
234         stopa
235         start
236         ;;
237   status)
238     status
239         ;;
240   help)  
241     help
242     ;;
243   *)
244     help
245 esac
246