home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / sys / next / programm / 7071 < prev    next >
Encoding:
Text File  |  1992-11-07  |  3.2 KB  |  100 lines

  1. Newsgroups: comp.sys.next.programmer
  2. Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!cs.utexas.edu!sdd.hp.com!decwrl!csus.edu!news
  3. From: eps@futon.SFSU.EDU (Eric P. Scott)
  4. Subject: Re: Extracting cmdline and env of other processes
  5. Message-ID: <1992Nov6.105127.22090@csus.edu>
  6. Sender: news@csus.edu
  7. Reply-To: eps@cs.sfsu.edu
  8. Organization: San Francisco State University
  9. References: <Bx6zu0.DAC@malihh.hanse.de>
  10. Date: Fri, 6 Nov 1992 10:51:27 GMT
  11. Lines: 87
  12.  
  13. In article <Bx6zu0.DAC@malihh.hanse.de> clu@malihh.hanse.de
  14.     (Carsten Lutz) writes:
  15. >I'm looking for a way to get the commandlines and environments ( at start-
  16. >time ) of other processes. Has anyone done this before or knows what the
  17. >best way to do this is ? 'ps' has to do this in some way 'cause it's able
  18. >to display both cmdline and environment.
  19.  
  20. [Answered for 2.1]
  21.  
  22. ps uses the indispensible table() call for TBL_ARGUMENTS to
  23. obtain this information.  Unfortunately, the return is somewhat
  24. bizarre and lacking in useful information; the attached program
  25. illustrates.  The strings comprising the argument list and the
  26. environment are run together, separated only by their terminating
  27. NULs.  There is no argc, argv, or envp.  The last 4 bytes will
  28. always be zero, and can be ignored.  Up to 3 zeroed padding bytes
  29. will preceed those as needed to longword-align the first argument.
  30.  
  31. ps finds the beginning of the argument list by searching
  32. backwards for some number of consecutive 0 bytes.  It's easily
  33. fooled, e.g. if I type
  34.  
  35.     sleep 30 "" "" "" "rm -rf *"&
  36.  
  37. and do a ps, guess what it appears to be doing!
  38.  
  39. By default, ps stops when it finds a string containing "="; the
  40. -e option suppresses this behavior.  Try
  41.  
  42.     sleep 30 = these are hidden arguments&
  43.  
  44. Then do a ps and a ps -e
  45.  
  46. >One way I could imagine is to find the stacks of other processes via /dev/kmem.
  47. >Undermost on this stacks there must be the commandlines and the environments
  48. >( both arguments to main() ) somewhere around. Is there a better way ? 
  49. >mach-hackers wanted !
  50.  
  51. You mean task_by_unix_pid() to obtain the task kernel port,
  52. task_threads() to obtain the thread ports, thread_get_state()
  53. against thread 0 for the registers, vm_region() to find the stack
  54. bottom, and vm_read() to examine the address space?  Good luck!
  55.  
  56. You might be better off trying to locate the __DATA segment; the
  57. first 3 longwords are usually _environ, _NXArgc, and _NXArgv.
  58. Of course, any of those (or what they point to) can be smashed
  59. by the user program, so there are no guarantees in any case.
  60.  
  61. [No, I haven't bothered figuring out exactly what TBL_ARGUMENTS
  62. does.]
  63.  
  64.                     -=EPS=-
  65. ------- compile with  cc -s -object -O -bsd
  66. #include <stdio.h>
  67. #include <sys/table.h>
  68.  
  69. main(argc, argv)
  70. int argc;
  71. char *argv[];
  72. {
  73.     char *malloc();
  74.     register char *p, *q;
  75.     register int s;
  76.  
  77.     if (argc<2||argc>3) {
  78.     usage:
  79.         (void)fprintf(stderr, "Usage: %s pid [bytes]\n", *argv);
  80.         exit(1);
  81.     }
  82.     s=argc>2 ? atoi(argv[2]) : 1024;
  83.     if (s<=0) goto usage;
  84.     if (!(p=malloc(s))) {
  85.         (void)fprintf(stderr, "%s: malloc failure\n", *argv);
  86.         exit(1);
  87.     }
  88.     bzero(p, s);
  89.     if (table(TBL_ARGUMENTS, atoi(argv[1]), p, 1, s)<0) {
  90.         perror("table");
  91.         exit(1);
  92.     }
  93.     q=p+s;
  94.     do {
  95.         putchar((*p<' '||*p>'~') ? '.' : *p);
  96.     } while (++p<q);
  97.     putchar('\n');
  98.     exit(0);
  99. }
  100.