From e9d61f9f55ef0541a068a8e5c330dfa3a626e99a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sun, 23 Apr 2023 19:54:22 +0200 Subject: [PATCH] Show warnings on failures of interactive user actions --- Affinity.c | 27 +++++++++++++++++++-------- Process.c | 10 +++++++++- Scheduling.c | 3 +++ linux/LinuxProcess.c | 8 +++++++- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/Affinity.c b/Affinity.c index dc6a7fba04..2d2d44eb34 100644 --- a/Affinity.c +++ b/Affinity.c @@ -10,8 +10,10 @@ in the source distribution for its full text. #include "Affinity.h" +#include #include +#include "ScreenWarning.h" #include "XUtils.h" #if defined(HAVE_LIBHWLOC) @@ -54,9 +56,11 @@ void Affinity_add(Affinity* this, unsigned int id) { Affinity* Affinity_get(const Process* proc, ProcessList* pl) { hwloc_cpuset_t cpuset = hwloc_bitmap_alloc(); - bool ok = (hwloc_get_proc_cpubind(pl->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0); + int r = hwloc_get_proc_cpubind(pl->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG); + if (r < 0) + ScreenWarning_add("Failed to get affinity for process %d (%s): %s", proc->pid, proc->procComm, strerror(errno)); Affinity* affinity = NULL; - if (ok) { + if (r == 0) { affinity = Affinity_new(pl); if (hwloc_bitmap_last(cpuset) == -1) { for (unsigned int i = 0; i < pl->existingCPUs; i++) { @@ -79,18 +83,22 @@ bool Affinity_set(Process* proc, Arg arg) { for (unsigned int i = 0; i < this->used; i++) { hwloc_bitmap_set(cpuset, this->cpus[i]); } - bool ok = (hwloc_set_proc_cpubind(this->pl->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0); + int r = hwloc_set_proc_cpubind(this->pl->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG); + if (r < 0) + ScreenWarning_add("Failed to set affinity for process %d (%s): %s", proc->pid, proc->procComm, strerror(errno)); hwloc_bitmap_free(cpuset); - return ok; + return r == 0; } #elif defined(HAVE_AFFINITY) Affinity* Affinity_get(const Process* proc, ProcessList* pl) { cpu_set_t cpuset; - bool ok = (sched_getaffinity(proc->pid, sizeof(cpu_set_t), &cpuset) == 0); - if (!ok) + int r = sched_getaffinity(proc->pid, sizeof(cpu_set_t), &cpuset); + if (r < 0) { + ScreenWarning_add("Failed to get affinity for process %d (%s): %s", proc->pid, proc->procComm, strerror(errno)); return NULL; + } Affinity* affinity = Affinity_new(pl); for (unsigned int i = 0; i < pl->existingCPUs; i++) { @@ -108,8 +116,11 @@ bool Affinity_set(Process* proc, Arg arg) { for (unsigned int i = 0; i < this->used; i++) { CPU_SET(this->cpus[i], &cpuset); } - bool ok = (sched_setaffinity(proc->pid, sizeof(unsigned long), &cpuset) == 0); - return ok; + int r = sched_setaffinity(proc->pid, sizeof(unsigned long), &cpuset); + if (r < 0) + ScreenWarning_add("Failed to set affinity for process %d (%s): %s", proc->pid, proc->procComm, strerror(errno)); + + return r == 0; } #endif diff --git a/Process.c b/Process.c index c92d01b99d..6f11e90dad 100644 --- a/Process.c +++ b/Process.c @@ -11,6 +11,7 @@ in the source distribution for its full text. #include "Process.h" #include +#include #include #include #include @@ -29,6 +30,7 @@ in the source distribution for its full text. #include "DynamicColumn.h" #include "RichString.h" #include "Scheduling.h" +#include "ScreenWarning.h" #include "Settings.h" #include "XUtils.h" @@ -1134,6 +1136,8 @@ bool Process_setPriority(Process* this, int priority) { int old_prio = getpriority(PRIO_PROCESS, this->pid); int err = setpriority(PRIO_PROCESS, this->pid, priority); + if (err < 0) + ScreenWarning_add("Failed to set priority %d for process %d (%s): %s", priority, this->pid, this->procComm, strerror(errno)); if (err == 0 && old_prio != getpriority(PRIO_PROCESS, this->pid)) { this->nice = priority; @@ -1146,7 +1150,11 @@ bool Process_changePriorityBy(Process* this, Arg delta) { } bool Process_sendSignal(Process* this, Arg sgn) { - return kill(this->pid, sgn.i) == 0; + int r = kill(this->pid, sgn.i); + if (r < 0) + ScreenWarning_add("Failed to send signal %d to process %d (%s): %s", sgn.i, this->pid, this->procComm, strerror(errno)); + + return r == 0; } int Process_compare(const void* v1, const void* v2) { diff --git a/Scheduling.c b/Scheduling.c index 5ca49ae2dc..44a702f557 100644 --- a/Scheduling.c +++ b/Scheduling.c @@ -17,6 +17,7 @@ in the source distribution for its full text. #include "Macros.h" #include "Object.h" #include "Panel.h" +#include "ScreenWarning.h" #include "XUtils.h" @@ -113,6 +114,8 @@ bool Scheduling_setPolicy(Process* proc, Arg arg) { #endif int r = sched_setscheduler(proc->pid, policy, ¶m); + if (r == -1) + ScreenWarning_add("Failed to set scheduling policy %d for process %d (%s): %s", policy, proc->pid, proc->procComm, strerror(errno)); /* POSIX says on success the previous scheduling policy should be returned, * but Linux always returns 0. */ diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index 8f9c346205..ab63918698 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -8,6 +8,7 @@ in the source distribution for its full text. #include "linux/LinuxProcess.h" +#include #include #include #include @@ -20,6 +21,7 @@ in the source distribution for its full text. #include "ProvideCurses.h" #include "RichString.h" #include "Scheduling.h" +#include "ScreenWarning.h" #include "XUtils.h" #include "linux/IOPriority.h" @@ -159,7 +161,9 @@ IOPriority LinuxProcess_updateIOPriority(LinuxProcess* this) { bool LinuxProcess_setIOPriority(Process* this, Arg ioprio) { // Other OSes masquerading as Linux (NetBSD?) don't have this syscall #ifdef SYS_ioprio_set - syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, this->pid, ioprio.i); + int r = syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, this->pid, ioprio.i); + if (r < 0) + ScreenWarning_add("Failed to set IO priority %d for process %d (%s): %s", ioprio.i, this->pid, this->procComm, strerror(errno)); #endif return (LinuxProcess_updateIOPriority((LinuxProcess*)this) == ioprio.i); } @@ -187,6 +191,8 @@ bool LinuxProcess_changeAutogroupPriorityBy(Process* this, Arg delta) { rewind(file); xSnprintf(buffer, sizeof(buffer), "%d", nice + delta.i); success = fputs(buffer, file) > 0; + if (!success) + ScreenWarning_add("Failed to set autogroup for process %d (%s): %s", this->pid, this->procComm, strerror(errno)); } else { success = false; }