socketcalls.c 9.5 KB

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