获取HPUX正在运行的进程可执行文件的完整路径 [英] Get full path of executable of running process on HPUX

查看:722
本文介绍了获取HPUX正在运行的进程可执行文件的完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得正在运行的进程(可执行文件)的完整路径,而无需使用C ++ code root权限。有人建议的方式来实现这一目标。

I want to get the full path of the running process (executable) without having root permission using C++ code. Can someone suggest a way to achieve this.

在Linux平台上,我可以通过下面的方式做到这一点。

on Linux platforms i can do it by using following way.

char exepath[1024] = {0};
char procid[1024] = {0};
char exelink[1024] = {0};

sprintf(procid, "%u", getpid());

strcpy(exelink, "/proc/");
strcat(exelink, procid);
strcat(exelink, "/exe");

readlink(exelink, exepath, sizeof(exepath));

下面exepath给我们的可执行文件的完整路径。

Here exepath gives us the full path of the executable.

同样,对于我们用它做窗口

Similarly for windows we do it using

GetModuleFileName(NULL, exepath, sizeof(exepath));  /* get fullpath of the service */

请帮助我如何做它在HP-UX因为在HP-UX没有/ proc目录。

Please help me how to do it on HP-UX since there is no /proc directory in HP-UX.

推荐答案

首先,我想对你的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.

这篇关于获取HPUX正在运行的进程可执行文件的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆