socketcalls.c 11 KB

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