Back to index

glibc  2.9
msgctl.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995,1997,1998,2000,2003,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 
00031 struct __old_msqid_ds
00032 {
00033   struct __old_ipc_perm msg_perm;  /* structure describing operation permission */
00034   struct msg *__unbounded __msg_first;    /* pointer to first message on queue */
00035   struct msg *__unbounded __msg_last;     /* pointer to last message on queue */
00036   __time_t msg_stime;                     /* time of last msgsnd command */
00037   __time_t msg_rtime;                     /* time of last msgrcv command */
00038   __time_t msg_ctime;                     /* time of last change */
00039   struct wait_queue *__unbounded __wwait; /* ??? */
00040   struct wait_queue *__unbounded __rwait; /* ??? */
00041   unsigned short int __msg_cbytes; /* current number of bytes on queue */
00042   unsigned short int msg_qnum;            /* number of messages currently on queue */
00043   unsigned short int msg_qbytes;   /* max number of bytes allowed on queue */
00044   __ipc_pid_t msg_lspid;           /* pid of last msgsnd() */
00045   __ipc_pid_t msg_lrpid;           /* pid of last msgrcv() */
00046 };
00047 
00048 /* Allows to control internal state and destruction of message queue
00049    objects.  */
00050 int __new_msgctl (int, int, struct msqid_ds *);
00051 
00052 int
00053 __new_msgctl (int msqid, int cmd, struct msqid_ds *buf)
00054 {
00055   /* This is a misnomer -- Alpha had 32-bit uids at the beginning
00056      of time.  However, msg_qnum and msg_qbytes changed size at
00057      the same time the size of uid changed elsewhere.  */
00058 #if __ASSUME_32BITUIDS > 0
00059   return INLINE_SYSCALL (msgctl, 3, msqid, cmd | __IPC_64, CHECK_1 (buf));
00060 #else
00061   switch (cmd) {
00062     case MSG_STAT:
00063     case IPC_STAT:
00064     case IPC_SET:
00065       break;
00066     default:
00067       return INLINE_SYSCALL (msgctl, 3, msqid, cmd, CHECK_1 (buf));
00068   }
00069 
00070   {
00071     int save_errno = errno, result;
00072     struct __old_msqid_ds old;
00073 
00074     /* Unfortunately there is no way how to find out for sure whether
00075        we should use old or new msgctl.  */
00076     result = INLINE_SYSCALL (msgctl, 3, msqid, cmd | __IPC_64, CHECK_1 (buf));
00077     if (result != -1 || errno != EINVAL)
00078       return result;
00079 
00080     __set_errno(save_errno);
00081     if (cmd == IPC_SET)
00082       {
00083        old.msg_perm.uid = buf->msg_perm.uid;
00084        old.msg_perm.gid = buf->msg_perm.gid;
00085        old.msg_perm.mode = buf->msg_perm.mode;
00086        old.msg_qbytes = buf->msg_qbytes;
00087        if (old.msg_perm.uid != buf->msg_perm.uid ||
00088            old.msg_perm.gid != buf->msg_perm.gid ||
00089            old.msg_qbytes != buf->msg_qbytes)
00090          {
00091            __set_errno (EINVAL);
00092            return -1;
00093          }
00094       }
00095     result = INLINE_SYSCALL (msgctl, 3, msqid, cmd, __ptrvalue (&old));
00096     if (result != -1 && cmd != IPC_SET)
00097       {
00098        memset(buf, 0, sizeof(*buf));
00099        buf->msg_perm.__key = old.msg_perm.__key;
00100        buf->msg_perm.uid = old.msg_perm.uid;
00101        buf->msg_perm.gid = old.msg_perm.gid;
00102        buf->msg_perm.cuid = old.msg_perm.cuid;
00103        buf->msg_perm.cgid = old.msg_perm.cgid;
00104        buf->msg_perm.mode = old.msg_perm.mode;
00105        buf->msg_perm.__seq = old.msg_perm.__seq;
00106        buf->msg_stime = old.msg_stime;
00107        buf->msg_rtime = old.msg_rtime;
00108        buf->msg_ctime = old.msg_ctime;
00109        buf->__msg_cbytes = old.__msg_cbytes;
00110        buf->msg_qnum = old.msg_qnum;
00111        buf->msg_qbytes = old.msg_qbytes;
00112        buf->msg_lspid = old.msg_lspid;
00113        buf->msg_lrpid = old.msg_lrpid;
00114       }
00115     return result;
00116   }
00117 #endif
00118 }
00119 
00120 #include <shlib-compat.h>
00121 versioned_symbol (libc, __new_msgctl, msgctl, GLIBC_2_2);