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

View File

@ -122,8 +122,8 @@ typedef struct LinuxProcess_ {
double io_rate_write_bps; double io_rate_write_bps;
#endif #endif
#ifdef HAVE_OPENVZ #ifdef HAVE_OPENVZ
unsigned int ctid; char* ctid;
unsigned int vpid; pid_t vpid;
#endif #endif
#ifdef HAVE_VSERVER #ifdef HAVE_VSERVER
unsigned int vxid; 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) { static void LinuxProcessList_readOpenVZData(LinuxProcess* process, const char* dirname, const char* name) {
if ( (access(PROCDIR "/vz", R_OK) != 0)) { if ( (access(PROCDIR "/vz", R_OK) != 0)) {
free(process->ctid);
process->ctid = NULL;
process->vpid = process->super.pid; process->vpid = process->super.pid;
process->ctid = 0;
return; return;
} }
char filename[MAX_NAME+1]; 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"); FILE* file = fopen(filename, "r");
if (!file) if (!file) {
free(process->ctid);
process->ctid = NULL;
process->vpid = process->super.pid;
return; return;
(void)! fscanf(file, }
"%*32u %*32s %*1c %*32u %*32u %*32u %*32u %*32u %*32u %*32u "
"%*32u %*32u %*32u %*32u %*32u %*32u %*32u %*32u " bool foundEnvID = false;
"%*32u %*32u %*32u %*32u %*32u %*32u %*32u %*32u " bool foundVPid = false;
"%*32u %*32u %*32u %*32u %*32u %*32u %*32u %*32u " char linebuf[256];
"%*32u %*32u %*32u %*32u %*32u %*32u %*32u %*32u " while(fgets(linebuf, sizeof(linebuf), file) != NULL) {
"%*32u %*32u %*32u %*32u %*32u %*32u %*32u " if(strchr(linebuf, '\n') == NULL) {
"%*32u %*32u %32u %32u", // Partial line, skip to end of this line
&process->vpid, &process->ctid); 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); fclose(file);
if(!foundEnvID) {
free(process->ctid);
process->ctid = NULL;
}
if(!foundVPid) {
process->vpid = process->super.pid;
}
} }
#endif #endif