Package: nethack-common
Version: 3.4.3-14
Severity: wishlist
Tags: patch

The attached patch adds native systemd support for nethack-common.

This factors out the recovery logic from the nethack-common init script
into a new script /usr/lib/games/nethack/recover-all, now invoked from
both the init script and the new nethack-common.service.  This service
uses ConditionPathExistsGlob to only launch when files exist to recover,
avoiding any extra processes at startup in the common case.

- Josh Triplett

-- System Information:
Debian Release: jessie/sid
  APT prefers unstable
  APT policy: (500, 'unstable'), (1, 'experimental')
Architecture: amd64 (x86_64)

Kernel: Linux 3.13-1-amd64 (SMP w/4 CPU cores)
Locale: LANG=C.UTF-8, LC_CTYPE=C.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash

Versions of packages nethack-common depends on:
ii  debconf [debconf-2.0]  1.5.52
ii  libc6                  2.18-4

Versions of packages nethack-common recommends:
ii  nethack-console  3.4.3-14

nethack-common suggests no packages.

-- debconf information excluded
>From 96cfac439f861485dbc01a2c447641ceb1659219 Mon Sep 17 00:00:00 2001
From: Josh Triplett <[email protected]>
Date: Sat, 8 Mar 2014 13:36:36 -0800
Subject: [PATCH] Add native systemd support.

Factor out the recovery logic from the nethack-common init script into a
new script /usr/lib/games/nethack/recover-all.

Add a nethack-common.service invoking recover-all.  This service uses
ConditionPathExistsGlob to only launch when files exist to recover,
avoiding any extra processes at startup in the common case.

Build-Depends: debhelper (>= 9.20130504) for systemd support in
dh_installinit.
---
 debian/changelog              | 12 +++++++++++-
 debian/control                |  2 +-
 debian/copyright              |  1 +
 debian/nethack-common.init    | 44 ++-----------------------------------------
 debian/nethack-common.service | 12 ++++++++++++
 debian/recover-all            | 38 +++++++++++++++++++++++++++++++++++++
 debian/rules                  |  2 ++
 7 files changed, 67 insertions(+), 44 deletions(-)
 create mode 100644 debian/nethack-common.service
 create mode 100644 debian/recover-all

diff --git a/debian/changelog b/debian/changelog
index 4469841..851e0de 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -7,7 +7,17 @@ nethack (3.4.3-15) UNRELEASED; urgency=low
   [ Evgeni Golov ]
   * Correct Vcs-* URLs to point to anonscm.debian.org
 
- -- Vincent Cheng <[email protected]>  Wed, 26 Sep 2012 00:10:43 -0700
+  [ Josh Triplett ]
+  * Add native systemd support.
+    - Factor out the recovery logic from the nethack-common init script into a
+      new script /usr/lib/games/nethack/recover-all.
+    - Add a nethack-common.service invoking recover-all.  This service uses
+      ConditionPathExistsGlob to only launch when files exist to recover,
+      avoiding any extra processes at startup in the common case.
+    - Build-Depends: debhelper (>= 9.20130504) for systemd support in
+      dh_installinit.
+
+ -- Vincent Cheng <[email protected]>  Sat, 08 Mar 2014 13:18:02 -0800
 
 nethack (3.4.3-14) unstable; urgency=low
 
diff --git a/debian/control b/debian/control
index 051a79c..a909fb2 100644
--- a/debian/control
+++ b/debian/control
@@ -6,7 +6,7 @@ Uploaders: Vincent Cheng <[email protected]>
 Build-Depends:
  bison,
  bsdmainutils,
- debhelper (>= 7),
+ debhelper (>= 9.20130504),
  flex,
  groff-base,
  libncurses5-dev | libncurses-dev,
diff --git a/debian/copyright b/debian/copyright
index 3a2cce2..8a20b82 100644
--- a/debian/copyright
+++ b/debian/copyright
@@ -20,6 +20,7 @@ Copyright: 1996-1997 Paul Haggart <[email protected]>
            1997-2003 Ben Gertzfield <[email protected]>
            2003-2009 Joshua Kwan <[email protected]>
            2012 Vincent Cheng <[email protected]>
+           2014 Josh Triplett <[email protected]>
 License: GPL-2.0+
 
 License: BSD-3-clause
diff --git a/debian/nethack-common.init b/debian/nethack-common.init
index 16943f8..2166f77 100644
--- a/debian/nethack-common.init
+++ b/debian/nethack-common.init
@@ -14,53 +14,13 @@
 
 PATH=/bin:/usr/bin:/sbin:/usr/sbin
 
-GAMEDIR=/var/games/nethack
-
 set -e
 
-cd $GAMEDIR
-
 case "$1" in
   start)
     # Has the nethack package been removed?
-    test -x /usr/lib/games/nethack/recover-helper || exit 0
-
-    for file in *.0; do
-
-    # Note "$file" is always explicitly quoted to avoid attack.
-    # If there are no files, then "$file" = "*.0", which doesn't
-    # exist, so we skip once through this loop and exit.
-    # Also, the way this is written, some of the files may
-    # disappear before we look at them.
-
-    # Also check -L--there shouldn't be any symlinks, but if there
-    # are, we aren't going to process them.
-
-    if [ -f "$file" ] && [ ! -L "$file" ]; then
-      # Use 'find' to reliably determine the file's owner user name.
-      owner="$(find "$file" -maxdepth 0 -printf '%u')"
-
-      # Refuse to recover root's nethack files.
-      if [ "xroot" = "x$owner" ]; then
-        echo "Ignoring root's Nethack unrecovered save file."
-      else 
-        echo "Recovering Nethack save files owned by $owner: "
-
-        # "$owner" is explicitly quoted to avoid attack.
-        # In particular, if the "find" command above fails,
-        # so will the 'su' command below.
-
-        # There really isn't a good safe way to pass a filename to
-        # a child shell through 'su -c', so instead we use a helper
-        # script running as the user which recovers everything
-        # owned by that user.  This avoids the issue of quoting
-        # filenames passed through the shell entirely.
-
-        su --shell=/bin/sh -c /usr/lib/games/nethack/recover-helper "$owner"
-      fi
-    fi
-
-    done
+    test -x /usr/lib/games/nethack/recover-all || exit 0
+    exec /usr/lib/games/nethack/recover-all
     ;;
   stop|reload|restart|force-reload)
     ;;
diff --git a/debian/nethack-common.service b/debian/nethack-common.service
new file mode 100644
index 0000000..850365b
--- /dev/null
+++ b/debian/nethack-common.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Recover NetHack save files
+Documentation=man:recover(6) man:nethack(6)
+RequiresMountsFor=/var/games/nethack
+ConditionPathExistsGlob=/var/games/nethack/*.0
+
+[Service]
+Type=oneshot
+ExecStart=/usr/lib/games/nethack/recover-all
+
+[Install]
+WantedBy=multi-user.target
diff --git a/debian/recover-all b/debian/recover-all
new file mode 100644
index 0000000..3438c7d
--- /dev/null
+++ b/debian/recover-all
@@ -0,0 +1,38 @@
+#!/bin/sh
+set -e
+
+cd /var/games/nethack
+for file in *.0; do
+    # Note "$file" is always explicitly quoted to avoid attack.
+    # If there are no files, then "$file" = "*.0", which doesn't
+    # exist, so we skip once through this loop and exit.
+    # Also, the way this is written, some of the files may
+    # disappear before we look at them.
+
+    # Also check -L--there shouldn't be any symlinks, but if there
+    # are, we aren't going to process them.
+
+    if [ -f "$file" ] && [ ! -L "$file" ]; then
+        # Use 'find' to reliably determine the file's owner user name.
+        owner="$(find "$file" -maxdepth 0 -printf '%u')"
+
+        # Refuse to recover root's nethack files.
+        if [ "xroot" = "x$owner" ]; then
+            echo "Ignoring root's Nethack unrecovered save file."
+        else
+            echo "Recovering Nethack save files owned by $owner: "
+
+            # "$owner" is explicitly quoted to avoid attack.
+            # In particular, if the "find" command above fails,
+            # so will the 'su' command below.
+
+            # There really isn't a good safe way to pass a filename to
+            # a child shell through 'su -c', so instead we use a helper
+            # script running as the user which recovers everything
+            # owned by that user.  This avoids the issue of quoting
+            # filenames passed through the shell entirely.
+
+            su --shell=/bin/sh -c /usr/lib/games/nethack/recover-helper "$owner"
+        fi
+    fi
+done
diff --git a/debian/rules b/debian/rules
index 7c44ea8..727079b 100755
--- a/debian/rules
+++ b/debian/rules
@@ -88,6 +88,8 @@ binary-arch: build-arch-stamp
 
 	install -m 0755 -o root -g root debian/recover-helper \
 	  debian/nethack-common/usr/lib/games/nethack/recover-helper
+	install -m 0755 -o root -g root debian/recover-all \
+	  debian/nethack-common/usr/lib/games/nethack/recover-all
 
 	install -m 0644 -o root -g root dat/nhdat \
 	  debian/nethack-common/usr/lib/games/nethack/nhdat
-- 
1.9.0

Reply via email to