4
0
mirror of git://sourceware.org/git/newlib-cygwin.git synced 2025-02-02 12:30:24 +08:00

116 Commits

Author SHA1 Message Date
Ken Brown
6841fcb82f Cygwin: AF_UNIX: SCM_RIGHTS: support pty master descriptors
This requires modifying fhandler_pty_master::dup so that it duplicates
handles when it is called with non-zero src_pid on an fhandler that
doesn't yet have an archetype.
2021-01-07 13:58:58 -05:00
Ken Brown
255afed438 Cygwin: AF_UNIX: SCM_RIGHTS: support console descriptors
This requires modifying fhandler_console::dup so that it duplicates
handles when it is called with non-zero src_pid on an fhandler that
doesn't yet have an archetype.

As in the recent change to fhandler_pty_slave::dup, dtable::dup_worker
will then create an archetype for the new fhandler after
fhandler_console::dup returns.

Untested.
2020-12-15 11:48:04 -05:00
Ken Brown
c605ea0d6d Cygwin: AF_UNIX: SCM_RIGHTS: support pty slave descriptors
This is the first case where the fhandler being sent uses an
archetype.  This archetype is of no use to the receiving process, so
we have to make the following changes:

- Modify fhandler_pty_slave::dup so that it duplicates handles when it
  is called on an fhandler that doesn't yet have an archetype.

- Modify dtable::dup_worker so that it creates an archetype for the
  new fhandler after fhandler_pty_slave::dup returns.
2020-12-11 17:41:06 -05:00
Ken Brown
81e265492a Cygwin: AF_UNIX: code simplification 2020-12-11 17:41:05 -05:00
Ken Brown
63f6443bba Cygwin: AF_UNIX: add FIXME comment 2020-11-28 09:40:56 -05:00
Ken Brown
3b8be197f7 Cygwin: AF_UNIX: SCM_RIGHTS: support socket descriptors
This requires modifying fhandler_socket_wsock::dup and
fhandler_socket_unix::dup to use the src_pid argument.

First cut.  Not yet tested.
2020-11-22 15:02:39 -05:00
Ken Brown
877427fbaa Cygwin: AF_UNIX: add _pipe_end member and related methods
Use this to keep track of whether our pipe handle is for the server or
client end of the pipe.
2020-11-22 14:17:52 -05:00
Ken Brown
d9bb7965f9 Cygwin: AF_UNIX: code simplification
Add a 'src_pid' argument to dtable::dup_worker.

Use the latter in serialize/deserialize rather than repeating much of
what it does.

Don't duplicate the path_conv handle; it isn't needed.
2020-11-22 14:13:00 -05:00
Ken Brown
81ceb96528 Cygwin: AF_UNIX: create an ack mechanism for SCM_RIGHTS
When a process sends a file descriptor via an SCM_RIGHTS control
message, it creates a temporary copy of the fhandler associated with
that descriptor and sends a serialization of that copy.  The
deserialization done by the receiver involves duplicating handles from
the copy, so the latter must stay alive until the deserialization is
done.  But it must ultimately be closed in order to avoid a memory
leak.

We coordinate all this as follows:

- Introduce a new struct scm_pending_fd that contains information
  about the temporary copy.  For brevity, call such a struct a
  "pending fd" in what follows.

- Maintain a list of pending fds in shared memory.

- Add several methods for manipulating the list to the af_unix_shmem_t
  and fhandler_socket_unix classes.

- Also add a lock, 'scm_fd_lock', to control access to the list.

- When a serialized fhandler is received, the receiver sends an ack
  back to the sender in an administrative packet with a control
  message of a new (Cygwin-specific) type SCM_RIGHTS_ACK.

- grab_admin_pkt is called in various places to process these packets.
  A complication here is that the process that calls grab_admin_pkt
  might not be the process that originally sent the serialized
  fhandler.  (It could be a subprocess of the original process, for
  example.)  This is why we need to maintain the list of pending fds
  in shared memory.

- Each fhandler_socket_unix keeps a count of the pending fds that it
  has created but not yet processed; this count is in a new data
  member 'my_npending_fd'.

- fhandler_socket_unix::close tries to process any remaining pending
  fds before closing, but it gives up after a short timeout and
  forcibly deletes them if necessary.
2020-11-21 08:01:10 -05:00
Ken Brown
ee4f7ec67e Cygwin: AF_UNIX: add a unique id to struct fh_ser 2020-11-21 08:01:10 -05:00
Ken Brown
5a98fbb4f8 Cygwin: AF_UNIX: tweak serialize/deserialize
Make them member functions of the fhandler_socket_unix class.

Make them use void * instead of fh_ser * so that fhandler.h doesn't
need to know about fh_ser.
2020-11-21 08:01:10 -05:00
Ken Brown
9e6f77d4ef Cygwin: AF_UNIX: peek_pipe_poll: fix thinko
The variable 'sleep_time' was being reset to 0 on every loop
iteration.
2020-11-21 08:01:10 -05:00
Ken Brown
05d40cdef3 Cygwin: AF_UNIX: implement sending/receiving SCM_RIGHTS ancillary data
First cut.  This is currently implemented only for disk files, and
many things still need to be fixed.  But it works in limited testing
with the programs scm_rights_{recv,send} in
winsup/cygwin/socket_tests.
2020-11-17 14:42:04 -05:00
Ken Brown
e8b866676f Cygwin: AF_UNIX: add serialization/deserialization for fhandlers
Define static helper functions serialize/deserialize in
fhandler_socket_unix.cc.  These will be used to support sending file
descriptors via SCM_RIGHTS control messages.

The serialize function creates an 'fh_ser' structure that contains a
copy of the fhandler associated with the file descriptor, with all
allocated memory freed.  The structure also contains the Windows pid
of the current process, which deserialize can use for duplicating
handles.

The deserialize function reconstructs an fhandler from an fh_ser
structure, with the handles duplicated into its own process.

For now, serialization and deserialization are fully implemented only
for disk files, and even in that case there are many FIXMEs that need
attention.
2020-11-17 14:42:01 -05:00
Ken Brown
91ce3d3fd7 Cygwin: fhandler_*::dup: add a 'src_pid' argument
This allows duplication of handles from an fhandler created in a
different process.  For now, this is implemented only for
fhandler_base and fhandler_disk_file.
2020-11-17 09:21:56 -05:00
Ken Brown
3d37fd9cef Cygwin: AF_UNIX: improve control message handling
This includes various changes to create_cmsg_data and
evaluate_cmsg_data.  The most important are:

- create_cmsg_data now allows only one SCM_RIGHTS message and one
  SCM_CREDENTIALS message.

- evaluate_cmsg_data now truncates the ancillary data to the number of
  control messages that will fit in the supplied buffer.  Previously
  it discarded all control messages if the buffer was too small.

See https://man7.org/linux/man-pages/man7/unix.7.html.
2020-11-02 11:50:46 -05:00
Ken Brown
d42acb9bc8 Cygwin: AF_UNIX: recv_peer_info: fix return value
The variable 'ret' was being used with two different meanings.  As a
result, setting ret to an error code was not actualy setting the
return value.
2020-11-01 21:58:34 -05:00
Ken Brown
852fb2afba Cygwin: AF_UNIX: allow more than one control message
Previously, create_cmsg_data and evaluate_cmsg_data required the
ancillary data to contain only a single control message, of type
SCM_CREDENTIALS.  In preparation for supporting SCM_RIGHTS in the
future, allow more than one.

create_cmsg_data now iterates through the specified control messages
and allows both SCM_CREDENTIALS and SCM_RIGHTS.  If no SCM_CREDENTIALS
message is present, it creates one.  This was previously done in
sendmsg.

evaluate_cmsg_data also iterates through the received control messages
and allows both SCM_CREDENTIALS and SCM_RIGHTS.  Control messages of
type SCM_CREDENTIALS are discarded unless the SO_PASSCRED option has
been set.

Update tests.
2020-11-01 15:58:00 -05:00
Ken Brown
a55b1a60c5 Cygwin: AF_UNIX: recvmsg: fix MSG_PEEK support for datagrams
Add a new HANDLE argument to peek_pipe and peek_pipe_poll so that the
caller can specify a pipe handle to use in lieu of get_handle().  Use
this in recvmsg to make the MSG_PEEK flag work for unbound datagram
sockets.

Untested.
2020-10-29 12:27:24 -04:00
Ken Brown
d68d091944 Cygwin: AF_UNIX: recvmsg: support the MSG_TRUNC flag
This is in Linux since Linux 3.4 but not in Posix.

Not tested.
2020-10-29 11:39:00 -04:00
Ken Brown
c69c850df4 Cygwin: AF_UNIX: sendmsg: always send credentials
If the caller doesn't specify ancillary data, add credentials to the
outgoing packet.

This enables us to satisfy the requirement
(https://man7.org/linux/man-pages/man7/unix.7.html) that a socket with
the SO_PASSCRED option enabled can get the credentials of its peer in
every message it receives.

FIXME: I'm not sure if this is the right way to satisfy that
requirement.  A possible alternative would be to arrange for a socket
to be notified when its peer enables SO_PASSCRED.
2020-10-29 10:14:34 -04:00
Ken Brown
20fd0db075 Cygwin: AF_UNIX: create_cmsg_data: fix credential checking
Require the pid specified by an administrator to be the pid of an
existing process.
2020-10-29 09:17:38 -04:00
Ken Brown
fab8f9b104 Cygwin: AF_UNIX: create_cmsg_data: check credentials
If the user is not an administrator, check that the specified
credentials match the user's credentials.
2020-10-28 12:10:54 -04:00
Ken Brown
e5f2a0b1c8 Cygwin: AF_UNIX: FIXME comments 2020-10-28 10:38:49 -04:00
Ken Brown
d662a977f9 Cygwin: AF_UNIX: socketpair: set blocking mode earlier
Call set_nonblocking before creating the pipe so that the pipe is
created with the correct blocking mode.

Also call set_pipe_non_blocking on the second socket so that the
client end of the pipe has the correct blocking mode.  This also makes
sure that the client end of the pipe is set to message mode for
reading.
2020-10-28 10:01:03 -04:00
Ken Brown
7563d755f4 Cygwin: AF_UNIX: recvmsg: various improvements
If the caller has requested the source address, try to get it on each
iteration of the main read loop, not just the first.  Set msg_namelen
to 0 if it is never received.

If a packet containing control message data is received, don't read
any more packets, even if MSG_WAITALL is set.
2020-10-28 09:25:16 -04:00
Ken Brown
b1593593f0 Cygwin: AF_UNIX: create_cmsg_data: improve error handling
Set an errno before returning false.
2020-10-28 09:06:13 -04:00
Ken Brown
3ef34ca308 Cygwin: AF_UNIX: evaluate_cmsg_data: improve error handling
Set msg_controllen to the number of bytes of ancillary data actually
copied to msg_control.

Set the MSG_CTRUNC flag when appropriate.
2020-10-28 09:06:13 -04:00
Ken Brown
82474332a6 Cygwin: AF_UNIX: recvmsg: set MSG_TRUNC for truncated datagrams
If a datagram is truncated because it is larger than the supplied
buffer, set MSG_TRUNC in the msg_flags field of the (msghdr *)
argument.  See https://man7.org/linux/man-pages/man2/recvmsg.2.html.
2020-10-28 09:06:12 -04:00
Ken Brown
785de1a08d Cygwin: AF_UNIX: recvmsg: fix indentation 2020-10-28 09:06:12 -04:00
Ken Brown
7c0d061ec8 Cygwin: AF_UNIX: minor tweak 2020-10-26 14:51:08 -04:00
Ken Brown
67da0f3087 Cygwin: AF_UNIX: peek_pipe: check for STATUS_BUFFER_OVERFLOW
Treat this the same as STATUS_SUCCESS.
2020-10-26 14:50:09 -04:00
Ken Brown
abb6475a3b Cygwin: AF_UNIX: grab_admin_pkt: check for unread data in pipe
If there's unread data in the pipe from a previous partial read of a
packet, just return.  There can't be an administrative packet waiting
to be read in that case.
2020-10-26 14:38:20 -04:00
Ken Brown
3a6137eb3a Cygwin: AF_UNIX: accept4: set O_RDWR on new file descriptor 2020-10-26 10:53:37 -04:00
Ken Brown
b3cef213d8 Cygwin: AF_UNIX: evaluate_cmsg_data: more FIXMEs 2020-10-24 10:48:55 -04:00
Ken Brown
408636b186 Cygwin: AF_UNIX: sendmsg: add FIXME comment 2020-10-24 10:05:17 -04:00
Ken Brown
f85a03882e Cygwin: AF_UNIX: implement evaluate_cmsg_data
And use it in recvmsg.

I'm not sure this implementation is what was intended when the
evaluate_cmsg_data method was added.

For now, just support an ancillary data block consisting of a single
cmsghdr, containing SCM_CREDENTIALS.

For convenience, add a 'mshdr *' argument and make the 'cloexec'
argument false by default.  The 'cloexec' argument is not currently
used, and I want to avoid having to artificially specify a value for
it when recvmsg calls evaluate_cmsg_data.
2020-10-24 10:00:40 -04:00
Ken Brown
9cfea13054 Cygwin: AF_UNIX: implement create_cmsg_data
For now, just support an ancillary data block consisting of a single
cmsghdr, containing SCM_CREDENTIALS.

FIXME: We're supposed to check the credentials.
2020-10-24 09:56:58 -04:00
Ken Brown
324faf1e07 Cygwin: AF_UNIX: socketpair: fix connect state
Both sockets should have connect state 'connected', not just the
second.
2020-10-22 15:12:29 -04:00
Ken Brown
6b08c5e15d Cygwin: AF_UNIX: reopen_shmem: fix view size
Use sizeof (af_unix_shmem_t) as the view size, as when the shared
memory was created.  Previously PAGESIZE was used, causing
NtMapViewOfSection to fail with STATUS_INVALID_VIEW_SIZE.
2020-10-22 14:55:52 -04:00
Ken Brown
7992fc068d Cygwin: AF_UNIX: fill in the default constructor
Previously the default constructor did nothing.  In particular,
fixup_after_fork never got called because need_fork_fixup wasn't set.
2020-10-22 14:45:34 -04:00
Ken Brown
8d99c2f9e2 Cygwin: AF_UNIX: socketpair: set O_RDWR 2020-10-22 12:41:59 -04:00
Ken Brown
58205dc374 Cygwin: AF_UNIX: move the definition of class af_unix_pkt_hdr_t
It is needed in select.cc, so move the definition from
fhandler_socket_unix.cc to fhandler.h.
2020-10-21 19:46:48 -04:00
Ken Brown
533c9b05b8 Cygwin: AF_UNIX: sendmsg: call grab_admin_pkt
Check to see if the peer has sent a shutdown packet before calling
saw_shutdown.
2020-10-21 08:44:06 -04:00
Ken Brown
0ee48565f0 Cygwin: AF_UNIX: recvmsg: check for shutdown
Call grab_admin_pkt at appropriate times to check whether we've been
shut down for reading.  Also, update our shutdown state whenever we
read a packet.

Untested.

FIXME: I'm not sure whether I've treated datagram sockets properly.
2020-10-19 17:23:40 -04:00
Ken Brown
99490d2825 Cygwin: AF_UNIX: grab_admin_pkg: refactor
Extract from grab_admin_pkg two new methods, record_shut_info and
process_admin_pkg.  Also add a new 'peek' argument to grab_admin_pkg.
If this is true, peek to see if the next packet in the pipe is an
administrative packet.  Otherwise, assume we already know it is.
2020-10-19 17:22:34 -04:00
Ken Brown
c60fcf1116 Cygwin: AF_UNIX: sendmsg: send shutdown state
Send the current shutdown state rather than _SHUT_NONE as part of
every packet sent.
2020-10-19 09:26:41 -04:00
Ken Brown
95012189c0 Cygwin: AF_UNIX: sendmsg: fix errno if socket is shut down
Following Posix and Linux, the errno should be EPIPE, not ESHUTDOWN.
2020-10-19 08:56:59 -04:00
Ken Brown
e1beea13e1 Cygwin: AF_UNIX: recvmsg: fix thinko
The pointer 'my_iovptr' was being incremented before it was used.
2020-10-17 14:58:53 -04:00
Ken Brown
c294f50f3d Cygwin: AF_UNIX: add two FIXME comments 2020-10-14 15:00:26 -04:00