#!/bin/bash
# We should be passed the PID
if [[ "$#" -ne 1 ]]; then
  echo "Usage: $0 <PID>" >&2
  exit 1
fi
pid=$1
# Make sure people are not messing with command input, check if we can signal the PID
if ! kill -0 $pid > /dev/null 2>&1; then
   echo "PID $pid is not a valid PID"
   exit 1
fi
if stat -c %U /tmp/hypernode_oom_protect$pid | grep -q '^app$'; then
  echo -1000 > /proc/self/oom_score_adj && exec /bin/su --login -c "/tmp/hypernode_oom_protect$pid" app
else
  logger -i --tag hypernode_oom_protect --stderr --priority local0.err "/tmp/hypernode_oom_protect$pid is modified by something other than kamikaze tool, exiting"
fi
