Back to index

glibc  2.9
shmctl.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/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 
00032 struct __old_shmid_ds
00033 {
00034   struct __old_ipc_perm shm_perm;  /* operation permission struct */
00035   int shm_segsz;                   /* size of segment in bytes */
00036   __time_t shm_atime;                     /* time of last shmat() */
00037   __time_t shm_dtime;                     /* time of last shmdt() */
00038   __time_t shm_ctime;                     /* time of last change by shmctl() */
00039   __ipc_pid_t shm_cpid;                   /* pid of creator */
00040   __ipc_pid_t shm_lpid;                   /* pid of last shmop */
00041   unsigned short int shm_nattch;   /* number of current attaches */
00042   unsigned short int __shm_npages; /* size of segment (pages) */
00043   unsigned long int *__unbounded __shm_pages; /* array of ptrs to frames -> SHMMAX */
00044   struct vm_area_struct *__unbounded __attaches; /* descriptors for attaches */
00045 };
00046 
00047 struct __old_shminfo
00048 {
00049   int shmmax;
00050   int shmmin;
00051   int shmmni;
00052   int shmseg;
00053   int shmall;
00054 };
00055 
00056 /* Provide operations to control over shared memory segments.  */
00057 int __new_shmctl (int, int, struct shmid_ds *);
00058 
00059 int
00060 __new_shmctl (int shmid, int cmd, struct shmid_ds *buf)
00061 {
00062 #if __ASSUME_32BITUIDS > 0
00063   return INLINE_SYSCALL (shmctl, 3, shmid, cmd | __IPC_64, CHECK_1 (buf));
00064 #else
00065   switch (cmd) {
00066     case SHM_STAT:
00067     case IPC_STAT:
00068     case IPC_SET:
00069     case IPC_INFO:
00070       break;
00071     default:
00072       return INLINE_SYSCALL (shmctl, 3, shmid, cmd, CHECK_1 (buf));
00073   }
00074 
00075   {
00076     int save_errno = errno, result;
00077     struct __old_shmid_ds old;
00078 
00079     /* Unfortunately there is no way how to find out for sure whether
00080        we should use old or new shmctl.  */
00081     result = INLINE_SYSCALL (shmctl, 3, shmid, cmd | __IPC_64, CHECK_1 (buf));
00082     if (result != -1 || errno != EINVAL)
00083       return result;
00084 
00085     __set_errno(save_errno);
00086     if (cmd == IPC_SET)
00087       {
00088        old.shm_perm.uid = buf->shm_perm.uid;
00089        old.shm_perm.gid = buf->shm_perm.gid;
00090        old.shm_perm.mode = buf->shm_perm.mode;
00091        if (old.shm_perm.uid != buf->shm_perm.uid ||
00092            old.shm_perm.gid != buf->shm_perm.gid)
00093          {
00094            __set_errno (EINVAL);
00095            return -1;
00096          }
00097       }
00098     result = INLINE_SYSCALL (shmctl, 3, shmid, cmd, __ptrvalue (&old));
00099     if (result != -1 && (cmd == SHM_STAT || cmd == IPC_STAT))
00100       {
00101        memset(buf, 0, sizeof(*buf));
00102        buf->shm_perm.__key = old.shm_perm.__key;
00103        buf->shm_perm.uid = old.shm_perm.uid;
00104        buf->shm_perm.gid = old.shm_perm.gid;
00105        buf->shm_perm.cuid = old.shm_perm.cuid;
00106        buf->shm_perm.cgid = old.shm_perm.cgid;
00107        buf->shm_perm.mode = old.shm_perm.mode;
00108        buf->shm_perm.__seq = old.shm_perm.__seq;
00109        buf->shm_atime = old.shm_atime;
00110        buf->shm_dtime = old.shm_dtime;
00111        buf->shm_ctime = old.shm_ctime;
00112        buf->shm_segsz = old.shm_segsz;
00113        buf->shm_nattch = old.shm_nattch;
00114        buf->shm_cpid = old.shm_cpid;
00115        buf->shm_lpid = old.shm_lpid;
00116       }
00117     else if (result != -1 && cmd == IPC_INFO)
00118       {
00119        struct __old_shminfo *oldi = (void *)&old;
00120        struct shminfo *i = (struct shminfo *)buf;
00121 
00122        memset(i, 0, sizeof(*i));
00123        i->shmmax = oldi->shmmax;
00124        i->shmmin = oldi->shmmin;
00125        i->shmmni = oldi->shmmni;
00126        i->shmseg = oldi->shmseg;
00127        i->shmall = oldi->shmall;
00128       }
00129     return result;
00130   }
00131 #endif
00132 }
00133 
00134 #include <shlib-compat.h>
00135 versioned_symbol (libc, __new_shmctl, shmctl, GLIBC_2_2);