1 /* Copyright (C) 2001,2002,2004 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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
21 #include <sys/socket.h>
26 #include <hurd/ifsock.h>
27 #include <hurd/socket.h>
29 /* Send a message described MESSAGE on socket FD.
30 Returns the number of bytes sent, or -1 for errors. */
32 __libc_sendmsg (int fd
, const struct msghdr
*message
, int flags
)
35 struct sockaddr_un
*addr
= message
->msg_name
;
36 socklen_t addr_len
= message
->msg_namelen
;
37 addr_port_t aport
= MACH_PORT_NULL
;
42 } data
= { .ptr
= NULL
};
44 mach_msg_type_number_t len
;
45 mach_msg_type_number_t amount
;
49 /* Find the total number of bytes to be written. */
51 for (i
= 0; i
< message
->msg_iovlen
; i
++)
53 if (message
->msg_iov
[i
].iov_len
> 0)
55 /* As an optimization, if we only have a single non-empty
56 iovec, we set DATA and LEN from it. */
58 data
.ptr
= message
->msg_iov
[i
].iov_base
;
62 len
+= message
->msg_iov
[i
].iov_len
;
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. */
77 err
= __vm_allocate (__mach_task_self (), &data
.addr
, len
, 1);
88 /* Copy the data into DATA. */
91 for (i
= 0; i
< len
; i
++)
93 #define min(a, b) ((a) > (b) ? (b) : (a))
94 size_t copy
= min (message
->msg_iov
[i
].iov_len
, to_copy
);
96 buf
= __mempcpy (buf
, message
->msg_iov
[i
].iov_base
, copy
);
106 if (addr
->sun_family
== AF_LOCAL
)
108 /* For the local domain, we must look up the name as a file
109 and talk to it with the ifsock protocol. */
110 file_t file
= __file_name_lookup (addr
->sun_path
, 0, 0);
111 if (file
== MACH_PORT_NULL
)
113 err
= __ifsock_getsockaddr (file
, &aport
);
114 __mach_port_deallocate (__mach_task_self (), file
);
115 if (err
== MIG_BAD_ID
|| err
== EOPNOTSUPP
)
116 /* The file did not grok the ifsock protocol. */
119 return __hurd_fail (err
);
125 err
= HURD_DPORT_USE (fd
,
128 err
= __socket_create_address (port
,
136 err
= __socket_send (port
, aport
,
137 flags
, data
.ptr
, len
,
139 MACH_MSG_TYPE_COPY_SEND
, 0,
140 message
->msg_control
,
141 message
->msg_controllen
,
143 __mach_port_deallocate (__mach_task_self (),
150 __vm_deallocate (__mach_task_self (), data
.addr
, len
);
152 return err
? __hurd_dfail (fd
, err
) : amount
;
155 weak_alias (__libc_sendmsg
, sendmsg
)
156 weak_alias (__libc_sendmsg
, __sendmsg
)