16# if defined(__GLIBCXX__) || defined(__ANDROID_NDK__)
18# elif defined(__APPLE__)
20# include <sys/sysctl.h>
22# elif defined( _WIN32 )
25# pragma message ("Unknown Platform in file: " __FILE__ )
32namespace alib {
namespace lang::system {
54#if defined(__GLIBC__) && defined(__unix__) || defined(__ANDROID_NDK__)
58 std::ifstream file( fileName );
61 getline(file, buffer);
64 if (buffer.size() > 2 )
65 for(
size_t i= 0 ; i < buffer.size() -2 ; ++i )
66 if ( buffer[i] ==
'\0' )
69 result.
Reset( buffer.c_str() );
78 while ( --fieldNo >= 0 && (result= tknzr.HasNext()) )
81 target.
Reset( tknzr.Next() );
105 NString64 procDir(
"/proc/"); procDir._<NC>(
PID )._(
'/' );
106 integer procPathLen= procDir.Length();
109 procDir <<
"cmdline"; readProcFile( procDir,
CmdLine ); procDir.ShortenTo(procPathLen);
110 procDir <<
"stat"; readProcFile( procDir,
Stat ); procDir.ShortenTo(procPathLen);
120 "CAMP",
"Error reading process Info" )
122 if (
Name.CharAtEnd () == ')' )
Name.DeleteEnd <NC>( 1 );
123 if (
Name.CharAtStart() == '(' )
Name.DeleteStart<NC>( 1 );
133 ssize_t length= readlink( procDir, buffer, 2048 );
134 procDir.ShortenTo(procPathLen);
141 "Executable path does not contain directory separator character.\n"
154#elif defined (__APPLE__)
160 pid=
static_cast<uinteger>( getpid() );
162 struct proc_bsdinfo proc;
163 int st = proc_pidinfo(
static_cast<int>(pid), PROC_PIDTBSDINFO, 0, &proc, PROC_PIDTBSDINFO_SIZE);
164 if (st != PROC_PIDTBSDINFO_SIZE)
172 Name.
_(
reinterpret_cast<const char*
>( proc.pbi_comm ) );
176 char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
178 if ( proc_pidpath (
static_cast<int>(
PID ), pathbuf, PROC_PIDPATHINFO_MAXSIZE) > 0 )
193#elif defined (_WIN32)
201 DWORD wPID= GetCurrentProcessId();
214 GetModuleFileNameA( NULL, buf, MAX_PATH );
218 "Executable path does not contain directory separator character: ",
224 STARTUPINFOA startupInfo;
225 GetStartupInfoA( &startupInfo );
232 #pragma message ("Unknown Platform in file: " __FILE__ )
This class represents process information.
AString CmdLine
The command line which invoked this process.
AString ConsoleTitle
For console processes, this is the title displayed in the title bar. (Windows OS only....
bool getStatField(int fieldNo, AString &target)
AString ExecFilePath
The path of the executable (if available to us).
static ALIB_API const ProcessInfo & Current()
uinteger PID
The process id as AString.
ALIB_API bool get(uinteger PID)
AString Stat
The contents of /proc/PID/stat file. (Unix like OS only.)
AString StatState
The state field (2) within Stat. (Unix like OS only.)
AString StatPGRP
The process group field (4) within Stat. (Unix like OS only.)
uinteger PPID
The parent's process id as AString. (Unix like OS / Mac OS only.)
static ALIB_API ProcessInfo current
TAString & ShortenTo(integer newLength)
TAString & Append(const TCharSrc *src, integer srcLength)
TAString & _(const TString< TChar > &src, integer regionStart, integer regionLength=MAX_LEN)
void SetLength(integer newLength)
constexpr bool IsEmpty() const
constexpr integer Length() const
TChar CharAtStart() const
ALIB_API int64_t ParseInt(integer startIdx=0, TNumberFormat< TChar > *numberFormat=nullptr, integer *newIdx=nullptr) const
ALIB_WARNINGS_RESTORE integer LastIndexOf(TChar needle, integer startIndex=MAX_LEN) const
#define IF_ALIB_THREADS(...)
#define ALIB_ASSERT_ERROR(cond,...)
#define ALIB_LOCK_WITH(lock)
platform_specific integer
platform_specific uinteger
lang::system::ProcessInfo ProcessInfo
Type alias in namespace alib.
lang::uinteger uinteger
Type alias in namespace alib.
NLocalString< 64 > NString64
Type alias name for TLocalString<nchar,64>.
strings::TString< nchar > NString
Type alias in namespace alib.
characters::nchar nchar
Type alias in namespace alib.