aboutsummaryrefslogtreecommitdiffstats
path: root/ipc
diff options
context:
space:
mode:
Diffstat (limited to 'ipc')
-rw-r--r--ipc/Makefile9
-rw-r--r--ipc/compat.c687
-rw-r--r--ipc/compat_mq.c146
-rw-r--r--ipc/mqueue.c1252
-rw-r--r--ipc/msg.c862
-rw-r--r--ipc/msgutil.c127
-rw-r--r--ipc/sem.c1384
-rw-r--r--ipc/shm.c917
-rw-r--r--ipc/util.c580
-rw-r--r--ipc/util.h81
10 files changed, 6045 insertions, 0 deletions
diff --git a/ipc/Makefile b/ipc/Makefile
new file mode 100644
index 00000000000..0a6d626cd79
--- /dev/null
+++ b/ipc/Makefile
@@ -0,0 +1,9 @@
+#
+# Makefile for the linux ipc.
+#
+
+obj-$(CONFIG_SYSVIPC_COMPAT) += compat.o
+obj-$(CONFIG_SYSVIPC) += util.o msgutil.o msg.o sem.o shm.o
+obj_mq-$(CONFIG_COMPAT) += compat_mq.o
+obj-$(CONFIG_POSIX_MQUEUE) += mqueue.o msgutil.o $(obj_mq-y)
+
diff --git a/ipc/compat.c b/ipc/compat.c
new file mode 100644
index 00000000000..70e4e4e10fd
--- /dev/null
+++ b/ipc/compat.c
@@ -0,0 +1,687 @@
+/*
+ * 32 bit compatibility code for System V IPC
+ *
+ * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
+ * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
+ * Copyright (C) 1999 Arun Sharma <arun.sharma@intel.com>
+ * Copyright (C) 2000 VA Linux Co
+ * Copyright (C) 2000 Don Dugger <n0ano@valinux.com>
+ * Copyright (C) 2000 Hewlett-Packard Co.
+ * Copyright (C) 2000 David Mosberger-Tang <davidm@hpl.hp.com>
+ * Copyright (C) 2000 Gerhard Tonn (ton@de.ibm.com)
+ * Copyright (C) 2000-2002 Andi Kleen, SuSE Labs (x86-64 port)
+ * Copyright (C) 2000 Silicon Graphics, Inc.
+ * Copyright (C) 2001 IBM
+ * Copyright (C) 2004 IBM Deutschland Entwicklung GmbH, IBM Corporation
+ * Copyright (C) 2004 Arnd Bergmann (arnd@arndb.de)
+ *
+ * This code is collected from the versions for sparc64, mips64, s390x, ia64,
+ * ppc64 and x86_64, all of which are based on the original sparc64 version
+ * by Jakub Jelinek.
+ *
+ */
+#include <linux/compat.h>
+#include <linux/config.h>
+#include <linux/errno.h>
+#include <linux/highuid.h>
+#include <linux/init.h>
+#include <linux/msg.h>
+#include <linux/shm.h>
+#include <linux/slab.h>
+#include <linux/syscalls.h>
+
+#include <asm/semaphore.h>
+#include <asm/uaccess.h>
+
+#include "util.h"
+
+struct compat_msgbuf {
+ compat_long_t mtype;
+ char mtext[1];
+};
+
+struct compat_ipc_perm {
+ key_t key;
+ compat_uid_t uid;
+ compat_gid_t gid;
+ compat_uid_t cuid;
+ compat_gid_t cgid;
+ compat_mode_t mode;
+ unsigned short seq;
+};
+
+struct compat_semid_ds {
+ struct compat_ipc_perm sem_perm;
+ compat_time_t sem_otime;
+ compat_time_t sem_ctime;
+ compat_uptr_t sem_base;
+ compat_uptr_t sem_pending;
+ compat_uptr_t sem_pending_last;
+ compat_uptr_t undo;
+ unsigned short sem_nsems;
+};
+
+struct compat_msqid_ds {
+ struct compat_ipc_perm msg_perm;
+ compat_uptr_t msg_first;
+ compat_uptr_t msg_last;
+ compat_time_t msg_stime;
+ compat_time_t msg_rtime;
+ compat_time_t msg_ctime;
+ compat_ulong_t msg_lcbytes;
+ compat_ulong_t msg_lqbytes;
+ unsigned short msg_cbytes;
+ unsigned short msg_qnum;
+ unsigned short msg_qbytes;
+ compat_ipc_pid_t msg_lspid;
+ compat_ipc_pid_t msg_lrpid;
+};
+
+struct compat_shmid_ds {
+ struct compat_ipc_perm shm_perm;
+ int shm_segsz;
+ compat_time_t shm_atime;
+ compat_time_t shm_dtime;
+ compat_time_t shm_ctime;
+ compat_ipc_pid_t shm_cpid;
+ compat_ipc_pid_t shm_lpid;
+ unsigned short shm_nattch;
+ unsigned short shm_unused;
+ compat_uptr_t shm_unused2;
+ compat_uptr_t shm_unused3;
+};
+
+struct compat_ipc_kludge {
+ compat_uptr_t msgp;
+ compat_long_t msgtyp;
+};
+
+struct compat_shminfo64 {
+ compat_ulong_t shmmax;
+ compat_ulong_t shmmin;
+ compat_ulong_t shmmni;
+ compat_ulong_t shmseg;
+ compat_ulong_t shmall;
+ compat_ulong_t __unused1;
+ compat_ulong_t __unused2;
+ compat_ulong_t __unused3;
+ compat_ulong_t __unused4;
+};
+
+struct compat_shm_info {
+ compat_int_t used_ids;
+ compat_ulong_t shm_tot, shm_rss, shm_swp;
+ compat_ulong_t swap_attempts, swap_successes;
+};
+
+extern int sem_ctls[];
+#define sc_semopm (sem_ctls[2])
+#define MAXBUF (64*1024)
+
+static inline int compat_ipc_parse_version(int *cmd)
+{
+ int version = *cmd & IPC_64;
+
+ /* this is tricky: architectures that have support for the old
+ * ipc structures in 64 bit binaries need to have IPC_64 set
+ * in cmd, the others need to have it cleared */
+#ifndef ipc_parse_version
+ *cmd |= IPC_64;
+#else
+ *cmd &= ~IPC_64;
+#endif
+ return version;
+}
+
+static inline int __get_compat_ipc64_perm(struct ipc64_perm *p64,
+ struct compat_ipc64_perm __user *up64)
+{
+ int err;
+
+ err = __get_user(p64->uid, &up64->uid);
+ err |= __get_user(p64->gid, &up64->gid);
+ err |= __get_user(p64->mode, &up64->mode);
+ return err;
+}
+
+static inline int __get_compat_ipc_perm(struct ipc64_perm *p,
+ struct compat_ipc_perm __user *up)
+{
+ int err;
+
+ err = __get_user(p->uid, &up->uid);
+ err |= __get_user(p->gid, &up->gid);
+ err |= __get_user(p->mode, &up->mode);
+ return err;
+}
+
+static inline int __put_compat_ipc64_perm(struct ipc64_perm *p64,
+ struct compat_ipc64_perm __user *up64)
+{
+ int err;
+
+ err = __put_user(p64->key, &up64->key);
+ err |= __put_user(p64->uid, &up64->uid);
+ err |= __put_user(p64->gid, &up64->gid);
+ err |= __put_user(p64->cuid, &up64->cuid);
+ err |= __put_user(p64->cgid, &up64->cgid);
+ err |= __put_user(p64->mode, &up64->mode);
+ err |= __put_user(p64->seq, &up64->seq);
+ return err;
+}
+
+static inline int __put_compat_ipc_perm(struct ipc64_perm *p,
+ struct compat_ipc_perm __user *up)
+{
+ int err;
+ compat_uid_t u;
+ compat_gid_t g;
+
+ err = __put_user(p->key, &up->key);
+ SET_UID(u, p->uid);
+ err |= __put_user(u, &up->uid);
+ SET_GID(g, p->gid);
+ err |= __put_user(g, &up->gid);
+ SET_UID(u, p->cuid);
+ err |= __put_user(u, &up->cuid);
+ SET_GID(g, p->cgid);
+ err |= __put_user(g, &up->cgid);
+ err |= __put_user(p->mode, &up->mode);
+ err |= __put_user(p->seq, &up->seq);
+ return err;
+}
+
+static inline int get_compat_semid64_ds(struct semid64_ds *s64,
+ struct compat_semid64_ds __user *up64)
+{
+ if (!access_ok (VERIFY_READ, up64, sizeof(*up64)))
+ return -EFAULT;
+ return __get_compat_ipc64_perm(&s64->sem_perm, &up64->sem_perm);
+}
+
+static inline int get_compat_semid_ds(struct semid64_ds *s,
+ struct compat_semid_ds __user *up)
+{
+ if (!access_ok (VERIFY_READ, up, sizeof(*up)))
+ return -EFAULT;
+ return __get_compat_ipc_perm(&s->sem_perm, &up->sem_perm);
+}
+
+static inline int put_compat_semid64_ds(struct semid64_ds *s64,
+ struct compat_semid64_ds __user *up64)
+{
+ int err;
+
+ if (!access_ok (VERIFY_WRITE, up64, sizeof(*up64)))
+ return -EFAULT;
+ err = __put_compat_ipc64_perm(&s64->sem_perm, &up64->sem_perm);
+ err |= __put_user(s64->sem_otime, &up64->sem_otime);
+ err |= __put_user(s64->sem_ctime, &up64->sem_ctime);
+ err |= __put_user(s64->sem_nsems, &up64->sem_nsems);
+ return err;
+}
+
+static inline int put_compat_semid_ds(struct semid64_ds *s,
+ struct compat_semid_ds __user *up)
+{
+ int err;
+
+ if (!access_ok (VERIFY_WRITE, up, sizeof(*up)))
+ err = -EFAULT;
+ err = __put_compat_ipc_perm(&s->sem_perm, &up->sem_perm);
+ err |= __put_user(s->sem_otime, &up->sem_otime);
+ err |= __put_user(s->sem_ctime, &up->sem_ctime);
+ err |= __put_user(s->sem_nsems, &up->sem_nsems);
+ return err;
+}
+
+long compat_sys_semctl(int first, int second, int third, void __user *uptr)
+{
+ union semun fourth;
+ u32 pad;
+ int err, err2;
+ struct semid64_ds s64;
+ struct semid64_ds __user *up64;
+ int version = compat_ipc_parse_version(&third);
+
+ if (!uptr)
+ return -EINVAL;
+ if (get_user(pad, (u32 __user *) uptr))
+ return -EFAULT;
+ if ((third & (~IPC_64)) == SETVAL)
+ fourth.val = (int) pad;
+ else
+ fourth.__pad = compat_ptr(pad);
+ switch (third & (~IPC_64)) {
+ case IPC_INFO:
+ case IPC_RMID:
+ case SEM_INFO:
+ case GETVAL:
+ case GETPID:
+ case GETNCNT:
+ case GETZCNT:
+ case GETALL:
+ case SETVAL:
+ case SETALL:
+ err = sys_semctl(first, second, third, fourth);
+ break;
+
+ case IPC_STAT:
+ case SEM_STAT:
+ up64 = compat_alloc_user_space(sizeof(s64));
+ fourth.__pad = up64;
+ err = sys_semctl(first, second, third, fourth);
+ if (err < 0)
+ break;
+ if (copy_from_user(&s64, up64, sizeof(s64)))
+ err2 = -EFAULT;
+ else if (version == IPC_64)
+ err2 = put_compat_semid64_ds(&s64, compat_ptr(pad));
+ else
+ err2 = put_compat_semid_ds(&s64, compat_ptr(pad));
+ if (err2)
+ err = -EFAULT;
+ break;
+
+ case IPC_SET:
+ if (version == IPC_64) {
+ err = get_compat_semid64_ds(&s64, compat_ptr(pad));
+ } else {
+ err = get_compat_semid_ds(&s64, compat_ptr(pad));
+ }
+ up64 = compat_alloc_user_space(sizeof(s64));
+ if (copy_to_user(up64, &s64, sizeof(s64)))
+ err = -EFAULT;
+ if (err)
+ break;
+
+ fourth.__pad = up64;
+ err = sys_semctl(first, second, third, fourth);
+ break;
+
+ default:
+ err = -EINVAL;
+ break;
+ }
+ return err;
+}
+
+long compat_sys_msgsnd(int first, int second, int third, void __user *uptr)
+{
+ struct msgbuf __user *p;
+ struct compat_msgbuf __user *up = uptr;
+ long type;
+
+ if (first < 0)
+ return -EINVAL;
+ if (second < 0 || (second >= MAXBUF - sizeof(struct msgbuf)))
+ return -EINVAL;
+
+ p = compat_alloc_user_space(second + sizeof(struct msgbuf));
+ if (get_user(type, &up->mtype) ||
+ put_user(type, &p->mtype) ||
+ copy_in_user(p->mtext, up->mtext, second))
+ return -EFAULT;
+
+ return sys_msgsnd(first, p, second, third);
+}
+
+long compat_sys_msgrcv(int first, int second, int msgtyp, int third,
+ int version, void __user *uptr)
+{
+ struct msgbuf __user *p;
+ struct compat_msgbuf __user *up;
+ long type;
+ int err;
+
+ if (first < 0)
+ return -EINVAL;
+ if (second < 0 || (second >= MAXBUF - sizeof(struct msgbuf)))
+ return -EINVAL;
+
+ if (!version) {
+ struct compat_ipc_kludge ipck;
+ err = -EINVAL;
+ if (!uptr)
+ goto out;
+ err = -EFAULT;
+ if (copy_from_user (&ipck, uptr, sizeof(ipck)))
+ goto out;
+ uptr = compat_ptr(ipck.msgp);
+ msgtyp = ipck.msgtyp;
+ }
+ p = compat_alloc_user_space(second + sizeof(struct msgbuf));
+ err = sys_msgrcv(first, p, second, msgtyp, third);
+ if (err < 0)
+ goto out;
+ up = uptr;
+ if (get_user(type, &p->mtype) ||
+ put_user(type, &up->mtype) ||
+ copy_in_user(up->mtext, p->mtext, err))
+ err = -EFAULT;
+out:
+ return err;
+}
+
+static inline int get_compat_msqid64(struct msqid64_ds *m64,
+ struct compat_msqid64_ds __user *up64)
+{
+ int err;
+
+ if (!access_ok(VERIFY_READ, up64, sizeof(*up64)))
+ return -EFAULT;
+ err = __get_compat_ipc64_perm(&m64->msg_perm, &up64->msg_perm);
+ err |= __get_user(m64->msg_qbytes, &up64->msg_qbytes);
+ return err;
+}
+
+static inline int get_compat_msqid(struct msqid64_ds *m,
+ struct compat_msqid_ds __user *up)
+{
+ int err;
+
+ if (!access_ok(VERIFY_READ, up, sizeof(*up)))
+ return -EFAULT;
+ err = __get_compat_ipc_perm(&m->msg_perm, &up->msg_perm);
+ err |= __get_user(m->msg_qbytes, &up->msg_qbytes);
+ return err;
+}
+
+static inline int put_compat_msqid64_ds(struct msqid64_ds *m64,
+ struct compat_msqid64_ds __user *up64)
+{
+ int err;
+
+ if (!access_ok(VERIFY_WRITE, up64, sizeof(*up64)))
+ return -EFAULT;
+ err = __put_compat_ipc64_perm(&m64->msg_perm, &up64->msg_perm);
+ err |= __put_user(m64->msg_stime, &up64->msg_stime);
+ err |= __put_user(m64->msg_rtime, &up64->msg_rtime);
+ err |= __put_user(m64->msg_ctime, &up64->msg_ctime);
+ err |= __put_user(m64->msg_cbytes, &up64->msg_cbytes);
+ err |= __put_user(m64->msg_qnum, &up64->msg_qnum);
+ err |= __put_user(m64->msg_qbytes, &up64->msg_qbytes);
+ err |= __put_user(m64->msg_lspid, &up64->msg_lspid);
+ err |= __put_user(m64->msg_lrpid, &up64->msg_lrpid);
+ return err;
+}
+
+static inline int put_compat_msqid_ds(struct msqid64_ds *m,
+ struct compat_msqid_ds __user *up)
+{
+ int err;
+
+ if (!access_ok(VERIFY_WRITE, up, sizeof(*up)))
+ return -EFAULT;
+ err = __put_compat_ipc_perm(&m->msg_perm, &up->msg_perm);
+ err |= __put_user(m->msg_stime, &up->msg_stime);
+ err |= __put_user(m->msg_rtime, &up->msg_rtime);
+ err |= __put_user(m->msg_ctime, &up->msg_ctime);
+ err |= __put_user(m->msg_cbytes, &up->msg_cbytes);
+ err |= __put_user(m->msg_qnum, &up->msg_qnum);
+ err |= __put_user(m->msg_qbytes, &up->msg_qbytes);
+ err |= __put_user(m->msg_lspid, &up->msg_lspid);
+ err |= __put_user(m->msg_lrpid, &up->msg_lrpid);
+ return err;
+}
+
+long compat_sys_msgctl(int first, int second, void __user *uptr)
+{
+ int err, err2;
+ struct msqid64_ds m64;
+ int version = compat_ipc_parse_version(&second);
+ void __user *p;
+
+ switch (second & (~IPC_64)) {
+ case IPC_INFO:
+ case IPC_RMID:
+ case MSG_INFO:
+ err = sys_msgctl(first, second, uptr);
+ break;
+
+ case IPC_SET:
+ if (version == IPC_64) {
+ err = get_compat_msqid64(&m64, uptr);
+ } else {
+ err = get_compat_msqid(&m64, uptr);
+ }
+ if (err)
+ break;
+ p = compat_alloc_user_space(sizeof(m64));
+ if (copy_to_user(p, &m64, sizeof(m64)))
+ err = -EFAULT;
+ else
+ err = sys_msgctl(first, second, p);
+ break;
+
+ case IPC_STAT:
+ case MSG_STAT:
+ p = compat_alloc_user_space(sizeof(m64));
+ err = sys_msgctl(first, second, p);
+ if (err < 0)
+ break;
+ if (copy_from_user(&m64, p, sizeof(m64)))
+ err2 = -EFAULT;
+ else if (version == IPC_64)
+ err2 = put_compat_msqid64_ds(&m64, uptr);
+ else
+ err2 = put_compat_msqid_ds(&m64, uptr);
+ if (err2)
+ err = -EFAULT;
+ break;
+
+ default:
+ err = -EINVAL;
+ break;
+ }
+ return err;
+}
+
+long compat_sys_shmat(int first, int second, compat_uptr_t third, int version,
+ void __user *uptr)
+{
+ int err;
+ unsigned long raddr;
+ compat_ulong_t __user *uaddr;
+
+ if (version == 1)
+ return -EINVAL;
+ err = do_shmat(first, uptr, second, &raddr);
+ if (err < 0)
+ return err;
+ uaddr = compat_ptr(third);
+ return put_user(raddr, uaddr);
+}
+
+static inline int get_compat_shmid64_ds(struct shmid64_ds *s64,
+ struct compat_shmid64_ds __user *up64)
+{
+ if (!access_ok(VERIFY_READ, up64, sizeof(*up64)))
+ return -EFAULT;
+ return __get_compat_ipc64_perm(&s64->shm_perm, &up64->shm_perm);
+}
+
+static inline int get_compat_shmid_ds(struct shmid64_ds *s,
+ struct compat_shmid_ds __user *up)
+{
+ if (!access_ok(VERIFY_READ, up, sizeof(*up)))
+ return -EFAULT;
+ return __get_compat_ipc_perm(&s->shm_perm, &up->shm_perm);
+}
+
+static inline int put_compat_shmid64_ds(struct shmid64_ds *s64,
+ struct compat_shmid64_ds __user *up64)
+{
+ int err;
+
+ if (!access_ok(VERIFY_WRITE, up64, sizeof(*up64)))
+ return -EFAULT;
+ err = __put_compat_ipc64_perm(&s64->shm_perm, &up64->shm_perm);
+ err |= __put_user(s64->shm_atime, &up64->shm_atime);
+ err |= __put_user(s64->shm_dtime, &up64->shm_dtime);
+ err |= __put_user(s64->shm_ctime, &up64->shm_ctime);
+ err |= __put_user(s64->shm_segsz, &up64->shm_segsz);
+ err |= __put_user(s64->shm_nattch, &up64->shm_nattch);
+ err |= __put_user(s64->shm_cpid, &up64->shm_cpid);
+ err |= __put_user(s64->shm_lpid, &up64->shm_lpid);
+ return err;
+}
+
+static inline int put_compat_shmid_ds(struct shmid64_ds *s,
+ struct compat_shmid_ds __user *up)
+{
+ int err;
+
+ if (!access_ok(VERIFY_WRITE, up, sizeof(*up)))
+ return -EFAULT;
+ err = __put_compat_ipc_perm(&s->shm_perm, &up->shm_perm);
+ err |= __put_user(s->shm_atime, &up->shm_atime);
+ err |= __put_user(s->shm_dtime, &up->shm_dtime);
+ err |= __put_user(s->shm_ctime, &up->shm_ctime);
+ err |= __put_user(s->shm_segsz, &up->shm_segsz);
+ err |= __put_user(s->shm_nattch, &up->shm_nattch);
+ err |= __put_user(s->shm_cpid, &up->shm_cpid);
+ err |= __put_user(s->shm_lpid, &up->shm_lpid);
+ return err;
+}
+
+static inline int put_compat_shminfo64(struct shminfo64 *smi,
+ struct compat_shminfo64 __user *up64)
+{
+ int err;
+
+ if (!access_ok(VERIFY_WRITE, up64, sizeof(*up64)))
+ return -EFAULT;
+ err = __put_user(smi->shmmax, &up64->shmmax);
+ err |= __put_user(smi->shmmin, &up64->shmmin);
+ err |= __put_user(smi->shmmni, &up64->shmmni);
+ err |= __put_user(smi->shmseg, &up64->shmseg);
+ err |= __put_user(smi->shmall, &up64->shmall);
+ return err;
+}
+
+static inline int put_compat_shminfo(struct shminfo64 *smi,
+ struct shminfo __user *up)
+{
+ int err;
+
+ if (!access_ok(VERIFY_WRITE, up, sizeof(*up)))
+ return -EFAULT;
+ err = __put_user(smi->shmmax, &up->shmmax);
+ err |= __put_user(smi->shmmin, &up->shmmin);
+ err |= __put_user(smi->shmmni, &up->shmmni);
+ err |= __put_user(smi->shmseg, &up->shmseg);
+ err |= __put_user(smi->shmall, &up->shmall);
+}
+
+static inline int put_compat_shm_info(struct shm_info __user *ip,
+ struct compat_shm_info __user *uip)
+{
+ int err;
+ struct shm_info si;
+
+ if (!access_ok(VERIFY_WRITE, uip, sizeof(*uip)) ||
+ copy_from_user(&si, ip, sizeof(si)))
+ return -EFAULT;
+ err = __put_user(si.used_ids, &uip->used_ids);
+ err |= __put_user(si.shm_tot, &uip->shm_tot);
+ err |= __put_user(si.shm_rss, &uip->shm_rss);
+ err |= __put_user(si.shm_swp, &uip->shm_swp);
+ err |= __put_user(si.swap_attempts, &uip->swap_attempts);
+ err |= __put_user(si.swap_successes, &uip->swap_successes);
+ return err;
+}
+
+long compat_sys_shmctl(int first, int second, void __user *uptr)
+{
+ void __user *p;
+ struct shmid64_ds s64;
+ struct shminfo64 smi;
+ int err, err2;
+ int version = compat_ipc_parse_version(&second);
+
+ switch (second & (~IPC_64)) {
+ case IPC_RMID:
+ case SHM_LOCK:
+ case SHM_UNLOCK:
+ err = sys_shmctl(first, second, uptr);
+ break;
+
+ case IPC_INFO:
+ p = compat_alloc_user_space(sizeof(smi));
+ err = sys_shmctl(first, second, p);
+ if (err < 0)
+ break;
+ if (copy_from_user(&smi, p, sizeof(smi)))
+ err2 = -EFAULT;
+ else if (version == IPC_64)
+ err2 = put_compat_shminfo64(&smi, uptr);
+ else
+ err2 = put_compat_shminfo(&smi, uptr);
+ if (err2)
+ err = -EFAULT;
+ break;
+
+
+ case IPC_SET:
+ if (version == IPC_64) {
+ err = get_compat_shmid64_ds(&s64, uptr);
+ } else {
+ err = get_compat_shmid_ds(&s64, uptr);
+ }
+ if (err)
+ break;
+ p = compat_alloc_user_space(sizeof(s64));
+ if (copy_to_user(p, &s64, sizeof(s64)))
+ err = -EFAULT;
+ else
+ err = sys_shmctl(first, second, p);
+ break;
+
+ case IPC_STAT:
+ case SHM_STAT:
+ p = compat_alloc_user_space(sizeof(s64));
+ err = sys_shmctl(first, second, p);
+ if (err < 0)
+ break;
+ if (copy_from_user(&s64, p, sizeof(s64)))
+ err2 = -EFAULT;
+ else if (version == IPC_64)
+ err2 = put_compat_shmid64_ds(&s64, uptr);
+ else
+ err2 = put_compat_shmid_ds(&s64, uptr);
+ if (err2)
+ err = -EFAULT;
+ break;
+
+ case SHM_INFO:
+ p = compat_alloc_user_space(sizeof(struct shm_info));
+ err = sys_shmctl(first, second, p);
+ if (err < 0)
+ break;
+ err2 = put_compat_shm_info(p, uptr);
+ if (err2)
+ err = -EFAULT;
+ break;
+
+ default:
+ err = -EINVAL;
+ break;
+ }
+ return err;
+}
+
+long compat_sys_semtimedop(int semid, struct sembuf __user *tsems,
+ unsigned nsops, const struct compat_timespec __user *timeout)
+{
+ struct timespec __user *ts64 = NULL;
+ if (timeout) {
+ struct timespec ts;
+ ts64 = compat_alloc_user_space(sizeof(*ts64));
+ if (get_compat_timespec(&ts, timeout))
+ return -EFAULT;
+ if (copy_to_user(ts64, &ts, sizeof(ts)))
+ return -EFAULT;
+ }
+ return sys_semtimedop(semid, tsems, nsops, ts64);
+}
diff --git a/ipc/compat_mq.c b/ipc/compat_mq.c
new file mode 100644
index 00000000000..d8d1e9ff4e8
--- /dev/null
+++ b/ipc/compat_mq.c
@@ -0,0 +1,146 @@
+/*
+ * ipc/compat_mq.c
+ * 32 bit emulation for POSIX message queue system calls
+ *
+ * Copyright (C) 2004 IBM Deutschland Entwicklung GmbH, IBM Corporation
+ * Author: Arnd Bergmann <arnd@arndb.de>
+ */
+
+#include <linux/compat.h>
+#include <linux/fs.h>
+#include <linux/kernel.h>
+#include <linux/mqueue.h>
+#include <linux/syscalls.h>
+
+#include <asm/uaccess.h>
+
+struct compat_mq_attr {
+ compat_long_t mq_flags; /* message queue flags */
+ compat_long_t mq_maxmsg; /* maximum number of messages */
+ compat_long_t mq_msgsize; /* maximum message size */
+ compat_long_t mq_curmsgs; /* number of messages currently queued */
+ compat_long_t __reserved[4]; /* ignored for input, zeroed for output */
+};
+
+static inline int get_compat_mq_attr(struct mq_attr *attr,
+ const struct compat_mq_attr __user *uattr)
+{
+ if (!access_ok(VERIFY_READ, uattr, sizeof *uattr))
+ return -EFAULT;
+
+ return __get_user(attr->mq_flags, &uattr->mq_flags)
+ | __get_user(attr->mq_maxmsg, &uattr->mq_maxmsg)
+ | __get_user(attr->mq_msgsize, &uattr->mq_msgsize)
+ | __get_user(attr->mq_curmsgs, &uattr->mq_curmsgs);
+}
+
+static inline int put_compat_mq_attr(const struct mq_attr *attr,
+ struct compat_mq_attr __user *uattr)
+{
+ if (clear_user(uattr, sizeof *uattr))
+ return -EFAULT;
+
+ return __put_user(attr->mq_flags, &uattr->mq_flags)
+ | __put_user(attr->mq_maxmsg, &uattr->mq_maxmsg)
+ | __put_user(attr->mq_msgsize, &uattr->mq_msgsize)
+ | __put_user(attr->mq_curmsgs, &uattr->mq_curmsgs);
+}
+
+asmlinkage long compat_sys_mq_open(const char __user *u_name,
+ int oflag, compat_mode_t mode,
+ struct compat_mq_attr __user *u_attr)
+{
+ void __user *p = NULL;
+ if (u_attr && oflag & O_CREAT) {
+ struct mq_attr attr;
+ p = compat_alloc_user_space(sizeof(attr));
+ if (get_compat_mq_attr(&attr, u_attr) ||
+ copy_to_user(p, &attr, sizeof(attr)))
+ return -EFAULT;
+ }
+ return sys_mq_open(u_name, oflag, mode, p);
+}
+
+static int compat_prepare_timeout(struct timespec __user * *p,
+ const struct compat_timespec __user *u)
+{
+ struct timespec ts;
+ if (!u) {
+ *p = NULL;
+ return 0;
+ }
+ *p = compat_alloc_user_space(sizeof(ts));
+ if (get_compat_timespec(&ts, u) || copy_to_user(*p, &ts, sizeof(ts)))
+ return -EFAULT;
+ return 0;
+}
+
+asmlinkage long compat_sys_mq_timedsend(mqd_t mqdes,
+ const char __user *u_msg_ptr,
+ size_t msg_len, unsigned int msg_prio,
+ const struct compat_timespec __user *u_abs_timeout)
+{
+ struct timespec __user *u_ts;
+
+ if (compat_prepare_timeout(&u_ts, u_abs_timeout))
+ return -EFAULT;
+
+ return sys_mq_timedsend(mqdes, u_msg_ptr, msg_len,
+ msg_prio, u_ts);
+}
+
+asmlinkage ssize_t compat_sys_mq_timedreceive(mqd_t mqdes,
+ char __user *u_msg_ptr,
+ size_t msg_len, unsigned int __user *u_msg_prio,
+ const struct compat_timespec __user *u_abs_timeout)
+{
+ struct timespec __user *u_ts;
+ if (compat_prepare_timeout(&u_ts, u_abs_timeout))
+ return -EFAULT;
+
+ return sys_mq_timedreceive(mqdes, u_msg_ptr, msg_len,
+ u_msg_prio, u_ts);
+}
+
+asmlinkage long compat_sys_mq_notify(mqd_t mqdes,
+ const struct compat_sigevent __user *u_notification)
+{
+ struct sigevent __user *p = NULL;
+ if (u_notification) {
+ struct sigevent n;
+ p = compat_alloc_user_space(sizeof(*p));
+ if (get_compat_sigevent(&n, u_notification))
+ return -EFAULT;
+ if (n.sigev_notify == SIGEV_THREAD)
+ n.sigev_value.sival_ptr = compat_ptr(n.sigev_value.sival_int);
+ if (copy_to_user(p, &n, sizeof(*p)))
+ return -EFAULT;
+ }
+ return sys_mq_notify(mqdes, p);
+}
+
+asmlinkage long compat_sys_mq_getsetattr(mqd_t mqdes,
+ const struct compat_mq_attr __user *u_mqstat,
+ struct compat_mq_attr __user *u_omqstat)
+{
+ struct mq_attr mqstat;
+ struct mq_attr __user *p = compat_alloc_user_space(2 * sizeof(*p));
+ long ret;
+
+ if (u_mqstat) {
+ if (get_compat_mq_attr(&mqstat, u_mqstat) ||
+ copy_to_user(p, &mqstat, sizeof(mqstat)))
+ return -EFAULT;
+ }
+ ret = sys_mq_getsetattr(mqdes,
+ u_mqstat ? p : NULL,
+ u_omqstat ? p + 1 : NULL);
+ if (ret)
+ return ret;
+ if (u_omqstat) {
+ if (copy_from_user(&mqstat, p + 1, sizeof(mqstat)) ||
+ put_compat_mq_attr(&mqstat, u_omqstat))
+ return -EFAULT;
+ }
+ return 0;
+}
diff --git a/ipc/mqueue.c b/ipc/mqueue.c
new file mode 100644
index 00000000000..cb0cd3cf3b5
--- /dev/null
+++ b/ipc/mqueue.c
@@ -0,0 +1,1252 @@
+/*
+ * POSIX message queues filesystem for Linux.
+ *
+ * Copyright (C) 2003,2004 Krzysztof Benedyczak (golbi@mat.uni.torun.pl)
+ * Michal Wronski (wrona@mat.uni.torun.pl)
+ *
+ * Spinlocks: Mohamed Abbas (abbas.mohamed@intel.com)
+ * Lockless receive & send, fd based notify:
+ * Manfred Spraul (manfred@colorfullife.com)
+ *
+ * This file is released under the GPL.
+ */
+
+#include <linux/init.h>
+#include <linux/pagemap.h>
+#include <linux/file.h>
+#include <linux/mount.h>
+#include <linux/namei.h>
+#include <linux/sysctl.h>
+#include <linux/poll.h>
+#include <linux/mqueue.h>
+#include <linux/msg.h>
+#include <linux/skbuff.h>
+#include <linux/netlink.h>
+#include <linux/syscalls.h>
+#include <net/sock.h>
+#include "util.h"
+
+#define MQUEUE_MAGIC 0x19800202
+#define DIRENT_SIZE 20
+#define FILENT_SIZE 80
+
+#define SEND 0
+#define RECV 1
+
+#define STATE_NONE 0
+#define STATE_PENDING 1
+#define STATE_READY 2
+
+/* used by sysctl */
+#define FS_MQUEUE 1
+#define CTL_QUEUESMAX 2
+#define CTL_MSGMAX 3
+#define CTL_MSGSIZEMAX 4
+
+/* default values */
+#define DFLT_QUEUESMAX 256 /* max number of message queues */
+#define DFLT_MSGMAX 10 /* max number of messages in each queue */
+#define HARD_MSGMAX (131072/sizeof(void*))
+#define DFLT_MSGSIZEMAX 8192 /* max message size */
+
+#define NOTIFY_COOKIE_LEN 32
+
+struct ext_wait_queue { /* queue of sleeping tasks */
+ struct task_struct *task;
+ struct list_head list;
+ struct msg_msg *msg; /* ptr of loaded message */
+ int state; /* one of STATE_* values */
+};
+
+struct mqueue_inode_info {
+ spinlock_t lock;
+ struct inode vfs_inode;
+ wait_queue_head_t wait_q;
+
+ struct msg_msg **messages;
+ struct mq_attr attr;
+
+ struct sigevent notify;
+ pid_t notify_owner;
+ struct user_struct *user; /* user who created, for accouting */
+ struct sock *notify_sock;
+ struct sk_buff *notify_cookie;
+
+ /* for tasks waiting for free space and messages, respectively */
+ struct ext_wait_queue e_wait_q[2];
+
+ unsigned long qsize; /* size of queue in memory (sum of all msgs) */
+};
+
+static struct inode_operations mqueue_dir_inode_operations;
+static struct file_operations mqueue_file_operations;
+static struct super_operations mqueue_super_ops;
+static void remove_notification(struct mqueue_inode_info *info);
+
+static spinlock_t mq_lock;
+static kmem_cache_t *mqueue_inode_cachep;
+static struct vfsmount *mqueue_mnt;
+
+static unsigned int queues_count;
+static unsigned int queues_max = DFLT_QUEUESMAX;
+static unsigned int msg_max = DFLT_MSGMAX;
+static unsigned int msgsize_max = DFLT_MSGSIZEMAX;
+
+static struct ctl_table_header * mq_sysctl_table;
+
+static inline struct mqueue_inode_info *MQUEUE_I(struct inode *inode)
+{
+ return container_of(inode, struct mqueue_inode_info, vfs_inode);
+}
+
+static struct inode *mqueue_get_inode(struct super_block *sb, int mode,
+ struct mq_attr *attr)
+{
+ struct inode *inode;
+
+ inode = new_inode(sb);
+ if (inode) {
+ inode->i_mode = mode;
+ inode->i_uid = current->fsuid;
+ inode->i_gid = current->fsgid;
+ inode->i_blksize = PAGE_CACHE_SIZE;
+ inode->i_blocks = 0;
+ inode->i_mtime = inode->i_ctime = inode->i_atime =
+ CURRENT_TIME;
+
+ if (S_ISREG(mode)) {
+ struct mqueue_inode_info *info;
+ struct task_struct *p = current;
+ struct user_struct *u = p->user;
+ unsigned long mq_bytes, mq_msg_tblsz;
+
+ inode->i_fop = &mqueue_file_operations;
+ inode->i_size = FILENT_SIZE;
+ /* mqueue specific info */
+ info = MQUEUE_I(inode);
+ spin_lock_init(&info->lock);
+ init_waitqueue_head(&info->wait_q);
+ INIT_LIST_HEAD(&info->e_wait_q[0].list);
+ INIT_LIST_HEAD(&info->e_wait_q[1].list);
+ info->messages = NULL;
+ info->notify_owner = 0;
+ info->qsize = 0;
+ info->user = NULL; /* set when all is ok */
+ memset(&info->attr, 0, sizeof(info->attr));
+ info->attr.mq_maxmsg = DFLT_MSGMAX;
+ info->attr.mq_msgsize = DFLT_MSGSIZEMAX;
+ if (attr) {
+ info->attr.mq_maxmsg = attr->mq_maxmsg;
+ info->attr.mq_msgsize = attr->mq_msgsize;
+ }
+ mq_msg_tblsz = info->attr.mq_maxmsg * sizeof(struct msg_msg *);
+ mq_bytes = (mq_msg_tblsz +
+ (info->attr.mq_maxmsg * info->attr.mq_msgsize));
+
+ spin_lock(&mq_lock);
+ if (u->mq_bytes + mq_bytes < u->mq_bytes ||
+ u->mq_bytes + mq_bytes >
+ p->signal->rlim[RLIMIT_MSGQUEUE].rlim_cur) {
+ spin_unlock(&mq_lock);
+ goto out_inode;
+ }
+ u->mq_bytes += mq_bytes;
+ spin_unlock(&mq_lock);
+
+ info->messages = kmalloc(mq_msg_tblsz, GFP_KERNEL);
+ if (!info->messages) {
+ spin_lock(&mq_lock);
+ u->mq_bytes -= mq_bytes;
+ spin_unlock(&mq_lock);
+ goto out_inode;
+ }
+ /* all is ok */
+ info->user = get_uid(u);
+ } else if (S_ISDIR(mode)) {
+ inode->i_nlink++;
+ /* Some things misbehave if size == 0 on a directory */
+ inode->i_size = 2 * DIRENT_SIZE;
+ inode->i_op = &mqueue_dir_inode_operations;
+ inode->i_fop = &simple_dir_operations;
+ }
+ }
+ return inode;
+out_inode:
+ make_bad_inode(inode);
+ iput(inode);
+ return NULL;
+}
+
+static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
+{
+ struct inode *inode;
+
+ sb->s_blocksize = PAGE_CACHE_SIZE;
+ sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
+ sb->s_magic = MQUEUE_MAGIC;
+ sb->s_op = &mqueue_super_ops;
+
+ inode = mqueue_get_inode(sb, S_IFDIR | S_ISVTX | S_IRWXUGO, NULL);
+ if (!inode)
+ return -ENOMEM;
+
+ sb->s_root = d_alloc_root(inode);
+ if (!sb->s_root) {
+ iput(inode);
+ return -ENOMEM;
+ }
+
+ return 0;
+}
+
+static struct super_block *mqueue_get_sb(struct file_system_type *fs_type,
+ int flags, const char *dev_name,
+ void *data)
+{
+ return get_sb_single(fs_type, flags, data, mqueue_fill_super);
+}
+
+static void init_once(void *foo, kmem_cache_t * cachep, unsigned long flags)
+{
+ struct mqueue_inode_info *p = (struct mqueue_inode_info *) foo;
+
+ if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
+ SLAB_CTOR_CONSTRUCTOR)
+ inode_init_once(&p->vfs_inode);
+}
+
+static struct inode *mqueue_alloc_inode(struct super_block *sb)
+{
+ struct mqueue_inode_info *ei;
+
+ ei = kmem_cache_alloc(mqueue_inode_cachep, SLAB_KERNEL);
+ if (!ei)
+ return NULL;
+ return &ei->vfs_inode;
+}
+
+static void mqueue_destroy_inode(struct inode *inode)
+{
+ kmem_cache_free(mqueue_inode_cachep, MQUEUE_I(inode));
+}
+
+static void mqueue_delete_inode(struct inode *inode)
+{
+ struct mqueue_inode_info *info;
+ struct user_struct *user;
+ unsigned long mq_bytes;
+ int i;
+
+ if (S_ISDIR(inode->i_mode)) {
+ clear_inode(inode);
+ return;
+ }
+ info = MQUEUE_I(inode);
+ spin_lock(&info->lock);
+ for (i = 0; i < info->attr.mq_curmsgs; i++)
+ free_msg(info->messages[i]);
+ kfree(info->messages);
+ spin_unlock(&info->lock);
+
+ clear_inode(inode);
+
+ mq_bytes = (info->attr.mq_maxmsg * sizeof(struct msg_msg *) +
+ (info->attr.mq_maxmsg * info->attr.mq_msgsize));
+ user = info->user;
+ if (user) {
+ spin_lock(&mq_lock);
+ user->mq_bytes -= mq_bytes;
+ queues_count--;
+ spin_unlock(&mq_lock);
+ free_uid(user);
+ }
+}
+
+static int mqueue_create(struct inode *dir, struct dentry *dentry,
+ int mode, struct nameidata *nd)
+{
+ struct inode *inode;
+ struct mq_attr *attr = dentry->d_fsdata;
+ int error;
+
+ spin_lock(&mq_lock);
+ if (queues_count >= queues_max && !capable(CAP_SYS_RESOURCE)) {
+ error = -ENOSPC;
+ goto out_lock;
+ }
+ queues_count++;
+ spin_unlock(&mq_lock);
+
+ inode = mqueue_get_inode(dir->i_sb, mode, attr);
+ if (!inode) {
+ error = -ENOMEM;
+ spin_lock(&mq_lock);
+ queues_count--;
+ goto out_lock;
+ }
+
+ dir->i_size += DIRENT_SIZE;
+ dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
+
+ d_instantiate(dentry, inode);
+ dget(dentry);
+ return 0;
+out_lock:
+ spin_unlock(&mq_lock);
+ return error;
+}
+
+static int mqueue_unlink(struct inode *dir, struct dentry *dentry)
+{
+ struct inode *inode = dentry->d_inode;
+
+ dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
+ dir->i_size -= DIRENT_SIZE;
+ inode->i_nlink--;
+ dput(dentry);
+ return 0;
+}
+
+/*
+* This is routine for system read from queue file.
+* To avoid mess with doing here some sort of mq_receive we allow
+* to read only queue size & notification info (the only values
+* that are interesting from user point of view and aren't accessible
+* through std routines)
+*/
+static ssize_t mqueue_read_file(struct file *filp, char __user *u_data,
+ size_t count, loff_t * off)
+{
+ struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
+ char buffer[FILENT_SIZE];
+ size_t slen;
+ loff_t o;
+
+ if (!count)
+ return 0;
+
+ spin_lock(&info->lock);
+ snprintf(buffer, sizeof(buffer),
+ "QSIZE:%-10lu NOTIFY:%-5d SIGNO:%-5d NOTIFY_PID:%-6d\n",
+ info->qsize,
+ info->notify_owner ? info->notify.sigev_notify : 0,
+ (info->notify_owner &&
+ info->notify.sigev_notify == SIGEV_SIGNAL) ?
+ info->notify.sigev_signo : 0,
+ info->notify_owner);
+ spin_unlock(&info->lock);
+ buffer[sizeof(buffer)-1] = '\0';
+ slen = strlen(buffer)+1;
+
+ o = *off;
+ if (o > slen)
+ return 0;
+
+ if (o + count > slen)
+ count = slen - o;
+
+ if (copy_to_user(u_data, buffer + o, count))
+ return -EFAULT;
+
+ *off = o + count;
+ filp->f_dentry->d_inode->i_atime = filp->f_dentry->d_inode->i_ctime = CURRENT_TIME;
+ return count;
+}
+
+static int mqueue_flush_file(struct file *filp)
+{
+ struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
+
+ spin_lock(&info->lock);
+ if (current->tgid == info->notify_owner)
+ remove_notification(info);
+
+ spin_unlock(&info->lock);
+ return 0;
+}
+
+static unsigned int mqueue_poll_file(struct file *filp, struct poll_table_struct *poll_tab)
+{
+ struct mqueue_inode_info *info = MQUEUE_I(filp->f_dentry->d_inode);
+ int retval = 0;
+
+ poll_wait(filp, &info->wait_q, poll_tab);
+
+ spin_lock(&info->lock);
+ if (info->attr.mq_curmsgs)
+ retval = POLLIN | POLLRDNORM;
+
+ if (info->attr.mq_curmsgs < info->attr.mq_maxmsg)
+ retval |= POLLOUT | POLLWRNORM;
+ spin_unlock(&info->lock);
+
+ return retval;
+}
+
+/* Adds current to info->e_wait_q[sr] before element with smaller prio */
+static void wq_add(struct mqueue_inode_info *info, int sr,
+ struct ext_wait_queue *ewp)
+{
+ struct ext_wait_queue *walk;
+
+ ewp->task = current;
+
+ list_for_each_entry(walk, &info->e_wait_q[sr].list, list) {
+ if (walk->task->static_prio <= current->static_prio) {
+ list_add_tail(&ewp->list, &walk->list);
+ return;
+ }
+ }
+ list_add_tail(&ewp->list, &info->e_wait_q[sr].list);
+}
+
+/*
+ * Puts current task to sleep. Caller must hold queue lock. After return
+ * lock isn't held.
+ * sr: SEND or RECV
+ */
+static int wq_sleep(struct mqueue_inode_info *info, int sr,
+ long timeout, struct ext_wait_queue *ewp)
+{
+ int retval;
+ signed long time;
+
+ wq_add(info, sr, ewp);
+
+ for (;;) {
+ set_current_state(TASK_INTERRUPTIBLE);
+
+ spin_unlock(&info->lock);
+ time = schedule_timeout(timeout);
+
+ while (ewp->state == STATE_PENDING)
+ cpu_relax();
+
+ if (ewp->state == STATE_READY) {
+ retval = 0;
+ goto out;
+ }
+ spin_lock(&info->lock);
+ if (ewp->state == STATE_READY) {
+ retval = 0;
+ goto out_unlock;
+ }
+ if (signal_pending(current)) {
+ retval = -ERESTARTSYS;
+ break;
+ }
+ if (time == 0) {
+ retval = -ETIMEDOUT;
+ break;
+ }
+ }
+ list_del(&ewp->list);
+out_unlock:
+ spin_unlock(&info->lock);
+out:
+ return retval;
+}
+
+/*
+ * Returns waiting task that should be serviced first or NULL if none exists
+ */
+static struct ext_wait_queue *wq_get_first_waiter(
+ struct mqueue_inode_info *info, int sr)
+{
+ struct list_head *ptr;
+
+ ptr = info->e_wait_q[sr].list.prev;
+ if (ptr == &info->e_wait_q[sr].list)
+ return NULL;
+ return list_entry(ptr, struct ext_wait_queue, list);
+}
+
+/* Auxiliary functions to manipulate messages' list */
+static void msg_insert(struct msg_msg *ptr, struct mqueue_inode_info *info)
+{
+ int k;
+
+ k = info->attr.mq_curmsgs - 1;
+ while (k >= 0 && info->messages[k]->m_type >= ptr->m_type) {
+ info->messages[k + 1] = info->messages[k];
+ k--;
+ }
+ info->attr.mq_curmsgs++;
+ info->qsize += ptr->m_ts;
+ info->messages[k + 1] = ptr;
+}
+
+static inline struct msg_msg *msg_get(struct mqueue_inode_info *info)
+{
+ info->qsize -= info->messages[--info->attr.mq_curmsgs]->m_ts;
+ return info->messages[info->attr.mq_curmsgs];
+}
+
+static inline void set_cookie(struct sk_buff *skb, char code)
+{
+ ((char*)skb->data)[NOTIFY_COOKIE_LEN-1] = code;
+}
+
+/*
+ * The next function is only to split too long sys_mq_timedsend
+ */
+static void __do_notify(struct mqueue_inode_info *info)
+{
+ /* notification
+ * invoked when there is registered process and there isn't process
+ * waiting synchronously for message AND state of queue changed from
+ * empty to not empty. Here we are sure that no one is waiting
+ * synchronously. */
+ if (info->notify_owner &&
+ info->attr.mq_curmsgs == 1) {
+ struct siginfo sig_i;
+ switch (info->notify.sigev_notify) {
+ case SIGEV_NONE:
+ break;
+ case SIGEV_SIGNAL:
+ /* sends signal */
+
+ sig_i.si_signo = info->notify.sigev_signo;
+ sig_i.si_errno = 0;
+ sig_i.si_code = SI_MESGQ;
+ sig_i.si_value = info->notify.sigev_value;
+ sig_i.si_pid = current->tgid;
+ sig_i.si_uid = current->uid;
+
+ kill_proc_info(info->notify.sigev_signo,
+ &sig_i, info->notify_owner);
+ break;
+ case SIGEV_THREAD:
+ set_cookie(info->notify_cookie, NOTIFY_WOKENUP);
+ netlink_sendskb(info->notify_sock,
+ info->notify_cookie, 0);
+ break;
+ }
+ /* after notification unregisters process */
+ info->notify_owner = 0;
+ }
+ wake_up(&info->wait_q);
+}
+
+static long prepare_timeout(const struct timespec __user *u_arg)
+{
+ struct timespec ts, nowts;
+ long timeout;
+
+ if (u_arg) {
+ if (unlikely(copy_from_user(&ts, u_arg,
+ sizeof(struct timespec))))
+ return -EFAULT;
+
+ if (unlikely(ts.tv_nsec < 0 || ts.tv_sec < 0
+ || ts.tv_nsec >= NSEC_PER_SEC))
+ return -EINVAL;
+ nowts = CURRENT_TIME;
+ /* first subtract as jiffies can't be too big */
+ ts.tv_sec -= nowts.tv_sec;
+ if (ts.tv_nsec < nowts.tv_nsec) {
+ ts.tv_nsec += NSEC_PER_SEC;
+ ts.tv_sec--;
+ }
+ ts.tv_nsec -= nowts.tv_nsec;
+ if (ts.tv_sec < 0)
+ return 0;
+
+ timeout = timespec_to_jiffies(&ts) + 1;
+ } else
+ return MAX_SCHEDULE_TIMEOUT;
+
+ return timeout;
+}
+
+static void remove_notification(struct mqueue_inode_info *info)
+{
+ if (info->notify_owner != 0 &&
+ info->notify.sigev_notify == SIGEV_THREAD) {
+ set_cookie(info->notify_cookie, NOTIFY_REMOVED);
+ netlink_sendskb(info->notify_sock, info->notify_cookie, 0);
+ }
+ info->notify_owner = 0;
+}
+
+static int mq_attr_ok(struct mq_attr *attr)
+{
+ if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
+ return 0;
+ if (capable(CAP_SYS_RESOURCE)) {
+ if (attr->mq_maxmsg > HARD_MSGMAX)
+ return 0;
+ } else {
+ if (attr->mq_maxmsg > msg_max ||
+ attr->mq_msgsize > msgsize_max)
+ return 0;
+ }
+ /* check for overflow */
+ if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
+ return 0;
+ if ((unsigned long)(attr->mq_maxmsg * attr->mq_msgsize) +
+ (attr->mq_maxmsg * sizeof (struct msg_msg *)) <
+ (unsigned long)(attr->mq_maxmsg * attr->mq_msgsize))
+ return 0;
+ return 1;
+}
+
+/*
+ * Invoked when creating a new queue via sys_mq_open
+ */
+static struct file *do_create(struct dentry *dir, struct dentry *dentry,
+ int oflag, mode_t mode, struct mq_attr __user *u_attr)
+{
+ struct file *filp;
+ struct mq_attr attr;
+ int ret;
+
+ if (u_attr != NULL) {
+ if (copy_from_user(&attr, u_attr, sizeof(attr)))
+ return ERR_PTR(-EFAULT);
+ if (!mq_attr_ok(&attr))
+ return ERR_PTR(-EINVAL);
+ /* store for use during create */
+ dentry->d_fsdata = &attr;
+ }
+
+ ret = vfs_create(dir->d_inode, dentry, mode, NULL);
+ dentry->d_fsdata = NULL;
+ if (ret)
+ return ERR_PTR(ret);
+
+ filp = dentry_open(dentry, mqueue_mnt, oflag);
+ if (!IS_ERR(filp))
+ dget(dentry);
+
+ return filp;
+}
+
+/* Opens existing queue */
+static struct file *do_open(struct dentry *dentry, int oflag)
+{
+static int oflag2acc[O_ACCMODE] = { MAY_READ, MAY_WRITE,
+ MAY_READ | MAY_WRITE };
+ struct file *filp;
+
+ if ((oflag & O_ACCMODE) == (O_RDWR | O_WRONLY))
+ return ERR_PTR(-EINVAL);
+
+ if (permission(dentry->d_inode, oflag2acc[oflag & O_ACCMODE], NULL))
+ return ERR_PTR(-EACCES);
+
+ filp = dentry_open(dentry, mqueue_mnt, oflag);
+
+ if (!IS_ERR(filp))
+ dget(dentry);
+
+ return filp;
+}
+
+asmlinkage long sys_mq_open(const char __user *u_name, int oflag, mode_t mode,
+ struct mq_attr __user *u_attr)
+{
+ struct dentry *dentry;
+ struct file *filp;
+ char *name;
+ int fd, error;
+
+ if (IS_ERR(name = getname(u_name)))
+ return PTR_ERR(name);
+
+ fd = get_unused_fd();
+ if (fd < 0)
+ goto out_putname;
+
+ down(&mqueue_mnt->mnt_root->d_inode->i_sem);
+ dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
+ if (IS_ERR(dentry)) {
+ error = PTR_ERR(dentry);
+ goto out_err;
+ }
+ mntget(mqueue_mnt);
+
+ if (oflag & O_CREAT) {
+ if (dentry->d_inode) { /* entry already exists */
+ filp = (oflag & O_EXCL) ? ERR_PTR(-EEXIST) :
+ do_open(dentry, oflag);
+ } else {
+ filp = do_create(mqueue_mnt->mnt_root, dentry,
+ oflag, mode, u_attr);
+ }
+ } else
+ filp = (dentry->d_inode) ? do_open(dentry, oflag) :
+ ERR_PTR(-ENOENT);
+
+ dput(dentry);
+
+ if (IS_ERR(filp)) {
+ error = PTR_ERR(filp);
+ goto out_putfd;
+ }
+
+ set_close_on_exec(fd, 1);
+ fd_install(fd, filp);
+ goto out_upsem;
+
+out_putfd:
+ mntput(mqueue_mnt);
+ put_unused_fd(fd);
+out_err:
+ fd = error;
+out_upsem:
+ up(&mqueue_mnt->mnt_root->d_inode->i_sem);
+out_putname:
+ putname(name);
+ return fd;
+}
+
+asmlinkage long sys_mq_unlink(const char __user *u_name)
+{
+ int err;
+ char *name;
+ struct dentry *dentry;
+ struct inode *inode = NULL;
+
+ name = getname(u_name);
+ if (IS_ERR(name))
+ return PTR_ERR(name);
+
+ down(&mqueue_mnt->mnt_root->d_inode->i_sem);
+ dentry = lookup_one_len(name, mqueue_mnt->mnt_root, strlen(name));
+ if (IS_ERR(dentry)) {
+ err = PTR_ERR(dentry);
+ goto out_unlock;
+ }
+
+ if (!dentry->d_inode) {
+ err = -ENOENT;
+ goto out_err;
+ }
+
+ inode = dentry->d_inode;
+ if (inode)
+ atomic_inc(&inode->i_count);
+
+ err = vfs_unlink(dentry->d_parent->d_inode, dentry);
+out_err:
+ dput(dentry);
+
+out_unlock:
+ up(&mqueue_mnt->mnt_root->d_inode->i_sem);
+ putname(name);
+ if (inode)
+ iput(inode);
+
+ return err;
+}
+
+/* Pipelined send and receive functions.
+ *
+ * If a receiver finds no waiting message, then it registers itself in the
+ * list of waiting receivers. A sender checks that list before adding the new
+ * message into the message array. If there is a waiting receiver, then it
+ * bypasses the message array and directly hands the message over to the
+ * receiver.
+ * The receiver accepts the message and returns without grabbing the queue
+ * spinlock. Therefore an intermediate STATE_PENDING state and memory barriers
+ * are necessary. The same algorithm is used for sysv semaphores, see
+ * ipc/sem.c fore more details.
+ *
+ * The same algorithm is used for senders.
+ */
+
+/* pipelined_send() - send a message directly to the task waiting in
+ * sys_mq_timedreceive() (without inserting message into a queue).
+ */
+static inline void pipelined_send(struct mqueue_inode_info *info,
+ struct msg_msg *message,
+ struct ext_wait_queue *receiver)
+{
+ receiver->msg = message;
+ list_del(&receiver->list);
+ receiver->state = STATE_PENDING;
+ wake_up_process(receiver->task);
+ wmb();
+ receiver->state = STATE_READY;
+}
+
+/* pipelined_receive() - if there is task waiting in sys_mq_timedsend()
+ * gets its message and put to the queue (we have one free place for sure). */
+static inline void pipelined_receive(struct mqueue_inode_info *info)
+{
+ struct ext_wait_queue *sender = wq_get_first_waiter(info, SEND);
+
+ if (!sender) {
+ /* for poll */
+ wake_up_interruptible(&info->wait_q);
+ return;
+ }
+ msg_insert(sender->msg, info);
+ list_del(&sender->list);
+ sender->state = STATE_PENDING;
+ wake_up_process(sender->task);
+ wmb();
+ sender->state = STATE_READY;
+}
+
+asmlinkage long sys_mq_timedsend(mqd_t mqdes, const char __user *u_msg_ptr,
+ size_t msg_len, unsigned int msg_prio,
+ const struct timespec __user *u_abs_timeout)
+{
+ struct file *filp;
+ struct inode *inode;
+ struct ext_wait_queue wait;
+ struct ext_wait_queue *receiver;
+ struct msg_msg *msg_ptr;
+ struct mqueue_inode_info *info;
+ long timeout;
+ int ret;
+
+ if (unlikely(msg_prio >= (unsigned long) MQ_PRIO_MAX))
+ return -EINVAL;
+
+ timeout = prepare_timeout(u_abs_timeout);
+
+ ret = -EBADF;
+ filp = fget(mqdes);
+ if (unlikely(!filp))
+ goto out;
+
+ inode = filp->f_dentry->d_inode;
+ if (unlikely(filp->f_op != &mqueue_file_operations))
+ goto out_fput;
+ info = MQUEUE_I(inode);
+
+ if (unlikely(!(filp->f_mode & FMODE_WRITE)))
+ goto out_fput;
+
+ if (unlikely(msg_len > info->attr.mq_msgsize)) {
+ ret = -EMSGSIZE;
+ goto out_fput;
+ }
+
+ /* First try to allocate memory, before doing anything with
+ * existing queues. */
+ msg_ptr = load_msg(u_msg_ptr, msg_len);
+ if (IS_ERR(msg_ptr)) {
+ ret = PTR_ERR(msg_ptr);
+ goto out_fput;
+ }
+ msg_ptr->m_ts = msg_len;
+ msg_ptr->m_type = msg_prio;
+
+ spin_lock(&info->lock);
+
+ if (info->attr.mq_curmsgs == info->attr.mq_maxmsg) {
+ if (filp->f_flags & O_NONBLOCK) {
+ spin_unlock(&info->lock);
+ ret = -EAGAIN;
+ } else if (unlikely(timeout < 0)) {
+ spin_unlock(&info->lock);
+ ret = timeout;
+ } else {
+ wait.task = current;
+ wait.msg = (void *) msg_ptr;
+ wait.state = STATE_NONE;
+ ret = wq_sleep(info, SEND, timeout, &wait);
+ }
+ if (ret < 0)
+ free_msg(msg_ptr);
+ } else {
+ receiver = wq_get_first_waiter(info, RECV);
+ if (receiver) {
+ pipelined_send(info, msg_ptr, receiver);
+ } else {
+ /* adds message to the queue */
+ msg_insert(msg_ptr, info);
+ __do_notify(info);
+ }
+ inode->i_atime = inode->i_mtime = inode->i_ctime =
+ CURRENT_TIME;
+ spin_unlock(&info->lock);
+ ret = 0;
+ }
+out_fput:
+ fput(filp);
+out:
+ return ret;
+}
+
+asmlinkage ssize_t sys_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,
+ size_t msg_len, unsigned int __user *u_msg_prio,
+ const struct timespec __user *u_abs_timeout)
+{
+ long timeout;
+ ssize_t ret;
+ struct msg_msg *msg_ptr;
+ struct file *filp;
+ struct inode *inode;
+ struct mqueue_inode_info *info;
+ struct ext_wait_queue wait;
+
+ timeout = prepare_timeout(u_abs_timeout);
+
+ ret = -EBADF;
+ filp = fget(mqdes);
+ if (unlikely(!filp))
+ goto out;
+
+ inode = filp->f_dentry->d_inode;
+ if (unlikely(filp->f_op != &mqueue_file_operations))
+ goto out_fput;
+ info = MQUEUE_I(inode);
+
+ if (unlikely(!(filp->f_mode & FMODE_READ)))
+ goto out_fput;
+
+ /* checks if buffer is big enough */
+ if (unlikely(msg_len < info->attr.mq_msgsize)) {
+ ret = -EMSGSIZE;
+ goto out_fput;
+ }
+
+ spin_lock(&info->lock);
+ if (info->attr.mq_curmsgs == 0) {
+ if (filp->f_flags & O_NONBLOCK) {
+ spin_unlock(&info->lock);
+ ret = -EAGAIN;
+ msg_ptr = NULL;
+ } else if (unlikely(timeout < 0)) {
+ spin_unlock(&info->lock);
+ ret = timeout;
+ msg_ptr = NULL;
+ } else {
+ wait.task = current;
+ wait.state = STATE_NONE;
+ ret = wq_sleep(info, RECV, timeout, &wait);
+ msg_ptr = wait.msg;
+ }
+ } else {
+ msg_ptr = msg_get(info);
+
+ inode->i_atime = inode->i_mtime = inode->i_ctime =
+ CURRENT_TIME;
+
+ /* There is now free space in queue. */
+ pipelined_receive(info);
+ spin_unlock(&info->lock);
+ ret = 0;
+ }
+ if (ret == 0) {
+ ret = msg_ptr->m_ts;
+
+ if ((u_msg_prio && put_user(msg_ptr->m_type, u_msg_prio)) ||
+ store_msg(u_msg_ptr, msg_ptr, msg_ptr->m_ts)) {
+ ret = -EFAULT;
+ }
+ free_msg(msg_ptr);
+ }
+out_fput:
+ fput(filp);
+out:
+ return ret;
+}
+
+/*
+ * Notes: the case when user wants us to deregister (with NULL as pointer)
+ * and he isn't currently owner of notification, will be silently discarded.
+ * It isn't explicitly defined in the POSIX.
+ */
+asmlinkage long sys_mq_notify(mqd_t mqdes,
+ const struct sigevent __user *u_notification)
+{
+ int ret;
+ struct file *filp;
+ struct sock *sock;
+ struct inode *inode;
+ struct sigevent notification;
+ struct mqueue_inode_info *info;
+ struct sk_buff *nc;
+
+ nc = NULL;
+ sock = NULL;
+ if (u_notification != NULL) {
+ if (copy_from_user(&notification, u_notification,
+ sizeof(struct sigevent)))
+ return -EFAULT;
+
+ if (unlikely(notification.sigev_notify != SIGEV_NONE &&
+ notification.sigev_notify != SIGEV_SIGNAL &&
+ notification.sigev_notify != SIGEV_THREAD))
+ return -EINVAL;
+ if (notification.sigev_notify == SIGEV_SIGNAL &&
+ (notification.sigev_signo < 0 ||
+ notification.sigev_signo > _NSIG)) {
+ return -EINVAL;
+ }
+ if (notification.sigev_notify == SIGEV_THREAD) {
+ /* create the notify skb */
+ nc = alloc_skb(NOTIFY_COOKIE_LEN, GFP_KERNEL);
+ ret = -ENOMEM;
+ if (!nc)
+ goto out;
+ ret = -EFAULT;
+ if (copy_from_user(nc->data,
+ notification.sigev_value.sival_ptr,
+ NOTIFY_COOKIE_LEN)) {
+ goto out;
+ }
+
+ /* TODO: add a header? */
+ skb_put(nc, NOTIFY_COOKIE_LEN);
+ /* and attach it to the socket */
+retry:
+ filp = fget(notification.sigev_signo);
+ ret = -EBADF;
+ if (!filp)
+ goto out;
+ sock = netlink_getsockbyfilp(filp);
+ fput(filp);
+ if (IS_ERR(sock)) {
+ ret = PTR_ERR(sock);
+ sock = NULL;
+ goto out;
+ }
+
+ ret = netlink_attachskb(sock, nc, 0, MAX_SCHEDULE_TIMEOUT);
+ if (ret == 1)
+ goto retry;
+ if (ret) {
+ sock = NULL;
+ nc = NULL;
+ goto out;
+ }
+ }
+ }
+
+ ret = -EBADF;
+ filp = fget(mqdes);
+ if (!filp)
+ goto out;
+
+ inode = filp->f_dentry->d_inode;
+ if (unlikely(filp->f_op != &mqueue_file_operations))
+ goto out_fput;
+ info = MQUEUE_I(inode);
+
+ ret = 0;
+ spin_lock(&info->lock);
+ if (u_notification == NULL) {
+ if (info->notify_owner == current->tgid) {
+ remove_notification(info);
+ inode->i_atime = inode->i_ctime = CURRENT_TIME;
+ }
+ } else if (info->notify_owner != 0) {
+ ret = -EBUSY;
+ } else {
+ switch (notification.sigev_notify) {
+ case SIGEV_NONE:
+ info->notify.sigev_notify = SIGEV_NONE;
+ break;
+ case SIGEV_THREAD:
+ info->notify_sock = sock;
+ info->notify_cookie = nc;
+ sock = NULL;
+ nc = NULL;
+ info->notify.sigev_notify = SIGEV_THREAD;
+ break;
+ case SIGEV_SIGNAL:
+ info->notify.sigev_signo = notification.sigev_signo;
+ info->notify.sigev_value = notification.sigev_value;
+ info->notify.sigev_notify = SIGEV_SIGNAL;
+ break;
+ }
+ info->notify_owner = current->tgid;
+ inode->i_atime = inode->i_ctime = CURRENT_TIME;
+ }
+ spin_unlock(&info->lock);
+out_fput:
+ fput(filp);
+out:
+ if (sock) {
+ netlink_detachskb(sock, nc);
+ } else if (nc) {
+ dev_kfree_skb(nc);
+ }
+ return ret;
+}
+
+asmlinkage long sys_mq_getsetattr(mqd_t mqdes,
+ const struct mq_attr __user *u_mqstat,
+ struct mq_attr __user *u_omqstat)
+{
+ int ret;
+ struct mq_attr mqstat, omqstat;
+ struct file *filp;
+ struct inode *inode;
+ struct mqueue_inode_info *info;
+
+ if (u_mqstat != NULL) {
+ if (copy_from_user(&mqstat, u_mqstat, sizeof(struct mq_attr)))
+ return -EFAULT;
+ if (mqstat.mq_flags & (~O_NONBLOCK))
+ return -EINVAL;
+ }
+
+ ret = -EBADF;
+ filp = fget(mqdes);
+ if (!filp)
+ goto out;
+
+ inode = filp->f_dentry->d_inode;
+ if (unlikely(filp->f_op != &mqueue_file_operations))
+ goto out_fput;
+ info = MQUEUE_I(inode);
+
+ spin_lock(&info->lock);
+
+ omqstat = info->attr;
+ omqstat.mq_flags = filp->f_flags & O_NONBLOCK;
+ if (u_mqstat) {
+ if (mqstat.mq_flags & O_NONBLOCK)
+ filp->f_flags |= O_NONBLOCK;
+ else
+ filp->f_flags &= ~O_NONBLOCK;
+
+ inode->i_atime = inode->i_ctime = CURRENT_TIME;
+ }
+
+ spin_unlock(&info->lock);
+
+ ret = 0;
+ if (u_omqstat != NULL && copy_to_user(u_omqstat, &omqstat,
+ sizeof(struct mq_attr)))
+ ret = -EFAULT;
+
+out_fput:
+ fput(filp);
+out:
+ return ret;
+}
+
+static struct inode_operations mqueue_dir_inode_operations = {
+ .lookup = simple_lookup,
+ .create = mqueue_create,
+ .unlink = mqueue_unlink,
+};
+
+static struct file_operations mqueue_file_operations = {
+ .flush = mqueue_flush_file,
+ .poll = mqueue_poll_file,
+ .read = mqueue_read_file,
+};
+
+static struct super_operations mqueue_super_ops = {
+ .alloc_inode = mqueue_alloc_inode,
+ .destroy_inode = mqueue_destroy_inode,
+ .statfs = simple_statfs,
+ .delete_inode = mqueue_delete_inode,
+ .drop_inode = generic_delete_inode,
+};
+
+static struct file_system_type mqueue_fs_type = {
+ .name = "mqueue",
+ .get_sb = mqueue_get_sb,
+ .kill_sb = kill_litter_super,
+};
+
+static int msg_max_limit_min = DFLT_MSGMAX;
+static int msg_max_limit_max = HARD_MSGMAX;
+
+static int msg_maxsize_limit_min = DFLT_MSGSIZEMAX;
+static int msg_maxsize_limit_max = INT_MAX;
+
+static ctl_table mq_sysctls[] = {
+ {
+ .ctl_name = CTL_QUEUESMAX,
+ .procname = "queues_max",
+ .data = &queues_max,
+ .maxlen = sizeof(int),
+ .mode = 0644,
+ .proc_handler = &proc_dointvec,
+ },
+ {
+ .ctl_name = CTL_MSGMAX,
+ .procname = "msg_max",
+ .data = &msg_max,
+ .maxlen = sizeof(int),
+ .mode = 0644,
+ .proc_handler = &proc_dointvec_minmax,
+ .extra1 = &msg_max_limit_min,
+ .extra2 = &msg_max_limit_max,
+ },
+ {
+ .ctl_name = CTL_MSGSIZEMAX,
+ .procname = "msgsize_max",
+ .data = &msgsize_max,
+ .maxlen = sizeof(int),
+ .mode = 0644,
+ .proc_handler = &proc_dointvec_minmax,
+ .extra1 = &msg_maxsize_limit_min,
+ .extra2 = &msg_maxsize_limit_max,
+ },
+ { .ctl_name = 0 }
+};
+
+static ctl_table mq_sysctl_dir[] = {
+ {
+ .ctl_name = FS_MQUEUE,
+ .procname = "mqueue",
+ .mode = 0555,
+ .child = mq_sysctls,
+ },
+ { .ctl_name = 0 }
+};
+
+static ctl_table mq_sysctl_root[] = {
+ {
+ .ctl_name = CTL_FS,
+ .procname = "fs",
+ .mode = 0555,
+ .child = mq_sysctl_dir,
+ },
+ { .ctl_name = 0 }
+};
+
+static int __init init_mqueue_fs(void)
+{
+ int error;
+
+ mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
+ sizeof(struct mqueue_inode_info), 0,
+ SLAB_HWCACHE_ALIGN, init_once, NULL);
+ if (mqueue_inode_cachep == NULL)
+ return -ENOMEM;
+
+ /* ignore failues - they are not fatal */
+ mq_sysctl_table = register_sysctl_table(mq_sysctl_root, 0);
+
+ error = register_filesystem(&mqueue_fs_type);
+ if (error)
+ goto out_sysctl;
+
+ if (IS_ERR(mqueue_mnt = kern_mount(&mqueue_fs_type))) {
+ error = PTR_ERR(mqueue_mnt);
+ goto out_filesystem;
+ }
+
+ /* internal initialization - not common for vfs */
+ queues_count = 0;
+ spin_lock_init(&mq_lock);
+
+ return 0;
+
+out_filesystem:
+ unregister_filesystem(&mqueue_fs_type);
+out_sysctl:
+ if (mq_sysctl_table)
+ unregister_sysctl_table(mq_sysctl_table);
+ if (kmem_cache_destroy(mqueue_inode_cachep)) {
+ printk(KERN_INFO
+ "mqueue_inode_cache: not all structures were freed\n");
+ }
+ return error;
+}
+
+__initcall(init_mqueue_fs);
diff --git a/ipc/msg.c b/ipc/msg.c
new file mode 100644
index 00000000000..27e516f96cd
--- /dev/null
+++ b/ipc/msg.c
@@ -0,0 +1,862 @@
+/*
+ * linux/ipc/msg.c
+ * Copyright (C) 1992 Krishna Balasubramanian
+ *
+ * Removed all the remaining kerneld mess
+ * Catch the -EFAULT stuff properly
+ * Use GFP_KERNEL for messages as in 1.2
+ * Fixed up the unchecked user space derefs
+ * Copyright (C) 1998 Alan Cox & Andi Kleen
+ *
+ * /proc/sysvipc/msg support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
+ *
+ * mostly rewritten, threaded and wake-one semantics added
+ * MSGMAX limit removed, sysctl's added
+ * (c) 1999 Manfred Spraul <manfreds@colorfullife.com>
+ */
+
+#include <linux/config.h>
+#include <linux/slab.h>
+#include <linux/msg.h>
+#include <linux/spinlock.h>
+#include <linux/init.h>
+#include <linux/proc_fs.h>
+#include <linux/list.h>
+#include <linux/security.h>
+#include <linux/sched.h>
+#include <linux/syscalls.h>
+#include <linux/audit.h>
+#include <asm/current.h>
+#include <asm/uaccess.h>
+#include "util.h"
+
+/* sysctl: */
+int msg_ctlmax = MSGMAX;
+int msg_ctlmnb = MSGMNB;
+int msg_ctlmni = MSGMNI;
+
+/* one msg_receiver structure for each sleeping receiver */
+struct msg_receiver {
+ struct list_head r_list;
+ struct task_struct* r_tsk;
+
+ int r_mode;
+ long r_msgtype;
+ long r_maxsize;
+
+ struct msg_msg* volatile r_msg;
+};
+
+/* one msg_sender for each sleeping sender */
+struct msg_sender {
+ struct list_head list;
+ struct task_struct* tsk;
+};
+
+#define SEARCH_ANY 1
+#define SEARCH_EQUAL 2
+#define SEARCH_NOTEQUAL 3
+#define SEARCH_LESSEQUAL 4
+
+static atomic_t msg_bytes = ATOMIC_INIT(0);
+static atomic_t msg_hdrs = ATOMIC_INIT(0);
+
+static struct ipc_ids msg_ids;
+
+#define msg_lock(id) ((struct msg_queue*)ipc_lock(&msg_ids,id))
+#define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
+#define msg_rmid(id) ((struct msg_queue*)ipc_rmid(&msg_ids,id))
+#define msg_checkid(msq, msgid) \
+ ipc_checkid(&msg_ids,&msq->q_perm,msgid)
+#define msg_buildid(id, seq) \
+ ipc_buildid(&msg_ids, id, seq)
+
+static void freeque (struct msg_queue *msq, int id);
+static int newque (key_t key, int msgflg);
+#ifdef CONFIG_PROC_FS
+static int sysvipc_msg_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
+#endif
+
+void __init msg_init (void)
+{
+ ipc_init_ids(&msg_ids,msg_ctlmni);
+
+#ifdef CONFIG_PROC_FS
+ create_proc_read_entry("sysvipc/msg", 0, NULL, sysvipc_msg_read_proc, NULL);
+#endif
+}
+
+static int newque (key_t key, int msgflg)
+{
+ int id;
+ int retval;
+ struct msg_queue *msq;
+
+ msq = ipc_rcu_alloc(sizeof(*msq));
+ if (!msq)
+ return -ENOMEM;
+
+ msq->q_perm.mode = (msgflg & S_IRWXUGO);
+ msq->q_perm.key = key;
+
+ msq->q_perm.security = NULL;
+ retval = security_msg_queue_alloc(msq);
+ if (retval) {
+ ipc_rcu_putref(msq);
+ return retval;
+ }
+
+ id = ipc_addid(&msg_ids, &msq->q_perm, msg_ctlmni);
+ if(id == -1) {
+ security_msg_queue_free(msq);
+ ipc_rcu_putref(msq);
+ return -ENOSPC;
+ }
+
+ msq->q_stime = msq->q_rtime = 0;
+ msq->q_ctime = get_seconds();
+ msq->q_cbytes = msq->q_qnum = 0;
+ msq->q_qbytes = msg_ctlmnb;
+ msq->q_lspid = msq->q_lrpid = 0;
+ INIT_LIST_HEAD(&msq->q_messages);
+ INIT_LIST_HEAD(&msq->q_receivers);
+ INIT_LIST_HEAD(&msq->q_senders);
+ msg_unlock(msq);
+
+ return msg_buildid(id,msq->q_perm.seq);
+}
+
+static inline void ss_add(struct msg_queue* msq, struct msg_sender* mss)
+{
+ mss->tsk=current;
+ current->state=TASK_INTERRUPTIBLE;
+ list_add_tail(&mss->list,&msq->q_senders);
+}
+
+static inline void ss_del(struct msg_sender* mss)
+{
+ if(mss->list.next != NULL)
+ list_del(&mss->list);
+}
+
+static void ss_wakeup(struct list_head* h, int kill)
+{
+ struct list_head *tmp;
+
+ tmp = h->next;
+ while (tmp != h) {
+ struct msg_sender* mss;
+
+ mss = list_entry(tmp,struct msg_sender,list);
+ tmp = tmp->next;
+ if(kill)
+ mss->list.next=NULL;
+ wake_up_process(mss->tsk);
+ }
+}
+
+static void expunge_all(struct msg_queue* msq, int res)
+{
+ struct list_head *tmp;
+
+ tmp = msq->q_receivers.next;
+ while (tmp != &msq->q_receivers) {
+ struct msg_receiver* msr;
+
+ msr = list_entry(tmp,struct msg_receiver,r_list);
+ tmp = tmp->next;
+ msr->r_msg = NULL;
+ wake_up_process(msr->r_tsk);
+ smp_mb();
+ msr->r_msg = ERR_PTR(res);
+ }
+}
+/*
+ * freeque() wakes up waiters on the sender and receiver waiting queue,
+ * removes the message queue from message queue ID
+ * array, and cleans up all the messages associated with this queue.
+ *
+ * msg_ids.sem and the spinlock for this message queue is hold
+ * before freeque() is called. msg_ids.sem remains locked on exit.
+ */
+static void freeque (struct msg_queue *msq, int id)
+{
+ struct list_head *tmp;
+
+ expunge_all(msq,-EIDRM);
+ ss_wakeup(&msq->q_senders,1);
+ msq = msg_rmid(id);
+ msg_unlock(msq);
+
+ tmp = msq->q_messages.next;
+ while(tmp != &msq->q_messages) {
+ struct msg_msg* msg = list_entry(tmp,struct msg_msg,m_list);
+ tmp = tmp->next;
+ atomic_dec(&msg_hdrs);
+ free_msg(msg);
+ }
+ atomic_sub(msq->q_cbytes, &msg_bytes);
+ security_msg_queue_free(msq);
+ ipc_rcu_putref(msq);
+}
+
+asmlinkage long sys_msgget (key_t key, int msgflg)
+{
+ int id, ret = -EPERM;
+ struct msg_queue *msq;
+
+ down(&msg_ids.sem);
+ if (key == IPC_PRIVATE)
+ ret = newque(key, msgflg);
+ else if ((id = ipc_findkey(&msg_ids, key)) == -1) { /* key not used */
+ if (!(msgflg & IPC_CREAT))
+ ret = -ENOENT;
+ else
+ ret = newque(key, msgflg);
+ } else if (msgflg & IPC_CREAT && msgflg & IPC_EXCL) {
+ ret = -EEXIST;
+ } else {
+ msq = msg_lock(id);
+ if(msq==NULL)
+ BUG();
+ if (ipcperms(&msq->q_perm, msgflg))
+ ret = -EACCES;
+ else {
+ int qid = msg_buildid(id, msq->q_perm.seq);
+ ret = security_msg_queue_associate(msq, msgflg);
+ if (!ret)
+ ret = qid;
+ }
+ msg_unlock(msq);
+ }
+ up(&msg_ids.sem);
+ return ret;
+}
+
+static inline unsigned long copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
+{
+ switch(version) {
+ case IPC_64:
+ return copy_to_user (buf, in, sizeof(*in));
+ case IPC_OLD:
+ {
+ struct msqid_ds out;
+
+ memset(&out,0,sizeof(out));
+
+ ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
+
+ out.msg_stime = in->msg_stime;
+ out.msg_rtime = in->msg_rtime;
+ out.msg_ctime = in->msg_ctime;
+
+ if(in->msg_cbytes > USHRT_MAX)
+ out.msg_cbytes = USHRT_MAX;
+ else
+ out.msg_cbytes = in->msg_cbytes;
+ out.msg_lcbytes = in->msg_cbytes;
+
+ if(in->msg_qnum > USHRT_MAX)
+ out.msg_qnum = USHRT_MAX;
+ else
+ out.msg_qnum = in->msg_qnum;
+
+ if(in->msg_qbytes > USHRT_MAX)
+ out.msg_qbytes = USHRT_MAX;
+ else
+ out.msg_qbytes = in->msg_qbytes;
+ out.msg_lqbytes = in->msg_qbytes;
+
+ out.msg_lspid = in->msg_lspid;
+ out.msg_lrpid = in->msg_lrpid;
+
+ return copy_to_user (buf, &out, sizeof(out));
+ }
+ default:
+ return -EINVAL;
+ }
+}
+
+struct msq_setbuf {
+ unsigned long qbytes;
+ uid_t uid;
+ gid_t gid;
+ mode_t mode;
+};
+
+static inline unsigned long copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
+{
+ switch(version) {
+ case IPC_64:
+ {
+ struct msqid64_ds tbuf;
+
+ if (copy_from_user (&tbuf, buf, sizeof (tbuf)))
+ return -EFAULT;
+
+ out->qbytes = tbuf.msg_qbytes;
+ out->uid = tbuf.msg_perm.uid;
+ out->gid = tbuf.msg_perm.gid;
+ out->mode = tbuf.msg_perm.mode;
+
+ return 0;
+ }
+ case IPC_OLD:
+ {
+ struct msqid_ds tbuf_old;
+
+ if (copy_from_user (&tbuf_old, buf, sizeof (tbuf_old)))
+ return -EFAULT;
+
+ out->uid = tbuf_old.msg_perm.uid;
+ out->gid = tbuf_old.msg_perm.gid;
+ out->mode = tbuf_old.msg_perm.mode;
+
+ if(tbuf_old.msg_qbytes == 0)
+ out->qbytes = tbuf_old.msg_lqbytes;
+ else
+ out->qbytes = tbuf_old.msg_qbytes;
+
+ return 0;
+ }
+ default:
+ return -EINVAL;
+ }
+}
+
+asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf)
+{
+ int err, version;
+ struct msg_queue *msq;
+ struct msq_setbuf setbuf;
+ struct kern_ipc_perm *ipcp;
+
+ if (msqid < 0 || cmd < 0)
+ return -EINVAL;
+
+ version = ipc_parse_version(&cmd);
+
+ switch (cmd) {
+ case IPC_INFO:
+ case MSG_INFO:
+ {
+ struct msginfo msginfo;
+ int max_id;
+ if (!buf)
+ return -EFAULT;
+ /* We must not return kernel stack data.
+ * due to padding, it's not enough
+ * to set all member fields.
+ */
+
+ err = security_msg_queue_msgctl(NULL, cmd);
+ if (err)
+ return err;
+
+ memset(&msginfo,0,sizeof(msginfo));
+ msginfo.msgmni = msg_ctlmni;
+ msginfo.msgmax = msg_ctlmax;
+ msginfo.msgmnb = msg_ctlmnb;
+ msginfo.msgssz = MSGSSZ;
+ msginfo.msgseg = MSGSEG;
+ down(&msg_ids.sem);
+ if (cmd == MSG_INFO) {
+ msginfo.msgpool = msg_ids.in_use;
+ msginfo.msgmap = atomic_read(&msg_hdrs);
+ msginfo.msgtql = atomic_read(&msg_bytes);
+ } else {
+ msginfo.msgmap = MSGMAP;
+ msginfo.msgpool = MSGPOOL;
+ msginfo.msgtql = MSGTQL;
+ }
+ max_id = msg_ids.max_id;
+ up(&msg_ids.sem);
+ if (copy_to_user (buf, &msginfo, sizeof(struct msginfo)))
+ return -EFAULT;
+ return (max_id < 0) ? 0: max_id;
+ }
+ case MSG_STAT:
+ case IPC_STAT:
+ {
+ struct msqid64_ds tbuf;
+ int success_return;
+ if (!buf)
+ return -EFAULT;
+ if(cmd == MSG_STAT && msqid >= msg_ids.entries->size)
+ return -EINVAL;
+
+ memset(&tbuf,0,sizeof(tbuf));
+
+ msq = msg_lock(msqid);
+ if (msq == NULL)
+ return -EINVAL;
+
+ if(cmd == MSG_STAT) {
+ success_return = msg_buildid(msqid, msq->q_perm.seq);
+ } else {
+ err = -EIDRM;
+ if (msg_checkid(msq,msqid))
+ goto out_unlock;
+ success_return = 0;
+ }
+ err = -EACCES;
+ if (ipcperms (&msq->q_perm, S_IRUGO))
+ goto out_unlock;
+
+ err = security_msg_queue_msgctl(msq, cmd);
+ if (err)
+ goto out_unlock;
+
+ kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
+ tbuf.msg_stime = msq->q_stime;
+ tbuf.msg_rtime = msq->q_rtime;
+ tbuf.msg_ctime = msq->q_ctime;
+ tbuf.msg_cbytes = msq->q_cbytes;
+ tbuf.msg_qnum = msq->q_qnum;
+ tbuf.msg_qbytes = msq->q_qbytes;
+ tbuf.msg_lspid = msq->q_lspid;
+ tbuf.msg_lrpid = msq->q_lrpid;
+ msg_unlock(msq);
+ if (copy_msqid_to_user(buf, &tbuf, version))
+ return -EFAULT;
+ return success_return;
+ }
+ case IPC_SET:
+ if (!buf)
+ return -EFAULT;
+ if (copy_msqid_from_user (&setbuf, buf, version))
+ return -EFAULT;
+ if ((err = audit_ipc_perms(setbuf.qbytes, setbuf.uid, setbuf.gid, setbuf.mode)))
+ return err;
+ break;
+ case IPC_RMID:
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ down(&msg_ids.sem);
+ msq = msg_lock(msqid);
+ err=-EINVAL;
+ if (msq == NULL)
+ goto out_up;
+
+ err = -EIDRM;
+ if (msg_checkid(msq,msqid))
+ goto out_unlock_up;
+ ipcp = &msq->q_perm;
+ err = -EPERM;
+ if (current->euid != ipcp->cuid &&
+ current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
+ /* We _could_ check for CAP_CHOWN above, but we don't */
+ goto out_unlock_up;
+
+ err = security_msg_queue_msgctl(msq, cmd);
+ if (err)
+ goto out_unlock_up;
+
+ switch (cmd) {
+ case IPC_SET:
+ {
+ err = -EPERM;
+ if (setbuf.qbytes > msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
+ goto out_unlock_up;
+
+ msq->q_qbytes = setbuf.qbytes;
+
+ ipcp->uid = setbuf.uid;
+ ipcp->gid = setbuf.gid;
+ ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
+ (S_IRWXUGO & setbuf.mode);
+ msq->q_ctime = get_seconds();
+ /* sleeping receivers might be excluded by
+ * stricter permissions.
+ */
+ expunge_all(msq,-EAGAIN);
+ /* sleeping senders might be able to send
+ * due to a larger queue size.
+ */
+ ss_wakeup(&msq->q_senders,0);
+ msg_unlock(msq);
+ break;
+ }
+ case IPC_RMID:
+ freeque (msq, msqid);
+ break;
+ }
+ err = 0;
+out_up:
+ up(&msg_ids.sem);
+ return err;
+out_unlock_up:
+ msg_unlock(msq);
+ goto out_up;
+out_unlock:
+ msg_unlock(msq);
+ return err;
+}
+
+static int testmsg(struct msg_msg* msg,long type,int mode)
+{
+ switch(mode)
+ {
+ case SEARCH_ANY:
+ return 1;
+ case SEARCH_LESSEQUAL:
+ if(msg->m_type <=type)
+ return 1;
+ break;
+ case SEARCH_EQUAL:
+ if(msg->m_type == type)
+ return 1;
+ break;
+ case SEARCH_NOTEQUAL:
+ if(msg->m_type != type)
+ return 1;
+ break;
+ }
+ return 0;
+}
+
+static inline int pipelined_send(struct msg_queue* msq, struct msg_msg* msg)
+{
+ struct list_head* tmp;
+
+ tmp = msq->q_receivers.next;
+ while (tmp != &msq->q_receivers) {
+ struct msg_receiver* msr;
+ msr = list_entry(tmp,struct msg_receiver,r_list);
+ tmp = tmp->next;
+ if(testmsg(msg,msr->r_msgtype,msr->r_mode) &&
+ !security_msg_queue_msgrcv(msq, msg, msr->r_tsk, msr->r_msgtype, msr->r_mode)) {
+ list_del(&msr->r_list);
+ if(msr->r_maxsize < msg->m_ts) {
+ msr->r_msg = NULL;
+ wake_up_process(msr->r_tsk);
+ smp_mb();
+ msr->r_msg = ERR_PTR(-E2BIG);
+ } else {
+ msr->r_msg = NULL;
+ msq->q_lrpid = msr->r_tsk->pid;
+ msq->q_rtime = get_seconds();
+ wake_up_process(msr->r_tsk);
+ smp_mb();
+ msr->r_msg = msg;
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+asmlinkage long sys_msgsnd (int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
+{
+ struct msg_queue *msq;
+ struct msg_msg *msg;
+ long mtype;
+ int err;
+
+ if (msgsz > msg_ctlmax || (long) msgsz < 0 || msqid < 0)
+ return -EINVAL;
+ if (get_user(mtype, &msgp->mtype))
+ return -EFAULT;
+ if (mtype < 1)
+ return -EINVAL;
+
+ msg = load_msg(msgp->mtext, msgsz);
+ if(IS_ERR(msg))
+ return PTR_ERR(msg);
+
+ msg->m_type = mtype;
+ msg->m_ts = msgsz;
+
+ msq = msg_lock(msqid);
+ err=-EINVAL;
+ if(msq==NULL)
+ goto out_free;
+
+ err= -EIDRM;
+ if (msg_checkid(msq,msqid))
+ goto out_unlock_free;
+
+ for (;;) {
+ struct msg_sender s;
+
+ err=-EACCES;
+ if (ipcperms(&msq->q_perm, S_IWUGO))
+ goto out_unlock_free;
+
+ err = security_msg_queue_msgsnd(msq, msg, msgflg);
+ if (err)
+ goto out_unlock_free;
+
+ if(msgsz + msq->q_cbytes <= msq->q_qbytes &&
+ 1 + msq->q_qnum <= msq->q_qbytes) {
+ break;
+ }
+
+ /* queue full, wait: */
+ if(msgflg&IPC_NOWAIT) {
+ err=-EAGAIN;
+ goto out_unlock_free;
+ }
+ ss_add(msq, &s);
+ ipc_rcu_getref(msq);
+ msg_unlock(msq);
+ schedule();
+
+ ipc_lock_by_ptr(&msq->q_perm);
+ ipc_rcu_putref(msq);
+ if (msq->q_perm.deleted) {
+ err = -EIDRM;
+ goto out_unlock_free;
+ }
+ ss_del(&s);
+
+ if (signal_pending(current)) {
+ err=-ERESTARTNOHAND;
+ goto out_unlock_free;
+ }
+ }
+
+ msq->q_lspid = current->tgid;
+ msq->q_stime = get_seconds();
+
+ if(!pipelined_send(msq,msg)) {
+ /* noone is waiting for this message, enqueue it */
+ list_add_tail(&msg->m_list,&msq->q_messages);
+ msq->q_cbytes += msgsz;
+ msq->q_qnum++;
+ atomic_add(msgsz,&msg_bytes);
+ atomic_inc(&msg_hdrs);
+ }
+
+ err = 0;
+ msg = NULL;
+
+out_unlock_free:
+ msg_unlock(msq);
+out_free:
+ if(msg!=NULL)
+ free_msg(msg);
+ return err;
+}
+
+static inline int convert_mode(long* msgtyp, int msgflg)
+{
+ /*
+ * find message of correct type.
+ * msgtyp = 0 => get first.
+ * msgtyp > 0 => get first message of matching type.
+ * msgtyp < 0 => get message with least type must be < abs(msgtype).
+ */
+ if(*msgtyp==0)
+ return SEARCH_ANY;
+ if(*msgtyp<0) {
+ *msgtyp=-(*msgtyp);
+ return SEARCH_LESSEQUAL;
+ }
+ if(msgflg & MSG_EXCEPT)
+ return SEARCH_NOTEQUAL;
+ return SEARCH_EQUAL;
+}
+
+asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz,
+ long msgtyp, int msgflg)
+{
+ struct msg_queue *msq;
+ struct msg_msg *msg;
+ int mode;
+
+ if (msqid < 0 || (long) msgsz < 0)
+ return -EINVAL;
+ mode = convert_mode(&msgtyp,msgflg);
+
+ msq = msg_lock(msqid);
+ if(msq==NULL)
+ return -EINVAL;
+
+ msg = ERR_PTR(-EIDRM);
+ if (msg_checkid(msq,msqid))
+ goto out_unlock;
+
+ for (;;) {
+ struct msg_receiver msr_d;
+ struct list_head* tmp;
+
+ msg = ERR_PTR(-EACCES);
+ if (ipcperms (&msq->q_perm, S_IRUGO))
+ goto out_unlock;
+
+ msg = ERR_PTR(-EAGAIN);
+ tmp = msq->q_messages.next;
+ while (tmp != &msq->q_messages) {
+ struct msg_msg *walk_msg;
+ walk_msg = list_entry(tmp,struct msg_msg,m_list);
+ if(testmsg(walk_msg,msgtyp,mode) &&
+ !security_msg_queue_msgrcv(msq, walk_msg, current, msgtyp, mode)) {
+ msg = walk_msg;
+ if(mode == SEARCH_LESSEQUAL && walk_msg->m_type != 1) {
+ msg=walk_msg;
+ msgtyp=walk_msg->m_type-1;
+ } else {
+ msg=walk_msg;
+ break;
+ }
+ }
+ tmp = tmp->next;
+ }
+ if(!IS_ERR(msg)) {
+ /* Found a suitable message. Unlink it from the queue. */
+ if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
+ msg = ERR_PTR(-E2BIG);
+ goto out_unlock;
+ }
+ list_del(&msg->m_list);
+ msq->q_qnum--;
+ msq->q_rtime = get_seconds();
+ msq->q_lrpid = current->tgid;
+ msq->q_cbytes -= msg->m_ts;
+ atomic_sub(msg->m_ts,&msg_bytes);
+ atomic_dec(&msg_hdrs);
+ ss_wakeup(&msq->q_senders,0);
+ msg_unlock(msq);
+ break;
+ }
+ /* No message waiting. Wait for a message */
+ if (msgflg & IPC_NOWAIT) {
+ msg = ERR_PTR(-ENOMSG);
+ goto out_unlock;
+ }
+ list_add_tail(&msr_d.r_list,&msq->q_receivers);
+ msr_d.r_tsk = current;
+ msr_d.r_msgtype = msgtyp;
+ msr_d.r_mode = mode;
+ if(msgflg & MSG_NOERROR)
+ msr_d.r_maxsize = INT_MAX;
+ else
+ msr_d.r_maxsize = msgsz;
+ msr_d.r_msg = ERR_PTR(-EAGAIN);
+ current->state = TASK_INTERRUPTIBLE;
+ msg_unlock(msq);
+
+ schedule();
+
+ /* Lockless receive, part 1:
+ * Disable preemption. We don't hold a reference to the queue
+ * and getting a reference would defeat the idea of a lockless
+ * operation, thus the code relies on rcu to guarantee the
+ * existance of msq:
+ * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
+ * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
+ * rcu_read_lock() prevents preemption between reading r_msg
+ * and the spin_lock() inside ipc_lock_by_ptr().
+ */
+ rcu_read_lock();
+
+ /* Lockless receive, part 2:
+ * Wait until pipelined_send or expunge_all are outside of
+ * wake_up_process(). There is a race with exit(), see
+ * ipc/mqueue.c for the details.
+ */
+ msg = (struct msg_msg*) msr_d.r_msg;
+ while (msg == NULL) {
+ cpu_relax();
+ msg = (struct msg_msg*) msr_d.r_msg;
+ }
+
+ /* Lockless receive, part 3:
+ * If there is a message or an error then accept it without
+ * locking.
+ */
+ if(msg != ERR_PTR(-EAGAIN)) {
+ rcu_read_unlock();
+ break;
+ }
+
+ /* Lockless receive, part 3:
+ * Acquire the queue spinlock.
+ */
+ ipc_lock_by_ptr(&msq->q_perm);
+ rcu_read_unlock();
+
+ /* Lockless receive, part 4:
+ * Repeat test after acquiring the spinlock.
+ */
+ msg = (struct msg_msg*)msr_d.r_msg;
+ if(msg != ERR_PTR(-EAGAIN))
+ goto out_unlock;
+
+ list_del(&msr_d.r_list);
+ if (signal_pending(current)) {
+ msg = ERR_PTR(-ERESTARTNOHAND);
+out_unlock:
+ msg_unlock(msq);
+ break;
+ }
+ }
+ if (IS_ERR(msg))
+ return PTR_ERR(msg);
+
+ msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
+ if (put_user (msg->m_type, &msgp->mtype) ||
+ store_msg(msgp->mtext, msg, msgsz)) {
+ msgsz = -EFAULT;
+ }
+ free_msg(msg);
+ return msgsz;
+}
+
+#ifdef CONFIG_PROC_FS
+static int sysvipc_msg_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
+{
+ off_t pos = 0;
+ off_t begin = 0;
+ int i, len = 0;
+
+ down(&msg_ids.sem);
+ len += sprintf(buffer, " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n");
+
+ for(i = 0; i <= msg_ids.max_id; i++) {
+ struct msg_queue * msq;
+ msq = msg_lock(i);
+ if(msq != NULL) {
+ len += sprintf(buffer + len, "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
+ msq->q_perm.key,
+ msg_buildid(i,msq->q_perm.seq),
+ msq->q_perm.mode,
+ msq->q_cbytes,
+ msq->q_qnum,
+ msq->q_lspid,
+ msq->q_lrpid,
+ msq->q_perm.uid,
+ msq->q_perm.gid,
+ msq->q_perm.cuid,
+ msq->q_perm.cgid,
+ msq->q_stime,
+ msq->q_rtime,
+ msq->q_ctime);
+ msg_unlock(msq);
+
+ pos += len;
+ if(pos < offset) {
+ len = 0;
+ begin = pos;
+ }
+ if(pos > offset + length)
+ goto done;
+ }
+
+ }
+ *eof = 1;
+done:
+ up(&msg_ids.sem);
+ *start = buffer + (offset - begin);
+ len -= (offset - begin);
+ if(len > length)
+ len = length;
+ if(len < 0)
+ len = 0;
+ return len;
+}
+#endif
diff --git a/ipc/msgutil.c b/ipc/msgutil.c
new file mode 100644
index 00000000000..66cfb87646e
--- /dev/null
+++ b/ipc/msgutil.c
@@ -0,0 +1,127 @@
+/*
+ * linux/ipc/util.c
+ * Copyright (C) 1999, 2004 Manfred Spraul
+ *
+ * This file is released under GNU General Public Licence version 2 or
+ * (at your option) any later version.
+ *
+ * See the file COPYING for more details.
+ */
+
+#include <linux/spinlock.h>
+#include <linux/init.h>
+#include <linux/security.h>
+#include <linux/slab.h>
+#include <linux/ipc.h>
+#include <asm/uaccess.h>
+
+#include "util.h"
+
+struct msg_msgseg {
+ struct msg_msgseg* next;
+ /* the next part of the message follows immediately */
+};
+
+#define DATALEN_MSG (PAGE_SIZE-sizeof(struct msg_msg))
+#define DATALEN_SEG (PAGE_SIZE-sizeof(struct msg_msgseg))
+
+struct msg_msg *load_msg(const void __user *src, int len)
+{
+ struct msg_msg *msg;
+ struct msg_msgseg **pseg;
+ int err;
+ int alen;
+
+ alen = len;
+ if (alen > DATALEN_MSG)
+ alen = DATALEN_MSG;
+
+ msg = (struct msg_msg *)kmalloc(sizeof(*msg) + alen, GFP_KERNEL);
+ if (msg == NULL)
+ return ERR_PTR(-ENOMEM);
+
+ msg->next = NULL;
+ msg->security = NULL;
+
+ if (copy_from_user(msg + 1, src, alen)) {
+ err = -EFAULT;
+ goto out_err;
+ }
+
+ len -= alen;
+ src = ((char __user *)src) + alen;
+ pseg = &msg->next;
+ while (len > 0) {
+ struct msg_msgseg *seg;
+ alen = len;
+ if (alen > DATALEN_SEG)
+ alen = DATALEN_SEG;
+ seg = (struct msg_msgseg *)kmalloc(sizeof(*seg) + alen,
+ GFP_KERNEL);
+ if (seg == NULL) {
+ err = -ENOMEM;
+ goto out_err;
+ }
+ *pseg = seg;
+ seg->next = NULL;
+ if (copy_from_user(seg + 1, src, alen)) {
+ err = -EFAULT;
+ goto out_err;
+ }
+ pseg = &seg->next;
+ len -= alen;
+ src = ((char __user *)src) + alen;
+ }
+
+ err = security_msg_msg_alloc(msg);
+ if (err)
+ goto out_err;
+
+ return msg;
+
+out_err:
+ free_msg(msg);
+ return ERR_PTR(err);
+}
+
+int store_msg(void __user *dest, struct msg_msg *msg, int len)
+{
+ int alen;
+ struct msg_msgseg *seg;
+
+ alen = len;
+ if (alen > DATALEN_MSG)
+ alen = DATALEN_MSG;
+ if (copy_to_user(dest, msg + 1, alen))
+ return -1;
+
+ len -= alen;
+ dest = ((char __user *)dest) + alen;
+ seg = msg->next;
+ while (len > 0) {
+ alen = len;
+ if (alen > DATALEN_SEG)
+ alen = DATALEN_SEG;
+ if (copy_to_user(dest, seg + 1, alen))
+ return -1;
+ len -= alen;
+ dest = ((char __user *)dest) + alen;
+ seg = seg->next;
+ }
+ return 0;
+}
+
+void free_msg(struct msg_msg *msg)
+{
+ struct msg_msgseg *seg;
+
+ security_msg_msg_free(msg);
+
+ seg = msg->next;
+ kfree(msg);
+ while (seg != NULL) {
+ struct msg_msgseg *tmp = seg->next;
+ kfree(seg);
+ seg = tmp;
+ }
+}
diff --git a/ipc/sem.c b/ipc/sem.c
new file mode 100644
index 00000000000..5ad7ac0ed60
--- /dev/null
+++ b/ipc/sem.c
@@ -0,0 +1,1384 @@
+/*
+ * linux/ipc/sem.c
+ * Copyright (C) 1992 Krishna Balasubramanian
+ * Copyright (C) 1995 Eric Schenk, Bruno Haible
+ *
+ * IMPLEMENTATION NOTES ON CODE REWRITE (Eric Schenk, January 1995):
+ * This code underwent a massive rewrite in order to solve some problems
+ * with the original code. In particular the original code failed to
+ * wake up processes that were waiting for semval to go to 0 if the
+ * value went to 0 and was then incremented rapidly enough. In solving
+ * this problem I have also modified the implementation so that it
+ * processes pending operations in a FIFO manner, thus give a guarantee
+ * that processes waiting for a lock on the semaphore won't starve
+ * unless another locking process fails to unlock.
+ * In addition the following two changes in behavior have been introduced:
+ * - The original implementation of semop returned the value
+ * last semaphore element examined on success. This does not
+ * match the manual page specifications, and effectively
+ * allows the user to read the semaphore even if they do not
+ * have read permissions. The implementation now returns 0
+ * on success as stated in the manual page.
+ * - There is some confusion over whether the set of undo adjustments
+ * to be performed at exit should be done in an atomic manner.
+ * That is, if we are attempting to decrement the semval should we queue
+ * up and wait until we can do so legally?
+ * The original implementation attempted to do this.
+ * The current implementation does not do so. This is because I don't
+ * think it is the right thing (TM) to do, and because I couldn't
+ * see a clean way to get the old behavior with the new design.
+ * The POSIX standard and SVID should be consulted to determine
+ * what behavior is mandated.
+ *
+ * Further notes on refinement (Christoph Rohland, December 1998):
+ * - The POSIX standard says, that the undo adjustments simply should
+ * redo. So the current implementation is o.K.
+ * - The previous code had two flaws:
+ * 1) It actively gave the semaphore to the next waiting process
+ * sleeping on the semaphore. Since this process did not have the
+ * cpu this led to many unnecessary context switches and bad
+ * performance. Now we only check which process should be able to
+ * get the semaphore and if this process wants to reduce some
+ * semaphore value we simply wake it up without doing the
+ * operation. So it has to try to get it later. Thus e.g. the
+ * running process may reacquire the semaphore during the current
+ * time slice. If it only waits for zero or increases the semaphore,
+ * we do the operation in advance and wake it up.
+ * 2) It did not wake up all zero waiting processes. We try to do
+ * better but only get the semops right which only wait for zero or
+ * increase. If there are decrement operations in the operations
+ * array we do the same as before.
+ *
+ * With the incarnation of O(1) scheduler, it becomes unnecessary to perform
+ * check/retry algorithm for waking up blocked processes as the new scheduler
+ * is better at handling thread switch than the old one.
+ *
+ * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
+ *
+ * SMP-threaded, sysctl's added
+ * (c) 1999 Manfred Spraul <manfreds@colorfullife.com>
+ * Enforced range limit on SEM_UNDO
+ * (c) 2001 Red Hat Inc <alan@redhat.com>
+ * Lockless wakeup
+ * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
+ */
+
+#include <linux/config.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/init.h>
+#include <linux/proc_fs.h>
+#include <linux/time.h>
+#include <linux/smp_lock.h>
+#include <linux/security.h>
+#include <linux/syscalls.h>
+#include <linux/audit.h>
+#include <asm/uaccess.h>
+#include "util.h"
+
+
+#define sem_lock(id) ((struct sem_array*)ipc_lock(&sem_ids,id))
+#define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
+#define sem_rmid(id) ((struct sem_array*)ipc_rmid(&sem_ids,id))
+#define sem_checkid(sma, semid) \
+ ipc_checkid(&sem_ids,&sma->sem_perm,semid)
+#define sem_buildid(id, seq) \
+ ipc_buildid(&sem_ids, id, seq)
+static struct ipc_ids sem_ids;
+
+static int newary (key_t, int, int);
+static void freeary (struct sem_array *sma, int id);
+#ifdef CONFIG_PROC_FS
+static int sysvipc_sem_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
+#endif
+
+#define SEMMSL_FAST 256 /* 512 bytes on stack */
+#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
+
+/*
+ * linked list protection:
+ * sem_undo.id_next,
+ * sem_array.sem_pending{,last},
+ * sem_array.sem_undo: sem_lock() for read/write
+ * sem_undo.proc_next: only "current" is allowed to read/write that field.
+ *
+ */
+
+int sem_ctls[4] = {SEMMSL, SEMMNS, SEMOPM, SEMMNI};
+#define sc_semmsl (sem_ctls[0])
+#define sc_semmns (sem_ctls[1])
+#define sc_semopm (sem_ctls[2])
+#define sc_semmni (sem_ctls[3])
+
+static int used_sems;
+
+void __init sem_init (void)
+{
+ used_sems = 0;
+ ipc_init_ids(&sem_ids,sc_semmni);
+
+#ifdef CONFIG_PROC_FS
+ create_proc_read_entry("sysvipc/sem", 0, NULL, sysvipc_sem_read_proc, NULL);
+#endif
+}
+
+/*
+ * Lockless wakeup algorithm:
+ * Without the check/retry algorithm a lockless wakeup is possible:
+ * - queue.status is initialized to -EINTR before blocking.
+ * - wakeup is performed by
+ * * unlinking the queue entry from sma->sem_pending
+ * * setting queue.status to IN_WAKEUP
+ * This is the notification for the blocked thread that a
+ * result value is imminent.
+ * * call wake_up_process
+ * * set queue.status to the final value.
+ * - the previously blocked thread checks queue.status:
+ * * 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 semaphore array.
+ * * otherwise it must acquire the spinlock and check what's up.
+ *
+ * The two-stage algorithm is necessary to protect against the following
+ * races:
+ * - if queue.status is set after wake_up_process, then the woken up idle
+ * thread could race forward and try (and fail) to acquire sma->lock
+ * before update_queue had a chance to set queue.status
+ * - if queue.status is written before wake_up_process and if the
+ * blocked process is woken up by a signal between writing
+ * queue.status and the wake_up_process, then the woken up
+ * process could return from semtimedop and die by calling
+ * sys_exit before wake_up_process is called. Then wake_up_process
+ * will oops, because the task structure is already invalid.
+ * (yes, this happened on s390 with sysv msg).
+ *
+ */
+#define IN_WAKEUP 1
+
+static int newary (key_t key, int nsems, int semflg)
+{
+ int id;
+ int retval;
+ struct sem_array *sma;
+ int size;
+
+ if (!nsems)
+ return -EINVAL;
+ if (used_sems + nsems > sc_semmns)
+ return -ENOSPC;
+
+ size = sizeof (*sma) + nsems * sizeof (struct sem);
+ sma = ipc_rcu_alloc(size);
+ if (!sma) {
+ return -ENOMEM;
+ }
+ memset (sma, 0, size);
+
+ sma->sem_perm.mode = (semflg & S_IRWXUGO);
+ sma->sem_perm.key = key;
+
+ sma->sem_perm.security = NULL;
+ retval = security_sem_alloc(sma);
+ if (retval) {
+ ipc_rcu_putref(sma);
+ return retval;
+ }
+
+ id = ipc_addid(&sem_ids, &sma->sem_perm, sc_semmni);
+ if(id == -1) {
+ security_sem_free(sma);
+ ipc_rcu_putref(sma);
+ return -ENOSPC;
+ }
+ used_sems += nsems;
+
+ sma->sem_base = (struct sem *) &sma[1];
+ /* sma->sem_pending = NULL; */
+ sma->sem_pending_last = &sma->sem_pending;
+ /* sma->undo = NULL; */
+ sma->sem_nsems = nsems;
+ sma->sem_ctime = get_seconds();
+ sem_unlock(sma);
+
+ return sem_buildid(id, sma->sem_perm.seq);
+}
+
+asmlinkage long sys_semget (key_t key, int nsems, int semflg)
+{
+ int id, err = -EINVAL;
+ struct sem_array *sma;
+
+ if (nsems < 0 || nsems > sc_semmsl)
+ return -EINVAL;
+ down(&sem_ids.sem);
+
+ if (key == IPC_PRIVATE) {
+ err = newary(key, nsems, semflg);
+ } else if ((id = ipc_findkey(&sem_ids, key)) == -1) { /* key not used */
+ if (!(semflg & IPC_CREAT))
+ err = -ENOENT;
+ else
+ err = newary(key, nsems, semflg);
+ } else if (semflg & IPC_CREAT && semflg & IPC_EXCL) {
+ err = -EEXIST;
+ } else {
+ sma = sem_lock(id);
+ if(sma==NULL)
+ BUG();
+ if (nsems > sma->sem_nsems)
+ err = -EINVAL;
+ else if (ipcperms(&sma->sem_perm, semflg))
+ err = -EACCES;
+ else {
+ int semid = sem_buildid(id, sma->sem_perm.seq);
+ err = security_sem_associate(sma, semflg);
+ if (!err)
+ err = semid;
+ }
+ sem_unlock(sma);
+ }
+
+ up(&sem_ids.sem);
+ return err;
+}
+
+/* Manage the doubly linked list sma->sem_pending as a FIFO:
+ * insert new queue elements at the tail sma->sem_pending_last.
+ */
+static inline void append_to_queue (struct sem_array * sma,
+ struct sem_queue * q)
+{
+ *(q->prev = sma->sem_pending_last) = q;
+ *(sma->sem_pending_last = &q->next) = NULL;
+}
+
+static inline void prepend_to_queue (struct sem_array * sma,
+ struct sem_queue * q)
+{
+ q->next = sma->sem_pending;
+ *(q->prev = &sma->sem_pending) = q;
+ if (q->next)
+ q->next->prev = &q->next;
+ else /* sma->sem_pending_last == &sma->sem_pending */
+ sma->sem_pending_last = &q->next;
+}
+
+static inline void remove_from_queue (struct sem_array * sma,
+ struct sem_queue * q)
+{
+ *(q->prev) = q->next;
+ if (q->next)
+ q->next->prev = q->prev;
+ else /* sma->sem_pending_last == &q->next */
+ sma->sem_pending_last = q->prev;
+ q->prev = NULL; /* mark as removed */
+}
+
+/*
+ * Determine whether a sequence of semaphore operations would succeed
+ * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
+ */
+
+static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
+ int nsops, struct sem_undo *un, int pid)
+{
+ int result, sem_op;
+ struct sembuf *sop;
+ struct sem * curr;
+
+ for (sop = sops; sop < sops + nsops; sop++) {
+ curr = sma->sem_base + sop->sem_num;
+ sem_op = sop->sem_op;
+ result = curr->semval;
+
+ if (!sem_op && result)
+ goto would_block;
+
+ result += sem_op;
+ if (result < 0)
+ goto would_block;
+ if (result > SEMVMX)
+ goto out_of_range;
+ if (sop->sem_flg & SEM_UNDO) {
+ int undo = un->semadj[sop->sem_num] - sem_op;
+ /*
+ * Exceeding the undo range is an error.
+ */
+ if (undo < (-SEMAEM - 1) || undo > SEMAEM)
+ goto out_of_range;
+ }
+ curr->semval = result;
+ }
+
+ sop--;
+ while (sop >= sops) {
+ sma->sem_base[sop->sem_num].sempid = pid;
+ if (sop->sem_flg & SEM_UNDO)
+ un->semadj[sop->sem_num] -= sop->sem_op;
+ sop--;
+ }
+
+ sma->sem_otime = get_seconds();
+ return 0;
+
+out_of_range:
+ result = -ERANGE;
+ goto undo;
+
+would_block:
+ if (sop->sem_flg & IPC_NOWAIT)
+ result = -EAGAIN;
+ else
+ result = 1;
+
+undo:
+ sop--;
+ while (sop >= sops) {
+ sma->sem_base[sop->sem_num].semval -= sop->sem_op;
+ sop--;
+ }
+
+ return result;
+}
+
+/* Go through the pending queue for the indicated semaphore
+ * looking for tasks that can be completed.
+ */
+static void update_queue (struct sem_array * sma)
+{
+ int error;
+ struct sem_queue * q;
+
+ q = sma->sem_pending;
+ while(q) {
+ error = try_atomic_semop(sma, q->sops, q->nsops,
+ q->undo, q->pid);
+
+ /* Does q->sleeper still need to sleep? */
+ if (error <= 0) {
+ struct sem_queue *n;
+ remove_from_queue(sma,q);
+ q->status = IN_WAKEUP;
+ /*
+ * Continue scanning. The next operation
+ * that must be checked depends on the type of the
+ * completed operation:
+ * - if the operation modified the array, then
+ * restart from the head of the queue and
+ * check for threads that might be waiting
+ * for semaphore values to become 0.
+ * - if the operation didn't modify the array,
+ * then just continue.
+ */
+ if (q->alter)
+ n = sma->sem_pending;
+ else
+ n = q->next;
+ wake_up_process(q->sleeper);
+ /* hands-off: q will disappear immediately after
+ * writing q->status.
+ */
+ q->status = error;
+ q = n;
+ } else {
+ q = q->next;
+ }
+ }
+}
+
+/* The following counts are associated to each semaphore:
+ * semncnt number of tasks waiting on semval being nonzero
+ * semzcnt number of tasks waiting on semval being zero
+ * This model assumes that a task waits on exactly one semaphore.
+ * Since semaphore operations are to be performed atomically, tasks actually
+ * wait on a whole sequence of semaphores simultaneously.
+ * The counts we return here are a rough approximation, but still
+ * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
+ */
+static int count_semncnt (struct sem_array * sma, ushort semnum)
+{
+ int semncnt;
+ struct sem_queue * q;
+
+ semncnt = 0;
+ for (q = sma->sem_pending; q; q = q->next) {
+ struct sembuf * sops = q->sops;
+ int nsops = q->nsops;
+ int i;
+ for (i = 0; i < nsops; i++)
+ if (sops[i].sem_num == semnum
+ && (sops[i].sem_op < 0)
+ && !(sops[i].sem_flg & IPC_NOWAIT))
+ semncnt++;
+ }
+ return semncnt;
+}
+static int count_semzcnt (struct sem_array * sma, ushort semnum)
+{
+ int semzcnt;
+ struct sem_queue * q;
+
+ semzcnt = 0;
+ for (q = sma->sem_pending; q; q = q->next) {
+ struct sembuf * sops = q->sops;
+ int nsops = q->nsops;
+ int i;
+ for (i = 0; i < nsops; i++)
+ if (sops[i].sem_num == semnum
+ && (sops[i].sem_op == 0)
+ && !(sops[i].sem_flg & IPC_NOWAIT))
+ semzcnt++;
+ }
+ return semzcnt;
+}
+
+/* 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)
+{
+ struct sem_undo *un;
+ struct sem_queue *q;
+ int size;
+
+ /* Invalidate the existing undo structures for this semaphore set.
+ * (They will be freed without any further action in exit_sem()
+ * or during the next semop.)
+ */
+ for (un = sma->undo; un; un = un->id_next)
+ un->semid = -1;
+
+ /* Wake up all pending processes and let them fail with EIDRM. */
+ q = sma->sem_pending;
+ while(q) {
+ struct sem_queue *n;
+ /* lazy remove_from_queue: we are killing the whole queue */
+ q->prev = NULL;
+ n = q->next;
+ q->status = IN_WAKEUP;
+ wake_up_process(q->sleeper); /* doesn't sleep */
+ q->status = -EIDRM; /* hands-off q */
+ q = n;
+ }
+
+ /* Remove the semaphore set from the ID array*/
+ sma = sem_rmid(id);
+ sem_unlock(sma);
+
+ used_sems -= sma->sem_nsems;
+ size = sizeof (*sma) + sma->sem_nsems * sizeof (struct sem);
+ security_sem_free(sma);
+ ipc_rcu_putref(sma);
+}
+
+static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
+{
+ switch(version) {
+ case IPC_64:
+ return copy_to_user(buf, in, sizeof(*in));
+ case IPC_OLD:
+ {
+ struct semid_ds out;
+
+ ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
+
+ out.sem_otime = in->sem_otime;
+ out.sem_ctime = in->sem_ctime;
+ out.sem_nsems = in->sem_nsems;
+
+ return copy_to_user(buf, &out, sizeof(out));
+ }
+ default:
+ return -EINVAL;
+ }
+}
+
+static int semctl_nolock(int semid, int semnum, int cmd, int version, union semun arg)
+{
+ int err = -EINVAL;
+ struct sem_array *sma;
+
+ switch(cmd) {
+ case IPC_INFO:
+ case SEM_INFO:
+ {
+ struct seminfo seminfo;
+ int max_id;
+
+ err = security_sem_semctl(NULL, cmd);
+ if (err)
+ return err;
+
+ memset(&seminfo,0,sizeof(seminfo));
+ seminfo.semmni = sc_semmni;
+ seminfo.semmns = sc_semmns;
+ seminfo.semmsl = sc_semmsl;
+ seminfo.semopm = sc_semopm;
+ seminfo.semvmx = SEMVMX;
+ seminfo.semmnu = SEMMNU;
+ seminfo.semmap = SEMMAP;
+ seminfo.semume = SEMUME;
+ down(&sem_ids.sem);
+ if (cmd == SEM_INFO) {
+ seminfo.semusz = sem_ids.in_use;
+ seminfo.semaem = used_sems;
+ } else {
+ seminfo.semusz = SEMUSZ;
+ seminfo.semaem = SEMAEM;
+ }
+ max_id = sem_ids.max_id;
+ up(&sem_ids.sem);
+ if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
+ return -EFAULT;
+ return (max_id < 0) ? 0: max_id;
+ }
+ case SEM_STAT:
+ {
+ struct semid64_ds tbuf;
+ int id;
+
+ if(semid >= sem_ids.entries->size)
+ return -EINVAL;
+
+ memset(&tbuf,0,sizeof(tbuf));
+
+ sma = sem_lock(semid);
+ if(sma == NULL)
+ return -EINVAL;
+
+ err = -EACCES;
+ if (ipcperms (&sma->sem_perm, S_IRUGO))
+ goto out_unlock;
+
+ err = security_sem_semctl(sma, cmd);
+ if (err)
+ goto out_unlock;
+
+ id = sem_buildid(semid, sma->sem_perm.seq);
+
+ 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 id;
+ }
+ default:
+ return -EINVAL;
+ }
+ return err;
+out_unlock:
+ sem_unlock(sma);
+ return err;
+}
+
+static int semctl_main(int semid, int semnum, int cmd, int version, union semun arg)
+{
+ struct sem_array *sma;
+ struct sem* curr;
+ int err;
+ ushort fast_sem_io[SEMMSL_FAST];
+ ushort* sem_io = fast_sem_io;
+ int nsems;
+
+ sma = sem_lock(semid);
+ if(sma==NULL)
+ return -EINVAL;
+
+ nsems = sma->sem_nsems;
+
+ err=-EIDRM;
+ if (sem_checkid(sma,semid))
+ goto out_unlock;
+
+ err = -EACCES;
+ if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
+ goto out_unlock;
+
+ err = security_sem_semctl(sma, cmd);
+ if (err)
+ goto out_unlock;
+
+ err = -EACCES;
+ switch (cmd) {
+ case GETALL:
+ {
+ ushort __user *array = arg.array;
+ int i;
+
+ if(nsems > SEMMSL_FAST) {
+ ipc_rcu_getref(sma);
+ sem_unlock(sma);
+
+ sem_io = ipc_alloc(sizeof(ushort)*nsems);
+ if(sem_io == NULL) {
+ ipc_lock_by_ptr(&sma->sem_perm);
+ ipc_rcu_putref(sma);
+ sem_unlock(sma);
+ return -ENOMEM;
+ }
+
+ ipc_lock_by_ptr(&sma->sem_perm);
+ ipc_rcu_putref(sma);
+ if (sma->sem_perm.deleted) {
+ sem_unlock(sma);
+ err = -EIDRM;
+ goto out_free;
+ }
+ }
+
+ for (i = 0; i < sma->sem_nsems; i++)
+ sem_io[i] = sma->sem_base[i].semval;
+ sem_unlock(sma);
+ err = 0;
+ if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
+ err = -EFAULT;
+ goto out_free;
+ }
+ case SETALL:
+ {
+ int i;
+ struct sem_undo *un;
+
+ ipc_rcu_getref(sma);
+ sem_unlock(sma);
+
+ if(nsems > SEMMSL_FAST) {
+ sem_io = ipc_alloc(sizeof(ushort)*nsems);
+ if(sem_io == NULL) {
+ ipc_lock_by_ptr(&sma->sem_perm);
+ ipc_rcu_putref(sma);
+ sem_unlock(sma);
+ return -ENOMEM;
+ }
+ }
+
+ if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
+ ipc_lock_by_ptr(&sma->sem_perm);
+ ipc_rcu_putref(sma);
+ sem_unlock(sma);
+ err = -EFAULT;
+ goto out_free;
+ }
+
+ for (i = 0; i < nsems; i++) {
+ if (sem_io[i] > SEMVMX) {
+ ipc_lock_by_ptr(&sma->sem_perm);
+ ipc_rcu_putref(sma);
+ sem_unlock(sma);
+ err = -ERANGE;
+ goto out_free;
+ }
+ }
+ ipc_lock_by_ptr(&sma->sem_perm);
+ ipc_rcu_putref(sma);
+ if (sma->sem_perm.deleted) {
+ sem_unlock(sma);
+ err = -EIDRM;
+ goto out_free;
+ }
+
+ for (i = 0; i < nsems; i++)
+ sma->sem_base[i].semval = sem_io[i];
+ for (un = sma->undo; un; un = un->id_next)
+ for (i = 0; i < nsems; i++)
+ un->semadj[i] = 0;
+ sma->sem_ctime = get_seconds();
+ /* maybe some queued-up processes were waiting for this */
+ update_queue(sma);
+ 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;
+ if(semnum < 0 || semnum >= nsems)
+ goto out_unlock;
+
+ curr = &sma->sem_base[semnum];
+
+ switch (cmd) {
+ case GETVAL:
+ err = curr->semval;
+ goto out_unlock;
+ case GETPID:
+ err = curr->sempid;
+ goto out_unlock;
+ case GETNCNT:
+ err = count_semncnt(sma,semnum);
+ goto out_unlock;
+ case GETZCNT:
+ err = count_semzcnt(sma,semnum);
+ goto out_unlock;
+ case SETVAL:
+ {
+ int val = arg.val;
+ struct sem_undo *un;
+ err = -ERANGE;
+ if (val > SEMVMX || val < 0)
+ goto out_unlock;
+
+ for (un = sma->undo; un; un = un->id_next)
+ un->semadj[semnum] = 0;
+ curr->semval = val;
+ curr->sempid = current->tgid;
+ sma->sem_ctime = get_seconds();
+ /* maybe some queued-up processes were waiting for this */
+ update_queue(sma);
+ err = 0;
+ goto out_unlock;
+ }
+ }
+out_unlock:
+ sem_unlock(sma);
+out_free:
+ if(sem_io != fast_sem_io)
+ ipc_free(sem_io, sizeof(ushort)*nsems);
+ return err;
+}
+
+struct sem_setbuf {
+ uid_t uid;
+ gid_t gid;
+ mode_t mode;
+};
+
+static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
+{
+ switch(version) {
+ case IPC_64:
+ {
+ struct semid64_ds tbuf;
+
+ if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
+ return -EFAULT;
+
+ out->uid = tbuf.sem_perm.uid;
+ out->gid = tbuf.sem_perm.gid;
+ out->mode = tbuf.sem_perm.mode;
+
+ return 0;
+ }
+ case IPC_OLD:
+ {
+ struct semid_ds tbuf_old;
+
+ if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
+ return -EFAULT;
+
+ out->uid = tbuf_old.sem_perm.uid;
+ out->gid = tbuf_old.sem_perm.gid;
+ out->mode = tbuf_old.sem_perm.mode;
+
+ return 0;
+ }
+ default:
+ return -EINVAL;
+ }
+}
+
+static int semctl_down(int semid, int semnum, int cmd, int version, union semun arg)
+{
+ struct sem_array *sma;
+ int err;
+ struct sem_setbuf setbuf;
+ struct kern_ipc_perm *ipcp;
+
+ 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)
+ return -EINVAL;
+
+ if (sem_checkid(sma,semid)) {
+ err=-EIDRM;
+ goto out_unlock;
+ }
+ ipcp = &sma->sem_perm;
+
+ if (current->euid != ipcp->cuid &&
+ current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
+ err=-EPERM;
+ goto out_unlock;
+ }
+
+ err = security_sem_semctl(sma, cmd);
+ if (err)
+ goto out_unlock;
+
+ switch(cmd){
+ case IPC_RMID:
+ freeary(sma, semid);
+ err = 0;
+ break;
+ case IPC_SET:
+ ipcp->uid = setbuf.uid;
+ ipcp->gid = setbuf.gid;
+ ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
+ | (setbuf.mode & S_IRWXUGO);
+ sma->sem_ctime = get_seconds();
+ sem_unlock(sma);
+ err = 0;
+ break;
+ default:
+ sem_unlock(sma);
+ err = -EINVAL;
+ break;
+ }
+ return err;
+
+out_unlock:
+ sem_unlock(sma);
+ return err;
+}
+
+asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
+{
+ int err = -EINVAL;
+ int version;
+
+ if (semid < 0)
+ return -EINVAL;
+
+ version = ipc_parse_version(&cmd);
+
+ switch(cmd) {
+ case IPC_INFO:
+ case SEM_INFO:
+ case SEM_STAT:
+ err = semctl_nolock(semid,semnum,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(semid,semnum,cmd,version,arg);
+ return err;
+ case IPC_RMID:
+ case IPC_SET:
+ down(&sem_ids.sem);
+ err = semctl_down(semid,semnum,cmd,version,arg);
+ up(&sem_ids.sem);
+ return err;
+ default:
+ return -EINVAL;
+ }
+}
+
+static inline void lock_semundo(void)
+{
+ struct sem_undo_list *undo_list;
+
+ undo_list = current->sysvsem.undo_list;
+ if ((undo_list != NULL) && (atomic_read(&undo_list->refcnt) != 1))
+ spin_lock(&undo_list->lock);
+}
+
+/* This code has an interaction with copy_semundo().
+ * Consider; two tasks are sharing the undo_list. task1
+ * acquires the undo_list lock in lock_semundo(). If task2 now
+ * exits before task1 releases the lock (by calling
+ * unlock_semundo()), then task1 will never call spin_unlock().
+ * This leave the sem_undo_list in a locked state. If task1 now creats task3
+ * and once again shares the sem_undo_list, the sem_undo_list will still be
+ * locked, and future SEM_UNDO operations will deadlock. This case is
+ * dealt with in copy_semundo() by having it reinitialize the spin lock when
+ * the refcnt goes from 1 to 2.
+ */
+static inline void unlock_semundo(void)
+{
+ struct sem_undo_list *undo_list;
+
+ undo_list = current->sysvsem.undo_list;
+ if ((undo_list != NULL) && (atomic_read(&undo_list->refcnt) != 1))
+ spin_unlock(&undo_list->lock);
+}
+
+
+/* If the task doesn't already have a undo_list, then allocate one
+ * here. We guarantee there is only one thread using this undo list,
+ * and current is THE ONE
+ *
+ * If this allocation and assignment succeeds, but later
+ * portions of this code fail, there is no need to free the sem_undo_list.
+ * Just let it stay associated with the task, and it'll be freed later
+ * at exit time.
+ *
+ * This can block, so callers must hold no locks.
+ */
+static inline int get_undo_list(struct sem_undo_list **undo_listp)
+{
+ struct sem_undo_list *undo_list;
+ int size;
+
+ undo_list = current->sysvsem.undo_list;
+ if (!undo_list) {
+ size = sizeof(struct sem_undo_list);
+ undo_list = (struct sem_undo_list *) kmalloc(size, GFP_KERNEL);
+ if (undo_list == NULL)
+ return -ENOMEM;
+ memset(undo_list, 0, size);
+ /* don't initialize unodhd->lock here. It's done
+ * in copy_semundo() instead.
+ */
+ atomic_set(&undo_list->refcnt, 1);
+ current->sysvsem.undo_list = undo_list;
+ }
+ *undo_listp = undo_list;
+ return 0;
+}
+
+static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
+{
+ struct sem_undo **last, *un;
+
+ last = &ulp->proc_list;
+ un = *last;
+ while(un != NULL) {
+ if(un->semid==semid)
+ break;
+ if(un->semid==-1) {
+ *last=un->proc_next;
+ kfree(un);
+ } else {
+ last=&un->proc_next;
+ }
+ un=*last;
+ }
+ return un;
+}
+
+static struct sem_undo *find_undo(int semid)
+{
+ struct sem_array *sma;
+ struct sem_undo_list *ulp;
+ struct sem_undo *un, *new;
+ int nsems;
+ int error;
+
+ error = get_undo_list(&ulp);
+ if (error)
+ return ERR_PTR(error);
+
+ lock_semundo();
+ un = lookup_undo(ulp, semid);
+ unlock_semundo();
+ if (likely(un!=NULL))
+ goto out;
+
+ /* no undo structure around - allocate one. */
+ sma = sem_lock(semid);
+ un = ERR_PTR(-EINVAL);
+ if(sma==NULL)
+ goto out;
+ un = ERR_PTR(-EIDRM);
+ if (sem_checkid(sma,semid)) {
+ sem_unlock(sma);
+ goto out;
+ }
+ nsems = sma->sem_nsems;
+ ipc_rcu_getref(sma);
+ sem_unlock(sma);
+
+ new = (struct sem_undo *) kmalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
+ if (!new) {
+ ipc_lock_by_ptr(&sma->sem_perm);
+ ipc_rcu_putref(sma);
+ sem_unlock(sma);
+ return ERR_PTR(-ENOMEM);
+ }
+ memset(new, 0, sizeof(struct sem_undo) + sizeof(short)*nsems);
+ new->semadj = (short *) &new[1];
+ new->semid = semid;
+
+ lock_semundo();
+ un = lookup_undo(ulp, semid);
+ if (un) {
+ unlock_semundo();
+ kfree(new);
+ ipc_lock_by_ptr(&sma->sem_perm);
+ ipc_rcu_putref(sma);
+ sem_unlock(sma);
+ goto out;
+ }
+ ipc_lock_by_ptr(&sma->sem_perm);
+ ipc_rcu_putref(sma);
+ if (sma->sem_perm.deleted) {
+ sem_unlock(sma);
+ unlock_semundo();
+ kfree(new);
+ un = ERR_PTR(-EIDRM);
+ goto out;
+ }
+ new->proc_next = ulp->proc_list;
+ ulp->proc_list = new;
+ new->id_next = sma->undo;
+ sma->undo = new;
+ sem_unlock(sma);
+ un = new;
+ unlock_semundo();
+out:
+ return un;
+}
+
+asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
+ unsigned nsops, const struct timespec __user *timeout)
+{
+ int error = -EINVAL;
+ struct sem_array *sma;
+ struct sembuf fast_sops[SEMOPM_FAST];
+ struct sembuf* sops = fast_sops, *sop;
+ struct sem_undo *un;
+ int undos = 0, decrease = 0, alter = 0, max;
+ struct sem_queue queue;
+ unsigned long jiffies_left = 0;
+
+ if (nsops < 1 || semid < 0)
+ return -EINVAL;
+ if (nsops > sc_semopm)
+ return -E2BIG;
+ if(nsops > SEMOPM_FAST) {
+ sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
+ if(sops==NULL)
+ return -ENOMEM;
+ }
+ if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
+ error=-EFAULT;
+ goto out_free;
+ }
+ if (timeout) {
+ struct timespec _timeout;
+ if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
+ error = -EFAULT;
+ goto out_free;
+ }
+ if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
+ _timeout.tv_nsec >= 1000000000L) {
+ error = -EINVAL;
+ goto out_free;
+ }
+ jiffies_left = timespec_to_jiffies(&_timeout);
+ }
+ max = 0;
+ for (sop = sops; sop < sops + nsops; sop++) {
+ if (sop->sem_num >= max)
+ max = sop->sem_num;
+ if (sop->sem_flg & SEM_UNDO)
+ undos++;
+ if (sop->sem_op < 0)
+ decrease = 1;
+ if (sop->sem_op > 0)
+ alter = 1;
+ }
+ alter |= decrease;
+
+retry_undos:
+ if (undos) {
+ un = find_undo(semid);
+ if (IS_ERR(un)) {
+ error = PTR_ERR(un);
+ goto out_free;
+ }
+ } else
+ un = NULL;
+
+ sma = sem_lock(semid);
+ error=-EINVAL;
+ if(sma==NULL)
+ goto out_free;
+ error = -EIDRM;
+ if (sem_checkid(sma,semid))
+ goto out_unlock_free;
+ /*
+ * semid identifies are not unique - find_undo may have
+ * allocated an undo structure, it was invalidated by an RMID
+ * and now a new array with received the same id. Check and retry.
+ */
+ if (un && un->semid == -1) {
+ sem_unlock(sma);
+ goto retry_undos;
+ }
+ error = -EFBIG;
+ if (max >= sma->sem_nsems)
+ goto out_unlock_free;
+
+ error = -EACCES;
+ if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
+ goto out_unlock_free;
+
+ error = security_sem_semop(sma, sops, nsops, alter);
+ if (error)
+ goto out_unlock_free;
+
+ error = try_atomic_semop (sma, sops, nsops, un, current->tgid);
+ if (error <= 0) {
+ if (alter && error == 0)
+ update_queue (sma);
+ goto out_unlock_free;
+ }
+
+ /* We need to sleep on this operation, so we put the current
+ * task into the pending queue and go to sleep.
+ */
+
+ queue.sma = sma;
+ queue.sops = sops;
+ queue.nsops = nsops;
+ queue.undo = un;
+ queue.pid = current->tgid;
+ queue.id = semid;
+ queue.alter = alter;
+ if (alter)
+ append_to_queue(sma ,&queue);
+ else
+ prepend_to_queue(sma ,&queue);
+
+ queue.status = -EINTR;
+ queue.sleeper = current;
+ current->state = TASK_INTERRUPTIBLE;
+ sem_unlock(sma);
+
+ if (timeout)
+ jiffies_left = schedule_timeout(jiffies_left);
+ else
+ schedule();
+
+ error = queue.status;
+ while(unlikely(error == IN_WAKEUP)) {
+ cpu_relax();
+ error = queue.status;
+ }
+
+ if (error != -EINTR) {
+ /* fast path: update_queue already obtained all requested
+ * resources */
+ goto out_free;
+ }
+
+ sma = sem_lock(semid);
+ if(sma==NULL) {
+ if(queue.prev != NULL)
+ BUG();
+ error = -EIDRM;
+ goto out_free;
+ }
+
+ /*
+ * If queue.status != -EINTR we are woken up by another process
+ */
+ error = queue.status;
+ if (error != -EINTR) {
+ goto out_unlock_free;
+ }
+
+ /*
+ * If an interrupt occurred we have to clean up the queue
+ */
+ if (timeout && jiffies_left == 0)
+ error = -EAGAIN;
+ remove_from_queue(sma,&queue);
+ goto out_unlock_free;
+
+out_unlock_free:
+ sem_unlock(sma);
+out_free:
+ if(sops != fast_sops)
+ kfree(sops);
+ return error;
+}
+
+asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
+{
+ return sys_semtimedop(semid, tsops, nsops, NULL);
+}
+
+/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
+ * parent and child tasks.
+ *
+ * See the notes above unlock_semundo() regarding the spin_lock_init()
+ * in this code. Initialize the undo_list->lock here instead of get_undo_list()
+ * because of the reasoning in the comment above unlock_semundo.
+ */
+
+int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
+{
+ struct sem_undo_list *undo_list;
+ int error;
+
+ if (clone_flags & CLONE_SYSVSEM) {
+ error = get_undo_list(&undo_list);
+ if (error)
+ return error;
+ if (atomic_read(&undo_list->refcnt) == 1)
+ spin_lock_init(&undo_list->lock);
+ atomic_inc(&undo_list->refcnt);
+ tsk->sysvsem.undo_list = undo_list;
+ } else
+ tsk->sysvsem.undo_list = NULL;
+
+ return 0;
+}
+
+/*
+ * add semadj values to semaphores, free undo structures.
+ * undo structures are not freed when semaphore arrays are destroyed
+ * so some of them may be out of date.
+ * IMPLEMENTATION NOTE: There is some confusion over whether the
+ * set of adjustments that needs to be done should be done in an atomic
+ * manner or not. That is, if we are attempting to decrement the semval
+ * should we queue up and wait until we can do so legally?
+ * The original implementation attempted to do this (queue and wait).
+ * The current implementation does not do so. The POSIX standard
+ * and SVID should be consulted to determine what behavior is mandated.
+ */
+void exit_sem(struct task_struct *tsk)
+{
+ struct sem_undo_list *undo_list;
+ struct sem_undo *u, **up;
+
+ undo_list = tsk->sysvsem.undo_list;
+ if (!undo_list)
+ return;
+
+ if (!atomic_dec_and_test(&undo_list->refcnt))
+ return;
+
+ /* There's no need to hold the semundo list lock, as current
+ * is the last task exiting for this undo list.
+ */
+ for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
+ struct sem_array *sma;
+ int nsems, i;
+ struct sem_undo *un, **unp;
+ int semid;
+
+ semid = u->semid;
+
+ if(semid == -1)
+ continue;
+ sma = sem_lock(semid);
+ if (sma == NULL)
+ continue;
+
+ if (u->semid == -1)
+ goto next_entry;
+
+ BUG_ON(sem_checkid(sma,u->semid));
+
+ /* remove u from the sma->undo list */
+ for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
+ if (u == un)
+ goto found;
+ }
+ printk ("exit_sem undo list error id=%d\n", u->semid);
+ goto next_entry;
+found:
+ *unp = un->id_next;
+ /* perform adjustments registered in u */
+ nsems = sma->sem_nsems;
+ for (i = 0; i < nsems; i++) {
+ struct sem * sem = &sma->sem_base[i];
+ if (u->semadj[i]) {
+ sem->semval += u->semadj[i];
+ /*
+ * Range checks of the new semaphore value,
+ * not defined by sus:
+ * - Some unices ignore the undo entirely
+ * (e.g. HP UX 11i 11.22, Tru64 V5.1)
+ * - some cap the value (e.g. FreeBSD caps
+ * at 0, but doesn't enforce SEMVMX)
+ *
+ * Linux caps the semaphore value, both at 0
+ * and at SEMVMX.
+ *
+ * Manfred <manfred@colorfullife.com>
+ */
+ if (sem->semval < 0)
+ sem->semval = 0;
+ if (sem->semval > SEMVMX)
+ sem->semval = SEMVMX;
+ sem->sempid = current->tgid;
+ }
+ }
+ sma->sem_otime = get_seconds();
+ /* maybe some queued-up processes were waiting for this */
+ update_queue(sma);
+next_entry:
+ sem_unlock(sma);
+ }
+ kfree(undo_list);
+}
+
+#ifdef CONFIG_PROC_FS
+static int sysvipc_sem_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
+{
+ off_t pos = 0;
+ off_t begin = 0;
+ int i, len = 0;
+
+ len += sprintf(buffer, " key semid perms nsems uid gid cuid cgid otime ctime\n");
+ down(&sem_ids.sem);
+
+ for(i = 0; i <= sem_ids.max_id; i++) {
+ struct sem_array *sma;
+ sma = sem_lock(i);
+ if(sma) {
+ len += sprintf(buffer + len, "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
+ sma->sem_perm.key,
+ sem_buildid(i,sma->sem_perm.seq),
+ sma->sem_perm.mode,
+ sma->sem_nsems,
+ sma->sem_perm.uid,
+ sma->sem_perm.gid,
+ sma->sem_perm.cuid,
+ sma->sem_perm.cgid,
+ sma->sem_otime,
+ sma->sem_ctime);
+ sem_unlock(sma);
+
+ pos += len;
+ if(pos < offset) {
+ len = 0;
+ begin = pos;
+ }
+ if(pos > offset + length)
+ goto done;
+ }
+ }
+ *eof = 1;
+done:
+ up(&sem_ids.sem);
+ *start = buffer + (offset - begin);
+ len -= (offset - begin);
+ if(len > length)
+ len = length;
+ if(len < 0)
+ len = 0;
+ return len;
+}
+#endif
diff --git a/ipc/shm.c b/ipc/shm.c
new file mode 100644
index 00000000000..06cd5c91056
--- /dev/null
+++ b/ipc/shm.c
@@ -0,0 +1,917 @@
+/*
+ * linux/ipc/shm.c
+ * Copyright (C) 1992, 1993 Krishna Balasubramanian
+ * Many improvements/fixes by Bruno Haible.
+ * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
+ * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
+ *
+ * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
+ * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
+ * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
+ * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
+ * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
+ * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
+ * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/slab.h>
+#include <linux/mm.h>
+#include <linux/hugetlb.h>
+#include <linux/shm.h>
+#include <linux/init.h>
+#include <linux/file.h>
+#include <linux/mman.h>
+#include <linux/proc_fs.h>
+#include <linux/shmem_fs.h>
+#include <linux/security.h>
+#include <linux/syscalls.h>
+#include <linux/audit.h>
+#include <asm/uaccess.h>
+
+#include "util.h"
+
+#define shm_flags shm_perm.mode
+
+static struct file_operations shm_file_operations;
+static struct vm_operations_struct shm_vm_ops;
+
+static struct ipc_ids shm_ids;
+
+#define shm_lock(id) ((struct shmid_kernel*)ipc_lock(&shm_ids,id))
+#define shm_unlock(shp) ipc_unlock(&(shp)->shm_perm)
+#define shm_get(id) ((struct shmid_kernel*)ipc_get(&shm_ids,id))
+#define shm_buildid(id, seq) \
+ ipc_buildid(&shm_ids, id, seq)
+
+static int newseg (key_t key, int shmflg, size_t size);
+static void shm_open (struct vm_area_struct *shmd);
+static void shm_close (struct vm_area_struct *shmd);
+#ifdef CONFIG_PROC_FS
+static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
+#endif
+
+size_t shm_ctlmax = SHMMAX;
+size_t shm_ctlall = SHMALL;
+int shm_ctlmni = SHMMNI;
+
+static int shm_tot; /* total number of shared memory pages */
+
+void __init shm_init (void)
+{
+ ipc_init_ids(&shm_ids, 1);
+#ifdef CONFIG_PROC_FS
+ create_proc_read_entry("sysvipc/shm", 0, NULL, sysvipc_shm_read_proc, NULL);
+#endif
+}
+
+static inline int shm_checkid(struct shmid_kernel *s, int id)
+{
+ if (ipc_checkid(&shm_ids,&s->shm_perm,id))
+ return -EIDRM;
+ return 0;
+}
+
+static inline struct shmid_kernel *shm_rmid(int id)
+{
+ return (struct shmid_kernel *)ipc_rmid(&shm_ids,id);
+}
+
+static inline int shm_addid(struct shmid_kernel *shp)
+{
+ return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni);
+}
+
+
+
+static inline void shm_inc (int id) {
+ struct shmid_kernel *shp;
+
+ if(!(shp = shm_lock(id)))
+ BUG();
+ shp->shm_atim = get_seconds();
+ shp->shm_lprid = current->tgid;
+ shp->shm_nattch++;
+ shm_unlock(shp);
+}
+
+/* This is called by fork, once for every shm attach. */
+static void shm_open (struct vm_area_struct *shmd)
+{
+ shm_inc (shmd->vm_file->f_dentry->d_inode->i_ino);
+}
+
+/*
+ * shm_destroy - free the struct shmid_kernel
+ *
+ * @shp: struct to free
+ *
+ * It has to be called with shp and shm_ids.sem locked,
+ * but returns with shp unlocked and freed.
+ */
+static void shm_destroy (struct shmid_kernel *shp)
+{
+ shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
+ shm_rmid (shp->id);
+ shm_unlock(shp);
+ if (!is_file_hugepages(shp->shm_file))
+ shmem_lock(shp->shm_file, 0, shp->mlock_user);
+ else
+ user_shm_unlock(shp->shm_file->f_dentry->d_inode->i_size,
+ shp->mlock_user);
+ fput (shp->shm_file);
+ security_shm_free(shp);
+ ipc_rcu_putref(shp);
+}
+
+/*
+ * remove the attach descriptor shmd.
+ * free memory for segment if it is marked destroyed.
+ * The descriptor has already been removed from the current->mm->mmap list
+ * and will later be kfree()d.
+ */
+static void shm_close (struct vm_area_struct *shmd)
+{
+ struct file * file = shmd->vm_file;
+ int id = file->f_dentry->d_inode->i_ino;
+ struct shmid_kernel *shp;
+
+ down (&shm_ids.sem);
+ /* remove from the list of attaches of the shm segment */
+ if(!(shp = shm_lock(id)))
+ BUG();
+ shp->shm_lprid = current->tgid;
+ shp->shm_dtim = get_seconds();
+ shp->shm_nattch--;
+ if(shp->shm_nattch == 0 &&
+ shp->shm_flags & SHM_DEST)
+ shm_destroy (shp);
+ else
+ shm_unlock(shp);
+ up (&shm_ids.sem);
+}
+
+static int shm_mmap(struct file * file, struct vm_area_struct * vma)
+{
+ file_accessed(file);
+ vma->vm_ops = &shm_vm_ops;
+ shm_inc(file->f_dentry->d_inode->i_ino);
+ return 0;
+}
+
+static struct file_operations shm_file_operations = {
+ .mmap = shm_mmap
+};
+
+static struct vm_operations_struct shm_vm_ops = {
+ .open = shm_open, /* callback for a new vm-area open */
+ .close = shm_close, /* callback for when the vm-area is released */
+ .nopage = shmem_nopage,
+#ifdef CONFIG_NUMA
+ .set_policy = shmem_set_policy,
+ .get_policy = shmem_get_policy,
+#endif
+};
+
+static int newseg (key_t key, int shmflg, size_t size)
+{
+ int error;
+ struct shmid_kernel *shp;
+ int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
+ struct file * file;
+ char name[13];
+ int id;
+
+ if (size < SHMMIN || size > shm_ctlmax)
+ return -EINVAL;
+
+ if (shm_tot + numpages >= shm_ctlall)
+ return -ENOSPC;
+
+ shp = ipc_rcu_alloc(sizeof(*shp));
+ if (!shp)
+ return -ENOMEM;
+
+ shp->shm_perm.key = key;
+ shp->shm_flags = (shmflg & S_IRWXUGO);
+ shp->mlock_user = NULL;
+
+ shp->shm_perm.security = NULL;
+ error = security_shm_alloc(shp);
+ if (error) {
+ ipc_rcu_putref(shp);
+ return error;
+ }
+
+ if (shmflg & SHM_HUGETLB) {
+ /* hugetlb_zero_setup takes care of mlock user accounting */
+ file = hugetlb_zero_setup(size);
+ shp->mlock_user = current->user;
+ } else {
+ sprintf (name, "SYSV%08x", key);
+ file = shmem_file_setup(name, size, VM_ACCOUNT);
+ }
+ error = PTR_ERR(file);
+ if (IS_ERR(file))
+ goto no_file;
+
+ error = -ENOSPC;
+ id = shm_addid(shp);
+ if(id == -1)
+ goto no_id;
+
+ shp->shm_cprid = current->tgid;
+ shp->shm_lprid = 0;
+ shp->shm_atim = shp->shm_dtim = 0;
+ shp->shm_ctim = get_seconds();
+ shp->shm_segsz = size;
+ shp->shm_nattch = 0;
+ shp->id = shm_buildid(id,shp->shm_perm.seq);
+ shp->shm_file = file;
+ file->f_dentry->d_inode->i_ino = shp->id;
+ if (shmflg & SHM_HUGETLB)
+ set_file_hugepages(file);
+ else
+ file->f_op = &shm_file_operations;
+ shm_tot += numpages;
+ shm_unlock(shp);
+ return shp->id;
+
+no_id:
+ fput(file);
+no_file:
+ security_shm_free(shp);
+ ipc_rcu_putref(shp);
+ return error;
+}
+
+asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
+{
+ struct shmid_kernel *shp;
+ int err, id = 0;
+
+ down(&shm_ids.sem);
+ if (key == IPC_PRIVATE) {
+ err = newseg(key, shmflg, size);
+ } else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
+ if (!(shmflg & IPC_CREAT))
+ err = -ENOENT;
+ else
+ err = newseg(key, shmflg, size);
+ } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
+ err = -EEXIST;
+ } else {
+ shp = shm_lock(id);
+ if(shp==NULL)
+ BUG();
+ if (shp->shm_segsz < size)
+ err = -EINVAL;
+ else if (ipcperms(&shp->shm_perm, shmflg))
+ err = -EACCES;
+ else {
+ int shmid = shm_buildid(id, shp->shm_perm.seq);
+ err = security_shm_associate(shp, shmflg);
+ if (!err)
+ err = shmid;
+ }
+ shm_unlock(shp);
+ }
+ up(&shm_ids.sem);
+
+ return err;
+}
+
+static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
+{
+ switch(version) {
+ case IPC_64:
+ return copy_to_user(buf, in, sizeof(*in));
+ case IPC_OLD:
+ {
+ struct shmid_ds out;
+
+ ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
+ out.shm_segsz = in->shm_segsz;
+ out.shm_atime = in->shm_atime;
+ out.shm_dtime = in->shm_dtime;
+ out.shm_ctime = in->shm_ctime;
+ out.shm_cpid = in->shm_cpid;
+ out.shm_lpid = in->shm_lpid;
+ out.shm_nattch = in->shm_nattch;
+
+ return copy_to_user(buf, &out, sizeof(out));
+ }
+ default:
+ return -EINVAL;
+ }
+}
+
+struct shm_setbuf {
+ uid_t uid;
+ gid_t gid;
+ mode_t mode;
+};
+
+static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
+{
+ switch(version) {
+ case IPC_64:
+ {
+ struct shmid64_ds tbuf;
+
+ if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
+ return -EFAULT;
+
+ out->uid = tbuf.shm_perm.uid;
+ out->gid = tbuf.shm_perm.gid;
+ out->mode = tbuf.shm_flags;
+
+ return 0;
+ }
+ case IPC_OLD:
+ {
+ struct shmid_ds tbuf_old;
+
+ if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
+ return -EFAULT;
+
+ out->uid = tbuf_old.shm_perm.uid;
+ out->gid = tbuf_old.shm_perm.gid;
+ out->mode = tbuf_old.shm_flags;
+
+ return 0;
+ }
+ default:
+ return -EINVAL;
+ }
+}
+
+static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
+{
+ switch(version) {
+ case IPC_64:
+ return copy_to_user(buf, in, sizeof(*in));
+ case IPC_OLD:
+ {
+ struct shminfo out;
+
+ if(in->shmmax > INT_MAX)
+ out.shmmax = INT_MAX;
+ else
+ out.shmmax = (int)in->shmmax;
+
+ out.shmmin = in->shmmin;
+ out.shmmni = in->shmmni;
+ out.shmseg = in->shmseg;
+ out.shmall = in->shmall;
+
+ return copy_to_user(buf, &out, sizeof(out));
+ }
+ default:
+ return -EINVAL;
+ }
+}
+
+static void shm_get_stat(unsigned long *rss, unsigned long *swp)
+{
+ int i;
+
+ *rss = 0;
+ *swp = 0;
+
+ for (i = 0; i <= shm_ids.max_id; i++) {
+ struct shmid_kernel *shp;
+ struct inode *inode;
+
+ shp = shm_get(i);
+ if(!shp)
+ continue;
+
+ inode = shp->shm_file->f_dentry->d_inode;
+
+ if (is_file_hugepages(shp->shm_file)) {
+ struct address_space *mapping = inode->i_mapping;
+ *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
+ } else {
+ struct shmem_inode_info *info = SHMEM_I(inode);
+ spin_lock(&info->lock);
+ *rss += inode->i_mapping->nrpages;
+ *swp += info->swapped;
+ spin_unlock(&info->lock);
+ }
+ }
+}
+
+asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
+{
+ struct shm_setbuf setbuf;
+ struct shmid_kernel *shp;
+ int err, version;
+
+ if (cmd < 0 || shmid < 0) {
+ err = -EINVAL;
+ goto out;
+ }
+
+ version = ipc_parse_version(&cmd);
+
+ switch (cmd) { /* replace with proc interface ? */
+ case IPC_INFO:
+ {
+ struct shminfo64 shminfo;
+
+ err = security_shm_shmctl(NULL, cmd);
+ if (err)
+ return err;
+
+ memset(&shminfo,0,sizeof(shminfo));
+ shminfo.shmmni = shminfo.shmseg = shm_ctlmni;
+ shminfo.shmmax = shm_ctlmax;
+ shminfo.shmall = shm_ctlall;
+
+ shminfo.shmmin = SHMMIN;
+ if(copy_shminfo_to_user (buf, &shminfo, version))
+ return -EFAULT;
+ /* reading a integer is always atomic */
+ err= shm_ids.max_id;
+ if(err<0)
+ err = 0;
+ goto out;
+ }
+ case SHM_INFO:
+ {
+ struct shm_info shm_info;
+
+ err = security_shm_shmctl(NULL, cmd);
+ if (err)
+ return err;
+
+ memset(&shm_info,0,sizeof(shm_info));
+ down(&shm_ids.sem);
+ shm_info.used_ids = shm_ids.in_use;
+ shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp);
+ shm_info.shm_tot = shm_tot;
+ shm_info.swap_attempts = 0;
+ shm_info.swap_successes = 0;
+ err = shm_ids.max_id;
+ up(&shm_ids.sem);
+ if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
+ err = -EFAULT;
+ goto out;
+ }
+
+ err = err < 0 ? 0 : err;
+ goto out;
+ }
+ case SHM_STAT:
+ case IPC_STAT:
+ {
+ struct shmid64_ds tbuf;
+ int result;
+ memset(&tbuf, 0, sizeof(tbuf));
+ shp = shm_lock(shmid);
+ if(shp==NULL) {
+ err = -EINVAL;
+ goto out;
+ } else if(cmd==SHM_STAT) {
+ err = -EINVAL;
+ if (shmid > shm_ids.max_id)
+ goto out_unlock;
+ result = shm_buildid(shmid, shp->shm_perm.seq);
+ } else {
+ err = shm_checkid(shp,shmid);
+ if(err)
+ goto out_unlock;
+ result = 0;
+ }
+ err=-EACCES;
+ if (ipcperms (&shp->shm_perm, S_IRUGO))
+ goto out_unlock;
+ err = security_shm_shmctl(shp, cmd);
+ if (err)
+ goto out_unlock;
+ kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
+ tbuf.shm_segsz = shp->shm_segsz;
+ tbuf.shm_atime = shp->shm_atim;
+ tbuf.shm_dtime = shp->shm_dtim;
+ tbuf.shm_ctime = shp->shm_ctim;
+ tbuf.shm_cpid = shp->shm_cprid;
+ tbuf.shm_lpid = shp->shm_lprid;
+ if (!is_file_hugepages(shp->shm_file))
+ tbuf.shm_nattch = shp->shm_nattch;
+ else
+ tbuf.shm_nattch = file_count(shp->shm_file) - 1;
+ shm_unlock(shp);
+ if(copy_shmid_to_user (buf, &tbuf, version))
+ err = -EFAULT;
+ else
+ err = result;
+ goto out;
+ }
+ case SHM_LOCK:
+ case SHM_UNLOCK:
+ {
+ shp = shm_lock(shmid);
+ if(shp==NULL) {
+ err = -EINVAL;
+ goto out;
+ }
+ err = shm_checkid(shp,shmid);
+ if(err)
+ goto out_unlock;
+
+ if (!capable(CAP_IPC_LOCK)) {
+ err = -EPERM;
+ if (current->euid != shp->shm_perm.uid &&
+ current->euid != shp->shm_perm.cuid)
+ goto out_unlock;
+ if (cmd == SHM_LOCK &&
+ !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
+ goto out_unlock;
+ }
+
+ err = security_shm_shmctl(shp, cmd);
+ if (err)
+ goto out_unlock;
+
+ if(cmd==SHM_LOCK) {
+ struct user_struct * user = current->user;
+ if (!is_file_hugepages(shp->shm_file)) {
+ err = shmem_lock(shp->shm_file, 1, user);
+ if (!err) {
+ shp->shm_flags |= SHM_LOCKED;
+ shp->mlock_user = user;
+ }
+ }
+ } else if (!is_file_hugepages(shp->shm_file)) {
+ shmem_lock(shp->shm_file, 0, shp->mlock_user);
+ shp->shm_flags &= ~SHM_LOCKED;
+ shp->mlock_user = NULL;
+ }
+ shm_unlock(shp);
+ goto out;
+ }
+ case IPC_RMID:
+ {
+ /*
+ * We cannot simply remove the file. The SVID states
+ * that the block remains until the last person
+ * detaches from it, then is deleted. A shmat() on
+ * an RMID segment is legal in older Linux and if
+ * we change it apps break...
+ *
+ * Instead we set a destroyed flag, and then blow
+ * the name away when the usage hits zero.
+ */
+ down(&shm_ids.sem);
+ shp = shm_lock(shmid);
+ err = -EINVAL;
+ if (shp == NULL)
+ goto out_up;
+ err = shm_checkid(shp, shmid);
+ if(err)
+ goto out_unlock_up;
+
+ if (current->euid != shp->shm_perm.uid &&
+ current->euid != shp->shm_perm.cuid &&
+ !capable(CAP_SYS_ADMIN)) {
+ err=-EPERM;
+ goto out_unlock_up;
+ }
+
+ err = security_shm_shmctl(shp, cmd);
+ if (err)
+ goto out_unlock_up;
+
+ if (shp->shm_nattch){
+ shp->shm_flags |= SHM_DEST;
+ /* Do not find it any more */
+ shp->shm_perm.key = IPC_PRIVATE;
+ shm_unlock(shp);
+ } else
+ shm_destroy (shp);
+ up(&shm_ids.sem);
+ goto out;
+ }
+
+ case IPC_SET:
+ {
+ if (copy_shmid_from_user (&setbuf, buf, version)) {
+ err = -EFAULT;
+ goto out;
+ }
+ if ((err = audit_ipc_perms(0, setbuf.uid, setbuf.gid, setbuf.mode)))
+ return err;
+ down(&shm_ids.sem);
+ shp = shm_lock(shmid);
+ err=-EINVAL;
+ if(shp==NULL)
+ goto out_up;
+ err = shm_checkid(shp,shmid);
+ if(err)
+ goto out_unlock_up;
+ err=-EPERM;
+ if (current->euid != shp->shm_perm.uid &&
+ current->euid != shp->shm_perm.cuid &&
+ !capable(CAP_SYS_ADMIN)) {
+ goto out_unlock_up;
+ }
+
+ err = security_shm_shmctl(shp, cmd);
+ if (err)
+ goto out_unlock_up;
+
+ shp->shm_perm.uid = setbuf.uid;
+ shp->shm_perm.gid = setbuf.gid;
+ shp->shm_flags = (shp->shm_flags & ~S_IRWXUGO)
+ | (setbuf.mode & S_IRWXUGO);
+ shp->shm_ctim = get_seconds();
+ break;
+ }
+
+ default:
+ err = -EINVAL;
+ goto out;
+ }
+
+ err = 0;
+out_unlock_up:
+ shm_unlock(shp);
+out_up:
+ up(&shm_ids.sem);
+ goto out;
+out_unlock:
+ shm_unlock(shp);
+out:
+ return err;
+}
+
+/*
+ * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
+ *
+ * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
+ * "raddr" thing points to kernel space, and there has to be a wrapper around
+ * this.
+ */
+long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
+{
+ struct shmid_kernel *shp;
+ unsigned long addr;
+ unsigned long size;
+ struct file * file;
+ int err;
+ unsigned long flags;
+ unsigned long prot;
+ unsigned long o_flags;
+ int acc_mode;
+ void *user_addr;
+
+ if (shmid < 0) {
+ err = -EINVAL;
+ goto out;
+ } else if ((addr = (ulong)shmaddr)) {
+ if (addr & (SHMLBA-1)) {
+ if (shmflg & SHM_RND)
+ addr &= ~(SHMLBA-1); /* round down */
+ else
+#ifndef __ARCH_FORCE_SHMLBA
+ if (addr & ~PAGE_MASK)
+#endif
+ return -EINVAL;
+ }
+ flags = MAP_SHARED | MAP_FIXED;
+ } else {
+ if ((shmflg & SHM_REMAP))
+ return -EINVAL;
+
+ flags = MAP_SHARED;
+ }
+
+ if (shmflg & SHM_RDONLY) {
+ prot = PROT_READ;
+ o_flags = O_RDONLY;
+ acc_mode = S_IRUGO;
+ } else {
+ prot = PROT_READ | PROT_WRITE;
+ o_flags = O_RDWR;
+ acc_mode = S_IRUGO | S_IWUGO;
+ }
+ if (shmflg & SHM_EXEC) {
+ prot |= PROT_EXEC;
+ acc_mode |= S_IXUGO;
+ }
+
+ /*
+ * We cannot rely on the fs check since SYSV IPC does have an
+ * additional creator id...
+ */
+ shp = shm_lock(shmid);
+ if(shp == NULL) {
+ err = -EINVAL;
+ goto out;
+ }
+ err = shm_checkid(shp,shmid);
+ if (err) {
+ shm_unlock(shp);
+ goto out;
+ }
+ if (ipcperms(&shp->shm_perm, acc_mode)) {
+ shm_unlock(shp);
+ err = -EACCES;
+ goto out;
+ }
+
+ err = security_shm_shmat(shp, shmaddr, shmflg);
+ if (err) {
+ shm_unlock(shp);
+ return err;
+ }
+
+ file = shp->shm_file;
+ size = i_size_read(file->f_dentry->d_inode);
+ shp->shm_nattch++;
+ shm_unlock(shp);
+
+ down_write(&current->mm->mmap_sem);
+ if (addr && !(shmflg & SHM_REMAP)) {
+ user_addr = ERR_PTR(-EINVAL);
+ if (find_vma_intersection(current->mm, addr, addr + size))
+ goto invalid;
+ /*
+ * If shm segment goes below stack, make sure there is some
+ * space left for the stack to grow (at least 4 pages).
+ */
+ if (addr < current->mm->start_stack &&
+ addr > current->mm->start_stack - size - PAGE_SIZE * 5)
+ goto invalid;
+ }
+
+ user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0);
+
+invalid:
+ up_write(&current->mm->mmap_sem);
+
+ down (&shm_ids.sem);
+ if(!(shp = shm_lock(shmid)))
+ BUG();
+ shp->shm_nattch--;
+ if(shp->shm_nattch == 0 &&
+ shp->shm_flags & SHM_DEST)
+ shm_destroy (shp);
+ else
+ shm_unlock(shp);
+ up (&shm_ids.sem);
+
+ *raddr = (unsigned long) user_addr;
+ err = 0;
+ if (IS_ERR(user_addr))
+ err = PTR_ERR(user_addr);
+out:
+ return err;
+}
+
+/*
+ * detach and kill segment if marked destroyed.
+ * The work is done in shm_close.
+ */
+asmlinkage long sys_shmdt(char __user *shmaddr)
+{
+ struct mm_struct *mm = current->mm;
+ struct vm_area_struct *vma, *next;
+ unsigned long addr = (unsigned long)shmaddr;
+ loff_t size = 0;
+ int retval = -EINVAL;
+
+ down_write(&mm->mmap_sem);
+
+ /*
+ * This function tries to be smart and unmap shm segments that
+ * were modified by partial mlock or munmap calls:
+ * - It first determines the size of the shm segment that should be
+ * unmapped: It searches for a vma that is backed by shm and that
+ * started at address shmaddr. It records it's size and then unmaps
+ * it.
+ * - Then it unmaps all shm vmas that started at shmaddr and that
+ * are within the initially determined size.
+ * Errors from do_munmap are ignored: the function only fails if
+ * it's called with invalid parameters or if it's called to unmap
+ * a part of a vma. Both calls in this function are for full vmas,
+ * the parameters are directly copied from the vma itself and always
+ * valid - therefore do_munmap cannot fail. (famous last words?)
+ */
+ /*
+ * If it had been mremap()'d, the starting address would not
+ * match the usual checks anyway. So assume all vma's are
+ * above the starting address given.
+ */
+ vma = find_vma(mm, addr);
+
+ while (vma) {
+ next = vma->vm_next;
+
+ /*
+ * Check if the starting address would match, i.e. it's
+ * a fragment created by mprotect() and/or munmap(), or it
+ * otherwise it starts at this address with no hassles.
+ */
+ if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
+ (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
+
+
+ size = vma->vm_file->f_dentry->d_inode->i_size;
+ do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
+ /*
+ * We discovered the size of the shm segment, so
+ * break out of here and fall through to the next
+ * loop that uses the size information to stop
+ * searching for matching vma's.
+ */
+ retval = 0;
+ vma = next;
+ break;
+ }
+ vma = next;
+ }
+
+ /*
+ * We need look no further than the maximum address a fragment
+ * could possibly have landed at. Also cast things to loff_t to
+ * prevent overflows and make comparisions vs. equal-width types.
+ */
+ while (vma && (loff_t)(vma->vm_end - addr) <= size) {
+ next = vma->vm_next;
+
+ /* finding a matching vma now does not alter retval */
+ if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
+ (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
+
+ do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
+ vma = next;
+ }
+
+ up_write(&mm->mmap_sem);
+ return retval;
+}
+
+#ifdef CONFIG_PROC_FS
+static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
+{
+ off_t pos = 0;
+ off_t begin = 0;
+ int i, len = 0;
+
+ down(&shm_ids.sem);
+ len += sprintf(buffer, " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n");
+
+ for(i = 0; i <= shm_ids.max_id; i++) {
+ struct shmid_kernel* shp;
+
+ shp = shm_lock(i);
+ if(shp!=NULL) {
+#define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
+#define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
+ char *format;
+
+ if (sizeof(size_t) <= sizeof(int))
+ format = SMALL_STRING;
+ else
+ format = BIG_STRING;
+ len += sprintf(buffer + len, format,
+ shp->shm_perm.key,
+ shm_buildid(i, shp->shm_perm.seq),
+ shp->shm_flags,
+ shp->shm_segsz,
+ shp->shm_cprid,
+ shp->shm_lprid,
+ is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch,
+ shp->shm_perm.uid,
+ shp->shm_perm.gid,
+ shp->shm_perm.cuid,
+ shp->shm_perm.cgid,
+ shp->shm_atim,
+ shp->shm_dtim,
+ shp->shm_ctim);
+ shm_unlock(shp);
+
+ pos += len;
+ if(pos < offset) {
+ len = 0;
+ begin = pos;
+ }
+ if(pos > offset + length)
+ goto done;
+ }
+ }
+ *eof = 1;
+done:
+ up(&shm_ids.sem);
+ *start = buffer + (offset - begin);
+ len -= (offset - begin);
+ if(len > length)
+ len = length;
+ if(len < 0)
+ len = 0;
+ return len;
+}
+#endif
diff --git a/ipc/util.c b/ipc/util.c
new file mode 100644
index 00000000000..e00c35f7b2b
--- /dev/null
+++ b/ipc/util.c
@@ -0,0 +1,580 @@
+/*
+ * linux/ipc/util.c
+ * Copyright (C) 1992 Krishna Balasubramanian
+ *
+ * Sep 1997 - Call suser() last after "normal" permission checks so we
+ * get BSD style process accounting right.
+ * Occurs in several places in the IPC code.
+ * Chris Evans, <chris@ferret.lmh.ox.ac.uk>
+ * Nov 1999 - ipc helper functions, unified SMP locking
+ * Manfred Spraul <manfreds@colorfullife.com>
+ * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
+ * Mingming Cao <cmm@us.ibm.com>
+ */
+
+#include <linux/config.h>
+#include <linux/mm.h>
+#include <linux/shm.h>
+#include <linux/init.h>
+#include <linux/msg.h>
+#include <linux/smp_lock.h>
+#include <linux/vmalloc.h>
+#include <linux/slab.h>
+#include <linux/highuid.h>
+#include <linux/security.h>
+#include <linux/rcupdate.h>
+#include <linux/workqueue.h>
+
+#include <asm/unistd.h>
+
+#include "util.h"
+
+/**
+ * ipc_init - initialise IPC subsystem
+ *
+ * The various system5 IPC resources (semaphores, messages and shared
+ * memory are initialised
+ */
+
+static int __init ipc_init(void)
+{
+ sem_init();
+ msg_init();
+ shm_init();
+ return 0;
+}
+__initcall(ipc_init);
+
+/**
+ * ipc_init_ids - initialise IPC identifiers
+ * @ids: Identifier set
+ * @size: Number of identifiers
+ *
+ * Given a size for the ipc identifier range (limited below IPCMNI)
+ * set up the sequence range to use then allocate and initialise the
+ * array itself.
+ */
+
+void __init ipc_init_ids(struct ipc_ids* ids, int size)
+{
+ int i;
+ sema_init(&ids->sem,1);
+
+ if(size > IPCMNI)
+ size = IPCMNI;
+ ids->in_use = 0;
+ ids->max_id = -1;
+ ids->seq = 0;
+ {
+ int seq_limit = INT_MAX/SEQ_MULTIPLIER;
+ if(seq_limit > USHRT_MAX)
+ ids->seq_max = USHRT_MAX;
+ else
+ ids->seq_max = seq_limit;
+ }
+
+ ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size +
+ sizeof(struct ipc_id_ary));
+
+ if(ids->entries == NULL) {
+ printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
+ size = 0;
+ ids->entries = &ids->nullentry;
+ }
+ ids->entries->size = size;
+ for(i=0;i<size;i++)
+ ids->entries->p[i] = NULL;
+}
+
+/**
+ * ipc_findkey - find a key in an ipc identifier set
+ * @ids: Identifier set
+ * @key: The key to find
+ *
+ * Requires ipc_ids.sem locked.
+ * Returns the identifier if found or -1 if not.
+ */
+
+int ipc_findkey(struct ipc_ids* ids, key_t key)
+{
+ int id;
+ struct kern_ipc_perm* p;
+ int max_id = ids->max_id;
+
+ /*
+ * rcu_dereference() is not needed here
+ * since ipc_ids.sem is held
+ */
+ for (id = 0; id <= max_id; id++) {
+ p = ids->entries->p[id];
+ if(p==NULL)
+ continue;
+ if (key == p->key)
+ return id;
+ }
+ return -1;
+}
+
+/*
+ * Requires ipc_ids.sem locked
+ */
+static int grow_ary(struct ipc_ids* ids, int newsize)
+{
+ struct ipc_id_ary* new;
+ struct ipc_id_ary* old;
+ int i;
+ int size = ids->entries->size;
+
+ if(newsize > IPCMNI)
+ newsize = IPCMNI;
+ if(newsize <= size)
+ return newsize;
+
+ new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
+ sizeof(struct ipc_id_ary));
+ if(new == NULL)
+ return size;
+ new->size = newsize;
+ memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size +
+ sizeof(struct ipc_id_ary));
+ for(i=size;i<newsize;i++) {
+ new->p[i] = NULL;
+ }
+ old = ids->entries;
+
+ /*
+ * Use rcu_assign_pointer() to make sure the memcpyed contents
+ * of the new array are visible before the new array becomes visible.
+ */
+ rcu_assign_pointer(ids->entries, new);
+
+ ipc_rcu_putref(old);
+ return newsize;
+}
+
+/**
+ * ipc_addid - add an IPC identifier
+ * @ids: IPC identifier set
+ * @new: new IPC permission set
+ * @size: new size limit for the id array
+ *
+ * Add an entry 'new' to the IPC arrays. The permissions object is
+ * initialised and the first free entry is set up and the id assigned
+ * is returned. The list is returned in a locked state on success.
+ * On failure the list is not locked and -1 is returned.
+ *
+ * Called with ipc_ids.sem held.
+ */
+
+int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
+{
+ int id;
+
+ size = grow_ary(ids,size);
+
+ /*
+ * rcu_dereference()() is not needed here since
+ * ipc_ids.sem is held
+ */
+ for (id = 0; id < size; id++) {
+ if(ids->entries->p[id] == NULL)
+ goto found;
+ }
+ return -1;
+found:
+ ids->in_use++;
+ if (id > ids->max_id)
+ ids->max_id = id;
+
+ new->cuid = new->uid = current->euid;
+ new->gid = new->cgid = current->egid;
+
+ new->seq = ids->seq++;
+ if(ids->seq > ids->seq_max)
+ ids->seq = 0;
+
+ spin_lock_init(&new->lock);
+ new->deleted = 0;
+ rcu_read_lock();
+ spin_lock(&new->lock);
+ ids->entries->p[id] = new;
+ return id;
+}
+
+/**
+ * ipc_rmid - remove an IPC identifier
+ * @ids: identifier set
+ * @id: Identifier to remove
+ *
+ * The identifier must be valid, and in use. The kernel will panic if
+ * fed an invalid identifier. The entry is removed and internal
+ * variables recomputed. The object associated with the identifier
+ * is returned.
+ * ipc_ids.sem and the spinlock for this ID is hold before this function
+ * is called, and remain locked on the exit.
+ */
+
+struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
+{
+ struct kern_ipc_perm* p;
+ int lid = id % SEQ_MULTIPLIER;
+ if(lid >= ids->entries->size)
+ BUG();
+
+ /*
+ * do not need a rcu_dereference()() here to force ordering
+ * on Alpha, since the ipc_ids.sem is held.
+ */
+ p = ids->entries->p[lid];
+ ids->entries->p[lid] = NULL;
+ if(p==NULL)
+ BUG();
+ ids->in_use--;
+
+ if (lid == ids->max_id) {
+ do {
+ lid--;
+ if(lid == -1)
+ break;
+ } while (ids->entries->p[lid] == NULL);
+ ids->max_id = lid;
+ }
+ p->deleted = 1;
+ return p;
+}
+
+/**
+ * ipc_alloc - allocate ipc space
+ * @size: size desired
+ *
+ * Allocate memory from the appropriate pools and return a pointer to it.
+ * NULL is returned if the allocation fails
+ */
+
+void* ipc_alloc(int size)
+{
+ void* out;
+ if(size > PAGE_SIZE)
+ out = vmalloc(size);
+ else
+ out = kmalloc(size, GFP_KERNEL);
+ return out;
+}
+
+/**
+ * ipc_free - free ipc space
+ * @ptr: pointer returned by ipc_alloc
+ * @size: size of block
+ *
+ * Free a block created with ipc_alloc. The caller must know the size
+ * used in the allocation call.
+ */
+
+void ipc_free(void* ptr, int size)
+{
+ if(size > PAGE_SIZE)
+ vfree(ptr);
+ else
+ kfree(ptr);
+}
+
+/*
+ * rcu allocations:
+ * There are three headers that are prepended to the actual allocation:
+ * - during use: ipc_rcu_hdr.
+ * - during the rcu grace period: ipc_rcu_grace.
+ * - [only if vmalloc]: ipc_rcu_sched.
+ * Their lifetime doesn't overlap, thus the headers share the same memory.
+ * Unlike a normal union, they are right-aligned, thus some container_of
+ * forward/backward casting is necessary:
+ */
+struct ipc_rcu_hdr
+{
+ int refcount;
+ int is_vmalloc;
+ void *data[0];
+};
+
+
+struct ipc_rcu_grace
+{
+ struct rcu_head rcu;
+ /* "void *" makes sure alignment of following data is sane. */
+ void *data[0];
+};
+
+struct ipc_rcu_sched
+{
+ struct work_struct work;
+ /* "void *" makes sure alignment of following data is sane. */
+ void *data[0];
+};
+
+#define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
+ sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
+#define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
+ sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
+
+static inline int rcu_use_vmalloc(int size)
+{
+ /* Too big for a single page? */
+ if (HDRLEN_KMALLOC + size > PAGE_SIZE)
+ return 1;
+ return 0;
+}
+
+/**
+ * ipc_rcu_alloc - allocate ipc and rcu space
+ * @size: size desired
+ *
+ * Allocate memory for the rcu header structure + the object.
+ * Returns the pointer to the object.
+ * NULL is returned if the allocation fails.
+ */
+
+void* ipc_rcu_alloc(int size)
+{
+ void* out;
+ /*
+ * We prepend the allocation with the rcu struct, and
+ * workqueue if necessary (for vmalloc).
+ */
+ if (rcu_use_vmalloc(size)) {
+ out = vmalloc(HDRLEN_VMALLOC + size);
+ if (out) {
+ out += HDRLEN_VMALLOC;
+ container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
+ container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
+ }
+ } else {
+ out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
+ if (out) {
+ out += HDRLEN_KMALLOC;
+ container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
+ container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
+ }
+ }
+
+ return out;
+}
+
+void ipc_rcu_getref(void *ptr)
+{
+ container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
+}
+
+/**
+ * ipc_schedule_free - free ipc + rcu space
+ *
+ * Since RCU callback function is called in bh,
+ * we need to defer the vfree to schedule_work
+ */
+static void ipc_schedule_free(struct rcu_head *head)
+{
+ struct ipc_rcu_grace *grace =
+ container_of(head, struct ipc_rcu_grace, rcu);
+ struct ipc_rcu_sched *sched =
+ container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
+
+ INIT_WORK(&sched->work, vfree, sched);
+ schedule_work(&sched->work);
+}
+
+/**
+ * ipc_immediate_free - free ipc + rcu space
+ *
+ * Free from the RCU callback context
+ *
+ */
+static void ipc_immediate_free(struct rcu_head *head)
+{
+ struct ipc_rcu_grace *free =
+ container_of(head, struct ipc_rcu_grace, rcu);
+ kfree(free);
+}
+
+void ipc_rcu_putref(void *ptr)
+{
+ if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
+ return;
+
+ if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
+ call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
+ ipc_schedule_free);
+ } else {
+ call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
+ ipc_immediate_free);
+ }
+}
+
+/**
+ * ipcperms - check IPC permissions
+ * @ipcp: IPC permission set
+ * @flag: desired permission set.
+ *
+ * Check user, group, other permissions for access
+ * to ipc resources. return 0 if allowed
+ */
+
+int ipcperms (struct kern_ipc_perm *ipcp, short flag)
+{ /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
+ int requested_mode, granted_mode;
+
+ requested_mode = (flag >> 6) | (flag >> 3) | flag;
+ granted_mode = ipcp->mode;
+ if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
+ granted_mode >>= 6;
+ else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
+ granted_mode >>= 3;
+ /* is there some bit set in requested_mode but not in granted_mode? */
+ if ((requested_mode & ~granted_mode & 0007) &&
+ !capable(CAP_IPC_OWNER))
+ return -1;
+
+ return security_ipc_permission(ipcp, flag);
+}
+
+/*
+ * Functions to convert between the kern_ipc_perm structure and the
+ * old/new ipc_perm structures
+ */
+
+/**
+ * kernel_to_ipc64_perm - convert kernel ipc permissions to user
+ * @in: kernel permissions
+ * @out: new style IPC permissions
+ *
+ * Turn the kernel object 'in' into a set of permissions descriptions
+ * for returning to userspace (out).
+ */
+
+
+void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
+{
+ out->key = in->key;
+ out->uid = in->uid;
+ out->gid = in->gid;
+ out->cuid = in->cuid;
+ out->cgid = in->cgid;
+ out->mode = in->mode;
+ out->seq = in->seq;
+}
+
+/**
+ * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
+ * @in: new style IPC permissions
+ * @out: old style IPC permissions
+ *
+ * Turn the new style permissions object in into a compatibility
+ * object and store it into the 'out' pointer.
+ */
+
+void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
+{
+ out->key = in->key;
+ SET_UID(out->uid, in->uid);
+ SET_GID(out->gid, in->gid);
+ SET_UID(out->cuid, in->cuid);
+ SET_GID(out->cgid, in->cgid);
+ out->mode = in->mode;
+ out->seq = in->seq;
+}
+
+/*
+ * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
+ * is called with shm_ids.sem locked. Since grow_ary() is also called with
+ * shm_ids.sem down(for Shared Memory), there is no need to add read
+ * barriers here to gurantee the writes in grow_ary() are seen in order
+ * here (for Alpha).
+ *
+ * However ipc_get() itself does not necessary require ipc_ids.sem down. So
+ * if in the future ipc_get() is used by other places without ipc_ids.sem
+ * down, then ipc_get() needs read memery barriers as ipc_lock() does.
+ */
+struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
+{
+ struct kern_ipc_perm* out;
+ int lid = id % SEQ_MULTIPLIER;
+ if(lid >= ids->entries->size)
+ return NULL;
+ out = ids->entries->p[lid];
+ return out;
+}
+
+struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
+{
+ struct kern_ipc_perm* out;
+ int lid = id % SEQ_MULTIPLIER;
+ struct ipc_id_ary* entries;
+
+ rcu_read_lock();
+ entries = rcu_dereference(ids->entries);
+ if(lid >= entries->size) {
+ rcu_read_unlock();
+ return NULL;
+ }
+ out = entries->p[lid];
+ if(out == NULL) {
+ rcu_read_unlock();
+ return NULL;
+ }
+ spin_lock(&out->lock);
+
+ /* ipc_rmid() may have already freed the ID while ipc_lock
+ * was spinning: here verify that the structure is still valid
+ */
+ if (out->deleted) {
+ spin_unlock(&out->lock);
+ rcu_read_unlock();
+ return NULL;
+ }
+ return out;
+}
+
+void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
+{
+ rcu_read_lock();
+ spin_lock(&perm->lock);
+}
+
+void ipc_unlock(struct kern_ipc_perm* perm)
+{
+ spin_unlock(&perm->lock);
+ rcu_read_unlock();
+}
+
+int ipc_buildid(struct ipc_ids* ids, int id, int seq)
+{
+ return SEQ_MULTIPLIER*seq + id;
+}
+
+int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
+{
+ if(uid/SEQ_MULTIPLIER != ipcp->seq)
+ return 1;
+ return 0;
+}
+
+#ifdef __ARCH_WANT_IPC_PARSE_VERSION
+
+
+/**
+ * ipc_parse_version - IPC call version
+ * @cmd: pointer to command
+ *
+ * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
+ * The cmd value is turned from an encoding command and version into
+ * just the command code.
+ */
+
+int ipc_parse_version (int *cmd)
+{
+ if (*cmd & IPC_64) {
+ *cmd ^= IPC_64;
+ return IPC_64;
+ } else {
+ return IPC_OLD;
+ }
+}
+
+#endif /* __ARCH_WANT_IPC_PARSE_VERSION */
diff --git a/ipc/util.h b/ipc/util.h
new file mode 100644
index 00000000000..07d68945236
--- /dev/null
+++ b/ipc/util.h
@@ -0,0 +1,81 @@
+/*
+ * linux/ipc/util.h
+ * Copyright (C) 1999 Christoph Rohland
+ *
+ * ipc helper functions (c) 1999 Manfred Spraul <manfreds@colorfullife.com>
+ */
+
+#ifndef _IPC_UTIL_H
+#define _IPC_UTIL_H
+
+#define USHRT_MAX 0xffff
+#define SEQ_MULTIPLIER (IPCMNI)
+
+void sem_init (void);
+void msg_init (void);
+void shm_init (void);
+
+struct ipc_id_ary {
+ int size;
+ struct kern_ipc_perm *p[0];
+};
+
+struct ipc_ids {
+ int in_use;
+ int max_id;
+ unsigned short seq;
+ unsigned short seq_max;
+ struct semaphore sem;
+ struct ipc_id_ary nullentry;
+ struct ipc_id_ary* entries;
+};
+
+void __init ipc_init_ids(struct ipc_ids* ids, int size);
+
+/* must be called with ids->sem acquired.*/
+int ipc_findkey(struct ipc_ids* ids, key_t key);
+int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size);
+
+/* must be called with both locks acquired. */
+struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id);
+
+int ipcperms (struct kern_ipc_perm *ipcp, short flg);
+
+/* for rare, potentially huge allocations.
+ * both function can sleep
+ */
+void* ipc_alloc(int size);
+void ipc_free(void* ptr, int size);
+
+/*
+ * For allocation that need to be freed by RCU.
+ * Objects are reference counted, they start with reference count 1.
+ * getref increases the refcount, the putref call that reduces the recount
+ * to 0 schedules the rcu destruction. Caller must guarantee locking.
+ */
+void* ipc_rcu_alloc(int size);
+void ipc_rcu_getref(void *ptr);
+void ipc_rcu_putref(void *ptr);
+
+struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id);
+struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id);
+void ipc_lock_by_ptr(struct kern_ipc_perm *ipcp);
+void ipc_unlock(struct kern_ipc_perm* perm);
+int ipc_buildid(struct ipc_ids* ids, int id, int seq);
+int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid);
+
+void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
+void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
+
+#if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__)
+ /* On IA-64, we always use the "64-bit version" of the IPC structures. */
+# define ipc_parse_version(cmd) IPC_64
+#else
+int ipc_parse_version (int *cmd);
+#endif
+
+extern void free_msg(struct msg_msg *msg);
+extern struct msg_msg *load_msg(const void __user *src, int len);
+extern int store_msg(void __user *dest, struct msg_msg *msg, int len);
+
+#endif