GNU bug report logs - #77707
[shepherd] chdir and invoke do not work as intended in services

Previous Next

Package: guix;

Reported by: Maxim Cournoyer <maxim.cournoyer <at> gmail.com>

Date: Thu, 10 Apr 2025 14:45:02 UTC

Severity: normal

Done: Ludovic Courtès <ludo <at> chbouib.org>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: help-debbugs <at> gnu.org (GNU bug Tracking System)
To: Maxim Cournoyer <maxim.cournoyer <at> gmail.com>
Subject: bug#77707: closed (Re: bug#77707: [shepherd] chdir and invoke do
 not work as intended in services)
Date: Sun, 13 Apr 2025 19:53:02 +0000
[Message part 1 (text/plain, inline)]
Your bug report

#77707: [shepherd] chdir and invoke do not work as intended in services

which was filed against the guix package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to 77707 <at> debbugs.gnu.org.

-- 
77707: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=77707
GNU Bug Tracking System
Contact help-debbugs <at> gnu.org with problems
[Message part 2 (message/rfc822, inline)]
From: Ludovic Courtès <ludo <at> chbouib.org>
To: Maxim Cournoyer <maxim.cournoyer <at> gmail.com>
Cc: 77707-done <at> debbugs.gnu.org
Subject: Re: bug#77707: [shepherd] chdir and invoke do not work as intended
 in services
Date: Sun, 13 Apr 2025 21:52:29 +0200
Hi,

Maxim Cournoyer <maxim.cournoyer <at> gmail.com> skribis:

> +                             (start #~(lambda _
> +                                        (mkdir "/etc/testdir")
> +                                        (chdir "/etc/testdir")
> +                                        (invoke #$(file-append coreutils "/bin/touch")
> +                                                "new-file.txt")

[...]

> You should see upon inspection that:
>
> root <at> komputilo ~# ls /new-file.txt 
> /new-file.txt                        # wrong place
> root <at> komputilo ~# ls /etc/testdir/   # empty

That’s because ‘invoke’ uses ‘system*’, which is in fact within the
‘shepherd’ process an alias for ‘spawn-command’.

This is fixed in 8d88024e0898dd7d4581c4e977c9786af734a855, which will be
in 1.0.4.

However generally speaking it’s a good idea to leave the current
directory of the ‘shepherd’ process unchanged as bad things could happen
otherwise (though ‘with-directory-excursion’ from (shepherd support)
should be safe.)

Thanks,
Ludo’.

[Message part 3 (message/rfc822, inline)]
From: Maxim Cournoyer <maxim.cournoyer <at> gmail.com>
To: bug-guix <bug-guix <at> gnu.org>
Subject: [shepherd] chdir and invoke do not work as intended in services
Date: Thu, 10 Apr 2025 23:44:04 +0900
Hi,

It appears that when changing directory and calling a program with
'invoke', the program still has its current working directory set to
'/'.

I'm betting this may be some kind of odd interaction between the
Shepherd service threads and forking a process?

Can we improve that?  If it's a limitation of the implementation model,
than I think it's a surprising enough gotcha to warrant documenting it
in Shepherd's manual.

Here's a simple reproducer:

--8<---------------cut here---------------start------------->8---
modified   gnu/system/examples/bare-bones.tmpl
@@ -3,8 +3,8 @@
 ;; for a "bare bones" setup, with no X11 display server.
 
 (use-modules (gnu))
-(use-service-modules networking ssh)
-(use-package-modules screen ssh)
+(use-service-modules networking shepherd ssh)
+(use-package-modules base screen ssh)
 
 (operating-system
   (host-name "komputilo")
@@ -47,7 +47,20 @@
 
   ;; Add services to the baseline: a DHCP client and an SSH
   ;; server.  You may wish to add an NTP service here.
-  (services (append (list (service dhcp-client-service-type)
+  (services (append (list (simple-service
+                           'forked-processes-cwd-wrong
+                           shepherd-root-service-type
+                           (list
+                            (shepherd-service
+                             (provision '(bug-77115))
+                             (start #~(lambda _
+                                        (mkdir "/etc/testdir")
+                                        (chdir "/etc/testdir")
+                                        (invoke #$(file-append coreutils "/bin/touch")
+                                                "new-file.txt")
+                                        #t))
+                             (one-shot? #t))))
+                          (service dhcp-client-service-type)
                           (service openssh-service-type
                                    (openssh-configuration
                                     (openssh openssh-sans-x)
--8<---------------cut here---------------end--------------->8---

Then run it from your tree using something like:

--8<---------------cut here---------------start------------->8---
$(./pre-inst-env guix system vm gnu/system/examples/bare-bones.tmpl --no-graphic)
--8<---------------cut here---------------end--------------->8---

You should see upon inspection that:

--8<---------------cut here---------------start------------->8---
root <at> komputilo ~# ls /new-file.txt 
/new-file.txt                        # wrong place
root <at> komputilo ~# ls /etc/testdir/   # empty
--8<---------------cut here---------------end--------------->8---

-- 
Thanks,
Maxim



This bug report was last modified 96 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.