darwin rxevent sleep instead of polling
[openafs.git] / src / rx / rx_kcommon.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_kcommon.c - Common kernel RX code for all system types.
12  */
13
14 #include <afsconfig.h>
15 #include "afs/param.h"
16
17
18 #include "rx/rx_kcommon.h"
19
20 #ifdef AFS_HPUX110_ENV
21 #include "h/tihdr.h"
22 #include <xti.h>
23 #endif
24 #include "afsint.h"
25
26 #ifndef RXK_LISTENER_ENV
27 int (*rxk_PacketArrivalProc) (struct rx_packet * ahandle, struct sockaddr_in * afrom, struct socket *arock, afs_int32 asize);   /* set to packet allocation procedure */
28 int (*rxk_GetPacketProc) (struct rx_packet **ahandle, int asize);
29 #endif
30
31 osi_socket *rxk_NewSocketHost(afs_uint32 ahost, short aport);
32 extern struct interfaceAddr afs_cb_interface;
33
34 rxk_ports_t rxk_ports;
35 rxk_portRocks_t rxk_portRocks;
36
37 int rxk_initDone = 0;
38
39 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
40 #define ADDRSPERSITE 16
41 static afs_uint32 myNetAddrs[ADDRSPERSITE];
42 static int myNetMTUs[ADDRSPERSITE];
43 static int numMyNetAddrs = 0;
44 #endif
45
46 #if defined(AFS_DARWIN80_ENV)
47 #define sobind sock_bind
48 #define soclose sock_close
49 #endif
50
51 /* add a port to the monitored list, port # is in network order */
52 static int
53 rxk_AddPort(u_short aport, char *arock)
54 {
55     int i;
56     unsigned short *tsp, ts;
57     int zslot;
58
59     zslot = -1;                 /* look for an empty slot simultaneously */
60     for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
61         if (((ts = *tsp) == 0) && (zslot == -1))
62             zslot = i;
63         if (ts == aport) {
64             return 0;
65         }
66     }
67     /* otherwise allocate a new port slot */
68     if (zslot < 0)
69         return E2BIG;           /* all full */
70     rxk_ports[zslot] = aport;
71     rxk_portRocks[zslot] = arock;
72     return 0;
73 }
74
75 /* remove as port from the monitored list, port # is in network order */
76 int
77 rxk_DelPort(u_short aport)
78 {
79     int i;
80     unsigned short *tsp;
81
82     for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
83         if (*tsp == aport) {
84             /* found it, adjust ref count and free the port reference if all gone */
85             *tsp = 0;
86             return 0;
87         }
88     }
89     /* otherwise port not found */
90     return ENOENT;
91 }
92
93 void
94 rxk_shutdownPorts(void)
95 {
96     int i;
97     for (i = 0; i < MAXRXPORTS; i++) {
98         if (rxk_ports[i]) {
99             rxk_ports[i] = 0;
100 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
101             soclose((struct socket *)rxk_portRocks[i]);
102 #endif
103             rxk_portRocks[i] = NULL;
104         }
105     }
106 }
107
108 osi_socket
109 rxi_GetHostUDPSocket(u_int host, u_short port)
110 {
111     osi_socket *sockp;
112     sockp = (osi_socket *)rxk_NewSocketHost(host, port);
113     if (sockp == (osi_socket *)0)
114         return OSI_NULLSOCKET;
115     rxk_AddPort(port, (char *)sockp);
116     return (osi_socket) sockp;
117 }
118
119 osi_socket
120 rxi_GetUDPSocket(u_short port)
121 {
122     return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
123 }
124
125 #if !defined(AFS_LINUX26_ENV)
126 void
127 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
128 osi_Panic(char *msg, void *a1, void *a2, void *a3)
129 #else
130 osi_Panic(char *msg, ...)
131 #endif
132 {
133 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
134     if (!msg)
135         msg = "Unknown AFS panic";
136     printf(msg, a1, a2, a3);
137     panic(msg);
138 #elif (defined(AFS_DARWIN80_ENV) && !defined(AFS_DARWIN90_ENV)) || (defined(AFS_LINUX22_ENV) && !defined(AFS_LINUX_26_ENV))
139     char buf[256];
140     va_list ap;
141     if (!msg)
142         msg = "Unknown AFS panic";
143
144     va_start(ap, msg);
145     vsnprintf(buf, sizeof(buf), msg, ap);
146     va_end(ap);
147     printf(buf);
148     panic(buf);
149 #else
150     va_list ap;
151     if (!msg)
152         msg = "Unknown AFS panic";
153
154     va_start(ap, msg);
155     vprintf(msg, ap);
156     va_end(ap);
157 # ifdef AFS_LINUX20_ENV
158     * ((char *) 0) = 0; 
159 # else
160     panic(msg);
161 # endif
162 #endif
163 }
164
165 /*
166  * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
167  * unsigned long value into the given buffer.  Returns 0 on success,
168  * and a value less than 0 on failure.  The contents of the buffer is
169  * defined only on success.
170  */
171
172 int
173 osi_utoa(char *buf, size_t len, unsigned long val)
174 {
175     long k;                     /* index of first byte of string value */
176
177     /* we definitely need room for at least one digit and NUL */
178
179     if (len < 2) {
180         return -1;
181     }
182
183     /* compute the string form from the high end of the buffer */
184
185     buf[len - 1] = '\0';
186     for (k = len - 2; k >= 0; k--) {
187         buf[k] = val % 10 + '0';
188         val /= 10;
189
190         if (val == 0)
191             break;
192     }
193
194     /* did we finish converting val to string form? */
195
196     if (val != 0) {
197         return -2;
198     }
199
200     /* this should never happen */
201
202     if (k < 0) {
203         return -3;
204     }
205
206     /* this should never happen */
207
208     if (k >= len) {
209         return -4;
210     }
211
212     /* if necessary, relocate string to beginning of buf[] */
213
214     if (k > 0) {
215
216         /*
217          * We need to achieve the effect of calling
218          *
219          * memmove(buf, &buf[k], len - k);
220          *
221          * However, since memmove() is not available in all
222          * kernels, we explicitly do an appropriate copy.
223          */
224
225         char *dst = buf;
226         char *src = buf + k;
227
228         while ((*dst++ = *src++) != '\0')
229             continue;
230     }
231
232     return 0;
233 }
234
235 /*
236  * osi_AssertFailK() -- used by the osi_Assert() macro.
237  *
238  * It essentially does
239  *
240  * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
241  *
242  * Since the kernel version of osi_Panic() only passes its first
243  * argument to the native panic(), we construct a single string and hand
244  * that to osi_Panic().
245  */
246 void
247 osi_AssertFailK(const char *expr, const char *file, int line)
248 {
249     static const char msg0[] = "assertion failed: ";
250     static const char msg1[] = ", file: ";
251     static const char msg2[] = ", line: ";
252     static const char msg3[] = "\n";
253
254     /*
255      * These buffers add up to 1K, which is a pleasantly nice round
256      * value, but probably not vital.
257      */
258     char buf[1008];
259     char linebuf[16];
260
261     /* check line number conversion */
262
263     if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
264         osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
265     }
266
267     /* okay, panic */
268
269 #define ADDBUF(BUF, STR)                                        \
270         if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) {    \
271                 strcat(BUF, (char *)(STR));                             \
272         }
273
274     buf[0] = '\0';
275     ADDBUF(buf, msg0);
276     ADDBUF(buf, expr);
277     ADDBUF(buf, msg1);
278     ADDBUF(buf, file);
279     ADDBUF(buf, msg2);
280     ADDBUF(buf, linebuf);
281     ADDBUF(buf, msg3);
282
283 #undef ADDBUF
284
285     osi_Panic("%s", buf);
286 }
287 #else
288 void
289 osi_AssertFailK(const char *expr, const char *file, int line)
290 {
291     printk(KERN_CRIT "assertion failed: %s, file: %s, line: %d\n", expr, file, line);
292 }
293 #endif
294
295 #ifndef UKERNEL
296 /* This is the server process request loop. Kernel server
297  * processes never become listener threads */
298 void *
299 rx_ServerProc(void *unused)
300 {
301     int threadID;
302
303 /* jaltman - rxi_dataQuota is protected by a mutex everywhere else */
304     rxi_MorePackets(rx_maxReceiveWindow + 2);   /* alloc more packets */
305     rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
306     /* threadID is used for making decisions in GetCall.  Get it by bumping
307      * number of threads handling incoming calls */
308     threadID = rxi_availProcs++;
309
310 #ifdef RX_ENABLE_LOCKS
311     AFS_GUNLOCK();
312 #endif /* RX_ENABLE_LOCKS */
313     rxi_ServerProc(threadID, NULL, NULL);
314 #ifdef RX_ENABLE_LOCKS
315     AFS_GLOCK();
316 #endif /* RX_ENABLE_LOCKS */
317
318     return NULL;
319 }
320 #endif /* !UKERNEL */
321
322 #ifndef RXK_LISTENER_ENV
323 /* asize includes the Rx header */
324 static int
325 MyPacketProc(struct rx_packet **ahandle, int asize)
326 {
327     struct rx_packet *tp;
328
329     /* If this is larger than we expected, increase rx_maxReceiveDataSize */
330     /* If we can't scrounge enough cbufs, then we have to drop the packet,
331      * but we should set a flag so we magic up some more at our leisure.
332      */
333
334     if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
335         tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
336         if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
337             if (0 <
338                 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
339                                  RX_PACKET_CLASS_RECV_CBUF)) {
340                 rxi_FreePacket(tp);
341                 tp = NULL;
342                 if (rx_stats_active) {
343                     MUTEX_ENTER(&rx_stats_mutex);
344                     rx_stats.noPacketBuffersOnRead++;
345                     MUTEX_EXIT(&rx_stats_mutex);
346                 }
347             }
348         }
349     } else {
350         /*
351          * XXX if packet is too long for our buffer,
352          * should do this at a higher layer and let other
353          * end know we're losing.
354          */
355         if (rx_stats_active) {
356             MUTEX_ENTER(&rx_stats_mutex);
357             rx_stats.bogusPacketOnRead++;
358             MUTEX_EXIT(&rx_stats_mutex);
359         }
360         /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
361         dpf(("rx: packet dropped: bad ulen=%d\n", asize));
362         tp = NULL;
363     }
364
365     if (!tp)
366         return -1;
367     /* otherwise we have a packet, set appropriate values */
368     *ahandle = tp;
369     return 0;
370 }
371
372 static int
373 MyArrivalProc(struct rx_packet *ahandle,
374               struct sockaddr_in *afrom,
375               struct socket *arock,
376               afs_int32 asize)
377 {
378     /* handle basic rx packet */
379     ahandle->length = asize - RX_HEADER_SIZE;
380     rxi_DecodePacketHeader(ahandle);
381     ahandle =
382         rxi_ReceivePacket(ahandle, arock,
383                           afrom->sin_addr.s_addr, afrom->sin_port, NULL,
384                           NULL);
385
386     /* free the packet if it has been returned */
387     if (ahandle)
388         rxi_FreePacket(ahandle);
389     return 0;
390 }
391 #endif /* !RXK_LISTENER_ENV */
392
393 void
394 rxi_StartListener(void)
395 {
396     /* if kernel, give name of appropriate procedures */
397 #ifndef RXK_LISTENER_ENV
398     rxk_GetPacketProc = MyPacketProc;
399     rxk_PacketArrivalProc = MyArrivalProc;
400     rxk_init();
401 #endif
402 }
403
404 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
405   to get interesting information. */
406 void
407 rxi_InitPeerParams(struct rx_peer *pp)
408 {
409     u_short rxmtu;
410
411 #ifdef  ADAPT_MTU
412 # ifndef AFS_SUN5_ENV
413 #  ifdef AFS_USERSPACE_IP_ADDR
414     afs_int32 i;
415     afs_int32 mtu;
416
417     i = rxi_Findcbi(pp->host);
418     if (i == -1) {
419         pp->timeout.sec = 3;
420         /* pp->timeout.usec = 0; */
421         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
422     } else {
423         pp->timeout.sec = 2;
424         /* pp->timeout.usec = 0; */
425         pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
426     }
427     if (i != -1) {
428         mtu = ntohl(afs_cb_interface.mtu[i]);
429         /* Diminish the packet size to one based on the MTU given by
430          * the interface. */
431         if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
432             rxmtu = mtu - RX_IPUDP_SIZE;
433             if (rxmtu < pp->ifMTU)
434                 pp->ifMTU = rxmtu;
435         }
436     } else {                    /* couldn't find the interface, so assume the worst */
437         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
438     }
439 #  else /* AFS_USERSPACE_IP_ADDR */
440     rx_ifnet_t ifn;
441
442 #   if !defined(AFS_SGI62_ENV)
443     if (numMyNetAddrs == 0)
444         (void)rxi_GetIFInfo();
445 #   endif
446
447     ifn = rxi_FindIfnet(pp->host, NULL);
448     if (ifn) {
449         pp->timeout.sec = 2;
450         /* pp->timeout.usec = 0; */
451         pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
452 #   ifdef IFF_POINTOPOINT
453         if (rx_ifnet_flags(ifn) & IFF_POINTOPOINT) {
454             /* wish we knew the bit rate and the chunk size, sigh. */
455             pp->timeout.sec = 4;
456             pp->ifMTU = RX_PP_PACKET_SIZE;
457         }
458 #   endif /* IFF_POINTOPOINT */
459         /* Diminish the packet size to one based on the MTU given by
460          * the interface. */
461         if (rx_ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
462             rxmtu = rx_ifnet_mtu(ifn) - RX_IPUDP_SIZE;
463             if (rxmtu < pp->ifMTU)
464                 pp->ifMTU = rxmtu;
465         }
466     } else {                    /* couldn't find the interface, so assume the worst */
467         pp->timeout.sec = 3;
468         /* pp->timeout.usec = 0; */
469         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
470     }
471 #  endif /* else AFS_USERSPACE_IP_ADDR */
472 # else /* AFS_SUN5_ENV */
473     afs_int32 mtu;
474
475     mtu = rxi_FindIfMTU(pp->host);
476
477     if (mtu <= 0) {
478         pp->timeout.sec = 3;
479         /* pp->timeout.usec = 0; */
480         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
481     } else {
482         pp->timeout.sec = 2;
483         /* pp->timeout.usec = 0; */
484         pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
485     }
486
487     if (mtu > 0) {
488         /* Diminish the packet size to one based on the MTU given by
489          * the interface. */
490         if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
491             rxmtu = mtu - RX_IPUDP_SIZE;
492             if (rxmtu < pp->ifMTU)
493                 pp->ifMTU = rxmtu;
494         }
495     } else {                    /* couldn't find the interface, so assume the worst */
496         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE,rx_MyMaxSendSize);
497     }
498 # endif /* AFS_SUN5_ENV */
499 #else /* ADAPT_MTU */
500     pp->rateFlag = 2;           /* start timing after two full packets */
501     pp->timeout.sec = 2;
502     pp->ifMTU = OLD_MAX_PACKET_SIZE;
503 #endif /* else ADAPT_MTU */
504     pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
505     pp->maxMTU = OLD_MAX_PACKET_SIZE;   /* for compatibility with old guys */
506     pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
507     pp->ifDgramPackets =
508         MIN(rxi_nDgramPackets,
509             rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
510     pp->maxDgramPackets = 1;
511
512     /* Initialize slow start parameters */
513     pp->MTU = MIN(pp->natMTU, pp->maxMTU);
514     pp->cwind = 1;
515     pp->nDgramPackets = 1;
516     pp->congestSeq = 0;
517 }
518
519
520 /* The following code is common to several system types, but not all. The
521  * separate ones are found in the system specific subdirectories.
522  */
523
524
525 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
526 /* Routine called during the afsd "-shutdown" process to put things back to
527  * the initial state.
528  */
529 static struct protosw parent_proto;     /* udp proto switch */
530
531 void
532 shutdown_rxkernel(void)
533 {
534     struct protosw *tpro, *last;
535     last = inetdomain.dom_protoswNPROTOSW;
536     for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
537         if (tpro->pr_protocol == IPPROTO_UDP) {
538             /* restore original udp protocol switch */
539             memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
540             memset((void *)&parent_proto, 0, sizeof(parent_proto));
541             rxk_initDone = 0;
542             rxk_shutdownPorts();
543             return;
544         }
545     dpf(("shutdown_rxkernel: no udp proto"));
546 }
547 #endif /* !AIX && !SUN && !NCR  && !UKERNEL */
548
549 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
550 /* Determine what the network interfaces are for this machine. */
551
552 #ifdef AFS_USERSPACE_IP_ADDR
553 int
554 rxi_GetcbiInfo(void)
555 {
556     int i, j, different = 0, num = ADDRSPERSITE;
557     int rxmtu, maxmtu;
558     afs_uint32 ifinaddr;
559     afs_uint32 addrs[ADDRSPERSITE];
560     int mtus[ADDRSPERSITE];
561
562     memset((void *)addrs, 0, sizeof(addrs));
563     memset((void *)mtus, 0, sizeof(mtus));
564
565     if (afs_cb_interface.numberOfInterfaces < num)
566         num = afs_cb_interface.numberOfInterfaces;
567     for (i = 0; i < num; i++) {
568         if (!afs_cb_interface.mtu[i])
569             afs_cb_interface.mtu[i] = htonl(1500);
570         rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
571         ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
572         if (myNetAddrs[i] != ifinaddr)
573             different++;
574
575         mtus[i] = rxmtu;
576         rxmtu = rxi_AdjustIfMTU(rxmtu);
577         maxmtu =
578             rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
579         maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
580         addrs[i++] = ifinaddr;
581         if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
582             rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
583             rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
584         }
585     }
586
587     rx_maxJumboRecvSize =
588         RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
589         ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
590     rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
591
592     if (different) {
593         for (j = 0; j < i; j++) {
594             myNetMTUs[j] = mtus[j];
595             myNetAddrs[j] = addrs[j];
596         }
597     }
598     return different;
599 }
600
601
602 /* Returns the afs_cb_interface inxex which best matches address.
603  * If none is found, we return -1.
604  */
605 afs_int32
606 rxi_Findcbi(afs_uint32 addr)
607 {
608     int j;
609     afs_uint32 myAddr, thisAddr, netMask, subnetMask;
610     afs_int32 rvalue = -1;
611     int match_value = 0;
612
613     if (numMyNetAddrs == 0)
614         (void)rxi_GetcbiInfo();
615
616     myAddr = ntohl(addr);
617
618     if (IN_CLASSA(myAddr))
619         netMask = IN_CLASSA_NET;
620     else if (IN_CLASSB(myAddr))
621         netMask = IN_CLASSB_NET;
622     else if (IN_CLASSC(myAddr))
623         netMask = IN_CLASSC_NET;
624     else
625         netMask = 0;
626
627     for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
628         thisAddr = ntohl(afs_cb_interface.addr_in[j]);
629         subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
630         if ((myAddr & netMask) == (thisAddr & netMask)) {
631             if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
632                 if (myAddr == thisAddr) {
633                     match_value = 4;
634                     rvalue = j;
635                     break;
636                 }
637                 if (match_value < 3) {
638                     match_value = 3;
639                     rvalue = j;
640                 }
641             } else {
642                 if (match_value < 2) {
643                     match_value = 2;
644                     rvalue = j;
645                 }
646             }
647         }
648     }
649
650     return (rvalue);
651 }
652
653 #else /* AFS_USERSPACE_IP_ADDR */
654
655 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
656 #define IFADDR2SA(f) (&((f)->ifa_addr))
657 #else /* AFS_AIX41_ENV */
658 #define IFADDR2SA(f) ((f)->ifa_addr)
659 #endif
660
661 int
662 rxi_GetIFInfo(void)
663 {
664     int i = 0;
665     int different = 0;
666
667     int rxmtu, maxmtu;
668     afs_uint32 addrs[ADDRSPERSITE];
669     int mtus[ADDRSPERSITE];
670     afs_uint32 ifinaddr;
671 #if defined(AFS_DARWIN80_ENV)
672     errno_t t;
673     unsigned int count;
674     int cnt=0, m, j;
675     rx_ifaddr_t *ifads;
676     rx_ifnet_t *ifns;
677     struct sockaddr sout;
678     struct sockaddr_in *sin;
679     struct in_addr pin;
680 #else
681     rx_ifaddr_t ifad;   /* ifnet points to a if_addrlist of ifaddrs */
682     rx_ifnet_t ifn;
683 #endif
684
685     memset(addrs, 0, sizeof(addrs));
686     memset(mtus, 0, sizeof(mtus));
687
688 #if defined(AFS_DARWIN80_ENV)
689     if (!ifnet_list_get(AF_INET, &ifns, &count)) {
690         for (m = 0; m < count; m++) {
691             if (!ifnet_get_address_list(ifns[m], &ifads)) {
692                 for (j = 0; ifads[j] != NULL && cnt < ADDRSPERSITE; j++) {
693                     if ((t = ifaddr_address(ifads[j], &sout, sizeof(struct sockaddr))) == 0) {
694                         sin = (struct sockaddr_in *)&sout;
695                         rxmtu = rx_ifnet_mtu(rx_ifaddr_ifnet(ifads[j])) - RX_IPUDP_SIZE;
696                         ifinaddr = ntohl(sin->sin_addr.s_addr);
697                         if (myNetAddrs[i] != ifinaddr) {
698                             different++;
699                         }
700                         mtus[i] = rxmtu;
701                         rxmtu = rxi_AdjustIfMTU(rxmtu);
702                         maxmtu =
703                             rxmtu * rxi_nRecvFrags +
704                             ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
705                         maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
706                         addrs[i++] = ifinaddr;
707                         if ((ifinaddr != 0x7f000001) && 
708                             (maxmtu > rx_maxReceiveSize)) {
709                             rx_maxReceiveSize = 
710                                 MIN(RX_MAX_PACKET_SIZE, maxmtu);
711                             rx_maxReceiveSize =
712                                 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
713                         }
714                         cnt++;
715                     }
716                 }
717                 ifnet_free_address_list(ifads);
718             }
719         }
720         ifnet_list_free(ifns);
721     }
722 #else
723 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
724     TAILQ_FOREACH(ifn, &ifnet, if_link) {
725         if (i >= ADDRSPERSITE)
726             break;
727 #elif defined(AFS_OBSD_ENV)
728     for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
729          ifn = ifn->if_list.tqe_next) {
730 #else
731     for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
732 #endif
733         rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
734 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
735         TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
736             if (i >= ADDRSPERSITE)
737                 break;
738 #elif defined(AFS_OBSD_ENV)
739         for (ifad = ifn->if_addrlist.tqh_first;
740              ifad != NULL && i < ADDRSPERSITE;
741              ifad = ifad->ifa_list.tqe_next) {
742 #else
743         for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
744              ifad = ifad->ifa_next) {
745 #endif
746             if (IFADDR2SA(ifad)->sa_family == AF_INET) {
747                 ifinaddr =
748                     ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
749                           s_addr);
750                 if (myNetAddrs[i] != ifinaddr) {
751                     different++;
752                 }
753                 mtus[i] = rxmtu;
754                 rxmtu = rxi_AdjustIfMTU(rxmtu);
755                 maxmtu =
756                     rxmtu * rxi_nRecvFrags +
757                     ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
758                 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
759                 addrs[i++] = ifinaddr;
760                 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
761                     rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
762                     rx_maxReceiveSize =
763                         MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
764                 }
765             }
766         }
767     }
768 #endif
769
770     rx_maxJumboRecvSize =
771         RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
772         (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
773     rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
774
775     if (different) {
776         int l;
777         for (l = 0; l < i; l++) {
778             myNetMTUs[l] = mtus[l];
779             myNetAddrs[l] = addrs[l];
780         }
781     }
782     return different;
783 }
784
785 #if defined(AFS_DARWIN60_ENV) || defined(AFS_XBSD_ENV)
786 /* Returns ifnet which best matches address */
787 rx_ifnet_t
788 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
789 {
790     struct sockaddr_in s, sr;
791     rx_ifaddr_t ifad;
792
793     s.sin_family = AF_INET;
794     s.sin_addr.s_addr = addr;
795     ifad = rx_ifaddr_withnet((struct sockaddr *)&s);
796
797     if (ifad && maskp) {
798         rx_ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
799         *maskp = sr.sin_addr.s_addr;
800     }
801     return (ifad ? rx_ifaddr_ifnet(ifad) : NULL);
802 }
803
804 #else /* DARWIN60 || XBSD */
805
806 /* Returns ifnet which best matches address */
807 rx_ifnet_t
808 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
809 {
810     int match_value = 0;
811     extern struct in_ifaddr *in_ifaddr;
812     struct in_ifaddr *ifa, *ifad = NULL;
813
814     addr = ntohl(addr);
815
816 #if defined(AFS_DARWIN_ENV)
817     for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
818          ifa = TAILQ_NEXT(ifa, ia_link)) {
819 #else
820     for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
821 #endif
822         if ((addr & ifa->ia_netmask) == ifa->ia_net) {
823             if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
824                 if (IA_SIN(ifa)->sin_addr.s_addr == addr) {     /* ie, ME!!!  */
825                     match_value = 4;
826                     ifad = ifa;
827                     goto done;
828                 }
829                 if (match_value < 3) {
830                     ifad = ifa;
831                     match_value = 3;
832                 }
833             } else {
834                 if (match_value < 2) {
835                     ifad = ifa;
836                     match_value = 2;
837                 }
838             }
839         }                       /* if net matches */
840     }                           /* for all in_ifaddrs */
841
842   done:
843     if (ifad && maskp)
844         *maskp = ifad->ia_subnetmask;
845     return (ifad ? ifad->ia_ifp : NULL);
846 }
847 #endif /* else DARWIN60 || XBSD */
848 #endif /* else AFS_USERSPACE_IP_ADDR */
849 #endif /* !SUN5 && !SGI62 */
850
851
852 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
853  * afs_osinet.c. One could argue that rxi_NewSocket could go into the
854  * system specific subdirectories for all systems. But for the moment,
855  * most of it is simple to follow common code.
856  */
857 #if !defined(UKERNEL)
858 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
859 /* rxk_NewSocket creates a new socket on the specified port. The port is
860  * in network byte order.
861  */
862 osi_socket *
863 rxk_NewSocketHost(afs_uint32 ahost, short aport)
864 {
865     afs_int32 code;
866 #ifdef AFS_DARWIN80_ENV
867     socket_t newSocket;
868 #else
869     struct socket *newSocket;
870 #endif
871 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD50_ENV))
872     struct mbuf *nam;
873 #endif
874     struct sockaddr_in myaddr;
875 #ifdef AFS_HPUX110_ENV
876     /* prototype copied from kernel source file streams/str_proto.h */
877     extern MBLKP allocb_wait(int, int);
878     MBLKP bindnam;
879     int addrsize = sizeof(struct sockaddr_in);
880     struct file *fp;
881     extern struct fileops socketops;
882 #endif
883 #ifdef AFS_SGI65_ENV
884     bhv_desc_t bhv;
885 #endif
886
887     AFS_STATCNT(osi_NewSocket);
888 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
889     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
890 #endif
891     AFS_ASSERT_GLOCK();
892     AFS_GUNLOCK();
893 #if     defined(AFS_HPUX102_ENV)
894 #if     defined(AFS_HPUX110_ENV)
895     /* we need a file associated with the socket so sosend in NetSend 
896      * will not fail */
897     /* blocking socket */
898     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
899     fp = falloc();
900     if (!fp)
901         goto bad;
902     fp->f_flag = FREAD | FWRITE;
903     fp->f_type = DTYPE_SOCKET;
904     fp->f_ops = &socketops;
905
906     fp->f_data = (void *)newSocket;
907     newSocket->so_fp = (void *)fp;
908
909 #else /* AFS_HPUX110_ENV */
910     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
911 #endif /* else AFS_HPUX110_ENV */
912 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
913     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
914 #elif defined(AFS_FBSD50_ENV)
915     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
916                     afs_osi_credp, curthread);
917 #elif defined(AFS_FBSD40_ENV)
918     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP, curproc);
919 #elif defined(AFS_DARWIN80_ENV)
920     code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
921 #else
922     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
923 #endif /* AFS_HPUX102_ENV */
924     if (code)
925         goto bad;
926
927     memset(&myaddr, 0, sizeof myaddr);
928     myaddr.sin_family = AF_INET;
929     myaddr.sin_port = aport;
930     myaddr.sin_addr.s_addr = ahost;
931 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
932     myaddr.sin_len = sizeof(myaddr);
933 #endif
934
935 #ifdef AFS_HPUX110_ENV
936     bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
937     if (!bindnam) {
938         setuerror(ENOBUFS);
939         goto bad;
940     }
941     memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
942            addrsize);
943     bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
944
945     code = sobind(newSocket, bindnam, addrsize);
946     if (code) {
947         soclose(newSocket);
948 #if !defined(AFS_HPUX1122_ENV)
949         m_freem(nam);
950 #endif
951         goto bad;
952     }
953
954     freeb(bindnam);
955 #else /* AFS_HPUX110_ENV */
956 #if defined(AFS_DARWIN80_ENV)
957     { 
958        int buflen = 50000;
959        int i,code2;
960        for (i=0;i<2;i++) {
961            code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
962                                   &buflen, sizeof(buflen));
963            code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
964                                   &buflen, sizeof(buflen));
965            if (!code && !code2)
966                break;
967            if (i == 2)
968               osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
969            buflen = 32766;
970        }
971     }
972 #else
973     code = soreserve(newSocket, 50000, 50000);
974     if (code) {
975         code = soreserve(newSocket, 32766, 32766);
976         if (code)
977             osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
978     }
979 #endif
980 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
981 #if defined(AFS_FBSD50_ENV)
982     code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
983 #elif defined(AFS_FBSD40_ENV)
984     code = sobind(newSocket, (struct sockaddr *)&myaddr, curproc);
985 #else
986     code = sobind(newSocket, (struct sockaddr *)&myaddr);
987 #endif
988     if (code) {
989         dpf(("sobind fails (%d)\n", (int)code));
990         soclose(newSocket);
991         AFS_GLOCK();
992         goto bad;
993     }
994 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
995 #ifdef  AFS_OSF_ENV
996     nam = m_getclr(M_WAIT, MT_SONAME);
997 #else /* AFS_OSF_ENV */
998     nam = m_get(M_WAIT, MT_SONAME);
999 #endif
1000     if (nam == NULL) {
1001 #if defined(KERNEL_HAVE_UERROR)
1002         setuerror(ENOBUFS);
1003 #endif
1004         goto bad;
1005     }
1006     nam->m_len = sizeof(myaddr);
1007     memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
1008 #if defined(AFS_SGI65_ENV)
1009     BHV_PDATA(&bhv) = (void *)newSocket;
1010     code = sobind(&bhv, nam);
1011     m_freem(nam);
1012 #elif defined(AFS_OBSD44_ENV)
1013     code = sobind(newSocket, nam, osi_curproc());
1014 #else
1015     code = sobind(newSocket, nam);
1016 #endif
1017     if (code) {
1018         dpf(("sobind fails (%d)\n", (int)code));
1019         soclose(newSocket);
1020 #ifndef AFS_SGI65_ENV
1021         m_freem(nam);
1022 #endif
1023         goto bad;
1024     }
1025 #endif /* else AFS_DARWIN_ENV */
1026 #endif /* else AFS_HPUX110_ENV */
1027
1028     AFS_GLOCK();
1029 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1030     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1031 #endif
1032     return (osi_socket *)newSocket;
1033
1034   bad:
1035     AFS_GLOCK();
1036 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1037     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1038 #endif
1039     return (osi_socket *)0;
1040 }
1041
1042 osi_socket *
1043 rxk_NewSocket(short aport)
1044 {
1045     return rxk_NewSocketHost(0, aport);
1046 }
1047
1048 /* free socket allocated by rxk_NewSocket */
1049 int
1050 rxk_FreeSocket(struct socket *asocket)
1051 {
1052     AFS_STATCNT(osi_FreeSocket);
1053 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1054     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1055 #endif
1056 #ifdef AFS_HPUX110_ENV
1057     if (asocket->so_fp) {
1058         struct file *fp = asocket->so_fp;
1059 #if !defined(AFS_HPUX1123_ENV)
1060         /* 11.23 still has falloc, but not FPENTRYFREE ! 
1061          * so for now if we shutdown, we will waist a file 
1062          * structure */
1063         FPENTRYFREE(fp);
1064         asocket->so_fp = NULL;
1065 #endif
1066     }
1067 #endif /* AFS_HPUX110_ENV */
1068     soclose(asocket);
1069 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1070     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1071 #endif
1072     return 0;
1073 }
1074 #endif /* !SUN5 && !LINUX20 */
1075
1076 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
1077 #ifdef AFS_DARWIN80_ENV
1078 /* Shutting down should wake us up, as should an earlier event. */
1079 void
1080 rxi_ReScheduleEvents(void)
1081 {
1082     /* needed to allow startup */
1083     int glock = ISAFS_GLOCK();
1084     if (!glock)
1085         AFS_GLOCK();
1086     osi_rxWakeup(&afs_termState);
1087     if (!glock)
1088         AFS_GUNLOCK();
1089 }
1090 #endif
1091 /*
1092  * Run RX event daemon every second (5 times faster than rest of systems)
1093  */
1094 void
1095 afs_rxevent_daemon(void)
1096 {
1097     struct clock temp;
1098     SPLVAR;
1099
1100     while (1) {
1101 #ifdef RX_ENABLE_LOCKS
1102         AFS_GUNLOCK();
1103 #endif /* RX_ENABLE_LOCKS */
1104         NETPRI;
1105         rxevent_RaiseEvents(&temp);
1106         USERPRI;
1107 #ifdef RX_ENABLE_LOCKS
1108         AFS_GLOCK();
1109 #endif /* RX_ENABLE_LOCKS */
1110 #ifdef RX_KERNEL_TRACE
1111         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1112                    "before afs_osi_Wait()");
1113 #endif
1114 #ifdef AFS_DARWIN80_ENV
1115         afs_osi_TimedSleep(&afs_termState, ((temp.sec * 1000) +
1116                                             (temp.usec / 1000)), 0);
1117 #else
1118         afs_osi_Wait(500, NULL, 0);
1119 #endif
1120 #ifdef RX_KERNEL_TRACE
1121         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1122                    "after afs_osi_Wait()");
1123 #endif
1124         if (afs_termState == AFSOP_STOP_RXEVENT) {
1125 #ifdef RXK_LISTENER_ENV
1126             afs_termState = AFSOP_STOP_RXK_LISTENER;
1127 #else
1128 #ifdef AFS_SUN510_ENV
1129             afs_termState = AFSOP_STOP_NETIF;
1130 #else
1131             afs_termState = AFSOP_STOP_COMPLETE;
1132 #endif
1133 #endif
1134             osi_rxWakeup(&afs_termState);
1135             return;
1136         }
1137     }
1138 }
1139 #endif
1140
1141 #ifdef RXK_LISTENER_ENV
1142
1143 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1144 int
1145 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1146 {
1147     int code;
1148     struct sockaddr_in from;
1149     int nbytes;
1150     afs_int32 rlen;
1151     afs_int32 tlen;
1152     afs_int32 savelen;          /* was using rlen but had aliasing problems */
1153     rx_computelen(p, tlen);
1154     rx_SetDataSize(p, tlen);    /* this is the size of the user data area */
1155
1156     tlen += RX_HEADER_SIZE;     /* now this is the size of the entire packet */
1157     rlen = rx_maxJumboRecvSize; /* this is what I am advertising.  Only check
1158                                  * it once in order to avoid races.  */
1159     tlen = rlen - tlen;
1160     if (tlen > 0) {
1161         tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1162         if (tlen > 0) {
1163             tlen = rlen - tlen;
1164         } else
1165             tlen = rlen;
1166     } else
1167         tlen = rlen;
1168
1169     /* add some padding to the last iovec, it's just to make sure that the 
1170      * read doesn't return more data than we expect, and is done to get around
1171      * our problems caused by the lack of a length field in the rx header. */
1172     savelen = p->wirevec[p->niovecs - 1].iov_len;
1173     p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1174
1175     nbytes = tlen + sizeof(afs_int32);
1176 #ifdef RX_KERNEL_TRACE
1177     if (ICL_SETACTIVE(afs_iclSetp)) {
1178         AFS_GLOCK();
1179         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1180                    "before osi_NetRecive()");
1181         AFS_GUNLOCK();
1182     }
1183 #endif
1184     code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1185
1186 #ifdef RX_KERNEL_TRACE
1187     if (ICL_SETACTIVE(afs_iclSetp)) {
1188         AFS_GLOCK();
1189         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1190                    "after osi_NetRecive()");
1191         AFS_GUNLOCK();
1192     }
1193 #endif
1194     /* restore the vec to its correct state */
1195     p->wirevec[p->niovecs - 1].iov_len = savelen;
1196
1197     if (!code) {
1198         p->length = nbytes - RX_HEADER_SIZE;;
1199         if ((nbytes > tlen) || (p->length & 0x8000)) {  /* Bogus packet */
1200             if (nbytes <= 0) {
1201                 if (rx_stats_active) {
1202                     MUTEX_ENTER(&rx_stats_mutex);
1203                     rx_stats.bogusPacketOnRead++;
1204                     rx_stats.bogusHost = from.sin_addr.s_addr;
1205                     MUTEX_EXIT(&rx_stats_mutex);
1206                 }
1207                 dpf(("B: bogus packet from [%x,%d] nb=%d",
1208                      from.sin_addr.s_addr, from.sin_port, nbytes));
1209             }
1210             return -1;
1211         } else {
1212             /* Extract packet header. */
1213             rxi_DecodePacketHeader(p);
1214
1215             *host = from.sin_addr.s_addr;
1216             *port = from.sin_port;
1217             if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1218                 if (rx_stats_active) {
1219                     MUTEX_ENTER(&rx_stats_mutex);
1220                     rx_stats.packetsRead[p->header.type - 1]++;
1221                     MUTEX_EXIT(&rx_stats_mutex);
1222                 }
1223             }
1224
1225 #ifdef RX_TRIMDATABUFS
1226             /* Free any empty packet buffers at the end of this packet */
1227             rxi_TrimDataBufs(p, 1);
1228 #endif
1229             return 0;
1230         }
1231     } else
1232         return code;
1233 }
1234
1235 /* rxk_Listener() 
1236  *
1237  * Listen for packets on socket. This thread is typically started after
1238  * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1239  * the start state is set before proceeding.
1240  *
1241  * Note that this thread is outside the AFS global lock for much of
1242  * it's existence.
1243  *
1244  * In many OS's, the socket receive code sleeps interruptibly. That's not what
1245  * we want here. So we need to either block all signals (including SIGKILL
1246  * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1247  * OS's socket receive routine returns as a result of a signal.
1248  */
1249 int rxk_ListenerPid;            /* Used to signal process to wakeup at shutdown */
1250 #ifdef AFS_LINUX20_ENV
1251 struct task_struct *rxk_ListenerTask;
1252 #endif
1253
1254 #ifdef AFS_SUN5_ENV
1255 /*
1256  * Run the listener as a kernel thread.
1257  */
1258 void
1259 rxk_Listener(void)
1260 {
1261     extern id_t syscid;
1262     void rxk_ListenerProc(void);
1263     if (thread_create
1264         (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1265          minclsyspri) == NULL)
1266         osi_Panic("rxk_Listener: failed to start listener thread!\n");
1267 }
1268
1269 void
1270 rxk_ListenerProc(void)
1271 #else /* AFS_SUN5_ENV */
1272 void
1273 rxk_Listener(void)
1274 #endif                          /* AFS_SUN5_ENV */
1275 {
1276     struct rx_packet *rxp = NULL;
1277     int code;
1278     int host, port;
1279
1280 #ifdef AFS_LINUX20_ENV
1281     rxk_ListenerPid = current->pid;
1282     rxk_ListenerTask = current;
1283 #endif
1284 #ifdef AFS_SUN5_ENV
1285     rxk_ListenerPid = 1;        /* No PID, just a flag that we're alive */
1286 #endif /* AFS_SUN5_ENV */
1287 #ifdef AFS_XBSD_ENV
1288     rxk_ListenerPid = curproc->p_pid;
1289 #endif /* AFS_FBSD_ENV */
1290 #ifdef AFS_DARWIN80_ENV
1291     rxk_ListenerPid = proc_selfpid();
1292 #elif defined(AFS_DARWIN_ENV)
1293     rxk_ListenerPid = current_proc()->p_pid;
1294 #endif
1295 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1296     AFS_GUNLOCK();
1297 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1298     while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1299         if (rxp) {
1300             rxi_RestoreDataBufs(rxp);
1301         } else {
1302             rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1303             if (!rxp)
1304                 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1305         }
1306         if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1307             rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1308         }
1309     }
1310
1311 #ifdef RX_ENABLE_LOCKS
1312     AFS_GLOCK();
1313 #endif /* RX_ENABLE_LOCKS */
1314     if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1315 #ifdef AFS_SUN510_ENV
1316         afs_termState = AFSOP_STOP_NETIF;
1317 #else
1318         afs_termState = AFSOP_STOP_COMPLETE;
1319 #endif
1320         osi_rxWakeup(&afs_termState);
1321     }
1322     rxk_ListenerPid = 0;
1323 #ifdef AFS_LINUX20_ENV
1324     rxk_ListenerTask = 0;
1325     osi_rxWakeup(&rxk_ListenerTask);
1326 #endif
1327 #if defined(AFS_SUN5_ENV)
1328     osi_rxWakeup(&rxk_ListenerPid);
1329 #endif
1330 #ifdef AFS_SUN5_ENV
1331     AFS_GUNLOCK();
1332 #endif /* AFS_SUN5_ENV */
1333 }
1334
1335 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1336 /* The manner of stopping the rx listener thread may vary. Most unix's should
1337  * be able to call soclose.
1338  */
1339 void
1340 osi_StopListener(void)
1341 {
1342     soclose(rx_socket);
1343 }
1344 #endif
1345 #endif /* RXK_LISTENER_ENV */
1346
1347 #endif /* !NCR && !UKERNEL */