Operating System - Linux
1753562 Members
6134 Online
108796 Solutions
New Discussion юеВ

Re: current working directory of process

 
SOLVED
Go to solution
sanjay khuntia
Occasional Contributor

current working directory of process

hi all,

i want to get current working directory for all processes. i am using pstat_ system calls to fetch those information.But the pstat_ calls nowhere return current directory of a process. Can anyone suggest me?
Regards,
sanjay
3 REPLIES 3
James R. Ferguson
Acclaimed Contributor

Re: current working directory of process

Hi:

An individual process can use 'getcwd()' to obtain its *own* current working directory [of course].

I don't believe that there is a way (in HP-UX at least) to fetch this information otherwise, though. For that matter, why would you want to do so?

Regards!

...JRF...

Vishwas Pai
Regular Advisor
Solution

Re: current working directory of process

Following code assumes that current working directory
is found in lookup cache (assumption is in pstat_getpathname). So it may not may not work in all situations..

--vishwas.

--

#include
#include
#include
#include

main(int argc , char **argv)
{

struct pst_status pst[1];
int count;
char buf[MAXPATHLEN+1];
pid_t pid;

if ( argc != 2 ) {
printf("Usage: pwdx \n");
exit(1);
}

pid = atol (argv[1]);


if ( getppid(pid) < 0 ) {

fprintf(stderr,"Error: %s %d is not a valid pid.\n",
argv[1],pid);
exit(1);
}

bzero(buf,MAXPATHLEN+1);

if ( (count=pstat_getproc(pst, sizeof(pst[0]), 0, pid )) > 0 ) {

printf("pid is %d, command is %s\n",
pst[0].pst_pid, pst[0].pst_ucomm);

}

count = pstat_getpathname(buf,MAXPATHLEN,&pst[0].pst_fid_cdir);

if (count > 0) {
printf("%s\n", buf);
exit(0);
}
else {
fprintf(stderr,"ERROR getting the CWD.\n");
perror("pstat_getpathname");
exit(1);
}
}
sanjay khuntia
Occasional Contributor

Re: current working directory of process

Thanks for the support. we get the current directory from struct pst_status in where the field struct __pst_fid pst_fid_cdir;/* Cookie for current working directory */ gives the current working directory for a process.