The LDT fixes in particular fix some potentially random strange behaviour.
[davej-history.git] / net / khttpd / accept.c
blob97dd21709823909ab6f301bc4d76ff6ec993c58d
1 /*
3 kHTTPd -- the next generation
5 Accept connections
7 */
8 /****************************************************************
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 ****************************************************************/
25 #include "structure.h"
26 #include "prototypes.h"
27 #include "sysctl.h"
29 #include <linux/smp_lock.h>
33 Purpose:
35 AcceptConnections puts all "accepted" connections in the
36 "WaitForHeader" queue.
38 Return value:
39 The number of accepted connections
43 int AcceptConnections(const int CPUNR, struct socket *Socket)
45 struct http_request *NewRequest;
46 struct socket *NewSock;
47 int count = 0;
48 int error;
50 EnterFunction("AcceptConnections");
52 if (atomic_read(&ConnectCount)>sysctl_khttpd_maxconnect)
54 LeaveFunction("AcceptConnections - to many active connections");
55 return 0;
58 if (Socket==NULL) return 0;
60 /*
61 Quick test to see if there are connections on the queue.
62 This is cheaper than accept() itself because this saves us
63 the allocation of a new socket. (Which doesn't seem to be
64 used anyway)
66 if (Socket->sk->tp_pinfo.af_tcp.accept_queue==NULL)
68 return 0;
71 error = 0;
72 while (error>=0)
74 NewSock = sock_alloc();
75 if (NewSock==NULL)
76 break;
79 NewSock->type = Socket->type;
80 NewSock->ops = Socket->ops;
83 error = Socket->ops->accept(Socket,NewSock,O_NONBLOCK);
86 if (error<0)
88 sock_release(NewSock);
89 break;
92 if (NewSock->sk->state==TCP_CLOSE)
94 sock_release(NewSock);
95 continue;
98 /* Allocate a request-entry for the connection */
99 NewRequest = kmalloc(sizeof(struct http_request),(int)GFP_KERNEL);
101 if (NewRequest == NULL)
103 Send50x(NewSock); /* Service not available. Try again later */
104 sock_release(NewSock);
105 break;
107 memset(NewRequest,0,sizeof(struct http_request));
109 NewRequest->sock = NewSock;
111 NewRequest->Next = threadinfo[CPUNR].WaitForHeaderQueue;
113 init_waitqueue_entry(&NewRequest->sleep,current);
115 add_wait_queue(NewSock->sk->sleep,&(NewRequest->sleep));
117 threadinfo[CPUNR].WaitForHeaderQueue = NewRequest;
119 atomic_inc(&ConnectCount);
122 count++;
125 LeaveFunction("AcceptConnections");
126 return count;