xf.li | bfc6e71 | 2025-02-07 01:54:34 -0800 | [diff] [blame^] | 1 | /* Copyright (C) 2001-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 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. |
| 8 | |
| 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. |
| 13 | |
| 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 <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <string.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <sys/un.h> |
| 22 | |
| 23 | #include <hurd.h> |
| 24 | #include <hurd/fd.h> |
| 25 | #include <hurd/ifsock.h> |
| 26 | #include <hurd/socket.h> |
| 27 | #include "hurd/hurdsocket.h" |
| 28 | |
| 29 | /* Send a message described MESSAGE on socket FD. |
| 30 | Returns the number of bytes sent, or -1 for errors. */ |
| 31 | ssize_t |
| 32 | __libc_sendmsg (int fd, const struct msghdr *message, int flags) |
| 33 | { |
| 34 | error_t err = 0; |
| 35 | struct sockaddr_un *addr = message->msg_name; |
| 36 | socklen_t addr_len = message->msg_namelen; |
| 37 | addr_port_t aport = MACH_PORT_NULL; |
| 38 | union |
| 39 | { |
| 40 | char *ptr; |
| 41 | vm_address_t addr; |
| 42 | } data = { .ptr = NULL }; |
| 43 | char data_buf[2048]; |
| 44 | mach_msg_type_number_t len; |
| 45 | mach_msg_type_number_t amount; |
| 46 | int dealloc = 0; |
| 47 | int i; |
| 48 | |
| 49 | /* Find the total number of bytes to be written. */ |
| 50 | len = 0; |
| 51 | for (i = 0; i < message->msg_iovlen; i++) |
| 52 | { |
| 53 | if (message->msg_iov[i].iov_len > 0) |
| 54 | { |
| 55 | /* As an optimization, if we only have a single non-empty |
| 56 | iovec, we set DATA and LEN from it. */ |
| 57 | if (len == 0) |
| 58 | data.ptr = message->msg_iov[i].iov_base; |
| 59 | else |
| 60 | data.ptr = NULL; |
| 61 | |
| 62 | len += message->msg_iov[i].iov_len; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if (data.ptr == NULL) |
| 67 | { |
| 68 | size_t to_copy; |
| 69 | char *buf; |
| 70 | |
| 71 | /* Allocate a temporary buffer to hold the data. For small |
| 72 | amounts of data, we allocate a buffer on the stack. Larger |
| 73 | amounts of data are stored in a page-aligned buffer. The |
| 74 | limit of 2048 bytes is inspired by the MiG stubs. */ |
| 75 | if (len > 2048) |
| 76 | { |
| 77 | err = __vm_allocate (__mach_task_self (), &data.addr, len, 1); |
| 78 | if (err) |
| 79 | { |
| 80 | __set_errno (err); |
| 81 | return -1; |
| 82 | } |
| 83 | dealloc = 1; |
| 84 | } |
| 85 | else |
| 86 | data.ptr = data_buf; |
| 87 | |
| 88 | /* Copy the data into DATA. */ |
| 89 | to_copy = len; |
| 90 | buf = data.ptr; |
| 91 | for (i = 0; i < len; i++) |
| 92 | { |
| 93 | #define min(a, b) ((a) > (b) ? (b) : (a)) |
| 94 | size_t copy = min (message->msg_iov[i].iov_len, to_copy); |
| 95 | |
| 96 | buf = __mempcpy (buf, message->msg_iov[i].iov_base, copy); |
| 97 | |
| 98 | to_copy -= copy; |
| 99 | if (to_copy == 0) |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (addr) |
| 105 | { |
| 106 | if (addr->sun_family == AF_LOCAL) |
| 107 | { |
| 108 | char *name = _hurd_sun_path_dupa (addr, addr_len); |
| 109 | /* For the local domain, we must look up the name as a file |
| 110 | and talk to it with the ifsock protocol. */ |
| 111 | file_t file = __file_name_lookup (name, 0, 0); |
| 112 | if (file == MACH_PORT_NULL) |
| 113 | { |
| 114 | if (dealloc) |
| 115 | __vm_deallocate (__mach_task_self (), data.addr, len); |
| 116 | return -1; |
| 117 | } |
| 118 | err = __ifsock_getsockaddr (file, &aport); |
| 119 | __mach_port_deallocate (__mach_task_self (), file); |
| 120 | if (err == MIG_BAD_ID || err == EOPNOTSUPP) |
| 121 | /* The file did not grok the ifsock protocol. */ |
| 122 | err = ENOTSOCK; |
| 123 | if (err) |
| 124 | { |
| 125 | if (dealloc) |
| 126 | __vm_deallocate (__mach_task_self (), data.addr, len); |
| 127 | return __hurd_fail (err); |
| 128 | } |
| 129 | } |
| 130 | else |
| 131 | err = EIEIO; |
| 132 | } |
| 133 | |
| 134 | err = HURD_DPORT_USE (fd, |
| 135 | ({ |
| 136 | if (err) |
| 137 | err = __socket_create_address (port, |
| 138 | addr->sun_family, |
| 139 | (char *) addr, |
| 140 | addr_len, |
| 141 | &aport); |
| 142 | if (! err) |
| 143 | { |
| 144 | /* Send the data. */ |
| 145 | err = __socket_send (port, aport, |
| 146 | flags, data.ptr, len, |
| 147 | NULL, |
| 148 | MACH_MSG_TYPE_COPY_SEND, 0, |
| 149 | message->msg_control, |
| 150 | message->msg_controllen, |
| 151 | &amount); |
| 152 | __mach_port_deallocate (__mach_task_self (), |
| 153 | aport); |
| 154 | } |
| 155 | err; |
| 156 | })); |
| 157 | |
| 158 | if (dealloc) |
| 159 | __vm_deallocate (__mach_task_self (), data.addr, len); |
| 160 | |
| 161 | return err ? __hurd_sockfail (fd, flags, err) : amount; |
| 162 | } |
| 163 | |
| 164 | weak_alias (__libc_sendmsg, sendmsg) |
| 165 | weak_alias (__libc_sendmsg, __sendmsg) |