more changes on original files
[linux-2.4.git] / net / khttpd / accept.c
1 /*
2
3 kHTTPd -- the next generation
4
5 Accept connections
6
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.
13  *
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.
18  *
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.
22  *
23  ****************************************************************/
24
25 #include "structure.h"
26 #include "prototypes.h"
27 #include "sysctl.h"
28
29 #include <linux/smp_lock.h>
30
31 /*
32
33 Purpose:
34
35 AcceptConnections puts all "accepted" connections in the 
36 "WaitForHeader" queue.
37
38 Return value:
39         The number of accepted connections
40 */
41
42
43 int AcceptConnections(const int CPUNR, struct socket *Socket)
44 {
45         struct http_request *NewRequest;
46         struct socket *NewSock;
47         int count = 0;
48         int error;
49
50         EnterFunction("AcceptConnections");
51         
52         if (atomic_read(&ConnectCount)>sysctl_khttpd_maxconnect)
53         {
54                 LeaveFunction("AcceptConnections - to many active connections");
55                 return 0;
56         }
57         
58         if (Socket==NULL) return 0;
59         
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)
65         */
66         if (Socket->sk->tp_pinfo.af_tcp.accept_queue==NULL)
67         {
68                 return 0;
69         }
70         
71         error = 0;      
72         while (error>=0)
73         {
74                 NewSock = sock_alloc();
75                 if (NewSock==NULL)
76                         break;
77                         
78                         
79                 NewSock->type = Socket->type;
80                 NewSock->ops = Socket->ops;
81                 
82                 
83                 error = Socket->ops->accept(Socket,NewSock,O_NONBLOCK);
84                 
85
86                 if (error<0)
87                 {
88                         sock_release(NewSock);
89                         break;
90                 }
91
92                 if (NewSock->sk->state==TCP_CLOSE)
93                 {
94                         sock_release(NewSock);
95                         continue;
96                 }
97                 
98                 /* Allocate a request-entry for the connection */
99                 NewRequest = kmalloc(sizeof(struct http_request),(int)GFP_KERNEL); 
100                 
101                 if (NewRequest == NULL)
102                 {
103                         Send50x(NewSock);       /* Service not available. Try again later */
104                         sock_release(NewSock);
105                         break;
106                 }
107                 memset(NewRequest,0,sizeof(struct http_request));  
108                 
109                 NewRequest->sock = NewSock;
110                 
111                 NewRequest->Next = threadinfo[CPUNR].WaitForHeaderQueue;
112                 
113                 init_waitqueue_entry(&NewRequest->sleep,current);
114                 
115                 add_wait_queue(NewSock->sk->sleep,&(NewRequest->sleep));
116                 
117                 threadinfo[CPUNR].WaitForHeaderQueue = NewRequest;
118                 
119                 atomic_inc(&ConnectCount);
120
121         
122                 count++;
123         }               
124         
125         LeaveFunction("AcceptConnections");
126         return count;
127 }