Extend buffer for reading lines from /proc.

Apparently a line longer than 255 chars was spotted in the wild:
http://serverfault.com/questions/577939/linux-ps-htop-show-processes-running-for-hundreds-or-thousands-of-days-though-h#comment676098_577939
This commit is contained in:
Hisham Muhammad 2015-12-14 13:27:11 -02:00
parent cccc18dd2f
commit 802e216870
3 changed files with 24 additions and 14 deletions

View File

@ -158,6 +158,8 @@ typedef struct ProcessClass_ {
#define ONE_DECIMAL_M (ONE_DECIMAL_K * ONE_DECIMAL_K) #define ONE_DECIMAL_M (ONE_DECIMAL_K * ONE_DECIMAL_K)
#define ONE_DECIMAL_G (ONE_DECIMAL_M * ONE_DECIMAL_K) #define ONE_DECIMAL_G (ONE_DECIMAL_M * ONE_DECIMAL_K)
extern char Process_pidFormat[20];
void Process_setupColumnWidths(); void Process_setupColumnWidths();
void Process_humanNumber(RichString* str, unsigned long number, bool coloring); void Process_humanNumber(RichString* str, unsigned long number, bool coloring);

View File

@ -78,6 +78,10 @@ typedef struct LinuxProcessList_ {
#define PROCMEMINFOFILE PROCDIR "/meminfo" #define PROCMEMINFOFILE PROCDIR "/meminfo"
#endif #endif
#ifndef PROC_LINE_LENGTH
#define PROC_LINE_LENGTH 512
#endif
}*/ }*/
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
@ -90,11 +94,11 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
if (file == NULL) { if (file == NULL) {
CRT_fatalError("Cannot open " PROCSTATFILE); CRT_fatalError("Cannot open " PROCSTATFILE);
} }
char buffer[256]; char buffer[PROC_LINE_LENGTH + 1];
int cpus = -1; int cpus = -1;
do { do {
cpus++; cpus++;
char * s = fgets(buffer, 255, file); char * s = fgets(buffer, PROC_LINE_LENGTH, file);
(void) s; (void) s;
} while (String_startsWith(buffer, "cpu")); } while (String_startsWith(buffer, "cpu"));
fclose(file); fclose(file);
@ -306,8 +310,8 @@ static bool LinuxProcessList_readStatmFile(LinuxProcess* process, const char* di
int fd = open(filename, O_RDONLY); int fd = open(filename, O_RDONLY);
if (fd == -1) if (fd == -1)
return false; return false;
char buf[256]; char buf[PROC_LINE_LENGTH + 1];
ssize_t rres = xread(fd, buf, 255); ssize_t rres = xread(fd, buf, PROC_LINE_LENGTH);
close(fd); close(fd);
if (rres < 1) return false; if (rres < 1) return false;
@ -361,13 +365,13 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d
process->cgroup = strdup(""); process->cgroup = strdup("");
return; return;
} }
char output[256]; char output[PROC_LINE_LENGTH + 1];
output[0] = '\0'; output[0] = '\0';
char* at = output; char* at = output;
int left = 255; int left = PROC_LINE_LENGTH;
while (!feof(file) && left > 0) { while (!feof(file) && left > 0) {
char buffer[256]; char buffer[PROC_LINE_LENGTH + 1];
char *ok = fgets(buffer, 255, file); char *ok = fgets(buffer, PROC_LINE_LENGTH, file);
if (!ok) break; if (!ok) break;
char* group = strchr(buffer, ':'); char* group = strchr(buffer, ':');
if (!group) break; if (!group) break;
@ -394,9 +398,9 @@ static void LinuxProcessList_readVServerData(LinuxProcess* process, const char*
FILE* file = fopen(filename, "r"); FILE* file = fopen(filename, "r");
if (!file) if (!file)
return; return;
char buffer[256]; char buffer[PROC_LINE_LENGTH + 1];
process->vxid = 0; process->vxid = 0;
while (fgets(buffer, 255, file)) { while (fgets(buffer, PROC_LINE_LENGTH, file)) {
if (String_startsWith(buffer, "VxID:")) { if (String_startsWith(buffer, "VxID:")) {
int vxid; int vxid;
int ok = sscanf(buffer, "VxID:\t%32d", &vxid); int ok = sscanf(buffer, "VxID:\t%32d", &vxid);
@ -425,8 +429,8 @@ static void LinuxProcessList_readOomData(LinuxProcess* process, const char* dirn
FILE* file = fopen(filename, "r"); FILE* file = fopen(filename, "r");
if (!file) if (!file)
return; return;
char buffer[256]; char buffer[PROC_LINE_LENGTH + 1];
if (fgets(buffer, 255, file)) { if (fgets(buffer, PROC_LINE_LENGTH, file)) {
unsigned int oom; unsigned int oom;
int ok = sscanf(buffer, "%32u", &oom); int ok = sscanf(buffer, "%32u", &oom);
if (ok >= 1) { if (ok >= 1) {
@ -677,14 +681,14 @@ static inline double LinuxProcessList_scanCPUTime(LinuxProcessList* this) {
int cpus = this->super.cpuCount; int cpus = this->super.cpuCount;
assert(cpus > 0); assert(cpus > 0);
for (int i = 0; i <= cpus; i++) { for (int i = 0; i <= cpus; i++) {
char buffer[256]; char buffer[PROC_LINE_LENGTH + 1];
unsigned long long int usertime, nicetime, systemtime, idletime; unsigned long long int usertime, nicetime, systemtime, idletime;
unsigned long long int ioWait, irq, softIrq, steal, guest, guestnice; unsigned long long int ioWait, irq, softIrq, steal, guest, guestnice;
ioWait = irq = softIrq = steal = guest = guestnice = 0; ioWait = irq = softIrq = steal = guest = guestnice = 0;
// Depending on your kernel version, // Depending on your kernel version,
// 5, 7, 8 or 9 of these fields will be set. // 5, 7, 8 or 9 of these fields will be set.
// The rest will remain at zero. // The rest will remain at zero.
char* ok = fgets(buffer, 255, file); char* ok = fgets(buffer, PROC_LINE_LENGTH, file);
if (!ok) buffer[0] = '\0'; if (!ok) buffer[0] = '\0';
if (i == 0) if (i == 0)
sscanf(buffer, "cpu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu", &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal, &guest, &guestnice); sscanf(buffer, "cpu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu", &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal, &guest, &guestnice);

View File

@ -59,6 +59,10 @@ typedef struct LinuxProcessList_ {
#define PROCMEMINFOFILE PROCDIR "/meminfo" #define PROCMEMINFOFILE PROCDIR "/meminfo"
#endif #endif
#ifndef PROC_LINE_LENGTH
#define PROC_LINE_LENGTH 512
#endif
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId); ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId);