sysv_sem.c revision 1.92 1 1.92 pgoyette /* $NetBSD: sysv_sem.c,v 1.92 2015/05/12 05:19:20 pgoyette Exp $ */
2 1.33 thorpej
3 1.33 thorpej /*-
4 1.70 ad * Copyright (c) 1999, 2007 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.70 ad * NASA Ames Research Center, and by Andrew Doran.
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 *
20 1.33 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.33 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.33 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.33 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.33 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.33 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.33 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.33 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.33 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.33 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.33 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.33 thorpej */
32 1.9 cgd
33 1.1 cgd /*
34 1.1 cgd * Implementation of SVID semaphores
35 1.1 cgd *
36 1.33 thorpej * Author: Daniel Boulet
37 1.1 cgd *
38 1.1 cgd * This software is provided ``AS IS'' without any warranties of any kind.
39 1.1 cgd */
40 1.42 lukem
41 1.42 lukem #include <sys/cdefs.h>
42 1.92 pgoyette __KERNEL_RCSID(0, "$NetBSD: sysv_sem.c,v 1.92 2015/05/12 05:19:20 pgoyette Exp $");
43 1.31 tron
44 1.92 pgoyette #ifdef _KERNEL_OPT
45 1.92 pgoyette #include "opt_sysv.h"
46 1.92 pgoyette #endif
47 1.1 cgd
48 1.3 mycroft #include <sys/param.h>
49 1.3 mycroft #include <sys/kernel.h>
50 1.3 mycroft #include <sys/sem.h>
51 1.38 simonb #include <sys/sysctl.h>
52 1.70 ad #include <sys/kmem.h>
53 1.38 simonb #include <sys/mount.h> /* XXX for <sys/syscallargs.h> */
54 1.10 cgd #include <sys/syscallargs.h>
55 1.61 elad #include <sys/kauth.h>
56 1.25 christos
57 1.74 rmind /*
58 1.74 rmind * Memory areas:
59 1.74 rmind * 1st: Pool of semaphore identifiers
60 1.74 rmind * 2nd: Semaphores
61 1.74 rmind * 3rd: Conditional variables
62 1.74 rmind * 4th: Undo structures
63 1.74 rmind */
64 1.87 rmind struct semid_ds * sema __read_mostly;
65 1.87 rmind static struct __sem * sem __read_mostly;
66 1.87 rmind static kcondvar_t * semcv __read_mostly;
67 1.87 rmind static int * semu __read_mostly;
68 1.87 rmind
69 1.87 rmind static kmutex_t semlock __cacheline_aligned;
70 1.87 rmind static bool sem_realloc_state __read_mostly;
71 1.87 rmind static kcondvar_t sem_realloc_cv;
72 1.87 rmind
73 1.87 rmind /*
74 1.87 rmind * List of active undo structures, total number of semaphores,
75 1.87 rmind * and total number of semop waiters.
76 1.87 rmind */
77 1.87 rmind static struct sem_undo *semu_list __read_mostly;
78 1.87 rmind static u_int semtot __cacheline_aligned;
79 1.87 rmind static u_int sem_waiters __cacheline_aligned;
80 1.74 rmind
81 1.74 rmind /* Macro to find a particular sem_undo vector */
82 1.74 rmind #define SEMU(s, ix) ((struct sem_undo *)(((long)s) + ix * seminfo.semusz))
83 1.1 cgd
84 1.27 christos #ifdef SEM_DEBUG
85 1.28 christos #define SEM_PRINTF(a) printf a
86 1.27 christos #else
87 1.27 christos #define SEM_PRINTF(a)
88 1.27 christos #endif
89 1.27 christos
90 1.53 junyoung struct sem_undo *semu_alloc(struct proc *);
91 1.53 junyoung int semundo_adjust(struct proc *, struct sem_undo **, int, int, int);
92 1.53 junyoung void semundo_clear(int, int);
93 1.25 christos
94 1.25 christos void
95 1.59 thorpej seminit(void)
96 1.1 cgd {
97 1.48 jdolecek int i, sz;
98 1.48 jdolecek vaddr_t v;
99 1.1 cgd
100 1.70 ad mutex_init(&semlock, MUTEX_DEFAULT, IPL_NONE);
101 1.74 rmind cv_init(&sem_realloc_cv, "semrealc");
102 1.74 rmind sem_realloc_state = false;
103 1.87 rmind semtot = 0;
104 1.87 rmind sem_waiters = 0;
105 1.70 ad
106 1.74 rmind /* Allocate the wired memory for our structures */
107 1.74 rmind sz = ALIGN(seminfo.semmni * sizeof(struct semid_ds)) +
108 1.74 rmind ALIGN(seminfo.semmns * sizeof(struct __sem)) +
109 1.74 rmind ALIGN(seminfo.semmni * sizeof(kcondvar_t)) +
110 1.74 rmind ALIGN(seminfo.semmnu * seminfo.semusz);
111 1.88 uebayasi sz = round_page(sz);
112 1.88 uebayasi v = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
113 1.56 yamt if (v == 0)
114 1.48 jdolecek panic("sysv_sem: cannot allocate memory");
115 1.48 jdolecek sema = (void *)v;
116 1.84 rmind sem = (void *)((uintptr_t)sema +
117 1.84 rmind ALIGN(seminfo.semmni * sizeof(struct semid_ds)));
118 1.84 rmind semcv = (void *)((uintptr_t)sem +
119 1.84 rmind ALIGN(seminfo.semmns * sizeof(struct __sem)));
120 1.84 rmind semu = (void *)((uintptr_t)semcv +
121 1.84 rmind ALIGN(seminfo.semmni * sizeof(kcondvar_t)));
122 1.5 mycroft
123 1.5 mycroft for (i = 0; i < seminfo.semmni; i++) {
124 1.33 thorpej sema[i]._sem_base = 0;
125 1.5 mycroft sema[i].sem_perm.mode = 0;
126 1.70 ad cv_init(&semcv[i], "semwait");
127 1.5 mycroft }
128 1.5 mycroft for (i = 0; i < seminfo.semmnu; i++) {
129 1.74 rmind struct sem_undo *suptr = SEMU(semu, i);
130 1.5 mycroft suptr->un_proc = NULL;
131 1.5 mycroft }
132 1.5 mycroft semu_list = NULL;
133 1.44 christos exithook_establish(semexit, NULL);
134 1.89 elad
135 1.89 elad sysvipcinit();
136 1.1 cgd }
137 1.1 cgd
138 1.74 rmind static int
139 1.74 rmind semrealloc(int newsemmni, int newsemmns, int newsemmnu)
140 1.74 rmind {
141 1.74 rmind struct semid_ds *new_sema, *old_sema;
142 1.74 rmind struct __sem *new_sem;
143 1.74 rmind struct sem_undo *new_semu_list, *suptr, *nsuptr;
144 1.74 rmind int *new_semu;
145 1.74 rmind kcondvar_t *new_semcv;
146 1.74 rmind vaddr_t v;
147 1.74 rmind int i, j, lsemid, nmnus, sz;
148 1.74 rmind
149 1.74 rmind if (newsemmni < 1 || newsemmns < 1 || newsemmnu < 1)
150 1.74 rmind return EINVAL;
151 1.74 rmind
152 1.74 rmind /* Allocate the wired memory for our structures */
153 1.74 rmind sz = ALIGN(newsemmni * sizeof(struct semid_ds)) +
154 1.74 rmind ALIGN(newsemmns * sizeof(struct __sem)) +
155 1.74 rmind ALIGN(newsemmni * sizeof(kcondvar_t)) +
156 1.74 rmind ALIGN(newsemmnu * seminfo.semusz);
157 1.88 uebayasi sz = round_page(sz);
158 1.88 uebayasi v = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
159 1.74 rmind if (v == 0)
160 1.74 rmind return ENOMEM;
161 1.74 rmind
162 1.74 rmind mutex_enter(&semlock);
163 1.74 rmind if (sem_realloc_state) {
164 1.74 rmind mutex_exit(&semlock);
165 1.74 rmind uvm_km_free(kernel_map, v, sz, UVM_KMF_WIRED);
166 1.74 rmind return EBUSY;
167 1.74 rmind }
168 1.74 rmind sem_realloc_state = true;
169 1.74 rmind if (sem_waiters) {
170 1.74 rmind /*
171 1.74 rmind * Mark reallocation state, wake-up all waiters,
172 1.74 rmind * and wait while they will all exit.
173 1.74 rmind */
174 1.74 rmind for (i = 0; i < seminfo.semmni; i++)
175 1.74 rmind cv_broadcast(&semcv[i]);
176 1.74 rmind while (sem_waiters)
177 1.74 rmind cv_wait(&sem_realloc_cv, &semlock);
178 1.74 rmind }
179 1.74 rmind old_sema = sema;
180 1.74 rmind
181 1.74 rmind /* Get the number of last slot */
182 1.74 rmind lsemid = 0;
183 1.74 rmind for (i = 0; i < seminfo.semmni; i++)
184 1.74 rmind if (sema[i].sem_perm.mode & SEM_ALLOC)
185 1.74 rmind lsemid = i;
186 1.74 rmind
187 1.74 rmind /* Get the number of currently used undo structures */
188 1.74 rmind nmnus = 0;
189 1.74 rmind for (i = 0; i < seminfo.semmnu; i++) {
190 1.74 rmind suptr = SEMU(semu, i);
191 1.74 rmind if (suptr->un_proc == NULL)
192 1.74 rmind continue;
193 1.74 rmind nmnus++;
194 1.74 rmind }
195 1.74 rmind
196 1.74 rmind /* We cannot reallocate less memory than we use */
197 1.74 rmind if (lsemid >= newsemmni || semtot > newsemmns || nmnus > newsemmnu) {
198 1.74 rmind mutex_exit(&semlock);
199 1.74 rmind uvm_km_free(kernel_map, v, sz, UVM_KMF_WIRED);
200 1.74 rmind return EBUSY;
201 1.74 rmind }
202 1.74 rmind
203 1.74 rmind new_sema = (void *)v;
204 1.84 rmind new_sem = (void *)((uintptr_t)new_sema +
205 1.84 rmind ALIGN(newsemmni * sizeof(struct semid_ds)));
206 1.84 rmind new_semcv = (void *)((uintptr_t)new_sem +
207 1.84 rmind ALIGN(newsemmns * sizeof(struct __sem)));
208 1.84 rmind new_semu = (void *)((uintptr_t)new_semcv +
209 1.84 rmind ALIGN(newsemmni * sizeof(kcondvar_t)));
210 1.74 rmind
211 1.74 rmind /* Initialize all semaphore identifiers and condvars */
212 1.74 rmind for (i = 0; i < newsemmni; i++) {
213 1.74 rmind new_sema[i]._sem_base = 0;
214 1.74 rmind new_sema[i].sem_perm.mode = 0;
215 1.74 rmind cv_init(&new_semcv[i], "semwait");
216 1.74 rmind }
217 1.74 rmind for (i = 0; i < newsemmnu; i++) {
218 1.74 rmind nsuptr = SEMU(new_semu, i);
219 1.74 rmind nsuptr->un_proc = NULL;
220 1.74 rmind }
221 1.74 rmind
222 1.74 rmind /*
223 1.74 rmind * Copy all identifiers, semaphores and list of the
224 1.74 rmind * undo structures to the new memory allocation.
225 1.74 rmind */
226 1.74 rmind j = 0;
227 1.74 rmind for (i = 0; i <= lsemid; i++) {
228 1.74 rmind if ((sema[i].sem_perm.mode & SEM_ALLOC) == 0)
229 1.74 rmind continue;
230 1.74 rmind memcpy(&new_sema[i], &sema[i], sizeof(struct semid_ds));
231 1.74 rmind new_sema[i]._sem_base = &new_sem[j];
232 1.74 rmind memcpy(new_sema[i]._sem_base, sema[i]._sem_base,
233 1.74 rmind (sizeof(struct __sem) * sema[i].sem_nsems));
234 1.74 rmind j += sema[i].sem_nsems;
235 1.74 rmind }
236 1.74 rmind KASSERT(j == semtot);
237 1.74 rmind
238 1.74 rmind j = 0;
239 1.74 rmind new_semu_list = NULL;
240 1.74 rmind for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
241 1.74 rmind KASSERT(j < newsemmnu);
242 1.74 rmind nsuptr = SEMU(new_semu, j);
243 1.74 rmind memcpy(nsuptr, suptr, SEMUSZ);
244 1.74 rmind nsuptr->un_next = new_semu_list;
245 1.74 rmind new_semu_list = nsuptr;
246 1.74 rmind j++;
247 1.74 rmind }
248 1.74 rmind
249 1.74 rmind for (i = 0; i < seminfo.semmni; i++) {
250 1.74 rmind KASSERT(cv_has_waiters(&semcv[i]) == false);
251 1.74 rmind cv_destroy(&semcv[i]);
252 1.74 rmind }
253 1.74 rmind
254 1.74 rmind sz = ALIGN(seminfo.semmni * sizeof(struct semid_ds)) +
255 1.74 rmind ALIGN(seminfo.semmns * sizeof(struct __sem)) +
256 1.74 rmind ALIGN(seminfo.semmni * sizeof(kcondvar_t)) +
257 1.74 rmind ALIGN(seminfo.semmnu * seminfo.semusz);
258 1.88 uebayasi sz = round_page(sz);
259 1.74 rmind
260 1.74 rmind /* Set the pointers and update the new values */
261 1.74 rmind sema = new_sema;
262 1.74 rmind sem = new_sem;
263 1.74 rmind semcv = new_semcv;
264 1.74 rmind semu = new_semu;
265 1.74 rmind semu_list = new_semu_list;
266 1.74 rmind
267 1.74 rmind seminfo.semmni = newsemmni;
268 1.74 rmind seminfo.semmns = newsemmns;
269 1.74 rmind seminfo.semmnu = newsemmnu;
270 1.74 rmind
271 1.74 rmind /* Reallocation completed - notify all waiters, if any */
272 1.74 rmind sem_realloc_state = false;
273 1.74 rmind cv_broadcast(&sem_realloc_cv);
274 1.74 rmind mutex_exit(&semlock);
275 1.74 rmind
276 1.74 rmind uvm_km_free(kernel_map, (vaddr_t)old_sema, sz, UVM_KMF_WIRED);
277 1.74 rmind return 0;
278 1.74 rmind }
279 1.74 rmind
280 1.1 cgd /*
281 1.37 sommerfe * Placebo.
282 1.1 cgd */
283 1.1 cgd
284 1.1 cgd int
285 1.78 dsl sys_semconfig(struct lwp *l, const struct sys_semconfig_args *uap, register_t *retval)
286 1.23 thorpej {
287 1.51 enami
288 1.5 mycroft *retval = 0;
289 1.37 sommerfe return 0;
290 1.1 cgd }
291 1.1 cgd
292 1.1 cgd /*
293 1.86 rmind * Allocate a new sem_undo structure for a process.
294 1.86 rmind * => Returns NULL on failure.
295 1.1 cgd */
296 1.1 cgd struct sem_undo *
297 1.59 thorpej semu_alloc(struct proc *p)
298 1.1 cgd {
299 1.86 rmind struct sem_undo *suptr, **supptr;
300 1.86 rmind bool attempted = false;
301 1.35 augustss int i;
302 1.1 cgd
303 1.70 ad KASSERT(mutex_owned(&semlock));
304 1.86 rmind again:
305 1.86 rmind /* Look for a free structure. */
306 1.86 rmind for (i = 0; i < seminfo.semmnu; i++) {
307 1.86 rmind suptr = SEMU(semu, i);
308 1.86 rmind if (suptr->un_proc == NULL) {
309 1.86 rmind /* Found. Fill it in and return. */
310 1.86 rmind suptr->un_next = semu_list;
311 1.86 rmind semu_list = suptr;
312 1.86 rmind suptr->un_cnt = 0;
313 1.86 rmind suptr->un_proc = p;
314 1.86 rmind return suptr;
315 1.86 rmind }
316 1.86 rmind }
317 1.70 ad
318 1.86 rmind /* Not found. Attempt to free some structures. */
319 1.86 rmind if (!attempted) {
320 1.86 rmind bool freed = false;
321 1.86 rmind
322 1.86 rmind attempted = true;
323 1.86 rmind supptr = &semu_list;
324 1.86 rmind while ((suptr = *supptr) != NULL) {
325 1.86 rmind if (suptr->un_cnt == 0) {
326 1.86 rmind suptr->un_proc = NULL;
327 1.86 rmind *supptr = suptr->un_next;
328 1.86 rmind freed = true;
329 1.86 rmind } else {
330 1.86 rmind supptr = &suptr->un_next;
331 1.5 mycroft }
332 1.5 mycroft }
333 1.86 rmind if (freed) {
334 1.86 rmind goto again;
335 1.5 mycroft }
336 1.1 cgd }
337 1.25 christos return NULL;
338 1.1 cgd }
339 1.1 cgd
340 1.1 cgd /*
341 1.1 cgd * Adjust a particular entry for a particular proc
342 1.1 cgd */
343 1.1 cgd
344 1.1 cgd int
345 1.59 thorpej semundo_adjust(struct proc *p, struct sem_undo **supptr, int semid, int semnum,
346 1.59 thorpej int adjval)
347 1.1 cgd {
348 1.35 augustss struct sem_undo *suptr;
349 1.91 matt struct sem_undo_entry *sunptr;
350 1.5 mycroft int i;
351 1.1 cgd
352 1.70 ad KASSERT(mutex_owned(&semlock));
353 1.70 ad
354 1.51 enami /*
355 1.51 enami * Look for and remember the sem_undo if the caller doesn't
356 1.51 enami * provide it
357 1.51 enami */
358 1.1 cgd
359 1.5 mycroft suptr = *supptr;
360 1.4 mycroft if (suptr == NULL) {
361 1.52 enami for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next)
362 1.52 enami if (suptr->un_proc == p)
363 1.5 mycroft break;
364 1.52 enami
365 1.5 mycroft if (suptr == NULL) {
366 1.5 mycroft suptr = semu_alloc(p);
367 1.5 mycroft if (suptr == NULL)
368 1.51 enami return (ENOSPC);
369 1.5 mycroft }
370 1.52 enami *supptr = suptr;
371 1.1 cgd }
372 1.1 cgd
373 1.6 mycroft /*
374 1.51 enami * Look for the requested entry and adjust it (delete if
375 1.51 enami * adjval becomes 0).
376 1.6 mycroft */
377 1.6 mycroft sunptr = &suptr->un_ent[0];
378 1.5 mycroft for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
379 1.6 mycroft if (sunptr->un_id != semid || sunptr->un_num != semnum)
380 1.6 mycroft continue;
381 1.52 enami sunptr->un_adjval += adjval;
382 1.6 mycroft if (sunptr->un_adjval == 0) {
383 1.6 mycroft suptr->un_cnt--;
384 1.6 mycroft if (i < suptr->un_cnt)
385 1.6 mycroft suptr->un_ent[i] =
386 1.6 mycroft suptr->un_ent[suptr->un_cnt];
387 1.5 mycroft }
388 1.51 enami return (0);
389 1.1 cgd }
390 1.1 cgd
391 1.5 mycroft /* Didn't find the right entry - create it */
392 1.11 mycroft if (suptr->un_cnt == SEMUME)
393 1.51 enami return (EINVAL);
394 1.11 mycroft
395 1.11 mycroft sunptr = &suptr->un_ent[suptr->un_cnt];
396 1.11 mycroft suptr->un_cnt++;
397 1.11 mycroft sunptr->un_adjval = adjval;
398 1.11 mycroft sunptr->un_id = semid;
399 1.11 mycroft sunptr->un_num = semnum;
400 1.51 enami return (0);
401 1.1 cgd }
402 1.1 cgd
403 1.1 cgd void
404 1.59 thorpej semundo_clear(int semid, int semnum)
405 1.1 cgd {
406 1.35 augustss struct sem_undo *suptr;
407 1.91 matt struct sem_undo_entry *sunptr, *sunend;
408 1.1 cgd
409 1.70 ad KASSERT(mutex_owned(&semlock));
410 1.70 ad
411 1.52 enami for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next)
412 1.52 enami for (sunptr = &suptr->un_ent[0],
413 1.52 enami sunend = sunptr + suptr->un_cnt; sunptr < sunend;) {
414 1.6 mycroft if (sunptr->un_id == semid) {
415 1.6 mycroft if (semnum == -1 || sunptr->un_num == semnum) {
416 1.6 mycroft suptr->un_cnt--;
417 1.52 enami sunend--;
418 1.52 enami if (sunptr != sunend)
419 1.52 enami *sunptr = *sunend;
420 1.52 enami if (semnum != -1)
421 1.52 enami break;
422 1.52 enami else
423 1.52 enami continue;
424 1.6 mycroft }
425 1.6 mycroft }
426 1.52 enami sunptr++;
427 1.6 mycroft }
428 1.1 cgd }
429 1.1 cgd
430 1.1 cgd int
431 1.85 christos sys_____semctl50(struct lwp *l, const struct sys_____semctl50_args *uap,
432 1.85 christos register_t *retval)
433 1.23 thorpej {
434 1.78 dsl /* {
435 1.10 cgd syscallarg(int) semid;
436 1.10 cgd syscallarg(int) semnum;
437 1.10 cgd syscallarg(int) cmd;
438 1.34 christos syscallarg(union __semun *) arg;
439 1.78 dsl } */
440 1.33 thorpej struct semid_ds sembuf;
441 1.33 thorpej int cmd, error;
442 1.34 christos void *pass_arg;
443 1.34 christos union __semun karg;
444 1.33 thorpej
445 1.33 thorpej cmd = SCARG(uap, cmd);
446 1.33 thorpej
447 1.69 dsl pass_arg = get_semctl_arg(cmd, &sembuf, &karg);
448 1.33 thorpej
449 1.34 christos if (pass_arg) {
450 1.34 christos error = copyin(SCARG(uap, arg), &karg, sizeof(karg));
451 1.33 thorpej if (error)
452 1.34 christos return error;
453 1.34 christos if (cmd == IPC_SET) {
454 1.34 christos error = copyin(karg.buf, &sembuf, sizeof(sembuf));
455 1.34 christos if (error)
456 1.34 christos return (error);
457 1.34 christos }
458 1.33 thorpej }
459 1.33 thorpej
460 1.63 ad error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd,
461 1.33 thorpej pass_arg, retval);
462 1.33 thorpej
463 1.33 thorpej if (error == 0 && cmd == IPC_STAT)
464 1.34 christos error = copyout(&sembuf, karg.buf, sizeof(sembuf));
465 1.33 thorpej
466 1.33 thorpej return (error);
467 1.33 thorpej }
468 1.33 thorpej
469 1.33 thorpej int
470 1.63 ad semctl1(struct lwp *l, int semid, int semnum, int cmd, void *v,
471 1.59 thorpej register_t *retval)
472 1.33 thorpej {
473 1.63 ad kauth_cred_t cred = l->l_cred;
474 1.33 thorpej union __semun *arg = v;
475 1.33 thorpej struct semid_ds *sembuf = v, *semaptr;
476 1.33 thorpej int i, error, ix;
477 1.1 cgd
478 1.27 christos SEM_PRINTF(("call to semctl(%d, %d, %d, %p)\n",
479 1.33 thorpej semid, semnum, cmd, v));
480 1.1 cgd
481 1.70 ad mutex_enter(&semlock);
482 1.70 ad
483 1.33 thorpej ix = IPCID_TO_IX(semid);
484 1.70 ad if (ix < 0 || ix >= seminfo.semmni) {
485 1.70 ad mutex_exit(&semlock);
486 1.33 thorpej return (EINVAL);
487 1.70 ad }
488 1.6 mycroft
489 1.33 thorpej semaptr = &sema[ix];
490 1.6 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
491 1.70 ad semaptr->sem_perm._seq != IPCID_TO_SEQ(semid)) {
492 1.70 ad mutex_exit(&semlock);
493 1.33 thorpej return (EINVAL);
494 1.70 ad }
495 1.1 cgd
496 1.6 mycroft switch (cmd) {
497 1.6 mycroft case IPC_RMID:
498 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_M)) != 0)
499 1.70 ad break;
500 1.61 elad semaptr->sem_perm.cuid = kauth_cred_geteuid(cred);
501 1.61 elad semaptr->sem_perm.uid = kauth_cred_geteuid(cred);
502 1.6 mycroft semtot -= semaptr->sem_nsems;
503 1.33 thorpej for (i = semaptr->_sem_base - sem; i < semtot; i++)
504 1.6 mycroft sem[i] = sem[i + semaptr->sem_nsems];
505 1.6 mycroft for (i = 0; i < seminfo.semmni; i++) {
506 1.6 mycroft if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
507 1.33 thorpej sema[i]._sem_base > semaptr->_sem_base)
508 1.33 thorpej sema[i]._sem_base -= semaptr->sem_nsems;
509 1.6 mycroft }
510 1.6 mycroft semaptr->sem_perm.mode = 0;
511 1.33 thorpej semundo_clear(ix, -1);
512 1.70 ad cv_broadcast(&semcv[ix]);
513 1.6 mycroft break;
514 1.1 cgd
515 1.6 mycroft case IPC_SET:
516 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
517 1.70 ad break;
518 1.64 christos KASSERT(sembuf != NULL);
519 1.33 thorpej semaptr->sem_perm.uid = sembuf->sem_perm.uid;
520 1.33 thorpej semaptr->sem_perm.gid = sembuf->sem_perm.gid;
521 1.6 mycroft semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
522 1.33 thorpej (sembuf->sem_perm.mode & 0777);
523 1.62 kardel semaptr->sem_ctime = time_second;
524 1.6 mycroft break;
525 1.1 cgd
526 1.6 mycroft case IPC_STAT:
527 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
528 1.70 ad break;
529 1.64 christos KASSERT(sembuf != NULL);
530 1.33 thorpej memcpy(sembuf, semaptr, sizeof(struct semid_ds));
531 1.80 njoly sembuf->sem_perm.mode &= 0777;
532 1.6 mycroft break;
533 1.1 cgd
534 1.6 mycroft case GETNCNT:
535 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
536 1.70 ad break;
537 1.70 ad if (semnum < 0 || semnum >= semaptr->sem_nsems) {
538 1.70 ad error = EINVAL;
539 1.70 ad break;
540 1.70 ad }
541 1.33 thorpej *retval = semaptr->_sem_base[semnum].semncnt;
542 1.6 mycroft break;
543 1.1 cgd
544 1.6 mycroft case GETPID:
545 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
546 1.70 ad break;
547 1.70 ad if (semnum < 0 || semnum >= semaptr->sem_nsems) {
548 1.70 ad error = EINVAL;
549 1.70 ad break;
550 1.70 ad }
551 1.33 thorpej *retval = semaptr->_sem_base[semnum].sempid;
552 1.6 mycroft break;
553 1.1 cgd
554 1.6 mycroft case GETVAL:
555 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
556 1.70 ad break;
557 1.70 ad if (semnum < 0 || semnum >= semaptr->sem_nsems) {
558 1.70 ad error = EINVAL;
559 1.70 ad break;
560 1.70 ad }
561 1.33 thorpej *retval = semaptr->_sem_base[semnum].semval;
562 1.6 mycroft break;
563 1.1 cgd
564 1.6 mycroft case GETALL:
565 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
566 1.70 ad break;
567 1.60 christos KASSERT(arg != NULL);
568 1.6 mycroft for (i = 0; i < semaptr->sem_nsems; i++) {
569 1.33 thorpej error = copyout(&semaptr->_sem_base[i].semval,
570 1.33 thorpej &arg->array[i], sizeof(arg->array[i]));
571 1.33 thorpej if (error != 0)
572 1.6 mycroft break;
573 1.6 mycroft }
574 1.6 mycroft break;
575 1.1 cgd
576 1.6 mycroft case GETZCNT:
577 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
578 1.70 ad break;
579 1.70 ad if (semnum < 0 || semnum >= semaptr->sem_nsems) {
580 1.70 ad error = EINVAL;
581 1.70 ad break;
582 1.70 ad }
583 1.33 thorpej *retval = semaptr->_sem_base[semnum].semzcnt;
584 1.6 mycroft break;
585 1.1 cgd
586 1.6 mycroft case SETVAL:
587 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
588 1.70 ad break;
589 1.70 ad if (semnum < 0 || semnum >= semaptr->sem_nsems) {
590 1.70 ad error = EINVAL;
591 1.70 ad break;
592 1.70 ad }
593 1.60 christos KASSERT(arg != NULL);
594 1.83 njoly if ((unsigned int)arg->val > seminfo.semvmx) {
595 1.83 njoly error = ERANGE;
596 1.83 njoly break;
597 1.83 njoly }
598 1.33 thorpej semaptr->_sem_base[semnum].semval = arg->val;
599 1.33 thorpej semundo_clear(ix, semnum);
600 1.70 ad cv_broadcast(&semcv[ix]);
601 1.6 mycroft break;
602 1.1 cgd
603 1.6 mycroft case SETALL:
604 1.33 thorpej if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
605 1.70 ad break;
606 1.60 christos KASSERT(arg != NULL);
607 1.6 mycroft for (i = 0; i < semaptr->sem_nsems; i++) {
608 1.83 njoly unsigned short semval;
609 1.83 njoly error = copyin(&arg->array[i], &semval,
610 1.33 thorpej sizeof(arg->array[i]));
611 1.33 thorpej if (error != 0)
612 1.6 mycroft break;
613 1.83 njoly if ((unsigned int)semval > seminfo.semvmx) {
614 1.83 njoly error = ERANGE;
615 1.83 njoly break;
616 1.83 njoly }
617 1.83 njoly semaptr->_sem_base[i].semval = semval;
618 1.6 mycroft }
619 1.33 thorpej semundo_clear(ix, -1);
620 1.70 ad cv_broadcast(&semcv[ix]);
621 1.6 mycroft break;
622 1.1 cgd
623 1.6 mycroft default:
624 1.70 ad error = EINVAL;
625 1.70 ad break;
626 1.6 mycroft }
627 1.4 mycroft
628 1.70 ad mutex_exit(&semlock);
629 1.33 thorpej return (error);
630 1.1 cgd }
631 1.1 cgd
632 1.1 cgd int
633 1.78 dsl sys_semget(struct lwp *l, const struct sys_semget_args *uap, register_t *retval)
634 1.23 thorpej {
635 1.78 dsl /* {
636 1.10 cgd syscallarg(key_t) key;
637 1.10 cgd syscallarg(int) nsems;
638 1.10 cgd syscallarg(int) semflg;
639 1.78 dsl } */
640 1.70 ad int semid, error = 0;
641 1.10 cgd int key = SCARG(uap, key);
642 1.10 cgd int nsems = SCARG(uap, nsems);
643 1.10 cgd int semflg = SCARG(uap, semflg);
644 1.63 ad kauth_cred_t cred = l->l_cred;
645 1.1 cgd
646 1.27 christos SEM_PRINTF(("semget(0x%x, %d, 0%o)\n", key, nsems, semflg));
647 1.1 cgd
648 1.70 ad mutex_enter(&semlock);
649 1.70 ad
650 1.6 mycroft if (key != IPC_PRIVATE) {
651 1.6 mycroft for (semid = 0; semid < seminfo.semmni; semid++) {
652 1.6 mycroft if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
653 1.33 thorpej sema[semid].sem_perm._key == key)
654 1.6 mycroft break;
655 1.6 mycroft }
656 1.6 mycroft if (semid < seminfo.semmni) {
657 1.27 christos SEM_PRINTF(("found public key\n"));
658 1.70 ad if ((error = ipcperm(cred, &sema[semid].sem_perm,
659 1.7 hpeyerl semflg & 0700)))
660 1.70 ad goto out;
661 1.6 mycroft if (nsems > 0 && sema[semid].sem_nsems < nsems) {
662 1.27 christos SEM_PRINTF(("too small\n"));
663 1.70 ad error = EINVAL;
664 1.70 ad goto out;
665 1.6 mycroft }
666 1.6 mycroft if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
667 1.27 christos SEM_PRINTF(("not exclusive\n"));
668 1.70 ad error = EEXIST;
669 1.70 ad goto out;
670 1.6 mycroft }
671 1.6 mycroft goto found;
672 1.6 mycroft }
673 1.6 mycroft }
674 1.6 mycroft
675 1.27 christos SEM_PRINTF(("need to allocate the semid_ds\n"));
676 1.6 mycroft if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
677 1.6 mycroft if (nsems <= 0 || nsems > seminfo.semmsl) {
678 1.27 christos SEM_PRINTF(("nsems out of range (0<%d<=%d)\n", nsems,
679 1.27 christos seminfo.semmsl));
680 1.70 ad error = EINVAL;
681 1.70 ad goto out;
682 1.6 mycroft }
683 1.6 mycroft if (nsems > seminfo.semmns - semtot) {
684 1.51 enami SEM_PRINTF(("not enough semaphores left "
685 1.51 enami "(need %d, got %d)\n",
686 1.27 christos nsems, seminfo.semmns - semtot));
687 1.70 ad error = ENOSPC;
688 1.70 ad goto out;
689 1.6 mycroft }
690 1.6 mycroft for (semid = 0; semid < seminfo.semmni; semid++) {
691 1.6 mycroft if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
692 1.6 mycroft break;
693 1.6 mycroft }
694 1.6 mycroft if (semid == seminfo.semmni) {
695 1.27 christos SEM_PRINTF(("no more semid_ds's available\n"));
696 1.70 ad error = ENOSPC;
697 1.70 ad goto out;
698 1.6 mycroft }
699 1.27 christos SEM_PRINTF(("semid %d is available\n", semid));
700 1.33 thorpej sema[semid].sem_perm._key = key;
701 1.61 elad sema[semid].sem_perm.cuid = kauth_cred_geteuid(cred);
702 1.61 elad sema[semid].sem_perm.uid = kauth_cred_geteuid(cred);
703 1.61 elad sema[semid].sem_perm.cgid = kauth_cred_getegid(cred);
704 1.61 elad sema[semid].sem_perm.gid = kauth_cred_getegid(cred);
705 1.6 mycroft sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
706 1.33 thorpej sema[semid].sem_perm._seq =
707 1.33 thorpej (sema[semid].sem_perm._seq + 1) & 0x7fff;
708 1.6 mycroft sema[semid].sem_nsems = nsems;
709 1.6 mycroft sema[semid].sem_otime = 0;
710 1.62 kardel sema[semid].sem_ctime = time_second;
711 1.33 thorpej sema[semid]._sem_base = &sem[semtot];
712 1.6 mycroft semtot += nsems;
713 1.33 thorpej memset(sema[semid]._sem_base, 0,
714 1.51 enami sizeof(sema[semid]._sem_base[0]) * nsems);
715 1.33 thorpej SEM_PRINTF(("sembase = %p, next = %p\n", sema[semid]._sem_base,
716 1.27 christos &sem[semtot]));
717 1.1 cgd } else {
718 1.27 christos SEM_PRINTF(("didn't find it and wasn't asked to create it\n"));
719 1.70 ad error = ENOENT;
720 1.70 ad goto out;
721 1.1 cgd }
722 1.1 cgd
723 1.70 ad found:
724 1.6 mycroft *retval = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
725 1.70 ad out:
726 1.70 ad mutex_exit(&semlock);
727 1.70 ad return (error);
728 1.1 cgd }
729 1.1 cgd
730 1.57 chs #define SMALL_SOPS 8
731 1.57 chs
732 1.1 cgd int
733 1.78 dsl sys_semop(struct lwp *l, const struct sys_semop_args *uap, register_t *retval)
734 1.23 thorpej {
735 1.78 dsl /* {
736 1.10 cgd syscallarg(int) semid;
737 1.10 cgd syscallarg(struct sembuf *) sops;
738 1.29 kleink syscallarg(size_t) nsops;
739 1.78 dsl } */
740 1.45 thorpej struct proc *p = l->l_proc;
741 1.52 enami int semid = SCARG(uap, semid), seq;
742 1.41 jdolecek size_t nsops = SCARG(uap, nsops);
743 1.57 chs struct sembuf small_sops[SMALL_SOPS];
744 1.57 chs struct sembuf *sops;
745 1.35 augustss struct semid_ds *semaptr;
746 1.35 augustss struct sembuf *sopptr = NULL;
747 1.35 augustss struct __sem *semptr = NULL;
748 1.6 mycroft struct sem_undo *suptr = NULL;
749 1.63 ad kauth_cred_t cred = l->l_cred;
750 1.70 ad int i, error;
751 1.25 christos int do_wakeup, do_undos;
752 1.1 cgd
753 1.58 christos SEM_PRINTF(("call to semop(%d, %p, %zd)\n", semid, SCARG(uap,sops), nsops));
754 1.81 ad
755 1.81 ad if (__predict_false((p->p_flag & PK_SYSVSEM) == 0)) {
756 1.81 ad mutex_enter(p->p_lock);
757 1.81 ad p->p_flag |= PK_SYSVSEM;
758 1.81 ad mutex_exit(p->p_lock);
759 1.81 ad }
760 1.81 ad
761 1.76 rmind restart:
762 1.70 ad if (nsops <= SMALL_SOPS) {
763 1.70 ad sops = small_sops;
764 1.71 ad } else if (nsops <= seminfo.semopm) {
765 1.70 ad sops = kmem_alloc(nsops * sizeof(*sops), KM_SLEEP);
766 1.71 ad } else {
767 1.70 ad SEM_PRINTF(("too many sops (max=%d, nsops=%zd)\n",
768 1.70 ad seminfo.semopm, nsops));
769 1.70 ad return (E2BIG);
770 1.70 ad }
771 1.70 ad
772 1.77 ad error = copyin(SCARG(uap, sops), sops, nsops * sizeof(sops[0]));
773 1.77 ad if (error) {
774 1.77 ad SEM_PRINTF(("error = %d from copyin(%p, %p, %zd)\n", error,
775 1.77 ad SCARG(uap, sops), &sops, nsops * sizeof(sops[0])));
776 1.79 ad if (sops != small_sops)
777 1.77 ad kmem_free(sops, nsops * sizeof(*sops));
778 1.77 ad return error;
779 1.77 ad }
780 1.77 ad
781 1.70 ad mutex_enter(&semlock);
782 1.76 rmind /* In case of reallocation, we will wait for completion */
783 1.76 rmind while (__predict_false(sem_realloc_state))
784 1.76 rmind cv_wait(&sem_realloc_cv, &semlock);
785 1.70 ad
786 1.6 mycroft semid = IPCID_TO_IX(semid); /* Convert back to zero origin */
787 1.70 ad if (semid < 0 || semid >= seminfo.semmni) {
788 1.70 ad error = EINVAL;
789 1.70 ad goto out;
790 1.70 ad }
791 1.6 mycroft
792 1.6 mycroft semaptr = &sema[semid];
793 1.52 enami seq = IPCID_TO_SEQ(SCARG(uap, semid));
794 1.11 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
795 1.70 ad semaptr->sem_perm._seq != seq) {
796 1.70 ad error = EINVAL;
797 1.70 ad goto out;
798 1.6 mycroft }
799 1.1 cgd
800 1.70 ad if ((error = ipcperm(cred, &semaptr->sem_perm, IPC_W))) {
801 1.70 ad SEM_PRINTF(("error = %d from ipaccess\n", error));
802 1.70 ad goto out;
803 1.6 mycroft }
804 1.1 cgd
805 1.52 enami for (i = 0; i < nsops; i++)
806 1.57 chs if (sops[i].sem_num >= semaptr->sem_nsems) {
807 1.70 ad error = EFBIG;
808 1.57 chs goto out;
809 1.57 chs }
810 1.52 enami
811 1.51 enami /*
812 1.6 mycroft * Loop trying to satisfy the vector of requests.
813 1.6 mycroft * If we reach a point where we must wait, any requests already
814 1.6 mycroft * performed are rolled back and we go to sleep until some other
815 1.6 mycroft * process wakes us up. At this point, we start all over again.
816 1.6 mycroft *
817 1.6 mycroft * This ensures that from the perspective of other tasks, a set
818 1.6 mycroft * of requests is atomic (never partially satisfied).
819 1.6 mycroft */
820 1.6 mycroft do_undos = 0;
821 1.1 cgd
822 1.6 mycroft for (;;) {
823 1.6 mycroft do_wakeup = 0;
824 1.1 cgd
825 1.6 mycroft for (i = 0; i < nsops; i++) {
826 1.6 mycroft sopptr = &sops[i];
827 1.33 thorpej semptr = &semaptr->_sem_base[sopptr->sem_num];
828 1.1 cgd
829 1.51 enami SEM_PRINTF(("semop: semaptr=%p, sem_base=%p, "
830 1.51 enami "semptr=%p, sem[%d]=%d : op=%d, flag=%s\n",
831 1.33 thorpej semaptr, semaptr->_sem_base, semptr,
832 1.6 mycroft sopptr->sem_num, semptr->semval, sopptr->sem_op,
833 1.51 enami (sopptr->sem_flg & IPC_NOWAIT) ?
834 1.51 enami "nowait" : "wait"));
835 1.1 cgd
836 1.6 mycroft if (sopptr->sem_op < 0) {
837 1.25 christos if ((int)(semptr->semval +
838 1.51 enami sopptr->sem_op) < 0) {
839 1.51 enami SEM_PRINTF(("semop: "
840 1.51 enami "can't do it now\n"));
841 1.6 mycroft break;
842 1.6 mycroft } else {
843 1.6 mycroft semptr->semval += sopptr->sem_op;
844 1.6 mycroft if (semptr->semval == 0 &&
845 1.6 mycroft semptr->semzcnt > 0)
846 1.6 mycroft do_wakeup = 1;
847 1.6 mycroft }
848 1.6 mycroft if (sopptr->sem_flg & SEM_UNDO)
849 1.6 mycroft do_undos = 1;
850 1.6 mycroft } else if (sopptr->sem_op == 0) {
851 1.6 mycroft if (semptr->semval > 0) {
852 1.27 christos SEM_PRINTF(("semop: not zero now\n"));
853 1.6 mycroft break;
854 1.6 mycroft }
855 1.6 mycroft } else {
856 1.6 mycroft if (semptr->semncnt > 0)
857 1.6 mycroft do_wakeup = 1;
858 1.6 mycroft semptr->semval += sopptr->sem_op;
859 1.6 mycroft if (sopptr->sem_flg & SEM_UNDO)
860 1.6 mycroft do_undos = 1;
861 1.6 mycroft }
862 1.6 mycroft }
863 1.1 cgd
864 1.6 mycroft /*
865 1.6 mycroft * Did we get through the entire vector?
866 1.6 mycroft */
867 1.6 mycroft if (i >= nsops)
868 1.6 mycroft goto done;
869 1.1 cgd
870 1.6 mycroft /*
871 1.6 mycroft * No ... rollback anything that we've already done
872 1.6 mycroft */
873 1.51 enami SEM_PRINTF(("semop: rollback 0 through %d\n", i - 1));
874 1.52 enami while (i-- > 0)
875 1.52 enami semaptr->_sem_base[sops[i].sem_num].semval -=
876 1.52 enami sops[i].sem_op;
877 1.1 cgd
878 1.6 mycroft /*
879 1.6 mycroft * If the request that we couldn't satisfy has the
880 1.6 mycroft * NOWAIT flag set then return with EAGAIN.
881 1.6 mycroft */
882 1.57 chs if (sopptr->sem_flg & IPC_NOWAIT) {
883 1.70 ad error = EAGAIN;
884 1.57 chs goto out;
885 1.57 chs }
886 1.1 cgd
887 1.6 mycroft if (sopptr->sem_op == 0)
888 1.6 mycroft semptr->semzcnt++;
889 1.6 mycroft else
890 1.6 mycroft semptr->semncnt++;
891 1.1 cgd
892 1.74 rmind sem_waiters++;
893 1.27 christos SEM_PRINTF(("semop: good night!\n"));
894 1.70 ad error = cv_wait_sig(&semcv[semid], &semlock);
895 1.70 ad SEM_PRINTF(("semop: good morning (error=%d)!\n", error));
896 1.74 rmind sem_waiters--;
897 1.74 rmind
898 1.76 rmind /* Notify reallocator, if it is waiting */
899 1.76 rmind cv_broadcast(&sem_realloc_cv);
900 1.1 cgd
901 1.6 mycroft /*
902 1.6 mycroft * Make sure that the semaphore still exists
903 1.6 mycroft */
904 1.6 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
905 1.52 enami semaptr->sem_perm._seq != seq) {
906 1.70 ad error = EIDRM;
907 1.57 chs goto out;
908 1.6 mycroft }
909 1.1 cgd
910 1.6 mycroft /*
911 1.6 mycroft * The semaphore is still alive. Readjust the count of
912 1.6 mycroft * waiting processes.
913 1.6 mycroft */
914 1.51 enami semptr = &semaptr->_sem_base[sopptr->sem_num];
915 1.6 mycroft if (sopptr->sem_op == 0)
916 1.6 mycroft semptr->semzcnt--;
917 1.6 mycroft else
918 1.6 mycroft semptr->semncnt--;
919 1.74 rmind
920 1.76 rmind /* In case of such state, restart the call */
921 1.76 rmind if (sem_realloc_state) {
922 1.76 rmind mutex_exit(&semlock);
923 1.76 rmind goto restart;
924 1.76 rmind }
925 1.76 rmind
926 1.74 rmind /* Is it really morning, or was our sleep interrupted? */
927 1.76 rmind if (error != 0) {
928 1.70 ad error = EINTR;
929 1.57 chs goto out;
930 1.57 chs }
931 1.50 christos SEM_PRINTF(("semop: good morning!\n"));
932 1.6 mycroft }
933 1.1 cgd
934 1.6 mycroft done:
935 1.6 mycroft /*
936 1.6 mycroft * Process any SEM_UNDO requests.
937 1.6 mycroft */
938 1.6 mycroft if (do_undos) {
939 1.5 mycroft for (i = 0; i < nsops; i++) {
940 1.6 mycroft /*
941 1.6 mycroft * We only need to deal with SEM_UNDO's for non-zero
942 1.6 mycroft * op's.
943 1.6 mycroft */
944 1.6 mycroft int adjval;
945 1.1 cgd
946 1.6 mycroft if ((sops[i].sem_flg & SEM_UNDO) == 0)
947 1.6 mycroft continue;
948 1.6 mycroft adjval = sops[i].sem_op;
949 1.6 mycroft if (adjval == 0)
950 1.6 mycroft continue;
951 1.70 ad error = semundo_adjust(p, &suptr, semid,
952 1.6 mycroft sops[i].sem_num, -adjval);
953 1.70 ad if (error == 0)
954 1.6 mycroft continue;
955 1.1 cgd
956 1.6 mycroft /*
957 1.6 mycroft * Oh-Oh! We ran out of either sem_undo's or undo's.
958 1.6 mycroft * Rollback the adjustments to this point and then
959 1.6 mycroft * rollback the semaphore ups and down so we can return
960 1.6 mycroft * with an error with all structures restored. We
961 1.6 mycroft * rollback the undo's in the exact reverse order that
962 1.6 mycroft * we applied them. This guarantees that we won't run
963 1.6 mycroft * out of space as we roll things back out.
964 1.6 mycroft */
965 1.52 enami while (i-- > 0) {
966 1.52 enami if ((sops[i].sem_flg & SEM_UNDO) == 0)
967 1.6 mycroft continue;
968 1.52 enami adjval = sops[i].sem_op;
969 1.6 mycroft if (adjval == 0)
970 1.6 mycroft continue;
971 1.6 mycroft if (semundo_adjust(p, &suptr, semid,
972 1.52 enami sops[i].sem_num, adjval) != 0)
973 1.1 cgd panic("semop - can't undo undos");
974 1.6 mycroft }
975 1.1 cgd
976 1.54 enami for (i = 0; i < nsops; i++)
977 1.54 enami semaptr->_sem_base[sops[i].sem_num].semval -=
978 1.54 enami sops[i].sem_op;
979 1.1 cgd
980 1.70 ad SEM_PRINTF(("error = %d from semundo_adjust\n", error));
981 1.57 chs goto out;
982 1.1 cgd } /* loop through the sops */
983 1.6 mycroft } /* if (do_undos) */
984 1.1 cgd
985 1.6 mycroft /* We're definitely done - set the sempid's */
986 1.6 mycroft for (i = 0; i < nsops; i++) {
987 1.1 cgd sopptr = &sops[i];
988 1.33 thorpej semptr = &semaptr->_sem_base[sopptr->sem_num];
989 1.1 cgd semptr->sempid = p->p_pid;
990 1.6 mycroft }
991 1.1 cgd
992 1.55 briggs /* Update sem_otime */
993 1.62 kardel semaptr->sem_otime = time_second;
994 1.55 briggs
995 1.6 mycroft /* Do a wakeup if any semaphore was up'd. */
996 1.6 mycroft if (do_wakeup) {
997 1.27 christos SEM_PRINTF(("semop: doing wakeup\n"));
998 1.70 ad cv_broadcast(&semcv[semid]);
999 1.27 christos SEM_PRINTF(("semop: back from wakeup\n"));
1000 1.6 mycroft }
1001 1.27 christos SEM_PRINTF(("semop: done\n"));
1002 1.6 mycroft *retval = 0;
1003 1.57 chs
1004 1.70 ad out:
1005 1.70 ad mutex_exit(&semlock);
1006 1.79 ad if (sops != small_sops)
1007 1.70 ad kmem_free(sops, nsops * sizeof(*sops));
1008 1.70 ad return error;
1009 1.1 cgd }
1010 1.1 cgd
1011 1.1 cgd /*
1012 1.51 enami * Go through the undo structures for this process and apply the
1013 1.51 enami * adjustments to semaphores.
1014 1.1 cgd */
1015 1.44 christos /*ARGSUSED*/
1016 1.25 christos void
1017 1.66 yamt semexit(struct proc *p, void *v)
1018 1.1 cgd {
1019 1.35 augustss struct sem_undo *suptr;
1020 1.35 augustss struct sem_undo **supptr;
1021 1.1 cgd
1022 1.81 ad if ((p->p_flag & PK_SYSVSEM) == 0)
1023 1.81 ad return;
1024 1.81 ad
1025 1.70 ad mutex_enter(&semlock);
1026 1.70 ad
1027 1.6 mycroft /*
1028 1.51 enami * Go through the chain of undo vectors looking for one
1029 1.51 enami * associated with this process.
1030 1.17 mycroft */
1031 1.17 mycroft
1032 1.17 mycroft for (supptr = &semu_list; (suptr = *supptr) != NULL;
1033 1.17 mycroft supptr = &suptr->un_next) {
1034 1.17 mycroft if (suptr->un_proc == p)
1035 1.17 mycroft break;
1036 1.17 mycroft }
1037 1.17 mycroft
1038 1.17 mycroft /*
1039 1.37 sommerfe * If there is no undo vector, skip to the end.
1040 1.14 mycroft */
1041 1.14 mycroft
1042 1.70 ad if (suptr == NULL) {
1043 1.70 ad mutex_exit(&semlock);
1044 1.37 sommerfe return;
1045 1.70 ad }
1046 1.51 enami
1047 1.14 mycroft /*
1048 1.37 sommerfe * We now have an undo vector for this process.
1049 1.15 mycroft */
1050 1.1 cgd
1051 1.27 christos SEM_PRINTF(("proc @%p has undo structure with %d entries\n", p,
1052 1.27 christos suptr->un_cnt));
1053 1.1 cgd
1054 1.5 mycroft /*
1055 1.5 mycroft * If there are any active undo elements then process them.
1056 1.5 mycroft */
1057 1.5 mycroft if (suptr->un_cnt > 0) {
1058 1.6 mycroft int ix;
1059 1.1 cgd
1060 1.6 mycroft for (ix = 0; ix < suptr->un_cnt; ix++) {
1061 1.6 mycroft int semid = suptr->un_ent[ix].un_id;
1062 1.6 mycroft int semnum = suptr->un_ent[ix].un_num;
1063 1.6 mycroft int adjval = suptr->un_ent[ix].un_adjval;
1064 1.6 mycroft struct semid_ds *semaptr;
1065 1.6 mycroft
1066 1.6 mycroft semaptr = &sema[semid];
1067 1.6 mycroft if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
1068 1.6 mycroft panic("semexit - semid not allocated");
1069 1.6 mycroft if (semnum >= semaptr->sem_nsems)
1070 1.6 mycroft panic("semexit - semnum out of range");
1071 1.6 mycroft
1072 1.51 enami SEM_PRINTF(("semexit: %p id=%d num=%d(adj=%d) ; "
1073 1.51 enami "sem=%d\n",
1074 1.6 mycroft suptr->un_proc, suptr->un_ent[ix].un_id,
1075 1.6 mycroft suptr->un_ent[ix].un_num,
1076 1.6 mycroft suptr->un_ent[ix].un_adjval,
1077 1.33 thorpej semaptr->_sem_base[semnum].semval));
1078 1.6 mycroft
1079 1.14 mycroft if (adjval < 0 &&
1080 1.33 thorpej semaptr->_sem_base[semnum].semval < -adjval)
1081 1.33 thorpej semaptr->_sem_base[semnum].semval = 0;
1082 1.14 mycroft else
1083 1.33 thorpej semaptr->_sem_base[semnum].semval += adjval;
1084 1.1 cgd
1085 1.70 ad cv_broadcast(&semcv[semid]);
1086 1.27 christos SEM_PRINTF(("semexit: back from wakeup\n"));
1087 1.6 mycroft }
1088 1.5 mycroft }
1089 1.1 cgd
1090 1.5 mycroft /*
1091 1.5 mycroft * Deallocate the undo vector.
1092 1.5 mycroft */
1093 1.27 christos SEM_PRINTF(("removing vector\n"));
1094 1.5 mycroft suptr->un_proc = NULL;
1095 1.5 mycroft *supptr = suptr->un_next;
1096 1.70 ad mutex_exit(&semlock);
1097 1.1 cgd }
1098 1.74 rmind
1099 1.74 rmind /*
1100 1.74 rmind * Sysctl initialization and nodes.
1101 1.74 rmind */
1102 1.74 rmind
1103 1.74 rmind static int
1104 1.74 rmind sysctl_ipc_semmni(SYSCTLFN_ARGS)
1105 1.74 rmind {
1106 1.74 rmind int newsize, error;
1107 1.74 rmind struct sysctlnode node;
1108 1.74 rmind node = *rnode;
1109 1.74 rmind node.sysctl_data = &newsize;
1110 1.74 rmind
1111 1.74 rmind newsize = seminfo.semmni;
1112 1.74 rmind error = sysctl_lookup(SYSCTLFN_CALL(&node));
1113 1.74 rmind if (error || newp == NULL)
1114 1.74 rmind return error;
1115 1.74 rmind
1116 1.74 rmind return semrealloc(newsize, seminfo.semmns, seminfo.semmnu);
1117 1.74 rmind }
1118 1.74 rmind
1119 1.74 rmind static int
1120 1.74 rmind sysctl_ipc_semmns(SYSCTLFN_ARGS)
1121 1.74 rmind {
1122 1.74 rmind int newsize, error;
1123 1.74 rmind struct sysctlnode node;
1124 1.74 rmind node = *rnode;
1125 1.74 rmind node.sysctl_data = &newsize;
1126 1.74 rmind
1127 1.74 rmind newsize = seminfo.semmns;
1128 1.74 rmind error = sysctl_lookup(SYSCTLFN_CALL(&node));
1129 1.74 rmind if (error || newp == NULL)
1130 1.74 rmind return error;
1131 1.74 rmind
1132 1.74 rmind return semrealloc(seminfo.semmni, newsize, seminfo.semmnu);
1133 1.74 rmind }
1134 1.74 rmind
1135 1.74 rmind static int
1136 1.74 rmind sysctl_ipc_semmnu(SYSCTLFN_ARGS)
1137 1.74 rmind {
1138 1.74 rmind int newsize, error;
1139 1.74 rmind struct sysctlnode node;
1140 1.74 rmind node = *rnode;
1141 1.74 rmind node.sysctl_data = &newsize;
1142 1.74 rmind
1143 1.74 rmind newsize = seminfo.semmnu;
1144 1.74 rmind error = sysctl_lookup(SYSCTLFN_CALL(&node));
1145 1.74 rmind if (error || newp == NULL)
1146 1.74 rmind return error;
1147 1.74 rmind
1148 1.74 rmind return semrealloc(seminfo.semmni, seminfo.semmns, newsize);
1149 1.74 rmind }
1150 1.74 rmind
1151 1.74 rmind SYSCTL_SETUP(sysctl_ipc_sem_setup, "sysctl kern.ipc subtree setup")
1152 1.74 rmind {
1153 1.74 rmind const struct sysctlnode *node = NULL;
1154 1.74 rmind
1155 1.74 rmind sysctl_createv(clog, 0, NULL, &node,
1156 1.74 rmind CTLFLAG_PERMANENT,
1157 1.74 rmind CTLTYPE_NODE, "ipc",
1158 1.74 rmind SYSCTL_DESCR("SysV IPC options"),
1159 1.74 rmind NULL, 0, NULL, 0,
1160 1.74 rmind CTL_KERN, KERN_SYSVIPC, CTL_EOL);
1161 1.74 rmind
1162 1.74 rmind if (node == NULL)
1163 1.74 rmind return;
1164 1.74 rmind
1165 1.74 rmind sysctl_createv(clog, 0, &node, NULL,
1166 1.74 rmind CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1167 1.74 rmind CTLTYPE_INT, "semmni",
1168 1.74 rmind SYSCTL_DESCR("Max number of number of semaphore identifiers"),
1169 1.74 rmind sysctl_ipc_semmni, 0, &seminfo.semmni, 0,
1170 1.74 rmind CTL_CREATE, CTL_EOL);
1171 1.74 rmind sysctl_createv(clog, 0, &node, NULL,
1172 1.74 rmind CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1173 1.74 rmind CTLTYPE_INT, "semmns",
1174 1.74 rmind SYSCTL_DESCR("Max number of number of semaphores in system"),
1175 1.74 rmind sysctl_ipc_semmns, 0, &seminfo.semmns, 0,
1176 1.74 rmind CTL_CREATE, CTL_EOL);
1177 1.74 rmind sysctl_createv(clog, 0, &node, NULL,
1178 1.74 rmind CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1179 1.74 rmind CTLTYPE_INT, "semmnu",
1180 1.74 rmind SYSCTL_DESCR("Max number of undo structures in system"),
1181 1.74 rmind sysctl_ipc_semmnu, 0, &seminfo.semmnu, 0,
1182 1.74 rmind CTL_CREATE, CTL_EOL);
1183 1.74 rmind }
1184