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