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