Andrew Benson (d
...@mtu.edu) wrote:
: Greets,
:
: I wanted to get some process information, so I first wrote
: a program just to test if I was doing things correctly. The program
: is below. It takes one argument on the command line (a process number)
: and opens /proc/that-process-number, then does a PIOCPSINFO ioctl to
: get some information. Then I try to print that processes argv[].
: [...]
I forgot to include the program (Thanks Kevin Thomas for pointing
that out to me). Here it is:
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/fault.h>
#include <sys/syscall.h>
#include <sys/procfs.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
char procname[1024];
prpsinfo_t p;
int fd;
int i;
/* Open process */
sprintf(procname, "/proc/%d", atol(argv[1]));
if ((fd = open(procname, O_RDWR)) == -1) {
perror(procname);
exit(1);
}
/* Get process summary info */
if (ioctl(fd, PIOCPSINFO, (void *)&p) == -1) {
perror("PIOCPSINFO");
exit(2);
}
/* Print argv[] of process */
for(i=0; i < p.pr_argc; ++i)
printf("%s%s%s", i ? " ": "arglist: ", p.pr_argv[i],
((i+1) == p.pr_argc) ? "\n" : "" );
close(fd);
exit(0);
}