45 #define NCBI_USE_ERRCODE_X Corelib_Unix
61 #if defined(NCBI_OS_SOLARIS) || \
62 (defined(HAVE_GETPWUID) && !defined(NCBI_HAVE_GETPWUID_R))
64 const struct passwd* pwd = getpwuid(uid);
65 if (pwd && pwd->pw_name) {
66 user.assign(pwd->pw_name);
69 #elif defined(NCBI_HAVE_GETPWUID_R)
72 char x_buf[
sizeof(*pwd) +
PWD_BUF];
77 pwd = (
struct passwd*)
buf;
79 # if NCBI_HAVE_GETPWUID_R == 4
81 pwd = getpwuid_r(uid, pwd, (
char*)(pwd + 1),
size);
83 # elif NCBI_HAVE_GETPWUID_R == 5
85 int x_errno = getpwuid_r(uid, pwd, (
char*)(pwd + 1),
size, &pwd);
92 # error "Unknown value of NCBI_HAVE_GETPWUID_R: 4 or 5 expected."
96 if (pwd || errno != ERANGE) {
102 # ifdef _SC_GETPW_R_SIZE_MAX
103 long sc = sysconf(_SC_GETPW_R_SIZE_MAX);
104 maxsize = sc > 0 ? (size_t) sc :
size;
109 "getpwuid_r() parse buffer too small ("
111 +
string(
size < maxsize
116 if (
size < maxsize) {
118 buf =
new char[
sizeof(*pwd) +
size];
131 buf =
new char[
sizeof(*pwd) +
size];
134 if (pwd && pwd->pw_name) {
135 user.assign(pwd->pw_name);
151 #if defined(NCBI_OS_SOLARIS) || \
152 (defined(HAVE_GETPWUID) && !defined(NCBI_HAVE_GETPWUID_R))
154 const struct passwd* pwd = getpwnam(user.c_str());
155 uid = pwd ? pwd->pw_uid : (uid_t)(-1);
157 #elif defined(NCBI_HAVE_GETPWUID_R)
160 char x_buf[
sizeof(*pwd) +
PWD_BUF];
165 pwd = (
struct passwd*)
buf;
167 # if NCBI_HAVE_GETPWUID_R == 4
169 pwd = getpwnam_r(user.c_str(), pwd, (
char*)(pwd + 1),
size);
171 # elif NCBI_HAVE_GETPWUID_R == 5
173 int x_errno = getpwnam_r(user.c_str(),
174 pwd, (
char*)(pwd + 1),
size, &pwd);
181 # error "Unknown value of NCBI_HAVE_GETPWUID_R: 4 or 5 expected."
185 if (pwd || errno != ERANGE) {
191 # ifdef _SC_GETPW_R_SIZE_MAX
192 long sc = sysconf(_SC_GETPW_R_SIZE_MAX);
193 maxsize = sc > 0 ? (size_t) sc :
size;
198 "getpwnam_r() parse buffer too small ("
200 +
string(
size < maxsize
205 if (
size < maxsize) {
207 buf =
new char[
sizeof(*pwd) +
size];
220 buf =
new char[
sizeof(*pwd) +
size];
223 uid = pwd ? pwd->pw_uid : (uid_t)(-1);
242 #if defined(NCBI_OS_SOLARIS) || \
243 (defined(HAVE_GETPWUID) && !defined(NCBI_HAVE_GETPWUID_R))
245 const struct group* grp = getgrgid(gid);
246 if (grp && grp->gr_name) {
247 group.assign(grp->gr_name);
250 #elif defined(NCBI_HAVE_GETPWUID_R)
253 char x_buf[
sizeof(*grp) +
GRP_BUF];
258 grp = (
struct group*)
buf;
260 # if NCBI_HAVE_GETPWUID_R == 4
262 grp = getgrgid_r(gid, grp, (
char*)(grp + 1),
size);
264 # elif NCBI_HAVE_GETPWUID_R == 5
266 int x_errno = getgrgid_r(gid, grp,(
char*)(grp + 1),
size, &grp);
273 # error "Unknown value of NCBI_HAVE_GETPWUID_R: 4 or 5 expected."
277 if (grp || errno != ERANGE) {
283 # ifdef _SC_GETGR_R_SIZE_MAX
284 long sc = sysconf(_SC_GETGR_R_SIZE_MAX);
285 maxsize = sc > 0 ? (size_t) sc :
size;
290 "getgrgid_r() parse buffer too small ("
292 +
string(
size < maxsize
297 if (
size < maxsize) {
299 buf =
new char[
sizeof(*grp) +
size];
312 buf =
new char[
sizeof(*grp) +
size];
315 if (grp && grp->gr_name) {
316 group.assign(grp->gr_name);
333 #if defined(NCBI_OS_SOLARIS) || \
334 (defined(HAVE_GETPWUID) && !defined(NCBI_HAVE_GETPWUID_R))
336 const struct group* grp = getgrnam(group.c_str());
337 gid = grp ? grp->gr_gid : (gid_t)(-1);
339 #elif defined(NCBI_HAVE_GETPWUID_R)
342 char x_buf[
sizeof(*grp) +
GRP_BUF];
347 grp = (
struct group*)
buf;
349 # if NCBI_HAVE_GETPWUID_R == 4
351 grp = getgrnam_r(group.c_str(), grp, (
char*)(grp + 1),
size);
353 # elif NCBI_HAVE_GETPWUID_R == 5
355 int x_errno = getgrnam_r(group.c_str(),
356 grp, (
char*)(grp + 1),
size, &grp);
363 # error "Unknown value of NCBI_HAVE_GETPWUID_R: 4 or 5 expected."
367 if (grp || errno != ERANGE) {
373 # ifdef _SC_GETGR_R_SIZE_MAX
374 long sc = sysconf(_SC_GETGR_R_SIZE_MAX);
375 maxsize = sc > 0 ? (size_t) sc :
size;
380 "getgrnam_r() parse buffer too small ("
382 +
string(
size < maxsize
387 if (
size < maxsize) {
389 buf =
new char[
sizeof(*grp) +
size];
402 buf =
new char[
sizeof(*grp) +
size];
405 gid = grp ? grp->gr_gid : (gid_t)(-1);
420 #if defined(NCBI_OS_LINUX)
422 #define PROCFS(pid, file) \
423 "/proc/" + (!pid ? "self" : NStr::NumericToString(pid)) + "/" + file
427 size_t s_ReadFile(
const string& filename,
char*
buf,
size_t size)
446 size_t s_ParseStatusVmValue(
const char* name,
char*
buf,
size_t min_value = 0)
451 char* pos = strstr(
buf, name);
455 size_t n = NStr::StringToNumeric<size_t>(pos+strlen(name)+1,
flags) * 1024;
466 n = s_ReadFile(PROCFS(pid,
"statm"),
buf,
sizeof(
buf));
471 usage.total *= page_size;
472 usage.resident *= page_size;
473 usage.shared *= page_size;
474 usage.text *= page_size;
475 usage.lib *= page_size;
481 n = s_ReadFile(PROCFS(pid,
"status"),
buf,
sizeof(
buf));
484 char* pos = strstr(
buf,
"Vm");
488 usage.total_peak = s_ParseStatusVmValue(
"VmPeak", pos,
usage.total);
489 usage.resident_peak = s_ParseStatusVmValue(
"VmHWM", pos,
usage.resident);
490 usage.data = s_ParseStatusVmValue(
"VmData", pos);
491 usage.swap = s_ParseStatusVmValue(
"VmSwap", pos);
493 usage.text = s_ParseStatusVmValue(
"VmExe", pos);
496 usage.lib = s_ParseStatusVmValue(
"VmLib", pos);
499 usage.stack = s_ParseStatusVmValue(
"VmStk", pos);
506 int CLinuxFeature::GetThreadCount(pid_t pid)
509 string name = PROCFS(pid,
"task");
510 DIR* dir = opendir(name.c_str());
512 while (readdir(dir) !=
NULL) {
526 int CLinuxFeature::GetFileDescriptorsCount(pid_t pid)
529 string name = PROCFS(pid,
"fd");
530 DIR* dir = opendir(name.c_str());
532 while (readdir(dir) !=
NULL) {
546 CLinuxFeature::CProcStat::CProcStat(pid_t pid)
551 size_t n = s_ReadFile(PROCFS(pid,
"stat"),
buf,
sizeof(
buf));
553 m_Storage.reserve(
n);
554 m_Storage.assign(
buf,
n);
558 size_t p1 = m_Storage.find(
'(');
563 size_t p2 = m_Storage.find(
')', p1+1);
567 m_List.push_back(
CTempString(m_Storage, p1+1, p2-p1-1));
568 NStr::Split(m_Storage.data() + p2 + 1,
" ", m_List);
Class for support low level input/output for files.
static unsigned long GetVirtualMemoryPageSize(void)
Return virtual memory page size.
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
static string GetUserNameByUID(uid_t uid)
Look up user name by given numeric user ID.
static gid_t GetGroupGIDByName(const string &group)
Look up numeric group ID by symbolic group name.
static string GetGroupNameByGID(gid_t gid)
Look up group name by given numeric group ID.
static uid_t GetUserUIDByName(const string &user)
Look up numeric user ID by symbolic user name.
The NCBI C++ standard methods for dealing with std::string.
#define ERR_POST_ONCE(message)
Error posting only once during program execution.
void Critical(CExceptionArgs_Base &args)
void Error(CExceptionArgs_Base &args)
static void Set(ECode code)
Set last error using native error code enum.
@ eOpen
Open an existing file, or create a new one.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
#define NCBI_AS_STRING(value)
Convert some value to string even if this value is macro itself.
static list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)
Split a string using specified delimiters.
int TStringToNumFlags
Bitwise OR of "EStringToNumFlags".
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
@ fConvErr_NoThrow
Do not throw an exception on error.
@ fAllowTrailingSymbols
Ignore trailing non-numerics characters.
@ fAllowLeadingSpaces
Ignore leading spaces in converted string.
Definition of all error codes used in corelib (xncbi.lib).
const struct ncbi::grid::netcache::search::fields::SIZE size
Private UNIX specific features.
bool GetMemoryUsage(size_t *total, size_t *resident, size_t *shared)
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
double f(double x_, const double &y_)
Process memory usage information, in bytes.