(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / unix / sysv / linux / shmctl.c
blob5561cca36f1a7f1e970e509692a016c382e1b9d4
1 /* Copyright (C) 1995, 1997, 1998, 2000, 2003, 2004
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <sys/shm.h>
23 #include <ipc_priv.h>
25 #include <sysdep.h>
26 #include <string.h>
27 #include <sys/syscall.h>
28 #include <bits/wordsize.h>
29 #include <shlib-compat.h>
30 #include <bp-checks.h>
32 #include "kernel-features.h"
34 struct __old_shmid_ds
36 struct __old_ipc_perm shm_perm; /* operation permission struct */
37 int shm_segsz; /* size of segment in bytes */
38 __time_t shm_atime; /* time of last shmat() */
39 __time_t shm_dtime; /* time of last shmdt() */
40 __time_t shm_ctime; /* time of last change by shmctl() */
41 __ipc_pid_t shm_cpid; /* pid of creator */
42 __ipc_pid_t shm_lpid; /* pid of last shmop */
43 unsigned short int shm_nattch; /* number of current attaches */
44 unsigned short int __shm_npages; /* size of segment (pages) */
45 unsigned long int *__unbounded __shm_pages; /* array of ptrs to frames -> SHMMAX */
46 struct vm_area_struct *__unbounded __attaches; /* descriptors for attaches */
49 struct __old_shminfo
51 int shmmax;
52 int shmmin;
53 int shmmni;
54 int shmseg;
55 int shmall;
58 /* Provide operations to control over shared memory segments. */
59 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
60 int __old_shmctl (int, int, struct __old_shmid_ds *);
61 #endif
62 int __new_shmctl (int, int, struct shmid_ds *);
64 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
65 int
66 attribute_compat_text_section
67 __old_shmctl (int shmid, int cmd, struct __old_shmid_ds *buf)
69 return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid,
70 cmd, 0, CHECK_1_NULL_OK (buf));
72 compat_symbol (libc, __old_shmctl, shmctl, GLIBC_2_0);
73 #endif
75 int
76 __new_shmctl (int shmid, int cmd, struct shmid_ds *buf)
78 #if __ASSUME_IPC64 > 0
79 return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd | __IPC_64, 0,
80 CHECK_1 (buf));
81 #else
82 switch (cmd) {
83 case SHM_STAT:
84 case IPC_STAT:
85 case IPC_SET:
86 #if __WORDSIZE != 32
87 case IPC_INFO:
88 #endif
89 break;
90 default:
91 return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd, 0,
92 CHECK_1 (buf));
96 int save_errno = errno, result;
97 union
99 struct __old_shmid_ds ds;
100 struct __old_shminfo info;
101 } old;
103 /* Unfortunately there is no way how to find out for sure whether
104 we should use old or new shmctl. */
105 result = INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd | __IPC_64, 0,
106 CHECK_1 (buf));
107 if (result != -1 || errno != EINVAL)
108 return result;
110 __set_errno(save_errno);
111 if (cmd == IPC_SET)
113 old.ds.shm_perm.uid = buf->shm_perm.uid;
114 old.ds.shm_perm.gid = buf->shm_perm.gid;
115 old.ds.shm_perm.mode = buf->shm_perm.mode;
116 if (old.ds.shm_perm.uid != buf->shm_perm.uid ||
117 old.ds.shm_perm.gid != buf->shm_perm.gid)
119 __set_errno (EINVAL);
120 return -1;
123 result = INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd, 0,
124 __ptrvalue (&old.ds));
125 if (result != -1 && (cmd == SHM_STAT || cmd == IPC_STAT))
127 memset(buf, 0, sizeof(*buf));
128 buf->shm_perm.__key = old.ds.shm_perm.__key;
129 buf->shm_perm.uid = old.ds.shm_perm.uid;
130 buf->shm_perm.gid = old.ds.shm_perm.gid;
131 buf->shm_perm.cuid = old.ds.shm_perm.cuid;
132 buf->shm_perm.cgid = old.ds.shm_perm.cgid;
133 buf->shm_perm.mode = old.ds.shm_perm.mode;
134 buf->shm_perm.__seq = old.ds.shm_perm.__seq;
135 buf->shm_atime = old.ds.shm_atime;
136 buf->shm_dtime = old.ds.shm_dtime;
137 buf->shm_ctime = old.ds.shm_ctime;
138 buf->shm_segsz = old.ds.shm_segsz;
139 buf->shm_nattch = old.ds.shm_nattch;
140 buf->shm_cpid = old.ds.shm_cpid;
141 buf->shm_lpid = old.ds.shm_lpid;
143 #if __WORDSIZE != 32
144 else if (result != -1 && cmd == IPC_INFO)
146 struct shminfo *i = (struct shminfo *)buf;
148 memset(i, 0, sizeof(*i));
149 i->shmmax = old.info.shmmax;
150 i->shmmin = old.info.shmmin;
151 i->shmmni = old.info.shmmni;
152 i->shmseg = old.info.shmseg;
153 i->shmall = old.info.shmall;
155 #endif
156 return result;
158 #endif
161 versioned_symbol (libc, __new_shmctl, shmctl, GLIBC_2_2);