sysv_sem.c revision 1.69 1 1.69 dsl /* $NetBSD: sysv_sem.c,v 1.69 2007/06/17 10:27:16 dsl Exp $ */
2 1.33 thorpej
3 1.33 thorpej /*-
4 1.33 thorpej * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.33 thorpej * All rights reserved.
6 1.33 thorpej *
7 1.33 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.33 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.33 thorpej * NASA Ames Research Center.
10 1.33 thorpej *
11 1.33 thorpej * Redistribution and use in source and binary forms, with or without
12 1.33 thorpej * modification, are permitted provided that the following conditions
13 1.33 thorpej * are met:
14 1.33 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.33 thorpej * notice, this list of conditions and the following disclaimer.
16 1.33 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.33 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.33 thorpej * documentation and/or other materials provided with the distribution.
19 1.33 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.33 thorpej * must display the following acknowledgement:
21 1.33 thorpej * This product includes software developed by the NetBSD
22 1.33 thorpej * Foundation, Inc. and its contributors.
23 1.33 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.33 thorpej * contributors may be used to endorse or promote products derived
25 1.33 thorpej * from this software without specific prior written permission.
26 1.33 thorpej *
27 1.33 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.33 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.33 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.33 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.33 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.33 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.33 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.33 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.33 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.33 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.33 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.33 thorpej */
39 1.9 cgd
40 1.1 cgd /*
41 1.1 cgd * Implementation of SVID semaphores
42 1.1 cgd *
43 1.33 thorpej * Author: Daniel Boulet
44 1.1 cgd *
45 1.1 cgd * This software is provided ``AS IS'' without any warranties of any kind.
46 1.1 cgd */
47 1.42 lukem
48 1.42 lukem #include <sys/cdefs.h>
49 1.69 dsl __KERNEL_RCSID(0, "$NetBSD: sysv_sem.c,v 1.69 2007/06/17 10:27:16 dsl Exp $");
50 1.31 tron
51 1.32 tron #define SYSVSEM
52 1.1 cgd
53 1.3 mycroft #include <sys/param.h>
54 1.3 mycroft #include <sys/kernel.h>
55 1.3 mycroft #include <sys/sem.h>
56 1.38 simonb #include <sys/sysctl.h>
57 1.57 chs #include <sys/malloc.h>
58 1.38 simonb #include <sys/mount.h> /* XXX for <sys/syscallargs.h> */
59 1.10 cgd #include <sys/syscallargs.h>
60 1.61 elad #include <sys/kauth.h>
61 1.25 christos
62 1.48 jdolecek static int semtot = 0;
63 1.48 jdolecek struct semid_ds *sema; /* semaphore id pool */
64 1.48 jdolecek static struct __sem *sem; /* semaphore pool */
65 1.48 jdolecek static struct sem_undo *semu_list; /* list of active undo structures */
66 1.48 jdolecek static int *semu; /* undo structure pool */
67 1.1 cgd
68 1.27 christos #ifdef SEM_DEBUG
69 1.28 christos #define SEM_PRINTF(a) printf a
70 1.27 christos #else
71 1.27 christos #define SEM_PRINTF(a)
72 1.27 christos #endif
73 1.27 christos
74 1.53 junyoung struct sem_undo *semu_alloc(struct proc *);
75 1.53 junyoung int semundo_adjust(struct proc *, struct sem_undo **, int, int, int);
76 1.53 junyoung void semundo_clear(int, int);
77 1.25 christos
78 1.37 sommerfe /*
79 1.37 sommerfe * XXXSMP Once we go MP, there needs to be a lock for the semaphore system.
80 1.37 sommerfe * Until then, we're saved by being a non-preemptive kernel.
81 1.37 sommerfe */
82 1.37 sommerfe
83 1.25 christos void
84 1.59 thorpej seminit(void)
85 1.1 cgd {
86 1.48 jdolecek int i, sz;
87 1.48 jdolecek vaddr_t v;
88 1.1 cgd
89 1.48 jdolecek /* Allocate pageable memory for our structures */
90 1.51 enami sz = seminfo.semmni * sizeof(struct semid_ds) +
91 1.51 enami seminfo.semmns * sizeof(struct __sem) +
92 1.51 enami seminfo.semmnu * seminfo.semusz;
93 1.56 yamt v = uvm_km_alloc(kernel_map, round_page(sz), 0,
94 1.56 yamt UVM_KMF_WIRED|UVM_KMF_ZERO);
95 1.56 yamt if (v == 0)
96 1.48 jdolecek panic("sysv_sem: cannot allocate memory");
97 1.48 jdolecek sema = (void *)v;
98 1.48 jdolecek sem = (void *)(sema + seminfo.semmni);
99 1.48 jdolecek semu = (void *)(sem + seminfo.semmns);
100 1.5 mycroft
101 1.5 mycroft for (i = 0; i < seminfo.semmni; i++) {
102 1.33 thorpej sema[i]._sem_base = 0;
103 1.5 mycroft sema[i].sem_perm.mode = 0;
104 1.5 mycroft }
105 1.5 mycroft for (i = 0; i < seminfo.semmnu; i++) {
106 1.35 augustss struct sem_undo *suptr = SEMU(i);
107 1.5 mycroft suptr->un_proc = NULL;
108 1.5 mycroft }
109 1.5 mycroft semu_list = NULL;
110 1.44 christos exithook_establish(semexit, NULL);
111 1.1 cgd }
112 1.1 cgd
113 1.1 cgd /*
114 1.37 sommerfe * Placebo.
115 1.1 cgd */
116 1.1 cgd
117 1.1 cgd int
118 1.66 yamt sys_semconfig(struct lwp *l, void *v, register_t *retval)
119 1.23 thorpej {
120 1.51 enami
121 1.5 mycroft *retval = 0;
122 1.37 sommerfe return 0;
123 1.1 cgd }
124 1.1 cgd
125 1.1 cgd /*
126 1.1 cgd * Allocate a new sem_undo structure for a process
127 1.1 cgd * (returns ptr to structure or NULL if no more room)
128 1.1 cgd */
129 1.1 cgd
130 1.1 cgd struct sem_undo *
131 1.59 thorpej semu_alloc(struct proc *p)
132 1.1 cgd {
133 1.35 augustss int i;
134 1.35 augustss struct sem_undo *suptr;
135 1.35 augustss struct sem_undo **supptr;
136 1.5 mycroft int attempt;
137 1.1 cgd
138 1.1 cgd /*
139 1.5 mycroft * Try twice to allocate something.
140 1.5 mycroft * (we'll purge any empty structures after the first pass so
141 1.5 mycroft * two passes are always enough)
142 1.1 cgd */
143 1.1 cgd
144 1.5 mycroft for (attempt = 0; attempt < 2; attempt++) {
145 1.5 mycroft /*
146 1.5 mycroft * Look for a free structure.
147 1.5 mycroft * Fill it in and return it if we find one.
148 1.5 mycroft */
149 1.5 mycroft
150 1.5 mycroft for (i = 0; i < seminfo.semmnu; i++) {
151 1.5 mycroft suptr = SEMU(i);
152 1.5 mycroft if (suptr->un_proc == NULL) {
153 1.5 mycroft suptr->un_next = semu_list;
154 1.5 mycroft semu_list = suptr;
155 1.5 mycroft suptr->un_cnt = 0;
156 1.5 mycroft suptr->un_proc = p;
157 1.51 enami return (suptr);
158 1.5 mycroft }
159 1.5 mycroft }
160 1.1 cgd
161 1.5 mycroft /*
162 1.5 mycroft * We didn't find a free one, if this is the first attempt
163 1.5 mycroft * then try to free some structures.
164 1.5 mycroft */
165 1.5 mycroft
166 1.5 mycroft if (attempt == 0) {
167 1.5 mycroft /* All the structures are in use - try to free some */
168 1.5 mycroft int did_something = 0;
169 1.5 mycroft
170 1.5 mycroft supptr = &semu_list;
171 1.5 mycroft while ((suptr = *supptr) != NULL) {
172 1.5 mycroft if (suptr->un_cnt == 0) {
173 1.5 mycroft suptr->un_proc = NULL;
174 1.5 mycroft *supptr = suptr->un_next;
175 1.5 mycroft did_something = 1;
176 1.5 mycroft } else
177 1.52 enami supptr = &suptr->un_next;
178 1.5 mycroft }
179 1.5 mycroft
180 1.5 mycroft /* If we didn't free anything then just give-up */
181 1.5 mycroft if (!did_something)
182 1.51 enami return (NULL);
183 1.5 mycroft } else {
184 1.5 mycroft /*
185 1.5 mycroft * The second pass failed even though we freed
186 1.5 mycroft * something after the first pass!
187 1.5 mycroft * This is IMPOSSIBLE!
188 1.5 mycroft */
189 1.5 mycroft panic("semu_alloc - second attempt failed");
190 1.5 mycroft }
191 1.1 cgd }
192 1.25 christos return NULL;
193 1.1 cgd }
194 1.1 cgd
195 1.1 cgd /*
196 1.1 cgd * Adjust a particular entry for a particular proc
197 1.1 cgd */
198 1.1 cgd
199 1.1 cgd int
200 1.59 thorpej semundo_adjust(struct proc *p, struct sem_undo **supptr, int semid, int semnum,
201 1.59 thorpej int adjval)
202 1.1 cgd {
203 1.35 augustss struct sem_undo *suptr;
204 1.35 augustss struct undo *sunptr;
205 1.5 mycroft int i;
206 1.1 cgd
207 1.51 enami /*
208 1.51 enami * Look for and remember the sem_undo if the caller doesn't
209 1.51 enami * provide it
210 1.51 enami */
211 1.1 cgd
212 1.5 mycroft suptr = *supptr;
213 1.4 mycroft if (suptr == NULL) {
214 1.52 enami for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next)
215 1.52 enami if (suptr->un_proc == p)
216 1.5 mycroft break;
217 1.52 enami
218 1.5 mycroft if (suptr == NULL) {
219 1.5 mycroft suptr = semu_alloc(p);
220 1.5 mycroft if (suptr == NULL)
221 1.51 enami return (ENOSPC);
222 1.5 mycroft }
223 1.52 enami *supptr = suptr;
224 1.1 cgd }
225 1.1 cgd
226 1.6 mycroft /*
227 1.51 enami * Look for the requested entry and adjust it (delete if
228 1.51 enami * adjval becomes 0).
229 1.6 mycroft */
230 1.6 mycroft sunptr = &suptr->un_ent[0];
231 1.5 mycroft for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
232 1.6 mycroft if (sunptr->un_id != semid || sunptr->un_num != semnum)
233 1.6 mycroft continue;
234 1.52 enami sunptr->un_adjval += adjval;
235 1.6 mycroft if (sunptr->un_adjval == 0) {
236 1.6 mycroft suptr->un_cnt--;
237 1.6 mycroft if (i < suptr->un_cnt)
238 1.6 mycroft suptr->un_ent[i] =
239 1.6 mycroft suptr->un_ent[suptr->un_cnt];
240 1.5 mycroft }
241 1.51 enami return (0);
242 1.1 cgd }
243 1.1 cgd
244 1.5 mycroft /* Didn't find the right entry - create it */
245 1.11 mycroft if (suptr->un_cnt == SEMUME)
246 1.51 enami return (EINVAL);
247 1.11 mycroft
248 1.11 mycroft sunptr = &suptr->un_ent[suptr->un_cnt];
249 1.11 mycroft suptr->un_cnt++;
250 1.11 mycroft sunptr->un_adjval = adjval;
251 1.11 mycroft sunptr->un_id = semid;
252 1.11 mycroft sunptr->un_num = semnum;
253 1.51 enami return (0);
254 1.1 cgd }
255 1.1 cgd
256 1.1 cgd void
257 1.59 thorpej semundo_clear(int semid, int semnum)
258 1.1 cgd {
259 1.35 augustss struct sem_undo *suptr;
260 1.52 enami struct undo *sunptr, *sunend;
261 1.1 cgd
262 1.52 enami for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next)
263 1.52 enami for (sunptr = &suptr->un_ent[0],
264 1.52 enami sunend = sunptr + suptr->un_cnt; sunptr < sunend;) {
265 1.6 mycroft if (sunptr->un_id == semid) {
266 1.6 mycroft if (semnum == -1 || sunptr->un_num == semnum) {
267 1.6 mycroft suptr->un_cnt--;
268 1.52 enami sunend--;
269 1.52 enami if (sunptr != sunend)
270 1.52 enami *sunptr = *sunend;
271 1.52 enami if (semnum != -1)
272 1.52 enami break;
273 1.52 enami else
274 1.52 enami continue;
275 1.6 mycroft }
276 1.6 mycroft }
277 1.52 enami sunptr++;
278 1.6 mycroft }
279 1.1 cgd }
280 1.1 cgd
281 1.1 cgd int
282 1.59 thorpej sys_____semctl13(struct lwp *l, void *v, register_t *retval)
283 1.23 thorpej {
284 1.34 christos struct sys_____semctl13_args /* {
285 1.10 cgd syscallarg(int) semid;
286 1.10 cgd syscallarg(int) semnum;
287 1.10 cgd syscallarg(int) cmd;
288 1.34 christos syscallarg(union __semun *) arg;
289 1.23 thorpej } */ *uap = v;
290 1.33 thorpej struct semid_ds sembuf;
291 1.33 thorpej int cmd, error;
292 1.34 christos void *pass_arg;
293 1.34 christos union __semun karg;
294 1.33 thorpej
295 1.33 thorpej cmd = SCARG(uap, cmd);
296 1.33 thorpej
297 1.69 dsl pass_arg = get_semctl_arg(cmd, &sembuf, &karg);
298 1.33 thorpej
299 1.34 christos if (pass_arg) {
300 1.34 christos error = copyin(SCARG(uap, arg), &karg, sizeof(karg));
301 1.33 thorpej if (error)
302 1.34 christos return error;
303 1.34 christos if (cmd == IPC_SET) {
304 1.34 christos error = copyin(karg.buf, &sembuf, sizeof(sembuf));
305 1.34 christos if (error)
306 1.34 christos return (error);
307 1.34 christos }
308 1.33 thorpej }
309 1.33 thorpej
310 1.63 ad error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd,
311 1.33 thorpej pass_arg, retval);
312 1.33 thorpej
313 1.33 thorpej if (error == 0 && cmd == IPC_STAT)
314 1.34 christos error = copyout(&sembuf, karg.buf, sizeof(sembuf));
315 1.33 thorpej
316 1.33 thorpej return (error);
317 1.33 thorpej }
318 1.33 thorpej
319 1.33 thorpej int
320 1.63 ad semctl1(struct lwp *l, int semid, int semnum, int cmd, void *v,
321 1.59 thorpej register_t *retval)
322 1.33 thorpej {
323 1.63 ad kauth_cred_t cred = l->l_cred;
324 1.33 thorpej union __semun *arg = v;
325 1.33 thorpej struct semid_ds *sembuf = v, *semaptr;
326 1.33 thorpej int i, error, ix;
327 1.1 cgd
328 1.27 christos SEM_PRINTF(("call to semctl(%d, %d, %d, %p)\n",
329 1.33 thorpej semid, semnum, cmd, v));
330 1.1 cgd
331 1.33 thorpej ix = IPCID_TO_IX(semid);
332 1.49 jdolecek if (ix < 0 || ix >= seminfo.semmni)
333 1.33 thorpej return (EINVAL);
334 1.6 mycroft
335 1.33 thorpej semaptr = &sema[ix];
336 1.6 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
337 1.33 thorpej semaptr->sem_perm._seq != IPCID_TO_SEQ(semid))
338 1.33 thorpej return (EINVAL);
339 1.1 cgd
340 1.6 mycroft switch (cmd) {
341 1.6 mycroft case IPC_RMID:
342 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_M)) != 0)
343 1.33 thorpej return (error);
344 1.61 elad semaptr->sem_perm.cuid = kauth_cred_geteuid(cred);
345 1.61 elad semaptr->sem_perm.uid = kauth_cred_geteuid(cred);
346 1.6 mycroft semtot -= semaptr->sem_nsems;
347 1.33 thorpej for (i = semaptr->_sem_base - sem; i < semtot; i++)
348 1.6 mycroft sem[i] = sem[i + semaptr->sem_nsems];
349 1.6 mycroft for (i = 0; i < seminfo.semmni; i++) {
350 1.6 mycroft if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
351 1.33 thorpej sema[i]._sem_base > semaptr->_sem_base)
352 1.33 thorpej sema[i]._sem_base -= semaptr->sem_nsems;
353 1.6 mycroft }
354 1.6 mycroft semaptr->sem_perm.mode = 0;
355 1.33 thorpej semundo_clear(ix, -1);
356 1.33 thorpej wakeup(semaptr);
357 1.6 mycroft break;
358 1.1 cgd
359 1.6 mycroft case IPC_SET:
360 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
361 1.33 thorpej return (error);
362 1.64 christos KASSERT(sembuf != NULL);
363 1.33 thorpej semaptr->sem_perm.uid = sembuf->sem_perm.uid;
364 1.33 thorpej semaptr->sem_perm.gid = sembuf->sem_perm.gid;
365 1.6 mycroft semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
366 1.33 thorpej (sembuf->sem_perm.mode & 0777);
367 1.62 kardel semaptr->sem_ctime = time_second;
368 1.6 mycroft break;
369 1.1 cgd
370 1.6 mycroft case IPC_STAT:
371 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
372 1.33 thorpej return (error);
373 1.64 christos KASSERT(sembuf != NULL);
374 1.33 thorpej memcpy(sembuf, semaptr, sizeof(struct semid_ds));
375 1.6 mycroft break;
376 1.1 cgd
377 1.6 mycroft case GETNCNT:
378 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
379 1.33 thorpej return (error);
380 1.6 mycroft if (semnum < 0 || semnum >= semaptr->sem_nsems)
381 1.33 thorpej return (EINVAL);
382 1.33 thorpej *retval = semaptr->_sem_base[semnum].semncnt;
383 1.6 mycroft break;
384 1.1 cgd
385 1.6 mycroft case GETPID:
386 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
387 1.33 thorpej return (error);
388 1.6 mycroft if (semnum < 0 || semnum >= semaptr->sem_nsems)
389 1.33 thorpej return (EINVAL);
390 1.33 thorpej *retval = semaptr->_sem_base[semnum].sempid;
391 1.6 mycroft break;
392 1.1 cgd
393 1.6 mycroft case GETVAL:
394 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
395 1.33 thorpej return (error);
396 1.6 mycroft if (semnum < 0 || semnum >= semaptr->sem_nsems)
397 1.33 thorpej return (EINVAL);
398 1.33 thorpej *retval = semaptr->_sem_base[semnum].semval;
399 1.6 mycroft break;
400 1.1 cgd
401 1.6 mycroft case GETALL:
402 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
403 1.33 thorpej return (error);
404 1.60 christos KASSERT(arg != NULL);
405 1.6 mycroft for (i = 0; i < semaptr->sem_nsems; i++) {
406 1.33 thorpej error = copyout(&semaptr->_sem_base[i].semval,
407 1.33 thorpej &arg->array[i], sizeof(arg->array[i]));
408 1.33 thorpej if (error != 0)
409 1.6 mycroft break;
410 1.6 mycroft }
411 1.6 mycroft break;
412 1.1 cgd
413 1.6 mycroft case GETZCNT:
414 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
415 1.33 thorpej return (error);
416 1.6 mycroft if (semnum < 0 || semnum >= semaptr->sem_nsems)
417 1.33 thorpej return (EINVAL);
418 1.33 thorpej *retval = semaptr->_sem_base[semnum].semzcnt;
419 1.6 mycroft break;
420 1.1 cgd
421 1.6 mycroft case SETVAL:
422 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
423 1.33 thorpej return (error);
424 1.6 mycroft if (semnum < 0 || semnum >= semaptr->sem_nsems)
425 1.33 thorpej return (EINVAL);
426 1.60 christos KASSERT(arg != NULL);
427 1.33 thorpej semaptr->_sem_base[semnum].semval = arg->val;
428 1.33 thorpej semundo_clear(ix, semnum);
429 1.33 thorpej wakeup(semaptr);
430 1.6 mycroft break;
431 1.1 cgd
432 1.6 mycroft case SETALL:
433 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
434 1.33 thorpej return (error);
435 1.60 christos KASSERT(arg != NULL);
436 1.6 mycroft for (i = 0; i < semaptr->sem_nsems; i++) {
437 1.33 thorpej error = copyin(&arg->array[i],
438 1.33 thorpej &semaptr->_sem_base[i].semval,
439 1.33 thorpej sizeof(arg->array[i]));
440 1.33 thorpej if (error != 0)
441 1.6 mycroft break;
442 1.6 mycroft }
443 1.33 thorpej semundo_clear(ix, -1);
444 1.33 thorpej wakeup(semaptr);
445 1.6 mycroft break;
446 1.1 cgd
447 1.6 mycroft default:
448 1.33 thorpej return (EINVAL);
449 1.6 mycroft }
450 1.4 mycroft
451 1.33 thorpej return (error);
452 1.1 cgd }
453 1.1 cgd
454 1.1 cgd int
455 1.59 thorpej sys_semget(struct lwp *l, void *v, register_t *retval)
456 1.23 thorpej {
457 1.35 augustss struct sys_semget_args /* {
458 1.10 cgd syscallarg(key_t) key;
459 1.10 cgd syscallarg(int) nsems;
460 1.10 cgd syscallarg(int) semflg;
461 1.23 thorpej } */ *uap = v;
462 1.6 mycroft int semid, eval;
463 1.10 cgd int key = SCARG(uap, key);
464 1.10 cgd int nsems = SCARG(uap, nsems);
465 1.10 cgd int semflg = SCARG(uap, semflg);
466 1.63 ad kauth_cred_t cred = l->l_cred;
467 1.1 cgd
468 1.27 christos SEM_PRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
469 1.1 cgd
470 1.6 mycroft if (key != IPC_PRIVATE) {
471 1.6 mycroft for (semid = 0; semid < seminfo.semmni; semid++) {
472 1.6 mycroft if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
473 1.33 thorpej sema[semid].sem_perm._key == key)
474 1.6 mycroft break;
475 1.6 mycroft }
476 1.6 mycroft if (semid < seminfo.semmni) {
477 1.27 christos SEM_PRINTF(("found public key\n"));
478 1.7 hpeyerl if ((eval = ipcperm(cred, &sema[semid].sem_perm,
479 1.7 hpeyerl semflg & 0700)))
480 1.51 enami return (eval);
481 1.6 mycroft if (nsems > 0 && sema[semid].sem_nsems < nsems) {
482 1.27 christos SEM_PRINTF(("too small\n"));
483 1.51 enami return (EINVAL);
484 1.6 mycroft }
485 1.6 mycroft if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
486 1.27 christos SEM_PRINTF(("not exclusive\n"));
487 1.51 enami return (EEXIST);
488 1.6 mycroft }
489 1.6 mycroft goto found;
490 1.6 mycroft }
491 1.6 mycroft }
492 1.6 mycroft
493 1.27 christos SEM_PRINTF(("need to allocate the semid_ds\n"));
494 1.6 mycroft if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
495 1.6 mycroft if (nsems <= 0 || nsems > seminfo.semmsl) {
496 1.27 christos SEM_PRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
497 1.27 christos seminfo.semmsl));
498 1.51 enami return (EINVAL);
499 1.6 mycroft }
500 1.6 mycroft if (nsems > seminfo.semmns - semtot) {
501 1.51 enami SEM_PRINTF(("not enough semaphores left "
502 1.51 enami "(need %d, got %d)\n",
503 1.27 christos nsems, seminfo.semmns - semtot));
504 1.51 enami return (ENOSPC);
505 1.6 mycroft }
506 1.6 mycroft for (semid = 0; semid < seminfo.semmni; semid++) {
507 1.6 mycroft if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
508 1.6 mycroft break;
509 1.6 mycroft }
510 1.6 mycroft if (semid == seminfo.semmni) {
511 1.27 christos SEM_PRINTF(("no more semid_ds's available\n"));
512 1.51 enami return (ENOSPC);
513 1.6 mycroft }
514 1.27 christos SEM_PRINTF(("semid %d is available\n", semid));
515 1.33 thorpej sema[semid].sem_perm._key = key;
516 1.61 elad sema[semid].sem_perm.cuid = kauth_cred_geteuid(cred);
517 1.61 elad sema[semid].sem_perm.uid = kauth_cred_geteuid(cred);
518 1.61 elad sema[semid].sem_perm.cgid = kauth_cred_getegid(cred);
519 1.61 elad sema[semid].sem_perm.gid = kauth_cred_getegid(cred);
520 1.6 mycroft sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
521 1.33 thorpej sema[semid].sem_perm._seq =
522 1.33 thorpej (sema[semid].sem_perm._seq + 1) & 0x7fff;
523 1.6 mycroft sema[semid].sem_nsems = nsems;
524 1.6 mycroft sema[semid].sem_otime = 0;
525 1.62 kardel sema[semid].sem_ctime = time_second;
526 1.33 thorpej sema[semid]._sem_base = &sem[semtot];
527 1.6 mycroft semtot += nsems;
528 1.33 thorpej memset(sema[semid]._sem_base, 0,
529 1.51 enami sizeof(sema[semid]._sem_base[0]) * nsems);
530 1.33 thorpej SEM_PRINTF(("sembase = %p, next = %p\n", sema[semid]._sem_base,
531 1.27 christos &sem[semtot]));
532 1.1 cgd } else {
533 1.27 christos SEM_PRINTF(("didn't find it and wasn't asked to create it\n"));
534 1.51 enami return (ENOENT);
535 1.1 cgd }
536 1.1 cgd
537 1.6 mycroft found:
538 1.6 mycroft *retval = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
539 1.51 enami return (0);
540 1.1 cgd }
541 1.1 cgd
542 1.57 chs #define SMALL_SOPS 8
543 1.57 chs
544 1.1 cgd int
545 1.59 thorpej sys_semop(struct lwp *l, void *v, register_t *retval)
546 1.23 thorpej {
547 1.35 augustss struct sys_semop_args /* {
548 1.10 cgd syscallarg(int) semid;
549 1.10 cgd syscallarg(struct sembuf *) sops;
550 1.29 kleink syscallarg(size_t) nsops;
551 1.23 thorpej } */ *uap = v;
552 1.45 thorpej struct proc *p = l->l_proc;
553 1.52 enami int semid = SCARG(uap, semid), seq;
554 1.41 jdolecek size_t nsops = SCARG(uap, nsops);
555 1.57 chs struct sembuf small_sops[SMALL_SOPS];
556 1.57 chs struct sembuf *sops;
557 1.35 augustss struct semid_ds *semaptr;
558 1.35 augustss struct sembuf *sopptr = NULL;
559 1.35 augustss struct __sem *semptr = NULL;
560 1.6 mycroft struct sem_undo *suptr = NULL;
561 1.63 ad kauth_cred_t cred = l->l_cred;
562 1.54 enami int i, eval;
563 1.25 christos int do_wakeup, do_undos;
564 1.1 cgd
565 1.58 christos SEM_PRINTF(("call to semop(%d, %p, %zd)\n", semid, SCARG(uap,sops), nsops));
566 1.1 cgd
567 1.6 mycroft semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
568 1.49 jdolecek if (semid < 0 || semid >= seminfo.semmni)
569 1.51 enami return (EINVAL);
570 1.6 mycroft
571 1.6 mycroft semaptr = &sema[semid];
572 1.52 enami seq = IPCID_TO_SEQ(SCARG(uap, semid));
573 1.11 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
574 1.52 enami semaptr->sem_perm._seq != seq)
575 1.51 enami return (EINVAL);
576 1.6 mycroft
577 1.7 hpeyerl if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W))) {
578 1.27 christos SEM_PRINTF(("eval = %d from ipaccess\n", eval));
579 1.51 enami return (eval);
580 1.6 mycroft }
581 1.1 cgd
582 1.57 chs if (nsops <= SMALL_SOPS) {
583 1.57 chs sops = small_sops;
584 1.57 chs } else if (nsops <= seminfo.semopm) {
585 1.57 chs sops = malloc(nsops * sizeof(*sops), M_TEMP, M_WAITOK);
586 1.57 chs } else {
587 1.57 chs SEM_PRINTF(("too many sops (max=%d, nsops=%zd)\n",
588 1.57 chs seminfo.semopm, nsops));
589 1.51 enami return (E2BIG);
590 1.6 mycroft }
591 1.1 cgd
592 1.51 enami if ((eval = copyin(SCARG(uap, sops),
593 1.51 enami sops, nsops * sizeof(sops[0]))) != 0) {
594 1.57 chs SEM_PRINTF(("eval = %d from copyin(%p, %p, %zd)\n", eval,
595 1.57 chs SCARG(uap, sops), &sops, nsops * sizeof(sops[0])));
596 1.57 chs goto out;
597 1.6 mycroft }
598 1.1 cgd
599 1.52 enami for (i = 0; i < nsops; i++)
600 1.57 chs if (sops[i].sem_num >= semaptr->sem_nsems) {
601 1.57 chs eval = EFBIG;
602 1.57 chs goto out;
603 1.57 chs }
604 1.52 enami
605 1.51 enami /*
606 1.6 mycroft * Loop trying to satisfy the vector of requests.
607 1.6 mycroft * If we reach a point where we must wait, any requests already
608 1.6 mycroft * performed are rolled back and we go to sleep until some other
609 1.6 mycroft * process wakes us up. At this point, we start all over again.
610 1.6 mycroft *
611 1.6 mycroft * This ensures that from the perspective of other tasks, a set
612 1.6 mycroft * of requests is atomic (never partially satisfied).
613 1.6 mycroft */
614 1.6 mycroft do_undos = 0;
615 1.1 cgd
616 1.6 mycroft for (;;) {
617 1.6 mycroft do_wakeup = 0;
618 1.1 cgd
619 1.6 mycroft for (i = 0; i < nsops; i++) {
620 1.6 mycroft sopptr = &sops[i];
621 1.33 thorpej semptr = &semaptr->_sem_base[sopptr->sem_num];
622 1.1 cgd
623 1.51 enami SEM_PRINTF(("semop: semaptr=%p, sem_base=%p, "
624 1.51 enami "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
625 1.33 thorpej semaptr, semaptr->_sem_base, semptr,
626 1.6 mycroft sopptr->sem_num, semptr->semval, sopptr->sem_op,
627 1.51 enami (sopptr->sem_flg & IPC_NOWAIT) ?
628 1.51 enami "nowait" : "wait"));
629 1.1 cgd
630 1.6 mycroft if (sopptr->sem_op < 0) {
631 1.25 christos if ((int)(semptr->semval +
632 1.51 enami sopptr->sem_op) < 0) {
633 1.51 enami SEM_PRINTF(("semop: "
634 1.51 enami "can't do it now\n"));
635 1.6 mycroft break;
636 1.6 mycroft } else {
637 1.6 mycroft semptr->semval += sopptr->sem_op;
638 1.6 mycroft if (semptr->semval == 0 &&
639 1.6 mycroft semptr->semzcnt > 0)
640 1.6 mycroft do_wakeup = 1;
641 1.6 mycroft }
642 1.6 mycroft if (sopptr->sem_flg & SEM_UNDO)
643 1.6 mycroft do_undos = 1;
644 1.6 mycroft } else if (sopptr->sem_op == 0) {
645 1.6 mycroft if (semptr->semval > 0) {
646 1.27 christos SEM_PRINTF(("semop: not zero now\n"));
647 1.6 mycroft break;
648 1.6 mycroft }
649 1.6 mycroft } else {
650 1.6 mycroft if (semptr->semncnt > 0)
651 1.6 mycroft do_wakeup = 1;
652 1.6 mycroft semptr->semval += sopptr->sem_op;
653 1.6 mycroft if (sopptr->sem_flg & SEM_UNDO)
654 1.6 mycroft do_undos = 1;
655 1.6 mycroft }
656 1.6 mycroft }
657 1.1 cgd
658 1.6 mycroft /*
659 1.6 mycroft * Did we get through the entire vector?
660 1.6 mycroft */
661 1.6 mycroft if (i >= nsops)
662 1.6 mycroft goto done;
663 1.1 cgd
664 1.6 mycroft /*
665 1.6 mycroft * No ... rollback anything that we've already done
666 1.6 mycroft */
667 1.51 enami SEM_PRINTF(("semop: rollback 0 through %d\n", i - 1));
668 1.52 enami while (i-- > 0)
669 1.52 enami semaptr->_sem_base[sops[i].sem_num].semval -=
670 1.52 enami sops[i].sem_op;
671 1.1 cgd
672 1.6 mycroft /*
673 1.6 mycroft * If the request that we couldn't satisfy has the
674 1.6 mycroft * NOWAIT flag set then return with EAGAIN.
675 1.6 mycroft */
676 1.57 chs if (sopptr->sem_flg & IPC_NOWAIT) {
677 1.57 chs eval = EAGAIN;
678 1.57 chs goto out;
679 1.57 chs }
680 1.1 cgd
681 1.6 mycroft if (sopptr->sem_op == 0)
682 1.6 mycroft semptr->semzcnt++;
683 1.6 mycroft else
684 1.6 mycroft semptr->semncnt++;
685 1.1 cgd
686 1.27 christos SEM_PRINTF(("semop: good night!\n"));
687 1.68 christos eval = tsleep((void *)semaptr, (PZERO - 4) | PCATCH,
688 1.6 mycroft "semwait", 0);
689 1.27 christos SEM_PRINTF(("semop: good morning (eval=%d)!\n", eval));
690 1.1 cgd
691 1.6 mycroft /*
692 1.6 mycroft * Make sure that the semaphore still exists
693 1.6 mycroft */
694 1.6 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
695 1.52 enami semaptr->sem_perm._seq != seq) {
696 1.57 chs eval = EIDRM;
697 1.57 chs goto out;
698 1.6 mycroft }
699 1.1 cgd
700 1.6 mycroft /*
701 1.6 mycroft * The semaphore is still alive. Readjust the count of
702 1.6 mycroft * waiting processes.
703 1.6 mycroft */
704 1.51 enami semptr = &semaptr->_sem_base[sopptr->sem_num];
705 1.6 mycroft if (sopptr->sem_op == 0)
706 1.6 mycroft semptr->semzcnt--;
707 1.6 mycroft else
708 1.6 mycroft semptr->semncnt--;
709 1.50 christos /*
710 1.50 christos * Is it really morning, or was our sleep interrupted?
711 1.50 christos * (Delayed check of tsleep() return code because we
712 1.50 christos * need to decrement sem[nz]cnt either way.)
713 1.50 christos */
714 1.57 chs if (eval != 0) {
715 1.57 chs eval = EINTR;
716 1.57 chs goto out;
717 1.57 chs }
718 1.50 christos SEM_PRINTF(("semop: good morning!\n"));
719 1.6 mycroft }
720 1.1 cgd
721 1.6 mycroft done:
722 1.6 mycroft /*
723 1.6 mycroft * Process any SEM_UNDO requests.
724 1.6 mycroft */
725 1.6 mycroft if (do_undos) {
726 1.5 mycroft for (i = 0; i < nsops; i++) {
727 1.6 mycroft /*
728 1.6 mycroft * We only need to deal with SEM_UNDO's for non-zero
729 1.6 mycroft * op's.
730 1.6 mycroft */
731 1.6 mycroft int adjval;
732 1.1 cgd
733 1.6 mycroft if ((sops[i].sem_flg & SEM_UNDO) == 0)
734 1.6 mycroft continue;
735 1.6 mycroft adjval = sops[i].sem_op;
736 1.6 mycroft if (adjval == 0)
737 1.6 mycroft continue;
738 1.6 mycroft eval = semundo_adjust(p, &suptr, semid,
739 1.6 mycroft sops[i].sem_num, -adjval);
740 1.6 mycroft if (eval == 0)
741 1.6 mycroft continue;
742 1.1 cgd
743 1.6 mycroft /*
744 1.6 mycroft * Oh-Oh! We ran out of either sem_undo's or undo's.
745 1.6 mycroft * Rollback the adjustments to this point and then
746 1.6 mycroft * rollback the semaphore ups and down so we can return
747 1.6 mycroft * with an error with all structures restored. We
748 1.6 mycroft * rollback the undo's in the exact reverse order that
749 1.6 mycroft * we applied them. This guarantees that we won't run
750 1.6 mycroft * out of space as we roll things back out.
751 1.6 mycroft */
752 1.52 enami while (i-- > 0) {
753 1.52 enami if ((sops[i].sem_flg & SEM_UNDO) == 0)
754 1.6 mycroft continue;
755 1.52 enami adjval = sops[i].sem_op;
756 1.6 mycroft if (adjval == 0)
757 1.6 mycroft continue;
758 1.6 mycroft if (semundo_adjust(p, &suptr, semid,
759 1.52 enami sops[i].sem_num, adjval) != 0)
760 1.1 cgd panic("semop - can't undo undos");
761 1.6 mycroft }
762 1.1 cgd
763 1.54 enami for (i = 0; i < nsops; i++)
764 1.54 enami semaptr->_sem_base[sops[i].sem_num].semval -=
765 1.54 enami sops[i].sem_op;
766 1.1 cgd
767 1.27 christos SEM_PRINTF(("eval = %d from semundo_adjust\n", eval));
768 1.57 chs goto out;
769 1.1 cgd } /* loop through the sops */
770 1.6 mycroft } /* if (do_undos) */
771 1.1 cgd
772 1.6 mycroft /* We're definitely done - set the sempid's */
773 1.6 mycroft for (i = 0; i < nsops; i++) {
774 1.1 cgd sopptr = &sops[i];
775 1.33 thorpej semptr = &semaptr->_sem_base[sopptr->sem_num];
776 1.1 cgd semptr->sempid = p->p_pid;
777 1.6 mycroft }
778 1.1 cgd
779 1.55 briggs /* Update sem_otime */
780 1.62 kardel semaptr->sem_otime = time_second;
781 1.55 briggs
782 1.6 mycroft /* Do a wakeup if any semaphore was up'd. */
783 1.6 mycroft if (do_wakeup) {
784 1.27 christos SEM_PRINTF(("semop: doing wakeup\n"));
785 1.1 cgd #ifdef SEM_WAKEUP
786 1.68 christos sem_wakeup((void *)semaptr);
787 1.1 cgd #else
788 1.68 christos wakeup((void *)semaptr);
789 1.1 cgd #endif
790 1.27 christos SEM_PRINTF(("semop: back from wakeup\n"));
791 1.6 mycroft }
792 1.27 christos SEM_PRINTF(("semop: done\n"));
793 1.6 mycroft *retval = 0;
794 1.57 chs
795 1.57 chs out:
796 1.57 chs if (sops != small_sops) {
797 1.57 chs free(sops, M_TEMP);
798 1.57 chs }
799 1.57 chs return eval;
800 1.1 cgd }
801 1.1 cgd
802 1.1 cgd /*
803 1.51 enami * Go through the undo structures for this process and apply the
804 1.51 enami * adjustments to semaphores.
805 1.1 cgd */
806 1.44 christos /*ARGSUSED*/
807 1.25 christos void
808 1.66 yamt semexit(struct proc *p, void *v)
809 1.1 cgd {
810 1.35 augustss struct sem_undo *suptr;
811 1.35 augustss struct sem_undo **supptr;
812 1.1 cgd
813 1.6 mycroft /*
814 1.51 enami * Go through the chain of undo vectors looking for one
815 1.51 enami * associated with this process.
816 1.17 mycroft */
817 1.17 mycroft
818 1.17 mycroft for (supptr = &semu_list; (suptr = *supptr) != NULL;
819 1.17 mycroft supptr = &suptr->un_next) {
820 1.17 mycroft if (suptr->un_proc == p)
821 1.17 mycroft break;
822 1.17 mycroft }
823 1.17 mycroft
824 1.17 mycroft /*
825 1.37 sommerfe * If there is no undo vector, skip to the end.
826 1.14 mycroft */
827 1.14 mycroft
828 1.37 sommerfe if (suptr == NULL)
829 1.37 sommerfe return;
830 1.51 enami
831 1.14 mycroft /*
832 1.37 sommerfe * We now have an undo vector for this process.
833 1.15 mycroft */
834 1.1 cgd
835 1.27 christos SEM_PRINTF(("proc @%p has undo structure with %d entries\n", p,
836 1.27 christos suptr->un_cnt));
837 1.1 cgd
838 1.5 mycroft /*
839 1.5 mycroft * If there are any active undo elements then process them.
840 1.5 mycroft */
841 1.5 mycroft if (suptr->un_cnt > 0) {
842 1.6 mycroft int ix;
843 1.1 cgd
844 1.6 mycroft for (ix = 0; ix < suptr->un_cnt; ix++) {
845 1.6 mycroft int semid = suptr->un_ent[ix].un_id;
846 1.6 mycroft int semnum = suptr->un_ent[ix].un_num;
847 1.6 mycroft int adjval = suptr->un_ent[ix].un_adjval;
848 1.6 mycroft struct semid_ds *semaptr;
849 1.6 mycroft
850 1.6 mycroft semaptr = &sema[semid];
851 1.6 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
852 1.6 mycroft panic("semexit - semid not allocated");
853 1.6 mycroft if (semnum >= semaptr->sem_nsems)
854 1.6 mycroft panic("semexit - semnum out of range");
855 1.6 mycroft
856 1.51 enami SEM_PRINTF(("semexit: %p id=%d num=%d(adj=%d) ; "
857 1.51 enami "sem=%d\n",
858 1.6 mycroft suptr->un_proc, suptr->un_ent[ix].un_id,
859 1.6 mycroft suptr->un_ent[ix].un_num,
860 1.6 mycroft suptr->un_ent[ix].un_adjval,
861 1.33 thorpej semaptr->_sem_base[semnum].semval));
862 1.6 mycroft
863 1.14 mycroft if (adjval < 0 &&
864 1.33 thorpej semaptr->_sem_base[semnum].semval < -adjval)
865 1.33 thorpej semaptr->_sem_base[semnum].semval = 0;
866 1.14 mycroft else
867 1.33 thorpej semaptr->_sem_base[semnum].semval += adjval;
868 1.1 cgd
869 1.1 cgd #ifdef SEM_WAKEUP
870 1.68 christos sem_wakeup((void *)semaptr);
871 1.1 cgd #else
872 1.68 christos wakeup((void *)semaptr);
873 1.1 cgd #endif
874 1.27 christos SEM_PRINTF(("semexit: back from wakeup\n"));
875 1.6 mycroft }
876 1.5 mycroft }
877 1.1 cgd
878 1.5 mycroft /*
879 1.5 mycroft * Deallocate the undo vector.
880 1.5 mycroft */
881 1.27 christos SEM_PRINTF(("removing vector\n"));
882 1.5 mycroft suptr->un_proc = NULL;
883 1.5 mycroft *supptr = suptr->un_next;
884 1.1 cgd }
885