[arch-projects] [initscripts] [PATCH 0/2] Some more trivial simplifications
Kurt J. Bosch (2): Get rid of simple if-constucts by using '&&' Simplify /etc/profile.d/locale.sh creation rc.multi | 4 +--- rc.shutdown | 58 ++++++++++++++++++++++++++-------------------------------- rc.sysinit | 42 ++++++++++++++---------------------------- 3 files changed, 41 insertions(+), 63 deletions(-)
--- rc.multi | 4 +--- rc.shutdown | 58 ++++++++++++++++++++++++++-------------------------------- rc.sysinit | 33 +++++++++++---------------------- 3 files changed, 38 insertions(+), 57 deletions(-) diff --git a/rc.multi b/rc.multi index 4b55a27..b801fb6 100755 --- a/rc.multi +++ b/rc.multi @@ -20,9 +20,7 @@ for daemon in "${DAEMONS[@]}"; do esac done -if [[ -x /etc/rc.local ]]; then - /etc/rc.local -fi +[[ -x /etc/rc.local ]] && /etc/rc.local run_hook multi_end diff --git a/rc.shutdown b/rc.shutdown index 473953f..9eb0b31 100755 --- a/rc.shutdown +++ b/rc.shutdown @@ -22,21 +22,20 @@ echo " " kill_everything shutdown stat_busy "Saving Random Seed" -RANDOM_SEED=/var/lib/misc/random-seed -install -TDm 0600 <(:) $RANDOM_SEED -POOL_FILE=/proc/sys/kernel/random/poolsize -if [[ -r $POOL_FILE ]]; then - read POOL_SIZE < $POOL_FILE -else - POOL_SIZE=512 -fi -dd if=/dev/urandom of=$RANDOM_SEED count=1 bs=$POOL_SIZE &>/dev/null + RANDOM_SEED=/var/lib/misc/random-seed + install -TDm 0600 <(:) $RANDOM_SEED + POOL_FILE=/proc/sys/kernel/random/poolsize + if [[ -r $POOL_FILE ]]; then + read POOL_SIZE < $POOL_FILE + else + POOL_SIZE=512 + fi + dd if=/dev/urandom of=$RANDOM_SEED count=1 bs=$POOL_SIZE &>/dev/null stat_done -if [[ $TIMEZONE ]]; then +[[ $TIMEZONE ]] && status "Saving Time Zone" \ cp --remove-destination "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime -fi # Write to wtmp file before unmounting halt -w @@ -44,41 +43,36 @@ halt -w status "Deactivating Swap" swapoff -a # stop monitoring of lvm2 groups before unmounting filesystems -if [[ $USELVM = [Yy][Ee][Ss] && -x $(type -P lvm) && -d /sys/block ]]; then +[[ $USELVM = [Yy][Ee][Ss] && -x $(type -P lvm) && -d /sys/block ]] && status "Deactivating monitoring of LVM2 groups" \ vgchange --monitor n &>/dev/null -fi -stat_busy "Unmounting Filesystems" - # if we don't have devtmpfs support, /dev is mounted as tmpfs, so don't unmount it +# if we don't have devtmpfs support, /dev is mounted as tmpfs, so don't unmount it +status "Unmounting Filesystems" \ umount -a -r -t nodevtmpfs,notmpfs,nosysfs,noproc,nodevpts -O no_netdev -stat_done run_hook shutdown_postumount # Kill non-root encrypted partition mappings if [[ -f /etc/crypttab && $CS ]] && grep -q ^[^#] /etc/crypttab; then stat_busy "Deactivating encrypted volumes:" - # Maybe someone has LVM on an encrypted block device - # executing an extra vgchange is errorless - if [[ $USELVM = [Yy][Ee][Ss] ]]; then - vgchange --sysinit -a n &>/dev/null - fi - do_lock() { - stat_append "${1}.." - if $CS remove "$1" &>/dev/null; then - stat_append "ok " - else - stat_append "failed " - fi - } - read_crypttab do_lock + # Maybe someone has LVM on an encrypted block device + # executing an extra vgchange is errorless + [[ $USELVM = [Yy][Ee][Ss] ]] && vgchange --sysinit -a n &>/dev/null + do_lock() { + stat_append "${1}.." + if $CS remove "$1" &>/dev/null; then + stat_append "ok " + else + stat_append "failed " + fi + } + read_crypttab do_lock stat_done fi -if [[ $USELVM = [Yy][Ee][Ss] && -x $(type -P lvm) && -d /sys/block ]]; then +[[ $USELVM = [Yy][Ee][Ss] && -x $(type -P lvm) && -d /sys/block ]] && status "Deactivating LVM2 groups" vgchange --sysinit -a n &>/dev/null -fi status "Remounting Root Filesystem Read-only" \ mount -n -o remount,ro / diff --git a/rc.sysinit b/rc.sysinit index 0d99aa3..c0a35df 100755 --- a/rc.sysinit +++ b/rc.sysinit @@ -66,9 +66,7 @@ if [[ $HWCLOCK_PARAMS ]]; then # configured timezone. Any new timezone put in rc.conf is copied over at # a later time. # This also sets the kernel time zone. - if [[ -f /etc/localtime ]]; then - hwclock $HWCLOCK_PARAMS - fi + [[ -f /etc/localtime ]] && hwclock $HWCLOCK_PARAMS fi # Start/trigger UDev, load MODULES and settle UDev @@ -79,14 +77,12 @@ udevd_modprobe sysinit status "Bringing up loopback interface" ip link set up dev lo # FakeRAID devices detection -if [[ $USEDMRAID = [Yy][Ee][Ss] && -x $(type -P dmraid) ]]; then +[[ $USEDMRAID = [Yy][Ee][Ss] && -x $(type -P dmraid) ]] && status "Activating FakeRAID arrays" dmraid -i -ay -fi # BTRFS devices detection -if [[ $USEBTRFS = [Yy][Ee][Ss] && -x $(type -P btrfs) ]]; then +[[ $USEBTRFS = [Yy][Ee][Ss] && -x $(type -P btrfs) ]] && status "Activating BTRFS volumes" btrfs device scan -fi activate_vgs @@ -123,9 +119,7 @@ if [[ -f /etc/crypttab && $CS ]] && grep -q ^[^#] /etc/crypttab; then # This sanity check _should_ be sufficient, but it might not. # This may cause dataloss if it is not used carefully. blkid -p "$2" &>/dev/null - if (( $? == 2 )); then - _overwriteokay=1 - fi + (( $? == 2 )) && _overwriteokay=1 fi if (( _overwriteokay == 0 )); then false @@ -206,23 +200,20 @@ fi mount_all # enable monitoring of lvm2 groups, now that the filesystems are mounted rw -if [[ $USELVM = [Yy][Ee][Ss] && -x $(type -P lvm) && -d /sys/block ]]; then +[[ $USELVM = [Yy][Ee][Ss] && -x $(type -P lvm) && -d /sys/block ]] && status "Activating monitoring of LVM2 groups" \ vgchange --monitor y >/dev/null -fi status "Activating Swap" swapon -a -if [[ $TIMEZONE ]]; then +[[ $TIMEZONE ]] && status "Configuring Time Zone" \ cp --remove-destination "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime -fi RANDOM_SEED=/var/lib/misc/random-seed -if [[ -f $RANDOM_SEED ]]; then +[[ -f $RANDOM_SEED ]] && status "Initializing Random Seed" \ cat $RANDOM_SEED > /dev/urandom -fi stat_busy "Removing Leftover Files" rm -rf /etc/{nologin,shutdownpid} /forcefsck /tmp/* /tmp/.* @@ -236,11 +227,9 @@ stat_busy "Removing Leftover Files" mkdir -m 1777 /tmp/.{X11,ICE}-unix stat_done -if [[ $HOSTNAME ]]; then - stat_busy "Setting Hostname: $HOSTNAME" - echo "$HOSTNAME" > /proc/sys/kernel/hostname - stat_done -fi +[[ $HOSTNAME ]] && + status "Setting Hostname: $HOSTNAME" \ + eval 'echo "$HOSTNAME" > /proc/sys/kernel/hostname' stat_busy "Setting Locale: ${LOCALE:=en_US}" # Flush old locale settings @@ -270,7 +259,7 @@ else echo 0 > /sys/module/vt/parameters/default_utf8 stat_done fi -[[ $KEYMAP ]] && \ +[[ $KEYMAP ]] && status "Loading Keyboard Map: $KEYMAP" loadkeys -q $KEYMAP # Set console font if required -- 1.7.1
--- rc.sysinit | 9 +++------ 1 files changed, 3 insertions(+), 6 deletions(-) diff --git a/rc.sysinit b/rc.sysinit index c0a35df..01f83f0 100755 --- a/rc.sysinit +++ b/rc.sysinit @@ -231,12 +231,9 @@ stat_done status "Setting Hostname: $HOSTNAME" \ eval 'echo "$HOSTNAME" > /proc/sys/kernel/hostname' -stat_busy "Setting Locale: ${LOCALE:=en_US}" - # Flush old locale settings - install -Tm 0755 <(:) /etc/profile.d/locale.sh - # Set user defined locale - echo "export LANG=$LOCALE" >>/etc/profile.d/locale.sh -stat_done +# Flush old locale settings and set user defined locale +status "Setting Locale: ${LOCALE:=en_US}" \ + install -Tm 0755 <(echo "export LANG=$LOCALE") /etc/profile.d/locale.sh if [[ ${LOCALE,,} =~ utf ]]; then stat_busy "Setting Consoles to UTF-8 mode" -- 1.7.1
From: "Kurt J. Bosch" <kjb-temp-2009@alpenjodel.de> --- rc.sysinit | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/rc.sysinit b/rc.sysinit index 4073fb9..75f8095 100755 --- a/rc.sysinit +++ b/rc.sysinit @@ -229,7 +229,7 @@ stat_done # Flush old locale settings and set user defined locale status "Setting Locale: ${LOCALE:=en_US}" \ - install -Tm 0755 <(echo "export LANG=$LOCALE") /etc/profile.d/locale.sh + eval 'install -Tm 0755 <(echo "export LANG=$LOCALE") /etc/profile.d/locale.sh' if [[ ${LOCALE,,} =~ utf ]]; then stat_busy "Setting Consoles to UTF-8 mode" -- 1.7.5.2
On Sat, Jun 25, 2011 at 03:17:40PM +0200, Kurt J. Bosch wrote:
From: "Kurt J. Bosch" <kjb-temp-2009@alpenjodel.de>
--- rc.sysinit | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/rc.sysinit b/rc.sysinit index 4073fb9..75f8095 100755 --- a/rc.sysinit +++ b/rc.sysinit @@ -229,7 +229,7 @@ stat_done
# Flush old locale settings and set user defined locale status "Setting Locale: ${LOCALE:=en_US}" \ - install -Tm 0755 <(echo "export LANG=$LOCALE") /etc/profile.d/locale.sh + eval 'install -Tm 0755 <(echo "export LANG=$LOCALE") /etc/profile.d/locale.sh'
if [[ ${LOCALE,,} =~ utf ]]; then stat_busy "Setting Consoles to UTF-8 mode" -- 1.7.5.2
NACK. As far as I can tell, the eval is not at all necessary. dave
Dave Reisner, 2011-06-25 15:27:
On Sat, Jun 25, 2011 at 03:17:40PM +0200, Kurt J. Bosch wrote:
From: "Kurt J. Bosch"<kjb-temp-2009@alpenjodel.de>
--- rc.sysinit | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/rc.sysinit b/rc.sysinit index 4073fb9..75f8095 100755 --- a/rc.sysinit +++ b/rc.sysinit @@ -229,7 +229,7 @@ stat_done
# Flush old locale settings and set user defined locale status "Setting Locale: ${LOCALE:=en_US}" \ - install -Tm 0755<(echo "export LANG=$LOCALE") /etc/profile.d/locale.sh + eval 'install -Tm 0755<(echo "export LANG=$LOCALE") /etc/profile.d/locale.sh'
if [[ ${LOCALE,,} =~ utf ]]; then stat_busy "Setting Consoles to UTF-8 mode" -- 1.7.5.2
NACK.
As far as I can tell, the eval is not at all necessary.
dave
Yes, that's a bit strange. When trying in a terminal it works without, but when actually booting it doesn't. Maybe some BASH bug? -- Kurt
Dave Reisner, 2011-06-25 15:27:
On Sat, Jun 25, 2011 at 03:17:40PM +0200, Kurt J. Bosch wrote:
From: "Kurt J. Bosch"<kjb-temp-2009@alpenjodel.de>
--- rc.sysinit | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/rc.sysinit b/rc.sysinit index 4073fb9..75f8095 100755 --- a/rc.sysinit +++ b/rc.sysinit @@ -229,7 +229,7 @@ stat_done
# Flush old locale settings and set user defined locale status "Setting Locale: ${LOCALE:=en_US}" \ - install -Tm 0755<(echo "export LANG=$LOCALE") /etc/profile.d/locale.sh + eval 'install -Tm 0755<(echo "export LANG=$LOCALE") /etc/profile.d/locale.sh'
if [[ ${LOCALE,,} =~ utf ]]; then stat_busy "Setting Consoles to UTF-8 mode" -- 1.7.5.2
NACK.
As far as I can tell, the eval is not at all necessary.
dave
I was able to track this down to the scripts from fbsplash (AUR). The following test case (run in a terminal) #!/bin/bash # custom override example stat_busy() { if (( PUSH_MSG )); then # This triggers the error in "$@" below # actual code was: # echo set message "$1" > "$spl_fifo" & : & fi echo "$1" BUSY } status() { stat_busy "$1" shift "$@" local retval=$? (( retval == 0 )) && echo DONE || echo FAIL "($retval)" } PUSH_MSG=0 status "test #0" cat <(echo foo) PUSH_MSG=1 status "test #1" cat <(echo foo) gives: test #0 BUSY foo DONE test #1 BUSY cat: /dev/fd/63: No such file or directory FAIL (1) BASH version installed on Arch is bash 4.2.010-1 GNU bash, version 4.2.10(2)-release (i686-pc-linux-gnu) BTW same behavior on Scientific 6 GNU bash, version 4.1.2(1)-release (i386-koji-linux-gnu) So i think we need the eval for now to work around this. CCing Tom Gundersen. -- Kurt
On Sun, Jun 26, 2011 at 1:03 PM, Kurt J. Bosch <kjb-temp-2009@alpenjodel.de> wrote:
The following test case (run in a terminal)
#!/bin/bash
# custom override example stat_busy() { if (( PUSH_MSG )); then # This triggers the error in "$@" below # actual code was: # echo set message "$1" > "$spl_fifo" & : & fi echo "$1" BUSY }
status() { stat_busy "$1" shift "$@" local retval=$? (( retval == 0 )) && echo DONE || echo FAIL "($retval)" }
PUSH_MSG=0 status "test #0" cat <(echo foo) PUSH_MSG=1 status "test #1" cat <(echo foo)
[...]
CCing Tom Gundersen.
Thanks. I have not figured it out, but here is a more minimal test: #!/bin/bash test_one() { : & "$@" } test_two() { : "$@" } # working test_one eval 'cat <(echo foo)' test_two cat <(echo foo) #not working test_one cat <(echo foo)
Tom Gundersen, 2011-06-26 13:51:
On Sun, Jun 26, 2011 at 1:03 PM, Kurt J. Bosch <kjb-temp-2009@alpenjodel.de> wrote:
The following test case (run in a terminal)
#!/bin/bash
# custom override example stat_busy() { if (( PUSH_MSG )); then # This triggers the error in "$@" below # actual code was: # echo set message "$1"> "$spl_fifo"& :& fi echo "$1" BUSY }
status() { stat_busy "$1" shift "$@" local retval=$? (( retval == 0 ))&& echo DONE || echo FAIL "($retval)" }
PUSH_MSG=0 status "test #0" cat<(echo foo) PUSH_MSG=1 status "test #1" cat<(echo foo)
[...]
CCing Tom Gundersen.
Thanks.
I have not figured it out, but here is a more minimal test:
#!/bin/bash
test_one() { : & "$@" }
test_two() { : "$@" }
# working test_one eval 'cat<(echo foo)' test_two cat<(echo foo)
#not working test_one cat<(echo foo)
Confirmed. -- Kurt
On Sun, Jun 26, 2011 at 03:12:24PM +0200, Kurt J. Bosch wrote:
Tom Gundersen, 2011-06-26 13:51:
On Sun, Jun 26, 2011 at 1:03 PM, Kurt J. Bosch <kjb-temp-2009@alpenjodel.de> wrote:
The following test case (run in a terminal)
#!/bin/bash
# custom override example stat_busy() { if (( PUSH_MSG )); then # This triggers the error in "$@" below # actual code was: # echo set message "$1"> "$spl_fifo"& :& fi echo "$1" BUSY }
status() { stat_busy "$1" shift "$@" local retval=$? (( retval == 0 ))&& echo DONE || echo FAIL "($retval)" }
PUSH_MSG=0 status "test #0" cat<(echo foo) PUSH_MSG=1 status "test #1" cat<(echo foo)
[...]
CCing Tom Gundersen.
Thanks.
I have not figured it out, but here is a more minimal test:
#!/bin/bash
test_one() { : & "$@" }
test_two() { : "$@" }
# working test_one eval 'cat<(echo foo)' test_two cat<(echo foo)
#not working test_one cat<(echo foo)
Confirmed.
-- Kurt
Now I see it (at least in Tom's example). It's expected behavior. You're only allowed to read the file descriptor created by a PE once. However, because file descriptors are inherited by subshells, when the subshell ends, the file descriptor is closed and it goes away. Look at what happens when you modify the script slightly: ------------------- #!/bin/bash test_one() { ls /dev/fd/ : & ls /dev/fd/ "$@" } test_one cat <(echo foo) ------------------- And then run it with debug output.... +foo[11]: test_one cat /dev/fd/63 +foo[4]: ls /dev/fd/ ++foo[11]: echo foo 0 1 2 3 63 +foo[6]: ls /dev/fd/ +foo[5]: : 0 1 2 3 +foo[7]: cat /dev/fd/63 cat: /dev/fd/63: No such file or directory The subshell inherits the descriptor and closes it on exit, destroying it for the parent. The eval "works" because the FD for the process substitution isn't created until _after_ the subshell. Can we _not_ do this? Our old solution didn't require voodoo like this, and it worked just fine (albeit a few more SLOC). Not everything needs to be a one line operation. Perhaps we could just write a simple function to take care of all these 1 line files that we write to: write_oneline_file() { local file=$1 line=$2 mode=$3 printf '%s\n' "$line" >"$file" [[ $mode ]] && chmod "$mode" "$file" } Which satisfies both parties, as it appears to be a one liner in execution, but it's actually sane on the back end. d
participants (3)
-
Dave Reisner
-
Kurt J. Bosch
-
Tom Gundersen