Unix Technical Forum

How to get a process working directory in HP-UX system

This is a discussion on How to get a process working directory in HP-UX system within the HP-UX Operating System forums, part of the Unix Operating Systems category; --> Hi all, Our code need get to know the working directory of given process based specific pid. Could anyone ...


Go Back   Unix Technical Forum > Unix Operating Systems > HP-UX Operating System

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-17-2008, 06:02 AM
Wu Ting Bin
 
Posts: n/a
Default How to get a process working directory in HP-UX system

Hi all,

Our code need get to know the working directory of given process based
specific pid. Could anyone give me a hand to give me informations?

This is a problem I can not get from google search.

Thank you very much

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-17-2008, 06:02 AM
Vishwas Pai
 
Posts: n/a
Default Re: How to get a process working directory in HP-UX system

Wu Ting Bin wrote:
> Hi all,
>
> Our code need get to know the working directory of given process based
> specific pid. Could anyone give me a hand to give me informations?
>


Following code assumes that current working directory
is found in lookup cache (assumption is in pstat_getpathname).
Please use this snip at your risk as this may not work in
all situations..

--vishwas.

--

#include <sys/pstat.h>
#include <sys/param.h>
#include <fcntl.h>
#include <stdio.h>

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 <pid>\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);
}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-17-2008, 06:02 AM
Kilgaard
 
Posts: n/a
Default Re: How to get a process working directory in HP-UX system

"Wu Ting Bin" <wutingbin@gmail.com> wrote in message
news:1194426153.123297.225880@19g2000hsx.googlegro ups.com...
> Hi all,
>
> Our code need get to know the working directory of given process based
> specific pid. Could anyone give me a hand to give me informations?
>
> This is a problem I can not get from google search.
>
> Thank you very much
>


Try lsof (with -p pid) available from http://hpux.cs.utah.edu/

The first line should have the FD of 'cwd' and be your current working
directory.



--
Posted via a free Usenet account from http://www.teranews.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-17-2008, 06:03 AM
Wu Ting Bin
 
Posts: n/a
Default Re: How to get a process working directory in HP-UX system

On 11月9日, 上午10时52分, Vishwas Pai <non...@noland..invalid> wrote:
> Wu Ting Bin wrote:
> > Hi all,

>
> > Our code need get to know theworkingdirectoryof given process based
> > specific pid. Could anyone give me a hand to give me informations?

>
> Following code assumes that currentworkingdirectory
> is found in lookup cache (assumption is in pstat_getpathname).
> Please use this snip at your risk as this may not work in
> all situations..
>
> --vishwas.
>
> --
>
> #include <sys/pstat.h>
> #include <sys/param.h>
> #include <fcntl.h>
> #include <stdio.h>
>
> 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 <pid>\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);
> }
>
>
>
> }- 隐藏被引用文字 -
>
> - 显示引用的文字 -


It works. Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-17-2008, 06:03 AM
Wu Ting Bin
 
Posts: n/a
Default Re: How to get a process working directory in HP-UX system

On 11月19日, 上午10时18分, "Kilgaard" <Kilga...@hotmail.com> wrote:
> "Wu Ting Bin" <wuting...@gmail.com> wrote in messagenews:1194426153.123297..225880@19g2000hsx.g ooglegroups.com...
>
> > Hi all,

>
> > Our code need get to know theworkingdirectoryof given process based
> > specific pid. Could anyone give me a hand to give me informations?

>
> > This is a problem I can not get from google search.

>
> > Thank you very much

>
> Try lsof (with -p pid) available fromhttp://hpux.cs.utah.edu/
>
> The first line should have the FD of 'cwd' and be your currentworkingdirectory.
>
> --
> Posted via a free Usenet account fromhttp://www.teranews.com


You are right. But I can not install lsof tool in that machine. Thank
you too.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:56 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com