Skip to content

Commit

Permalink
Implement OpenRC support by providing RC service script
Browse files Browse the repository at this point in the history
This commit adds a service script for an OpenRC init system.
It intends to mitigate "race" caused by a start of long-living process
(that is `usbmuxd` itself) by udev rules, which is highly discouraged by
the `udev` manpage:
"Starting daemons or other long-running processes is not allowed; the
forked processes, detached or not, will be unconditionally killed after
the event handling has finished."
There is a bug in `usbmuxd`, which causes the hang, but this is to be
handled in a different commit.

This adds two new `configure` flags:
* `--with-openrc`: toggles on or off (default - off) OpenRC service
  installation and udev activation via that. Conflicts with systemd, so
  the latter must be disabled explicitly:
  `--with-openrc --without-systemd`
* `--with-rcservicedir=DIR`: specifies directory where to install service
  file. Defaults to `$sysconfdir/init.d` (which is `/etc/init.d`)

Issue: #210
Signed-off-by: BalkanMadman <[email protected]>
  • Loading branch information
BalkanMadman committed Jan 16, 2024
1 parent 360619c commit 98601f5
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AUTOMAKE_OPTIONS = foreign
ACLOCAL_AMFLAGS = -I m4
SUBDIRS = src $(UDEV_SUB) $(SYSTEMD_SUB) docs
SUBDIRS = src $(UDEV_SUB) $(SYSTEMD_SUB) $(OPENRC_SUB) docs

EXTRA_DIST = \
docs \
Expand All @@ -10,4 +10,4 @@ EXTRA_DIST = \

DISTCHECK_CONFIGURE_FLAGS = \
--with-udevrulesdir=$$dc_install_base/$(udevrulesdir) \
--with-systemdsystemunitdir=$$dc_install_base/$(systemdsystemunitdir)
--with-systemdsystemunitdir=$$dc_install_base/$(systemdsystemunitdir)
37 changes: 35 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,56 @@ if test "x$with_udevrulesdir" = "xauto"; then
fi
fi

AC_ARG_WITH([openrc],
[AS_HELP_STRING([--with-openrc],
[build with support for OpenRC init system @<:@default=no@:>@])],
[],
[with_openrc=no])

AC_ARG_WITH([systemd],
[AS_HELP_STRING([--without-systemd],
[do not build with systemd support @<:@default=yes@:>@])],
[with_systemd=$withval],
[with_systemd=yes])

if test "x$with_systemd" = xyes -a "x$with_openrc" = xyes; then
AC_MSG_ERROR([Both systemd and OpenRC support can't be enabled at the same time])
fi


AC_ARG_WITH([systemdsystemunitdir],
[AS_HELP_STRING([--with-systemdsystemunitdir=DIR],
[directory for systemd service files])],
[with_systemdsystemunitdir=$withval],
[with_systemdsystemunitdir=$($PKG_CONFIG --variable=systemdsystemunitdir systemd)])

if test "x$with_systemdsystemunitdir" != xno; then
if test "x$with_systemdsystemunitdir" != "xno"; then
AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])
fi

AC_ARG_WITH([rcservicedir],
[AS_HELP_STRING([--with-rcservicedir=DIR],
[directory for OpenRC service files])],
[rcservicedir="$withval"],
[rcservicedir="$sysconfdir/init.d"])
if test "x$with_rcservicedir" != "xno"; then
AC_SUBST([rcservicedir], [$rcservicedir])
fi

AM_CONDITIONAL(WANT_SYSTEMD, [test -n "$with_systemdsystemunitdir" -a "x$with_systemdsystemunitdir" != xno -a "x$with_systemd" = "xyes" ])
AM_CONDITIONAL(WANT_OPENRC, [test "x$with_openrc" = xyes -a "x$with_rcservicedir" != "xno" ])

if test "x$with_systemd" = xyes; then
udev_activation_rule="ENV{SYSTEMD_WANTS}=\"usbmuxd.service\""
udev_deactivation_rule="RUN+=\"@sbindir@/usbmuxd --exit\""
elif test "x$with_openrc" = "xyes" -a "x$with_rcservicedir" != "xno"; then
udev_activation_rule="RUN+=\"@rcservicedir@/usbmuxd start\""
udev_deactivation_rule="RUN+=\"@rcservicedir@/usbmuxd stop\""
else
udev_activation_rule="RUN+=\"@sbindir@/usbmuxd --user usbmux --udev\""
udev_deactivation_rule="RUN+=\"@sbindir@/usbmuxd --exit\""
fi
AC_SUBST(udev_activation_rule)
AC_SUBST(udev_deactivation_rule)

# Checks for header files.
AC_CHECK_HEADERS([stdint.h stdlib.h string.h])
Expand All @@ -117,6 +144,7 @@ AC_CHECK_FUNCS([ppoll clock_gettime localtime_r])
# Check for operating system
AC_MSG_CHECKING([whether to enable WIN32 build settings])
UDEV_SUB=
OPENRC_SUB=
SYSTEMD_SUB=
case ${host_os} in
*mingw32*|*cygwin*)
Expand All @@ -142,6 +170,9 @@ case ${host_os} in
if test "x$with_systemd" != "xyes"; then
echo "*** Note: support for systemd activation has been disabled, using udev activation instead ***"
activation_method="udev"
if test "x$with_openrc" = "xyes"; then
OPENRC_SUB=openrc
fi
else
AC_DEFINE(HAVE_SYSTEMD, 1, [Define to enable systemd support])
SYSTEMD_SUB=systemd
Expand All @@ -151,6 +182,7 @@ esac
AM_CONDITIONAL(WIN32, test x$win32 = xtrue)

AC_SUBST([UDEV_SUB])
AC_SUBST([OPENRC_SUB])
AC_SUBST([SYSTEMD_SUB])

AS_COMPILER_FLAGS(GLOBAL_CFLAGS, "-g -Wall -Wextra -Wmissing-declarations -Wredundant-decls -Wshadow -Wpointer-arith -Wwrite-strings -Wswitch-default -Wno-unused-parameter")
Expand All @@ -168,6 +200,7 @@ Makefile
src/Makefile
udev/Makefile
systemd/Makefile
openrc/Makefile
docs/Makefile
])
AC_OUTPUT
Expand Down
20 changes: 20 additions & 0 deletions openrc/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
if WANT_OPENRC
edit = \
$(SED) -r \
-e 's|@sbindir[@]|$(sbindir)|g' \
< $< > $@ || rm $@

rcservice_SCRIPTS = usbmuxd

usbmuxd: usbmuxd.in
$(edit)

EXTRA_DIST = \
usbmuxd.in

MAINTAINERCLEANFILES = \
usbmuxd

CLEANFILES = \
usbmuxd
endif
9 changes: 9 additions & 0 deletions openrc/usbmuxd.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/sbin/openrc-run
# Copyright 2023 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

name="USB multiplexing daemon"
description="Socket daemon for the usbmux protocol used by Apple devices"
command="@sbindir@/usbmuxd"
command_args="--user usbmux"
pidfile="/run/${RC_SVCNAME}.pid"
4 changes: 2 additions & 2 deletions udev/39-usbmuxd.rules.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ENV{PRODUCT}=="5ac/8600/*", ACTION


# Make sure properties don't get lost when bind action is called
SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ENV{PRODUCT}=="5ac/12[9a][0-9a-f]/*|5ac/190[1-5]/*|5ac/8600/*", ACTION=="bind", ENV{USBMUX_SUPPORTED}="1", OWNER="usbmux"
SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ENV{PRODUCT}=="5ac/12[9a][0-9a-f]/*|5ac/190[1-5]/*|5ac/8600/*", ACTION=="bind", ENV{USBMUX_SUPPORTED}="1", OWNER="usbmux", @udev_activation_rule@

# Exit usbmuxd when the last device is removed
SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ENV{PRODUCT}=="5ac/12[9a][0-9a-f]/*|5ac/190[1-5]/*|5ac/8600/*", ACTION=="remove", RUN+="@sbindir@/usbmuxd -x"
SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ENV{PRODUCT}=="5ac/12[9a][0-9a-f]/*|5ac/190[1-5]/*|5ac/8600/*", ACTION=="remove", @udev_deactivation_rule@
2 changes: 2 additions & 0 deletions udev/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
edit = \
$(SED) -r \
-e 's|@udev_activation_rule[@]|$(udev_activation_rule)|g' \
-e 's|@udev_deactivation_rule[@]|$(udev_deactivation_rule)|g' \
-e 's|@sbindir[@]|$(sbindir)|g' \
-e 's|@rcservicedir[@]|$(rcservicedir)|g' \
< $< > $@ || rm $@

udevrules_DATA = \
Expand Down

0 comments on commit 98601f5

Please sign in to comment.