Submitted By:            Armin K. <krejzi at email dot com>
Date:                    2014-02-08
Initial Package Version: 1.7.1
Upstream Status:         Unknown
Origin:                  ArchLinux
Description:             Adds systemd units and systemd socket activation capability to cupsd.

--- a/config.h.in	2014-02-27 16:57:59.000000000 +0100
+++ b/config.h.in	2014-04-11 23:05:56.075331880 +0200
@@ -451,6 +451,13 @@
 
 
 /*
+ * Do we have systemd support?
+ */
+
+#undef HAVE_SYSTEMD
+
+
+/*
  * Various scripting languages...
  */
 
--- a/config-scripts/cups-systemd.m4	1970-01-01 01:00:00.000000000 +0100
+++ b/config-scripts/cups-systemd.m4	2014-04-11 23:05:56.075331880 +0200
@@ -0,0 +1,36 @@
+dnl
+dnl "$Id$"
+dnl
+dnl   systemd stuff for CUPS.
+
+dnl Find whether systemd is available
+
+SDLIBS=""
+AC_ARG_WITH([systemdsystemunitdir],
+        AS_HELP_STRING([--with-systemdsystemunitdir=DIR], [Directory for systemd service files]),
+        [], [with_systemdsystemunitdir=$($PKGCONFIG --variable=systemdsystemunitdir systemd)])
+if test "x$with_systemdsystemunitdir" != xno; then
+        AC_MSG_CHECKING(for libsystemd-daemon)
+        AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])
+        if $PKGCONFIG --exists libsystemd-daemon; then
+		AC_MSG_RESULT(yes)
+        	SDCFLAGS=`$PKGCONFIG --cflags libsystemd-daemon`
+        	SDLIBS=`$PKGCONFIG --libs libsystemd-daemon`
+		AC_DEFINE(HAVE_SYSTEMD)
+	else
+		AC_MSG_RESULT(no)
+	fi
+fi
+
+if test -n "$with_systemdsystemunitdir" -a "x$with_systemdsystemunitdir" != xno ; then
+        SYSTEMD_UNITS="cups.service cups.socket cups.path"
+else
+        SYSTEMD_UNITS=""
+fi
+
+AC_SUBST(SYSTEMD_UNITS)
+AC_SUBST(SDLIBS)
+
+dnl
+dnl "$Id$"
+dnl
--- a/configure.in	2013-07-08 23:15:13.000000000 +0200
+++ b/configure.in	2014-04-11 23:05:56.075331880 +0200
@@ -33,6 +33,7 @@
 sinclude(config-scripts/cups-largefile.m4)
 sinclude(config-scripts/cups-dnssd.m4)
 sinclude(config-scripts/cups-launchd.m4)
+sinclude(config-scripts/cups-systemd.m4)
 sinclude(config-scripts/cups-defaults.m4)
 sinclude(config-scripts/cups-scripting.m4)
 
@@ -67,6 +68,9 @@
 	  conf/snmp.conf
 	  cups-config
 	  data/testprint
+	  data/cups.service
+	  data/cups.socket
+	  data/cups.path
 	  desktop/cups.desktop
 	  doc/help/ref-cups-files-conf.html
 	  doc/help/ref-cupsd-conf.html
--- a/cups/usersys.c	2014-03-05 22:22:12.000000000 +0100
+++ b/cups/usersys.c	2014-04-11 23:05:56.075331880 +0200
@@ -1050,7 +1050,7 @@
     struct stat	sockinfo;		/* Domain socket information */
 
     if (!stat(CUPS_DEFAULT_DOMAINSOCKET, &sockinfo) &&
-	(sockinfo.st_mode & S_IRWXO) == S_IRWXO)
+	(sockinfo.st_mode & (S_IROTH | S_IWOTH)) == (S_IROTH | S_IWOTH))
       cups_server = CUPS_DEFAULT_DOMAINSOCKET;
     else
 #endif /* CUPS_DEFAULT_DOMAINSOCKET */
--- a/data/cups.path.in	1970-01-01 01:00:00.000000000 +0100
+++ b/data/cups.path.in	2014-04-11 23:05:56.075331880 +0200
@@ -0,0 +1,8 @@
+[Unit]
+Description=CUPS Printer Service Spool
+
+[Path]
+PathExistsGlob=@CUPS_REQUESTS@/d*
+
+[Install]
+WantedBy=multi-user.target
--- a/data/cups.service.in	1970-01-01 01:00:00.000000000 +0100
+++ b/data/cups.service.in	2014-04-11 23:05:56.075331880 +0200
@@ -0,0 +1,10 @@
+[Unit]
+Description=CUPS Printing Service
+
+[Service]
+ExecStart=@sbindir@/cupsd -f
+PrivateTmp=true
+
+[Install]
+Also=cups.socket cups.path
+WantedBy=printer.target
--- a/data/cups.socket.in	1970-01-01 01:00:00.000000000 +0100
+++ b/data/cups.socket.in	2014-04-11 23:05:56.075331880 +0200
@@ -0,0 +1,10 @@
+[Unit]
+Description=CUPS Printing Service Sockets
+
+[Socket]
+ListenStream=@CUPS_DEFAULT_DOMAINSOCKET@
+ListenStream=@DEFAULT_IPP_PORT@
+BindIPv6Only=ipv6-only
+
+[Install]
+WantedBy=sockets.target
--- a/data/Makefile	2013-05-29 13:51:34.000000000 +0200
+++ b/data/Makefile	2014-04-11 23:05:56.075331880 +0200
@@ -100,6 +100,12 @@
 		$(INSTALL_DATA) $$file $(DATADIR)/ppdc; \
 	done
 	$(INSTALL_DIR) -m 755 $(DATADIR)/profiles
+	if test "x$(SYSTEMD_UNITS)" != "x" ; then \
+		$(INSTALL_DIR) -m 755 $(SYSTEMDUNITDIR); \
+		for file in $(SYSTEMD_UNITS); do \
+			$(INSTALL_DATA) $$file $(SYSTEMDUNITDIR); \
+		done; \
+	fi
 
 
 #
@@ -143,6 +149,9 @@
 	-$(RMDIR) $(DATADIR)/data
 	-$(RMDIR) $(DATADIR)/banners
 	-$(RMDIR) $(DATADIR)
+	for file in $(SYSTEMD_UNITS); do \
+		$(RM) $(SYSTEMDUNITDIR)/$$file; \
+	done
 
 
 #
--- a/Makedefs.in	2013-07-17 17:21:18.000000000 +0200
+++ b/Makedefs.in	2014-04-11 23:05:56.076331900 +0200
@@ -134,6 +134,7 @@
 CXXLIBS		=	@CXXLIBS@
 DBUS_NOTIFIER	=	@DBUS_NOTIFIER@
 DBUS_NOTIFIERLIBS =	@DBUS_NOTIFIERLIBS@
+SYSTEMD_UNITS   =       @SYSTEMD_UNITS@
 DNSSD_BACKEND	=	@DNSSD_BACKEND@
 DSOFLAGS	=	-L../cups @DSOFLAGS@
 DSOLIBS		=	@DSOLIBS@ $(COMMONLIBS)
@@ -141,6 +142,7 @@
 IPPFIND_BIN	=	@IPPFIND_BIN@
 IPPFIND_MAN	=	@IPPFIND_MAN@
 LAUNCHDLIBS	=	@LAUNCHDLIBS@
+SDLIBS		=	@SDLIBS@
 LDFLAGS		=	-L../cgi-bin -L../cups -L../filter -L../ppdc \
 			-L../scheduler @LDARCHFLAGS@ \
 			@LDFLAGS@ @RELROFLAGS@ @PIEFLAGS@ $(OPTIM)
@@ -232,6 +234,7 @@
 
 DEFAULT_LAUNCHD_CONF =	@DEFAULT_LAUNCHD_CONF@
 DBUSDIR		=	@DBUSDIR@
+SYSTEMDUNITDIR  =       $(BUILDROOT)@systemdsystemunitdir@
 
 
 #
--- a/scheduler/client.h	2013-08-02 00:23:18.000000000 +0200
+++ b/scheduler/client.h	2014-04-11 23:05:56.076331900 +0200
@@ -79,6 +79,9 @@
   int			fd;		/* File descriptor for this server */
   http_addr_t		address;	/* Bind address of socket */
   http_encryption_t	encryption;	/* To encrypt or not to encrypt... */
+#ifdef HAVE_SYSTEMD
+  int			is_systemd;	/* Is this a systemd socket? */
+#endif /* HAVE_SYSTEMD */
 } cupsd_listener_t;
 
 
--- a/scheduler/listen.c	2013-05-29 13:51:34.000000000 +0200
+++ b/scheduler/listen.c	2014-04-11 23:05:56.076331900 +0200
@@ -401,7 +401,11 @@
        lis;
        lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
   {
-    if (lis->fd != -1)
+    if (lis->fd != -1
+#ifdef HAVE_SYSTEMD
+        && !lis->is_systemd
+#endif /* HAVE_SYSTEMD */
+        )
     {
 #ifdef WIN32
       closesocket(lis->fd);
--- a/scheduler/main.c	2014-03-21 19:18:56.000000000 +0100
+++ b/scheduler/main.c	2014-04-11 23:06:45.580257244 +0200
@@ -39,6 +39,10 @@
 #  endif /* !LAUNCH_JOBKEY_SERVICEIPC */
 #endif /* HAVE_LAUNCH_H */
 
+#ifdef HAVE_SYSTEMD
+#include <systemd/sd-daemon.h>
+#endif /* HAVE_SYSTEMD */
+
 #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
 #  include <malloc.h>
 #endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
@@ -64,6 +68,9 @@
 static void		launchd_checkin(void);
 static void		launchd_checkout(void);
 #endif /* HAVE_LAUNCHD */
+#ifdef HAVE_SYSTEMD
+static void		systemd_checkin(void);
+#endif /* HAVE_SYSTEMD */
 static void		parent_handler(int sig);
 static void		process_children(void);
 static void		sigchld_handler(int sig);
@@ -553,6 +560,13 @@
   }
 #endif /* HAVE_LAUNCHD */
 
+#ifdef HAVE_SYSTEMD
+ /*
+  * If we were started by systemd get the listen sockets file descriptors...
+  */
+  systemd_checkin();
+#endif /* HAVE_SYSTEMD */
+
  /*
   * Startup the server...
   */
@@ -741,6 +755,15 @@
 	}
 #endif /* HAVE_LAUNCHD */
 
+#ifdef HAVE_SYSTEMD
+       /*
+	* If we were started by systemd get the listen sockets file
+	* descriptors...
+        */
+
+        systemd_checkin();
+#endif /* HAVE_SYSTEMD */
+
        /*
         * Startup the server...
         */
@@ -1490,6 +1513,102 @@
 }
 #endif /* HAVE_LAUNCHD */
 
+#ifdef HAVE_SYSTEMD
+static void
+systemd_checkin(void)
+{
+  int n, fd;
+
+  n = sd_listen_fds(0);
+  if (n < 0)
+  {
+    cupsdLogMessage(CUPSD_LOG_ERROR,
+           "systemd_checkin: Failed to acquire sockets from systemd - %s",
+           strerror(-n));
+    exit(EXIT_FAILURE);
+    return;
+  }
+
+  if (n == 0)
+    return;
+
+  for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++)
+  {
+    http_addr_t addr;
+    socklen_t addrlen = sizeof (addr);
+    int r;
+    cupsd_listener_t *lis;
+    char s[256];
+
+    r = sd_is_socket(fd, AF_UNSPEC, SOCK_STREAM, 1);
+    if (r < 0)
+    {
+      cupsdLogMessage(CUPSD_LOG_ERROR,
+             "systemd_checkin: Unable to verify socket type - %s",
+             strerror(-r));
+      continue;
+    }
+
+    if (!r)
+    {
+      cupsdLogMessage(CUPSD_LOG_ERROR,
+             "systemd_checkin: Socket not of the right type");
+      continue;
+    }
+
+    if (getsockname(fd, (struct sockaddr*) &addr, &addrlen))
+    {
+      cupsdLogMessage(CUPSD_LOG_ERROR,
+             "systemd_checkin: Unable to get local address - %s",
+             strerror(errno));
+      continue;
+    }
+
+   /*
+    * Try to match the systemd socket address to one of the listeners...
+    */
+
+    for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
+       lis;
+       lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
+      if (httpAddrEqual(&lis->address, &addr))
+	break;
+
+    if (lis)
+    {
+      cupsdLogMessage(CUPSD_LOG_DEBUG,
+                      "systemd_checkin: Matched existing listener %s with fd %d...",
+                      httpAddrString(&(lis->address), s, sizeof(s)), fd);
+    }
+    else
+    {
+      cupsdLogMessage(CUPSD_LOG_DEBUG,
+                      "systemd_checkin: Adding new listener %s with fd %d...",
+                      httpAddrString(&addr, s, sizeof(s)), fd);
+
+      if ((lis = calloc(1, sizeof(cupsd_listener_t))) == NULL)
+      {
+        cupsdLogMessage(CUPSD_LOG_ERROR,
+                        "systemd_checkin: Unable to allocate listener - "
+                        "%s.", strerror(errno));
+        exit(EXIT_FAILURE);
+      }
+
+      cupsArrayAdd(Listeners, lis);
+
+      memcpy(&lis->address, &addr, sizeof(lis->address));
+    }
+
+    lis->fd = fd;
+    lis->is_systemd = 1;
+
+#  ifdef HAVE_SSL
+    if (_httpAddrPort(&(lis->address)) == 443)
+      lis->encryption = HTTP_ENCRYPT_ALWAYS;
+#  endif /* HAVE_SSL */
+  }
+}
+#endif /* HAVE_SYSTEMD */
 
 /*
  * 'parent_handler()' - Catch USR1/CHLD signals...
--- a/scheduler/Makefile	2013-05-29 13:51:34.000000000 +0200
+++ b/scheduler/Makefile	2014-04-11 23:05:56.076331900 +0200
@@ -381,7 +381,7 @@
 	$(CC) $(LDFLAGS) -o cupsd $(CUPSDOBJS) -L. -lcupsmime \
 		$(LIBZ) $(SSLLIBS) $(LIBSLP) $(LIBLDAP) $(PAMLIBS) \
 		$(LIBPAPER) $(LIBMALLOC) $(SERVERLIBS) $(DNSSDLIBS) $(LIBS) \
-		$(LIBGSSAPI) $(LIBWRAP)
+		$(LIBGSSAPI) $(LIBWRAP) $(SDLIBS)
 
 cupsd-static:	$(CUPSDOBJS) libcupsmime.a ../cups/$(LIBCUPSSTATIC)
 	echo Linking $@...
@@ -389,7 +389,7 @@
 		$(LIBZ) $(SSLLIBS) $(LIBSLP) $(LIBLDAP) $(PAMLIBS) \
 		../cups/$(LIBCUPSSTATIC) $(COMMONLIBS) $(LIBZ) $(LIBPAPER) \
 		$(LIBMALLOC) $(SERVERLIBS) $(DNSSDLIBS) $(LIBGSSAPI) \
-		$(LIBWRAP)
+		$(LIBWRAP)  $(SDLIBS)
 
 tls.o:	tls-darwin.c tls-gnutls.c tls-openssl.c
 
