Update.
[glibc.git] / sysdeps / unix / sysv / linux / i386 / shmctl.c
blobca25ff0c7a3d2a84767492c1f1ae6a08a1b8bff2
1 /* Copyright (C) 1995, 1997, 1998, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <sys/shm.h>
22 #include <ipc_priv.h>
24 #include <sysdep.h>
25 #include <string.h>
26 #include <sys/syscall.h>
27 #include <bits/wordsize.h>
29 #include "kernel-features.h"
30 #include <shlib-compat.h>
32 struct __old_shmid_ds
34 struct __old_ipc_perm shm_perm; /* operation permission struct */
35 int shm_segsz; /* size of segment in bytes */
36 __time_t shm_atime; /* time of last shmat() */
37 __time_t shm_dtime; /* time of last shmdt() */
38 __time_t shm_ctime; /* time of last change by shmctl() */
39 __ipc_pid_t shm_cpid; /* pid of creator */
40 __ipc_pid_t shm_lpid; /* pid of last shmop */
41 unsigned short int shm_nattch; /* number of current attaches */
42 unsigned short int __shm_npages; /* size of segment (pages) */
43 unsigned long int *__shm_pages; /* array of ptrs to frames -> SHMMAX */
44 struct vm_area_struct *__attaches; /* descriptors for attaches */
47 struct __old_shminfo
49 int shmmax;
50 int shmmin;
51 int shmmni;
52 int shmseg;
53 int shmall;
56 #ifdef __NR_getuid32
57 # if __ASSUME_32BITUIDS == 0
58 /* This variable is shared with all files that need to check for 32bit
59 uids. */
60 extern int __libc_missing_32bit_uids;
61 # endif
62 #endif
64 /* Provide operations to control over shared memory segments. */
65 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
66 int __old_shmctl (int, int, struct __old_shmid_ds *);
67 #endif
68 int __new_shmctl (int, int, struct shmid_ds *);
70 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
71 int
72 __old_shmctl (int shmid, int cmd, struct __old_shmid_ds *buf)
74 return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd, 0, buf);
76 compat_symbol (libc, __old_shmctl, shmctl, GLIBC_2_0);
77 #endif
79 int
80 __new_shmctl (int shmid, int cmd, struct shmid_ds *buf)
82 #if __ASSUME_32BITUIDS > 0
83 return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd | __IPC_64, 0, buf);
84 #else
85 switch (cmd) {
86 case SHM_STAT:
87 case IPC_STAT:
88 case IPC_SET:
89 # if __WORDSIZE != 32
90 case IPC_INFO:
91 # endif
92 break;
93 default:
94 return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd, 0, buf);
98 struct __old_shmid_ds old;
99 int result;
101 # ifdef __NR_getuid32
102 if (__libc_missing_32bit_uids <= 0)
104 if (__libc_missing_32bit_uids < 0)
106 int save_errno = errno;
108 /* Test presence of new IPC by testing for getuid32 syscall. */
109 result = INLINE_SYSCALL (getuid32, 0);
110 if (result == -1 && errno == ENOSYS)
111 __libc_missing_32bit_uids = 1;
112 else
113 __libc_missing_32bit_uids = 0;
114 __set_errno(save_errno);
116 if (__libc_missing_32bit_uids <= 0)
117 return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd | __IPC_64,
118 0, buf);
120 # endif
122 if (cmd == IPC_SET)
124 old.shm_perm.uid = buf->shm_perm.uid;
125 old.shm_perm.gid = buf->shm_perm.gid;
126 old.shm_perm.mode = buf->shm_perm.mode;
127 if (old.shm_perm.uid != buf->shm_perm.uid ||
128 old.shm_perm.gid != buf->shm_perm.gid)
130 __set_errno (EINVAL);
131 return -1;
134 result = INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd, 0, &old);
135 if (result != -1 && (cmd == SHM_STAT || cmd == IPC_STAT))
137 memset(buf, 0, sizeof(*buf));
138 buf->shm_perm.__key = old.shm_perm.__key;
139 buf->shm_perm.uid = old.shm_perm.uid;
140 buf->shm_perm.gid = old.shm_perm.gid;
141 buf->shm_perm.cuid = old.shm_perm.cuid;
142 buf->shm_perm.cgid = old.shm_perm.cgid;
143 buf->shm_perm.mode = old.shm_perm.mode;
144 buf->shm_perm.__seq = old.shm_perm.__seq;
145 buf->shm_atime = old.shm_atime;
146 buf->shm_dtime = old.shm_dtime;
147 buf->shm_ctime = old.shm_ctime;
148 buf->shm_segsz = old.shm_segsz;
149 buf->shm_nattch = old.shm_nattch;
150 buf->shm_cpid = old.shm_cpid;
151 buf->shm_lpid = old.shm_lpid;
153 # if __WORDSIZE != 32
154 else if (result != -1 && cmd == IPC_INFO)
156 struct __old_shminfo *oldi = (struct __old_shminfo *)&old;
157 struct shminfo *i = (struct shminfo *)buf;
159 memset(i, 0, sizeof(*i));
160 i->shmmax = oldi->shmmax;
161 i->shmmin = oldi->shmmin;
162 i->shmmni = oldi->shmmni;
163 i->shmseg = oldi->shmseg;
164 i->shmall = oldi->shmall;
166 # endif
167 return result;
169 #endif
172 versioned_symbol (libc, __new_shmctl, shmctl, GLIBC_2_2);