From mboxrd@z Thu Jan 1 00:00:00 1970 From: Mathieu Lirzin Subject: Re: [PATCH 3/3] support: Rename user-dmddir to %user-shepherd-dir. Date: Sun, 17 Jan 2016 22:06:57 +0100 Message-ID: <87r3hf29vy.fsf@gnu.org> References: <1452982661-17268-1-git-send-email-mthl@gnu.org> <1452982661-17268-4-git-send-email-mthl@gnu.org> <87wpr870gw.fsf@gnu.org> <87y4bo0w3w.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:58651) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aKuXP-00036Q-R8 for guix-devel@gnu.org; Sun, 17 Jan 2016 16:07:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aKuXL-0005fK-OA for guix-devel@gnu.org; Sun, 17 Jan 2016 16:07:03 -0500 In-Reply-To: <87y4bo0w3w.fsf@gnu.org> (Mathieu Lirzin's message of "Sun, 17 Jan 2016 21:49:55 +0100") List-Id: "Development of GNU Guix and the GNU System distribution." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org Sender: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org To: Ludovic =?utf-8?Q?Court=C3=A8s?= Cc: guix-devel@gnu.org --=-=-= Content-Type: text/plain Patch updated with copyright added for the test. -- Mathieu Lirzin --=-=-= Content-Type: text/x-diff; charset=utf-8 Content-Disposition: inline; filename=0001-support-Rename-user-dmddir-to-user-config-dir.patch Content-Transfer-Encoding: quoted-printable >From 32a9e3b00a7679d561e18d623a8c2936e8729f03 Mon Sep 17 00:00:00 2001 From: Mathieu Lirzin Date: Sat, 16 Jan 2016 22:22:26 +0100 Subject: [PATCH] support: Rename user-dmddir to %user-config-dir. * modules/shepherd/support.scm (user-dmddir): Rename to ... (%user-config-dir): ... this. Honor XDG_CONFIG_HOME and use '$HOME/.config/shepherd' as default value. All consumers changed. (mkdir-p): New procedure. Export it. (default-config-file): Use it. (verify-dir): Likewise. * shepherd.texi (Jump Start, Invoking shepherd): Document the new user configuration directory. * tests/basic.sh: Check if XDG_CONFIG_HOME is honored. * Makefile.am (AM_TESTS_ENVIRONMENT): Unset it. --- Makefile.am | 3 ++- modules/shepherd/support.scm | 47 ++++++++++++++++++++++++++++++++++++----= ---- shepherd.texi | 16 ++++++++------- tests/basic.sh | 31 +++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 16 deletions(-) diff --git a/Makefile.am b/Makefile.am index 7b3e59f..7eb2a27 100644 --- a/Makefile.am +++ b/Makefile.am @@ -148,7 +148,8 @@ TEST_EXTENSIONS =3D .sh EXTRA_DIST +=3D $(TESTS) =20 AM_TESTS_ENVIRONMENT =3D \ - PATH=3D"$(abs_top_builddir):$$PATH" LC_ALL=3DC \ + unset XDG_CONFIG_HOME; LC_ALL=3DC \ + PATH=3D"$(abs_top_builddir):$$PATH" \ SHELL=3D"$(SHELL)" GUILE=3D"$(GUILE)" \ GUILE_LOAD_PATH=3D"$(abs_top_srcdir)/modules:$(abs_top_builddir)/modules= :$$GUILE_LOAD_PATH" \ GUILE_LOAD_COMPILED_PATH=3D"$(abs_top_srcdir)/modules:$(abs_top_builddir= )/modules:$$GUILE_LOAD_COMPILED_PATH" diff --git a/modules/shepherd/support.scm b/modules/shepherd/support.scm index 5b1ca7e..319fa91 100644 --- a/modules/shepherd/support.scm +++ b/modules/shepherd/support.scm @@ -32,6 +32,7 @@ with-system-error-handling EINTR-safe with-atomic-file-output + mkdir-p =20 l10n local-output @@ -155,6 +156,33 @@ output port, and PROC's result is returned." (lambda (key . args) (catch-system-error (delete-file template)))))) =20 +(define* (mkdir-p dir #:optional mode) ;copied from Guix + "Create directory DIR and all its ancestors." + (define absolute? + (string-prefix? "/" dir)) + + (define not-slash + (char-set-complement (char-set #\/))) + + (let loop ((components (string-tokenize dir not-slash)) + (root (if absolute? + "" + "."))) + (match components + ((head tail ...) + (let ((path (string-append root "/" head))) + (catch 'system-error + (lambda () + (if mode + (mkdir path mode) + (mkdir path)) + (loop tail path)) + (lambda args + (if (=3D EEXIST (system-error-errno args)) + (loop tail path) + (apply throw args)))))) + (() #t)))) + =20 ;; Localized version of STR. @@ -186,8 +214,11 @@ There is NO WARRANTY, to the extent permitted by law."= ))) (false-if-exception (passwd:dir (getpwuid (getuid)))) "/")) =20 -;; dmd default subdirectory if dmd is run as a normal user. -(define user-dmddir (string-append user-homedir "/.dmd.d")) +(define %user-config-dir + ;; Default config directory if shepherd is run as a normal user. + (string-append (or (getenv "XDG_CONFIG_HOME") + (string-append user-homedir "/.config")) + "/shepherd")) =20 (define (make-bare-init-file target) "Return #t if a bare init file was created at TARGET; #f otherwise. @@ -216,7 +247,7 @@ TARGET should be a string representing a filepath + nam= e." (define default-logfile (if (zero? (getuid)) (string-append %localstatedir "/log/shepherd.log") - (string-append user-dmddir "/shepherd.log"))) + (string-append %user-config-dir "/shepherd.log"))) =20 ;; Configuration file. (define (default-config-file) @@ -225,8 +256,8 @@ global system configuration file when running as 'root'= . As a side effect, create a template configuration file if non exists." (if (zero? (getuid)) (string-append %sysconfdir "/dmdconf.scm") - (let ((config-file (string-append user-dmddir "/init.scm"))) - (catch-system-error (mkdir user-dmddir)) + (let ((config-file (string-append %user-config-dir "/init.scm"))) + (mkdir-p %user-config-dir #o700) (if (not (file-exists? config-file)) (make-bare-init-file config-file)) config-file))) @@ -239,7 +270,7 @@ create a template configuration file if non exists." (define default-socket-dir (if (zero? (getuid)) %system-socket-dir - (string-append user-dmddir "/run"))) + (string-append %user-config-dir "/run"))) =20 ;; Unix domain socket for receiving commands in dmd. (define default-socket-file @@ -253,7 +284,7 @@ create a template configuration file if non exists." (define default-persistency-state-file (if (zero? (getuid)) (string-append %localstatedir "/lib/misc/dmd-state") - (string-append user-dmddir "/dmd-state"))) + (string-append %user-config-dir "/dmd-state"))) =20 ;; Global variables set from (dmd). (define persistency #f) @@ -284,7 +315,7 @@ directory are not checked." (and (string=3D? dir default-socket-dir) ;; If it exists already, this is fine, thus ignore errors. (catch-system-error - (mkdir default-socket-dir #o700))) + (mkdir-p default-socket-dir #o700))) ;; Check for permissions. (when secure? (let ((dir-stat (stat dir))) diff --git a/shepherd.texi b/shepherd.texi index 6cca61c..3131d04 100644 --- a/shepherd.texi +++ b/shepherd.texi @@ -139,12 +139,14 @@ instead, add this directory name in front of the abso= lute file names mentioned below. =20 @cindex Configuration file -When @command{shepherd} gets started, it reads and evaluates a configurati= on file. When it is -started with superuser priviledges, it tries to use -@code{/etc/dmdconf.scm}, when started as normal user, it looks for a -file called @code{.dmd.d/init.scm} in the user's home directory. With the -option @code{--config} (or, for short, @code{-c}), you can specify -where to look instead. So if you want to start @command{shepherd} with an +When @command{shepherd} gets started, it reads and evaluates a +configuration file. When it is started with superuser priviledges, it +tries to use @code{/etc/dmdconf.scm}. When started as normal user, it +looks for a file called @code{$XDG_CONFIG_HOME/shepherd/init.scm}. If +the XDG_CONFIG_HOME environment variable is not defined, +@code{$HOME/.config/shepherd/init.scm} is used instead. With the option +@code{--config} (or, for short, @code{-c}), you can specify where to +look instead. So if you want to start @command{shepherd} with an alternative file, use one of the following commands: =20 @example @@ -402,7 +404,7 @@ permissions are not as expected. @cindex log file Log output into @var{file}, or if @var{file} is not given, @code{/var/log/shepherd.log} when running as superuser, -@var{~/.dmd.d/shepherd.log} otherwise. +@code{$XDG_CONFIG_HOME/shepherd/shepherd.log} otherwise. =20 @item --pid[=3D@var{file}] When @command{shepherd} is ready to accept connections, write its PID to @= var{file} or diff --git a/tests/basic.sh b/tests/basic.sh index dfe5d18..3e500df 100644 --- a/tests/basic.sh +++ b/tests/basic.sh @@ -1,5 +1,6 @@ # GNU Shepherd --- Test basic communication capabilities. # Copyright =C2=A9 2013, 2014, 2016 Ludovic Court=C3=A8s +# Copyright =C2=A9 2016 Mathieu Lirzin # Copyright =C2=A9 2014 Alex Sassmannshausen # # This file is part of the GNU Shepherd. @@ -22,6 +23,7 @@ herd --version =20 socket=3D"t-socket-$$" conf=3D"t-conf-$$" +confdir=3D"t-confdir-$$" log=3D"t-log-$$" stamp=3D"t-stamp-$$" pid=3D"t-pid-$$" @@ -97,3 +99,32 @@ $herd stop dmd ! kill -0 $dmd_pid =20 test -f "$log" + +## ------------------------ ## +## Usage of XDG variables. ## +## ------------------------ ## + +# Set XDG_CONFIG_HOME for configuration files. +export XDG_CONFIG_HOME=3D$confdir +mkdir -p $confdir/shepherd +mv $conf $confdir/shepherd/init.scm +rm -f "$pid" +shepherd -I -s "$socket" --pid=3D"$pid" & + +# Wait till it's ready. +while ! test -f "$pid" ; do : ; done + +# Launch a service from $confdir/shepherd/init.scm. +$herd start test +test -f "$stamp" +$herd status test | grep started + +$herd stop test +! test -f "$stamp" + +dmd_pid=3D"`cat $pid`" + +$herd stop dmd +! kill -0 $dmd_pid + +rm -rf $confdir --=20 2.6.3 --=-=-=--