Skip to content

Commit 5b1f551

Browse files
committed
fix crash on systems with pid 0
fixes #118
1 parent bf08ac4 commit 5b1f551

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/procman.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ class ProcInfo
190190

191191
gchar *security_context;
192192

193-
const guint pid;
194-
guint ppid;
193+
const pid_t pid;
194+
pid_t ppid;
195195
guint uid;
196196

197197
// private:

src/proctable.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,15 @@ update_info (ProcData *procdata, ProcInfo *info)
812812

813813
ProcInfo::cpu_times[info->pid] = info->cpu_time = proctime.rtime;
814814
info->nice = procuid.nice;
815-
info->ppid = procuid.ppid;
815+
816+
// set the ppid only if one can exist
817+
// i.e. pid=0 can never have a parent
818+
if (info->pid > 0) {
819+
info->ppid = procuid.ppid;
820+
}
821+
822+
g_assert(info->pid != info->ppid);
823+
g_assert(info->ppid != -1 || info->pid == 0);
816824

817825
/* get cgroup data */
818826
get_process_cgroup_info(info);
@@ -822,11 +830,14 @@ update_info (ProcData *procdata, ProcInfo *info)
822830

823831

824832
ProcInfo::ProcInfo(pid_t pid)
825-
: tooltip(NULL),
833+
: node(),
834+
pixbuf(),
835+
tooltip(NULL),
826836
name(NULL),
827837
arguments(NULL),
828838
security_context(NULL),
829839
pid(pid),
840+
ppid(-1),
830841
uid(-1)
831842
{
832843
ProcInfo * const info = this;
@@ -943,6 +954,8 @@ refresh_list (ProcData *procdata, const pid_t* pid_list, const guint n)
943954

944955

945956
// inserts the process in the treeview if :
957+
// - it has no parent (ppid = -1),
958+
// ie it is for example the [kernel] on FreeBSD
946959
// - it is init
947960
// - its parent is already in tree
948961
// - its parent is unreachable
@@ -956,7 +969,7 @@ refresh_list (ProcData *procdata, const pid_t* pid_list, const guint n)
956969
// see proctable_update_list (ProcData * const procdata)
957970

958971

959-
if ((*it)->ppid == 0 or in_tree.find((*it)->ppid) != in_tree.end()) {
972+
if ((*it)->ppid <= 0 or in_tree.find((*it)->ppid) != in_tree.end()) {
960973
insert_info_to_tree(*it, procdata);
961974
in_tree.insert((*it)->pid);
962975
it = addition.erase(it);
@@ -1031,6 +1044,10 @@ proctable_update_list (ProcData * const procdata)
10311044
procdata->cpu_total_time = MAX(cpu.total - procdata->cpu_total_time_last, 1);
10321045
procdata->cpu_total_time_last = cpu.total;
10331046

1047+
// FIXME: not sure if glibtop always returns a sorted list of pid
1048+
// but it is important otherwise refresh_list won't find the parent
1049+
std::sort(pid_list, pid_list + proclist.number);
1050+
10341051
refresh_list (procdata, pid_list, proclist.number);
10351052

10361053
selection.restore(procdata->tree);

0 commit comments

Comments
 (0)