Move setfsgid/setfsuid definitions to syscalls.list (bug 14138).
[glibc.git] / sysdeps / unix / sysv / linux / semctl.c
blobb20fb39c30f2c45331286ccdb7b38bc03dcc13e4
1 /* Copyright (C) 1995-2014 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 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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <stdarg.h>
21 #include <sys/sem.h>
22 #include <ipc_priv.h>
24 #include <sysdep.h>
25 #include <string.h>
26 #include <sys/syscall.h>
27 #include <shlib-compat.h>
29 #include <kernel-features.h>
31 struct __old_semid_ds
33 struct __old_ipc_perm sem_perm; /* operation permission struct */
34 __time_t sem_otime; /* last semop() time */
35 __time_t sem_ctime; /* last time changed by semctl() */
36 struct sem *__sembase; /* ptr to first semaphore in array */
37 struct sem_queue *__sem_pending; /* pending operations */
38 struct sem_queue *__sem_pending_last; /* last pending operation */
39 struct sem_undo *__undo; /* ondo requests on this array */
40 unsigned short int sem_nsems; /* number of semaphores in set */
43 /* Define a `union semun' suitable for Linux here. */
44 union semun
46 int val; /* value for SETVAL */
47 struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
48 unsigned short int *array; /* array for GETALL & SETALL */
49 struct seminfo *__buf; /* buffer for IPC_INFO */
50 struct __old_semid_ds *__old_buf;
53 /* Return identifier for array of NSEMS semaphores associated with
54 KEY. */
55 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
56 int __old_semctl (int semid, int semnum, int cmd, ...);
57 #endif
58 int __new_semctl (int semid, int semnum, int cmd, ...);
60 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
61 int
62 attribute_compat_text_section
63 __old_semctl (int semid, int semnum, int cmd, ...)
65 union semun arg;
66 va_list ap;
68 va_start (ap, cmd);
70 /* Get the argument only if required. */
71 arg.buf = NULL;
72 switch (cmd)
74 case SETVAL: /* arg.val */
75 case GETALL: /* arg.array */
76 case SETALL:
77 case IPC_STAT: /* arg.buf */
78 case IPC_SET:
79 case SEM_STAT:
80 case IPC_INFO: /* arg.__buf */
81 case SEM_INFO:
82 va_start (ap, cmd);
83 arg = va_arg (ap, union semun);
84 va_end (ap);
85 break;
88 va_end (ap);
90 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
91 &arg);
93 compat_symbol (libc, __old_semctl, semctl, GLIBC_2_0);
94 #endif
96 int
97 __new_semctl (int semid, int semnum, int cmd, ...)
99 union semun arg;
100 va_list ap;
102 va_start (ap, cmd);
104 /* Get the argument only if required. */
105 arg.buf = NULL;
106 switch (cmd)
108 case SETVAL: /* arg.val */
109 case GETALL: /* arg.array */
110 case SETALL:
111 case IPC_STAT: /* arg.buf */
112 case IPC_SET:
113 case SEM_STAT:
114 case IPC_INFO: /* arg.__buf */
115 case SEM_INFO:
116 va_start (ap, cmd);
117 arg = va_arg (ap, union semun);
118 va_end (ap);
119 break;
122 va_end (ap);
124 #if __ASSUME_IPC64 > 0
125 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
126 &arg);
127 #else
128 switch (cmd)
130 case SEM_STAT:
131 case IPC_STAT:
132 case IPC_SET:
133 break;
134 default:
135 return INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
136 &arg);
140 int save_errno = errno, result;
141 struct __old_semid_ds old;
142 struct semid_ds *buf;
144 /* Unfortunately there is no way how to find out for sure whether
145 we should use old or new semctl. */
146 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd | __IPC_64,
147 &arg);
148 if (result != -1 || errno != EINVAL)
149 return result;
151 __set_errno(save_errno);
152 buf = arg.buf;
153 arg.__old_buf = &old;
154 if (cmd == IPC_SET)
156 old.sem_perm.uid = buf->sem_perm.uid;
157 old.sem_perm.gid = buf->sem_perm.gid;
158 old.sem_perm.mode = buf->sem_perm.mode;
159 if (old.sem_perm.uid != buf->sem_perm.uid ||
160 old.sem_perm.gid != buf->sem_perm.gid)
162 __set_errno (EINVAL);
163 return -1;
166 result = INLINE_SYSCALL (ipc, 5, IPCOP_semctl, semid, semnum, cmd,
167 &arg);
168 if (result != -1 && cmd != IPC_SET)
170 memset(buf, 0, sizeof(*buf));
171 buf->sem_perm.__key = old.sem_perm.__key;
172 buf->sem_perm.uid = old.sem_perm.uid;
173 buf->sem_perm.gid = old.sem_perm.gid;
174 buf->sem_perm.cuid = old.sem_perm.cuid;
175 buf->sem_perm.cgid = old.sem_perm.cgid;
176 buf->sem_perm.mode = old.sem_perm.mode;
177 buf->sem_perm.__seq = old.sem_perm.__seq;
178 buf->sem_otime = old.sem_otime;
179 buf->sem_ctime = old.sem_ctime;
180 buf->sem_nsems = old.sem_nsems;
182 return result;
184 #endif
187 versioned_symbol (libc, __new_semctl, semctl, GLIBC_2_2);