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/sched/signal.h>
  16. #include <linux/net.h>
  17. #include <linux/signal.h>
  18. #include <linux/tcp.h>
  19. #include <linux/wait.h>
  20. #include <net/sock.h>
  21. /**
  22. * sk_stream_write_space - stream socket write_space callback.
  23. * @sk: socket
  24. *
  25. * FIXME: write proper description
  26. */
  27. void sk_stream_write_space(struct sock *sk)
  28. {
  29. struct socket *sock = sk->sk_socket;
  30. struct socket_wq *wq;
  31. if (sk_stream_is_writeable(sk) && sock) {
  32. clear_bit(SOCK_NOSPACE, &sock->flags);
  33. rcu_read_lock();
  34. wq = rcu_dereference(sk->sk_wq);
  35. if (skwq_has_sleeper(wq))
  36. wake_up_interruptible_poll(&wq->wait, POLLOUT |
  37. POLLWRNORM | POLLWRBAND);
  38. if (wq && wq->fasync_list && !(sk->sk_shutdown & SEND_SHUTDOWN))
  39. sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
  40. rcu_read_unlock();
  41. }
  42. }
  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. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  53. struct task_struct *tsk = current;
  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. add_wait_queue(sk_sleep(sk), &wait);
  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)), &wait);
  71. remove_wait_queue(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_FUNC(wait, woken_wake_function);
  90. add_wait_queue(sk_sleep(sk), &wait);
  91. do {
  92. if (sk_wait_event(sk, &timeout, !sk_stream_closing(sk), &wait))
  93. break;
  94. } while (!signal_pending(current) && timeout);
  95. remove_wait_queue(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_FUNC(wait, woken_wake_function);
  111. if (sk_stream_memory_free(sk))
  112. current_timeo = vm_wait = (prandom_u32() % (HZ / 5)) + 2;
  113. add_wait_queue(sk_sleep(sk), &wait);
  114. while (1) {
  115. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  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), &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. remove_wait_queue(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);