socketcalls.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  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. #define __FORCE_GLIBC
  7. #include <features.h>
  8. #include <errno.h>
  9. #include <syscall.h>
  10. #include <sys/socket.h>
  11. #ifdef __NR_socketcall
  12. extern int __socketcall(int call, unsigned long *args) attribute_hidden;
  13. /* Various socketcall numbers */
  14. #define SYS_SOCKET 1
  15. #define SYS_BIND 2
  16. #define SYS_CONNECT 3
  17. #define SYS_LISTEN 4
  18. #define SYS_ACCEPT 5
  19. #define SYS_GETSOCKNAME 6
  20. #define SYS_GETPEERNAME 7
  21. #define SYS_SOCKETPAIR 8
  22. #define SYS_SEND 9
  23. #define SYS_RECV 10
  24. #define SYS_SENDTO 11
  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. #endif
  32. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  33. #include <sysdep-cancel.h>
  34. #include <pthreadP.h>
  35. #else
  36. #define SINGLE_THREAD_P 1
  37. #endif
  38. #ifdef L_accept
  39. extern __typeof(accept) __libc_accept;
  40. #ifdef __NR_accept
  41. #define __NR___libc_accept __NR_accept
  42. _syscall3(int, __libc_accept, int, call, struct sockaddr *, addr, socklen_t *,addrlen)
  43. #elif defined(__NR_socketcall)
  44. int __libc_accept(int s, struct sockaddr *addr, socklen_t * addrlen)
  45. {
  46. unsigned long args[3];
  47. args[0] = s;
  48. args[1] = (unsigned long) addr;
  49. args[2] = (unsigned long) addrlen;
  50. if (SINGLE_THREAD_P)
  51. return __socketcall(SYS_ACCEPT, args);
  52. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  53. int oldtype = LIBC_CANCEL_ASYNC ();
  54. int result = __socketcall(SYS_ACCEPT, args);
  55. LIBC_CANCEL_RESET (oldtype);
  56. return result;
  57. #endif
  58. }
  59. #endif
  60. weak_alias(__libc_accept,accept)
  61. libc_hidden_weak(accept)
  62. #endif
  63. #ifdef L_bind
  64. #ifdef __NR_bind
  65. _syscall3(int, bind, int, sockfd, const struct sockaddr *, myaddr, socklen_t, addrlen)
  66. #elif defined(__NR_socketcall)
  67. int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen)
  68. {
  69. unsigned long args[3];
  70. args[0] = sockfd;
  71. args[1] = (unsigned long) myaddr;
  72. args[2] = addrlen;
  73. return __socketcall(SYS_BIND, args);
  74. }
  75. #endif
  76. libc_hidden_def(bind)
  77. #endif
  78. #ifdef L_connect
  79. extern __typeof(connect) __libc_connect;
  80. #ifdef __NR_connect
  81. #define __NR___libc_connect __NR_connect
  82. _syscall3(int, __libc_connect, int, sockfd, const struct sockaddr *, saddr, socklen_t, addrlen)
  83. #elif defined(__NR_socketcall)
  84. int __libc_connect(int sockfd, const struct sockaddr *saddr, socklen_t addrlen)
  85. {
  86. unsigned long args[3];
  87. args[0] = sockfd;
  88. args[1] = (unsigned long) saddr;
  89. args[2] = addrlen;
  90. if (SINGLE_THREAD_P)
  91. return __socketcall(SYS_CONNECT, args);
  92. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  93. int oldtype = LIBC_CANCEL_ASYNC ();
  94. int result = __socketcall(SYS_CONNECT, args);
  95. LIBC_CANCEL_RESET (oldtype);
  96. return result;
  97. #endif
  98. }
  99. #endif
  100. weak_alias(__libc_connect,connect)
  101. libc_hidden_weak(connect)
  102. #endif
  103. #ifdef L_getpeername
  104. #ifdef __NR_getpeername
  105. _syscall3(int, getpeername, int, sockfd, struct sockaddr *, addr, socklen_t *,paddrlen)
  106. #elif defined(__NR_socketcall)
  107. int getpeername(int sockfd, struct sockaddr *addr, socklen_t * paddrlen)
  108. {
  109. unsigned long args[3];
  110. args[0] = sockfd;
  111. args[1] = (unsigned long) addr;
  112. args[2] = (unsigned long) paddrlen;
  113. return __socketcall(SYS_GETPEERNAME, args);
  114. }
  115. #endif
  116. #endif
  117. #ifdef L_getsockname
  118. #ifdef __NR_getsockname
  119. _syscall3(int, getsockname, int, sockfd, struct sockaddr *, addr, socklen_t *,paddrlen)
  120. #elif defined(__NR_socketcall)
  121. int getsockname(int sockfd, struct sockaddr *addr, socklen_t * paddrlen)
  122. {
  123. unsigned long args[3];
  124. args[0] = sockfd;
  125. args[1] = (unsigned long) addr;
  126. args[2] = (unsigned long) paddrlen;
  127. return __socketcall(SYS_GETSOCKNAME, args);
  128. }
  129. #endif
  130. libc_hidden_def(getsockname)
  131. #endif
  132. #ifdef L_getsockopt
  133. #ifdef __NR_getsockopt
  134. _syscall5(int, getsockopt, int, fd, int, level, int, optname, __ptr_t, optval, socklen_t *, optlen)
  135. #elif defined(__NR_socketcall)
  136. int getsockopt(int fd, int level, int optname, __ptr_t optval,
  137. socklen_t * optlen)
  138. {
  139. unsigned long args[5];
  140. args[0] = fd;
  141. args[1] = level;
  142. args[2] = optname;
  143. args[3] = (unsigned long) optval;
  144. args[4] = (unsigned long) optlen;
  145. return (__socketcall(SYS_GETSOCKOPT, args));
  146. }
  147. #endif
  148. #endif
  149. #ifdef L_listen
  150. #ifdef __NR_listen
  151. _syscall2(int, listen, int, sockfd, int, backlog)
  152. #elif defined(__NR_socketcall)
  153. int listen(int sockfd, int backlog)
  154. {
  155. unsigned long args[2];
  156. args[0] = sockfd;
  157. args[1] = backlog;
  158. return __socketcall(SYS_LISTEN, args);
  159. }
  160. #endif
  161. libc_hidden_def(listen)
  162. #endif
  163. #ifdef L_recv
  164. extern __typeof(recv) __libc_recv;
  165. #ifdef __NR_recv
  166. #define __NR___libc_recv __NR_recv
  167. _syscall4(ssize_t, __libc_recv, int, sockfd, __ptr_t, buffer, size_t, len,
  168. int, flags)
  169. #elif defined(__NR_socketcall)
  170. /* recv, recvfrom added by bir7@leland.stanford.edu */
  171. ssize_t __libc_recv(int sockfd, __ptr_t buffer, size_t len, int flags)
  172. {
  173. unsigned long args[4];
  174. args[0] = sockfd;
  175. args[1] = (unsigned long) buffer;
  176. args[2] = len;
  177. args[3] = flags;
  178. if (SINGLE_THREAD_P)
  179. return (__socketcall(SYS_RECV, args));
  180. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  181. int oldtype = LIBC_CANCEL_ASYNC ();
  182. int result = __socketcall(SYS_RECV, args);
  183. LIBC_CANCEL_RESET (oldtype);
  184. return result;
  185. #endif
  186. }
  187. #elif defined(__NR_recvfrom)
  188. ssize_t __libc_recv(int sockfd, __ptr_t buffer, size_t len, int flags)
  189. {
  190. return (recvfrom(sockfd, buffer, len, flags, NULL, NULL));
  191. }
  192. #endif
  193. weak_alias(__libc_recv,recv)
  194. libc_hidden_weak(recv)
  195. #endif
  196. #ifdef L_recvfrom
  197. extern __typeof(recvfrom) __libc_recvfrom;
  198. #ifdef __NR_recvfrom
  199. #define __NR___libc_recvfrom __NR_recvfrom
  200. _syscall6(ssize_t, __libc_recvfrom, int, sockfd, __ptr_t, buffer, size_t, len,
  201. int, flags, struct sockaddr *, to, socklen_t *, tolen)
  202. #elif defined(__NR_socketcall)
  203. /* recv, recvfrom added by bir7@leland.stanford.edu */
  204. ssize_t __libc_recvfrom(int sockfd, __ptr_t buffer, size_t len, int flags,
  205. struct sockaddr *to, socklen_t * tolen)
  206. {
  207. unsigned long args[6];
  208. args[0] = sockfd;
  209. args[1] = (unsigned long) buffer;
  210. args[2] = len;
  211. args[3] = flags;
  212. args[4] = (unsigned long) to;
  213. args[5] = (unsigned long) tolen;
  214. if (SINGLE_THREAD_P)
  215. return (__socketcall(SYS_RECVFROM, args));
  216. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  217. int oldtype = LIBC_CANCEL_ASYNC ();
  218. int result = __socketcall(SYS_RECVFROM, args);
  219. LIBC_CANCEL_RESET (oldtype);
  220. return result;
  221. #endif
  222. }
  223. #endif
  224. weak_alias(__libc_recvfrom,recvfrom)
  225. libc_hidden_weak(recvfrom)
  226. #endif
  227. #ifdef L_recvmsg
  228. extern __typeof(recvmsg) __libc_recvmsg;
  229. #ifdef __NR_recvmsg
  230. #define __NR___libc_recvmsg __NR_recvmsg
  231. _syscall3(ssize_t, __libc_recvmsg, int, sockfd, struct msghdr *, msg, int, flags)
  232. #elif defined(__NR_socketcall)
  233. ssize_t __libc_recvmsg(int sockfd, struct msghdr *msg, int flags)
  234. {
  235. unsigned long args[3];
  236. args[0] = sockfd;
  237. args[1] = (unsigned long) msg;
  238. args[2] = flags;
  239. if (SINGLE_THREAD_P)
  240. return (__socketcall(SYS_RECVMSG, args));
  241. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  242. int oldtype = LIBC_CANCEL_ASYNC ();
  243. int result = __socketcall(SYS_RECVMSG, args);
  244. LIBC_CANCEL_RESET (oldtype);
  245. return result;
  246. #endif
  247. }
  248. #endif
  249. weak_alias(__libc_recvmsg,recvmsg)
  250. libc_hidden_weak(recvmsg)
  251. #endif
  252. #ifdef L_send
  253. extern __typeof(send) __libc_send;
  254. #ifdef __NR_send
  255. #define __NR___libc_send __NR_send
  256. _syscall4(ssize_t, __libc_send, int, sockfd, const void *, buffer, size_t, len, int, flags)
  257. #elif defined(__NR_socketcall)
  258. /* send, sendto added by bir7@leland.stanford.edu */
  259. ssize_t __libc_send(int sockfd, const void *buffer, size_t len, int flags)
  260. {
  261. unsigned long args[4];
  262. args[0] = sockfd;
  263. args[1] = (unsigned long) buffer;
  264. args[2] = len;
  265. args[3] = flags;
  266. if (SINGLE_THREAD_P)
  267. return (__socketcall(SYS_SEND, args));
  268. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  269. int oldtype = LIBC_CANCEL_ASYNC ();
  270. int result = __socketcall(SYS_SEND, args);
  271. LIBC_CANCEL_RESET (oldtype);
  272. return result;
  273. #endif
  274. }
  275. #elif defined(__NR_sendto)
  276. ssize_t __libc_send(int sockfd, const void *buffer, size_t len, int flags)
  277. {
  278. return (sendto(sockfd, buffer, len, flags, NULL, 0));
  279. }
  280. #endif
  281. weak_alias(__libc_send,send)
  282. libc_hidden_weak(send)
  283. #endif
  284. #ifdef L_sendmsg
  285. extern __typeof(sendmsg) __libc_sendmsg;
  286. #ifdef __NR_sendmsg
  287. #define __NR___libc_sendmsg __NR_sendmsg
  288. _syscall3(ssize_t, __libc_sendmsg, int, sockfd, const struct msghdr *, msg, int, flags)
  289. #elif defined(__NR_socketcall)
  290. ssize_t __libc_sendmsg(int sockfd, const struct msghdr *msg, int flags)
  291. {
  292. unsigned long args[3];
  293. args[0] = sockfd;
  294. args[1] = (unsigned long) msg;
  295. args[2] = flags;
  296. if (SINGLE_THREAD_P)
  297. return (__socketcall(SYS_SENDMSG, args));
  298. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  299. int oldtype = LIBC_CANCEL_ASYNC ();
  300. int result = __socketcall(SYS_SENDMSG, args);
  301. LIBC_CANCEL_RESET (oldtype);
  302. return result;
  303. #endif
  304. }
  305. #endif
  306. weak_alias(__libc_sendmsg,sendmsg)
  307. libc_hidden_weak(sendmsg)
  308. #endif
  309. #ifdef L_sendto
  310. extern __typeof(sendto) __libc_sendto;
  311. #ifdef __NR_sendto
  312. #define __NR___libc_sendto __NR_sendto
  313. _syscall6(ssize_t, __libc_sendto, int, sockfd, const void *, buffer,
  314. size_t, len, int, flags, const struct sockaddr *, to, socklen_t, tolen)
  315. #elif defined(__NR_socketcall)
  316. /* send, sendto added by bir7@leland.stanford.edu */
  317. ssize_t __libc_sendto(int sockfd, const void *buffer, size_t len, int flags,
  318. const struct sockaddr *to, socklen_t tolen)
  319. {
  320. unsigned long args[6];
  321. args[0] = sockfd;
  322. args[1] = (unsigned long) buffer;
  323. args[2] = len;
  324. args[3] = flags;
  325. args[4] = (unsigned long) to;
  326. args[5] = tolen;
  327. if (SINGLE_THREAD_P)
  328. return (__socketcall(SYS_SENDTO, args));
  329. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  330. int oldtype = LIBC_CANCEL_ASYNC ();
  331. int result = __socketcall(SYS_SENDTO, args);
  332. LIBC_CANCEL_RESET (oldtype);
  333. return result;
  334. #endif
  335. }
  336. #endif
  337. weak_alias(__libc_sendto,sendto)
  338. libc_hidden_weak(sendto)
  339. #endif
  340. #ifdef L_setsockopt
  341. #ifdef __NR_setsockopt
  342. _syscall5(int, setsockopt, int, fd, int, level, int, optname, const void *, optval, socklen_t, optlen)
  343. #elif defined(__NR_socketcall)
  344. /* [sg]etsockoptions by bir7@leland.stanford.edu */
  345. int setsockopt(int fd, int level, int optname, const void *optval,
  346. socklen_t optlen)
  347. {
  348. unsigned long args[5];
  349. args[0] = fd;
  350. args[1] = level;
  351. args[2] = optname;
  352. args[3] = (unsigned long) optval;
  353. args[4] = optlen;
  354. return (__socketcall(SYS_SETSOCKOPT, args));
  355. }
  356. #endif
  357. libc_hidden_def(setsockopt)
  358. #endif
  359. #ifdef L_shutdown
  360. #ifdef __NR_shutdown
  361. _syscall2(int, shutdown, int, sockfd, int, how)
  362. #elif defined(__NR_socketcall)
  363. /* shutdown by bir7@leland.stanford.edu */
  364. int shutdown(int sockfd, int how)
  365. {
  366. unsigned long args[2];
  367. args[0] = sockfd;
  368. args[1] = how;
  369. return (__socketcall(SYS_SHUTDOWN, args));
  370. }
  371. #endif
  372. #endif
  373. #ifdef L_socket
  374. #ifdef __NR_socket
  375. _syscall3(int, socket, int, family, int, type, int, protocol)
  376. #elif defined(__NR_socketcall)
  377. int socket(int family, int type, int protocol)
  378. {
  379. unsigned long args[3];
  380. args[0] = family;
  381. args[1] = type;
  382. args[2] = (unsigned long) protocol;
  383. return __socketcall(SYS_SOCKET, args);
  384. }
  385. #endif
  386. libc_hidden_def(socket)
  387. #endif
  388. #ifdef L_socketpair
  389. #ifdef __NR_socketpair
  390. _syscall4(int, socketpair, int, family, int, type, int, protocol, int *, sockvec)
  391. #elif defined(__NR_socketcall)
  392. int socketpair(int family, int type, int protocol, int sockvec[2])
  393. {
  394. unsigned long args[4];
  395. args[0] = family;
  396. args[1] = type;
  397. args[2] = protocol;
  398. args[3] = (unsigned long) sockvec;
  399. return __socketcall(SYS_SOCKETPAIR, args);
  400. }
  401. #endif
  402. #endif