socketcalls.c 9.9 KB

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