Commit 7c51589c authored by nextime's avatar nextime

Refactor init scripts to start daemons directly before watchdog

- Init scripts now start wssshd/wssshc daemons directly
- Wait for daemons to be fully running before starting watchdog
- Watchdog only monitors existing daemons, doesn't start them
- Watchdog exits if daemon restart fails
- Proper startup sequence: daemon → verify running → watchdog
- Fixed both wssshd and wssshc init scripts and watchdogs
parent fa858590
...@@ -242,6 +242,9 @@ main() { ...@@ -242,6 +242,9 @@ main() {
sleep 3 sleep 3
else else
log_message "Failed to restart $DAEMON_NAME" log_message "Failed to restart $DAEMON_NAME"
# If daemon fails to start, exit watchdog
log_message "Watchdog exiting due to daemon restart failure"
break
fi fi
fi fi
...@@ -262,8 +265,8 @@ case "$1" in ...@@ -262,8 +265,8 @@ case "$1" in
fi fi
echo "Calling main() function..." echo "Calling main() function..."
# Properly daemonize the main() function # Run main() function in background
setsid bash -c 'main' & main &
# Give it a moment to start # Give it a moment to start
sleep 1 sleep 1
......
...@@ -104,16 +104,11 @@ is_running() { ...@@ -104,16 +104,11 @@ is_running() {
start() { start() {
echo -n "Starting $NAME: " echo -n "Starting $NAME: "
# Check if watchdog is already running # Check if daemon is already running
if [ -f "$WATCHDOG_PIDFILE" ]; then if is_running; then
local watchdog_pid=$(cat "$WATCHDOG_PIDFILE") local pid=$(cat "$PIDFILE")
if kill -0 "$watchdog_pid" 2>/dev/null; then echo "already running (PID: $pid)"
echo "already running (watchdog PID: $watchdog_pid)" return 0
return 0
else
# Stale watchdog PID file
rm -f "$WATCHDOG_PIDFILE"
fi
fi fi
# Check if START is enabled # Check if START is enabled
...@@ -141,37 +136,55 @@ start() { ...@@ -141,37 +136,55 @@ start() {
return 1 return 1
fi fi
# Check if watchdog script exists
if [ ! -x "$WATCHDOG" ]; then
echo "watchdog script $WATCHDOG not found or not executable"
return 1
fi
# Create necessary directories # Create necessary directories
mkdir -p /var/run /var/log/wssshc mkdir -p /var/run /var/log/wssshc
chown $USER:$GROUP /var/run /var/log/wssshc 2>/dev/null || true chown $USER:$GROUP /var/run /var/log/wssshc 2>/dev/null || true
# Start the watchdog (which will start and monitor the daemon) # Start the daemon directly
$WATCHDOG start >/dev/null 2>&1 echo "Starting daemon..."
if "$DAEMON" &
local retval=$? then
echo $! > "$PIDFILE"
echo "Daemon started successfully"
else
echo "FAILED"
echo "Could not start daemon"
return 1
fi
if [ $retval -eq 0 ]; then # Wait for daemon to be fully running
# Wait a moment for the watchdog to start (reduced since watchdog now waits for PID file) local count=0
while [ $count -lt 10 ] && ! is_running; do
echo "Waiting for daemon to start... ($count/10)"
sleep 1 sleep 1
count=$((count + 1))
done
# Check if watchdog is running if is_running; then
if [ -f "$WATCHDOG_PIDFILE" ] && kill -0 "$(cat "$WATCHDOG_PIDFILE")" 2>/dev/null; then local pid=$(cat "$PIDFILE")
echo "OK" echo "Daemon is running (PID: $pid)"
return 0
# Now start the watchdog to monitor the running daemon
echo "Starting watchdog..."
if [ -x "$WATCHDOG" ]; then
$WATCHDOG start >/dev/null 2>&1
sleep 2
if [ -f "$WATCHDOG_PIDFILE" ] && kill -0 "$(cat "$WATCHDOG_PIDFILE")" 2>/dev/null; then
echo "OK"
return 0
else
echo "OK (daemon running, watchdog failed)"
return 0
fi
else else
echo "FAILED" echo "OK (daemon running, no watchdog)"
echo "Watchdog failed to start properly" return 0
return 1
fi fi
else else
echo "FAILED" echo "FAILED"
return $retval echo "Daemon failed to start properly"
return 1
fi fi
} }
......
...@@ -57,16 +57,11 @@ is_running() { ...@@ -57,16 +57,11 @@ is_running() {
start() { start() {
echo -n "Starting $NAME: " echo -n "Starting $NAME: "
# Check if watchdog is already running # Check if daemon is already running
if [ -f "$WATCHDOG_PIDFILE" ]; then if is_running; then
local watchdog_pid=$(cat "$WATCHDOG_PIDFILE") local pid=$(cat "$PIDFILE")
if kill -0 "$watchdog_pid" 2>/dev/null; then echo "already running (PID: $pid)"
echo "already running (watchdog PID: $watchdog_pid)" return 0
return 0
else
# Stale watchdog PID file
rm -f "$WATCHDOG_PIDFILE"
fi
fi fi
# Check if config file exists # Check if config file exists
...@@ -84,37 +79,62 @@ start() { ...@@ -84,37 +79,62 @@ start() {
return 1 return 1
fi fi
# Check if watchdog script exists
if [ ! -x "$WATCHDOG" ]; then
echo "watchdog script $WATCHDOG not found or not executable"
return 1
fi
# Create necessary directories # Create necessary directories
mkdir -p /var/run /var/log/wssshd mkdir -p /var/run /var/log/wssshd
chown $USER:$GROUP /var/run /var/log/wssshd 2>/dev/null || true chown $USER:$GROUP /var/run /var/log/wssshd 2>/dev/null || true
# Start the watchdog (which will start and monitor the daemon) # Start the daemon directly
$WATCHDOG start >/dev/null 2>&1 echo "Starting daemon..."
if start-stop-daemon --start --quiet --pidfile "$PIDFILE" \
local retval=$? --chuid $USER:$GROUP --background --make-pidfile \
--exec "$DAEMON" 2>/dev/null; then
echo "Daemon started successfully"
else
echo "start-stop-daemon failed, trying direct execution..."
if "$DAEMON" &
then
echo $! > "$PIDFILE"
echo "Daemon started successfully (direct execution)"
else
echo "FAILED"
echo "Could not start daemon"
return 1
fi
fi
if [ $retval -eq 0 ]; then # Wait for daemon to be fully running
# Wait a moment for the watchdog to start (reduced since watchdog now waits for PID file) local count=0
while [ $count -lt 10 ] && ! is_running; do
echo "Waiting for daemon to start... ($count/10)"
sleep 1 sleep 1
count=$((count + 1))
done
# Check if watchdog is running if is_running; then
if [ -f "$WATCHDOG_PIDFILE" ] && kill -0 "$(cat "$WATCHDOG_PIDFILE")" 2>/dev/null; then local pid=$(cat "$PIDFILE")
echo "OK" echo "Daemon is running (PID: $pid)"
return 0
# Now start the watchdog to monitor the running daemon
echo "Starting watchdog..."
if [ -x "$WATCHDOG" ]; then
$WATCHDOG start >/dev/null 2>&1
sleep 2
if [ -f "$WATCHDOG_PIDFILE" ] && kill -0 "$(cat "$WATCHDOG_PIDFILE")" 2>/dev/null; then
echo "OK"
return 0
else
echo "OK (daemon running, watchdog failed)"
return 0
fi
else else
echo "FAILED" echo "OK (daemon running, no watchdog)"
echo "Watchdog failed to start properly" return 0
return 1
fi fi
else else
echo "FAILED" echo "FAILED"
return $retval echo "Daemon failed to start properly"
return 1
fi fi
} }
......
...@@ -237,6 +237,9 @@ main() { ...@@ -237,6 +237,9 @@ main() {
sleep 3 sleep 3
else else
log_message "Failed to restart $DAEMON_NAME" log_message "Failed to restart $DAEMON_NAME"
# If daemon fails to start, exit watchdog
log_message "Watchdog exiting due to daemon restart failure"
break
fi fi
fi fi
...@@ -257,8 +260,8 @@ case "$1" in ...@@ -257,8 +260,8 @@ case "$1" in
fi fi
echo "Calling main() function..." echo "Calling main() function..."
# Properly daemonize the main() function # Run main() function in background
setsid bash -c 'main' & main &
# Give it a moment to start # Give it a moment to start
sleep 1 sleep 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment