Ability to adopt file descriptors
commit6622d115c795d88b99d844ce8b74e979a2b55ae6
authorChristopher Lord <christopher@lord.ac>
Sat, 3 Jan 2015 17:15:58 +0000 (3 10:15 -0700)
committerEric Wong <normalperson@yhbt.net>
Sun, 4 Jan 2015 23:51:17 +0000 (4 23:51 +0000)
tree291420e6af7d4b7576b8638539ee278e96c7aad7
parentb14ae708a9e53b1f8a274aa78a610b2f776847f5
Ability to adopt file descriptors

This patch adds support for adopting an existing file descriptor, together
with testcases. The need for this comes up when we use systemd with the
ListenMessageQueue directive. For socket activation, systemd opens the POSIX
message queue and expects user code to begin using the file descriptor
without opening it in-process.

To support the systemd model in the `posix_mq` gem, this patch suggests
imitating the behavior on the Socket class, which uses `#for_fd` to create a
socket class from a descriptor.

One confounding factor exists. POSIX queues have a name but it is difficult
to get access to this name in a safe manner from the file descriptor. One
option would be to `readlink(2)` on `/proc/self/fd/N` to get the name[1], but
note that if the descriptor is unlinked we wouldn't get anything usable.
Rather than risk incorrect behavior and extra complexity, I've decided to
just raise an `ArgumentError` if `#name` is called on adopted descriptors.
Typically one wouldn't need the actual name in a systemd socket-activated
situation, anyway.

[1]: http://stackoverflow.com/questions/1188757/getting-filename-from-file-descriptor-in-c

[ew: simplified type checks to be consistent with IO.for_fd,
 fixed test case]

Signed-off-by: Eric Wong <normalperson@yhbt.net>
ext/posix_mq/posix_mq.c
test/test_posix_mq.rb