home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / sys / sun / apps / 3069 < prev    next >
Encoding:
Text File  |  1993-01-23  |  5.0 KB  |  107 lines

  1. Xref: sparky comp.sys.sun.apps:3069 comp.unix.programmer:6033 comp.unix.internals:2152
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  3. From: torek@horse.ee.lbl.gov (Chris Torek)
  4. Newsgroups: comp.sys.sun.apps,comp.unix.programmer,comp.unix.internals
  5. Subject: Re: modifying ARGV within a running program
  6. Date: 23 Jan 1993 13:18:09 GMT
  7. Organization: Lawrence Berkeley Laboratory, Berkeley CA
  8. Lines: 94
  9. Message-ID: <28603@dog.ee.lbl.gov>
  10. References: <1jqninINNa8l@usenet.INS.CWRU.Edu>
  11. NNTP-Posting-Host: 128.3.112.15
  12.  
  13. In article <1jqninINNa8l@usenet.INS.CWRU.Edu> edvax@pyrite.som.cwru.edu
  14. (Ed Reznichenko) writes:
  15. >Does anyone know how to SAFELY modify [what ps will show for a process]?
  16. >Any ideas on how to do this portably (if at all) would be appreciated.
  17.  
  18. It cannot be done portably.  In fact, on some UNIX systems, it cannot
  19. be done at all.  These systems save away the first few arguments at
  20. process startup, keeping it in a kernel area off limits to normal user
  21. code.  Here the `ps' command reads this kernel area.  Since the kernel
  22. area is read-only, the stuff ps will show is likewise read-only.
  23.  
  24. Since this is going to comp.sys.sun.apps: on Suns running SunOS, at
  25. least---I know nothing about the new Solaris systems---the strings ps
  26. prints are found at the top of the user stack.  (This is true in all
  27. traditional BSD-based systems.)  Specifically, on a SPARC, the layout
  28. is:
  29.  
  30.      [end of user accessible space]      0xf8000000 (approx.)
  31.     --------------------------------- 0xf7ffffff (approx.)
  32.     |       <reserved>        |  < fixed but system-dependent size
  33.     +-------------------------------+
  34.     |     strings & <alignment>    |  < variable size
  35.     +-------------------------------+
  36.     |          NULL        |    ^  higher addresses
  37.     |     environ[ne - 1]    |    |
  38.     |     environ[ne - 2]    |    |
  39.     |           ...        |    |
  40.     |        environ[0]        |    v  lower addresses
  41.     +-------------------------------+
  42.     |          NULL        |
  43.     |     argv[argc - 1]        |
  44.     |     argv[argc - 2]        |  < each pointer is 4 bytes
  45.     |           ...        |
  46.     |         argv[0]        |
  47.     +-------------------------------+
  48.     |          argc        |  < argc is also 4 bytes
  49.     +-------------------------------+
  50.     |       <register window>    |  < exactly 64 bytes
  51.     +-------------------------------+ <-- %sp at program startup
  52.  
  53. In SunOS, the <reserved> space is probably empty.  In 4.4BSD on the
  54. SPARC, it contains the signal trampoline code and something I will
  55. get to in a moment.
  56.  
  57. The strings, plus any needed padding for alignment, appear immediately
  58. below the reserved space.  They are `in order', with the string for
  59. argv[0] at the lowest address and the last environment string at the
  60. highest.  These are immediately preceded by pointers to each string;
  61. these make up the environ[] and argv[] arrays.  The `ps' program scans
  62. backwards (under SunOS, and used to under BSD) through the top few user
  63. pages looking for the argv strings.  Since they are in plain old user
  64. space pages, it is possible to overwrite them.  Note, however, that
  65. overwriting more space than is available will cause trouble.  Since the
  66. environment strings immediately follow the argv strings, it is possible
  67. to clobber one's own environment, causing library routines such as
  68. execlp() and getenv() to fail mysteriously.  (Under BSD, one could even
  69. overwrite the signal trampoline code.)
  70.  
  71. Incidentally, this is also the reason that a large environment can
  72. make `ps' fail to find arguments.  If those strings take up enough
  73. space, ps's backwards scan fails, as it only reads the last few pages.
  74.  
  75. In 4.4BSD, the very top of the <reserved> area always contains a
  76. structure known as `ps_strings', as defined in <sys/exec.h>:
  77.  
  78. /*
  79.  * The following structure is found at the top of the user stack of each
  80.  * user process. The ps program uses it to locate argv and environment
  81.  * strings. Programs that wish ps to display other information may modify
  82.  * it; normally ps_argvstr points to the text for argv[0], and ps_nargvstr
  83.  * is the same as the program's argc. The fields ps_envstr and ps_nenvstr
  84.  * are the equivalent for the environment.
  85.  */
  86. struct ps_strings {
  87.     char    *ps_argvstr;    /* first of 0 or more argument strings */
  88.     int    ps_nargvstr;    /* the number of argument strings */
  89.     char    *ps_envstr;    /* first of 0 or more environment strings */
  90.     int    ps_nenvstr;    /* the number of environment strings */
  91. };
  92.  
  93. A C library routine (not yet written, but to be called `setproctitle'
  94. ---this is the name used in sendmail, ftp, etc) will provide a portable
  95. interface to altering ps_strings.  The `ps' program simply reads the
  96. pointer and count, then fetchs ps_nargvstr NUL-terminated strings
  97. starting at ps_argvstr.  There is no need to arrange `extra' space in
  98. advance for long setproctitle()s, as the space can simply be allocated
  99. on the heap.
  100.  
  101. Note: all of the above is based on the alpha 4.4BSD SPARC kernel.  While
  102. this kernel runs SunOS binaries, there is no guarantee that the SunOS
  103. C startup code does not modify the picture somehow.
  104. -- 
  105. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  106. Berkeley, CA        Domain:    torek@ee.lbl.gov
  107.