/*============================================================================
*
* 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 */
[ cut code ]
check out /proc/self/cmdline, this appears to be what you want.
--
Jens-Uwe Mager <pgp-mailto:62CFDB25>
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?
>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.
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);
}