Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Q] Linux pstat() or table() equivalent

192 views
Skip to first unread message

Paul Roebuck

unread,
Apr 18, 1999, 3:00:00 AM4/18/99
to
I was trying a quick port of a library I have that runs on both HP-UX and
Digital Alpha.
It works fine except I don't know the equivalent routine under Linux. I
have another
vanilla version that parses "ps" output obtained via popen() but that
seems clumsy
and inelegant. Can someone provide an elegant Linux solution, if one
exists? Thanks.


/*============================================================================
*
* Name:: RetrieveAppProcessName
*
* Description::
* Provides the "glue" to give the high level API routines access to
* the name of the process. This should be replaced by an application
* registry call of sorts but that doesn't exist at this point in time.
*
* Calling Sequence::
* RetrieveAppProcessName (&processNameStr);
*
* Inputs::
* (None)
*
* Outputs::
* processName - Name of the application (i.e., argv [0]).
*
* Return Values::
* (None)
*
*============================================================================*/

#ifdef _OSF_SOURCE
#include <sys/table.h>

#ifndef _NO_PROTO
static void RetrieveAppProcessName (String *processName)
#else
static void RetrieveAppProcessName (processName)
String *processName;
#endif /* _NO_PROTO */
{
struct tbl_procinfo procInfoTable;
int retCode;

assert (processName != NULL);

retCode= table (TBL_PROCINFO,
(int)getpid (),
(char *)&procInfoTable,
1,
sizeof (struct tbl_procinfo));

if (retCode == 1)
*processName= XtNewString (procInfoTable.pi_comm);
else
*processName= XtNewString ("Unknown");
}
#endif /* _OSF_SOURCE */

#ifdef _HPUX_SOURCE
#include <sys/param.h>
#include <sys/pstat.h>

#ifndef _NO_PROTO
static void RetrieveAppProcessName (String *processName)
#else
static void RetrieveAppProcessName (processName)
String *processName;
#endif /* _NO_PROTO */
{
struct pst_status procInfoTable;
int retCode;

assert (processName != NULL);

retCode= pstat_getproc (&procInfoTable,
sizeof (struct pst_status),
0,
(int)getpid ());

if (retCode == 1)
*processName= XtNewString (procInfoTable.pst_ucomm);
else
*processName= XtNewString ("Unknown");
}
#endif /* _HPUX_SOURCE */

Jens-Uwe Mager

unread,
Apr 18, 1999, 3:00:00 AM4/18/99
to
On Sun, 18 Apr 1999 00:02:43 -0500, Paul Roebuck <pa...@ghg.net> wrote:
>I was trying a quick port of a library I have that runs on both HP-UX and
>Digital Alpha.
>It works fine except I don't know the equivalent routine under Linux. I
>have another
>vanilla version that parses "ps" output obtained via popen() but that
>seems clumsy
>and inelegant. Can someone provide an elegant Linux solution, if one
>exists? Thanks.

[ cut code ]

check out /proc/self/cmdline, this appears to be what you want.


--
Jens-Uwe Mager <pgp-mailto:62CFDB25>

Paul Roebuck

unread,
Apr 19, 1999, 3:00:00 AM4/19/99
to

Is the /proc filesystem guaranteed to be accessible? On some systems I
have used in the past, there were security considerations as to whether
this filesystem would be mounted. Is this not the case for Linux?

Jens-Uwe Mager

unread,
Apr 19, 1999, 3:00:00 AM4/19/99
to
On Mon, 19 Apr 1999 07:45:23 -0500, Paul Roebuck <pa...@ghg.net> wrote:

>Is the /proc filesystem guaranteed to be accessible? On some systems I
>have used in the past, there were security considerations as to whether
>this filesystem would be mounted. Is this not the case for Linux?

You can umount it, yes. But there quite a few things that rely on /proc
functionality nowadays, so I would not expect anyone doing it.

Paul Roebuck

unread,
Apr 21, 1999, 3:00:00 AM4/21/99
to

Bearing that in mind, I'll go with the following code. It's less than what
I would
have hoped to find but it does work. I still feel that a Linux version of
pstat()
that simply did this kind of thing for you would have been handy. Anyway, thanks
for your help!


#ifndef _PATH_PROC
#define _PATH_PROC "/proc"
#endif

/*============================================================================
*
* Name:: RetrieveAppProcessName
*
* Description::

* Provides the "glue" to give the high level routines access to the
* name of the process. This should be replaced by an application


* registry call of sorts but that doesn't exist at this point in time.
*
* Calling Sequence::
* RetrieveAppProcessName (&processNameStr);
*
* Inputs::
* (None)
*
* Outputs::
* processName - Name of the application (i.e., argv [0]).
*
* Return Values::
* (None)
*
*============================================================================*/

#ifndef _NO_PROTO


static void RetrieveAppProcessName (String *processName)
#else
static void RetrieveAppProcessName (processName)
String *processName;
#endif /* _NO_PROTO */
{

FILE *proc;
char pathName [PATH_MAX+1];
char buffer [LINE_MAX+1];
char *buffPtr;
Boolean found= False;

assert (processName != NULL);

/* Read the cmdline file for our process from /proc filesystem */
sprintf (pathName, "%s/%d/cmdline", _PATH_PROC, getpid ());
if ((proc= fopen (pathName, "r")) != NULL) {
if (fgets (buffer, sizeof (buffer), proc) != NULL) {

/* Strip path from executable name, if necessary */
if ((buffPtr= strrchr (buffer, '/')) != NULL)
buffPtr++;
else
buffPtr= buffer;
found= True;
}
fclose (proc);
}

if (found)
*processName= XtNewString (buffPtr);
else
*processName= XtNewString (UNKNOWN_STR);
}

0 new messages