首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Unix / Linux中获取进程的路径

问如何在Unix / Linux中获取进程的路径
EN

Stack Overflow用户
提问于 2009-03-03 11:35:05
回答 11查看 224.6K关注 0票数 154

在Windows环境中,有一个API来获取正在运行的进程的路径。Unix / Linux中也有类似的东西吗?

或者,在这些环境中有其他方法可以做到这一点吗?

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2009-03-03 11:40:45

在Linux上,符号链接/proc/<pid>/exe包含可执行文件的路径。使用命令readlink -f /proc/<pid>/exe获取该值。

在AIX上,该文件不存在。你可以比较一下cksum <actual path to binary>和cksum /proc/<pid>/object/a.out。

票数 207
EN

Stack Overflow用户

发布于 2014-07-01 16:19:54

你可以通过这些方法很容易找到exe,你自己试试就行了。

  • ll /proc/<PID>/exe
  • pwdx <PID>
  • lsof -p <PID> | grep cwd
票数 72
EN

Stack Overflow用户

发布于 2009-03-09 09:19:33

有点晚了,但所有的答案都是针对linux的。

如果您还需要unix,那么您需要以下内容:

代码语言:javascript
复制
char * getExecPath (char * path,size_t dest_len, char * argv0)
{
    char * baseName = NULL;
    char * systemPath = NULL;
    char * candidateDir = NULL;

    /* the easiest case: we are in linux */
    size_t buff_len;
    if (buff_len = readlink ("/proc/self/exe", path, dest_len - 1) != -1)
    {
        path [buff_len] = '\0';
        dirname (path);
        strcat  (path, "/");
        return path;
    }

    /* Ups... not in linux, no  guarantee */

    /* check if we have something like execve("foobar", NULL, NULL) */
    if (argv0 == NULL)
    {
        /* we surrender and give current path instead */
        if (getcwd (path, dest_len) == NULL) return NULL;
        strcat  (path, "/");
        return path;
    }


    /* argv[0] */
    /* if dest_len < PATH_MAX may cause buffer overflow */
    if ((realpath (argv0, path)) && (!access (path, F_OK)))
    {
        dirname (path);
        strcat  (path, "/");
        return path;
    }

    /* Current path */
    baseName = basename (argv0);
    if (getcwd (path, dest_len - strlen (baseName) - 1) == NULL)
        return NULL;

    strcat (path, "/");
    strcat (path, baseName);
    if (access (path, F_OK) == 0)
    {
        dirname (path);
        strcat  (path, "/");
        return path;
    }

    /* Try the PATH. */
    systemPath = getenv ("PATH");
    if (systemPath != NULL)
    {
        dest_len--;
        systemPath = strdup (systemPath);
        for (candidateDir = strtok (systemPath, ":"); candidateDir != NULL; candidateDir = strtok (NULL, ":"))
        {
            strncpy (path, candidateDir, dest_len);
            strncat (path, "/", dest_len);
            strncat (path, baseName, dest_len);

            if (access(path, F_OK) == 0)
            {
                free (systemPath);
                dirname (path);
                strcat  (path, "/");
                return path;
            }
        }
        free(systemPath);
        dest_len++;
    }

    /* again someone has use execve: we dont knowe the executable name; we surrender and give instead current path */
    if (getcwd (path, dest_len - 1) == NULL) return NULL;
    strcat  (path, "/");
    return path;
}

编辑:修复了Mark lakata报告的错误。

票数 30
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/606041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档