arch/i386/kernel/entry.S   |    6 +-
 arch/ppc/kernel/misc.S     |    6 +-
 include/asm-ppc/unistd.h   |    4 -
 include/linux/capability.h |    1 
 kernel/sched.c             |  100 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 109 insertions(+), 8 deletions(-)


diff -urN linux-2.4.20/arch/i386/kernel/entry.S linux/arch/i386/kernel/entry.S
--- linux-2.4.20/arch/i386/kernel/entry.S	2002-11-28 18:53:09.000000000 -0500
+++ linux/arch/i386/kernel/entry.S	2003-01-14 19:51:09.000000000 -0500
@@ -1,4 +1,4 @@
-/*
+	*
  *  linux/arch/i386/entry.S
  *
  *  Copyright (C) 1991, 1992  Linus Torvalds
@@ -645,8 +645,8 @@
  	.long SYMBOL_NAME(sys_tkill)
 	.long SYMBOL_NAME(sys_ni_syscall)	/* reserved for sendfile64 */
 	.long SYMBOL_NAME(sys_ni_syscall)	/* 240 reserved for futex */
-	.long SYMBOL_NAME(sys_ni_syscall)	/* reserved for sched_setaffinity */
-	.long SYMBOL_NAME(sys_ni_syscall)	/* reserved for sched_getaffinity */
+	.long SYMBOL_NAME(sys_sched_setaffinity)
+	.long SYMBOL_NAME(sys_sched_getaffinity)
 	.long SYMBOL_NAME(sys_ni_syscall)	/* sys_set_thread_area */
 	.long SYMBOL_NAME(sys_ni_syscall)	/* sys_get_thread_area */
 	.long SYMBOL_NAME(sys_ni_syscall)	/* 245 sys_io_setup */
diff -urN linux-2.4.20/arch/ppc/kernel/misc.S linux/arch/ppc/kernel/misc.S
--- linux-2.4.20/arch/ppc/kernel/misc.S	2002-11-28 18:53:11.000000000 -0500
+++ linux/arch/ppc/kernel/misc.S	2003-01-14 19:51:59.000000000 -0500
@@ -1,4 +1,4 @@
-/*
+	*
  * BK Id: %F% %I% %G% %U% %#%
  */
 /*
@@ -1174,8 +1174,8 @@
 	.long sys_lremovexattr
 	.long sys_fremovexattr	/* 220  */
 	.long sys_ni_syscall 	/*	reserved for sys_futex */
-	.long sys_ni_syscall 	/*	reserved for sys_sched_setaffinity */
-	.long sys_ni_syscall 	/*	reserved for sys_sched_getaffinity */
+	.long sys_sched_setaffinity
+	.long sys_sched_getaffinity
 	.long sys_ni_syscall 	/*	reserved for sys_security */
 	.long sys_ni_syscall 	/* 225	reserved for Tux */
 	.long sys_ni_syscall 	/*	reserved for sys_sendfile64 */
diff -urN linux-2.4.20/include/asm-ppc/unistd.h linux/include/asm-ppc/unistd.h
--- linux-2.4.20/include/asm-ppc/unistd.h	2002-11-28 18:53:15.000000000 -0500
+++ linux/include/asm-ppc/unistd.h	2003-01-14 19:50:23.000000000 -0500
@@ -228,10 +228,10 @@
 #define __NR_removexattr	218
 #define __NR_lremovexattr	219
 #define __NR_fremovexattr	220
-#if 0
-#define __NR_futex		221
 #define __NR_sched_setaffinity	222
 #define __NR_sched_getaffinity	223
+#if 0
+#define __NR_futex		221
 #define __NR_security		224
 #define __NR_tuxcall		225
 #define __NR_sendfile64		226
diff -urN linux-2.4.20/include/linux/capability.h linux/include/linux/capability.h
--- linux-2.4.20/include/linux/capability.h	2001-11-22 14:46:19.000000000 -0500
+++ linux/include/linux/capability.h	2003-01-14 19:49:40.000000000 -0500
@@ -243,6 +243,7 @@
 /* Allow use of FIFO and round-robin (realtime) scheduling on own
    processes and setting the scheduling algorithm used by another
    process. */
+/* Allow setting cpu affinity on other processes */
 
 #define CAP_SYS_NICE         23
 
diff -urN linux-2.4.20/kernel/sched.c linux/kernel/sched.c
--- linux-2.4.20/kernel/sched.c	2002-12-17 17:06:40.000000000 -0500
+++ linux/kernel/sched.c	2003-01-14 19:52:32.000000000 -0500
@@ -1139,6 +1139,106 @@
 	return retval;
 }
 
+/**
+ * sys_sched_setaffinity - set the cpu affinity of a process
+ * @pid: pid of the process
+ * @len: length in bytes of the bitmask pointed to by user_mask_ptr
+ * @user_mask_ptr: user-space pointer to the new cpu mask
+ */
+asmlinkage int sys_sched_setaffinity(pid_t pid, unsigned int len,
+				     unsigned long *user_mask_ptr)
+{
+	unsigned long new_mask;
+	struct task_struct *p;
+	int retval, reschedule = 0;
+
+	if (len < sizeof(new_mask))
+		return -EINVAL;
+
+	if (copy_from_user(&new_mask, user_mask_ptr, sizeof(new_mask)))
+		return -EFAULT;
+
+	new_mask &= cpu_online_map;
+	if (!new_mask)
+		return -EINVAL;
+
+	read_lock_irq(&tasklist_lock);
+	spin_lock(&runqueue_lock);
+
+	retval = -ESRCH;
+	p = find_process_by_pid(pid);
+	if (!p)
+		goto out_unlock;
+
+	retval = -EPERM;
+	if ((current->euid != p->euid) && (current->euid != p->uid) &&
+			!capable(CAP_SYS_NICE))
+		goto out_unlock;
+
+	p->cpus_allowed = new_mask;
+
+#ifdef CONFIG_SMP
+	if (!(p->cpus_runnable & p->cpus_allowed)) {
+		if (p == current)
+			reschedule = 1;
+		else {
+			p->need_resched = 1;
+			smp_send_reschedule(p->processor);
+		}
+	}
+#endif
+
+	retval = 0;
+out_unlock:
+	spin_unlock(&runqueue_lock);
+	read_unlock_irq(&tasklist_lock);
+
+	if (reschedule)
+		schedule();
+
+	return retval;
+}
+
+/**
+ * sys_sched_getaffinity - get the cpu affinity of a process
+ * @pid: pid of the process
+ * @len: length in bytes of the bitmask pointed to by user_mask_ptr
+ * @user_mask_ptr: user-space pointer to hold the current mask
+ */
+asmlinkage int sys_sched_getaffinity(pid_t pid, unsigned int len,
+				     unsigned long *user_mask_ptr)
+{
+	unsigned long mask;
+	unsigned int real_len;
+	struct task_struct *p;
+	int retval;
+
+	real_len = sizeof(mask);
+
+	if (len < real_len)
+		return -EINVAL;
+
+	read_lock_irq(&tasklist_lock);
+	spin_lock(&runqueue_lock);
+
+	retval = -ESRCH;
+	p = find_process_by_pid(pid);
+	if (!p)
+		goto out_unlock;
+
+	retval = 0;
+	mask = p->cpus_allowed & cpu_online_map;
+
+out_unlock:
+	spin_unlock(&runqueue_lock);
+	read_unlock_irq(&tasklist_lock);
+	if (retval)
+		return retval;
+	if (copy_to_user(user_mask_ptr, &mask, real_len))
+		return -EFAULT;
+	return real_len;
+}
+
 static void show_task(struct task_struct * p)
 {
 	unsigned long free = 0;