home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / sps / part02 / waitingfor.c < prev   
Encoding:
C/C++ Source or Header  |  1987-07-21  |  6.4 KB  |  223 lines

  1. # include       "sps.h"
  2. # include       <h/text.h>
  3. # ifdef NFS
  4. # include       <h/vnode.h>
  5. # include       <ufs/inode.h>
  6. # else
  7. # include       <h/inode.h>
  8. # endif NFS
  9. # include       <h/ioctl.h>
  10. # include       <h/tty.h>
  11. # include       <h/buf.h>
  12. # ifdef BSD42
  13. # ifndef NFS
  14. # include       <h/quota.h>
  15. # endif NFS
  16. # include       <h/mbuf.h>
  17. # include       <h/socket.h>
  18. # include       <h/socketvar.h>
  19. # endif BSD42
  20.  
  21. /* 1 if `w' is in the address range defined by `a1' and `a2' ... */
  22. # define        INRANGE( w, a1, a2 ) \
  23.             ( (caddr_t)(a1) <= (w) && (w) < (caddr_t)(a2) )
  24.  
  25. /* NFS changes incorporated by Alexander Dupuy <dupuy@amsterdam.columbia.edu> */
  26.  
  27. /* WAITINGFOR - Determine what a process is waiting for and describe it. */
  28. char    *waitingfor ( p )
  29.  
  30. struct process                  *p ;
  31.  
  32. {
  33.     register caddr_t        w ;
  34.     register struct ttyline *lp ;
  35.     register struct symbol  *s ;
  36.     register char           *cp ;
  37. # ifdef BSD42
  38.     struct socket           sc ;
  39. # endif
  40.     int            rc ;
  41.     static char             wbuf[ 8 ] ;
  42.     extern struct info      Info ;
  43.     extern struct symbol    Symbollist[] ;
  44.     char                    *sprintf() ;
  45.  
  46.     w = p->pr_p.p_wchan ;
  47.     if ( !w )
  48.         return ( "null" ) ;
  49.     /* Waiting for a child process, alternatively in a vfork() ? */
  50.     if ( INRANGE( w, Info.i_proc0, &Info.i_proc0[ Info.i_nproc ] ) )
  51.         return ( p->pr_p.p_flag & SNOVM ? "vfork" : "child" ) ;
  52.     /* Waiting for a page to be brought in ? */
  53.     if ( INRANGE( w, Info.i_swbuf0, &Info.i_swbuf0[ Info.i_nswbuf ] ) )
  54.         return ( "swap" ) ;
  55.     /* Waiting for discio through a block device to complete ? */
  56.     if ( INRANGE( w, Info.i_buf0, &Info.i_buf0[ Info.i_nbuf ] ) )
  57.         /* SHOULD ACTUALLY READ AS "blkio" BUT "discio" IS WHAT
  58.            IS GENERALLY MEANT HERE. */
  59.         return ( "discio" ) ;
  60.     /* Waiting for a text page to be brought in ? */
  61.     if ( INRANGE( w, Info.i_text0, &Info.i_text0[ Info.i_ntext ] ) )
  62.         return ( "swtext" ) ;
  63. # ifdef BSD42
  64. # ifndef NFS
  65.     /* Waiting for an event associated with the quota system ? */
  66.     if ( INRANGE( w, Info.i_quota0, &Info.i_quota0[ Info.i_nquota ] ) )
  67.         return ( "quota" ) ;
  68. # endif NFS
  69. # endif BSD42
  70.     /* Waiting for tty I/O ? If so, find which tty it is */
  71.     for ( lp = Info.i_ttyline ; lp->l_name[0] ; lp++ )
  72.         if ( INRANGE( w, &lp->l_addr[0], &lp->l_addr[1] ) )
  73.         {
  74.             switch ( (int)w - (int)lp->l_addr )
  75.             {
  76.                 case (int)&((struct tty*)0)->t_rawq :
  77.                     /* Read from a tty or slave pty */
  78.                     cp = "rtty??" ;
  79.                     break ;
  80.                 case (int)&((struct tty*)0)->t_outq :
  81.                     /* Write to a tty or slave pty */
  82.                     cp = "wtty??" ;
  83.                     break ;
  84.                 case (int)&((struct tty*)0)->t_state :
  85.                     /* Tty not open */
  86.                     cp = "otty??" ;
  87.                     break ;
  88.                 case (int)&((struct tty*)0)->t_outq.c_cf :
  89.                     /* Read from a controller pty */
  90.                     cp = "rpty??" ;
  91.                     break ;
  92.                 case (int)&((struct tty*)0)->t_rawq.c_cf :
  93.                     /* Write to a controller pty */
  94.                     cp = "wpty??" ;
  95.                     break ;
  96.                 default :
  97.                     cp = "?tty??" ;
  98.                     break ;
  99.             }
  100.             cp[4] = lp->l_name[0] ;
  101.             cp[5] = lp->l_name[1] ;
  102.             return ( cp ) ;
  103.         }
  104.     /* Waiting for an inode ? */
  105.     if ( INRANGE( w, Info.i_inode0, &Info.i_inode0[ Info.i_ninode ] ) )
  106.         switch ( ((int)w - (int)Info.i_inode0) % sizeof( struct inode ))
  107.         {
  108. # ifdef BSD42
  109. # ifndef NFS
  110.             case (int)&((struct inode*)0)->i_exlockc :
  111.                 /* Exclusive lock on this inode */
  112.                 return ( "exlock" ) ;
  113.             case (int)&((struct inode*)0)->i_shlockc :
  114.                 /* Shared lock on this inode */
  115.                 return ( "shlock" ) ;
  116. # endif NFS
  117. # else
  118.             case 1 :
  119.                 return ( "wpipe" ) ;
  120.             case 2 :
  121.                 return ( "rpipe" ) ;
  122.             case (int)&((struct inode*)0)->i_un.i_group.g_datq :
  123.                 return ( "rmux" ) ;
  124. # endif BSD42
  125.             default :
  126.                 /* Inode probably locked */
  127.                 return ( "inode" ) ;
  128.         }
  129. # ifdef BSD42
  130.     /* Waiting for a structure inside an mbuf ? If so, try to find why */
  131.     if ( INRANGE( w, Info.i_mbutl,
  132.     &Info.i_mbutl[ NMBCLUSTERS * CLBYTES / sizeof( struct mbuf ) ] ) )
  133.         switch ( ((int)w - (int)Info.i_mbutl) % sizeof( struct mbuf )
  134.             - (int)&((struct mbuf*)0)->m_dat[0] )
  135.         {
  136.             case (int)&((struct socket*)0)->so_timeo :
  137.                 /* Socket timeout event - Guess why */
  138.                 rc = getsocket( (struct socket*)(w
  139.                     - (int)&((struct socket*)0)->so_timeo),
  140.                         &sc ) ;
  141.                 return ( rc && (sc.so_state & SS_ISCONNECTING)
  142.                     ? "connct" 
  143.                     : rc && ((sc.so_options & SO_ACCEPTCONN)
  144.                       && !sc.so_qlen)
  145.                     ? "accept" : "socket" ) ;
  146.             case (int)&((struct socket*)0)->so_rcv.sb_cc :
  147.                 /* Read from an empty socket. Here we actually
  148.                    attempt to determine whether the socket
  149.                    structure in question really does refer to
  150.                    a socket, or whether it is in fact a pipe
  151.                    in disguise. */
  152.                 return ( getsocket( (struct socket*)(w
  153.                     - (int)&((struct socket*)0)->so_rcv.sb_cc),
  154.                         &sc )
  155.                     && sc.so_type == SOCK_STREAM
  156. #ifdef BSD43
  157.                     && ((sc.so_state
  158.                         & (SS_ISCONNECTED|SS_CANTSENDMORE))
  159.                         == (SS_ISCONNECTED|SS_CANTSENDMORE))
  160. #else
  161.                     && !sc.so_rcv.sb_hiwat
  162.                     && !sc.so_rcv.sb_mbmax
  163.                     && (sc.so_state
  164.                         & (SS_ISCONNECTED|SS_CANTRCVMORE))
  165. #endif BSD43
  166.                     ? "rpipe" : "rsockt" ) ;
  167.             case (int)&((struct socket*)0)->so_snd.sb_cc :
  168.                 /* Write to a full socket. Again, we try
  169.                    to determine whether or not this is a
  170.                    real socket or a pipe. */
  171.                 return ( getsocket( (struct socket*)(w
  172.                     - (int)&((struct socket*)0)->so_snd.sb_cc),
  173.                         &sc )
  174. #ifdef BSD43
  175.                     && sc.so_type == SOCK_STREAM
  176.                     && ((sc.so_state
  177.                         & (SS_ISCONNECTED|SS_CANTRCVMORE))
  178.                         == (SS_ISCONNECTED|SS_CANTRCVMORE))
  179. #else
  180.                     && sc.so_rcv.sb_hiwat == 2048
  181.                     && sc.so_rcv.sb_mbmax == 4096
  182.                     && (sc.so_state
  183.                         & (SS_ISCONNECTED|SS_CANTSENDMORE))
  184. #endif BSD43
  185.                     ? "wpipe" : "wsockt" ) ;
  186.             default :
  187.                 /* Other mbuf event */
  188.                 return ( "mbuf" ) ;
  189.         }
  190. # endif BSD42
  191.     /* Look in the symbol table for known wait addresses. */
  192.     for ( s = Symbollist ; s->s_kname ; s++ )
  193.         if ( s->s_wait && w == *s->s_info )
  194.             return ( s->s_wait ) ;  
  195.     /* No reason for the wait state has been found.
  196.        Return the wait channel as a hexadecimal address. */
  197. # ifdef SUN
  198.     (void)sprintf( wbuf, "x%05x", w ) ;
  199. # else
  200.     (void)sprintf( wbuf, "x%05x", w - 0x80000000 ) ;
  201. # endif SUN
  202.     return ( wbuf ) ;
  203. }
  204.  
  205. # ifdef BSD42
  206. /*
  207. ** GETSOCKET - Reads a `struct socket' from the kernel virtual memory address
  208. ** identified by `ks' into the buffer `s'.
  209. */
  210. getsocket ( ks, s )
  211.  
  212. struct socket                   *ks ;
  213. struct socket                   *s ;
  214.  
  215. {
  216.     extern int              Flkmem ;
  217.  
  218.     memseek( Flkmem, (long)ks ) ;
  219.     return ( read( Flkmem, (char*)s, sizeof( struct socket ) )
  220.         == sizeof( struct socket ) ) ;
  221. }
  222. # endif BSD42
  223.