site  contact  subhomenews

Pulseaudio startup script moved to /etc/init.d

October 26, 2022 — BarryK

The saga of getting pulseaudio working in EasyOS was summarized in November 2021:

https://bkhome.org/news/202111/how-pulseaudio-is-implemented-in-easyos.html

The startup script was placed in /root/Startup, script 'pulseaudio', as wanted it to run after X has started. I did it this way, as that is how Slackware does it -- or at least how they did it back then.

I had the script in /root/Startup, which is the "old Puppy" way of running programs after X has started, but the modern way is how Slackware does it, with /etc/xdg/autostart/pulseaudio.desktop

However, I would prefer the startup script to be in /etc/init.d, as that is really the "proper" place to launch daemons.

EasyOS builds upon Busybox init, with pup_event and pup_event_ipc "service manager" to start or stop the scripts in /etc/init.d when certain conditions are met. One of those conditions is "x", set when Xorg is running. To add the condition that /etc/init.d/pulseaudio will be run, with "start" parameter, it is a simple matter of adding "x%pulseaudio" to the PE_SERVICE_MANAGER variable in file /etc/eventmanager. Showing part of that file:

#180222 simple ipc-based service manager
#ref: /etc/rc.d/rc.services, rc.services_ipc
#names of services in /etc/init.d that must only start when a condition is met...
#format of each space-delimited parameter: dep1[:dep2[...]]%service1[:service2[...]]
#ex: network%rc.samba:sshd
#180228 optional append "ONESHOT" if no "stop)" option in script.
#200107 bluetoothhw condition, refer /usr/local/pup_event/bluetoothhw
# 'bluetooth' is first, to make sure runs before 'bluealsa'...
# note, rc.services_ipc stops the services in reverse order, so bluealsa will stop first.
#200211 bluetooth requires dbus-daemon to be running, so rc.services now writes to
# /tmp/pup_event_ipc/dbus_ when dbus service has finished. put dbus as a dep...
PE_SERVICE_MANAGER='network%cups-net-fixONESHOT:qsyncONESHOT:dnsmasq:ec-net bluetoothhw:dbus%bluetooth:bluealsa x%pulseaudio'

I removed /root/Startup and created /etc/init.d/pulseaudio:

#!/bin/sh
#20211102 /etc/pulse/client.conf may have been changed if ran pw.
#20211103 note that user and group 'pulse' is only when start pulseaudio with --system.
# so code below about 'pulse' group not required. however, see /etc/pulse/default.pa
# -- users must belong to group 'audio'.
#20211109 10alsa wait. 20211114 restore volume levels.
#20211228 added optional $1 = restart
#20221026 this script moved from /root/Startup, entry added to PE_SERVICE_MANAGER in /etc/eventmanager

export DISPLAY=:0 #20221026
. /etc/profile.d/pulseaudio #20221026 coz this script runs before PULSE_RUNTIME_PATH set.
sleep 3

if [ "$1" == "restart" ];then
pulseaudio -k
sleep 0.1
/usr/bin/pulseaudio --start --log-target=syslog --disallow-exit --exit-idle-time=-1
exec start-pulseaudio-x11
fi

if [ "$1" == "stop" ];then #20221026
pulseaudio -k
exit
fi

#run start-pulseaudio-x11 in blueman_tray...
[ -x /root/Startup/blueman_tray ] && exit

#20211109 wait until /etc/init.d/10alsa has exited...
PIDa="$(cat /tmp/10alsa-PID)"
tail --pid=${PIDa} -f /dev/null

#20211109 bluetoothd will only start if pup_event detects bt i/f hardware.
# don't hang around here very long waiting for it...
#wait for bluetoothd...
if [ -x /etc/init.d/bluetooth ];then
for CNT in 0 1 2 #3 4 5 6 7 8 9 10 11 12 13 14
do
pidof bluetoothd >/dev/null
[ $? -eq 0 ] && break
sleep 1
done
echo "/etc/init.d/pulseaudio: waited ${CNT} seconds for bluetoothd"
fi

#20211114 this used to be done in /etc/init.d/10alsa
# for pa, 1st bootup, level setting and store is in /usr/sbin/delayedrun
if [ -f /etc/asound.state ];then
alsactl -f /etc/asound.state restore
fi

echo "/etc/init.d/pulseaudio: starting pulseaudio daemon"
#i have configured /etc/pulse/daemon.conf so that it doesn't exit.
# (so probably don't need respawning enabled)
#note, --system mode, daemon changes user and group to 'pulse', and users have to
# be in that group to use audio. The pulse user needs to be in the audio and
# bluetooth groups in order to be able to use ALSA and bluetooth devices.
# All users that need access to PulseAudio have to be in the pulse-access group, even root.
# /usr/bin/pulseaudio --start --log-target=syslog --system --disallow-exit
#20211103 no, run as user root,
# see ref: http://billauer.co.il/blog/2014/01/pa-multiple-users/
# ...unix socket, changes to /etc/pulse/default.pa and for non-root users ~/.config/pulse/defaut.pa
# see also /usr/local/clients/setup-client, /etc/pulse/daemon.conf,
# /etc/pulse/default.pa, /etc/pulse/client.conf
#note, --start also implies --daemonize
#note, --disallow-exit --exit-idle-time=-1 are already specified in /etc/pulse/daemon.conf
/usr/bin/pulseaudio --start --log-target=syslog --disallow-exit --exit-idle-time=-1

echo "/etc/init.d/pulseaudio: executing start-pulseaudio-x11"
exec start-pulseaudio-x11

Good, it works.

EasyOS runs only one instance of the pulseaudio daemon, and non-root client apps and apps in containers share that same instance, via two sockets. This is a compromise, as it is considered to be less secure that running separate instances; however, I just don't like the idea of lots of pulseaudio daemons running. And it could be a lot of them.

I needed to move the pulseaudio startup to /etc/init.d, as want to run it as root, when logged in as user zeus. Note, login as zeus is a work-in-progress, an experiment, see previous blog post:

https://bkhome.org/news/202210/first-baby-steps-to-running-as-user-zeus.html

I'm getting into holiday mode, so progress with zeus will be slow for awhile.   

Tags: easy