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.
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-))
15 #include <linux/module.h>
16 #include <linux/net.h>
17 #include <linux/signal.h>
18 #include <linux/tcp.h>
19 #include <linux/wait.h>
22 void sk_stream_write_space(struct sock
*sk
)
24 struct socket
*sock
= sk
->sk_socket
;
27 if (sk_stream_wspace(sk
) >= sk_stream_min_wspace(sk
) && sock
) {
28 clear_bit(SOCK_NOSPACE
, &sock
->flags
);
31 wq
= rcu_dereference(sk
->sk_wq
);
32 if (wq_has_sleeper(wq
))
33 wake_up_interruptible_poll(&wq
->wait
, POLLOUT
|
34 POLLWRNORM
| POLLWRBAND
);
35 if (wq
&& wq
->fasync_list
&& !(sk
->sk_shutdown
& SEND_SHUTDOWN
))
36 sock_wake_async(sock
, SOCK_WAKE_SPACE
, POLL_OUT
);
40 EXPORT_SYMBOL(sk_stream_write_space
);
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
47 * Must be called with the socket locked.
49 int sk_stream_wait_connect(struct sock
*sk
, long *timeo_p
)
51 struct task_struct
*tsk
= current
;
56 int err
= sock_error(sk
);
59 if ((1 << sk
->sk_state
) & ~(TCPF_SYN_SENT
| TCPF_SYN_RECV
))
63 if (signal_pending(tsk
))
64 return sock_intr_errno(*timeo_p
);
66 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
67 sk
->sk_write_pending
++;
68 done
= sk_wait_event(sk
, timeo_p
,
70 !((1 << sk
->sk_state
) &
71 ~(TCPF_ESTABLISHED
| TCPF_CLOSE_WAIT
)));
72 finish_wait(sk_sleep(sk
), &wait
);
73 sk
->sk_write_pending
--;
77 EXPORT_SYMBOL(sk_stream_wait_connect
);
80 * sk_stream_closing - Return 1 if we still have things to send in our buffers.
81 * @sk: socket to verify
83 static inline int sk_stream_closing(struct sock
*sk
)
85 return (1 << sk
->sk_state
) &
86 (TCPF_FIN_WAIT1
| TCPF_CLOSING
| TCPF_LAST_ACK
);
89 void sk_stream_wait_close(struct sock
*sk
, long timeout
)
95 prepare_to_wait(sk_sleep(sk
), &wait
,
97 if (sk_wait_event(sk
, &timeout
, !sk_stream_closing(sk
)))
99 } while (!signal_pending(current
) && timeout
);
101 finish_wait(sk_sleep(sk
), &wait
);
104 EXPORT_SYMBOL(sk_stream_wait_close
);
107 * sk_stream_wait_memory - Wait for more memory for a socket
108 * @sk: socket to wait for memory
109 * @timeo_p: for how long
111 int sk_stream_wait_memory(struct sock
*sk
, long *timeo_p
)
115 long current_timeo
= *timeo_p
;
118 if (sk_stream_memory_free(sk
))
119 current_timeo
= vm_wait
= (net_random() % (HZ
/ 5)) + 2;
122 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
124 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
126 if (sk
->sk_err
|| (sk
->sk_shutdown
& SEND_SHUTDOWN
))
130 if (signal_pending(current
))
132 clear_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
133 if (sk_stream_memory_free(sk
) && !vm_wait
)
136 set_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
137 sk
->sk_write_pending
++;
138 sk_wait_event(sk
, ¤t_timeo
, sk
->sk_err
||
139 (sk
->sk_shutdown
& SEND_SHUTDOWN
) ||
140 (sk_stream_memory_free(sk
) &&
142 sk
->sk_write_pending
--;
145 vm_wait
-= current_timeo
;
146 current_timeo
= *timeo_p
;
147 if (current_timeo
!= MAX_SCHEDULE_TIMEOUT
&&
148 (current_timeo
-= vm_wait
) < 0)
152 *timeo_p
= current_timeo
;
155 finish_wait(sk_sleep(sk
), &wait
);
165 err
= sock_intr_errno(*timeo_p
);
168 EXPORT_SYMBOL(sk_stream_wait_memory
);
170 int sk_stream_error(struct sock
*sk
, int flags
, int err
)
173 err
= sock_error(sk
) ? : -EPIPE
;
174 if (err
== -EPIPE
&& !(flags
& MSG_NOSIGNAL
))
175 send_sig(SIGPIPE
, current
, 0);
178 EXPORT_SYMBOL(sk_stream_error
);
180 void sk_stream_kill_queues(struct sock
*sk
)
182 /* First the read buffer. */
183 __skb_queue_purge(&sk
->sk_receive_queue
);
185 /* Next, the error queue. */
186 __skb_queue_purge(&sk
->sk_error_queue
);
188 /* Next, the write queue. */
189 WARN_ON(!skb_queue_empty(&sk
->sk_write_queue
));
191 /* Account for returned memory. */
194 WARN_ON(sk
->sk_wmem_queued
);
195 WARN_ON(sk
->sk_forward_alloc
);
197 /* It is _impossible_ for the backlog to contain anything
198 * when we get here. All user references to this socket
199 * have gone away, only the net layer knows can touch it.
202 EXPORT_SYMBOL(sk_stream_kill_queues
);