fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / kernel / sys.c
index a87239d..2af7e02 100644 (file)
@@ -10,7 +10,6 @@
 #include <linux/mman.h>
 #include <linux/smp_lock.h>
 #include <linux/notifier.h>
-#include <linux/kmod.h>
 #include <linux/reboot.h>
 #include <linux/prctl.h>
 #include <linux/highuid.h>
 #include <linux/tty.h>
 #include <linux/signal.h>
 #include <linux/cn_proc.h>
-#include <linux/vs_cvirt.h>
+#include <linux/getcpu.h>
 
 #include <linux/compat.h>
 #include <linux/syscalls.h>
 #include <linux/kprobes.h>
+#include <linux/vs_pid.h>
 
 #include <asm/uaccess.h>
 #include <asm/io.h>
@@ -93,7 +93,8 @@ EXPORT_SYMBOL(fs_overflowgid);
  */
 
 int C_A_D = 1;
-int cad_pid = 1;
+struct pid *cad_pid;
+EXPORT_SYMBOL(cad_pid);
 
 /*
  *     Notifier list for kernel code which wants to be called
@@ -153,7 +154,7 @@ static int __kprobes notifier_call_chain(struct notifier_block **nl,
 
 /*
  *     Atomic notifier chain routines.  Registration and unregistration
- *     use a mutex, and call_chain is synchronized by RCU (no locks).
+ *     use a spinlock, and call_chain is synchronized by RCU (no locks).
  */
 
 /**
@@ -222,7 +223,7 @@ EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
  *     of the last notifier function called.
  */
  
-int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
+int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
                unsigned long val, void *v)
 {
        int ret;
@@ -323,11 +324,18 @@ EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
                unsigned long val, void *v)
 {
-       int ret;
+       int ret = NOTIFY_DONE;
 
-       down_read(&nh->rwsem);
-       ret = notifier_call_chain(&nh->head, val, v);
-       up_read(&nh->rwsem);
+       /*
+        * We check the head outside the lock, but if this access is
+        * racy then it does not matter what the result of the test
+        * is, we re-check the list after having taken the lock anyway:
+        */
+       if (rcu_dereference(nh->head)) {
+               down_read(&nh->rwsem);
+               ret = notifier_call_chain(&nh->head, val, v);
+               up_read(&nh->rwsem);
+       }
        return ret;
 }
 
@@ -401,6 +409,129 @@ int raw_notifier_call_chain(struct raw_notifier_head *nh,
 
 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
 
+/*
+ *     SRCU notifier chain routines.    Registration and unregistration
+ *     use a mutex, and call_chain is synchronized by SRCU (no locks).
+ */
+
+/**
+ *     srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
+ *     @nh: Pointer to head of the SRCU notifier chain
+ *     @n: New entry in notifier chain
+ *
+ *     Adds a notifier to an SRCU notifier chain.
+ *     Must be called in process context.
+ *
+ *     Currently always returns zero.
+ */
+
+int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
+               struct notifier_block *n)
+{
+       int ret;
+
+       /*
+        * This code gets used during boot-up, when task switching is
+        * not yet working and interrupts must remain disabled.  At
+        * such times we must not call mutex_lock().
+        */
+       if (unlikely(system_state == SYSTEM_BOOTING))
+               return notifier_chain_register(&nh->head, n);
+
+       mutex_lock(&nh->mutex);
+       ret = notifier_chain_register(&nh->head, n);
+       mutex_unlock(&nh->mutex);
+       return ret;
+}
+
+EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
+
+/**
+ *     srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
+ *     @nh: Pointer to head of the SRCU notifier chain
+ *     @n: Entry to remove from notifier chain
+ *
+ *     Removes a notifier from an SRCU notifier chain.
+ *     Must be called from process context.
+ *
+ *     Returns zero on success or %-ENOENT on failure.
+ */
+int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
+               struct notifier_block *n)
+{
+       int ret;
+
+       /*
+        * This code gets used during boot-up, when task switching is
+        * not yet working and interrupts must remain disabled.  At
+        * such times we must not call mutex_lock().
+        */
+       if (unlikely(system_state == SYSTEM_BOOTING))
+               return notifier_chain_unregister(&nh->head, n);
+
+       mutex_lock(&nh->mutex);
+       ret = notifier_chain_unregister(&nh->head, n);
+       mutex_unlock(&nh->mutex);
+       synchronize_srcu(&nh->srcu);
+       return ret;
+}
+
+EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
+
+/**
+ *     srcu_notifier_call_chain - Call functions in an SRCU notifier chain
+ *     @nh: Pointer to head of the SRCU notifier chain
+ *     @val: Value passed unmodified to notifier function
+ *     @v: Pointer passed unmodified to notifier function
+ *
+ *     Calls each function in a notifier chain in turn.  The functions
+ *     run in a process context, so they are allowed to block.
+ *
+ *     If the return value of the notifier can be and'ed
+ *     with %NOTIFY_STOP_MASK then srcu_notifier_call_chain
+ *     will return immediately, with the return value of
+ *     the notifier function which halted execution.
+ *     Otherwise the return value is the return value
+ *     of the last notifier function called.
+ */
+
+int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
+               unsigned long val, void *v)
+{
+       int ret;
+       int idx;
+
+       idx = srcu_read_lock(&nh->srcu);
+       ret = notifier_call_chain(&nh->head, val, v);
+       srcu_read_unlock(&nh->srcu, idx);
+       return ret;
+}
+
+EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
+
+/**
+ *     srcu_init_notifier_head - Initialize an SRCU notifier head
+ *     @nh: Pointer to head of the srcu notifier chain
+ *
+ *     Unlike other sorts of notifier heads, SRCU notifier heads require
+ *     dynamic initialization.  Be sure to call this routine before
+ *     calling any of the other SRCU notifier routines for this head.
+ *
+ *     If an SRCU notifier head is deallocated, it must first be cleaned
+ *     up by calling srcu_cleanup_notifier_head().  Otherwise the head's
+ *     per-cpu data (used by the SRCU mechanism) will leak.
+ */
+
+void srcu_init_notifier_head(struct srcu_notifier_head *nh)
+{
+       mutex_init(&nh->mutex);
+       if (init_srcu_struct(&nh->srcu) < 0)
+               BUG();
+       nh->head = NULL;
+}
+
+EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
+
 /**
  *     register_reboot_notifier - Register function to be called at reboot time
  *     @nb: Info about notifier function to be called
@@ -493,6 +624,8 @@ asmlinkage long sys_setpriority(int which, int who, int niceval)
                        if (!who)
                                who = process_group(current);
                        do_each_task_pid(who, PIDTYPE_PGID, p) {
+                               if (!vx_check(p->xid, VS_ADMIN_P | VS_IDENT))
+                                       continue;
                                error = set_one_prio(p, niceval, error);
                        } while_each_task_pid(who, PIDTYPE_PGID, p);
                        break;
@@ -550,6 +683,8 @@ asmlinkage long sys_getpriority(int which, int who)
                        if (!who)
                                who = process_group(current);
                        do_each_task_pid(who, PIDTYPE_PGID, p) {
+                               if (!vx_check(p->xid, VS_ADMIN_P | VS_IDENT))
+                                       continue;
                                niceval = 20 - task_nice(p);
                                if (niceval > retval)
                                        retval = niceval;
@@ -613,12 +748,10 @@ static void kernel_restart_prepare(char *cmd)
 void kernel_restart(char *cmd)
 {
        kernel_restart_prepare(cmd);
-       if (!cmd) {
+       if (!cmd)
                printk(KERN_EMERG "Restarting system.\n");
-       } else {
+       else
                printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
-       }
-       printk(".\n");
        machine_restart(cmd);
 }
 EXPORT_SYMBOL_GPL(kernel_restart);
@@ -634,9 +767,8 @@ static void kernel_kexec(void)
 #ifdef CONFIG_KEXEC
        struct kimage *image;
        image = xchg(&kexec_image, NULL);
-       if (!image) {
+       if (!image)
                return;
-       }
        kernel_restart_prepare(NULL);
        printk(KERN_EMERG "Starting new kernel\n");
        machine_shutdown();
@@ -710,7 +842,7 @@ asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user
        if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
                cmd = LINUX_REBOOT_CMD_HALT;
 
-       if (!vx_check(0, VX_ADMIN|VX_WATCH))
+       if (!vx_check(0, VS_ADMIN|VS_WATCH))
                return vs_reboot(cmd, arg);
 
        lock_kernel();
@@ -771,7 +903,7 @@ asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user
        return 0;
 }
 
-static void deferred_cad(void *dummy)
+static void deferred_cad(struct work_struct *dummy)
 {
        kernel_restart(NULL);
 }
@@ -783,15 +915,14 @@ static void deferred_cad(void *dummy)
  */
 void ctrl_alt_del(void)
 {
-       static DECLARE_WORK(cad_work, deferred_cad, NULL);
+       static DECLARE_WORK(cad_work, deferred_cad);
 
        if (C_A_D)
                schedule_work(&cad_work);
        else
-               kill_proc(cad_pid, SIGINT, 1);
+               kill_cad_pid(SIGINT, 1);
 }
        
-
 /*
  * Unprivileged users may change the real gid to the effective gid
  * or vice versa.  (BSD-style)
@@ -836,12 +967,10 @@ asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
                    (current->sgid == egid) ||
                    capable(CAP_SETGID))
                        new_egid = egid;
-               else {
+               else
                        return -EPERM;
-               }
        }
-       if (new_egid != old_egid)
-       {
+       if (new_egid != old_egid) {
                current->mm->dumpable = suid_dumpable;
                smp_wmb();
        }
@@ -870,19 +999,14 @@ asmlinkage long sys_setgid(gid_t gid)
        if (retval)
                return retval;
 
-       if (capable(CAP_SETGID))
-       {
-               if(old_egid != gid)
-               {
+       if (capable(CAP_SETGID)) {
+               if (old_egid != gid) {
                        current->mm->dumpable = suid_dumpable;
                        smp_wmb();
                }
                current->gid = current->egid = current->sgid = current->fsgid = gid;
-       }
-       else if ((gid == current->gid) || (gid == current->sgid))
-       {
-               if(old_egid != gid)
-               {
+       } else if ((gid == current->gid) || (gid == current->sgid)) {
+               if (old_egid != gid) {
                        current->mm->dumpable = suid_dumpable;
                        smp_wmb();
                }
@@ -913,8 +1037,7 @@ static int set_user(uid_t new_ruid, int dumpclear)
 
        switch_uid(new_user);
 
-       if(dumpclear)
-       {
+       if (dumpclear) {
                current->mm->dumpable = suid_dumpable;
                smp_wmb();
        }
@@ -970,8 +1093,7 @@ asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
        if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
                return -EAGAIN;
 
-       if (new_euid != old_euid)
-       {
+       if (new_euid != old_euid) {
                current->mm->dumpable = suid_dumpable;
                smp_wmb();
        }
@@ -1003,14 +1125,14 @@ asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
 asmlinkage long sys_setuid(uid_t uid)
 {
        int old_euid = current->euid;
-       int old_ruid, old_suid, new_ruid, new_suid;
+       int old_ruid, old_suid, new_suid;
        int retval;
 
        retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
        if (retval)
                return retval;
 
-       old_ruid = new_ruid = current->uid;
+       old_ruid = current->uid;
        old_suid = current->suid;
        new_suid = old_suid;
        
@@ -1021,8 +1143,7 @@ asmlinkage long sys_setuid(uid_t uid)
        } else if ((uid != current->uid) && (uid != new_suid))
                return -EPERM;
 
-       if (old_euid != uid)
-       {
+       if (old_euid != uid) {
                current->mm->dumpable = suid_dumpable;
                smp_wmb();
        }
@@ -1067,8 +1188,7 @@ asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
                        return -EAGAIN;
        }
        if (euid != (uid_t) -1) {
-               if (euid != current->euid)
-               {
+               if (euid != current->euid) {
                        current->mm->dumpable = suid_dumpable;
                        smp_wmb();
                }
@@ -1118,8 +1238,7 @@ asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
                        return -EPERM;
        }
        if (egid != (gid_t) -1) {
-               if (egid != current->egid)
-               {
+               if (egid != current->egid) {
                        current->mm->dumpable = suid_dumpable;
                        smp_wmb();
                }
@@ -1164,10 +1283,8 @@ asmlinkage long sys_setfsuid(uid_t uid)
 
        if (uid == current->uid || uid == current->euid ||
            uid == current->suid || uid == current->fsuid || 
-           capable(CAP_SETUID))
-       {
-               if (uid != old_fsuid)
-               {
+           capable(CAP_SETUID)) {
+               if (uid != old_fsuid) {
                        current->mm->dumpable = suid_dumpable;
                        smp_wmb();
                }
@@ -1195,10 +1312,8 @@ asmlinkage long sys_setfsgid(gid_t gid)
 
        if (gid == current->gid || gid == current->egid ||
            gid == current->sgid || gid == current->fsgid || 
-           capable(CAP_SETGID))
-       {
-               if (gid != old_fsgid)
-               {
+           capable(CAP_SETGID)) {
+               if (gid != old_fsgid) {
                        current->mm->dumpable = suid_dumpable;
                        smp_wmb();
                }
@@ -1292,7 +1407,7 @@ asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
 
        if (p->parent == group_leader) {
                err = -EPERM;
-               if (p->signal->session != group_leader->signal->session)
+               if (process_session(p) != process_session(group_leader))
                        goto out;
                err = -EACCES;
                if (p->did_exec)
@@ -1308,16 +1423,13 @@ asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
                goto out;
 
        if (pgid != pid) {
-               struct task_struct *p;
+               struct task_struct *g =
+                       find_task_by_pid_type(PIDTYPE_PGID, rpgid);
 
-               do_each_task_pid(rpgid, PIDTYPE_PGID, p) {
-                       if (p->signal->session == group_leader->signal->session)
-                               goto ok_pgid;
-               } while_each_task_pid(rpgid, PIDTYPE_PGID, p);
-               goto out;
+               if (!g || process_session(g) != process_session(group_leader))
+                       goto out;
        }
 
-ok_pgid:
        err = security_task_setpgid(p, rpgid);
        if (err)
                goto out;
@@ -1337,9 +1449,9 @@ out:
 
 asmlinkage long sys_getpgid(pid_t pid)
 {
-       if (!pid) {
+       if (!pid)
                return vx_rmap_pid(process_group(current));
-       else {
+       else {
                int retval;
                struct task_struct *p;
 
@@ -1369,9 +1481,9 @@ asmlinkage long sys_getpgrp(void)
 
 asmlinkage long sys_getsid(pid_t pid)
 {
-       if (!pid) {
-               return current->signal->session;
-       else {
+       if (!pid)
+               return process_session(current);
+       else {
                int retval;
                struct task_struct *p;
 
@@ -1379,10 +1491,10 @@ asmlinkage long sys_getsid(pid_t pid)
                p = find_task_by_pid(pid);
 
                retval = -ESRCH;
-               if(p) {
+               if (p) {
                        retval = security_task_getsid(p);
                        if (!retval)
-                               retval = p->signal->session;
+                               retval = process_session(p);
                }
                read_unlock(&tasklist_lock);
                return retval;
@@ -1449,9 +1561,9 @@ struct group_info *groups_alloc(int gidsetsize)
        group_info->nblocks = nblocks;
        atomic_set(&group_info->usage, 1);
 
-       if (gidsetsize <= NGROUPS_SMALL) {
+       if (gidsetsize <= NGROUPS_SMALL)
                group_info->blocks[0] = group_info->small_block;
-       else {
+       else {
                for (i = 0; i < nblocks; i++) {
                        gid_t *b;
                        b = (void *)__get_free_page(GFP_USER);
@@ -1507,7 +1619,7 @@ static int groups_to_user(gid_t __user *grouplist,
 /* fill a group_info from a user-space array - it must be allocated already */
 static int groups_from_user(struct group_info *group_info,
     gid_t __user *grouplist)
- {
+{
        int i;
        int count = group_info->ngroups;
 
@@ -1665,9 +1777,8 @@ asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
 int in_group_p(gid_t grp)
 {
        int retval = 1;
-       if (grp != current->fsgid) {
+       if (grp != current->fsgid)
                retval = groups_search(current->group_info, grp);
-       }
        return retval;
 }
 
@@ -1676,9 +1787,8 @@ EXPORT_SYMBOL(in_group_p);
 int in_egroup_p(gid_t grp)
 {
        int retval = 1;
-       if (grp != current->egid) {
+       if (grp != current->egid)
                retval = groups_search(current->group_info, grp);
-       }
        return retval;
 }
 
@@ -1693,7 +1803,7 @@ asmlinkage long sys_newuname(struct new_utsname __user * name)
        int errno = 0;
 
        down_read(&uts_sem);
-       if (copy_to_user(name, vx_new_utsname(), sizeof *name))
+       if (copy_to_user(name, utsname(), sizeof *name))
                errno = -EFAULT;
        up_read(&uts_sem);
        return errno;
@@ -1711,10 +1821,8 @@ asmlinkage long sys_sethostname(char __user *name, int len)
        down_write(&uts_sem);
        errno = -EFAULT;
        if (!copy_from_user(tmp, name, len)) {
-               char *ptr = vx_new_uts(nodename);
-
-               memcpy(ptr, tmp, len);
-               ptr[len] = 0;
+               memcpy(utsname()->nodename, tmp, len);
+               utsname()->nodename[len] = 0;
                errno = 0;
        }
        up_write(&uts_sem);
@@ -1726,17 +1834,15 @@ asmlinkage long sys_sethostname(char __user *name, int len)
 asmlinkage long sys_gethostname(char __user *name, int len)
 {
        int i, errno;
-       char *ptr;
 
        if (len < 0)
                return -EINVAL;
        down_read(&uts_sem);
-       ptr = vx_new_uts(nodename);
-       i = 1 + strlen(ptr);
+       i = 1 + strlen(utsname()->nodename);
        if (i > len)
                i = len;
        errno = 0;
-       if (copy_to_user(name, ptr, i))
+       if (copy_to_user(name, utsname()->nodename, i))
                errno = -EFAULT;
        up_read(&uts_sem);
        return errno;
@@ -1761,10 +1867,8 @@ asmlinkage long sys_setdomainname(char __user *name, int len)
        down_write(&uts_sem);
        errno = -EFAULT;
        if (!copy_from_user(tmp, name, len)) {
-               char *ptr = vx_new_uts(domainname);
-
-               memcpy(ptr, tmp, len);
-               ptr[len] = 0;
+               memcpy(utsname()->domainname, tmp, len);
+               utsname()->domainname[len] = 0;
                errno = 0;
        }
        up_write(&uts_sem);
@@ -1799,9 +1903,9 @@ asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *r
        task_lock(current->group_leader);
        x = current->signal->rlim[resource];
        task_unlock(current->group_leader);
-       if(x.rlim_cur > 0x7FFFFFFF)
+       if (x.rlim_cur > 0x7FFFFFFF)
                x.rlim_cur = 0x7FFFFFFF;
-       if(x.rlim_max > 0x7FFFFFFF)
+       if (x.rlim_max > 0x7FFFFFFF)
                x.rlim_max = 0x7FFFFFFF;
        return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
 }
@@ -2086,3 +2190,33 @@ asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
        }
        return error;
 }
+
+asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
+                          struct getcpu_cache __user *cache)
+{
+       int err = 0;
+       int cpu = raw_smp_processor_id();
+       if (cpup)
+               err |= put_user(cpu, cpup);
+       if (nodep)
+               err |= put_user(cpu_to_node(cpu), nodep);
+       if (cache) {
+               /*
+                * The cache is not needed for this implementation,
+                * but make sure user programs pass something
+                * valid. vsyscall implementations can instead make
+                * good use of the cache. Only use t0 and t1 because
+                * these are available in both 32bit and 64bit ABI (no
+                * need for a compat_getcpu). 32bit has enough
+                * padding
+                */
+               unsigned long t0, t1;
+               get_user(t0, &cache->blob[0]);
+               get_user(t1, &cache->blob[1]);
+               t0++;
+               t1++;
+               put_user(t0, &cache->blob[0]);
+               put_user(t1, &cache->blob[1]);
+       }
+       return err ? -EFAULT : 0;
+}