Back to index

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