#!/bin/bash

## Copyright (C) 2012 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

exit_function() {
   printf '%s\n' ""
   printf '%s\n' "$0: output of '/usr/bin/systemctl --no-pager --no-block status tor@default':"
   printf '%s\n' ""
   /usr/bin/systemctl --no-pager --no-block status tor@default
   exit "$exit_code"
}

if [ ! -f "/run/tor/tor.pid" ]; then
   printf '%s\n' "$0: Pid file '/run/tor/tor.pid' does not exist."
   exit_code=1
   exit_function
fi

## override with || true to avoid race condition.
pid="$(cat -- "/run/tor/tor.pid")" || true

if [ "$pid" = "" ]; then
   printf '%s\n' "$0: Pid file '/run/tor/tor.pid' is empty."
   exit_code=2
   exit_function
fi

if ! kill -0 -- "$pid"; then
   printf '%s\n' "$0: Pid is not running."
   exit_code=3
   exit_function
fi

printf '%s\n' "$0: Pid running."
