Skip to content

Instantly share code, notes, and snippets.

@bossjones
Created June 30, 2017 23:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bossjones/cadad5d4f036f2496e1b31f12c0f1ef2 to your computer and use it in GitHub Desktop.
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"
#!/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
#!/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"
@bossjones
Copy link
Author

Why aren't ${ADDRESS_FILE} and ${PID_FILE} being substituted correctly?

Busted code ...

jhbuild_pygobject3_1  |  [verify] ADDRESS_FILE:${ADDRESS_FILE}
jhbuild_pygobject3_1  |  [verify] PID_FILE:${PID_FILE}

## Extended Output


jhbuild_pygobject3_1  | [cont-init.d] 00-init-env: exited 0.
jhbuild_pygobject3_1  | [cont-init.d] 01-init-ssh: executing...
jhbuild_pygobject3_1  |  [run] ssh-keygen -A
jhbuild_pygobject3_1  |  [run] Generate SSH host keys on container (re)start if they are not present
jhbuild_pygobject3_1  | [cont-init.d] 01-init-ssh: exited 0.
jhbuild_pygobject3_1  | [cont-init.d] 30-init-dbus: executing...
jhbuild_pygobject3_1  |  [run] ADDRESS_FILE:/tmp/pydbustest_address.6p5o0iju5
jhbuild_pygobject3_1  |  [run] PID_FILE:/tmp/pydbustest_pid_file.B8IE8jvMq
jhbuild_pygobject3_1  | multisubstitute: usage: see http://skarnet.org/software/execline/multisubstitute.html
jhbuild_pygobject3_1  |  [verify] ADDRESS_FILE:${ADDRESS_FILE}
jhbuild_pygobject3_1  |  [verify] PID_FILE:${PID_FILE}
jhbuild_pygobject3_1  | [cont-init.d] 30-init-dbus: exited 0.
jhbuild_pygobject3_1  | [cont-init.d] 40-init-jhbuild: executing...
jhbuild_pygobject3_1  | [run] starting Jhbuild
jhbuild_pygobject3_1  |  [run] SCARLETT_BUILD_GNOME not set, moving on ...
jhbuild_pygobject3_1  | [cont-init.d] 40-init-jhbuild: exited 0.
jhbuild_pygobject3_1  | [cont-init.d] 50-init-dgoss: executing...
jhbuild_pygobject3_1  |  [run] goss jhbuild tests
jhbuild_pygobject3_1  |  [run] env before multisubstitute
jhbuild_pygobject3_1  |  [run] multisubstitute for GOSS TESTS
jhbuild_pygobject3_1  |  [run] AFTER s6-env -i

@bossjones
Copy link
Author

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