Back to index

glibc  2.9
shmctl.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995,1997,1998,2000,2003,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/shm.h>
00023 #include <ipc_priv.h>
00024 
00025 #include <sysdep.h>
00026 #include <string.h>
00027 #include <sys/syscall.h>
00028 #include <bits/wordsize.h>
00029 #include <shlib-compat.h>
00030 #include <bp-checks.h>
00031 
00032 #include <kernel-features.h>
00033 
00034 struct __old_shmid_ds
00035 {
00036   struct __old_ipc_perm shm_perm;  /* operation permission struct */
00037   int shm_segsz;                   /* size of segment in bytes */
00038   __time_t shm_atime;                     /* time of last shmat() */
00039   __time_t shm_dtime;                     /* time of last shmdt() */
00040   __time_t shm_ctime;                     /* time of last change by shmctl() */
00041   __ipc_pid_t shm_cpid;                   /* pid of creator */
00042   __ipc_pid_t shm_lpid;                   /* pid of last shmop */
00043   unsigned short int shm_nattch;   /* number of current attaches */
00044   unsigned short int __shm_npages; /* size of segment (pages) */
00045   unsigned long int *__unbounded __shm_pages; /* array of ptrs to frames -> SHMMAX */
00046   struct vm_area_struct *__unbounded __attaches; /* descriptors for attaches */
00047 };
00048 
00049 struct __old_shminfo
00050 {
00051   int shmmax;
00052   int shmmin;
00053   int shmmni;
00054   int shmseg;
00055   int shmall;
00056 };
00057 
00058 /* Provide operations to control over shared memory segments.  */
00059 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
00060 int __old_shmctl (int, int, struct __old_shmid_ds *);
00061 #endif
00062 int __new_shmctl (int, int, struct shmid_ds *);
00063 
00064 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
00065 int
00066 attribute_compat_text_section
00067 __old_shmctl (int shmid, int cmd, struct __old_shmid_ds *buf)
00068 {
00069   return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid,
00070                       cmd, 0, CHECK_1_NULL_OK (buf));
00071 }
00072 compat_symbol (libc, __old_shmctl, shmctl, GLIBC_2_0);
00073 #endif
00074 
00075 int
00076 __new_shmctl (int shmid, int cmd, struct shmid_ds *buf)
00077 {
00078 #if __ASSUME_IPC64 > 0
00079   return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd | __IPC_64, 0,
00080                       CHECK_1 (buf));
00081 #else
00082   switch (cmd) {
00083     case SHM_STAT:
00084     case IPC_STAT:
00085     case IPC_SET:
00086 #if __WORDSIZE != 32
00087     case IPC_INFO:
00088 #endif
00089       break;
00090     default:
00091       return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd, 0,
00092                           CHECK_1 (buf));
00093   }
00094 
00095   {
00096     int save_errno = errno, result;
00097     union
00098       {
00099        struct __old_shmid_ds ds;
00100        struct __old_shminfo info;
00101       } old;
00102 
00103     /* Unfortunately there is no way how to find out for sure whether
00104        we should use old or new shmctl.  */
00105     result = INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd | __IPC_64, 0,
00106                           CHECK_1 (buf));
00107     if (result != -1 || errno != EINVAL)
00108       return result;
00109 
00110     __set_errno(save_errno);
00111     if (cmd == IPC_SET)
00112       {
00113        old.ds.shm_perm.uid = buf->shm_perm.uid;
00114        old.ds.shm_perm.gid = buf->shm_perm.gid;
00115        old.ds.shm_perm.mode = buf->shm_perm.mode;
00116        if (old.ds.shm_perm.uid != buf->shm_perm.uid ||
00117            old.ds.shm_perm.gid != buf->shm_perm.gid)
00118          {
00119            __set_errno (EINVAL);
00120            return -1;
00121          }
00122       }
00123     result = INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd, 0,
00124                           __ptrvalue (&old.ds));
00125     if (result != -1 && (cmd == SHM_STAT || cmd == IPC_STAT))
00126       {
00127        memset(buf, 0, sizeof(*buf));
00128        buf->shm_perm.__key = old.ds.shm_perm.__key;
00129        buf->shm_perm.uid = old.ds.shm_perm.uid;
00130        buf->shm_perm.gid = old.ds.shm_perm.gid;
00131        buf->shm_perm.cuid = old.ds.shm_perm.cuid;
00132        buf->shm_perm.cgid = old.ds.shm_perm.cgid;
00133        buf->shm_perm.mode = old.ds.shm_perm.mode;
00134        buf->shm_perm.__seq = old.ds.shm_perm.__seq;
00135        buf->shm_atime = old.ds.shm_atime;
00136        buf->shm_dtime = old.ds.shm_dtime;
00137        buf->shm_ctime = old.ds.shm_ctime;
00138        buf->shm_segsz = old.ds.shm_segsz;
00139        buf->shm_nattch = old.ds.shm_nattch;
00140        buf->shm_cpid = old.ds.shm_cpid;
00141        buf->shm_lpid = old.ds.shm_lpid;
00142       }
00143 #if __WORDSIZE != 32
00144     else if (result != -1 && cmd == IPC_INFO)
00145       {
00146        struct shminfo *i = (struct shminfo *)buf;
00147 
00148        memset(i, 0, sizeof(*i));
00149        i->shmmax = old.info.shmmax;
00150        i->shmmin = old.info.shmmin;
00151        i->shmmni = old.info.shmmni;
00152        i->shmseg = old.info.shmseg;
00153        i->shmall = old.info.shmall;
00154       }
00155 #endif
00156     return result;
00157   }
00158 #endif
00159 }
00160 
00161 versioned_symbol (libc, __new_shmctl, shmctl, GLIBC_2_2);