5

This is most similar to Get full path of executable of running process on HPUX…, except for AIX.

The basic question is: how, on AIX, can I determine the full path to the current executable? Having to do this before doing anything else (e.g., chdir) is fine.

The most accurate answer I've found so far is to check the output from
svmon -P $$ -O format=nolimit,filename=on,filtertype=client
(where $$ has its shell meaning: current pid). That's not only heavy amounts of C, but svmon is also not very fast and can easily overwhelm the runtime of the rest of the application.

The next best answer seems to be to simply look at argv[0], and, if it has a slash in it, it's either a full path name (starts with a leading /) or a relative-to-current-dir name (does not start with a leading /). If it doesn't have a slash in it, it's relative to something in PATH.
And if, after this resolution, I end up with a symlink, then there's all the resolution of symlink(s) to deal with as well (hard links are probably beyond the scope of any solution). This solution looks like it's relatively cross-platform, but is also very heavy in the C code (should be faster than svmon). And I expect there are race-conditions and such to deal with.

Thanks,

Update: I'm looking for a solution to submit to the perl devs. And they're going to worry about an insecure PATH, especially in setuid/setgid scenarios. See perlsec. I think that we could be okay here, but if you combined setuid with faking argv[0], you could force perl to think it's somewhere else, and load the wrong modules. The "next best" answer above only really works when not in perl's taint-mode.

Community
  • 1
  • 1
Tanktalus
  • 21,664
  • 5
  • 41
  • 68
  • Check out [this similar question](http://stackoverflow.com/questions/933850/how-to-find-the-location-of-the-executable-in-c/933996#933996). – Kerrek SB Jun 21 '11 at 15:55
  • Check http://stackoverflow.com/questions/933850/how-to-find-the-location-of-the-executable-in-c/933996#933996 – karlphillip Jun 21 '11 at 15:55
  • I would like to do this in a library so that I can put it in an error log, and I cannot reasonably require my callers to pass in `argv[0]`. – PJTraill May 29 '15 at 11:05

2 Answers2

0

Use ps to get executable path address

ps -aef | grep app | awk '{print $8}'

above command gives your app executable path address

0

Why can't you use ps options as a base line? Granted, you'll still need to process the cmd value to see if has a leading '/' or not. Something like

ps -o pid,env,cwd,cmd | grep youAppName | awk -f programToRationalizePathName 

I don't have access to AIX anymore, but I did work on it for 2 1/2 years and I know I've used this sort of feature. I didn't think it was slow, but I might have had different requirements than you do.

I get the impression you want a utility function, a 1-at-time call that returns the full path, but if you need an on-going process and are concerned about re-starting ps every 1 minute (for example), look at the AIX specific nmon utility. Not sure if it can generate output similar to the ps -o cmd but it can be set up to run as long as you want, as often as you want (down to 1 second intervals) and it is just one process, whose output can be redirected as needed. (Nmon is not part of the std install, but most organizations do install it, as it is IBM blessed (if not supported directly)).

Of course all of the 'not 100%' caveats apply from the similar questions mentioned by you and in the comments.

I hope this helps.

shellter
  • 36,525
  • 7
  • 83
  • 90
  • I try the command you suggest but that gives me a very nice error with the ps option not correct – Kiwy Jan 14 '14 at 09:34
  • in my case `env` and `cwd` flag does not exist and the correct synthax is `ps -o=pid,cmd` and also the command does not necessarily show a full path. – Kiwy Jan 14 '14 at 11:24
  • hm.. yes, my Linux doesn't have `cwd` either, not sure where I found that. I'm assuming you found this question because of AIX? I can't remember, does AIX support /proc/$PID ? If so, (and you need cwd) you might find it as a sub-value. If you have an active use case, post a question. Good luck! – shellter Jan 15 '14 at 02:21
  • Surely you can pass the current pid to `ps`, cutting out the `grep`. – PJTraill May 29 '15 at 11:06
  • @PJTraill :Yes, well I'm still not sure what the O.P.s use case was. The title says one thing, the long exposition says something else. Feel free to post an answer ;-) Good luck to all. – shellter May 29 '15 at 11:41