openbsd-20021011
[openafs.git] / src / rx / rx_kcommon.c
index b888d8f..c306672 100644 (file)
@@ -21,18 +21,24 @@ RCSID("$Header$");
 #ifdef AFS_HPUX110_ENV
 #include "../h/tihdr.h"
 #include <xti.h>
-#include "../afs/hpux_110.h"
 #endif
 #include "../afsint/afsint.h"
 
-afs_int32 rxi_Findcbi();
-extern  struct interfaceAddr afs_cb_interface;
+#ifdef AFS_DARWIN60_ENV
+struct ifnet *rxi_FindIfnet(afs_uint32 addr, struct ifaddr **pifad);
+#else
+struct ifnet *rxi_FindIfnet(afs_uint32 addr, struct in_ifaddr **pifad);
+#endif
 
 #ifndef RXK_LISTENER_ENV
-int (*rxk_GetPacketProc)(); /* set to packet allocation procedure */
-int (*rxk_PacketArrivalProc)();
+int (*rxk_PacketArrivalProc)(register struct rx_packet *ahandle,
+        register struct sockaddr_in *afrom, char *arock,
+        afs_int32 asize); /* set to packet allocation procedure */
+int (*rxk_GetPacketProc)(char **ahandle, int asize);
 #endif
 
+extern  struct interfaceAddr afs_cb_interface;
+
 rxk_ports_t rxk_ports;
 rxk_portRocks_t rxk_portRocks;
 
@@ -61,8 +67,8 @@ static int rxk_AddPort(u_short aport, char * arock)
 }
 
 /* remove as port from the monitored list, port # is in network order */
-rxk_DelPort(aport)
-u_short aport; {
+int rxk_DelPort(u_short aport)
+{
     register int i;
     register unsigned short *tsp;
 
@@ -86,13 +92,12 @@ void rxk_shutdownPorts(void)
 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
            soclose((struct socket *)rxk_portRocks[i]);
 #endif
-           rxk_portRocks[i] = (char *)0;
+           rxk_portRocks[i] = NULL;
        }
     }
 }
 
-osi_socket rxi_GetUDPSocket(port)
-    u_short port;
+osi_socket rxi_GetUDPSocket(u_short port)
 {
     struct osi_socket *sockp;
     sockp = (struct osi_socket *) rxk_NewSocket(port);
@@ -123,8 +128,7 @@ char *msg;
  * defined only on success.
  */
 
-int
-osi_utoa(char *buf, size_t len, unsigned long val)
+int osi_utoa(char *buf, size_t len, unsigned long val)
 {
        long k; /* index of first byte of string value */
 
@@ -197,8 +201,7 @@ osi_utoa(char *buf, size_t len, unsigned long val)
  * argument to the native panic(), we construct a single string and hand
  * that to osi_Panic().
  */
-void
-osi_AssertFailK(const char *expr, const char *file, int line)
+void osi_AssertFailK(const char *expr, const char *file, int line)
 {
        static const char msg0[] = "assertion failed: ";
        static const char msg1[] = ", file: ";
@@ -242,7 +245,7 @@ osi_AssertFailK(const char *expr, const char *file, int line)
 #ifndef UKERNEL
 /* This is the server process request loop. Kernel server
  * processes never become listener threads */
-void rx_ServerProc()
+void rx_ServerProc(void)
 {
     int threadID;
 
@@ -263,9 +266,8 @@ void rx_ServerProc()
 #endif /* !UKERNEL */
 
 #ifndef RXK_LISTENER_ENV
-static int MyPacketProc(ahandle, asize)
-int asize;   /* this includes the Rx header */
-char **ahandle;
+/* asize includes the Rx header */
+static int MyPacketProc(char **ahandle, int asize)
 {
     register struct rx_packet *tp;
 
@@ -306,11 +308,10 @@ char **ahandle;
     return 0;
 }
 
-static int MyArrivalProc(ahandle, afrom, arock, asize)
-register struct rx_packet *ahandle;
-register struct sockaddr_in *afrom;
-char *arock;
-afs_int32 asize; {
+static int MyArrivalProc(register struct rx_packet *ahandle, 
+       register struct sockaddr_in *afrom, char *arock, 
+       afs_int32 asize)
+{
     /* handle basic rx packet */
     ahandle->length = asize - RX_HEADER_SIZE;
     rxi_DecodePacketHeader(ahandle);
@@ -324,8 +325,8 @@ afs_int32 asize; {
 }
 #endif /* !RXK_LISTENER_ENV */
 
-void
-rxi_StartListener() {
+void rxi_StartListener(void)
+{
     /* if kernel, give name of appropriate procedures */
 #ifndef RXK_LISTENER_ENV
     rxk_GetPacketProc = MyPacketProc;
@@ -336,8 +337,7 @@ rxi_StartListener() {
 
 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
   to get interesting information. */
-void rxi_InitPeerParams(pp)
-register struct rx_peer *pp;
+void rxi_InitPeerParams(register struct rx_peer *pp)
 {
 #ifdef ADAPT_MTU
     u_short rxmtu;
@@ -368,7 +368,11 @@ register struct rx_peer *pp;
       pp->ifMTU = RX_REMOTE_PACKET_SIZE;
     }
 #else /* AFS_USERSPACE_IP_ADDR */
+#ifdef AFS_DARWIN60_ENV
+    struct ifaddr *ifad = (struct ifaddr *) 0;
+#else
     struct in_ifaddr *ifad = (struct in_ifaddr *) 0;
+#endif
     struct ifnet *ifn;
 
     /* At some time we need to iterate through rxi_FindIfnet() to find the
@@ -481,11 +485,10 @@ void shutdown_rxkernel(void)
 #define ADDRSPERSITE 16
 static afs_uint32 myNetAddrs[ADDRSPERSITE];
 static int myNetMTUs[ADDRSPERSITE];
-static int myNetFlags[ADDRSPERSITE];
 static int numMyNetAddrs = 0;
 
 #ifdef AFS_USERSPACE_IP_ADDR   
-int rxi_GetcbiInfo()
+int rxi_GetcbiInfo(void)
 {
    int     i, j, different = 0;
    int     rxmtu, maxmtu;
@@ -530,8 +533,7 @@ int rxi_GetcbiInfo()
 /* Returns the afs_cb_interface inxex which best matches address.
  * If none is found, we return -1.
  */
-afs_int32 rxi_Findcbi(addr)
-     afs_uint32 addr;
+afs_int32 rxi_Findcbi(afs_uint32 addr)
 {
    int j;
    afs_uint32 myAddr, thisAddr, netMask, subnetMask;
@@ -571,7 +573,6 @@ afs_int32 rxi_Findcbi(addr)
       }
    }
 
- done:
    return(rvalue);
 }
 
@@ -583,7 +584,7 @@ afs_int32 rxi_Findcbi(addr)
 #define IFADDR2SA(f) ((f)->ifa_addr)
 #endif
 
-int rxi_GetIFInfo()
+int rxi_GetIFInfo(void)
 {
     int i = 0;
     int different = 0;
@@ -598,12 +599,16 @@ int rxi_GetIFInfo()
     memset(addrs, 0, sizeof(addrs));
     memset(mtus, 0, sizeof(mtus));
 
-#if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
+#if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
     TAILQ_FOREACH(ifn, &ifnet, if_link) {
       if (i >= ADDRSPERSITE) break;
-#else 
+#else
+#if defined(AFS_OBSD_ENV)
+    for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL; ifn = ifn->if_list.tqe_next) {
+#else
     for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
 #endif
+#endif
       rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
       TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
@@ -645,12 +650,27 @@ int rxi_GetIFInfo()
     }
    return different;
 }
-
+#ifdef AFS_DARWIN60_ENV
 /* Returns ifnet which best matches address */
 struct ifnet *
 rxi_FindIfnet(addr, pifad) 
      afs_uint32 addr;
-     struct in_ifaddr **pifad;
+     struct ifaddr **pifad;
+{
+  struct sockaddr_in s;
+
+  if (numMyNetAddrs == 0)
+    (void) rxi_GetIFInfo();
+
+  s.sin_family=AF_INET;
+  s.sin_addr.s_addr=addr;
+  *pifad=ifa_ifwithnet((struct sockaddr *)&s);
+ done:
+  return (*pifad ?  (*pifad)->ifa_ifp : NULL );
+}
+#else
+/* Returns ifnet which best matches address */
+struct ifnet *rxi_FindIfnet(afs_uint32 addr, struct in_ifaddr **pifad) 
 {
   afs_uint32 ppaddr;
   int match_value = 0;
@@ -710,6 +730,7 @@ rxi_FindIfnet(addr, pifad)
  done:
   return (*pifad ?  (*pifad)->ia_ifp : NULL );
 }
+#endif
 #endif /* else AFS_USERSPACE_IP_ADDR */
 #endif /* !SUN5 && !SGI62 */
 
@@ -766,6 +787,9 @@ struct osi_socket *rxk_NewSocket(short aport)
     myaddr.sin_family = AF_INET;
     myaddr.sin_port = aport;
     myaddr.sin_addr.s_addr = 0;
+#ifdef STRUCT_SOCKADDR_HAS_SA_LEN
+    myaddr.sin_len = sizeof(myaddr);
+#endif
 
 #ifdef AFS_HPUX110_ENV
     bindnam = allocb_wait((addrsize+SO_MSGOFFSET+1), BPRI_MED);
@@ -792,7 +816,6 @@ struct osi_socket *rxk_NewSocket(short aport)
            osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
     }
 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
-    myaddr.sin_len = sizeof(myaddr);
 #if defined(AFS_XBSD_ENV)
     code = sobind(newSocket, (struct sockaddr *)&myaddr, curproc);
 #else
@@ -853,8 +876,7 @@ bad:
 
 
 /* free socket allocated by rxk_NewSocket */
-int rxk_FreeSocket(asocket)
-    register struct socket *asocket;
+int rxk_FreeSocket(register struct socket *asocket)
 {
     AFS_STATCNT(osi_FreeSocket);
 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
@@ -872,9 +894,8 @@ int rxk_FreeSocket(asocket)
 /*
  * Run RX event daemon every second (5 times faster than rest of systems)
  */
-afs_rxevent_daemon() 
+void afs_rxevent_daemon(void) 
 {
-    int s, code;
     struct clock temp;
     SPLVAR;
 
@@ -890,7 +911,15 @@ afs_rxevent_daemon()
 #ifdef RX_ENABLE_LOCKS
        AFS_GLOCK();
 #endif /* RX_ENABLE_LOCKS */
-       afs_osi_Wait(500, (char *)0, 0);
+#ifdef RX_KERNEL_TRACE
+       afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP,
+                  ICL_TYPE_STRING, "before afs_osi_Wait()");
+#endif
+       afs_osi_Wait(500, NULL, 0);
+#ifdef RX_KERNEL_TRACE
+       afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP,
+                  ICL_TYPE_STRING, "after afs_osi_Wait()");
+#endif
        if (afs_termState == AFSOP_STOP_RXEVENT )
        {
 #ifdef RXK_LISTENER_ENV
@@ -898,7 +927,7 @@ afs_rxevent_daemon()
 #else
                afs_termState = AFSOP_STOP_COMPLETE;
 #endif
-               afs_osi_Wakeup(&afs_termState);
+               osi_rxWakeup(&afs_termState);
                return;
        }
     }
@@ -939,9 +968,21 @@ int rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
     p->wirevec[p->niovecs-1].iov_len = savelen + RX_EXTRABUFFERSIZE;
 
     nbytes = tlen + sizeof(afs_int32);
+#ifdef RX_KERNEL_TRACE
+    AFS_GLOCK();
+    afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP,
+               ICL_TYPE_STRING, "before osi_NetRecive()");
+    AFS_GUNLOCK();
+#endif
     code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs,
                            &nbytes);
 
+#ifdef RX_KERNEL_TRACE
+    AFS_GLOCK();
+    afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP,
+               ICL_TYPE_STRING, "after osi_NetRecive()");
+    AFS_GUNLOCK();
+#endif
    /* restore the vec to its correct state */
     p->wirevec[p->niovecs-1].iov_len = savelen;
 
@@ -1000,14 +1041,15 @@ int rxk_ListenerPid; /* Used to signal process to wakeup at shutdown */
 
 #ifdef AFS_SUN5_ENV
 /*
- * Run the listener as a kernel process.
+ * Run the listener as a kernel thread.
  */
 void rxk_Listener(void)
 {
     extern id_t syscid;
     void rxk_ListenerProc(void);
-    if (newproc(rxk_ListenerProc, syscid, 59))
-       osi_Panic("rxk_Listener: failed to fork listener process!\n");
+    if (thread_create(NULL, DEFAULTSTKSZ, rxk_ListenerProc,
+       0, 0, &p0, TS_RUN, minclsyspri) == NULL)
+       osi_Panic("rxk_Listener: failed to start listener thread!\n");
 }
 
 void rxk_ListenerProc(void)
@@ -1023,9 +1065,12 @@ void rxk_Listener(void)
     rxk_ListenerPid = current->pid;
 #endif
 #ifdef AFS_SUN5_ENV
-    rxk_ListenerPid = curproc->p_pid;
+    rxk_ListenerPid = 1;       /* No PID, just a flag that we're alive */
 #endif /* AFS_SUN5_ENV */
-#if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
+#ifdef AFS_XBSD_ENV
+    rxk_ListenerPid = curproc->p_pid;
+#endif /* AFS_FBSD_ENV */
+#if defined(AFS_DARWIN_ENV)
     rxk_ListenerPid = current_proc()->p_pid;
 #endif
 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
@@ -1043,7 +1088,7 @@ void rxk_Listener(void)
        }
        if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
            AFS_RXGLOCK();
-           rxp = rxi_ReceivePacket(rxp, rx_socket, host, port);
+           rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
            AFS_RXGUNLOCK();
        }
     }
@@ -1053,19 +1098,14 @@ void rxk_Listener(void)
 #endif /* RX_ENABLE_LOCKS */
     if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
        afs_termState = AFSOP_STOP_COMPLETE;
-       afs_osi_Wakeup(&afs_termState);
+       osi_rxWakeup(&afs_termState);
     }
     rxk_ListenerPid = 0;
 #if defined(AFS_LINUX22_ENV) || defined(AFS_SUN5_ENV)
-    afs_osi_Wakeup(&rxk_ListenerPid);
+    osi_rxWakeup(&rxk_ListenerPid);
 #endif
 #ifdef AFS_SUN5_ENV
     AFS_GUNLOCK();
-#ifdef HAVE_P_COREFILE
-    if (!curproc->p_corefile)  /* newproc doesn't set it, but exit frees it */
-       curproc->p_corefile = refstr_alloc("core");
-#endif
-    exit(CLD_EXITED, 0);
 #endif /* AFS_SUN5_ENV */
 }