stream.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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_is_writeable(sk) && sock) {
  31. clear_bit(SOCK_NOSPACE, &sock->flags);
  32. rcu_read_lock();
  33. wq = rcu_dereference(sk->sk_wq);
  34. if (skwq_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(wq, SOCK_WAKE_SPACE, POLL_OUT);
  39. rcu_read_unlock();
  40. }
  41. }
  42. /**
  43. * sk_stream_wait_connect - Wait for a socket to get into the connected state
  44. * @sk: sock to wait on
  45. * @timeo_p: for how long to wait
  46. *
  47. * Must be called with the socket locked.
  48. */
  49. int sk_stream_wait_connect(struct sock *sk, long *timeo_p)
  50. {
  51. struct task_struct *tsk = current;
  52. DEFINE_WAIT(wait);
  53. int done;
  54. do {
  55. int err = sock_error(sk);
  56. if (err)
  57. return err;
  58. if ((1 << sk->sk_state) & ~(TCPF_SYN_SENT | TCPF_SYN_RECV))
  59. return -EPIPE;
  60. if (!*timeo_p)
  61. return -EAGAIN;
  62. if (signal_pending(tsk))
  63. return sock_intr_errno(*timeo_p);
  64. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  65. sk->sk_write_pending++;
  66. done = sk_wait_event(sk, timeo_p,
  67. !sk->sk_err &&
  68. !((1 << sk->sk_state) &
  69. ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)));
  70. finish_wait(sk_sleep(sk), &wait);
  71. sk->sk_write_pending--;
  72. } while (!done);
  73. return 0;
  74. }
  75. EXPORT_SYMBOL(sk_stream_wait_connect);
  76. /**
  77. * sk_stream_closing - Return 1 if we still have things to send in our buffers.
  78. * @sk: socket to verify
  79. */
  80. static inline int sk_stream_closing(struct sock *sk)
  81. {
  82. return (1 << sk->sk_state) &
  83. (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK);
  84. }
  85. void sk_stream_wait_close(struct sock *sk, long timeout)
  86. {
  87. if (timeout) {
  88. DEFINE_WAIT(wait);
  89. do {
  90. prepare_to_wait(sk_sleep(sk), &wait,
  91. TASK_INTERRUPTIBLE);
  92. if (sk_wait_event(sk, &timeout, !sk_stream_closing(sk)))
  93. break;
  94. } while (!signal_pending(current) && timeout);
  95. finish_wait(sk_sleep(sk), &wait);
  96. }
  97. }
  98. EXPORT_SYMBOL(sk_stream_wait_close);
  99. /**
  100. * sk_stream_wait_memory - Wait for more memory for a socket
  101. * @sk: socket to wait for memory
  102. * @timeo_p: for how long
  103. */
  104. int sk_stream_wait_memory(struct sock *sk, long *timeo_p)
  105. {
  106. int err = 0;
  107. long vm_wait = 0;
  108. long current_timeo = *timeo_p;
  109. DEFINE_WAIT(wait);
  110. if (sk_stream_memory_free(sk))
  111. current_timeo = vm_wait = (prandom_u32() % (HZ / 5)) + 2;
  112. while (1) {
  113. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  114. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  115. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
  116. goto do_error;
  117. if (!*timeo_p)
  118. goto do_eagain;
  119. if (signal_pending(current))
  120. goto do_interrupted;
  121. sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  122. if (sk_stream_memory_free(sk) && !vm_wait)
  123. break;
  124. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  125. sk->sk_write_pending++;
  126. sk_wait_event(sk, &current_timeo, sk->sk_err ||
  127. (sk->sk_shutdown & SEND_SHUTDOWN) ||
  128. (sk_stream_memory_free(sk) &&
  129. !vm_wait));
  130. sk->sk_write_pending--;
  131. if (vm_wait) {
  132. vm_wait -= current_timeo;
  133. current_timeo = *timeo_p;
  134. if (current_timeo != MAX_SCHEDULE_TIMEOUT &&
  135. (current_timeo -= vm_wait) < 0)
  136. current_timeo = 0;
  137. vm_wait = 0;
  138. }
  139. *timeo_p = current_timeo;
  140. }
  141. out:
  142. finish_wait(sk_sleep(sk), &wait);
  143. return err;
  144. do_error:
  145. err = -EPIPE;
  146. goto out;
  147. do_eagain:
  148. /* Make sure that whenever EAGAIN is returned, EPOLLOUT event can
  149. * be generated later.
  150. * When TCP receives ACK packets that make room, tcp_check_space()
  151. * only calls tcp_new_space() if SOCK_NOSPACE is set.
  152. */
  153. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  154. err = -EAGAIN;
  155. goto out;
  156. do_interrupted:
  157. err = sock_intr_errno(*timeo_p);
  158. goto out;
  159. }
  160. EXPORT_SYMBOL(sk_stream_wait_memory);
  161. int sk_stream_error(struct sock *sk, int flags, int err)
  162. {
  163. if (err == -EPIPE)
  164. err = sock_error(sk) ? : -EPIPE;
  165. if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
  166. send_sig(SIGPIPE, current, 0);
  167. return err;
  168. }
  169. EXPORT_SYMBOL(sk_stream_error);
  170. void sk_stream_kill_queues(struct sock *sk)
  171. {
  172. /* First the read buffer. */
  173. __skb_queue_purge(&sk->sk_receive_queue);
  174. /* Next, the error queue. */
  175. __skb_queue_purge(&sk->sk_error_queue);
  176. /* Next, the write queue. */
  177. WARN_ON(!skb_queue_empty(&sk->sk_write_queue));
  178. /* Account for returned memory. */
  179. sk_mem_reclaim(sk);
  180. WARN_ON(sk->sk_wmem_queued);
  181. WARN_ON(sk->sk_forward_alloc);
  182. /* It is _impossible_ for the backlog to contain anything
  183. * when we get here. All user references to this socket
  184. * have gone away, only the net layer knows can touch it.
  185. */
  186. }
  187. EXPORT_SYMBOL(sk_stream_kill_queues);