chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / sysdeps / unix / sysv / linux / i386 / semctl.c
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.
5
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.
10
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.
15
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.  */
20
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <sys/sem.h>
24 #include <ipc_priv.h>
25
26 #include <sysdep.h>
27 #include <string.h>
28 #include <sys/syscall.h>
29
30 #include <kernel-features.h>
31 #include <shlib-compat.h>
32
33 struct __old_semid_ds
34 {
35   struct __old_ipc_perm sem_perm;       /* operation permission struct */
36   __time_t sem_otime;                   /* last semop() time */
37   __time_t sem_ctime;                   /* last time changed by semctl() */
38   struct sem *__sembase;                /* ptr to first semaphore in array */
39   struct sem_queue *__sem_pending;      /* pending operations */
40   struct sem_queue *__sem_pending_last; /* last pending operation */
41   struct sem_undo *__undo;              /* ondo requests on this array */
42   unsigned short int sem_nsems;         /* number of semaphores in set */
43 };
44
45 /* Define a `union semun' suitable for Linux here.  */
46 union semun
47 {
48   int val;                      /* value for SETVAL */
49   struct semid_ds *buf;         /* buffer for IPC_STAT & IPC_SET */
50   unsigned short int *array;    /* array for GETALL & SETALL */
51   struct seminfo *__buf;        /* buffer for IPC_INFO */
52   struct __old_semid_ds *__old_buf;
53 };
54
55 #include <bp-checks.h>
56 #include <bp-semctl.h>          /* definition of CHECK_SEMCTL needs union semum */
57
58 #ifdef __NR_getuid32
59 # if __ASSUME_32BITUIDS == 0
60 /* This variable is shared with all files that need to check for 32bit
61    uids.  */
62 extern int __libc_missing_32bit_uids;
63 # endif
64 #endif
65
66 /* Return identifier for array of NSEMS semaphores associated with
67    KEY.  */
68 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
69 int __old_semctl (int semid, int semnum, int cmd, ...);
70 #endif
71 int __new_semctl (int semid, int semnum, int cmd, ...);
72
73 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
74 int
75 attribute_compat_text_section
76 __old_semctl (int semid, int semnum, int cmd, ...)
77 {
78   union semun arg;
79   va_list ap;
80
81   va_start (ap, cmd);
82
83   /* Get the argument.  */
84   arg = va_arg (ap, union semun);
85
86   va_end (ap);
87
88   return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
89                          CHECK_SEMCTL (&arg, semid, cmd));
90 }
91 compat_symbol (libc, __old_semctl, semctl, GLIBC_2_0);
92 #endif
93
94 int
95 __new_semctl (int semid, int semnum, int cmd, ...)
96 {
97   union semun arg;
98   va_list ap;
99
100   va_start (ap, cmd);
101
102   /* Get the argument.  */
103   arg = va_arg (ap, union semun);
104
105   va_end (ap);
106
107 #if __ASSUME_32BITUIDS > 0
108   return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
109                          CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
110 #else
111   switch (cmd) {
112     case SEM_STAT:
113     case IPC_STAT:
114     case IPC_SET:
115       break;
116     default:
117       return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
118                              CHECK_SEMCTL (&arg, semid, cmd));
119   }
120
121   {
122     int result;
123     struct __old_semid_ds old;
124     struct semid_ds *buf;
125
126 #ifdef __NR_getuid32
127     if (__libc_missing_32bit_uids <= 0)
128       {
129         if (__libc_missing_32bit_uids < 0)
130           {
131             int save_errno = errno;
132
133             /* Test presence of new IPC by testing for getuid32 syscall.  */
134             result = INLINE_SYSCALL (getuid32, 0);
135             if (result == -1 && errno == ENOSYS)
136               __libc_missing_32bit_uids = 1;
137             else
138               __libc_missing_32bit_uids = 0;
139             __set_errno(save_errno);
140           }
141         if (__libc_missing_32bit_uids <= 0)
142           {
143             result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
144                                      CHECK_SEMCTL (&arg, semid, cmd | __IPC_64));
145             return result;
146           }
147       }
148 #endif
149
150     buf = arg.buf;
151     arg.__old_buf = &old;
152     if (cmd == IPC_SET)
153       {
154         old.sem_perm.uid = buf->sem_perm.uid;
155         old.sem_perm.gid = buf->sem_perm.gid;
156         old.sem_perm.mode = buf->sem_perm.mode;
157         if (old.sem_perm.uid != buf->sem_perm.uid ||
158             old.sem_perm.gid != buf->sem_perm.gid)
159           {
160             __set_errno (EINVAL);
161             return -1;
162           }
163       }
164     result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
165                              CHECK_SEMCTL (&arg, semid, cmd));
166     if (result != -1 && cmd != IPC_SET)
167       {
168         memset(buf, 0, sizeof(*buf));
169         buf->sem_perm.__key = old.sem_perm.__key;
170         buf->sem_perm.uid = old.sem_perm.uid;
171         buf->sem_perm.gid = old.sem_perm.gid;
172         buf->sem_perm.cuid = old.sem_perm.cuid;
173         buf->sem_perm.cgid = old.sem_perm.cgid;
174         buf->sem_perm.mode = old.sem_perm.mode;
175         buf->sem_perm.__seq = old.sem_perm.__seq;
176         buf->sem_otime = old.sem_otime;
177         buf->sem_ctime = old.sem_ctime;
178         buf->sem_nsems = old.sem_nsems;
179       }
180     return result;
181   }
182 #endif
183 }
184
185 versioned_symbol (libc, __new_semctl, semctl, GLIBC_2_2);