stream.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. bool noblock = (*timeo_p ? false : true);
  110. DEFINE_WAIT(wait);
  111. if (sk_stream_memory_free(sk))
  112. current_timeo = vm_wait = (prandom_u32() % (HZ / 5)) + 2;
  113. while (1) {
  114. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  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. if (noblock)
  120. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  121. goto do_nonblock;
  122. }
  123. if (signal_pending(current))
  124. goto do_interrupted;
  125. sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  126. if (sk_stream_memory_free(sk) && !vm_wait)
  127. break;
  128. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  129. sk->sk_write_pending++;
  130. sk_wait_event(sk, &current_timeo, sk->sk_err ||
  131. (sk->sk_shutdown & SEND_SHUTDOWN) ||
  132. (sk_stream_memory_free(sk) &&
  133. !vm_wait));
  134. sk->sk_write_pending--;
  135. if (vm_wait) {
  136. vm_wait -= current_timeo;
  137. current_timeo = *timeo_p;
  138. if (current_timeo != MAX_SCHEDULE_TIMEOUT &&
  139. (current_timeo -= vm_wait) < 0)
  140. current_timeo = 0;
  141. vm_wait = 0;
  142. }
  143. *timeo_p = current_timeo;
  144. }
  145. out:
  146. finish_wait(sk_sleep(sk), &wait);
  147. return err;
  148. do_error:
  149. err = -EPIPE;
  150. goto out;
  151. do_nonblock:
  152. err = -EAGAIN;
  153. goto out;
  154. do_interrupted:
  155. err = sock_intr_errno(*timeo_p);
  156. goto out;
  157. }
  158. EXPORT_SYMBOL(sk_stream_wait_memory);
  159. int sk_stream_error(struct sock *sk, int flags, int err)
  160. {
  161. if (err == -EPIPE)
  162. err = sock_error(sk) ? : -EPIPE;
  163. if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
  164. send_sig(SIGPIPE, current, 0);
  165. return err;
  166. }
  167. EXPORT_SYMBOL(sk_stream_error);
  168. void sk_stream_kill_queues(struct sock *sk)
  169. {
  170. /* First the read buffer. */
  171. __skb_queue_purge(&sk->sk_receive_queue);
  172. /* Next, the error queue. */
  173. __skb_queue_purge(&sk->sk_error_queue);
  174. /* Next, the write queue. */
  175. WARN_ON(!skb_queue_empty(&sk->sk_write_queue));
  176. /* Account for returned memory. */
  177. sk_mem_reclaim(sk);
  178. WARN_ON(sk->sk_wmem_queued);
  179. WARN_ON(sk->sk_forward_alloc);
  180. /* It is _impossible_ for the backlog to contain anything
  181. * when we get here. All user references to this socket
  182. * have gone away, only the net layer knows can touch it.
  183. */
  184. }
  185. EXPORT_SYMBOL(sk_stream_kill_queues);