#!/bin/bash # -------------- # llxcfg-policy # -------------- # # ---------------------------------------------------------- # Lliurex Policy tool # Sept 2006. Luis Garcia for LliureX # ---------------------------------------------------------- # # ------------------------------------------------------------------------- # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA # ------------------------------------------------------------------------- # set -e # ---------- # VARIABLES # ---------- POLICY_LIB="/usr/share/lliurex/llxcfg-runtime/llxcfg-policyfuncs.sh" [ -e ${POLICY_LIB} ] || error_msg "ERROR: File \"${POLICY_LIB}\" not found" . ${POLICY_LIB} # ---------- # FUNCTIONS # ---------- verbose_script_test(){ [ -z "${1}" ] && usage [ -x /etc/init.d/$1 ] || error_msg "ERROR: Unknown initscript \"${1}\"" return 0 } quiet_script_test(){ [ -z "${1}" ] && return 1 [ -x /etc/init.d/$1 ] || return 1 return 0 } get_rc_info(){ local RL SCRIPT_NAME SCRIPT_NAME="$1" for RL in 0 1 2 3 4 5 6 S ; do find_link ${SCRIPT_NAME} ${RL} done return 0 } find_link(){ local RUN_LEVEL SCRIPT_NAME LINK_NAME BASE_NAME NN ACTION SCRIPT_NAME="$1" RUN_LEVEL="$2" LINK_NAME="`find /etc/rc${RUN_LEVEL}.d/ -lname "../init.d/${SCRIPT_NAME}"`" [ -z "${LINK_NAME}" ] && return 0 BASE_NAME="`basename ${LINK_NAME}`" NN="`echo ${BASE_NAME} |sed -ne 's%.\{1\}\(.\{2\}\).*%\1%p'`" ACTION="`echo ${BASE_NAME} |sed -ne 's%\(.\{1\}\).*%\1%p'`" case "${ACTION}" in S) echo -n "start ${NN} ${RUN_LEVEL} . " ;; K) echo -n "stop ${NN} ${RUN_LEVEL} . " ;; esac return 0 } enable_script(){ local SCRIPT_NAME SCRIPT_NAME="$1" llxpol_test_active_script "${SCRIPT_NAME}" && return 0 read RC_INFO < ${POLICY_DIR}/${SCRIPT_NAME} || true if [ "$RC_INFO" ] ; then update-rc.d ${SCRIPT_NAME} ${RC_INFO} fi rm -f ${POLICY_DIR}/${SCRIPT_NAME} return 0 } disable_script(){ local SCRIPT_NAME SCRIPT_NAME="$1" llxpol_test_active_script "${SCRIPT_NAME}" || return 0 get_rc_info ${SCRIPT_NAME} |llxcfg-install - "${POLICY_DIR}/${SCRIPT_NAME}" update-rc.d -f ${SCRIPT_NAME} remove return 0 } list_disabled_scripts(){ local SC SC_LIST [ -d ${POLICY_DIR} ] || return 0 SC_LIST="`find ${POLICY_DIR} -xtype f`" for SC in ${SC_LIST}; do echo "`basename ${SC}`" done return 0 } error_msg() { echo -e "$1" >&2 exit 1 } usage() { error_msg "Usage: $0 {enable|disable|status} \n\ $0 {list-disabled|enable-all}" } # ----- # MAIN # ----- SCRIPT_NAME="$2" [ -z "${1}" ] && usage case "$1" in enable) verbose_script_test ${SCRIPT_NAME} enable_script ${SCRIPT_NAME} ;; disable) verbose_script_test ${SCRIPT_NAME} disable_script ${SCRIPT_NAME} ;; status) verbose_script_test ${SCRIPT_NAME} if llxpol_test_active_script "${SCRIPT_NAME}" ; then echo "ENABLED" else echo "DISABLED" fi ;; list-disabled) list_disabled_scripts ;; enable-all) SCRIPT_LIST="`list_disabled_scripts`" for SC in ${SCRIPT_LIST}; do if quiet_script_test ${SC} ; then enable_script ${SC} fi done ;; *) usage ;; esac exit 0