1

Not getting full process name in psinfo_t struct's field pr_fname while reading process info from psinfo data file(/proc/%d/psinfo) into struct psinfo_t from procfs.h in solaris.

Full psinfo_t struct definition is present on below site:

http://docs.oracle.com/cd/E19253-01/816-5174/6mbb98ui2/index.html

Only if the process name is less than equal to 15 characters then I am getting the full process name other wise if process name is more than 15 characters then I am getting only first 15 characters of process name rest characters are truncated.

The code I am using is as below:

#include <iostream>
#include <cstdlib>
#include <procfs.h>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    // get the pid from command line
    int pid  = atoi(argv[1]);

    // create the pstatus struct from procfs
    psinfo_t info;

    char file[100];
    sprintf(file, "/proc/%d/psinfo", pid);
    ifstream in(file);
    if (in)
    {
        in.read((char*)&info, sizeof(psinfo_t));
        in.close();

        cout << "My Name: " << info.pr_fname << endl;
    }
    else
    {
        cout << "Process Not Exists!" << endl;
    }

return 0;
}

Do I have to read some other file (other than psinfo) from procfs file system in order to get full process name. Also if I am using belwo ps command from command line then I am able to get full process name:

ps -p 4970 -o comm

but I don't want to get process name by executing ps command inside my code. I am curious from where ps binary is picking up the process name.

mSatyam
  • 531
  • 7
  • 25

2 Answers2

2

The pr_fname field of the psinfo_t structure is 16 bytes long, per the source code:

#define PRFNSZ      16  /* Maximum size of execed filename */

So it will in fact be truncated to a maximum of 15 characters.

You can get the the name of the actual binary file from /proc/PID/map. For a 32-bit process the exec'd binary will be mapped at address 0x00010000.

You can browse the source code for ps at http://src.illumos.org/source/xref/illumos-gate/usr/src/cmd/ps/ if you want to see where it finds its data.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • is there a pre-defined struct in which I can store data of this map file. – mSatyam Mar 09 '16 at 13:06
  • @mSatyam It's on the same `proc.4` man page you linked in your question. – Andrew Henle Mar 10 '16 at 11:47
  • I tried reading the map data file into prmap_t struct but when I printed the data member pr_mapname it was an empty string. Did you intended me to do something else. Am I doing right? – mSatyam Mar 15 '16 at 09:48
  • How did you read the map data? You pretty much need to read binary `prmap_t` structures from `/proc/pid/map` with one low-level `read()` call. – Andrew Henle Mar 15 '16 at 19:43
  • I read it the same way I read psinfo struct as in my question. Will try with low level read call now. – mSatyam Mar 16 '16 at 17:33
2

Solaris 11.3 SRU 5 introduced /proc/<pid>/execname which contains the full command name, so you can check to see if that file exists and use it if so, else fall back to the limited pr_fname.

See Solaris 11.3 SRU 5.6: updates in ps(1) and /proc/<pid>/{cmdline,environ,execname} for details.

alanc
  • 4,102
  • 21
  • 24