Back to index

glibc  2.9
shmat.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995,1997,1998,1999,2000,2002 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 <unistd.h>
00026 #include <sys/syscall.h>
00027 #include <bp-checks.h>
00028 
00029 /* Attach the shared memory segment associated with SHMID to the data
00030    segment of the calling process.  SHMADDR and SHMFLG determine how
00031    and where the segment is attached.  */
00032 
00033 void *
00034 shmat (shmid, shmaddr, shmflg)
00035      int shmid;
00036      const void *shmaddr;
00037      int shmflg;
00038 {
00039   void *__unbounded result;
00040   void *__unbounded raddr;
00041 
00042 #if __BOUNDED_POINTERS__
00043   size_t length = ~0;
00044   struct shmid_ds shmds;
00045   /* It's unfortunate that we need to make another system call to get
00046      the shared memory segment length...  */
00047   if (shmctl (shmid, IPC_STAT, &shmds) == 0)
00048     length = shmds.shm_segsz;
00049 #endif
00050 
00051   result = (void *__unbounded) INLINE_SYSCALL (ipc, 5, IPCOP_shmat,
00052                                           shmid, shmflg,
00053                                           (long int) __ptrvalue (&raddr),
00054                                           __ptrvalue ((void *) shmaddr));
00055   if ((unsigned long) result <= -(unsigned long) SHMLBA)
00056     result = raddr;
00057 
00058   return BOUNDED_N (result, length);
00059 }