1 /* Copyright (C) 2001-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, see <https://www.gnu.org/licenses/>. */
20 #include <sys/socket.h>
24 #include <hurd/socket.h>
25 #include <sysdep-cancel.h>
27 /* Receive a message as described by MESSAGE from socket FD.
28 Returns the number of bytes read or -1 for errors. */
30 __libc_recvmsg (int fd
, struct msghdr
*message
, int flags
)
35 mach_msg_type_number_t len
= 0;
36 mach_port_t
*ports
, *newports
= NULL
;
37 mach_msg_type_number_t nports
= 0;
40 mach_msg_type_number_t clen
= 0;
43 int nfds
, *opened_fds
= NULL
;
48 error_t
reauthenticate (mach_port_t port
, mach_port_t
*result
)
52 ref
= __mach_reply_port ();
55 cancel_oldtype
= LIBC_CANCEL_ASYNC();
57 err
= __io_reauthenticate (port
, ref
, MACH_MSG_TYPE_MAKE_SEND
);
61 err
= __USEPORT_CANCEL (AUTH
, __auth_user_authenticate (port
,
62 ref
, MACH_MSG_TYPE_MAKE_SEND
,
65 LIBC_CANCEL_RESET (cancel_oldtype
);
67 __mach_port_destroy (__mach_task_self (), ref
);
71 /* Find the total number of bytes to be read. */
73 for (i
= 0; i
< message
->msg_iovlen
; i
++)
75 amount
+= message
->msg_iov
[i
].iov_len
;
77 /* As an optimization, we set the initial values of DATA and LEN
78 from the first non-empty iovec. This kicks-in in the case
79 where the whole packet fits into that iovec buffer. */
80 if (data
== NULL
&& message
->msg_iov
[i
].iov_len
> 0)
82 data
= message
->msg_iov
[i
].iov_base
;
83 len
= message
->msg_iov
[i
].iov_len
;
88 cancel_oldtype
= LIBC_CANCEL_ASYNC();
89 err
= HURD_DPORT_USE_CANCEL (fd
, __socket_recv (port
, &aport
,
93 &message
->msg_flags
, amount
));
94 LIBC_CANCEL_RESET (cancel_oldtype
);
96 return __hurd_sockfail (fd
, flags
, err
);
98 if (message
->msg_name
!= NULL
&& aport
!= MACH_PORT_NULL
)
100 char *buf
= message
->msg_name
;
101 mach_msg_type_number_t buflen
= message
->msg_namelen
;
104 cancel_oldtype
= LIBC_CANCEL_ASYNC();
105 err
= __socket_whatis_address (aport
, &type
, &buf
, &buflen
);
106 LIBC_CANCEL_RESET (cancel_oldtype
);
108 if (err
== EOPNOTSUPP
)
109 /* If the protocol server can't tell us the address, just return a
112 buf
= message
->msg_name
;
119 __mach_port_deallocate (__mach_task_self (), aport
);
120 return __hurd_sockfail (fd
, flags
, err
);
123 if (message
->msg_namelen
> buflen
)
124 message
->msg_namelen
= buflen
;
126 if (buf
!= message
->msg_name
)
128 memcpy (message
->msg_name
, buf
, message
->msg_namelen
);
129 __vm_deallocate (__mach_task_self (), (vm_address_t
) buf
, buflen
);
133 ((struct sockaddr
*) message
->msg_name
)->sa_family
= type
;
135 else if (message
->msg_name
!= NULL
)
136 message
->msg_namelen
= 0;
138 if (MACH_PORT_VALID (aport
))
139 __mach_port_deallocate (__mach_task_self (), aport
);
145 /* Copy the data into MSG. */
147 message
->msg_flags
|= MSG_TRUNC
;
152 for (i
= 0; i
< message
->msg_iovlen
; i
++)
154 #define min(a, b) ((a) > (b) ? (b) : (a))
155 size_t copy
= min (message
->msg_iov
[i
].iov_len
, amount
);
157 memcpy (message
->msg_iov
[i
].iov_base
, buf
, copy
);
165 __vm_deallocate (__mach_task_self (), (vm_address_t
) data
, len
);
168 /* Copy the control message into MSG. */
169 if (clen
> message
->msg_controllen
)
170 message
->msg_flags
|= MSG_CTRUNC
;
172 message
->msg_controllen
= clen
;
173 memcpy (message
->msg_control
, cdata
, message
->msg_controllen
);
177 newports
= __alloca (nports
* sizeof (mach_port_t
));
178 opened_fds
= __alloca (nports
* sizeof (int));
181 /* This counts how many ports we processed completely. */
183 /* This counts how many new fds we create. */
186 for (cmsg
= CMSG_FIRSTHDR (message
);
188 cmsg
= CMSG_NXTHDR (message
, cmsg
))
190 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
)
192 /* SCM_RIGHTS support. */
193 /* The fd's flags are passed in the control data. */
194 int *fds
= (int *) CMSG_DATA (cmsg
);
195 nfds
= (cmsg
->cmsg_len
- CMSG_ALIGN (sizeof (struct cmsghdr
)))
198 for (j
= 0; j
< nfds
; j
++)
200 int fd_flags
= (flags
& MSG_CMSG_CLOEXEC
) ? O_CLOEXEC
: 0;
201 err
= reauthenticate (ports
[i
], &newports
[newfds
]);
204 /* We do not currently take any flag from the sender. */
205 fds
[j
] = opened_fds
[newfds
] = _hurd_intern_fd (newports
[newfds
],
212 __mach_port_deallocate (__mach_task_self (), newports
[newfds
]);
221 for (i
= 0; i
< nports
; i
++)
222 __mach_port_deallocate (mach_task_self (), ports
[i
]);
224 __vm_deallocate (__mach_task_self (), (vm_address_t
) cdata
, clen
);
229 /* Clean up all the file descriptors from port 0 to i-1. */
234 for (cmsg
= CMSG_FIRSTHDR (message
);
236 cmsg
= CMSG_NXTHDR (message
, cmsg
))
238 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
)
240 nfds
= (cmsg
->cmsg_len
- CMSG_ALIGN (sizeof (struct cmsghdr
)))
242 for (j
= 0; j
< nfds
&& ii
< i
; j
++, ii
++, newfds
++)
244 _hurd_fd_close (_hurd_fd_get (opened_fds
[newfds
]));
245 __mach_port_deallocate (__mach_task_self (), newports
[newfds
]);
246 __mach_port_deallocate (__mach_task_self (), ports
[ii
]);
252 __vm_deallocate (__mach_task_self (), (vm_address_t
) cdata
, clen
);
253 return __hurd_fail (err
);
256 weak_alias (__libc_recvmsg
, recvmsg
)
257 weak_alias (__libc_recvmsg
, __recvmsg
)