Back to index

glibc  2.9
msgctl.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995,1997,1998,2000,2002,2004,2006
00002        Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <sys/msg.h>
00023 #include <ipc_priv.h>
00024 
00025 #include <sysdep.h>
00026 #include <string.h>
00027 #include <sys/syscall.h>
00028 #include <shlib-compat.h>
00029 #include <bp-checks.h>
00030 
00031 #include <kernel-features.h>
00032 
00033 struct __old_msqid_ds
00034 {
00035   struct __old_ipc_perm msg_perm;  /* structure describing operation permission */
00036   struct msg *__unbounded __msg_first;    /* pointer to first message on queue */
00037   struct msg *__unbounded __msg_last;     /* pointer to last message on queue */
00038   __time_t msg_stime;                     /* time of last msgsnd command */
00039   __time_t msg_rtime;                     /* time of last msgrcv command */
00040   __time_t msg_ctime;                     /* time of last change */
00041   struct wait_queue *__unbounded __wwait; /* ??? */
00042   struct wait_queue *__unbounded __rwait; /* ??? */
00043   unsigned short int __msg_cbytes; /* current number of bytes on queue */
00044   unsigned short int msg_qnum;            /* number of messages currently on queue */
00045   unsigned short int msg_qbytes;   /* max number of bytes allowed on queue */
00046   __ipc_pid_t msg_lspid;           /* pid of last msgsnd() */
00047   __ipc_pid_t msg_lrpid;           /* pid of last msgrcv() */
00048 };
00049 
00050 /* Allows to control internal state and destruction of message queue
00051    objects.  */
00052 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
00053 int __old_msgctl (int, int, struct __old_msqid_ds *);
00054 #endif
00055 int __new_msgctl (int, int, struct msqid_ds *);
00056 
00057 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
00058 int
00059 attribute_compat_text_section
00060 __old_msgctl (int msqid, int cmd, struct __old_msqid_ds *buf)
00061 {
00062   return INLINE_SYSCALL (ipc, 5, IPCOP_msgctl, msqid, cmd, 0, CHECK_1 (buf));
00063 }
00064 compat_symbol (libc, __old_msgctl, msgctl, GLIBC_2_0);
00065 #endif
00066 
00067 int
00068 __new_msgctl (int msqid, int cmd, struct msqid_ds *buf)
00069 {
00070 #if __ASSUME_IPC64 > 0
00071   return INLINE_SYSCALL (ipc, 5, IPCOP_msgctl,
00072                       msqid, cmd | __IPC_64, 0, CHECK_1 (buf));
00073 #else
00074   switch (cmd) {
00075     case MSG_STAT:
00076     case IPC_STAT:
00077     case IPC_SET:
00078       break;
00079     default:
00080       return INLINE_SYSCALL (ipc, 5, IPCOP_msgctl,
00081                           msqid, cmd, 0, CHECK_1 (buf));
00082   }
00083 
00084   {
00085     int result;
00086     struct __old_msqid_ds old;
00087 
00088     /* Unfortunately there is no way how to find out for sure whether
00089        we should use old or new msgctl.  */
00090     result = INLINE_SYSCALL (ipc, 5, IPCOP_msgctl,
00091                           msqid, cmd | __IPC_64, 0, CHECK_1 (buf));
00092     if (result != -1 || errno != EINVAL)
00093       return result;
00094 
00095     if (cmd == IPC_SET)
00096       {
00097        old.msg_perm.uid = buf->msg_perm.uid;
00098        old.msg_perm.gid = buf->msg_perm.gid;
00099        old.msg_perm.mode = buf->msg_perm.mode;
00100        old.msg_qbytes = buf->msg_qbytes;
00101        if (old.msg_perm.uid != buf->msg_perm.uid ||
00102            old.msg_perm.gid != buf->msg_perm.gid ||
00103            old.msg_qbytes != buf->msg_qbytes)
00104          {
00105            __set_errno (EINVAL);
00106            return -1;
00107          }
00108       }
00109     result = INLINE_SYSCALL (ipc, 5, IPCOP_msgctl,
00110                           msqid, cmd, 0, __ptrvalue (&old));
00111     if (result != -1 && cmd != IPC_SET)
00112       {
00113        memset(buf, 0, sizeof(*buf));
00114        buf->msg_perm.__key = old.msg_perm.__key;
00115        buf->msg_perm.uid = old.msg_perm.uid;
00116        buf->msg_perm.gid = old.msg_perm.gid;
00117        buf->msg_perm.cuid = old.msg_perm.cuid;
00118        buf->msg_perm.cgid = old.msg_perm.cgid;
00119        buf->msg_perm.mode = old.msg_perm.mode;
00120        buf->msg_perm.__seq = old.msg_perm.__seq;
00121        buf->msg_stime = old.msg_stime;
00122        buf->msg_rtime = old.msg_rtime;
00123        buf->msg_ctime = old.msg_ctime;
00124        buf->__msg_cbytes = old.__msg_cbytes;
00125        buf->msg_qnum = old.msg_qnum;
00126        buf->msg_qbytes = old.msg_qbytes;
00127        buf->msg_lspid = old.msg_lspid;
00128        buf->msg_lrpid = old.msg_lrpid;
00129       }
00130     return result;
00131   }
00132 #endif
00133 }
00134 
00135 versioned_symbol (libc, __new_msgctl, msgctl, GLIBC_2_2);