Created
June 30, 2017 23:22
-
-
Save bossjones/cadad5d4f036f2496e1b31f12c0f1ef2 to your computer and use it in GitHub Desktop.
Trying to make "cont-init.d/30-init-dbus" similar to "init_dbus.sh"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/execlineb -P | |
# NOTE: cont-init.d/30-init-dbus | |
# -*- mode: bash -*- | |
# vi: set ft=bash: | |
with-contenv | |
trap -x | |
{ | |
term | |
{ | |
foreground | |
{ | |
# find location of PID_FILE | |
backtick -n PID_FILE { redirfd -w 2 /dev/null find /tmp -name 'pydbustest_pid_file*' -print } | |
foreground { s6-echo " [sigterm-dbus-daemon] PID_FILE:${PID_FILE}" } | |
importas PID_FILE PID_FILE | |
backtick -n ADDRESS_FILE { redirfd -w 2 /dev/null find /tmp -name 'pydbustest_address*' -print } | |
foreground { s6-echo " [sigterm-dbus-daemon] ADDRESS_FILE:${ADDRESS_FILE}" } | |
importas ADDRESS_FILE ADDRESS_FILE | |
# find PID number | |
backtick -n PID { redirfd -w 2 /dev/null cat ${PID_FILE} } | |
foreground { s6-echo " [sigterm-dbus-daemon] PID:${PID}" } | |
importas PID PID | |
# kill process | |
foreground { | |
kill -TERM ${PID} | |
rm ${ADDRESS_FILE} | |
rm ${PID_FILE} | |
} | |
} | |
echo [sigterm-dbus-daemon] graceful shutdown complete | |
} | |
} | |
foreground { | |
backtick -n ADDRESS_FILE { redirfd -w 2 /dev/null s6-applyuidgid -u 1000 -g 1000 mktemp /tmp/pydbustest_address.XXXXXXXXX } | |
importas ADDRESS_FILE ADDRESS_FILE | |
backtick -n PID_FILE { redirfd -w 2 /dev/null s6-applyuidgid -u 1000 -g 1000 mktemp /tmp/pydbustest_pid_file.XXXXXXXXX } | |
importas PID_FILE PID_FILE | |
foreground { sudo chown pi:pi -R ${ADDRESS_FILE} ${PID_FILE} } | |
foreground { s6-echo " [run] ADDRESS_FILE:${ADDRESS_FILE}" } | |
foreground { s6-echo " [run] PID_FILE:${PID_FILE}" } | |
} | |
foreground { | |
multisubstitute { | |
importas ADDRESS_FILE ADDRESS_FILE | |
importas PID_FILE PID_FILE | |
} | |
} | |
foreground { s6-echo " [verify] ADDRESS_FILE:${ADDRESS_FILE}" } | |
foreground { s6-echo " [verify] PID_FILE:${PID_FILE}" } | |
redirfd -w 0 $ADDRESS_FILE | |
redirfd -w 1 $PID_FILE | |
s6-applyuidgid -u 1000 -g 1000 dbus-daemon --session --print-address=0 --print-pid=1 --fork |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# original script | |
set -e | |
# Startup dbus session | |
ADDRESS_FILE=$(mktemp /tmp/pydbustest.XXXXXXXXX) | |
PID_FILE=$(mktemp /tmp/pydbustest.XXXXXXXXX) | |
dbus-daemon --session --print-address=0 --print-pid=1 --fork 0>"$ADDRESS_FILE" 1>"$PID_FILE" | |
export DBUS_SESSION_BUS_ADDRESS=$(cat "$ADDRESS_FILE") | |
PID=$(cat "$PID_FILE") | |
echo "export DBUS_SESSION_BUS_ADDRESS=$(cat "$ADDRESS_FILE")" > ~pi/.exports | |
echo "D-Bus per-session daemon address is: $DBUS_SESSION_BUS_ADDRESS" | |
trap 'kill -TERM $PID' EXIT | |
rm "$ADDRESS_FILE" "$PID_FILE" |
This was the fix! Parallel multi-substitution is needed ... serial substitution causes problems.
#!/usr/bin/execlineb -P
# -*- mode: bash -*-
# vi: set ft=bash:
with-contenv
trap -x
{
term
{
foreground
{
# find location of PID_FILE
backtick -n PID_FILE { redirfd -w 2 /dev/null find /tmp -name 'pydbustest_pid_file*' -print }
backtick -n ADDRESS_FILE { redirfd -w 2 /dev/null find /tmp -name 'pydbustest_address*' -print }
backtick -n PID { redirfd -w 2 /dev/null cat ${PID_FILE} }
multisubstitute {
importas PID_FILE PID_FILE
importas ADDRESS_FILE ADDRESS_FILE
importas PID PID
}
foreground { s6-echo " [sigterm-dbus-daemon] PID_FILE:${PID_FILE}" }
foreground { s6-echo " [sigterm-dbus-daemon] ADDRESS_FILE:${ADDRESS_FILE}" }
foreground { s6-echo " [sigterm-dbus-daemon] PID:${PID}" }
# kill process
foreground {
kill -TERM ${PID}
rm ${ADDRESS_FILE}
rm ${PID_FILE}
}
}
s6-echo " [sigterm-dbus-daemon] graceful shutdown complete"
}
}
backtick -n ADDRESS_FILE { redirfd -w 2 /dev/null s6-applyuidgid -u 1000 -g 1000 mktemp /tmp/pydbustest_address.XXXXXXXXX }
backtick -n PID_FILE { redirfd -w 2 /dev/null s6-applyuidgid -u 1000 -g 1000 mktemp /tmp/pydbustest_pid_file.XXXXXXXXX }
multisubstitute {
importas ADDRESS_FILE ADDRESS_FILE
importas PID_FILE PID_FILE
# NOTE: This is needed for pydbus
importas DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_ADDRESS
}
foreground { sudo chown pi:pi -R ${ADDRESS_FILE} ${PID_FILE} }
foreground { s6-echo " [run] ADDRESS_FILE:${ADDRESS_FILE}" }
foreground { s6-echo " [run] PID_FILE:${PID_FILE}" }
foreground { s6-echo " [run] starting dbus-daemon ..." }
# https://github.com/search?q=redirfd+-w+1&type=Code&utf8=%E2%9C%93
# https://github.com/shirley-zhou/CS111/blob/052f16e0efcf5c83d042b7ef43f0b0f0f9bbb9df/lab1/exec5.sh
# NOTE: redirfd program
# redirfd redirects a given file descriptor to a file, then executes a program.
# redirfd [ -r | -w | -u | -a | -c | -x ] [ -n | -b ] fd file prog...
# -w : open file for writing, truncating it if it already exists.
# 0 Standard input STDIN_FILENO stdin
# 1 Standard output STDOUT_FILENO stdout
# 2 Standard error STDERR_FILENO stderr
redirfd -w 0 $ADDRESS_FILE
redirfd -w 1 $PID_FILE
s6-applyuidgid -u 1000 -g 1000 dbus-daemon --session --print-address=0 --print-pid=1 --fork
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why aren't
${ADDRESS_FILE}
and${PID_FILE}
being substituted correctly?Busted code ...
## Extended Output