Update to LGPL v2.1.
[jlayton/glibc.git] / sysdeps / unix / sysv / linux / shmctl.c
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.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
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    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <errno.h>
21 #include <sys/shm.h>
22 #include <ipc_priv.h>
23
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>
30
31 #include "kernel-features.h"
32
33 struct __old_shmid_ds
34 {
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 */
46 };
47
48 struct __old_shminfo
49 {
50   int shmmax;
51   int shmmin;
52   int shmmni;
53   int shmseg;
54   int shmall;
55 };
56
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 *);
62
63 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
64 int
65 __old_shmctl (int shmid, int cmd, struct __old_shmid_ds *buf)
66 {
67   return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid,
68                          cmd, 0, CHECK_1_NULL_OK (buf));
69 }
70 compat_symbol (libc, __old_shmctl, shmctl, GLIBC_2_0);
71 #endif
72
73 int
74 __new_shmctl (int shmid, int cmd, struct shmid_ds *buf)
75 {
76 #if __ASSUME_IPC64 > 0
77   return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd | __IPC_64, 0, CHECK_1 (buf));
78 #else
79   switch (cmd) {
80     case SHM_STAT:
81     case IPC_STAT:
82     case IPC_SET:
83 #if __WORDSIZE != 32
84     case IPC_INFO:
85 #endif
86       break;
87     default:
88       return INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd, 0, CHECK_1 (buf));
89   }
90
91   {
92     int save_errno = errno, result;
93     struct __old_shmid_ds old;
94
95     /* Unfortunately there is no way how to find out for sure whether
96        we should use old or new shmctl.  */
97     result = INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd | __IPC_64, 0, CHECK_1 (buf));
98     if (result != -1 || errno != EINVAL)
99       return result;
100
101     __set_errno(save_errno);
102     if (cmd == IPC_SET)
103       {
104         old.shm_perm.uid = buf->shm_perm.uid;
105         old.shm_perm.gid = buf->shm_perm.gid;
106         old.shm_perm.mode = buf->shm_perm.mode;
107         if (old.shm_perm.uid != buf->shm_perm.uid ||
108             old.shm_perm.gid != buf->shm_perm.gid)
109           {
110             __set_errno (EINVAL);
111             return -1;
112           }
113       }
114     result = INLINE_SYSCALL (ipc, 5, IPCOP_shmctl, shmid, cmd, 0, __ptrvalue (&old));
115     if (result != -1 && (cmd == SHM_STAT || cmd == IPC_STAT))
116       {
117         memset(buf, 0, sizeof(*buf));
118         buf->shm_perm.__key = old.shm_perm.__key;
119         buf->shm_perm.uid = old.shm_perm.uid;
120         buf->shm_perm.gid = old.shm_perm.gid;
121         buf->shm_perm.cuid = old.shm_perm.cuid;
122         buf->shm_perm.cgid = old.shm_perm.cgid;
123         buf->shm_perm.mode = old.shm_perm.mode;
124         buf->shm_perm.__seq = old.shm_perm.__seq;
125         buf->shm_atime = old.shm_atime;
126         buf->shm_dtime = old.shm_dtime;
127         buf->shm_ctime = old.shm_ctime;
128         buf->shm_segsz = old.shm_segsz;
129         buf->shm_nattch = old.shm_nattch;
130         buf->shm_cpid = old.shm_cpid;
131         buf->shm_lpid = old.shm_lpid;
132       }
133 #if __WORDSIZE != 32
134     else if (result != -1 && cmd == IPC_INFO)
135       {
136         struct __old_shminfo *oldi = (struct __old_shminfo *)&old;
137         struct shminfo *i = (struct shminfo *)buf;
138
139         memset(i, 0, sizeof(*i));
140         i->shmmax = oldi->shmmax;
141         i->shmmin = oldi->shmmin;
142         i->shmmni = oldi->shmmni;
143         i->shmseg = oldi->shmseg;
144         i->shmall = oldi->shmall;
145       }
146 #endif
147     return result;
148   }
149 #endif
150 }
151
152 versioned_symbol (libc, __new_shmctl, shmctl, GLIBC_2_2);