unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Marko Rauhamaa <marko@pacujo.net>
To: Mark H Weaver <mhw@netris.org>
Cc: guile-user@gnu.org
Subject: Re: Guile: passing a file descriptor
Date: Sun, 12 Oct 2014 01:20:36 +0300	[thread overview]
Message-ID: <87k3462qq3.fsf@elektro.pacujo.net> (raw)
In-Reply-To: <87r3yeijxl.fsf@yeeloong.lan> (Mark H. Weaver's message of "Sat,  11 Oct 2014 13:40:38 -0400")

Mark H Weaver <mhw@netris.org>:

> Marko Rauhamaa <marko@pacujo.net> writes:
>
>> A PF_UNIX socket can be used to pass an open file descriptor between
>> processes. However, the relevant control message seems to be missing
>> in Guile's POSIX facilities (send only accepts a data message).
>
> Indeed, we lack that functionality in core Guile.
>
>> Am I mistaken, and are there any plans to add control message support to
>> Guile's library?
>
> I'm not aware of any plans to add it, but if we can agree on an API
> and someone contributed a suitable implementation, I don't see why we
> couldn't add it.

The C API is really messy. However, after looking at it from different
angles, I'm thinking one shouldn't deviate too far from it. Here is the
germ of a Guile API proposal:

   sendmsg NAME BYTEVECTOR-INPUT-PORT [CMSG-LIST [flags]]
   => integer

NAME is a placeholder and should be set to #f.

BYTEVECTOR-INPUT-PORT may be #f. It captures the idea of the gather
array.

CMSG-LIST may be #f. Otherwise, it is a list or lists. The member lists
have three elements: level, type, bytevector.

To support creating the CMSG-LIST elements portably, auxiliary functions
are provided:

   cmsghdr:make-socket-rights list-of-ports-and/or-fds
   => bytevector

   cmsghdr:make-credentials pid uid gid
   => bytevector

For example:

   (sendmsg #f #f (list (list SOL_SOCKET
                              SCM_RIGHTS
                              (cmsghdr:make-socket-rights (list input-fd)))))


For the other direction:

   recvmsg [BYTEVECTOR-OUTPUT-PORT [flags]]
   => values COUNT NAME CMSG-LIST

BYTEVECTOR-OUTPUT-PORT may be #f. It represents the scatter array.

NAME is a number or #f.

CMSG-LIST may be #f. Otherwise, it is as above.

To support interpreting the CMSG-LIST elements portably, auxiliary
functions are provided:

   cmsghdr:socket-rights bytevector
   => list-of-fds

   cmsghdr:socket-credentials bytevector
   => values pid uid gid

For example:

   (call-with-values
     recvmsg
     (lambda (count name cmsg-list)
       (let loop ((cmsgs cmsg-list) (fds '()))
         (if (null? cmsgs)
             fds
             (loop (cdr cmsgs)
                   (let ((cmsg (car cmsgs)))
                     (if (and (= SOL_SOCKET (car cmsg))
                              (= SCM_RIGHTS (cadr cmsg)))
                         (append fds (cmsghdr:socket-rights (caddr cmsg)))))
                         fds)))))


Marko



      reply	other threads:[~2014-10-11 22:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-11  7:47 Guile: passing a file descriptor Marko Rauhamaa
2014-10-11 17:40 ` Mark H Weaver
2014-10-11 22:20   ` Marko Rauhamaa [this message]

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=87k3462qq3.fsf@elektro.pacujo.net \
    --to=marko@pacujo.net \
    --cc=guile-user@gnu.org \
    --cc=mhw@netris.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).