10 #include <sys/types.h>
17 static void *xcalloc(
void *pointer,
int size)
22 if (!(ret = calloc(1, size)))
24 fprintf(stderr,
"xcalloc: allocation error, size = %d\n", size);
30 static void *xmalloc(
unsigned int size)
39 fprintf(stderr,
"xmalloc: malloc(%u) failed", size);
45 static int task_dir_missing;
82 #define F(x) {#x, sizeof(#x)-1, &&case_##x},
83 #define NUL {"", 0, 0},
100 static const unsigned char asso[] =
102 61, 61, 61, 61, 61, 61, 61, 61, 61, 61,
103 61, 61, 61, 61, 61, 61, 61, 61, 61, 61,
104 61, 61, 61, 61, 61, 61, 61, 61, 61, 61,
105 61, 61, 61, 61, 61, 61, 61, 61, 61, 61,
106 61, 61, 61, 61, 61, 61, 61, 61, 61, 61,
107 61, 61, 61, 61, 61, 61, 61, 61, 15, 61,
108 61, 61, 61, 61, 61, 61, 30, 3, 5, 5,
109 61, 5, 61, 8, 61, 61, 3, 61, 10, 61,
110 6, 61, 13, 0, 30, 25, 0, 61, 61, 61,
111 61, 61, 61, 61, 61, 61, 61, 3, 61, 13,
112 0, 0, 61, 30, 61, 25, 61, 61, 61, 0,
113 61, 61, 61, 61, 5, 61, 0, 61, 61, 61,
114 0, 61, 61, 61, 61, 61, 61, 61
188 entry = table[63 & (asso[(
unsigned char)S[3]] + asso[(
unsigned char)S[2]] + asso[(
unsigned char)S[0]])];
189 colon = strchr(S,
':');
203 while(u < sizeof P->
cmd - 1u)
227 memcpy(P->signal, S, 16);
228 P->signal[16] =
'\0';
231 memcpy(P->blocked, S, 16);
232 P->blocked[16] =
'\0';
235 memcpy(P->sigcatch, S, 16);
236 P->sigcatch[16] =
'\0';
239 memcpy(P->sigignore, S, 16);
240 P->sigignore[16] =
'\0';
243 memcpy(P->_sigpnd, S, 16);
244 P->_sigpnd[16] =
'\0';
251 Tgid = strtol(S,&S,10);
254 Pid = strtol(S,&S,10);
257 P->ppid = strtol(S,&S,10);
260 Threads = strtol(S,&S,10);
263 P->ruid = strtol(S,&S,10);
264 P->euid = strtol(S,&S,10);
265 P->suid = strtol(S,&S,10);
266 P->fuid = strtol(S,&S,10);
269 P->rgid = strtol(S,&S,10);
270 P->egid = strtol(S,&S,10);
271 P->sgid = strtol(S,&S,10);
272 P->fgid = strtol(S,&S,10);
275 P->vm_data = strtol(S,&S,10);
278 P->vm_exe = strtol(S,&S,10);
281 P->vm_lock = strtol(S,&S,10);
284 P->vm_lib = strtol(S,&S,10);
287 P->vm_rss = strtol(S,&S,10);
290 P->vm_size = strtol(S,&S,10);
293 P->vm_stack = strtol(S,&S,10);
298 if(!is_proc || !P->signal[0])
300 memcpy(P->signal, P->_sigpnd, 16);
301 P->signal[16] =
'\0';
332 S = strchr(S,
'(') + 1;
333 tmp = strrchr((
char*)S,
')');
335 if(
unlikely(num >=
sizeof P->cmd)) num =
sizeof P->cmd - 1;
336 memcpy(P->cmd, S, num);
342 "%10d %10d %10d %10d %10d "
343 "%20lu %20lu %20lu %20lu %20lu "
344 "%20llu %20llu %20llu %20llu "
351 "%20lu %20lu %20lu %20lu %20lu %20lu "
357 &P->ppid, &P->pgrp, &P->session, &P->tty, &P->tpgid,
358 &P->flags, &P->min_flt, &P->cmin_flt, &P->maj_flt, &P->cmaj_flt,
359 &P->utime, &P->stime, &P->cutime, &P->cstime,
360 &P->priority, &P->nice,
366 &P->rss_rlim, &P->start_code, &P->end_code, &P->start_stack, &P->kstk_esp, &P->kstk_eip,
370 &P->exit_signal, &P->processor,
372 &P->rtprio, &P->sched
385 sscanf(s,
"%20ld %20ld %20ld %20ld %20ld %20ld %20ld", &P->size, &P->resident, &P->share, &P->trs, &P->lrs, &P->drs, &P->dt);
389 static int file2str(
const char *directory,
const char *what,
char *ret,
int cap)
391 static char filename[80];
394 sprintf(filename,
"%s/%s", directory, what);
395 fd = open(filename, O_RDONLY, 0);
398 num_read = read(fd, ret, cap - 1);
402 ret[num_read] =
'\0';
412 # define XinL(T, X, L) ( { \
413 T x = (X), *l = (L); \
414 while (*l && *l != x) l++; \
419 # define XinLN(T, X, L, N) ( { \
420 T x = (X), *l = (L); \
421 int i = 0, n = (N); \
422 while (i < n && l[i] != x) i++; \
423 i < n && l[i] == x; \
432 static struct stat sb;
433 static char sbuf[1024];
434 char *
restrict const path = PT->path;
435 unsigned flags = PT->flags;
437 if (
unlikely(stat(path, &sb) == -1))
440 if ((flags &
PROC_UID) && !
XinLN(uid_t, sb.st_uid, PT->uids, PT->nuid))
448 if (
unlikely( file2str(path,
"stat", sbuf,
sizeof sbuf) == -1 ))
455 if (
likely( file2str(path,
"statm", sbuf,
sizeof sbuf) != -1 ))
461 if (
likely( file2str(path,
"status", sbuf,
sizeof sbuf) != -1 ))
463 status2proc(sbuf, p, 1);
470 p->wchan = (long)~0ull;
482 static struct direct *ent;
483 char *
restrict const path = PT->path;
486 ent = readdir(PT->procfs);
492 p->tgid = strtoul(ent->d_name, NULL, 10);
494 memcpy(path,
"/proc/", 6);
495 strcpy(path+6, ent->d_name);
504 static struct direct *ent;
505 if(PT->taskdir_user != p->tgid)
509 closedir(PT->taskdir);
512 snprintf(path,
PROCPATHLEN,
"/proc/%d/task", p->tgid);
513 PT->taskdir = opendir(path);
514 if(!PT->taskdir)
return 0;
515 PT->taskdir_user = p->tgid;
519 ent = readdir(PT->taskdir);
525 t->tid = strtoul(ent->d_name, NULL, 10);
528 snprintf(path,
PROCPATHLEN,
"/proc/%d/task/%s", p->tgid, ent->d_name);
536 char *
restrict const path = PT->path;
537 pid_t tgid = *(PT->pids)++;
565 p = (
proc_t*)xcalloc(p,
sizeof *p);
574 ret = PT->reader(PT,p);
586 static PROCTAB* openproc(
int flags, ...)
595 task_dir_missing = stat(
"/proc/self/task", &sbuf);
602 PT->
reader = simple_readproc;
606 PT->
finder = listed_nextpid;
610 PT->
procfs = opendir(
"/proc");
611 if(!PT->
procfs)
return NULL;
612 PT->
finder = simple_nextpid;
617 if (flags & PROC_PID)
618 PT->
pids = va_arg(ap, pid_t*);
621 PT->
uids = va_arg(ap, uid_t*);
622 PT->
nuid = va_arg(ap,
int);
630 static void closeproc(
PROCTAB* PT)
638 memset(PT,
'#',
sizeof(
PROCTAB));
644 static void freeproc(
proc_t* p)
664 static char path[PATH_MAX], sbuf[1024];
667 sprintf(path,
"/proc/%d", pid);
668 if (stat(path, &statbuf))
675 p = (
proc_t*)xcalloc(p,
sizeof *p);
677 if (file2str(path,
"stat", sbuf,
sizeof sbuf) >= 0)
679 if (file2str(path,
"statm", sbuf,
sizeof sbuf) >= 0)
681 if (file2str(path,
"status", sbuf,
sizeof sbuf) >= 0)
682 status2proc(sbuf, p, 0 );
689 #include <Poco/Logger.h>
690 #include "Poco/TemporaryFile.h"
706 Poco::Mutex::ScopedLock lock(
mutex);
714 Poco::Mutex::ScopedLock lock(
mutex);
725 HCE::drce::DRCEReadProcessData::getChildrenList(pid, processes);
726 processes.push_back(pid);
730 void DRCEReadProcessData::getChildrenList(pid_t pid, std::vector<pid_t>& children)
733 for (
size_t i=0;i<processes.size();++i)
735 if (processes[i].ppid == pid)
737 children.push_back(processes[i].pid);
738 DRCEReadProcessData::getChildrenList(processes[i].pid, children);
746 Poco::Mutex::ScopedLock lock(
mutex);
752 proc_t* procInfo=
nullptr;
753 while ((procInfo = readproc(proc,
nullptr)) !=
nullptr)
756 processData.
pid = procInfo->
tid;
765 processData.
cmd = procInfo->
cmd;
766 processes.push_back(processData);
777 Poco::Mutex::ScopedLock lock(
mutex);
783 proc_t* procInfo =
nullptr;
784 procInfo = getProcStats(pid, procInfo);
787 processData.
pid = procInfo->
tid;
796 processData.
cmd = procInfo->
cmd;
807 Poco::Mutex::ScopedLock lock(
mutex);
814 proc_t* procInfo=
nullptr;
815 while ((procInfo = readproc(proc,
nullptr)) !=
nullptr)
818 threadsCount +=
static_cast<unsigned int>(procInfo->
nlwp);