From 2e036290d1489523f7aabced476e229fc64d4ab3 Mon Sep 17 00:00:00 2001 From: Calin Crisan Date: Thu, 24 Jan 2019 17:54:54 +0200 Subject: [PATCH] Small init scripts refactorization --- board/common/overlay/etc/init.d/S04syslog | 14 +++-- board/common/overlay/etc/init.d/S12udev | 18 +++--- board/common/overlay/etc/init.d/S13watchdog | 13 +++- board/common/overlay/etc/init.d/S30dbus | 17 ++++-- board/common/overlay/etc/init.d/S33hostapd | 6 +- board/common/overlay/etc/init.d/S35wifi | 3 +- board/common/overlay/etc/init.d/S36ppp | 6 +- board/common/overlay/etc/init.d/S37bluetooth | 39 ++++++------ board/common/overlay/etc/init.d/S41netwatch | 12 ++-- board/common/overlay/etc/init.d/S43firewall | 1 + board/common/overlay/etc/init.d/S45dnsmasq | 4 +- board/common/overlay/etc/init.d/S51crond | 12 +++- board/common/overlay/etc/init.d/S60sshd | 15 +++-- board/common/overlay/etc/init.d/S61proftpd | 52 ++++++++++++++++ board/common/overlay/etc/init.d/S62smb | 62 ++++++++++++++++++++ 15 files changed, 216 insertions(+), 58 deletions(-) create mode 100755 board/common/overlay/etc/init.d/S61proftpd create mode 100755 board/common/overlay/etc/init.d/S62smb diff --git a/board/common/overlay/etc/init.d/S04syslog b/board/common/overlay/etc/init.d/S04syslog index facaeedb98..78586fadf3 100755 --- a/board/common/overlay/etc/init.d/S04syslog +++ b/board/common/overlay/etc/init.d/S04syslog @@ -1,24 +1,28 @@ #!/bin/bash -test -n "${OS_VERSION}" || source /etc/init.d/base +PROG="/sbin/syslogd" +PROG_D="/bin/dmesg" DMESG_LOG="/var/log/dmesg.log" +test -n "${OS_VERSION}" || source /etc/init.d/base + + case "$1" in start) msg_begin "Starting syslogd" - syslogd + ${PROG} test $? == 0 && msg_done || msg_fail echo "---- booting ${OS_NAME} ${OS_VERSION} ----" >> ${DMESG_LOG} - dmesg -T -w >> ${DMESG_LOG} & + ${PROG_D} -T -w >> ${DMESG_LOG} & ;; stop) msg_begin "Stopping syslogd" - killall syslogd &>/dev/null + killall -q $(basename ${PROG}) test $? == 0 && msg_done || msg_fail - killall dmesg &>/dev/null + killall -q $(basename ${PROG_D}) ;; *) diff --git a/board/common/overlay/etc/init.d/S12udev b/board/common/overlay/etc/init.d/S12udev index 3228aefc17..fecf68e5cc 100755 --- a/board/common/overlay/etc/init.d/S12udev +++ b/board/common/overlay/etc/init.d/S12udev @@ -1,31 +1,35 @@ #!/bin/bash -CONF=/etc/udev/udev.conf +CONF="/etc/udev/udev.conf" +PROG="/sbin/udevd" +PROG_UA="/sbin/udevadm" +test -f ${PROG} || exit 0 test -f ${CONF} || exit 0 test -n "${OS_VERSION}" || source /etc/init.d/base source ${CONF} + start() { msg_begin "Starting eudev" echo '\000\000\000\000' > /proc/sys/kernel/hotplug - /sbin/udevd --daemon --resolve-names=never + ${PROG} --daemon --resolve-names=never test $? == 0 && msg_done || msg_fail - /sbin/udevadm trigger --type=subsystems --action=add - /sbin/udevadm trigger --type=devices --action=add - /sbin/udevadm settle --timeout=30 + ${PROG_UA} trigger --type=subsystems --action=add + ${PROG_UA} trigger --type=devices --action=add + ${PROG_UA} settle --timeout=30 return 0 } stop() { msg_begin "Stopping eudev" - udevadm control --stop-exec-queue - killall udevd &>/dev/null + ${PROG_UA} control --stop-exec-queue + killall -q $(basename ${PROG}) test $? == 0 && msg_done || msg_fail } diff --git a/board/common/overlay/etc/init.d/S13watchdog b/board/common/overlay/etc/init.d/S13watchdog index 331bdcfc66..4b5e686b6f 100755 --- a/board/common/overlay/etc/init.d/S13watchdog +++ b/board/common/overlay/etc/init.d/S13watchdog @@ -1,13 +1,20 @@ #!/bin/bash -test -c /dev/watchdog || exit 0 +DEV="/dev/watchdog" + +PROG="/sbin/watchdog" + + +test -c ${PROG} || exit 0 +test -c ${DEV} || exit 0 test -n "${OS_VERSION}" || source /etc/init.d/base + case "$1" in start) msg_begin "Starting watchdog" - watchdog -t 5 /dev/watchdog + ${PROG} -t 5 ${DEV} test $? == 0 && msg_done || msg_fail ;; @@ -17,7 +24,7 @@ case "$1" in reallystop) msg_begin "Stopping watchdog" - killall watchdog &>/dev/null + killall -q $(basename ${PROG}) test $? == 0 && msg_done || msg_fail ;; diff --git a/board/common/overlay/etc/init.d/S30dbus b/board/common/overlay/etc/init.d/S30dbus index 5f9bc53f4e..c239b80654 100755 --- a/board/common/overlay/etc/init.d/S30dbus +++ b/board/common/overlay/etc/init.d/S30dbus @@ -4,30 +4,35 @@ SYS_BTCONF="/etc/bluetooth.conf" BOOT_BTCONF="/boot/bluetooth.conf" BTCONF="/data/etc/bluetooth.conf" +PROG="/usr/bin/dbus-daemon" +PROG_UG="/usr/bin/dbus-uuidgen" +PROG_UA="/usr/bin/udevadm" + + +test -x ${PROG} || exit 0 # dbus is currently only used by bluez test -f ${BTCONF} || test -f ${BOOT_BTCONF} || test -f ${SYS_BTCONF} || exit 0 -test -x /usr/bin/dbus-daemon || exit 0 - test -n "${OS_VERSION}" || source /etc/init.d/base + start() { mkdir -p /tmp/dbus mkdir -p /var/lib/dbus msg_begin "Starting dbus" - dbus-uuidgen --ensure - dbus-daemon --system + ${PROG_UG} --ensure + ${PROG} --system test $? == 0 && msg_done || msg_fail } stop() { msg_begin "Stopping dbus" - udevadm control --stop-exec-queue - killall dbus-daemon &>/dev/null + ${PROG_UA} control --stop-exec-queue + killall -q $(basename ${PROG}) test $? == 0 && msg_done || msg_fail rm -f /var/run/messagebus.pid diff --git a/board/common/overlay/etc/init.d/S33hostapd b/board/common/overlay/etc/init.d/S33hostapd index d25ffae608..c7da890c31 100755 --- a/board/common/overlay/etc/init.d/S33hostapd +++ b/board/common/overlay/etc/init.d/S33hostapd @@ -9,10 +9,12 @@ PROG="/usr/sbin/hostapd" WATCH_CONF="/data/etc/watch.conf" -link_watch=yes -link_watch_timeout=20 +LINK_WATCH=yes +LINK_WATCH_TIMEOUT=20 +test -x {$PROG} || exit 0 + test -n "${OS_VERSION}" || source /etc/init.d/base prepare_conf ${CONF} ${SYS_CONF} ${BOOT_CONF} diff --git a/board/common/overlay/etc/init.d/S35wifi b/board/common/overlay/etc/init.d/S35wifi index 93294aad89..d1b6a3901c 100755 --- a/board/common/overlay/etc/init.d/S35wifi +++ b/board/common/overlay/etc/init.d/S35wifi @@ -13,6 +13,8 @@ BOOT_WATCH_CONF="/boot/watch.conf" WATCH_CONF="/data/etc/watch.conf" +test -x ${PROG} || exit 0 + test -n "${OS_VERSION}" || source /etc/init.d/base prepare_conf ${WATCH_CONF} ${SYS_WATCH_CONF} ${BOOT_WATCH_CONF} @@ -24,7 +26,6 @@ test -f ${CONF} || exit 0 ssid=$(cat ${CONF} | grep ssid | grep -v scan_ssid | cut -d '"' -f 2) test -n "${ssid}" || exit 0 - test "${OS_NETWORKLESS}" == "true" && exit 0 diff --git a/board/common/overlay/etc/init.d/S36ppp b/board/common/overlay/etc/init.d/S36ppp index 14dbe7cd6c..9186e6e926 100755 --- a/board/common/overlay/etc/init.d/S36ppp +++ b/board/common/overlay/etc/init.d/S36ppp @@ -10,6 +10,8 @@ PROVIDER="mobile" WATCH_CONF="/data/etc/watch.conf" +test -x ${PROG} || exit 0 + test -n "${OS_VERSION}" || source /etc/init.d/base prepare_conf ${CONF} ${SYS_CONF} ${BOOT_CONF} @@ -88,7 +90,7 @@ start() { break fi - if [[ ${count} -gt ${LINK_WATCH_TIMEOUT} ]] || ! pidof pppd > /dev/null; then + if [[ ${count} -gt ${LINK_WATCH_TIMEOUT} ]] || ! pidof $(basename ${PROG}) > /dev/null; then msg_fail return fi @@ -105,7 +107,7 @@ start() { stop() { msg_begin "Stopping pppd" - killall pppd &>/dev/null + killall -q $(basename ${PROG}) ps | grep ppp | grep -v $$ | grep -v grep | tr -s ' ' | sed -e 's/^\s//' | cut -d ' ' -f 1 | xargs -r kill msg_done } diff --git a/board/common/overlay/etc/init.d/S37bluetooth b/board/common/overlay/etc/init.d/S37bluetooth index 8d2fde898d..16cc5eab45 100755 --- a/board/common/overlay/etc/init.d/S37bluetooth +++ b/board/common/overlay/etc/init.d/S37bluetooth @@ -3,34 +3,35 @@ SYS_CONF="/etc/bluetooth.conf" BOOT_CONF="/boot/bluetooth.conf" CONF="/data/etc/bluetooth.conf" +RUN_CONF="/var/lib/bluetooth.conf" +ADAPTER="hci0" +PROG="/usr/libexec/bluetooth/bluetoothd" +PROG_HC="/usr/bin/hciconfig" +DATA_DIR="/var/lib/bluetooth" +RUN_DATA_DIR="/data/bluetooth" + + +test -x ${PROG} || exit 0 test -n "${OS_VERSION}" || source /etc/init.d/base prepare_conf ${CONF} ${SYS_CONF} ${BOOT_CONF} test -f ${CONF} || exit 0 -hci=hci0 -bluetoothd=/usr/libexec/bluetooth/bluetoothd -data_dir=/var/lib/bluetooth -run_data_dir=/data/bluetooth -run_conf=/var/lib/bluetooth.conf - -test -x ${bluetoothd} || exit 0 - configure() { - mkdir -p ${run_data_dir} - ln -sf ${run_data_dir} ${data_dir} - cp ${CONF} ${run_conf} + mkdir -p ${RUN_DATA_DIR} + ln -sf ${RUN_DATA_DIR} ${DATA_DIR} + cp ${CONF} ${RUN_CONF} # if no specific name configured, use hostname - if ! grep -E 'Name\s*=' ${run_conf} &>/dev/null; then - sed -ri "s/(\[General\])/\1\nName = $(hostname)/" ${run_conf} + if ! grep -E 'Name\s*=' ${RUN_CONF} &>/dev/null; then + sed -ri "s/(\[General\])/\1\nName = $(hostname)/" ${RUN_CONF} fi # bring adapter up - hciconfig ${hci} up + ${PROG_HC} ${ADAPTER} up } start() { @@ -38,7 +39,7 @@ start() { # wait up to 10 seconds for device count=0 - while ! hciconfig ${hci} &>/dev/null; do + while ! ${PROG_HC} ${ADAPTER} &>/dev/null; do sleep 1 count=$((${count} + 1)) if [[ ${count} -ge 10 ]]; then @@ -57,19 +58,19 @@ start() { fi msg_begin "Starting bluetoothd" - ${bluetoothd} &>/dev/null & + ${PROG} &>/dev/null & msg_done # if DiscoverableTimeout is set to 0, make adapter discoverable from boot time - if grep -E '^DiscoverableTimeout\s*=\s*0$' ${run_conf} &>/dev/null; then + if grep -E '^DiscoverableTimeout\s*=\s*0$' ${RUN_CONF} &>/dev/null; then sleep 1 - hciconfig ${hci} piscan + ${PROG_HC} ${ADAPTER} piscan fi } stop() { msg_begin "Stopping bluetoothd" - killall bluetoothd &>/dev/null + killall -q $(basename ${PROG}) test $? == 0 && msg_done || msg_fail } diff --git a/board/common/overlay/etc/init.d/S41netwatch b/board/common/overlay/etc/init.d/S41netwatch index cfab7e3c91..bb2f18be17 100755 --- a/board/common/overlay/etc/init.d/S41netwatch +++ b/board/common/overlay/etc/init.d/S41netwatch @@ -1,16 +1,18 @@ #!/bin/bash -WATCH_CONF="/data/etc/watch.conf" +CONF="/data/etc/watch.conf" NETWATCH_RETRIES=3 NETWATCH_TIMEOUT=5 NETWATCH_INTERVAL=20 -test -f ${WATCH_CONF} && source ${WATCH_CONF} || exit 0 +# watch configuration is already prepared by wifi init script -if [[ -z "${NETWATCH_HOST}" || -z "${NETWATCH_PORT}" ]]; then - exit 0 -fi +test -f ${CONF} || exit 0 +source ${CONF} + +test -z "${NETWATCH_HOST}" || exit 0 +test -z "${NETWATCH_PORT}" || exit 0 test -n "${OS_VERSION}" || source /etc/init.d/base diff --git a/board/common/overlay/etc/init.d/S43firewall b/board/common/overlay/etc/init.d/S43firewall index c588fd0d6d..bab1a54053 100755 --- a/board/common/overlay/etc/init.d/S43firewall +++ b/board/common/overlay/etc/init.d/S43firewall @@ -10,6 +10,7 @@ test -n "${OS_VERSION}" || source /etc/init.d/base prepare_conf ${CONF} ${SYS_CONF} ${BOOT_CONF} test -f ${CONF} || exit 0 + start() { msg_begin "Starting firewall" diff --git a/board/common/overlay/etc/init.d/S45dnsmasq b/board/common/overlay/etc/init.d/S45dnsmasq index df0d849cbe..f178f84a3f 100755 --- a/board/common/overlay/etc/init.d/S45dnsmasq +++ b/board/common/overlay/etc/init.d/S45dnsmasq @@ -8,6 +8,8 @@ LOG="/var/log/dnsmasq.log" PROG="/usr/sbin/dnsmasq" +test -x ${PROG} || exit 0 + test -n "${OS_VERSION}" || source /etc/init.d/base prepare_conf ${CONF} ${SYS_CONF} ${BOOT_CONF} @@ -29,7 +31,7 @@ start() { stop() { msg_begin "Stopping dnsmasq" - killall dnsmasq &>/dev/null + killall -q $(basename ${PROG}) test $? == 0 && msg_done || msg_fail } diff --git a/board/common/overlay/etc/init.d/S51crond b/board/common/overlay/etc/init.d/S51crond index 7485031ef7..9529ee31d8 100755 --- a/board/common/overlay/etc/init.d/S51crond +++ b/board/common/overlay/etc/init.d/S51crond @@ -3,24 +3,30 @@ SYS_CONF="/etc/crontabs" CONF="/data/etc/crontabs" +PROG="/usr/sbin/crond" + + +test -x ${PROG} || exit 0 + test -n "${OS_VERSION}" || source /etc/init.d/base + start() { msg_begin "Starting crond" if [[ -d ${SYS_CONF} ]]; then - /usr/sbin/crond -c ${SYS_CONF} + ${PROG} -c ${SYS_CONF} fi mkdir -p ${CONF} - /usr/sbin/crond -c ${CONF} + ${PROG} -c ${CONF} test $? == 0 && msg_done || msg_fail } stop() { msg_begin "Stopping crond" - killall crond &>/dev/null + killall -q $(basename ${PROG}) test $? == 0 && msg_done || msg_fail } diff --git a/board/common/overlay/etc/init.d/S60sshd b/board/common/overlay/etc/init.d/S60sshd index 3d7f7620c2..26709bd001 100755 --- a/board/common/overlay/etc/init.d/S60sshd +++ b/board/common/overlay/etc/init.d/S60sshd @@ -2,17 +2,24 @@ CONF="/etc/sshd_config" -test -f ${CONF} || exit 0 +PROG="/usr/sbin/sshd" +KGPROG="/usr/bin/ssh-keygen" + + +test -x ${PROG} || exit 0 test -n "${OS_VERSION}" || source /etc/init.d/base +test -x ${CONF} || exit 0 + test "${OS_NETWORKLESS}" == "true" && exit 0 + start() { msg_begin "Starting sshd" # create any missing keys - if ! /usr/bin/ssh-keygen -A >/dev/null; then + if ! ${KGPROG} -A >/dev/null; then msg_fail return 1 fi @@ -22,13 +29,13 @@ start() { echo "Welcome to ${hostname}!" > /var/cache/sshd_banner sync - /usr/sbin/sshd -f ${CONF} + ${PROG} -f ${CONF} test $? == 0 && msg_done || msg_fail } stop() { msg_begin "Stopping sshd" - killall sshd &>/dev/null + killall -q $(basename ${PROG}) test $? == 0 && msg_done || msg_fail } diff --git a/board/common/overlay/etc/init.d/S61proftpd b/board/common/overlay/etc/init.d/S61proftpd new file mode 100755 index 0000000000..e83b05451b --- /dev/null +++ b/board/common/overlay/etc/init.d/S61proftpd @@ -0,0 +1,52 @@ +#!/bin/bash + +CONF="/etc/proftpd.conf" +PROG="/usr/sbin/proftpd" + + +test -x ${PROG} || exit 0 + +test -n "${OS_VERSION}" || source /etc/init.d/base +test -n "${OS_DEBUG}" || source /etc/init.d/conf + +test -x ${CONF} || exit 0 + +test "${OS_NETWORKLESS}" == "true" && exit 0 + + +start() { + msg_begin "Starting proftpd" + mkdir -p /var/run/proftpd + touch /var/log/wtmp + ${PROG} &>/dev/null + test $? == 0 && msg_done || msg_fail +} + +stop() { + msg_begin "Stopping proftpd" + killall -q $(basename ${PROG}) + test $? == 0 && msg_done || msg_fail +} + +case "$1" in + start) + start + ;; + + stop) + stop + ;; + + restart) + stop + start + ;; + + *) + echo "Usage: $0 {start|stop|restart}" + exit 1 + ;; +esac + +exit 0 + diff --git a/board/common/overlay/etc/init.d/S62smb b/board/common/overlay/etc/init.d/S62smb new file mode 100755 index 0000000000..1135975435 --- /dev/null +++ b/board/common/overlay/etc/init.d/S62smb @@ -0,0 +1,62 @@ +#!/bin/bash + +CONF="/etc/samba/smb.conf" + +PROG="/usr/sbin/smbd" +PROG_N="/usr/sbin/nmbd" + + +test -x ${PROG} || exit 0 + +test -n "${OS_VERSION}" || source /etc/init.d/base +test -n "${OS_DEBUG}" || source /etc/init.d/conf + +test -x ${CONF} || exit 0 + +test "${OS_NETWORKLESS}" == "true" && exit 0 + + +start() { + mkdir -p /var/log/samba + mkdir -p /var/lib/samba/private + + msg_begin "Starting smbd" + ${PROG} -D + test $? == 0 && msg_done || msg_fail + + msg_begin "Starting nmbd" + ${PROG_N} -D + test $? == 0 && msg_done || msg_fail +} + +stop() { + msg_begin "Stopping smbd" + killall -q $(basename ${PROG}) + test $? == 0 && msg_done || msg_fail + + msg_begin "Stopping nmbd" + killall -q $(basename ${PROG_N}) + test $? == 0 && msg_done || msg_fail +} + +case "$1" in + start) + start + ;; + + stop) + stop + ;; + + restart) + stop + start + ;; + + *) + echo "Usage: $0 {start|stop|restart}" + exit 1 +esac + +exit $? +