Re: Can you pass a file descriptor to another process?
Re: Can you pass a file descriptor to another process?
- Subject: Re: Can you pass a file descriptor to another process?
- From: Wim Lewis <email@hidden>
- Date: Wed, 20 Aug 2014 19:21:17 -0700
- Resent-date: Wed, 20 Aug 2014 19:24:29 -0700
- Resent-from: Wim Lewis <email@hidden>
- Resent-message-id: <email@hidden>
- Resent-to: email@hidden
On 20 Aug 2014, at 5:46 PM, James Bucanek wrote:
> I was sure this wouldn't work for pipes, but I was wrong. (Of course it works, this is UNIX.) The code was easy:
>
> char path[MAXPATHLEN];
> int fd = fileno(stdout);
> int result = fcntl(fd, F_GETPATH, path);
>
> When you get the path of your stdout pipe, fcntl() returns a path to a /dev/ttyxxx filesystem object. Magically, my helper process was able to open(+a) that bad boy and write to it. Will wonders never cease?
I think that'll only work if your stdout is something that has a path. :) If the fd in question is an actual pipe (e.g. "command | less") or a socket or a file that has been unlinked but still exists due to having an open fd, then I assume the F_GETPATH fcntl will fail. (I suppose it could return a /dev/fd/XX path, but that wouldn't be useful to you.)
You could achieve what you want using the following dance:
- either the daemon or the tool creates a unix-domain datagram socket and binds it to somewhere in /tmp (e.g. a randomly-generated path with restrictive permissions)
- the other one opens that socket, perhaps after learning its pathname via DO
- the tool sends a message over the socket to the daemon, containing the relevant fd as ancillary data
IIRC, you can send arbitrary Mach ports over a DO connection by wrapping them in NSPorts, right? If the private API that lets you pass fds around via Mach ports exists on 10.6, you could use that. But then you're using private API. The SCM_RIGHTS stuff has the advantage of being standard, public BSD API.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Filesystem-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden