stream.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * SUCS NET3:
  3. *
  4. * Generic stream handling routines. These are generic for most
  5. * protocols. Even IP. Tonight 8-).
  6. * This is used because TCP, LLC (others too) layer all have mostly
  7. * identical sendmsg() and recvmsg() code.
  8. * So we (will) share it here.
  9. *
  10. * Authors: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  11. * (from old tcp.c code)
  12. * Alan Cox <alan@lxorguk.ukuu.org.uk> (Borrowed comments 8-))
  13. */
  14. #include <linux/module.h>
  15. #include <linux/net.h>
  16. #include <linux/signal.h>
  17. #include <linux/tcp.h>
  18. #include <linux/wait.h>
  19. #include <net/sock.h>
  20. /**
  21. * sk_stream_write_space - stream socket write_space callback.
  22. * @sk: socket
  23. *
  24. * FIXME: write proper description
  25. */
  26. void sk_stream_write_space(struct sock *sk)
  27. {
  28. struct socket *sock = sk->sk_socket;
  29. struct socket_wq *wq;
  30. if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) && sock) {
  31. clear_bit(SOCK_NOSPACE, &sock->flags);
  32. rcu_read_lock();
  33. wq = rcu_dereference(sk->sk_wq);
  34. if (wq_has_sleeper(wq))
  35. wake_up_interruptible_poll(&wq->wait, POLLOUT |
  36. POLLWRNORM | POLLWRBAND);
  37. if (wq && wq->fasync_list && !(sk->sk_shutdown & SEND_SHUTDOWN))
  38. sock_wake_async(sock, SOCK_WAKE_SPACE, POLL_OUT);
  39. rcu_read_unlock();
  40. }
  41. }
  42. EXPORT_SYMBOL(sk_stream_write_space);
  43. /**
  44. * sk_stream_wait_connect - Wait for a socket to get into the connected state
  45. * @sk: sock to wait on
  46. * @timeo_p: for how long to wait
  47. *
  48. * Must be called with the socket locked.
  49. */
  50. int sk_stream_wait_connect(struct sock *sk, long *timeo_p)
  51. {
  52. struct task_struct *tsk = current;
  53. DEFINE_WAIT(wait);
  54. int done;
  55. do {
  56. int err = sock_error(sk);
  57. if (err)
  58. return err;
  59. if ((1 << sk->sk_state) & ~(TCPF_SYN_SENT | TCPF_SYN_RECV))
  60. return -EPIPE;
  61. if (!*timeo_p)
  62. return -EAGAIN;
  63. if (signal_pending(tsk))
  64. return sock_intr_errno(*timeo_p);
  65. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  66. sk->sk_write_pending++;
  67. done = sk_wait_event(sk, timeo_p,
  68. !sk->sk_err &&
  69. !((1 << sk->sk_state) &
  70. ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)));
  71. finish_wait(sk_sleep(sk), &wait);
  72. sk->sk_write_pending--;
  73. } while (!done);
  74. return 0;
  75. }
  76. EXPORT_SYMBOL(sk_stream_wait_connect);
  77. /**
  78. * sk_stream_closing - Return 1 if we still have things to send in our buffers.
  79. * @sk: socket to verify
  80. */
  81. static inline int sk_stream_closing(struct sock *sk)
  82. {
  83. return (1 << sk->sk_state) &
  84. (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK);
  85. }
  86. void sk_stream_wait_close(struct sock *sk, long timeout)
  87. {
  88. if (timeout) {
  89. DEFINE_WAIT(wait);
  90. do {
  91. prepare_to_wait(sk_sleep(sk), &wait,
  92. TASK_INTERRUPTIBLE);
  93. if (sk_wait_event(sk, &timeout, !sk_stream_closing(sk)))
  94. break;
  95. } while (!signal_pending(current) && timeout);
  96. finish_wait(sk_sleep(sk), &wait);
  97. }
  98. }
  99. EXPORT_SYMBOL(sk_stream_wait_close);
  100. /**
  101. * sk_stream_wait_memory - Wait for more memory for a socket
  102. * @sk: socket to wait for memory
  103. * @timeo_p: for how long
  104. */
  105. int sk_stream_wait_memory(struct sock *sk, long *timeo_p)
  106. {
  107. int err = 0;
  108. long vm_wait = 0;
  109. long current_timeo = *timeo_p;
  110. DEFINE_WAIT(wait);
  111. if (sk_stream_memory_free(sk))
  112. current_timeo = vm_wait = (net_random() % (HZ / 5)) + 2;
  113. while (1) {
  114. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  115. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  116. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
  117. goto do_error;
  118. if (!*timeo_p)
  119. goto do_eagain;
  120. if (signal_pending(current))
  121. goto do_interrupted;
  122. clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  123. if (sk_stream_memory_free(sk) && !vm_wait)
  124. break;
  125. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  126. sk->sk_write_pending++;
  127. sk_wait_event(sk, &current_timeo, sk->sk_err ||
  128. (sk->sk_shutdown & SEND_SHUTDOWN) ||
  129. (sk_stream_memory_free(sk) &&
  130. !vm_wait));
  131. sk->sk_write_pending--;
  132. if (vm_wait) {
  133. vm_wait -= current_timeo;
  134. current_timeo = *timeo_p;
  135. if (current_timeo != MAX_SCHEDULE_TIMEOUT &&
  136. (current_timeo -= vm_wait) < 0)
  137. current_timeo = 0;
  138. vm_wait = 0;
  139. }
  140. *timeo_p = current_timeo;
  141. }
  142. out:
  143. finish_wait(sk_sleep(sk), &wait);
  144. return err;
  145. do_error:
  146. err = -EPIPE;
  147. goto out;
  148. do_eagain:
  149. /* Make sure that whenever EAGAIN is returned, EPOLLOUT event can
  150. * be generated later.
  151. * When TCP receives ACK packets that make room, tcp_check_space()
  152. * only calls tcp_new_space() if SOCK_NOSPACE is set.
  153. */
  154. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  155. err = -EAGAIN;
  156. goto out;
  157. do_interrupted:
  158. err = sock_intr_errno(*timeo_p);
  159. goto out;
  160. }
  161. EXPORT_SYMBOL(sk_stream_wait_memory);
  162. int sk_stream_error(struct sock *sk, int flags, int err)
  163. {
  164. if (err == -EPIPE)
  165. err = sock_error(sk) ? : -EPIPE;
  166. if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
  167. send_sig(SIGPIPE, current, 0);
  168. return err;
  169. }
  170. EXPORT_SYMBOL(sk_stream_error);
  171. void sk_stream_kill_queues(struct sock *sk)
  172. {
  173. /* First the read buffer. */
  174. __skb_queue_purge(&sk->sk_receive_queue);
  175. /* Next, the error queue. */
  176. __skb_queue_purge(&sk->sk_error_queue);
  177. /* Next, the write queue. */
  178. WARN_ON(!skb_queue_empty(&sk->sk_write_queue));
  179. /* Account for returned memory. */
  180. sk_mem_reclaim(sk);
  181. WARN_ON(sk->sk_wmem_queued);
  182. WARN_ON(sk->sk_forward_alloc);
  183. /* It is _impossible_ for the backlog to contain anything
  184. * when we get here. All user references to this socket
  185. * have gone away, only the net layer knows can touch it.
  186. */
  187. }
  188. EXPORT_SYMBOL(sk_stream_kill_queues);