]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
taskset: warn if affinity is not settable
authorKarel Zak <kzak@redhat.com>
Thu, 11 Aug 2022 08:59:40 +0000 (10:59 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 11 Aug 2022 08:59:40 +0000 (10:59 +0200)
Let's read /proc/#/stat to get process flags and print warning
if the affinity is not settable.

Based on patch from Carsten.

Tested-by: Carsten Emde <C.Emde@osadl.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
schedutils/taskset.c

index 0ab7d12e20775de50c8dc7a8e7f39c73849efdc0..0872aa273a9500f470d74e67df2cd41f2b16320b 100644 (file)
 #include "c.h"
 #include "closestream.h"
 
+#ifndef PF_NO_SETAFFINITY
+# define PF_NO_SETAFFINITY 0x04000000
+#endif
+
 struct taskset {
        pid_t           pid;            /* task PID */
        cpu_set_t       *set;           /* task CPU mask */
@@ -112,6 +116,7 @@ static void __attribute__((__noreturn__)) err_affinity(pid_t pid, int set)
        err(EXIT_FAILURE, msg, pid ? pid : getpid());
 }
 
+
 static void do_taskset(struct taskset *ts, size_t setsize, cpu_set_t *set)
 {
        /* read the current mask */
@@ -125,8 +130,22 @@ static void do_taskset(struct taskset *ts, size_t setsize, cpu_set_t *set)
                return;
 
        /* set new mask */
-       if (sched_setaffinity(ts->pid, setsize, set) < 0)
+       if (sched_setaffinity(ts->pid, setsize, set) < 0) {
+               uintmax_t flags = 0;
+               struct path_cxt *pc;
+               int errsv = errno;
+
+               if (errno != EPERM
+                   && (pc = ul_new_procfs_path(ts->pid, NULL))
+                   && procfs_process_get_stat_nth(pc, 9, &flags) == 0
+                   && (flags & PF_NO_SETAFFINITY)) {
+                       warnx(_("affinity cannot be set due to PF_NO_SETAFFINITY flag set"));
+                       errno = EINVAL;
+               } else
+                       errno = errsv;
+
                err_affinity(ts->pid, 1);
+       }
 
        /* re-read the current mask */
        if (ts->pid) {