Imported Upstream version 2.3
[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.3"
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
115 # Start function
116 start()
117 {
118     SDAEMONS="ossec-execd ossec-agentd ossec-logcollector ossec-syscheckd"
119     
120     echo "Starting $NAME $VERSION (by $AUTHOR)..."
121     lock;
122     checkpid;
123
124     # We first loop to check the config. 
125     for i in ${SDAEMONS}; do
126         ${DIR}/bin/${i} -t;
127         if [ $? != 0 ]; then
128             echo "${i}: Configuration error. Exiting"
129             unlock;
130             exit 1;
131         fi    
132     done
133     
134     # We actually start them now.
135     for i in ${SDAEMONS}; do
136         pstatus ${i};
137         if [ $? = 0 ]; then
138             ${DIR}/bin/${i};
139             if [ $? != 0 ]; then
140                 unlock;
141                 exit 1;
142             fi 
143
144             echo "Started ${i}..."            
145         else
146             echo "${i} already running..."                
147         fi    
148     
149     done    
150
151     # After we start we give 2 seconds for the daemons
152     # to internally create their PID files.
153     sleep 2;
154     unlock;
155     echo "Completed."
156 }
157
158 # Process status
159 pstatus()
160 {
161     pfile=$1;
162     
163     # pfile must be set
164     if [ "X${pfile}" = "X" ]; then
165         return 0;
166     fi
167         
168     ls ${DIR}/var/run/${pfile}*.pid > /dev/null 2>&1
169     if [ $? = 0 ]; then
170         for j in `cat ${DIR}/var/run/${pfile}*.pid 2>/dev/null`; do
171             ps -p $j |grep ossec >/dev/null 2>&1
172             if [ ! $? = 0 ]; then
173                 echo "${pfile}: Process $j not used by ossec, removing .."
174                 rm -f ${DIR}/var/run/${pfile}-$j.pid
175                 continue;
176             fi
177                 
178             kill -0 $j > /dev/null 2>&1
179             if [ $? = 0 ]; then
180                 return 1;
181             fi    
182         done    
183     fi
184     
185     return 0;    
186 }
187
188
189 # Stop all
190 stopa()
191 {
192     lock;
193     checkpid;
194     for i in ${DAEMONS}; do
195         pstatus ${i};
196         if [ $? = 1 ]; then
197             echo "Killing ${i} .. ";
198             
199             kill `cat ${DIR}/var/run/${i}*.pid`;
200         else
201             echo "${i} not running .."; 
202         fi
203         
204         rm -f ${DIR}/var/run/${i}*.pid
205         
206      done    
207     
208     unlock;
209     echo "$NAME $VERSION Stopped"
210 }
211
212
213 ### MAIN HERE ###
214
215 case "$1" in
216   start)
217         start
218         ;;
219   stop) 
220         stopa
221         ;;
222   restart)
223         stopa
224         start
225         ;;
226   status)
227     status
228         ;;
229   help)  
230     help
231     ;;
232   *)
233     help
234 esac