[systemd-devel] a service depending on a sound card

Andrei Borzenkov arvidjaar at gmail.com
Wed Jun 15 13:52:01 UTC 2016


On Wed, Jun 15, 2016 at 4:12 PM,  <me at beroal.in.ua> wrote:
> Hello. I want to create a service that starts after a specific sound card is
> active. I have 2 sound cards, you can see them in the output below. The
> service file contains
> {{{
> [Unit]
> After=sound.target
> After=sys-devices-pci0000:00-0000:00:05.0-sound-card0.device
> }}}
> But when the service starts, the device files of Card 0 are absent in
> "/dev/snd". To debug, I added the following shell script to the service.
> {{{
> function print_status {
>     echo "/dev:"
>     ls -l /dev/snd
>     systemctl status sys-devices-pci0000:00-0000:00:05.0-sound-card0.device
>     echo "/sys 0:"
>     ls -l /sys/devices/pci0000:00/0000:00:05.0/sound/card0
> }
> print_status
> systemctl status sound.target
> print_status
> }}}
> And this is its output:
> {{{
> /dev:
> total 0
> drwxr-xr-x 2 root root       60 Jun 15 13:21 by-path
> crw-rw---- 1 root audio 116,  2 Jun 15 13:21 controlC1
> crw-rw---- 1 root audio 116,  4 Jun 15 13:21 hwC1D0
> crw-rw---- 1 root audio 116,  3 Jun 15 13:21 pcmC1D3p
> crw-rw---- 1 root audio 116,  1 Jun 15 13:21 seq
> crw-rw---- 1 root audio 116, 33 Jun 15 13:21 timer
> * sys-devices-pci0000:00-0000:00:05.0-sound-card0.device
>    Loaded: loaded
>    Active: inactive (dead)
> /sys 0:
> ls: cannot access '/sys/devices/pci0000:00/0000:00:05.0/sound/card0': No
> such file or directory
> * sound.target - Sound Card
>    Loaded: loaded (/usr/lib/systemd/system/sound.target; static; vendor
> preset: disabled)
>    Active: active since Wed 2016-06-15 13:21:01 EEST; 912ms ago
>      Docs: man:systemd.special(7)
>
> Jun 15 13:21:01 beroal systemd[1]: Reached target Sound Card.
> /dev:
> total 0
> drwxr-xr-x 2 root root       80 Jun 15 13:21 by-path
> crw-rw---- 1 root audio 116,  5 Jun 15 13:21 controlC0
> crw-rw---- 1 root audio 116,  2 Jun 15 13:21 controlC1
> crw-rw---- 1 root audio 116, 10 Jun 15 13:21 hwC0D0
> crw-rw---- 1 root audio 116,  4 Jun 15 13:21 hwC1D0
> crw-rw---- 1 root audio 116,  7 Jun 15 13:21 pcmC0D0c
> crw-rw---- 1 root audio 116,  6 Jun 15 13:21 pcmC0D0p
> crw-rw---- 1 root audio 116,  8 Jun 15 13:21 pcmC0D1p
> crw-rw---- 1 root audio 116,  9 Jun 15 13:21 pcmC0D2c
> crw-rw---- 1 root audio 116,  3 Jun 15 13:21 pcmC1D3p
> lrwxrwxrwx 1 root root        9 Jun 15 13:21 perm-mb -> controlC0
> crw-rw---- 1 root audio 116,  1 Jun 15 13:21 seq
> crw-rw---- 1 root audio 116, 33 Jun 15 13:21 timer
> * sys-devices-pci0000:00-0000:00:05.0-sound-card0.device - MCP61 High
> Definition Audio
>    Loaded: loaded
>    Active: active (plugged) since Wed 2016-06-15 13:21:02 EEST; 711ms ago
>    Device: /sys/devices/pci0000:00/0000:00:05.0/sound/card0
> /sys 0:
> total 0
> drwxr-xr-x 3 root root    0 Jun 15 13:21 controlC0
> lrwxrwxrwx 1 root root    0 Jun 15 13:21 device -> ../../../0000:00:05.0
> drwxr-xr-x 3 root root    0 Jun 15 13:21 hwC0D0
> -rw-r--r-- 1 root root 4096 Jun 15 13:21 id
> drwxr-xr-x 6 root root    0 Jun 15 13:21 input12
> drwxr-xr-x 6 root root    0 Jun 15 13:21 input13
> drwxr-xr-x 6 root root    0 Jun 15 13:21 input14
> -r--r--r-- 1 root root 4096 Jun 15 13:21 number
> drwxr-xr-x 3 root root    0 Jun 15 13:21 pcmC0D0c
> drwxr-xr-x 3 root root    0 Jun 15 13:21 pcmC0D0p
> drwxr-xr-x 3 root root    0 Jun 15 13:21 pcmC0D1p
> drwxr-xr-x 3 root root    0 Jun 15 13:21 pcmC0D2c
> drwxr-xr-x 2 root root    0 Jun 15 13:21 power
> lrwxrwxrwx 1 root root    0 Jun 15 13:21 subsystem ->
> ../../../../../class/sound
> -rw-r--r-- 1 root root 4096 Jun 15 13:21 uevent
> }}}
>
> Questions.
> 0. When "sound.target" is supposed to be active, when any sound card is
> active or when all sound cards are active?

sound.target is started by udev rule when any device that belongs to
"sound" subsystem appears. In general it can be used to avoid starting
sound-related services unconditionally if no hardware is present (i.e.
- make then to be WantedBy sound.target). It does not offer any
promises regarding order in which services are started.

> 1. Why the service starts when Card 0 is inactive?

After is effective only between concurrent jobs. To make systemd
respect After in this case you need to make sure corresponding job to
"start" device is also queued every time your service is started. It
makes sense to use BindsTo for it - it also makes sure service is
stopped when device disappears.

> 2. Why "systemctl status sound.target" waits until Card 0 is active? It does
> not make sense.

It does not wait for anything. It is simply that starting of
sound.target is initiated only after systemd detects that Card 0 is
present.


More information about the systemd-devel mailing list