socketcalls.c 11 KB

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