Thread View: comp.sys.next.programmer
2 messages
2 total messages
Started by Carsten Lutz
Wed, 04 Nov 1992 12:53
Extracting cmdline and env of other processes
Author: Carsten Lutz
Date: Wed, 04 Nov 1992 12:53
Date: Wed, 04 Nov 1992 12:53
19 lines
1100 bytes
1100 bytes
Moin ! I'm looking for a way to get the commandlines and environments ( at start- time ) of other processes. Has anyone done this before or knows what the best way to do this is ? 'ps' has to do this in some way 'cause it's able to display both cmdline and environment. One way I could imagine is to find the stacks of other processes via /dev/kmem. Undermost on this stacks there must be the commandlines and the environments ( both arguments to main() ) somewhere around. Is there a better way ? mach-hackers wanted ! greetings, Carsten -- * Carsten Lutz, Rellingen, FRG / clu@malihh.hanse.de ( NeXTmail accepted ) * * Voice : +49 4101 512493 Fax: +49 4101 27757 Traily : +49 4101 22306 *
Re: Extracting cmdline and env of other processes
Author: Eric P. Scott
Date: Fri, 06 Nov 1992 10:51
Date: Fri, 06 Nov 1992 10:51
87 lines
3345 bytes
3345 bytes
In article <Bx6zu0.DAC@malihh.hanse.de> clu@malihh.hanse.de
(Carsten Lutz) writes:
>I'm looking for a way to get the commandlines and environments ( at start-
>time ) of other processes. Has anyone done this before or knows what the
>best way to do this is ? 'ps' has to do this in some way 'cause it's able
>to display both cmdline and environment.
[Answered for 2.1]
ps uses the indispensible table() call for TBL_ARGUMENTS to
obtain this information. Unfortunately, the return is somewhat
bizarre and lacking in useful information; the attached program
illustrates. The strings comprising the argument list and the
environment are run together, separated only by their terminating
NULs. There is no argc, argv, or envp. The last 4 bytes will
always be zero, and can be ignored. Up to 3 zeroed padding bytes
will preceed those as needed to longword-align the first argument.
ps finds the beginning of the argument list by searching
backwards for some number of consecutive 0 bytes. It's easily
fooled, e.g. if I type
sleep 30 "" "" "" "rm -rf *"&
and do a ps, guess what it appears to be doing!
By default, ps stops when it finds a string containing "="; the
-e option suppresses this behavior. Try
sleep 30 = these are hidden arguments&
Then do a ps and a ps -e
>One way I could imagine is to find the stacks of other processes via /dev/kmem.
>Undermost on this stacks there must be the commandlines and the environments
>( both arguments to main() ) somewhere around. Is there a better way ?
>mach-hackers wanted !
You mean task_by_unix_pid() to obtain the task kernel port,
task_threads() to obtain the thread ports, thread_get_state()
against thread 0 for the registers, vm_region() to find the stack
bottom, and vm_read() to examine the address space? Good luck!
You might be better off trying to locate the __DATA segment; the
first 3 longwords are usually _environ, _NXArgc, and _NXArgv.
Of course, any of those (or what they point to) can be smashed
by the user program, so there are no guarantees in any case.
[No, I haven't bothered figuring out exactly what TBL_ARGUMENTS
does.]
-=EPS=-
------- compile with cc -s -object -O -bsd
#include <stdio.h>
#include <sys/table.h>
main(argc, argv)
int argc;
char *argv[];
{
char *malloc();
register char *p, *q;
register int s;
if (argc<2||argc>3) {
usage:
(void)fprintf(stderr, "Usage: %s pid [bytes]\n", *argv);
exit(1);
}
s=argc>2 ? atoi(argv[2]) : 1024;
if (s<=0) goto usage;
if (!(p=malloc(s))) {
(void)fprintf(stderr, "%s: malloc failure\n", *argv);
exit(1);
}
bzero(p, s);
if (table(TBL_ARGUMENTS, atoi(argv[1]), p, 1, s)<0) {
perror("table");
exit(1);
}
q=p+s;
do {
putchar((*p<' '||*p>'~') ? '.' : *p);
} while (++p<q);
putchar('\n');
exit(0);
}
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads