Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / unix / sysv / linux / shmctl.c
blobdbc965e29ca9c701c12abbcfca801bb76c2a2571
1 /* Copyright (C) 1995,1997,1998,2000,2003,2004,2006
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, see
18 <http://www.gnu.org/licenses/>. */
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>
28 #include <shlib-compat.h>
29 #include <bp-checks.h>
31 #include <kernel-features.h>
33 struct __old_shmid_ds
35 struct __old_ipc_perm shm_perm; /* operation permission struct */
36 int shm_segsz; /* size of segment in bytes */
37 __time_t shm_atime; /* time of last shmat() */
38 __time_t shm_dtime; /* time of last shmdt() */
39 __time_t shm_ctime; /* time of last change by shmctl() */
40 __ipc_pid_t shm_cpid; /* pid of creator */
41 __ipc_pid_t shm_lpid; /* pid of last shmop */
42 unsigned short int shm_nattch; /* number of current attaches */
43 unsigned short int __shm_npages; /* size of segment (pages) */
44 unsigned long int *__unbounded __shm_pages; /* array of ptrs to frames -> SHMMAX */
45 struct vm_area_struct *__unbounded __attaches; /* descriptors for attaches */
48 struct __old_shminfo
50 int shmmax;
51 int shmmin;
52 int shmmni;
53 int shmseg;
54 int shmall;
57 /* Provide operations to control over shared memory segments. */
58 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
59 int __old_shmctl (int, int, struct __old_shmid_ds *);
60 #endif
61 int __new_shmctl (int, int, struct shmid_ds *);
63 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
64 int
65 attribute_compat_text_section
66 __old_shmctl (int shmid, int cmd, struct __old_shmid_ds *buf)
68 return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid,
69 cmd, 0, CHECK_1_NULL_OK (buf));
71 compat_symbol (libc, __old_shmctl, shmctl, GLIBC_2_0);
72 #endif
74 int
75 __new_shmctl (int shmid, int cmd, struct shmid_ds *buf)
77 #if __ASSUME_IPC64 > 0
78 return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd | __IPC_64, 0,
79 CHECK_1 (buf));
80 #else
81 switch (cmd) {
82 case SHM_STAT:
83 case IPC_STAT:
84 case IPC_SET:
85 #if __WORDSIZE != 32
86 case IPC_INFO:
87 #endif
88 break;
89 default:
90 return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd, 0,
91 CHECK_1 (buf));
95 int save_errno = errno, result;
96 union
98 struct __old_shmid_ds ds;
99 struct __old_shminfo info;
100 } old;
102 /* Unfortunately there is no way how to find out for sure whether
103 we should use old or new shmctl. */
104 result = INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd | __IPC_64, 0,
105 CHECK_1 (buf));
106 if (result != -1 || errno != EINVAL)
107 return result;
109 __set_errno(save_errno);
110 if (cmd == IPC_SET)
112 old.ds.shm_perm.uid = buf->shm_perm.uid;
113 old.ds.shm_perm.gid = buf->shm_perm.gid;
114 old.ds.shm_perm.mode = buf->shm_perm.mode;
115 if (old.ds.shm_perm.uid != buf->shm_perm.uid ||
116 old.ds.shm_perm.gid != buf->shm_perm.gid)
118 __set_errno (EINVAL);
119 return -1;
122 result = INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd, 0,
123 __ptrvalue (&old.ds));
124 if (result != -1 && (cmd == SHM_STAT || cmd == IPC_STAT))
126 memset(buf, 0, sizeof(*buf));
127 buf->shm_perm.__key = old.ds.shm_perm.__key;
128 buf->shm_perm.uid = old.ds.shm_perm.uid;
129 buf->shm_perm.gid = old.ds.shm_perm.gid;
130 buf->shm_perm.cuid = old.ds.shm_perm.cuid;
131 buf->shm_perm.cgid = old.ds.shm_perm.cgid;
132 buf->shm_perm.mode = old.ds.shm_perm.mode;
133 buf->shm_perm.__seq = old.ds.shm_perm.__seq;
134 buf->shm_atime = old.ds.shm_atime;
135 buf->shm_dtime = old.ds.shm_dtime;
136 buf->shm_ctime = old.ds.shm_ctime;
137 buf->shm_segsz = old.ds.shm_segsz;
138 buf->shm_nattch = old.ds.shm_nattch;
139 buf->shm_cpid = old.ds.shm_cpid;
140 buf->shm_lpid = old.ds.shm_lpid;
142 #if __WORDSIZE != 32
143 else if (result != -1 && cmd == IPC_INFO)
145 struct shminfo *i = (struct shminfo *)buf;
147 memset(i, 0, sizeof(*i));
148 i->shmmax = old.info.shmmax;
149 i->shmmin = old.info.shmmin;
150 i->shmmni = old.info.shmmni;
151 i->shmseg = old.info.shmseg;
152 i->shmall = old.info.shmall;
154 #endif
155 return result;
157 #endif
160 versioned_symbol (libc, __new_shmctl, shmctl, GLIBC_2_2);