From 577538f213cd7132c7450db393fd6fef6bb46b5a Mon Sep 17 00:00:00 2001
From: Martin Szulecki
Date: Sat, 26 Jul 2014 17:08:41 +0200
Subject: Add systemd activation autofoo with fallback for udev and manual
 activation

---
 .gitignore                 |  3 +-
 Makefile.am                |  7 ++--
 configure.ac               | 82 +++++++++++++++++++++++++++++++++++++++++++---
 systemd/Makefile.am        | 21 ++++++++++++
 systemd/usbmuxd.service.in |  7 ++++
 udev/39-usbmuxd.rules.in   |  8 ++---
 udev/Makefile.am           | 20 +++++++++++
 7 files changed, 136 insertions(+), 12 deletions(-)
 create mode 100644 systemd/Makefile.am
 create mode 100644 systemd/usbmuxd.service.in
 create mode 100644 udev/Makefile.am

diff --git a/.gitignore b/.gitignore
index 9a9e6e6..4e65390 100644
--- a/.gitignore
+++ b/.gitignore
@@ -34,4 +34,5 @@ py-compile
 stamp-h1
 src/.libs
 src/usbmuxd
-udev/85-usbmuxd.rules
+udev/39-usbmuxd.rules
+systemd/usbmuxd.service
diff --git a/Makefile.am b/Makefile.am
index 4ee9db6..789b9b0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,7 @@
 AUTOMAKE_OPTIONS = foreign
 ACLOCAL_AMFLAGS = -I m4
-SUBDIRS = src
+SUBDIRS = src $(UDEV_SUB) $(SYSTEMD_SUB)
 
-udevrulesdir = /lib/udev/rules.d
-udevrules_DATA = udev/39-usbmuxd.rules
+DISTCHECK_CONFIGURE_FLAGS =				\
+	--with-udevrulesdir=$$dc_install_base/$(udevrulesdir) \
+	--with-systemdsystemunitdir=$$dc_install_base/$(systemdsystemunitdir)
\ No newline at end of file
diff --git a/configure.ac b/configure.ac
index 6737f6f..33f6151 100644
--- a/configure.ac
+++ b/configure.ac
@@ -42,6 +42,45 @@ else
   fi
 fi
 
+AC_ARG_WITH([udevrulesdir],
+            AS_HELP_STRING([--with-udevrulesdir=DIR],
+            [Directory for udev rules]),
+            [],
+            [with_udevrulesdir=auto])
+if test "x$with_udevrulesdir" = "xauto"; then
+  udevdir=$($PKG_CONFIG --variable=udevdir udev)
+  if test "x$udevdir" != "x"; then
+    with_udevrulesdir=$udevdir"/rules.d"
+  else
+    with_udevrulesdir=$prefix/lib/udev/rules.d
+  fi
+fi
+AC_SUBST([udevrulesdir], [$with_udevrulesdir])
+
+AC_ARG_WITH([systemd],
+            [AS_HELP_STRING([--without-systemd],
+            [do not build with systemd support @<:@default=yes@:>@])],
+            [with_systemd=no],
+            [with_systemd=yes])
+
+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
+  AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])
+fi
+AM_CONDITIONAL(HAVE_SYSTEMD, [test -n "$with_systemdsystemunitdir" -a "x$with_systemdsystemunitdir" != xno -a "x$with_systemd" = "xyes" ])
+
+if test "x$with_systemd" = xyes; then
+  udev_activation_rule="TAG+=\"systemd\", ENV{SYSTEMD_WANTS}=\"usbmuxd.service\""
+else
+  udev_activation_rule="RUN+=\"@sbindir@/usbmuxd -u -U usbmux\""
+fi
+AC_SUBST(udev_activation_rule)
+
 # Checks for header files.
 AC_HEADER_STDC
 AC_CHECK_HEADERS([stdint.h stdlib.h string.h])
@@ -61,20 +100,40 @@ AC_CHECK_FUNCS([strcasecmp strdup strerror strndup stpcpy])
 
 # Check for operating system
 AC_MSG_CHECKING([whether to enable WIN32 build settings])
+UDEV_SUB=
+SYSTEMD_SUB=
 case ${host_os} in
   *mingw32*|*cygwin*)
     win32=true
     AC_MSG_RESULT([yes])
     AC_CHECK_TOOL([WINDRES], [windres], AC_MSG_ERROR([windres not found]))
     AC_SUBST(WINDRES)
+    activation_method="manual"
+    ;;
+  darwin*)
+    win32=false
+    AC_MSG_RESULT([no])
+    activation_method="launchd"
     ;;
   *)
     win32=false
     AC_MSG_RESULT([no])
+    UDEV_SUB=udev
+    activation_method="systemd"
+    if test "x$with_systemd" != "xyes"; then
+      echo "*** Note: support for systemd activation has been disabled, using udev activation instead ***"
+      activation_method="udev"
+    else
+      AC_DEFINE(WITH_SYSTEMD, 1, [Define to enable systemd support])
+      SYSTEMD_SUB=systemd
+    fi
     ;;
 esac
 AM_CONDITIONAL(WIN32, test x$win32 = xtrue)
 
+AC_SUBST([UDEV_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")
 AC_SUBST(GLOBAL_CFLAGS)
 
@@ -83,15 +142,30 @@ m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])])
 AC_OUTPUT([
 Makefile
 src/Makefile
-udev/39-usbmuxd.rules
+udev/Makefile
+systemd/Makefile
 ])
 
-echo "
+echo -n "
 Configuration for $PACKAGE $VERSION:
 -------------------------------------------
 
-  Install prefix: ...........: $prefix
-  Preflight worker support ..: $have_limd
+  install prefix ............: $prefix
+  preflight worker support ..: $have_limd
+  activation method .........: $activation_method
+"
+
+if test "x$activation_method" = "xsystemd"; then
+  echo -n "  systemd unit directory ....: ${systemdsystemunitdir}
+"
+fi
+
+if test -n "$udevrulesdir"; then
+  echo -n "  udev rules directory ......: ${udevrulesdir}
+"
+fi
+
+echo "  compiler ..................: ${CC}
 
   Now type 'make' to build $PACKAGE $VERSION,
   and then 'make install' for installation.
diff --git a/systemd/Makefile.am b/systemd/Makefile.am
new file mode 100644
index 0000000..6626d60
--- /dev/null
+++ b/systemd/Makefile.am
@@ -0,0 +1,21 @@
+edit = \
+	$(SED) -r \
+	-e 's|@sbindir[@]|$(sbindir)|g' \
+	-e 's|@localstatedir[@]|$(localstatedir)|g' \
+	< $< > $@ || rm $@
+
+if HAVE_SYSTEMD
+
+systemdsystemunit_DATA = \
+	usbmuxd.service
+
+usbmuxd.service: usbmuxd.service.in
+	$(edit)
+
+EXTRA_DIST = \
+	usbmuxd.service.in
+
+CLEANFILES = \
+	usbmuxd.service
+
+endif
diff --git a/systemd/usbmuxd.service.in b/systemd/usbmuxd.service.in
new file mode 100644
index 0000000..761b39a
--- /dev/null
+++ b/systemd/usbmuxd.service.in
@@ -0,0 +1,7 @@
+[Unit]
+Description=Socket daemon for the usbmux protocol used by Apple devices
+
+[Service]
+Type=simple
+ExecStart=@sbindir@/usbmuxd -u -U usbmux -f
+PIDFile=@localstatedir@/run/usbmuxd.pid
diff --git a/udev/39-usbmuxd.rules.in b/udev/39-usbmuxd.rules.in
index f4eb66f..91b14db 100644
--- a/udev/39-usbmuxd.rules.in
+++ b/udev/39-usbmuxd.rules.in
@@ -1,7 +1,7 @@
-# usbmuxd ("Apple Mobile Device" muxer listening on /var/run/usbmuxd)
+# usbmuxd (Apple Mobile Device Muxer listening on /var/run/usbmuxd)
 
-# Initialize iOS devices into "deactivated" USB configuration state and run usbmuxd
-ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="05ac", ATTR{idProduct}=="12[9a][0-9a-f]", ENV{USBMUX_SUPPORTED}="1", ATTR{bConfigurationValue}="0", OWNER="usbmux", RUN+="@prefix@/sbin/usbmuxd -u -U usbmux"
+# Initialize iOS devices into "deactivated" USB configuration state and activate usbmuxd
+ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="05ac", ATTR{idProduct}=="12[9a][0-9a-f]", ENV{USBMUX_SUPPORTED}="1", ATTR{bConfigurationValue}="0", OWNER="usbmux", @udev_activation_rule@
 
 # Exit usbmuxd when the last device is removed
-ACTION=="remove", SUBSYSTEM=="usb", ENV{PRODUCT}=="5ac/12[9a][0-9a-f]/*", ENV{INTERFACE}=="255/*", RUN+="@prefix@/sbin/usbmuxd -x"
+ACTION=="remove", SUBSYSTEM=="usb", ENV{PRODUCT}=="5ac/12[9a][0-9a-f]/*", ENV{INTERFACE}=="255/*", RUN+="@sbindir@/usbmuxd -x"
diff --git a/udev/Makefile.am b/udev/Makefile.am
new file mode 100644
index 0000000..95f565d
--- /dev/null
+++ b/udev/Makefile.am
@@ -0,0 +1,20 @@
+edit = \
+	$(SED) -r \
+	-e 's|@udev_activation_rule[@]|$(udev_activation_rule)|g' \
+	-e 's|@sbindir[@]|$(sbindir)|g' \
+	< $< > $@ || rm $@
+
+udevrules_DATA = \
+	39-usbmuxd.rules
+
+39-usbmuxd.rules: 39-usbmuxd.rules.in
+	$(edit)
+
+EXTRA_DIST = \
+	39-usbmuxd.rules.in
+
+MAINTAINERCLEANFILES = \
+	39-usbmuxd.rules
+
+CLEANFILES = \
+	39-usbmuxd.rules
-- 
cgit v1.1-32-gdbae