linux-rx-listener-flush-signals-20030605
[openafs.git] / src / rx / LINUX / rx_knet.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * rx_knet.c - RX kernel send, receive and timer routines.
12  *
13  * Linux implementation.
14  */
15 #include <afsconfig.h>
16 #include "afs/param.h"
17
18 RCSID("$Header$");
19
20 #include <linux/version.h>
21 #ifdef AFS_LINUX22_ENV
22 #include "rx/rx_kcommon.h"
23 #if defined(AFS_LINUX24_ENV)
24 #include "h/smp_lock.h"
25 #endif
26 #include <asm/uaccess.h>
27
28 /* rxk_NewSocket
29  * open and bind RX socket
30  */
31 struct osi_socket *rxk_NewSocket(short aport)
32 {
33     struct socket *sockp;
34     struct sockaddr_in myaddr;
35     int code;
36
37     
38     code = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp);
39     if (code<0)
40         return NULL;
41
42     /* Bind socket */
43     myaddr.sin_family = AF_INET;
44     myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
45     myaddr.sin_port = aport;
46     code = sockp->ops->bind(sockp, (struct sockaddr*)&myaddr, sizeof(myaddr));
47
48     if (code<0) {
49 #if defined(AFS_LINUX24_ENV)
50         printk("sock_release(rx_socket) FIXME\n");
51 #else
52         sock_release(sockp);
53 #endif
54         return NULL;
55     }
56
57     return (struct osi_socket*)sockp;
58 }
59
60
61 /* free socket allocated by osi_NetSocket */
62 int rxk_FreeSocket(register struct socket *asocket)
63 {
64     AFS_STATCNT(osi_FreeSocket);
65     return 0;
66 }
67
68
69 /* osi_NetSend
70  *
71  * Return codes:
72  * 0 = success
73  * non-zero = failure
74  */
75 int osi_NetSend(osi_socket sop, struct sockaddr_in *to,
76                 struct iovec *iov, int iovcnt, afs_int32 size, int istack)
77 {
78     KERNEL_SPACE_DECL;
79     struct msghdr msg;
80     int code;
81     struct iovec tmpvec[RX_MAXWVECS+2];
82
83     if (iovcnt > RX_MAXWVECS+2) {
84         osi_Panic("Too many (%d) iovecs passed to osi_NetSend\n", iovcnt);
85     }
86
87     if (iovcnt <= 2) { /* avoid pointless uiomove */
88         tmpvec[0].iov_base = iov[0].iov_base;
89         tmpvec[0].iov_len = size;
90         msg.msg_iovlen = 1;
91     }
92     else {
93         memcpy(tmpvec, iov, iovcnt * sizeof(struct iovec));
94         msg.msg_iovlen = iovcnt;
95     }
96     msg.msg_iov = tmpvec;
97     msg.msg_name = to;
98     msg.msg_namelen = sizeof(*to);
99     msg.msg_control = NULL;
100     msg.msg_controllen = 0;
101     msg.msg_flags = 0;
102
103     TO_USER_SPACE();
104     code = sock_sendmsg(sop, &msg, size);
105     TO_KERNEL_SPACE();
106     return (code<0) ? code : 0;
107 }
108
109
110 /* osi_NetReceive
111  * OS dependent part of kernel RX listener thread.
112  *
113  * Arguments:
114  *      so      socket to receive on, typically rx_socket
115  *      from    pointer to a sockaddr_in. 
116  *      iov     array of iovecs to fill in.
117  *      iovcnt  how many iovecs there are.
118  *      lengthp IN/OUT in: total space available in iovecs. out: size of read.
119  *
120  * Return
121  * 0 if successful
122  * error code (such as EINTER) if not
123  *
124  * Environment
125  *      Note that the maximum number of iovecs is 2 + RX_MAXWVECS. This is
126  *      so we have a little space to look for packets larger than 
127  *      rx_maxReceiveSize.
128  */
129 int rxk_lastSocketError;
130 int rxk_nSocketErrors;
131 int osi_NetReceive(osi_socket so, struct sockaddr_in *from, 
132                    struct iovec *iov, int iovcnt, int *lengthp)
133 {
134     KERNEL_SPACE_DECL;
135     struct msghdr msg;
136     int code;
137     struct iovec tmpvec[RX_MAXWVECS+2];
138     struct socket *sop = (struct socket*)so;
139
140     if (iovcnt > RX_MAXWVECS+2) {
141         osi_Panic("Too many (%d) iovecs passed to osi_NetReceive\n", iovcnt);
142     }
143     memcpy(tmpvec, iov, iovcnt * sizeof(struct iovec));
144     msg.msg_name = from;
145     msg.msg_iov = tmpvec;
146     msg.msg_iovlen = iovcnt;
147     msg.msg_control = NULL;
148     msg.msg_controllen = 0;
149     msg.msg_flags = 0;
150
151     TO_USER_SPACE();
152     code = sock_recvmsg(sop, &msg, *lengthp, 0);
153     TO_KERNEL_SPACE();
154
155     if (code<0) {
156         /* Clear the error before using the socket again.
157          * Oh joy, Linux has hidden header files as well. It appears we can
158          * simply call again and have it clear itself via sock_error().
159          */
160 #ifdef AFS_LINUX22_ENV
161         flush_signals(current); /* We don't want no stinkin' signals. */
162 #else
163         current->signal = 0; /* We don't want no stinkin' signals. */
164 #endif
165         rxk_lastSocketError = code;
166         rxk_nSocketErrors ++ ;
167     }
168     else {
169         *lengthp = code;
170         code = 0;
171     }
172
173     return code;
174 }
175
176 void osi_StopListener(void)
177 {
178     struct task_struct *listener;
179     extern int rxk_ListenerPid;
180
181 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
182     read_lock(&tasklist_lock);
183 #endif
184     listener =  find_task_by_pid(rxk_ListenerPid);
185 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
186     read_unlock(&tasklist_lock);
187 #endif
188     while (rxk_ListenerPid) {
189         struct task_struct *p;
190
191         flush_signals(listener);
192         force_sig(SIGKILL, listener);
193         afs_osi_Sleep(&rxk_ListenerPid); 
194     }
195     sock_release(rx_socket);
196     rx_socket = NULL;
197 }
198
199 #endif /* AFS_LINUX22_ENV */