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