2 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
4 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
8 #include <sys/syscall.h>
9 #include <sys/socket.h>
12 #ifdef __NR_socketcall
13 /* Various socketcall numbers */
19 #define SYS_GETSOCKNAME 6
20 #define SYS_GETPEERNAME 7
21 #define SYS_SOCKETPAIR 8
25 #define SYS_RECVFROM 12
26 #define SYS_SHUTDOWN 13
27 #define SYS_SETSOCKOPT 14
28 #define SYS_GETSOCKOPT 15
29 #define SYS_SENDMSG 16
30 #define SYS_RECVMSG 17
31 #define SYS_ACCEPT4 18
39 #undef __NR_getpeername
40 #undef __NR_getsockname
41 #undef __NR_getsockopt
49 #undef __NR_setsockopt
52 #undef __NR_socketpair
56 static int __NC(accept
)(int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
)
59 return INLINE_SYSCALL(accept
, 3, sockfd
, addr
, addrlen
);
61 unsigned long args
[3];
64 args
[1] = (unsigned long) addr
;
65 args
[2] = (unsigned long) addrlen
;
67 return __socketcall(SYS_ACCEPT
, args
);
70 CANCELLABLE_SYSCALL(int, accept
, (int sockfd
, struct sockaddr
*addr
, socklen_t
*addrlen
),
71 (sockfd
, addr
, addrlen
))
72 lt_libc_hidden(accept
)
77 # define __NR___sys_accept4 __NR_accept4
78 static _syscall4(int, __sys_accept4
, int, fd
, struct sockaddr
*, addr
, socklen_t
*, addrlen
, int, flags
)
79 int accept4(int fd
, struct sockaddr
*addr
, socklen_t
* addrlen
, int flags
)
82 return __sys_accept4(fd
, addr
, addrlen
, flags
);
83 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
85 int oldtype
= LIBC_CANCEL_ASYNC ();
86 int result
= __sys_accept4(fd
, addr
, addrlen
, flags
);
87 LIBC_CANCEL_RESET (oldtype
);
92 #elif defined(__NR_socketcall)
93 int accept4(int fd
, struct sockaddr
*addr
, socklen_t
*addrlen
, int flags
)
95 unsigned long args
[4];
98 args
[1] = (unsigned long) addr
;
99 args
[2] = (unsigned long) addrlen
;
102 return __socketcall(SYS_ACCEPT4
, args
);
103 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
105 int oldtype
= LIBC_CANCEL_ASYNC ();
106 int result
= __socketcall(SYS_ACCEPT4
, args
);
107 LIBC_CANCEL_RESET (oldtype
);
116 int bind(int sockfd
, const struct sockaddr
*myaddr
, socklen_t addrlen
)
119 return INLINE_SYSCALL(bind
, 3, sockfd
, myaddr
, addrlen
);
121 unsigned long args
[3];
124 args
[1] = (unsigned long) myaddr
;
126 return __socketcall(SYS_BIND
, args
);
129 libc_hidden_def(bind
)
133 static int __NC(connect
)(int sockfd
, const struct sockaddr
*saddr
, socklen_t addrlen
)
136 return INLINE_SYSCALL(connect
, 3, sockfd
, saddr
, addrlen
);
138 unsigned long args
[3];
141 args
[1] = (unsigned long) saddr
;
143 return __socketcall(SYS_CONNECT
, args
);
146 CANCELLABLE_SYSCALL(int, connect
, (int sockfd
, const struct sockaddr
*saddr
, socklen_t addrlen
),
147 (sockfd
, saddr
, addrlen
))
148 lt_libc_hidden(connect
)
152 int getpeername(int sockfd
, struct sockaddr
*addr
, socklen_t
*paddrlen
)
154 # ifdef __NR_getpeername
155 return INLINE_SYSCALL(getpeername
, 3, sockfd
, addr
, paddrlen
);
157 unsigned long args
[3];
160 args
[1] = (unsigned long) addr
;
161 args
[2] = (unsigned long) paddrlen
;
162 return __socketcall(SYS_GETPEERNAME
, args
);
168 int getsockname(int sockfd
, struct sockaddr
*addr
, socklen_t
* paddrlen
)
170 # ifdef __NR_getsockname
171 return INLINE_SYSCALL(getsockname
, 3, sockfd
, addr
, paddrlen
);
173 unsigned long args
[3];
176 args
[1] = (unsigned long) addr
;
177 args
[2] = (unsigned long) paddrlen
;
178 return __socketcall(SYS_GETSOCKNAME
, args
);
181 libc_hidden_def(getsockname
)
185 int getsockopt(int fd
, int level
, int optname
, void *optval
,
188 # ifdef __NR_getsockopt
189 return INLINE_SYSCALL(getsockopt
, 5, fd
, level
, optname
, optval
, optlen
);
191 unsigned long args
[5];
196 args
[3] = (unsigned long) optval
;
197 args
[4] = (unsigned long) optlen
;
198 return (__socketcall(SYS_GETSOCKOPT
, args
));
204 int listen(int sockfd
, int backlog
)
207 return INLINE_SYSCALL(listen
, 2, sockfd
, backlog
);
209 unsigned long args
[2];
213 return __socketcall(SYS_LISTEN
, args
);
216 libc_hidden_def(listen
)
220 static ssize_t
__NC(recv
)(int sockfd
, void *buffer
, size_t len
, int flags
)
223 return (ssize_t
)INLINE_SYSCALL(recv
, 4, sockfd
, buffer
, len
, flags
);
224 # elif defined __NR_recvfrom && defined _syscall6
225 return __NC(recvfrom
)(sockfd
, buffer
, len
, flags
, NULL
, NULL
);
227 unsigned long args
[4];
230 args
[1] = (unsigned long) buffer
;
233 return (ssize_t
)__socketcall(SYS_RECV
, args
);
236 CANCELLABLE_SYSCALL(ssize_t
, recv
, (int sockfd
, void *buffer
, size_t len
, int flags
),
237 (sockfd
, buffer
, len
, flags
))
242 ssize_t
__NC(recvfrom
)(int sockfd
, void *buffer
, size_t len
, int flags
,
243 struct sockaddr
*to
, socklen_t
*tolen
)
245 # if defined __NR_recvfrom && defined _syscall6
246 return (ssize_t
)INLINE_SYSCALL(recvfrom
, 6, sockfd
, buffer
, len
,
249 unsigned long args
[6];
252 args
[1] = (unsigned long) buffer
;
255 args
[4] = (unsigned long) to
;
256 args
[5] = (unsigned long) tolen
;
257 return (ssize_t
)__socketcall(SYS_RECVFROM
, args
);
260 CANCELLABLE_SYSCALL(ssize_t
, recvfrom
, (int sockfd
, void *buffer
, size_t len
,
261 int flags
, struct sockaddr
*to
, socklen_t
*tolen
),
262 (sockfd
, buffer
, len
, flags
, to
, tolen
))
263 lt_libc_hidden(recvfrom
)
267 static ssize_t
__NC(recvmsg
)(int sockfd
, struct msghdr
*msg
, int flags
)
270 return (ssize_t
)INLINE_SYSCALL(recvmsg
, 3, sockfd
, msg
, flags
);
272 unsigned long args
[3];
275 args
[1] = (unsigned long) msg
;
277 return (ssize_t
)__socketcall(SYS_RECVMSG
, args
);
280 CANCELLABLE_SYSCALL(ssize_t
, recvmsg
, (int sockfd
, struct msghdr
*msg
, int flags
),
281 (sockfd
, msg
, flags
))
282 lt_libc_hidden(recvmsg
)
286 static ssize_t
__NC(send
)(int sockfd
, const void *buffer
, size_t len
, int flags
)
289 return (ssize_t
)INLINE_SYSCALL(send
, 4, sockfd
, buffer
, len
, flags
);
290 # elif defined __NR_sendto && defined _syscall6
291 return __NC(sendto
)(sockfd
, buffer
, len
, flags
, NULL
, 0);
293 unsigned long args
[4];
296 args
[1] = (unsigned long) buffer
;
299 return (ssize_t
)__socketcall(SYS_SEND
, args
);
302 CANCELLABLE_SYSCALL(ssize_t
, send
, (int sockfd
, const void *buffer
, size_t len
, int flags
),
303 (sockfd
, buffer
, len
, flags
))
308 static ssize_t
__NC(sendmsg
)(int sockfd
, const struct msghdr
*msg
, int flags
)
311 return (ssize_t
)INLINE_SYSCALL(sendmsg
, 3, sockfd
, msg
, flags
);
313 unsigned long args
[3];
316 args
[1] = (unsigned long) msg
;
318 return (ssize_t
)__socketcall(SYS_SENDMSG
, args
);
321 CANCELLABLE_SYSCALL(ssize_t
, sendmsg
, (int sockfd
, const struct msghdr
*msg
, int flags
),
322 (sockfd
, msg
, flags
))
323 lt_libc_hidden(sendmsg
)
327 ssize_t
__NC(sendto
)(int sockfd
, const void *buffer
, size_t len
, int flags
,
328 const struct sockaddr
*to
, socklen_t tolen
)
330 # if defined __NR_sendto && defined _syscall6
331 return (ssize_t
)INLINE_SYSCALL(sendto
, 6, sockfd
, buffer
, len
, flags
, to
, tolen
);
333 unsigned long args
[6];
336 args
[1] = (unsigned long) buffer
;
339 args
[4] = (unsigned long) to
;
341 return (ssize_t
)__socketcall(SYS_SENDTO
, args
);
344 CANCELLABLE_SYSCALL(ssize_t
, sendto
, (int sockfd
, const void *buffer
, size_t len
,
345 int flags
, const struct sockaddr
*to
, socklen_t tolen
),
346 (sockfd
, buffer
, len
, flags
, to
, tolen
))
347 lt_libc_hidden(sendto
)
351 int setsockopt(int fd
, int level
, int optname
, const void *optval
, socklen_t optlen
)
353 # ifdef __NR_setsockopt
354 return INLINE_SYSCALL(setsockopt
, 5, fd
, level
, optname
, optval
, optlen
);
356 unsigned long args
[5];
361 args
[3] = (unsigned long) optval
;
363 return __socketcall(SYS_SETSOCKOPT
, args
);
366 libc_hidden_def(setsockopt
)
370 int shutdown(int sockfd
, int how
)
372 # ifdef __NR_shutdown
373 return INLINE_SYSCALL(shutdown
, 2, sockfd
, how
);
375 unsigned long args
[2];
379 return __socketcall(SYS_SHUTDOWN
, args
);
385 int socket(int family
, int type
, int protocol
)
388 return INLINE_SYSCALL(socket
, 3, family
, type
, protocol
);
390 unsigned long args
[3];
394 args
[2] = (unsigned long) protocol
;
395 return __socketcall(SYS_SOCKET
, args
);
398 libc_hidden_def(socket
)
402 int socketpair(int family
, int type
, int protocol
, int sockvec
[2])
404 # ifdef __NR_socketpair
405 return INLINE_SYSCALL(socketpair
, 4, family
, type
, protocol
, sockvec
);
407 unsigned long args
[4];
412 args
[3] = (unsigned long) sockvec
;
413 return __socketcall(SYS_SOCKETPAIR
, args
);