Linux Kernel Patch; setpriority

Stephen Baker (stbaker@cisco.com)
Wed, 27 Mar 2002 14:59:13 -0600


All,

This patch will allow a process or thread to changes it's priority
dynamically based on it's capabilities. In our case we wanted to use
threads with Linux. To have true priorities we need root to use
SCHED_FIFO or SCHED_RR; in many case root access is not allowed but we
still wanted priorities. So we started using setpriority to change a
threads priority. Now we used nice values from 19 to 0 which did not
require root access. In some cases a thread need to raise it's nice
level and this would fail. I also saw a note man renice(8) that said
this bug exists.
So the following patch address this problem. It allows any process or
thread to raise or lower it's nice value for it's current capability.
For example a CAP_SYS_NICE process can use 19 to -20 for it's value and
a normal user can use 19 to 0. By capping normal user to zero then we
don't have any problems with conflicts with higher priority programs in
the system since zero is the default value.

SB

--- linux-2.4.9-31/kernel/sys.c Wed Mar 27 13:11:10 2002
+++ linux/kernel/sys.c Wed Mar 27 13:09:36 2002
@@ -194,6 +194,12 @@
return 0;
}

+/*
+ * Allow the process to adjust it's priority higher or lower.
+ * If the process has CAP_SYS_NICE set then we can use
+ * -20 to 19. Otherwise we use 0 to 19 as our valid priority
+ * range.
+ */
asmlinkage long sys_setpriority(int which, int who, int niceval)
{
struct task_struct *p;
@@ -220,7 +226,8 @@
}
if (error == -ESRCH)
error = 0;
- if (niceval < p->nice && !capable(CAP_SYS_NICE))
+ if ((niceval < 0) &&
+ (niceval < p->nice && !capable(CAP_SYS_NICE)))
error = -EACCES;
else
p->nice = niceval;

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/