socketcalls.c 9.5 KB

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