Running mpc from udev rule fails with Address family not supported by protocol

Need help with MPD?
Post Reply
owl770
Posts: 3
Joined: September 26th, 2017, 10:28 am

Running mpc from udev rule fails with Address family not supported by protocol

Post by owl770 »

I have a bluetooth speaker that works with MPD on a RPi. When I turn the bluetooth speaker on, it auto connects with the RPi. I have a script that runs (via udev) when the speaker connects/disconnects. When it connects, the script enables the MPD output for the bluetooth speaker and then starts MPD playing. When the speaker disconnects, the script disables the MPD output (and does nothing with the MPD play state).

If I run the script in a shell, it works as expected. When the speaker connects, it enables the MPD output and starts MPD playing. When the speaker disconnects, it disables the MPD output.

When udev runs the script, each mpc command eventually times out with the message:
MPD error: Address family not supported by protocol
I have ipv6 disabled on my RPi. I have tried enabling ipv6 but the mpc commands still timeout but with a timeout message rather than the
MPD error: Address family not supported by protocol
message.

I assume there must be something in the udev environment causing the failure but I can't figure it out. I have tried various quoting around the mpc commands. I have tried running the mpc commands with
/bin/sh -c ...
and
sudo -i -u pi ...
but nothing works.

Any suggestions???

The contents of the udev rule (
/etc/udev/rules.d/10-bluetooth.rules
) is:

Code: Select all

SUBSYSTEM=="input", SUBSYSTEMS=="input", ATTRS{name}=="B8:F6:53:F3:81:A6", RUN+="/usr/local/bin/bt_connect"
The contents of
/usr/local/bin/bt_connect
is:

Code: Select all

#!/bin/bash
#
# This script is run by udev when the bluetooth speaker connects
# It will turn on the MPD output for the speaker and start playing
# or turn off the MPD output if the speaker disconnects
#

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PROG=`basename $0`
MPD_OUTPUT_NAME="Bluetooth Speaker"
MPD_HOST="localhost"
MPD_PORT="6601"
MPC="mpc"


doit() {
if [ "$ACTION" = "remove" ]
then
	logger -p user.info -t $PROG "The bluetooth speaker has disconnected"
	logger -p user.info -t $PROG "Disabling the MPD output $MPD_OUTPUT_NAME"
	set -x
	$MPC -h $MPD_HOST -p $MPD_PORT disable "$MPD_OUTPUT_NAME"
	set +x
elif [ "$ACTION" = "add" ]
then
	logger -p user.info -t $PROG "The bluetooth speaker has connected"
	logger -p user.info -t $PROG "Enabling the MPD output $MPD_OUTPUT_NAME"
	set -x
	$MPC -h $MPD_HOST -p $MPD_PORT enable "$MPD_OUTPUT_NAME"
	logger -p user.info -t $PROG "Sending MPD the play command"
	$MPC -h $MPD_HOST -p $MPD_PORT play
	set +x
else
	logger -p user.warning -t $PROG "An unknown action of <$ACTION> was received. Aborting."
fi
}

doit >/tmp/$PROG.out 2>&1
exit 0
Thanks for any assistance you can provide.

owl770
Posts: 3
Joined: September 26th, 2017, 10:28 am

Re: Running mpc from udev rule fails with Address family not supported by protocol

Post by owl770 »

I think this has something to do with the namespace that udev uses/runs in. I have moved my bt_connect script to a systemd service and got it working.

The udev rules file now looks like this:

Code: Select all

SUBSYSTEM=="input", SUBSYSTEMS=="input", ACTION=="add", ATTRS{name}=="B8:F6:53:F3:81:A6", RUN+="/bin/systemctl start bt_connect.service"
SUBSYSTEM=="input", SUBSYSTEMS=="input", ACTION=="remove", ATTRS{name}=="B8:F6:53:F3:81:A6", RUN+="/bin/systemctl start bt_disconnect.service"
The bt_connect.service looks like this:

Code: Select all

[Unit]
Description=Run the bt_connect script when the bluetooth speaker connects

[Service]
ExecStart=/usr/local/bin/bt_connect add

[Install]
WantedBy=mpd.service
The bt_disconnect.service looks like this:

Code: Select all

[Unit]
Description=Run the bt_connect script when the bluetooth speaker disconnects

[Service]
ExecStart=/usr/local/bin/bt_connect remove

[Install]
WantedBy=mpd.service
and I changed the bt_connect script to accept an argument (either 'add' or 'remove') and process the commands accordingly.

Works just like I want it to. Hope this helps someone else in the future.

max
Forum team
Posts: 1024
Joined: January 15th, 2013, 3:43 pm

Re: Running mpc from udev rule fails with Address family not supported by protocol

Post by max »

Another workaround could be connecting to MPD with a local socket.

Post Reply