Back to index

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