1

On Ubuntu we can extract full path of exe of running process by reading /proc/'pid'/exe.

On solaris there is no 'exe' file in /proc/'pid'. I read the psinfo. But it gives just the name of process and arguments. It does not have full path of exe. On solaris how can we do this? My solaris version is 11.3.

aaa
  • 415
  • 6
  • 15
  • Do you have at least Solaris 11.3 SRU 5.6? – Candy Gumdrop Nov 24 '17 at 12:37
  • 1
    Possible duplicate of [psinfo\_t solaris does not contain full process name in its field](https://stackoverflow.com/questions/35888100/psinfo-t-solaris-does-not-contain-full-process-name-in-its-field) – Candy Gumdrop Nov 24 '17 at 12:45

2 Answers2

4

Through command, you can get the full path of the exe running like this:

# ls -l /proc/<pid>/path/a.out

For e.g.

# ls -l /proc/$$/path/a.out
lrwxrwxrwx   1 root     root           0 Nov 24 17:19 /proc/14921/path/a.out -> /usr/bin/bash

This way you can get the executable path.

More convinient way is:

# readlink -f /proc/<pid>/path/a.out

For e.g.:

# readlink -f /proc/$$/path/a.out
/usr/bin/bash

And programmatically you can do it like this:

#include <stdio.h>
#include <unistd.h>

#define BUF_SIZE 1024

int main(int argc, char *argv[]) {
    char outbuf[BUF_SIZE] = {'\0'};
    char inbuf[BUF_SIZE] = {'\0'};
    ssize_t len;

    if (argc != 2) {
            printf ("Invalid argument\n");
            return -1;
    }

    snprintf (inbuf, BUF_SIZE, "/proc/%s/path/a.out", argv[1]);

    if ((len = readlink(inbuf, outbuf, BUF_SIZE-1)) != -1) {
            outbuf[len] = '\0';
    } else {
            perror ("readlink failed: ");
            return -1;
    }

    printf ("%s\n", outbuf);
    return 0;
}

It's usage:

# ./a.out <pid>  
H.S.
  • 11,654
  • 2
  • 15
  • 32
  • Re `readlink`. This is by default installed on any Solaris 11 host. It is part of the *GNU Coreutils* package. Although it will always exist in the global zone, it doesn't by default make into local zone because that package is not part of the `solaris-small-server` IPS group (which is what a local zone gets by default). In other words: on a local zone you must explicitly install : `pkg install gnu-coreutils`. But you should consider that [best practice](https://unix.stackexchange.com/questions/66415/solaris-default-install-user-tools) anyhow. – peterh Nov 26 '17 at 19:40
0

Unsure for Solaris, but some old unixes, the only thing that could be retrieved was the command name in argv[0]. Then we had to search that command in the PATH environment variable in correct order to find the full path of the command.

A bit manual but bullet proof.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252