linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / ipc / sem.c
index b3cbd4c..1a68338 100644 (file)
--- a/ipc/sem.c
+++ b/ipc/sem.c
  * (c) 2001 Red Hat Inc <alan@redhat.com>
  * Lockless wakeup
  * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
- *
- * support for audit of ipc object properties and permission changes
- * Dustin Kirkland <dustin.kirkland@us.ibm.com>
  */
 
+#include <linux/config.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/init.h>
@@ -77,8 +75,6 @@
 #include <linux/audit.h>
 #include <linux/capability.h>
 #include <linux/seq_file.h>
-#include <linux/mutex.h>
-
 #include <asm/uaccess.h>
 #include "util.h"
 
@@ -143,7 +139,7 @@ void __init sem_init (void)
  *     * if it's IN_WAKEUP, then it must wait until the value changes
  *     * if it's not -EINTR, then the operation was completed by
  *       update_queue. semtimedop can return queue.status without
- *       performing any operation on the sem array.
+ *       performing any operation on the semaphore array.
  *     * otherwise it must acquire the spinlock and check what's up.
  *
  * The two-stage algorithm is necessary to protect against the following
@@ -219,7 +215,7 @@ asmlinkage long sys_semget (key_t key, int nsems, int semflg)
 
        if (nsems < 0 || nsems > sc_semmsl)
                return -EINVAL;
-       mutex_lock(&sem_ids.mutex);
+       down(&sem_ids.sem);
        
        if (key == IPC_PRIVATE) {
                err = newary(key, nsems, semflg);
@@ -232,7 +228,8 @@ asmlinkage long sys_semget (key_t key, int nsems, int semflg)
                err = -EEXIST;
        } else {
                sma = sem_lock(id);
-               BUG_ON(sma==NULL);
+               if(sma==NULL)
+                       BUG();
                if (nsems > sma->sem_nsems)
                        err = -EINVAL;
                else if (ipcperms(&sma->sem_perm, semflg))
@@ -246,7 +243,7 @@ asmlinkage long sys_semget (key_t key, int nsems, int semflg)
                sem_unlock(sma);
        }
 
-       mutex_unlock(&sem_ids.mutex);
+       up(&sem_ids.sem);
        return err;
 }
 
@@ -441,8 +438,8 @@ static int count_semzcnt (struct sem_array * sma, ushort semnum)
        return semzcnt;
 }
 
-/* Free a semaphore set. freeary() is called with sem_ids.mutex locked and
- * the spinlock for this semaphore set hold. sem_ids.mutex remains locked
+/* Free a semaphore set. freeary() is called with sem_ids.sem down and
+ * the spinlock for this semaphore set hold. sem_ids.sem remains locked
  * on exit.
  */
 static void freeary (struct sem_array *sma, int id)
@@ -529,7 +526,7 @@ static int semctl_nolock(int semid, int semnum, int cmd, int version, union semu
                seminfo.semmnu = SEMMNU;
                seminfo.semmap = SEMMAP;
                seminfo.semume = SEMUME;
-               mutex_lock(&sem_ids.mutex);
+               down(&sem_ids.sem);
                if (cmd == SEM_INFO) {
                        seminfo.semusz = sem_ids.in_use;
                        seminfo.semaem = used_sems;
@@ -538,7 +535,7 @@ static int semctl_nolock(int semid, int semnum, int cmd, int version, union semu
                        seminfo.semaem = SEMAEM;
                }
                max_id = sem_ids.max_id;
-               mutex_unlock(&sem_ids.mutex);
+               up(&sem_ids.sem);
                if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo))) 
                        return -EFAULT;
                return (max_id < 0) ? 0: max_id;
@@ -813,6 +810,8 @@ static int semctl_down(int semid, int semnum, int cmd, int version, union semun
        if(cmd == IPC_SET) {
                if(copy_semid_from_user (&setbuf, arg.buf, version))
                        return -EFAULT;
+               if ((err = audit_ipc_perms(0, setbuf.uid, setbuf.gid, setbuf.mode)))
+                       return err;
        }
        sma = sem_lock(semid);
        if(sma==NULL)
@@ -823,16 +822,7 @@ static int semctl_down(int semid, int semnum, int cmd, int version, union semun
                goto out_unlock;
        }       
        ipcp = &sma->sem_perm;
-
-       err = audit_ipc_obj(ipcp);
-       if (err)
-               goto out_unlock;
-
-       if (cmd == IPC_SET) {
-               err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
-               if (err)
-                       goto out_unlock;
-       }
+       
        if (current->euid != ipcp->cuid && 
            current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
                err=-EPERM;
@@ -897,9 +887,9 @@ asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
                return err;
        case IPC_RMID:
        case IPC_SET:
-               mutex_lock(&sem_ids.mutex);
+               down(&sem_ids.sem);
                err = semctl_down(semid,semnum,cmd,version,arg);
-               mutex_unlock(&sem_ids.mutex);
+               up(&sem_ids.sem);
                return err;
        default:
                return -EINVAL;
@@ -1193,7 +1183,8 @@ retry_undos:
 
        sma = sem_lock(semid);
        if(sma==NULL) {
-               BUG_ON(queue.prev != NULL);
+               if(queue.prev != NULL)
+                       BUG();
                error = -EIDRM;
                goto out_free;
        }
@@ -1310,9 +1301,9 @@ found:
                /* perform adjustments registered in u */
                nsems = sma->sem_nsems;
                for (i = 0; i < nsems; i++) {
-                       struct sem * semaphore = &sma->sem_base[i];
+                       struct sem * sem = &sma->sem_base[i];
                        if (u->semadj[i]) {
-                               semaphore->semval += u->semadj[i];
+                               sem->semval += u->semadj[i];
                                /*
                                 * Range checks of the new semaphore value,
                                 * not defined by sus:
@@ -1326,11 +1317,11 @@ found:
                                 *
                                 *      Manfred <manfred@colorfullife.com>
                                 */
-                               if (semaphore->semval < 0)
-                                       semaphore->semval = 0;
-                               if (semaphore->semval > SEMVMX)
-                                       semaphore->semval = SEMVMX;
-                               semaphore->sempid = current->tgid;
+                               if (sem->semval < 0)
+                                       sem->semval = 0;
+                               if (sem->semval > SEMVMX)
+                                       sem->semval = SEMVMX;
+                               sem->sempid = current->tgid;
                        }
                }
                sma->sem_otime = get_seconds();