blob: aae7e93bfeb1314e2ef1d1149dd41e6d6d141474 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
3 *
4 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
5 */
6
7#define __FORCE_GLIBC
8#include <features.h>
9#include <errno.h>
10#include <syscall.h>
11#include <sys/socket.h>
12
13#ifdef __NR_socketcall
14extern int __socketcall(int call, unsigned long *args) attribute_hidden;
15
16/* Various socketcall numbers */
17#define SYS_SOCKET 1
18#define SYS_BIND 2
19#define SYS_CONNECT 3
20#define SYS_LISTEN 4
21#define SYS_ACCEPT 5
22#define SYS_GETSOCKNAME 6
23#define SYS_GETPEERNAME 7
24#define SYS_SOCKETPAIR 8
25#define SYS_SEND 9
26#define SYS_RECV 10
27#define SYS_SENDTO 11
28#define SYS_RECVFROM 12
29#define SYS_SHUTDOWN 13
30#define SYS_SETSOCKOPT 14
31#define SYS_GETSOCKOPT 15
32#define SYS_SENDMSG 16
33#define SYS_RECVMSG 17
34#define SYS_ACCEPT4 18
35#endif
36
37#ifdef __UCLIBC_HAS_THREADS_NATIVE__
38#include <sysdep-cancel.h>
39#include <pthreadP.h>
40#else
41#define SINGLE_THREAD_P 1
42#endif
43
44#ifdef L_accept
45extern __typeof(accept) __libc_accept;
46#ifdef __NR_accept
47#define __NR___sys_accept __NR_accept
48static
49_syscall3(int, __sys_accept, int, call, struct sockaddr *, addr, socklen_t *,addrlen)
50int __libc_accept(int s, struct sockaddr *addr, socklen_t * addrlen)
51{
52 if (SINGLE_THREAD_P)
53 return __sys_accept(s, addr, addrlen);
54
55#ifdef __UCLIBC_HAS_THREADS_NATIVE__
56 int oldtype = LIBC_CANCEL_ASYNC ();
57 int result = __sys_accept(s, addr, addrlen);
58 LIBC_CANCEL_RESET (oldtype);
59 return result;
60#endif
61}
62#elif defined(__NR_socketcall)
63int __libc_accept(int s, struct sockaddr *addr, socklen_t * addrlen)
64{
65 unsigned long args[3];
66
67 args[0] = s;
68 args[1] = (unsigned long) addr;
69 args[2] = (unsigned long) addrlen;
70
71 if (SINGLE_THREAD_P)
72 return __socketcall(SYS_ACCEPT, args);
73
74#ifdef __UCLIBC_HAS_THREADS_NATIVE__
75 int oldtype = LIBC_CANCEL_ASYNC ();
76 int result = __socketcall(SYS_ACCEPT, args);
77 LIBC_CANCEL_RESET (oldtype);
78 return result;
79#endif
80}
81#endif
82weak_alias(__libc_accept,accept)
83libc_hidden_weak(accept)
84#endif
85
86#ifdef L_accept4
87#ifdef __NR_accept4
88# define __NR___sys_accept4 __NR_accept4
89static _syscall4(int, __sys_accept4, int, fd, struct sockaddr *, addr, socklen_t *, addrlen, int, flags)
90int accept4(int fd, struct sockaddr *addr, socklen_t * addrlen, int flags)
91{
92 if (SINGLE_THREAD_P)
93 return __sys_accept4(fd, addr, addrlen, flags);
94#ifdef __UCLIBC_HAS_THREADS_NATIVE__
95 else {
96 int oldtype = LIBC_CANCEL_ASYNC ();
97 int result = __sys_accept4(fd, addr, addrlen, flags);
98 LIBC_CANCEL_RESET (oldtype);
99 return result;
100 }
101#endif
102}
103#elif defined(__NR_socketcall)
104int accept4(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags)
105{
106 unsigned long args[4];
107
108 args[0] = fd;
109 args[1] = (unsigned long) addr;
110 args[2] = (unsigned long) addrlen;
111 args[3] = flags;
112 if (SINGLE_THREAD_P)
113 return __socketcall(SYS_ACCEPT4, args);
114#ifdef __UCLIBC_HAS_THREADS_NATIVE__
115 else {
116 int oldtype = LIBC_CANCEL_ASYNC ();
117 int result = __socketcall(SYS_ACCEPT4, args);
118 LIBC_CANCEL_RESET (oldtype);
119 return result;
120 }
121#endif
122}
123#endif
124#endif
125
126#ifdef L_bind
127#ifdef __NR_bind
128_syscall3(int, bind, int, sockfd, const struct sockaddr *, myaddr, socklen_t, addrlen)
129#elif defined(__NR_socketcall)
130int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen)
131{
132 unsigned long args[3];
133
134 args[0] = sockfd;
135 args[1] = (unsigned long) myaddr;
136 args[2] = addrlen;
137 return __socketcall(SYS_BIND, args);
138}
139#endif
140libc_hidden_def(bind)
141#endif
142
143#ifdef L_connect
144extern __typeof(connect) __libc_connect;
145#ifdef __NR_connect
146#define __NR___sys_connect __NR_connect
147static
148_syscall3(int, __sys_connect, int, sockfd, const struct sockaddr *, saddr, socklen_t, addrlen)
149int __libc_connect(int sockfd, const struct sockaddr *saddr, socklen_t addrlen)
150{
151 if (SINGLE_THREAD_P)
152 return __sys_connect(sockfd, saddr, addrlen);
153
154#ifdef __UCLIBC_HAS_THREADS_NATIVE__
155 int oldtype = LIBC_CANCEL_ASYNC ();
156 int result = __sys_connect(sockfd, saddr, addrlen);
157 LIBC_CANCEL_RESET (oldtype);
158 return result;
159#endif
160}
161#elif defined(__NR_socketcall)
162int __libc_connect(int sockfd, const struct sockaddr *saddr, socklen_t addrlen)
163{
164 unsigned long args[3];
165
166 args[0] = sockfd;
167 args[1] = (unsigned long) saddr;
168 args[2] = addrlen;
169
170 if (SINGLE_THREAD_P)
171 return __socketcall(SYS_CONNECT, args);
172
173#ifdef __UCLIBC_HAS_THREADS_NATIVE__
174 int oldtype = LIBC_CANCEL_ASYNC ();
175 int result = __socketcall(SYS_CONNECT, args);
176 LIBC_CANCEL_RESET (oldtype);
177 return result;
178#endif
179}
180#endif
181weak_alias(__libc_connect,connect)
182libc_hidden_weak(connect)
183#endif
184
185#ifdef L_getpeername
186#ifdef __NR_getpeername
187_syscall3(int, getpeername, int, sockfd, struct sockaddr *, addr, socklen_t *,paddrlen)
188#elif defined(__NR_socketcall)
189int getpeername(int sockfd, struct sockaddr *addr, socklen_t * paddrlen)
190{
191 unsigned long args[3];
192
193 args[0] = sockfd;
194 args[1] = (unsigned long) addr;
195 args[2] = (unsigned long) paddrlen;
196 return __socketcall(SYS_GETPEERNAME, args);
197}
198#endif
199#endif
200
201#ifdef L_getsockname
202#ifdef __NR_getsockname
203_syscall3(int, getsockname, int, sockfd, struct sockaddr *, addr, socklen_t *,paddrlen)
204#elif defined(__NR_socketcall)
205int getsockname(int sockfd, struct sockaddr *addr, socklen_t * paddrlen)
206{
207 unsigned long args[3];
208
209 args[0] = sockfd;
210 args[1] = (unsigned long) addr;
211 args[2] = (unsigned long) paddrlen;
212 return __socketcall(SYS_GETSOCKNAME, args);
213}
214#endif
215libc_hidden_def(getsockname)
216#endif
217
218#ifdef L_getsockopt
219#ifdef __NR_getsockopt
220_syscall5(int, getsockopt, int, fd, int, level, int, optname, __ptr_t, optval, socklen_t *, optlen)
221#elif defined(__NR_socketcall)
222int getsockopt(int fd, int level, int optname, __ptr_t optval,
223 socklen_t * optlen)
224{
225 unsigned long args[5];
226
227 args[0] = fd;
228 args[1] = level;
229 args[2] = optname;
230 args[3] = (unsigned long) optval;
231 args[4] = (unsigned long) optlen;
232 return (__socketcall(SYS_GETSOCKOPT, args));
233}
234#endif
235#endif
236
237#ifdef L_listen
238#ifdef __NR_listen
239_syscall2(int, listen, int, sockfd, int, backlog)
240#elif defined(__NR_socketcall)
241int listen(int sockfd, int backlog)
242{
243 unsigned long args[2];
244
245 args[0] = sockfd;
246 args[1] = backlog;
247 return __socketcall(SYS_LISTEN, args);
248}
249#endif
250libc_hidden_def(listen)
251#endif
252
253#ifdef L_recv
254extern __typeof(recv) __libc_recv;
255#ifdef __NR_recv
256#define __NR___sys_recv __NR_recv
257static
258_syscall4(ssize_t, __sys_recv, int, sockfd, __ptr_t, buffer, size_t, len,
259 int, flags)
260ssize_t __libc_recv(int sockfd, __ptr_t buffer, size_t len, int flags)
261{
262 if (SINGLE_THREAD_P)
263 return __sys_recv(sockfd, buffer, len, flags);
264
265#ifdef __UCLIBC_HAS_THREADS_NATIVE__
266 int oldtype = LIBC_CANCEL_ASYNC ();
267 int result = __sys_recv(sockfd, buffer, len, flags);
268 LIBC_CANCEL_RESET (oldtype);
269 return result;
270#endif
271}
272#elif defined(__NR_socketcall)
273/* recv, recvfrom added by bir7@leland.stanford.edu */
274ssize_t __libc_recv(int sockfd, __ptr_t buffer, size_t len, int flags)
275{
276 unsigned long args[4];
277
278 args[0] = sockfd;
279 args[1] = (unsigned long) buffer;
280 args[2] = len;
281 args[3] = flags;
282
283 if (SINGLE_THREAD_P)
284 return (__socketcall(SYS_RECV, args));
285
286#ifdef __UCLIBC_HAS_THREADS_NATIVE__
287 int oldtype = LIBC_CANCEL_ASYNC ();
288 int result = __socketcall(SYS_RECV, args);
289 LIBC_CANCEL_RESET (oldtype);
290 return result;
291#endif
292}
293#elif defined(__NR_recvfrom)
294ssize_t __libc_recv(int sockfd, __ptr_t buffer, size_t len, int flags)
295{
296 return (recvfrom(sockfd, buffer, len, flags, NULL, NULL));
297}
298#endif
299weak_alias(__libc_recv,recv)
300libc_hidden_weak(recv)
301#endif
302
303#ifdef L_recvfrom
304extern __typeof(recvfrom) __libc_recvfrom;
305#ifdef __NR_recvfrom
306#define __NR___sys_recvfrom __NR_recvfrom
307static
308_syscall6(ssize_t, __sys_recvfrom, int, sockfd, __ptr_t, buffer, size_t, len,
309 int, flags, struct sockaddr *, to, socklen_t *, tolen)
310ssize_t __libc_recvfrom(int sockfd, __ptr_t buffer, size_t len, int flags,
311 struct sockaddr *to, socklen_t * tolen)
312{
313 if (SINGLE_THREAD_P)
314 return __sys_recvfrom(sockfd, buffer, len, flags, to, tolen);
315
316#ifdef __UCLIBC_HAS_THREADS_NATIVE__
317 int oldtype = LIBC_CANCEL_ASYNC ();
318 int result = __sys_recvfrom(sockfd, buffer, len, flags, to, tolen);
319 LIBC_CANCEL_RESET (oldtype);
320 return result;
321#endif
322}
323#elif defined(__NR_socketcall)
324/* recv, recvfrom added by bir7@leland.stanford.edu */
325ssize_t __libc_recvfrom(int sockfd, __ptr_t buffer, size_t len, int flags,
326 struct sockaddr *to, socklen_t * tolen)
327{
328 unsigned long args[6];
329
330 args[0] = sockfd;
331 args[1] = (unsigned long) buffer;
332 args[2] = len;
333 args[3] = flags;
334 args[4] = (unsigned long) to;
335 args[5] = (unsigned long) tolen;
336
337 if (SINGLE_THREAD_P)
338 return (__socketcall(SYS_RECVFROM, args));
339
340#ifdef __UCLIBC_HAS_THREADS_NATIVE__
341 int oldtype = LIBC_CANCEL_ASYNC ();
342 int result = __socketcall(SYS_RECVFROM, args);
343 LIBC_CANCEL_RESET (oldtype);
344 return result;
345#endif
346}
347#endif
348weak_alias(__libc_recvfrom,recvfrom)
349libc_hidden_weak(recvfrom)
350#endif
351
352#ifdef L_recvmsg
353extern __typeof(recvmsg) __libc_recvmsg;
354#ifdef __NR_recvmsg
355#define __NR___sys_recvmsg __NR_recvmsg
356static
357_syscall3(ssize_t, __sys_recvmsg, int, sockfd, struct msghdr *, msg, int, flags)
358ssize_t __libc_recvmsg(int sockfd, struct msghdr *msg, int flags)
359{
360 if (SINGLE_THREAD_P)
361 return __sys_recvmsg(sockfd, msg, flags);
362
363#ifdef __UCLIBC_HAS_THREADS_NATIVE__
364 int oldtype = LIBC_CANCEL_ASYNC ();
365 int result = __sys_recvmsg(sockfd, msg, flags);
366 LIBC_CANCEL_RESET (oldtype);
367 return result;
368#endif
369}
370#elif defined(__NR_socketcall)
371ssize_t __libc_recvmsg(int sockfd, struct msghdr *msg, int flags)
372{
373 unsigned long args[3];
374
375 args[0] = sockfd;
376 args[1] = (unsigned long) msg;
377 args[2] = flags;
378
379 if (SINGLE_THREAD_P)
380 return (__socketcall(SYS_RECVMSG, args));
381
382#ifdef __UCLIBC_HAS_THREADS_NATIVE__
383 int oldtype = LIBC_CANCEL_ASYNC ();
384 int result = __socketcall(SYS_RECVMSG, args);
385 LIBC_CANCEL_RESET (oldtype);
386 return result;
387#endif
388}
389#endif
390weak_alias(__libc_recvmsg,recvmsg)
391libc_hidden_weak(recvmsg)
392#endif
393
394#ifdef L_send
395extern __typeof(send) __libc_send;
396#ifdef __NR_send
397#define __NR___sys_send __NR_send
398static
399_syscall4(ssize_t, __sys_send, int, sockfd, const void *, buffer, size_t, len, int, flags)
400ssize_t __libc_send(int sockfd, const void *buffer, size_t len, int flags)
401{
402 if (SINGLE_THREAD_P)
403 return __sys_send(sockfd, buffer, len, flags);
404
405#ifdef __UCLIBC_HAS_THREADS_NATIVE__
406 int oldtype = LIBC_CANCEL_ASYNC ();
407 int result = __sys_send(sockfd, buffer, len, flags);
408 LIBC_CANCEL_RESET (oldtype);
409 return result;
410#endif
411}
412#elif defined(__NR_socketcall)
413/* send, sendto added by bir7@leland.stanford.edu */
414ssize_t __libc_send(int sockfd, const void *buffer, size_t len, int flags)
415{
416 unsigned long args[4];
417
418 args[0] = sockfd;
419 args[1] = (unsigned long) buffer;
420 args[2] = len;
421 args[3] = flags;
422
423 if (SINGLE_THREAD_P)
424 return (__socketcall(SYS_SEND, args));
425
426#ifdef __UCLIBC_HAS_THREADS_NATIVE__
427 int oldtype = LIBC_CANCEL_ASYNC ();
428 int result = __socketcall(SYS_SEND, args);
429 LIBC_CANCEL_RESET (oldtype);
430 return result;
431#endif
432}
433
434#elif defined(__NR_sendto)
435ssize_t __libc_send(int sockfd, const void *buffer, size_t len, int flags)
436{
437 return (sendto(sockfd, buffer, len, flags, NULL, 0));
438}
439#endif
440weak_alias(__libc_send,send)
441libc_hidden_weak(send)
442#endif
443
444#ifdef L_sendmsg
445extern __typeof(sendmsg) __libc_sendmsg;
446#ifdef __NR_sendmsg
447#define __NR___sys_sendmsg __NR_sendmsg
448static
449_syscall3(ssize_t, __sys_sendmsg, int, sockfd, const struct msghdr *, msg, int, flags)
450ssize_t __libc_sendmsg(int sockfd, const struct msghdr *msg, int flags)
451{
452 if (SINGLE_THREAD_P)
453 return __sys_sendmsg(sockfd, msg, flags);
454
455#ifdef __UCLIBC_HAS_THREADS_NATIVE__
456 int oldtype = LIBC_CANCEL_ASYNC ();
457 int result = __sys_sendmsg(sockfd, msg, flags);
458 LIBC_CANCEL_RESET (oldtype);
459 return result;
460#endif
461}
462#elif defined(__NR_socketcall)
463ssize_t __libc_sendmsg(int sockfd, const struct msghdr *msg, int flags)
464{
465 unsigned long args[3];
466
467 args[0] = sockfd;
468 args[1] = (unsigned long) msg;
469 args[2] = flags;
470
471 if (SINGLE_THREAD_P)
472 return (__socketcall(SYS_SENDMSG, args));
473
474#ifdef __UCLIBC_HAS_THREADS_NATIVE__
475 int oldtype = LIBC_CANCEL_ASYNC ();
476 int result = __socketcall(SYS_SENDMSG, args);
477 LIBC_CANCEL_RESET (oldtype);
478 return result;
479#endif
480}
481#endif
482weak_alias(__libc_sendmsg,sendmsg)
483libc_hidden_weak(sendmsg)
484#endif
485
486#ifdef L_sendto
487extern __typeof(sendto) __libc_sendto;
488#ifdef __NR_sendto
489#define __NR___sys_sendto __NR_sendto
490static
491_syscall6(ssize_t, __sys_sendto, int, sockfd, const void *, buffer,
492 size_t, len, int, flags, const struct sockaddr *, to, socklen_t, tolen)
493ssize_t __libc_sendto(int sockfd, const void *buffer, size_t len, int flags,const struct sockaddr *to, socklen_t tolen)
494{
495 if (SINGLE_THREAD_P)
496 return __sys_sendto(sockfd, buffer, len, flags, to, tolen);
497
498#ifdef __UCLIBC_HAS_THREADS_NATIVE__
499 int oldtype = LIBC_CANCEL_ASYNC ();
500 int result = __sys_sendto(sockfd, buffer, len, flags, to, tolen);
501 LIBC_CANCEL_RESET (oldtype);
502 return result;
503#endif
504}
505#elif defined(__NR_socketcall)
506/* send, sendto added by bir7@leland.stanford.edu */
507ssize_t __libc_sendto(int sockfd, const void *buffer, size_t len, int flags,
508 const struct sockaddr *to, socklen_t tolen)
509{
510 unsigned long args[6];
511
512 args[0] = sockfd;
513 args[1] = (unsigned long) buffer;
514 args[2] = len;
515 args[3] = flags;
516 args[4] = (unsigned long) to;
517 args[5] = tolen;
518
519 if (SINGLE_THREAD_P)
520 return (__socketcall(SYS_SENDTO, args));
521
522#ifdef __UCLIBC_HAS_THREADS_NATIVE__
523 int oldtype = LIBC_CANCEL_ASYNC ();
524 int result = __socketcall(SYS_SENDTO, args);
525 LIBC_CANCEL_RESET (oldtype);
526 return result;
527#endif
528}
529#endif
530weak_alias(__libc_sendto,sendto)
531libc_hidden_weak(sendto)
532#endif
533
534#ifdef L_setsockopt
535#ifdef __NR_setsockopt
536_syscall5(int, setsockopt, int, fd, int, level, int, optname, const void *, optval, socklen_t, optlen)
537#elif defined(__NR_socketcall)
538/* [sg]etsockoptions by bir7@leland.stanford.edu */
539int setsockopt(int fd, int level, int optname, const void *optval,
540 socklen_t optlen)
541{
542 unsigned long args[5];
543
544 args[0] = fd;
545 args[1] = level;
546 args[2] = optname;
547 args[3] = (unsigned long) optval;
548 args[4] = optlen;
549 return (__socketcall(SYS_SETSOCKOPT, args));
550}
551#endif
552libc_hidden_def(setsockopt)
553#endif
554
555#ifdef L_shutdown
556#ifdef __NR_shutdown
557_syscall2(int, shutdown, int, sockfd, int, how)
558#elif defined(__NR_socketcall)
559/* shutdown by bir7@leland.stanford.edu */
560int shutdown(int sockfd, int how)
561{
562 unsigned long args[2];
563
564 args[0] = sockfd;
565 args[1] = how;
566 return (__socketcall(SYS_SHUTDOWN, args));
567}
568#endif
569#endif
570
571#ifdef L_socket
572#ifdef __NR_socket
573_syscall3(int, socket, int, family, int, type, int, protocol)
574#elif defined(__NR_socketcall)
575int socket(int family, int type, int protocol)
576{
577 unsigned long args[3];
578
579 args[0] = family;
580 args[1] = type;
581 args[2] = (unsigned long) protocol;
582 return __socketcall(SYS_SOCKET, args);
583}
584#endif
585libc_hidden_def(socket)
586#endif
587
588#ifdef L_socketpair
589#ifdef __NR_socketpair
590_syscall4(int, socketpair, int, family, int, type, int, protocol, int *, sockvec)
591#elif defined(__NR_socketcall)
592int socketpair(int family, int type, int protocol, int sockvec[2])
593{
594 unsigned long args[4];
595
596 args[0] = family;
597 args[1] = type;
598 args[2] = protocol;
599 args[3] = (unsigned long) sockvec;
600 return __socketcall(SYS_SOCKETPAIR, args);
601}
602#endif
603#endif
604