Subject: bug#78757: closed (Re: bug#78757: shepherd: dangling file descriptor to /dev/console)

From: [email-masked] (GNU bug Tracking System)
Date: Mon, 16 Jun 2025 13:38:09 +0000

Your bug report

#78757: shepherd: dangling file descriptor to /dev/console

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 [email-masked].

-- 
78757: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=78757
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#78757: shepherd: dangling file descriptor to /dev/console
From: Ludovic Courtès <[email-masked]>
To: [email-masked]
Cc: [email-masked]
Date: Mon, 16 Jun 2025 15:35:19 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi,

[email-masked] writes:

>> ‘shepherd’ marks all previously-opened FDs as O_CLOEXEC upfront—see
>> ‘mark-as-close-on-exec’ in ‘shepherd.scm’.  Those opened later are
>> opened with O_CLOEXEC, except for user code that explicitly omits the
>> O_CLOEXEC flag.
>
> Hmmm.... I have at one point (before the above (port-for-each ...)
>
> (define *s* (open-output-file "/proc/self/fd/1"))
>
> to have an early log output to (on ordinary PC) /dev/tty1. But maybe /dev/tty1
> is a synonym for /dev/console. Let's test that:
>
> (fileno *s*) returns 19 indeed! Now all I have to do is (fcntl *s*
> F_SETFD FD_CLOEXEC) after creating *s*.

Ah yes, it’s easy to shot oneself in the foot.  ;-)

To reduce the risks, ‘shepherd’ replaces ‘call-with-input-file’ and
‘call-with-output-file’ with O_CLOEXEC variants (commit
47c693d51a991e9a3d5ed6e75760c61e329cc537) because it wouldn’t make much
sense to not have O_CLOEXEC for these.  However, the other procedures
are left unchanged because users might need it.

> So you can close that ticket.

Thanks,
Ludo’.

Subject: shepherd: dangling file descriptor to /dev/console
From: [email-masked]
To: [email-masked]
Date: Tue, 10 Jun 2025 20:31:51 +0000
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello,

All shepherd children have an open file descriptor to /dev/console,
probably because of the use of dup2 (and not dup3) in system.scm.

The pb. disapears if I force that flag with that code before I define
my services (context: shepherd used as init system on Debian):
(port-for-each (lambda (x)
                 (catch #t
		   (lambda ()
		     (when (= (fcntl x F_GETFD) 0)
		       (fcntl x F_SETFD FD_CLOEXEC)
		       (format #t "FD_CLOEXEC flag set on port ~a (fd: ~a)\n" x (port->fdes x))))
		   (lambda (keys . args) (format #t "fcntl error for port ~a\n" x)))
		 ))

Sincerely.

-- 

Bernard