home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume23 / sps2 / part01 / getcmd.c next >
C/C++ Source or Header  |  1991-01-08  |  7KB  |  269 lines

  1. # ifndef lint
  2. static char SccsId[] =  "@(#)getcmd.c    1.7\t8/6/90" ;
  3. # endif
  4.  
  5. # include       "sps.h"
  6. # include       "flags.h"
  7. # ifdef KVM
  8. #  include      <kvm.h>
  9. #  include      <ctype.h>
  10. # else
  11. #  include      <h/vm.h>
  12. #  ifdef BSD42
  13. #   include    <machine/pte.h>
  14. #  else BSD42
  15. #   include      <h/pte.h>
  16. #  endif BSD42
  17. # endif KVM
  18.  
  19. /*
  20. ** GETCMD - Returns a character string read from a process' upage.
  21. ** This character string should represent the arguments to the current process.
  22. */
  23. char    *getcmd ( p )
  24.  
  25. register struct process         *p ;
  26.  
  27. {
  28. # ifdef KVM
  29.     char                    **ap ;
  30.     char                    *cp ;
  31.     char                    *sp ;
  32.     char                    **argv ;
  33.     char                    **env ;
  34.     extern kvm_t            *Flkvm ;
  35. # else
  36.     register int            *ip ;
  37.     register char           *cp ;
  38.     register char           *cp0 ;
  39.     struct dblock           db ;
  40.     struct pte              ptetbl[ UPAGES + CLSIZE ] ;
  41.     extern int              Flmem, Flswap ;
  42. # endif
  43.     unsigned                nbad ;
  44.     union
  45.     {
  46.         char            a_argc[ CLSIZE * NBPG ] ;
  47.         int             a_argi[ CLSIZE * NBPG / sizeof( int ) ] ;
  48.     } argbuf ;
  49.     extern struct flags     Flg ;
  50.     extern union userstate  User ;
  51.     char                    *strcat(), *strncpy(), *strsave() ;
  52.  
  53.     p->pr_csaved = 0 ;
  54.     p->pr_upag = 0 ;
  55.     if ( p->pr_p.p_stat == SZOMB )
  56.         return ( "** Exit **" ) ;
  57.     if ( !(p->pr_p.p_flag & SLOAD) && Flg.flg_o )
  58.         return ( "** Swapped out **" ) ;
  59.     /* Find the process' upage */
  60. # ifdef KVM
  61.     if ( !getupage( p ) )           
  62. # else
  63.     if ( !getupage( p, ptetbl ) )           
  64. # endif
  65.         return ( "** No upage **" ) ;
  66.     p->pr_upag = 1 ;
  67.     /* Is this a system process ? */
  68.     if ( p->pr_p.p_flag & SSYS )            
  69.         switch ( p->pr_p.p_pid )
  70.         {
  71.             case 0 :
  72.                 return ( "Unix Swapper" ) ;
  73.             case 2 :
  74.                 return ( "Unix Pager" ) ;
  75. # ifdef SUNOS40
  76.             case 3 :
  77.             case 4 :
  78.                 return ( "Unix Idle" ) ;
  79. # endif
  80.             default :
  81.                 break ;
  82.         }
  83. # ifdef DEC3100
  84.     /* Reading the command arguments doesn't work on the DEC 3100 so
  85.        we resort to this kludge until one day it does. */
  86.     if ( 1 )
  87. # else DEC3100
  88.     if ( Flg.flg_c )
  89. # endif DEC3100
  90.     {
  91.         p->pr_csaved = 1 ;
  92.         (void)strncpy( argbuf.a_argc, User.u_us.u_comm,
  93.             sizeof( User.u_us.u_comm ) ) ;
  94.         argbuf.a_argc[ sizeof ( User.u_us.u_comm ) ] = '\0' ;
  95.         return ( strsave( argbuf.a_argc ) ) ;
  96.     }
  97. # ifdef KVM
  98.     if ( kvm_getcmd( Flkvm, &p->pr_p, &User.u_us, &argv,
  99.         Flg.flg_e ? &env : (char ***)NULL ) < 0 || argv == NULL )
  100.             goto getsysargs ;
  101.     p->pr_csaved = 1 ;
  102.     sp = argbuf.a_argc ;
  103.     nbad = 0 ;
  104.     ap = argv ;
  105.     do {
  106.         /* Copy one string from argv or env */
  107.         for ( cp = *ap++; *cp; )
  108.             if ( isprint( *cp ) )
  109.                 *sp++ = *cp++ ;
  110.             else
  111.             {
  112.                 /* Replace control characters with ?'s */
  113.                 if ( ++nbad > 5 )
  114.                 {
  115.                       *sp++ = ' ' ;
  116.                       break ;
  117.                   }
  118.                 *sp++ = '?' ;
  119.                 cp++ ;
  120.             }
  121.         *sp++ = ' ' ;
  122.         /* Check if at end of argv and user wants to see env */
  123.         if ( *ap == 0 && Flg.flg_e && argv != 0 )
  124.         {
  125.             free( (char *) argv ) ;
  126.             argv = NULL ;
  127.             ap = env ;
  128.             if ( ap == NULL )
  129.                 break ;
  130.         }
  131.     } while ( *ap ) ;
  132.      if ( Flg.flg_e )
  133.                 free( (char*)env ) ;
  134.     while ( *--sp == ' ' )
  135.         *sp = '\0' ;
  136.     return ( strsave( argbuf.a_argc ) ) ;
  137. # else
  138.     /* Fix by Alexander Dupuy <dupuy@amsterdam.columbia.edu> */
  139.     /* Check for lack of stack, jack! (Sun 3.0 biod's) */
  140.     if (User.u_us.u_ssize == 0)
  141.         goto getsysargs ;
  142.     /* Look at the top of the upage to locate the command arguments.
  143.        The page is loaded if the process itself is loaded and the pte
  144.        contains is marked as valid. */
  145.     if ( (p->pr_p.p_flag & SLOAD)
  146.     && !ptetbl[0].pg_fod && ptetbl[0].pg_pfnum )
  147.     {       /* If the page is loaded, read the arguments from
  148.            physical memory. */
  149.         memseek( Flmem, (long)ctob( ptetbl[0].pg_pfnum ) ) ;
  150.         if ( read( Flmem, argbuf.a_argc, CLSIZE*NBPG ) != CLSIZE*NBPG )
  151.             return ( "** Memory read error **" ) ;
  152.     }
  153.     else                            
  154.     {       /* Otherwise the page is on the swap device */
  155.         vstodb( 0, ctod( CLSIZE ), &User.u_us.u_smap, &db, 1 ) ;
  156. #  ifdef BSD42
  157.         swseek( (long)dtob( db.db_base ) ) ;
  158. #  else
  159.         swseek( (long)ctob( db.db_base ) ) ;
  160. #  endif
  161.         if ( Flg.flg_o )
  162.             return ( "** Swapped page **" ) ;
  163.         if ( read( Flswap, argbuf.a_argc, CLSIZE*NBPG ) != CLSIZE*NBPG )
  164.             return ( "** Swap device read error **" ) ;
  165.     }
  166.     /* Look down until the end of command arguments is found. */
  167.     ip = &argbuf.a_argi[ CLSIZE*NBPG / sizeof( int ) ] ;
  168.     ip -= 2 ;
  169.     while ( *--ip )
  170.         if ( ip == &argbuf.a_argi[0] )
  171.             goto getsysargs ;
  172.     p->pr_csaved = 1 ;
  173.     /* Process the command arguments, looking for nulls and unprintable
  174.        characters. */
  175.     cp0 = (char*)(ip + 1) ;
  176.     if ( !*cp0 )                    
  177.         cp0++ ;                 
  178.     if ( *cp0 )
  179.     {
  180.         nbad = 0 ;                      
  181.         for ( cp = cp0 ; cp < &argbuf.a_argc[ CLSIZE*NBPG ] ; cp++ )
  182.         {
  183.             *cp &= 0177 ;
  184.             if ( !*cp )             
  185.             {       /* Replace nulls with spaces */
  186.                 *cp = ' ' ;
  187.                 continue ;
  188.             }
  189.             if ( *cp < ' ' || *cp == 0177 )
  190.             {       /* Replace control characters with ?'s */
  191.                 if ( ++nbad > 5 )
  192.                 {
  193.                     *cp++ = ' ' ;
  194.                     break ;
  195.                 }
  196.                 *cp = '?' ;
  197.                 continue ;
  198.             }
  199.             if ( !Flg.flg_e && *cp == '=' )
  200.             {       /* Break on an `=' if we are not interested
  201.                    in the environment strings. */
  202.                 *cp = '\0' ;
  203.                 while ( cp > cp0 && *--cp != ' ' )
  204.                     *cp = '\0' ;
  205.                 break ;
  206.             }
  207.         }
  208.         while ( *--cp == ' ' )
  209.             *cp = '\0' ;
  210.         return ( strsave( cp0 ) ) ;
  211.     }
  212. # endif KVM
  213. getsysargs :
  214.     /* If the command arguments cannot be accessed from the user's memory
  215.        space, get the command name from the system's idea of what the
  216.        name should be. */
  217.     p->pr_csaved = 1 ;
  218.     argbuf.a_argc[0] = '(' ;
  219.     (void)strncpy( &argbuf.a_argc[1], User.u_us.u_comm,
  220.         sizeof( User.u_us.u_comm ) ) ;
  221.     argbuf.a_argc[ sizeof ( User.u_us.u_comm ) + 1 ] = '\0' ;
  222.     (void)strcat( &argbuf.a_argc[0], ")" ) ;
  223.     return ( strsave( argbuf.a_argc ) ) ;
  224. }
  225.  
  226. # ifndef KVM
  227. /*
  228. ** VSTODB - Given a base/size pair in virtual swap area,
  229. ** return a physical base/size pair which is the
  230. ** (largest) initial, physically contiguous block.
  231. /* This code is stolen from the kernel file /sys/sys/vm_drum.c.
  232. */
  233. vstodb ( vsbase, vssize, dmp, dbp, rev )
  234.  
  235. register int                    vsbase ;
  236. register int                    vssize;
  237. struct dmap                     *dmp ;
  238. register struct dblock          *dbp ;
  239. int                             rev ;
  240.  
  241. {
  242.     register int            blk ;
  243.     register swblk_t        *ip ;
  244. # ifdef BSD42
  245.     extern struct info      Info ;
  246. # endif
  247.  
  248. # ifdef BSD42
  249.     blk = Info.i_dmmin ;
  250. # else
  251.     blk = DMMIN ;
  252. # endif
  253.     ip = dmp->dm_map ;
  254.     while ( vsbase >= blk )
  255.     {
  256.         vsbase -= blk ;
  257. # ifdef BSD42
  258.         if ( blk < Info.i_dmmax )
  259. # else
  260.         if ( blk < DMMAX )
  261. # endif
  262.             blk *= 2 ;
  263.         ip++ ;
  264.     }
  265.     dbp->db_size = vssize < blk - vsbase ? vssize : blk - vsbase ;
  266.     dbp->db_base = *ip + (rev ? blk - (vsbase + dbp->db_size) : vsbase);
  267. }
  268. # endif
  269.