Handle parsing envID & VPid from process status file

Fixes #55
Fixes #192
This commit is contained in:
Benny Baumann 2020-09-30 23:46:52 +02:00 committed by cgzones
parent ba282cfe19
commit 2970cae543
3 changed files with 90 additions and 18 deletions

View File

@ -148,6 +148,9 @@ void Process_delete(Object* cast) {
Process_done((Process*)cast);
#ifdef HAVE_CGROUP
free(this->cgroup);
#endif
#ifdef HAVE_OPENVZ
free(this->ctid);
#endif
free(this->secattr);
free(this->ttyDevice);
@ -253,7 +256,7 @@ void LinuxProcess_writeField(Process* this, RichString* str, ProcessField field)
}
#endif
#ifdef HAVE_OPENVZ
case CTID: xSnprintf(buffer, n, "%7u ", lp->ctid); break;
case CTID: xSnprintf(buffer, n, "%-8s ", lp->ctid ? lp->ctid : ""); break;
case VPID: xSnprintf(buffer, n, Process_pidFormat, lp->vpid); break;
#endif
#ifdef HAVE_VSERVER
@ -351,7 +354,7 @@ long LinuxProcess_compare(const void* v1, const void* v2) {
#endif
#ifdef HAVE_OPENVZ
case CTID:
return (p2->ctid - p1->ctid);
return strcmp(p1->ctid ? p1->ctid : "", p2->ctid ? p2->ctid : "");
case VPID:
return (p2->vpid - p1->vpid);
#endif

View File

@ -122,8 +122,8 @@ typedef struct LinuxProcess_ {
double io_rate_write_bps;
#endif
#ifdef HAVE_OPENVZ
unsigned int ctid;
unsigned int vpid;
char* ctid;
pid_t vpid;
#endif
#ifdef HAVE_VSERVER
unsigned int vxid;

View File

@ -482,25 +482,94 @@ static bool LinuxProcessList_readSmapsFile(LinuxProcess* process, const char* di
static void LinuxProcessList_readOpenVZData(LinuxProcess* process, const char* dirname, const char* name) {
if ( (access(PROCDIR "/vz", R_OK) != 0)) {
free(process->ctid);
process->ctid = NULL;
process->vpid = process->super.pid;
process->ctid = 0;
return;
}
char filename[MAX_NAME+1];
xSnprintf(filename, MAX_NAME, "%s/%s/stat", dirname, name);
xSnprintf(filename, sizeof(filename), "%s/%s/status", dirname, name);
FILE* file = fopen(filename, "r");
if (!file)
if (!file) {
free(process->ctid);
process->ctid = NULL;
process->vpid = process->super.pid;
return;
(void)! fscanf(file,
"%*32u %*32s %*1c %*32u %*32u %*32u %*32u %*32u %*32u %*32u "
"%*32u %*32u %*32u %*32u %*32u %*32u %*32u %*32u "
"%*32u %*32u %*32u %*32u %*32u %*32u %*32u %*32u "
"%*32u %*32u %*32u %*32u %*32u %*32u %*32u %*32u "
"%*32u %*32u %*32u %*32u %*32u %*32u %*32u %*32u "
"%*32u %*32u %*32u %*32u %*32u %*32u %*32u "
"%*32u %*32u %32u %32u",
&process->vpid, &process->ctid);
}
bool foundEnvID = false;
bool foundVPid = false;
char linebuf[256];
while(fgets(linebuf, sizeof(linebuf), file) != NULL) {
if(strchr(linebuf, '\n') == NULL) {
// Partial line, skip to end of this line
while(fgets(linebuf, sizeof(linebuf), file) != NULL) {
if(strchr(linebuf, '\n') != NULL) {
break;
}
}
continue;
}
char* name_value_sep = strchr(linebuf, ':');
if(name_value_sep == NULL) {
continue;
}
int field;
if(0 == strncasecmp(linebuf, "envID", name_value_sep - linebuf)) {
field = 1;
} else if(0 == strncasecmp(linebuf, "VPid", name_value_sep - linebuf)) {
field = 2;
} else {
continue;
}
do {
name_value_sep++;
} while(*name_value_sep != '\0' && *name_value_sep <= 32);
char* value_end = name_value_sep;
while(*value_end != '\0' && *value_end > 32) {
value_end++;
}
if(name_value_sep == value_end) {
continue;
}
*value_end = '\0';
switch(field) {
case 1:
foundEnvID = true;
if(0 != strcmp(name_value_sep, process->ctid ? process->ctid : "")) {
free(process->ctid);
process->ctid = xStrdup(name_value_sep);
}
break;
case 2:
foundVPid = true;
process->vpid = strtoul(name_value_sep, NULL, 0);
break;
default:
//Sanity Check: Should never reach here, or the implementation is missing something!
assert(false && "OpenVZ handling: Unimplemented case for field handling reached.");
}
}
fclose(file);
if(!foundEnvID) {
free(process->ctid);
process->ctid = NULL;
}
if(!foundVPid) {
process->vpid = process->super.pid;
}
}
#endif