#!/bin/bash

## Copyright (C) 2012 - 2020 ENCRYPTED SUPPORT LP <adrelanos@riseup.net>
## See the file COPYING for copying conditions.

if [ ! -f "/run/tor/tor.pid" ]; then
   echo "Pid file /run/tor/tor.pid does not exist."
   exit 1
fi

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

if [ "$pid" = "" ]; then
   echo "Pid file /run/tor/tor.pid is empty."
   exit 2
fi

ps_p_exit_code="0"
ps -p "$pid" >/dev/null 2>/dev/null || { ps_p_exit_code="$?" ; true; };

if [ ! "$ps_p_exit_code" = "0" ]; then
   echo "Pid $pid is not running."
   exit 3
fi

echo "Pid $pid running."
exit 0
