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