socketcalls.c 10.0 KB

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