且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取HPUX正在运行的进程可执行文件的完整路径

更新时间:2021-12-25 02:21:52

首先,我想对你的Linux解决方案发表评论:这是约5倍,只要它需要,并进行了大量完全不必要的操作,以及使用1024幻数是完全错误的:

First, I'd like to comment on your Linux solution: it is about 5 times as long as it needs to be, and performs a lot of completely unnecessary operations, as well as using 1024 magic number which is just plain wrong:

$ grep PATH_MAX /usr/include/linux/limits.h 
#define PATH_MAX        4096    /* # chars in a path name */

下面是一个正确的最小的替换:

Here is a correct minimal replacement:

#include <limits.h>
...
  char exepath[PATH_MAX] = {0};
  readlink("/proc/self/exe", exepath, sizeof(exepath));

二,HP-UX,您可以使用 shl_get_r()来获得关于所有加载模块的信息。在索引为0,你会发现有关主可执行文件的信息。在 desc.filename 将指向可执行文件的名称在的execve(2)的时间。

Second, on HP-UX you can use shl_get_r() to obtain information about all loaded modules. At index 0, you will find information about the main executable. The desc.filename will point to the name of the executable at execve(2) time.

不幸的是,这个名字是相对的,所以你可能需要搜索 $ PATH ,如果应用程序没有传给putenv(PATH可能失败=一些新:路径),或者原来的EXENAME是如 ./ a.out的和应用程序执行 CHDIR(2),因为

Unfortunately, that name is relative, so you may have to search $PATH, and may fail if the application did putenv("PATH=some:new:path") or if the original exename was e.g. ./a.out and the application has performed chdir(2) since.