#!/bin/sh #---------------------------------------------------------------------------- # /var/install/bin/edit - ask user for edit # # Creation : 2003-07-19 fm # Last Update: $Id$ # # Copyright (c) 2001-@@YEAR@@ the eisfair team, team(at)eisfair(dot)org # # usage: edit [--apply|--apply-stopstart] file # edit [--apply|--apply-stopstart] package message # # return values: # 0 editor started # 1 editor not started # # 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. #---------------------------------------------------------------------------- # include eislib . /var/install/include/eislib ASK='/var/install/bin/ask' MKTEMP='/usr/bin/mktemp' EISCHK='/usr/bin/eischk' DTOU='/usr/bin/dtou' SERVICE='/usr/sbin/service' PIDFILE='' # handle pid file / lock double edit # $1 package_name handle_pid() { c_package=$1 # set pidfile to block multiple # instances from edit running PIDFILE="/run/lock/${c_package}_edit.pid" if [ -f ${PIDFILE} ] then pid=$(< ${PIDFILE}) # get info, if running without info line, # like 'PID TTY TIME CMD' _running=$(ps -p ${pid} -o comm=) if [ -z "${_running}" ] then rm ${PIDFILE} else case ${_running} in show-menu*) : ;; *) rm ${PIDFILE} ;; esac fi fi if [ ! -f ${PIDFILE} ] then # PPID = Parent Process ID echo "${PPID}" > ${PIDFILE} else echo mecho --warn "The package edit menu for ${c_package} is currently" mecho --warn "used by another instance." mecho --warn "Please exit this other instance to use package edit here." echo anykey # musst by exit code 2 # to handle in show-menu correct exit 2 fi } # include function library . /var/install/bin/config_shlib # include configuration . /etc/config.d/setup apply='' case "${1}" in --apply|-apply) apply='restart' shift ;; --apply-stopstart|-apply-stopstart) apply='stopstart' shift ;; esac if [ $# -eq 1 ] then package=$(basename $1) configdir=$(dirname $1) else package="$1" configdir='/etc/config.d' clrhome mecho --info "$2" echo fi # if EDITOR is set, try to add path [ "$EDITOR" = "" ] && EDITOR='joe' [ "$EDITOR_FALLBACK" = "" ] && EDITOR_FALLBACK='joe' editor="$(echo "$EDITOR_FALLBACK" | cut -d' ' -f1)" editor_param="$(echo "$EDITOR_FALLBACK" | cut -d' ' -f2-)" [ "$editor" = "$editor_param" ] && editor_param='' if [ "$editor" != "" ] && [ ! -x "$editor" ] then if [ -x "/var/install/bin/$editor" ] then EDITOR_FALLBACK="/var/install/bin/$editor $editor_param" elif which $editor >/dev/null 2>/dev/null then EDITOR_FALLBACK="$(which $editor) $editor_param" else EDITOR_FALLBACK="$(which joe)" fi fi editor="$(echo "$EDITOR" | cut -d' ' -f1)" editor_param="$(echo "$EDITOR" | cut -d' ' -f2-)" [ "$editor" = "$editor_param" ] && editor_param='' if [ "$editor" != "" ] && [ ! -x "$editor" ] then if [ -x "/var/install/bin/$editor" ] then EDITOR="/var/install/bin/$editor $editor_param" elif which $editor >/dev/null 2>/dev/null then EDITOR="$(which $editor) $editor_param" else EDITOR="$EDITOR_FALLBACK" fi fi exitval=0 # edit first package... current_package=$(echo $package | sed -e 's/ .*$//g') if [ -f $configdir/$current_package ] then handle_pid $current_package editor="$EDITOR_FALLBACK" param='' editor_pgm=$(echo $EDITOR | cut -d' ' -f1) dirname=$(dirname $editor_pgm) if [ "$dirname" = "/var/install/bin" ] then if $EDITOR --check $configdir/$current_package then editor="$EDITOR" else echo anykey echo mecho "The configuration of this package can't be opened by the config" mecho "editor currently setup on your system. This may be due to some" mecho "inconsistent config files or options." mecho "You can now edit the configuration manually using editor $editor." echo fi else editor="$EDITOR" echo mecho "There is no configuration editor for the 'eisfair' console configured" mecho "on your system. Alternatively you can edit the configuration manually" mecho "using editor $editor." echo fi editor_pgm=$(echo $editor | cut -d' ' -f1) basename=$(basename $editor_pgm) if [ "$basename" = "joe" ] then mecho "You can get help by joe if you press 'Ctrl-K H'." mecho param="-nobackups" elif [ "$basename" = "vi" -o "$basename" = "vim" ] then mecho "You can get help by vi/vim if you enter ':help'." mecho fi dirname=$(dirname $editor_pgm) if [ "$dirname" != "/var/install/bin" ] then ask_tmpfile=$(${MKTEMP} -t XXXXXXXXXXXXX) ${ASK} "Continue" "yes" > ${ask_tmpfile} rc=${?} read a <${ask_tmpfile} rm -f ${ask_tmpfile} # if ask break, ask returned 255 if [ ${rc} = 255 ] then a=no fi if [ "${a}" != "yes" ] then exit 1 fi fi old_umask=$(umask) umask 077 # do not allow anybody to read anything # config or logfile may contain secret passwords # delete old tmpfile first, if exists ;-) rm -f /tmp/edit-${current_package}-* # create tmpfile name, # do not change this name, # many programs get the old config settings from it, # to compare the value tmp_edit_file=/tmp/edit-${current_package}-$$ # copy to tmpfile cp -p $configdir/$current_package ${tmp_edit_file} while [ 1 ] do $editor $param $configdir/$current_package ${DTOU} $configdir/$current_package # check if newline on tail exist in config file, or added it # do not change the 'sed' syntax layout, # the second line musst be to begin. sed -i '${/^$/!s/$/\ /;}' $configdir/$current_package if [ -f ${EISCHK} -a -f /etc/check.d/$current_package ] then mecho --info "Checking configuration file ..." mecho if [ "$current_package" = "environment" ] then eischk_weak='--weak' else eischk_weak='' fi if ! ${EISCHK} $eischk_weak \ --config /etc/config.d \ --check /etc/check.d \ --log /tmp/$current_package.log \ --package $current_package then ask_tmpfile=$(${MKTEMP} -t XXXXXXXXXXXXX) ${ASK} "Edit configuration again" "yes" >${ask_tmpfile} rc=${?} read a <${ask_tmpfile} rm -f ${ask_tmpfile} # if ask break, ask returned 255 if [ ${rc} = 255 ] then a=no fi if [ "${a}" != "yes" ] then exitval=1 # rollback ${tmp_edit_file} cp -p ${tmp_edit_file} ${configdir}/${current_package} mecho -n --warn 'Old configuratuion file ' mecho -n --std "'${current_package}' " mecho --warn 'will be restored!' break fi else break fi else break fi done # config_shlib has owne handling umask $old_umask if [ $exitval -eq 0 ] then if ! diff -B -E -Z ${tmp_edit_file} $configdir/$current_package >/dev/null then backup_config $current_package ${tmp_edit_file} fi fi rm -f /tmp/$current_package.log fi # call apply-script if exists apply_script() { # set alway to true, but base and environment # delete after restart, to get the old value # to compare it in the init script _rm=${2:-true} if [ -f /var/install/config.d/$1.sh ] then sh /var/install/config.d/$1.sh else return 0 fi if ${_rm} then rm -f ${tmp_edit_file} fi } # can start package can_start() { local package=${1} # check if service is enabled if ! ${SERVICE} is-enabled ${package} then return 1 fi # read configuration file . /etc/config.d/${package} # change also '-' to '_', many packages use a '-' in the name, # like package-dev and START_PACKAGE-DEV does not work :-( startvar=$(echo START_${1} | tr 'a-z' 'A-Z' | tr '-' '_') eval start='$'${startvar} test "${start}" = "yes" } if [ $exitval -eq 0 -a "$apply" != "" ] then echo if [ "$package" = "base" -o "$package" = "environment" ] then # set delete to false, to do after restart, # to get the old value to compare in the init script if apply_script $package false && ${SERVICE} is-service ${package} then ${SERVICE} restart ${package} fi # delete after restart, to get the old value # to compare in the init script rm -f ${tmp_edit_file} else ask_tmpfile=$(${MKTEMP} -t XXXXXXXXXXXXX) ${ASK} "Activate configuration now" "yes" >${ask_tmpfile} rc=${?} read a <${ask_tmpfile} rm -f ${ask_tmpfile} # if ask break, ask returned 255 if [ ${rc} = 255 ] then a=no rm -f ${tmp_edit_file} fi if [ "${a}" = "yes" ] then # activate changes for current_package in $package do if [ "$apply" = "restart" ] then if apply_script $current_package && ${SERVICE} is-service $current_package then if can_start $current_package then # restart package ${SERVICE} restart $current_package else # stop package ${SERVICE} stop $current_package fi fi elif [ "$apply" = "stopstart" ] then if ${SERVICE} is-service $current_package then ${SERVICE} stop $current_package fi if apply_script $current_package then if ${SERVICE} is-service $current_package && can_start $current_package then ${SERVICE} start $current_package fi fi fi done fi fi anykey fi # do it twice ;-) rm -f ${tmp_edit_file} # remove block pidfile rm -f ${PIDFILE} exit $exitval