Commit 00d1fb01 authored by Hisham Muhammad's avatar Hisham Muhammad
Browse files

Linux: change how kernel threads are detected

Use the same method that ps and top use to determine if a
process is a kernel thread on Linux: check if cmdline is empty.

Thanks to @wangqr's investigation reported here:
https://github.com/hishamhm/htop/issues/761#issuecomment-375306069

Fixes #761.
parent dd175b68
...@@ -117,6 +117,7 @@ typedef enum LinuxProcessFields { ...@@ -117,6 +117,7 @@ typedef enum LinuxProcessFields {
typedef struct LinuxProcess_ { typedef struct LinuxProcess_ {
Process super; Process super;
bool isKernelThread;
IOPriority ioPriority; IOPriority ioPriority;
unsigned long int cminflt; unsigned long int cminflt;
unsigned long int cmajflt; unsigned long int cmajflt;
...@@ -185,7 +186,7 @@ typedef struct LinuxProcess_ { ...@@ -185,7 +186,7 @@ typedef struct LinuxProcess_ {
} LinuxProcess; } LinuxProcess;
#ifndef Process_isKernelThread #ifndef Process_isKernelThread
#define Process_isKernelThread(_process) (_process->pgrp == 0) #define Process_isKernelThread(_process) ((LinuxProcess*)(_process)->isKernelThread)
#endif #endif
#ifndef Process_isUserlandThread #ifndef Process_isUserlandThread
......
...@@ -109,6 +109,7 @@ typedef enum LinuxProcessFields { ...@@ -109,6 +109,7 @@ typedef enum LinuxProcessFields {
typedef struct LinuxProcess_ { typedef struct LinuxProcess_ {
Process super; Process super;
bool isKernelThread;
IOPriority ioPriority; IOPriority ioPriority;
unsigned long int cminflt; unsigned long int cminflt;
unsigned long int cmajflt; unsigned long int cmajflt;
...@@ -177,7 +178,7 @@ typedef struct LinuxProcess_ { ...@@ -177,7 +178,7 @@ typedef struct LinuxProcess_ {
} LinuxProcess; } LinuxProcess;
#ifndef Process_isKernelThread #ifndef Process_isKernelThread
#define Process_isKernelThread(_process) (_process->pgrp == 0) #define Process_isKernelThread(_process) (((LinuxProcess*)(_process))->isKernelThread)
#endif #endif
#ifndef Process_isUserlandThread #ifndef Process_isUserlandThread
......
...@@ -748,9 +748,6 @@ static void setCommand(Process* process, const char* command, int len) { ...@@ -748,9 +748,6 @@ static void setCommand(Process* process, const char* command, int len) {
} }
static bool LinuxProcessList_readCmdlineFile(Process* process, const char* dirname, const char* name) { static bool LinuxProcessList_readCmdlineFile(Process* process, const char* dirname, const char* name) {
if (Process_isKernelThread(process))
return true;
char filename[MAX_NAME+1]; char filename[MAX_NAME+1];
xSnprintf(filename, MAX_NAME, "%s/%s/cmdline", dirname, name); xSnprintf(filename, MAX_NAME, "%s/%s/cmdline", dirname, name);
int fd = open(filename, O_RDONLY); int fd = open(filename, O_RDONLY);
...@@ -762,7 +759,10 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, const char* dirna ...@@ -762,7 +759,10 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, const char* dirna
close(fd); close(fd);
int tokenEnd = 0; int tokenEnd = 0;
int lastChar = 0; int lastChar = 0;
if (amtRead <= 0) { if (amtRead == 0) {
((LinuxProcess*)process)->isKernelThread = true;
return true;
} else if (amtRead < 0) {
return false; return false;
} }
for (int i = 0; i < amtRead; i++) { for (int i = 0; i < amtRead; i++) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment