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