unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Marius Bakke <marius@gnu.org>
To: guile-devel <guile-devel@gnu.org>
Subject: Preventing file descriptor leak to execl'd processes
Date: Sat, 06 Mar 2021 17:55:02 +0100	[thread overview]
Message-ID: <87czwc5ijd.fsf@gnu.org> (raw)

[-- Attachment #1: Type: text/plain, Size: 2169 bytes --]

Hello Guilers,

I recently had the insa^W bright idea of making my login shell a Guile
script instead of fiddling so much with bashrc and the likes.

For example, here is how I start my window manager when logging into
TTY2 or TTY3:

$ cat /tmp/test-shell
#!/gnu/store/18hp7flyb3yid3yp49i6qcdq0sbi5l1n-guile-3.0.2/bin/guile --no-auto-compile
!#
(let ((bash "/gnu/store/87kif0bpf0anwbsaw0jvg8fyciw4sz67-bash-5.0.16/bin/bash")
      (sway "/gnu/store/9zffdhn3yrfim36ya1g0dwbj012pjk2p-sway-1.5.1/bin/sway")
      (tty (readlink "/proc/self/fd/0"))
      (args (cdr (program-arguments))))
  (if (and (string-prefix? "/dev/tty" tty)
           (or (string-suffix? "2" tty) (string-suffix? "3" tty))
           (not (getenv "DISPLAY")))
      (execl bash bash "--login" "-c"
                  (string-append "exec sway --config /etc/sway/config"))
      (apply execl bash bash args)))

It works great, except that the script filename (/tmp/test-shell) has
an open file descriptor which leaks into the new process:

$ ls -l /proc/self/fd
lrwx------ 1 marius marius 64 Mar  6 17:41 0 -> /dev/pts/18
lrwx------ 1 marius marius 64 Mar  6 17:41 1 -> /dev/pts/18
lrwx------ 1 marius marius 64 Mar  6 17:41 2 -> /dev/pts/18
lr-x------ 1 marius marius 64 Mar  6 17:41 3 -> /proc/9940/fd

$ /tmp/test-shell -c 'ls -l /proc/self/fd'
lrwx------ 1 marius marius 64 Mar  6 17:41 0 -> /dev/pts/18
lrwx------ 1 marius marius 64 Mar  6 17:41 1 -> /dev/pts/18
lrwx------ 1 marius marius 64 Mar  6 17:41 2 -> /dev/pts/18
lr-x------ 1 marius marius 64 Mar  6 17:41 3 -> /proc/9951/fd
lr-x------ 1 marius marius 64 Mar  6 17:41 7 -> /tmp/test-shell

I've managed to work around it by setting FD_CLOEXEC on it:

  (port-for-each (lambda (port)
                   (let ((name (port-filename port))
                         (self (car (program-arguments))))
                     (when (and name (string=? name self))
                       (fcntl port F_SETFD (logior FD_CLOEXEC
                                                   (fcntl port F_GETFD)))))))

But it seems heavy-handed.  Is there an easier way to access the "script
port"?  Perhaps Guile itself should make it FD_CLOEXEC by default?

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 507 bytes --]

             reply	other threads:[~2021-03-06 16:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-06 16:55 Marius Bakke [this message]
2021-03-06 18:04 ` Preventing file descriptor leak to execl'd processes Maxime Devos
2021-03-12 21:22 ` Andy Wingo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87czwc5ijd.fsf@gnu.org \
    --to=marius@gnu.org \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).