#!/bin/bash
#-------------------------------------------------------------------------------
# charon_oomprotect
#-------------------------------------------------------------------------------
# Script is running within crontab that periodically protects Charon processes
# related to license management
#-------------------------------------------------------------------------------
# Copyright (C) 2013-2023 STROMASYS.
# All rights reserved.
# Products: AXP/VAX + PAR
#
VERSION=2.0

for P in $(ls /etc/profile.d/charon*);do . $P;done
. $(dirname $0)/charon_common

get_preferences
DEBUG=0
test "$1" = "--debug" && DEBUG=1

if test "${OOMPRVPIDKILL}" = "enabled"
then
  for EXE in /usr/sbin/aksusbd /usr/sbin/hasplmd /opt/license-server/license_server
  do
    test ${DEBUG} -eq 1 && echo "EXE = ${EXE}"
    for P in $(pgrep -f ${EXE})
    do
      test ${DEBUG} -eq 1 && echo "  PID = ${P} (score=$(cat /proc/${P}/oom_score_adj))"
      echo -1000 > /proc/${P}/oom_score_adj
    done
  done
else
  test ${DEBUG} -eq 1 && echo "Protection against 'OOM Killer' is disabled."
fi

exit
