IPC/semaphores: consolidate SEM_STAT and IPC_STAT commands
[sfrench/cifs-2.6.git] / ipc / sem.c
index 35952c0bae4629cac017c4c62623c91a022ebee5..7836edb0cf967f645470dfdac3239477f03bcb57 100644 (file)
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -82,6 +82,7 @@
 #include <linux/seq_file.h>
 #include <linux/rwsem.h>
 #include <linux/nsproxy.h>
+#include <linux/ipc_namespace.h>
 
 #include <asm/uaccess.h>
 #include "util.h"
@@ -128,6 +129,7 @@ static void __sem_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
        ipc_init_ids(ids);
 }
 
+#ifdef CONFIG_IPC_NS
 int sem_init_ns(struct ipc_namespace *ns)
 {
        struct ipc_ids *ids;
@@ -143,6 +145,7 @@ int sem_init_ns(struct ipc_namespace *ns)
 void sem_exit_ns(struct ipc_namespace *ns)
 {
        struct sem_array *sma;
+       struct kern_ipc_perm *perm;
        int next_id;
        int total, in_use;
 
@@ -151,10 +154,11 @@ void sem_exit_ns(struct ipc_namespace *ns)
        in_use = sem_ids(ns).in_use;
 
        for (total = 0, next_id = 0; total < in_use; next_id++) {
-               sma = idr_find(&sem_ids(ns).ipcs_idr, next_id);
-               if (sma == NULL)
+               perm = idr_find(&sem_ids(ns).ipcs_idr, next_id);
+               if (perm == NULL)
                        continue;
-               ipc_lock_by_ptr(&sma->sem_perm);
+               ipc_lock_by_ptr(perm);
+               sma = container_of(perm, struct sem_array, sem_perm);
                freeary(ns, sma);
                total++;
        }
@@ -163,6 +167,7 @@ void sem_exit_ns(struct ipc_namespace *ns)
        kfree(ns->ids[IPC_SEM_IDS]);
        ns->ids[IPC_SEM_IDS] = NULL;
 }
+#endif
 
 void __init sem_init (void)
 {
@@ -181,6 +186,9 @@ static inline struct sem_array *sem_lock_check_down(struct ipc_namespace *ns,
 {
        struct kern_ipc_perm *ipcp = ipc_lock_check_down(&sem_ids(ns), id);
 
+       if (IS_ERR(ipcp))
+               return (struct sem_array *)ipcp;
+
        return container_of(ipcp, struct sem_array, sem_perm);
 }
 
@@ -192,6 +200,9 @@ static inline struct sem_array *sem_lock(struct ipc_namespace *ns, int id)
 {
        struct kern_ipc_perm *ipcp = ipc_lock(&sem_ids(ns), id);
 
+       if (IS_ERR(ipcp))
+               return (struct sem_array *)ipcp;
+
        return container_of(ipcp, struct sem_array, sem_perm);
 }
 
@@ -200,6 +211,9 @@ static inline struct sem_array *sem_lock_check(struct ipc_namespace *ns,
 {
        struct kern_ipc_perm *ipcp = ipc_lock_check(&sem_ids(ns), id);
 
+       if (IS_ERR(ipcp))
+               return (struct sem_array *)ipcp;
+
        return container_of(ipcp, struct sem_array, sem_perm);
 }
 
@@ -603,8 +617,8 @@ static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in,
        }
 }
 
-static int semctl_nolock(struct ipc_namespace *ns, int semid, int semnum,
-               int cmd, int version, union semun arg)
+static int semctl_nolock(struct ipc_namespace *ns, int semid,
+                        int cmd, int version, union semun arg)
 {
        int err = -EINVAL;
        struct sem_array *sma;
@@ -643,14 +657,23 @@ static int semctl_nolock(struct ipc_namespace *ns, int semid, int semnum,
                        return -EFAULT;
                return (max_id < 0) ? 0: max_id;
        }
+       case IPC_STAT:
        case SEM_STAT:
        {
                struct semid64_ds tbuf;
                int id;
 
-               sma = sem_lock(ns, semid);
-               if (IS_ERR(sma))
-                       return PTR_ERR(sma);
+               if (cmd == SEM_STAT) {
+                       sma = sem_lock(ns, semid);
+                       if (IS_ERR(sma))
+                               return PTR_ERR(sma);
+                       id = sma->sem_perm.id;
+               } else {
+                       sma = sem_lock_check(ns, semid);
+                       if (IS_ERR(sma))
+                               return PTR_ERR(sma);
+                       id = 0;
+               }
 
                err = -EACCES;
                if (ipcperms (&sma->sem_perm, S_IRUGO))
@@ -660,8 +683,6 @@ static int semctl_nolock(struct ipc_namespace *ns, int semid, int semnum,
                if (err)
                        goto out_unlock;
 
-               id = sma->sem_perm.id;
-
                memset(&tbuf, 0, sizeof(tbuf));
 
                kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
@@ -796,19 +817,6 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
                err = 0;
                goto out_unlock;
        }
-       case IPC_STAT:
-       {
-               struct semid64_ds tbuf;
-               memset(&tbuf,0,sizeof(tbuf));
-               kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
-               tbuf.sem_otime  = sma->sem_otime;
-               tbuf.sem_ctime  = sma->sem_ctime;
-               tbuf.sem_nsems  = sma->sem_nsems;
-               sem_unlock(sma);
-               if (copy_semid_to_user (arg.buf, &tbuf, version))
-                       return -EFAULT;
-               return 0;
-       }
        /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
        }
        err = -EINVAL;
@@ -975,15 +983,15 @@ asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
        switch(cmd) {
        case IPC_INFO:
        case SEM_INFO:
+       case IPC_STAT:
        case SEM_STAT:
-               err = semctl_nolock(ns,semid,semnum,cmd,version,arg);
+               err = semctl_nolock(ns, semid, cmd, version, arg);
                return err;
        case GETALL:
        case GETVAL:
        case GETPID:
        case GETNCNT:
        case GETZCNT:
-       case IPC_STAT:
        case SETVAL:
        case SETALL:
                err = semctl_main(ns,semid,semnum,cmd,version,arg);