Examines the /proc directory to establish all the processes currently running on the machine. The function populates a map of process ids to hzProginfo instances. Note this function will fail only if the /proc directory cannot be read or there is a duplicate entry in it. As the /proc is usually readable and executable by everyone and Linux/Unix has few bugs, neither of these events ever acually happens.
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | ListProcesses | (hzMapS<uint32_t,hzProginfo*>&,) |
Declared in file: hzUnixacc.h
Defined in file : hzProcess.cpp
Function Logic:
Function body:
hzEcode ListProcesses (hzMapS<uint32_t,hzProginfo*>& procs)
{
// Category: Diagnostics
//
// Examines the /proc directory to establish all the processes currently running on the machine. The function populates a map of process ids to hzProginfo instances.
//
// Arguments: 1) procs This is a reference to a hzMapS<uint32_t,hzProginfo*> that this query function will populate.
//
// Returns: E_CORRUPT If there was an error reading the /proc directory (see note)
// E_OK If the operation was successful
//
// Note this function will fail only if the /proc directory cannot be read or there is a duplicate entry in it. As the /proc is usually readable and executable by everyone and
// Linux/Unix has few bugs, neither of these events ever acually happens.
_hzfunc("ListProcesses") ;
hzProginfo* pPI ; // Program information (from /proc)
FSTAT fs ; // File status
dirent* pDE ; // Directory entry meta data
DIR* pDir ; // Operating directory
uint32_t ino ; // Inode number of directory entry
uint32_t cpid ; // Current process id
uint32_t xpid ; // Process id derived from sub-dirs of /proc
uint32_t nIndex ; // General iterator
char buf [512]; // Directory entry name buffer
/*
** ** Obtain current executable
** */
// Clear tables
for (nIndex = 0; nIndex < procs.Count() ; nIndex++)
{
pPI = procs.GetObj(nIndex) ;
delete pPI ;
}
procs.Clear() ;
// Open the /proc directory and read it.
pDir = opendir("/proc") ;
if (!pDir)
{
hzerr(E_CORRUPT, "Could not open the /proc directory") ;
return E_CORRUPT ;
}
for (; pDE = readdir(pDir) ;)
{
// Eliminate self
if (pDE->d_name[0]== ''.'')
continue ;
// Eliminate all non numeric directories
if (!IsAllDigits(pDE->d_name))
continue ;
xpid = atoi(pDE->d_name) ;
pPI = procs[xpid] ;
if (pPI)
{
hzerr(E_SETONCE, "We already have process %d in the process list", xpid) ;
return E_SETONCE ;
}
pPI = new hzProginfo() ;
pPI->m_PID = xpid ;
sprintf(buf, "/proc/%d/exe", xpid) ;
if (lstat(buf, &fs) == -1)
std::cout << "Could not stat " << buf << std::endl ;
else
{
if (fs.st_ino == ino)
{
std::cout << "pid: " << cpid << " - Have located an existing process of " << xpid << std::endl ;
std::cerr << "Both the existing and current process involve executable with inode of " << ino << std::endl ;
exit(-4);
}
}
procs.Insert(pPI->m_PID, pPI) ;
}
closedir(pDir) ;
return E_OK ;
}