#!/sbin/runscript # Copyright 1999-2004 Gentoo Foundation # Distributed under the terms of the GNU General Public License, v2 or later # $Header: $ [ -n "${PIDFILE}" ] || PIDFILE=/var/run/slimserver.pid depend() { after net } # Utility function to test files access rights. # Example: access_test "r.x" /path/to/foo/bar access_test() { [ $# -ne 2 ] && return 2 [ ! -e "${2}" ] && return 1 local my_stat=$(stat -c"%U:%G:%A" "${2}") local stat_a=$(cut -d: -f3 <<<"${my_stat}") # Test on user local stat_u=$(cut -d: -f1 <<<"${my_stat}") local stat_a_u=$(cut -b2-4 <<<"${stat_a}") [ "${SLIM_USER}" = "${stat_u}" ] \ && grep -q "${1}" <<<"${stat_a_u}" \ && return 0 # Test on group local stat_g=$(cut -d: -f2 <<<"${my_stat}") local stat_a_g=$(cut -b5-7 <<<"${stat_a}") [ "${SLIM_GROUP}" = "${stat_g}" ] \ && grep -q "${1}" <<<"${stat_a_g}" \ && return 0 # Test on other local stat_a_o=$(cut -b8-10 <<<"${stat_a}") grep -q "${1}" <<<"${stat_a_o}" \ && return 0 # Failed... return 1 } start() { ebegin "Starting SlimServer" # This are settings that got a default value from the ebuild SLIM_INSTALL_DIR="${SLIM_INSTALL_DIR:-}" SLIM_HOME_DIR="${SLIM_HOME_DIR:-}" SLIM_LOG_FILE="${SLIM_LOG_FILE:-}" SLIM_CONFIG_FILE="${SLIM_CONFIG_FILE:-}" SLIM_USER="${SLIM_USER:-}" SLIM_GROUP="${SLIM_GROUP:-}" SLIM_CACHE_DIR="${SLIM_CACHE_DIR:-}" if [ ! -f "${SLIM_CONFIG_FILE}" ] ; then # Use default only if slimserver has never been configured, # otherwise it would make the web config useless. SLIM_MUSIC_DIR="${SLIM_MUSIC_DIR:-}" SLIM_PLAYLISTS_DIR="${SLIM_PLAYLISTS_DIR:-}" fi local var_names_list="SLIM_INSTALL_DIR SLIM_HOME_DIR SLIM_CACHE_DIR \ SLIM_MUSIC_DIR SLIM_PLAYLISTS_DIR SLIM_LOG_FILE SLIM_CONFIG_FILE \ SLIM_USER SLIM_GROUP" local var_name var_value for var_name in ${var_names_list} ; do eval var_value="\${${var_name}}" [ -z "${var_value}" ] || [ "${var_value}" = "unset" ] \ && unset "${var_name}" done # If SLIM_USER or SLIM_GROUP are unset, it's more convenient in the # following to have them set to "root": SLIM_USER="${SLIM_USER:-root}" SLIM_GROUP="${SLIM_GROUP:-root}" # Check $SLIM_USER grep -q "^${SLIM_USER}\\>" /etc/passwd \ || eend 1 "User \"${SLIM_USER}\" does not exists." \ || return 1 # Check $SLIM_GROUP grep -q "\\<${SLIM_GROUP}\\>" <( groups ${SLIM_USER} ) \ || eend 1 "User \"${SLIM_USER}\" is not in group \"${SLIM_GROUP}\"." \ || return 1 # Directories check/creation local mydir for mydir in "${SLIM_HOME_DIR}" "${SLIM_MUSIC_DIR}" \ "${SLIM_PLAYLISTS_DIR}" "${SLIM_CACHE_DIR}" do if [ -n "${mydir}" ] && [ ! -d "${mydir}" ] ; then einfo "Directory ${mydir} missing, creating..." mkdir -p "${mydir}" chown ${SLIM_USER}:${SLIM_GROUP} "${mydir}" if [ "${mydir}" = "${SLIM_MUSIC_DIR}" ] \ || [ "${mydir}" = "${SLIM_PLAYLISTS_DIR}" ] ; then # Chmod only if creating new dirs einfo "Making ${mydir} writable for group ${SLIM_GROUP}..." chmod 775 "${mydir}" fi fi done # Is cache dir rwx? [ -z "${SLIM_CACHE_DIR}" ] \ || access_test rwx "${SLIM_CACHE_DIR}" \ || eend 1 "${SLIM_CACHE_DIR} should be writable for user ${SLIM_USER}:${SLIM_GROUP}" \ || return 1 # Is music dir r-x? [ -z "${SLIM_MUSIC_DIR}" ] \ || access_test "r.x" "${SLIM_MUSIC_DIR}" \ || ewarn "${SLIM_MUSIC_DIR} is not readable for user ${SLIM_USER}:${SLIM_GROUP}" # Is playlist dir r-x? [ -z "${SLIM_PLAYLISTS_DIR}" ] \ || access_test "r.x" "${SLIM_PLAYLISTS_DIR}" \ || ewarn "${SLIM_PLAYLISTS_DIR} is not readable for user ${SLIM_USER}:${SLIM_GROUP}" # Is playlist dir -w-? [ -z "${SLIM_PLAYLISTS_DIR}" ] \ || access_test w "${SLIM_PLAYLISTS_DIR}" \ || ewarn "${SLIM_PLAYLISTS_DIR} is not writable for user ${SLIM_USER}:${SLIM_GROUP}" # Config file check/creation if [ -n "${SLIM_CONFIG_FILE}" ] ; then if [ "${SLIM_DISABLE_SERVER_SETUP}" != "yes" ] \ && [ "${SLIM_DISABLE_SETUP}" != "yes" ] ; then touch "${SLIM_CONFIG_FILE}" \ || eend 1 "Can't write config file ${SLIM_CONFIG_FILE}" \ || return 1 chown ${SLIM_USER}:${SLIM_GROUP} "${SLIM_CONFIG_FILE}" chmod 644 "${SLIM_CONFIG_FILE}" fi access_test r "${SLIM_CONFIG_FILE}" \ || eend 1 "${SLIM_CONFIG_FILE} should be readable for user ${SLIM_USER}:${SLIM_GROUP}" \ || return 1 fi # Log file check/creation if [ -n "${SLIM_LOG_FILE}" ] && [ "${SLIM_LOG_FILE}" != "/dev/null" ] ; then touch "${SLIM_LOG_FILE}" \ || eend 1 "Can't write log file ${SLIM_LOG_FILE}" \ || return 1 chown ${SLIM_USER}:${SLIM_GROUP} "${SLIM_LOG_FILE}" chmod 644 "${SLIM_LOG_FILE}" fi # Minimal command line options set local SLIM_OPTIONS="--daemon --pidfile=${PIDFILE}" # To avoid bugs with spaces in paths, use a double quoting and then eval [ -n "${SLIM_CONFIG_FILE}" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --prefsfile=\"\${SLIM_CONFIG_FILE}\"" [ -n "${SLIM_MUSIC_DIR}" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --audiodir=\"\${SLIM_MUSIC_DIR}\"" [ -n "${SLIM_PLAYLISTS_DIR}" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --playlistdir=\"\${SLIM_PLAYLISTS_DIR}\"" [ -n "${SLIM_CACHE_DIR}" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --cachedir=\"\${SLIM_CACHE_DIR}\"" [ -n "${SLIM_LOG_FILE}" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --logfile=\"\${SLIM_LOG_FILE}\"" [ -n "${SLIM_USER}" ] && [ "${SLIM_USER}" != "root" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --user=${SLIM_USER}" [ -n "${SLIM_GROUP}" ] && [ "${SLIM_GROUP}" != "root" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --group=${SLIM_GROUP}" [ -n "${SLIM_HTTP_PORT}" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --httpport=${SLIM_HTTP_PORT}" [ -n "${SLIM_HTTP_ADDR}" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --httpaddr=${SLIM_HTTP_ADDR}" [ -n "${SLIM_CLI_PORT}" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --cliport=${SLIM_CLI_PORT}" [ -n "${SLIM_CLI_ADDR}" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --cliaddr=${SLIM_CLI_ADDR}" [ -n "${SLIM_PLAYER_ADDR}" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --playeraddr=${SLIM_PLAYER_ADDR}" [ -n "${SLIM_STREAM_ADDR}" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --streamaddr=${SLIM_STREAM_ADDR}" [ "${SLIM_DISABLE_SERVER_SETUP}" = "yes" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --noserver" [ "${SLIM_DISABLE_SETUP}" = "yes" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --nosetup" [ "${SLIM_DEBUG}" = "yes" ] && \ SLIM_OPTIONS="${SLIM_OPTIONS} --diag" [ "${SLIM_QUIET}" = "no" ] || \ SLIM_OPTIONS="${SLIM_OPTIONS} --quiet" if [ -n "${SLIM_PRIORITY}" ] ; then [ ${SLIM_PRIORITY} -ge -20 -a ${SLIM_PRIORITY} -le 20 ] \ || eend 1 "Invalid priority: ${SLIM_PRIORITY}" \ || return 1 SLIM_OPTIONS="${SLIM_OPTIONS} --priority=${SLIM_PRIORITY}" fi eval start-stop-daemon --start --quiet \ --exec="${SLIM_INSTALL_DIR}/slimserver.pl" -- ${SLIM_OPTIONS} eend $? "Failed to start SlimServer" } stop() { ebegin "Stopping SlimServer" start-stop-daemon --stop --quiet --pidfile=${PIDFILE} local status=$? # For some reason, the pid file remains on exit (because of non-root?) [ -f ${PIDFILE} ] && rm -f ${PIDFILE} eend ${status} "Failed to stop SlimServer" }