uipc_sem.c revision 1.25.2.1 1 1.25.2.1 wrstuden /* $NetBSD: uipc_sem.c,v 1.25.2.1 2008/05/10 23:49:05 wrstuden Exp $ */
2 1.3 thorpej
3 1.3 thorpej /*-
4 1.21 ad * Copyright (c) 2003, 2007 The NetBSD Foundation, Inc.
5 1.3 thorpej * All rights reserved.
6 1.3 thorpej *
7 1.3 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.21 ad * by Jason R. Thorpe of Wasabi Systems, Inc, and by Andrew Doran.
9 1.3 thorpej *
10 1.3 thorpej * Redistribution and use in source and binary forms, with or without
11 1.3 thorpej * modification, are permitted provided that the following conditions
12 1.3 thorpej * are met:
13 1.3 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.3 thorpej * notice, this list of conditions and the following disclaimer.
15 1.3 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.3 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.3 thorpej * documentation and/or other materials provided with the distribution.
18 1.3 thorpej *
19 1.3 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.3 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.3 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.3 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.3 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.3 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.3 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.3 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.3 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.3 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.3 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.3 thorpej */
31 1.1 christos
32 1.1 christos /*
33 1.1 christos * Copyright (c) 2002 Alfred Perlstein <alfred (at) FreeBSD.org>
34 1.1 christos * All rights reserved.
35 1.1 christos *
36 1.1 christos * Redistribution and use in source and binary forms, with or without
37 1.1 christos * modification, are permitted provided that the following conditions
38 1.1 christos * are met:
39 1.1 christos * 1. Redistributions of source code must retain the above copyright
40 1.1 christos * notice, this list of conditions and the following disclaimer.
41 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 christos * notice, this list of conditions and the following disclaimer in the
43 1.1 christos * documentation and/or other materials provided with the distribution.
44 1.1 christos *
45 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 1.1 christos * SUCH DAMAGE.
56 1.1 christos */
57 1.9 lukem
58 1.9 lukem #include <sys/cdefs.h>
59 1.25.2.1 wrstuden __KERNEL_RCSID(0, "$NetBSD: uipc_sem.c,v 1.25.2.1 2008/05/10 23:49:05 wrstuden Exp $");
60 1.1 christos
61 1.1 christos #include "opt_posix.h"
62 1.1 christos
63 1.1 christos #include <sys/param.h>
64 1.1 christos #include <sys/systm.h>
65 1.1 christos #include <sys/kernel.h>
66 1.1 christos #include <sys/proc.h>
67 1.1 christos #include <sys/ksem.h>
68 1.25.2.1 wrstuden #include <sys/sa.h>
69 1.1 christos #include <sys/syscall.h>
70 1.1 christos #include <sys/stat.h>
71 1.21 ad #include <sys/kmem.h>
72 1.1 christos #include <sys/fcntl.h>
73 1.14 elad #include <sys/kauth.h>
74 1.22 rmind #include <sys/sysctl.h>
75 1.1 christos
76 1.1 christos #include <sys/mount.h>
77 1.1 christos
78 1.1 christos #include <sys/syscallargs.h>
79 1.1 christos
80 1.22 rmind #define SEM_MAX 128
81 1.1 christos #define SEM_MAX_NAMELEN 14
82 1.1 christos #define SEM_VALUE_MAX (~0U)
83 1.13 cube #define SEM_HASHTBL_SIZE 13
84 1.1 christos
85 1.13 cube #define SEM_TO_ID(x) (((x)->ks_id))
86 1.13 cube #define SEM_HASH(id) ((id) % SEM_HASHTBL_SIZE)
87 1.4 thorpej
88 1.4 thorpej MALLOC_DEFINE(M_SEM, "p1003_1b_sem", "p1003_1b semaphores");
89 1.1 christos
90 1.3 thorpej /*
91 1.3 thorpej * Note: to read the ks_name member, you need either the ks_interlock
92 1.3 thorpej * or the ksem_slock. To write the ks_name member, you need both. Make
93 1.3 thorpej * sure the order is ksem_slock -> ks_interlock.
94 1.3 thorpej */
95 1.1 christos struct ksem {
96 1.1 christos LIST_ENTRY(ksem) ks_entry; /* global list entry */
97 1.13 cube LIST_ENTRY(ksem) ks_hash; /* hash list entry */
98 1.20 ad kmutex_t ks_interlock; /* lock on this ksem */
99 1.20 ad kcondvar_t ks_cv; /* condition variable */
100 1.21 ad unsigned int ks_ref; /* number of references */
101 1.1 christos char *ks_name; /* if named, this is the name */
102 1.21 ad size_t ks_namelen; /* length of name */
103 1.1 christos mode_t ks_mode; /* protection bits */
104 1.1 christos uid_t ks_uid; /* creator uid */
105 1.1 christos gid_t ks_gid; /* creator gid */
106 1.1 christos unsigned int ks_value; /* current value */
107 1.3 thorpej unsigned int ks_waiters; /* number of waiters */
108 1.13 cube semid_t ks_id; /* unique identifier */
109 1.3 thorpej };
110 1.3 thorpej
111 1.3 thorpej struct ksem_ref {
112 1.3 thorpej LIST_ENTRY(ksem_ref) ksr_list;
113 1.3 thorpej struct ksem *ksr_ksem;
114 1.3 thorpej };
115 1.3 thorpej
116 1.3 thorpej struct ksem_proc {
117 1.20 ad krwlock_t kp_lock;
118 1.3 thorpej LIST_HEAD(, ksem_ref) kp_ksems;
119 1.1 christos };
120 1.1 christos
121 1.13 cube LIST_HEAD(ksem_list, ksem);
122 1.13 cube
123 1.1 christos /*
124 1.3 thorpej * ksem_slock protects ksem_head and nsems. Only named semaphores go
125 1.3 thorpej * onto ksem_head.
126 1.1 christos */
127 1.20 ad static kmutex_t ksem_mutex;
128 1.13 cube static struct ksem_list ksem_head = LIST_HEAD_INITIALIZER(&ksem_head);
129 1.13 cube static struct ksem_list ksem_hash[SEM_HASHTBL_SIZE];
130 1.22 rmind static u_int sem_max = SEM_MAX;
131 1.3 thorpej static int nsems = 0;
132 1.1 christos
133 1.13 cube /*
134 1.13 cube * ksem_counter is the last assigned semid_t. It needs to be COMPAT_NETBSD32
135 1.13 cube * friendly, even though semid_t itself is defined as uintptr_t.
136 1.13 cube */
137 1.13 cube static uint32_t ksem_counter = 1;
138 1.13 cube
139 1.16 thorpej static specificdata_key_t ksem_specificdata_key;
140 1.13 cube
141 1.3 thorpej static void
142 1.3 thorpej ksem_free(struct ksem *ks)
143 1.3 thorpej {
144 1.1 christos
145 1.21 ad KASSERT(mutex_owned(&ks->ks_interlock));
146 1.20 ad
147 1.3 thorpej /*
148 1.3 thorpej * If the ksem is anonymous (or has been unlinked), then
149 1.3 thorpej * this is the end if its life.
150 1.3 thorpej */
151 1.3 thorpej if (ks->ks_name == NULL) {
152 1.20 ad mutex_exit(&ks->ks_interlock);
153 1.20 ad mutex_destroy(&ks->ks_interlock);
154 1.20 ad cv_destroy(&ks->ks_cv);
155 1.1 christos
156 1.20 ad mutex_enter(&ksem_mutex);
157 1.3 thorpej nsems--;
158 1.13 cube LIST_REMOVE(ks, ks_hash);
159 1.20 ad mutex_exit(&ksem_mutex);
160 1.13 cube
161 1.21 ad kmem_free(ks, sizeof(*ks));
162 1.3 thorpej return;
163 1.3 thorpej }
164 1.20 ad mutex_exit(&ks->ks_interlock);
165 1.3 thorpej }
166 1.1 christos
167 1.12 perry static inline void
168 1.3 thorpej ksem_addref(struct ksem *ks)
169 1.1 christos {
170 1.1 christos
171 1.21 ad KASSERT(mutex_owned(&ks->ks_interlock));
172 1.1 christos ks->ks_ref++;
173 1.21 ad KASSERT(ks->ks_ref != 0);
174 1.1 christos }
175 1.1 christos
176 1.12 perry static inline void
177 1.3 thorpej ksem_delref(struct ksem *ks)
178 1.1 christos {
179 1.1 christos
180 1.21 ad KASSERT(mutex_owned(&ks->ks_interlock));
181 1.21 ad KASSERT(ks->ks_ref != 0);
182 1.3 thorpej if (--ks->ks_ref == 0) {
183 1.1 christos ksem_free(ks);
184 1.3 thorpej return;
185 1.3 thorpej }
186 1.20 ad mutex_exit(&ks->ks_interlock);
187 1.3 thorpej }
188 1.3 thorpej
189 1.3 thorpej static struct ksem_proc *
190 1.3 thorpej ksem_proc_alloc(void)
191 1.3 thorpej {
192 1.3 thorpej struct ksem_proc *kp;
193 1.3 thorpej
194 1.21 ad kp = kmem_alloc(sizeof(*kp), KM_SLEEP);
195 1.20 ad rw_init(&kp->kp_lock);
196 1.3 thorpej LIST_INIT(&kp->kp_ksems);
197 1.3 thorpej
198 1.3 thorpej return (kp);
199 1.1 christos }
200 1.1 christos
201 1.3 thorpej static void
202 1.16 thorpej ksem_proc_dtor(void *arg)
203 1.16 thorpej {
204 1.16 thorpej struct ksem_proc *kp = arg;
205 1.16 thorpej struct ksem_ref *ksr;
206 1.16 thorpej
207 1.20 ad rw_enter(&kp->kp_lock, RW_WRITER);
208 1.16 thorpej
209 1.16 thorpej while ((ksr = LIST_FIRST(&kp->kp_ksems)) != NULL) {
210 1.16 thorpej LIST_REMOVE(ksr, ksr_list);
211 1.20 ad mutex_enter(&ksr->ksr_ksem->ks_interlock);
212 1.16 thorpej ksem_delref(ksr->ksr_ksem);
213 1.21 ad kmem_free(ksr, sizeof(*ksr));
214 1.16 thorpej }
215 1.16 thorpej
216 1.20 ad rw_exit(&kp->kp_lock);
217 1.20 ad rw_destroy(&kp->kp_lock);
218 1.21 ad kmem_free(kp, sizeof(*kp));
219 1.16 thorpej }
220 1.16 thorpej
221 1.16 thorpej static void
222 1.3 thorpej ksem_add_proc(struct proc *p, struct ksem *ks)
223 1.3 thorpej {
224 1.3 thorpej struct ksem_proc *kp;
225 1.3 thorpej struct ksem_ref *ksr;
226 1.3 thorpej
227 1.16 thorpej kp = proc_getspecific(p, ksem_specificdata_key);
228 1.16 thorpej if (kp == NULL) {
229 1.3 thorpej kp = ksem_proc_alloc();
230 1.16 thorpej proc_setspecific(p, ksem_specificdata_key, kp);
231 1.16 thorpej }
232 1.3 thorpej
233 1.21 ad ksr = kmem_alloc(sizeof(*ksr), KM_SLEEP);
234 1.3 thorpej ksr->ksr_ksem = ks;
235 1.3 thorpej
236 1.20 ad rw_enter(&kp->kp_lock, RW_WRITER);
237 1.3 thorpej LIST_INSERT_HEAD(&kp->kp_ksems, ksr, ksr_list);
238 1.20 ad rw_exit(&kp->kp_lock);
239 1.3 thorpej }
240 1.3 thorpej
241 1.3 thorpej /* We MUST have a write lock on the ksem_proc list! */
242 1.3 thorpej static struct ksem_ref *
243 1.3 thorpej ksem_drop_proc(struct ksem_proc *kp, struct ksem *ks)
244 1.1 christos {
245 1.3 thorpej struct ksem_ref *ksr;
246 1.1 christos
247 1.21 ad KASSERT(mutex_owned(&ks->ks_interlock));
248 1.3 thorpej LIST_FOREACH(ksr, &kp->kp_ksems, ksr_list) {
249 1.3 thorpej if (ksr->ksr_ksem == ks) {
250 1.3 thorpej ksem_delref(ks);
251 1.3 thorpej LIST_REMOVE(ksr, ksr_list);
252 1.3 thorpej return (ksr);
253 1.3 thorpej }
254 1.1 christos }
255 1.3 thorpej #ifdef DIAGNOSTIC
256 1.3 thorpej panic("ksem_drop_proc: ksem_proc %p ksem %p", kp, ks);
257 1.3 thorpej #endif
258 1.1 christos return (NULL);
259 1.1 christos }
260 1.1 christos
261 1.3 thorpej static int
262 1.15 ad ksem_perm(struct lwp *l, struct ksem *ks)
263 1.3 thorpej {
264 1.14 elad kauth_cred_t uc;
265 1.3 thorpej
266 1.21 ad KASSERT(mutex_owned(&ks->ks_interlock));
267 1.15 ad uc = l->l_cred;
268 1.14 elad if ((kauth_cred_geteuid(uc) == ks->ks_uid && (ks->ks_mode & S_IWUSR) != 0) ||
269 1.14 elad (kauth_cred_getegid(uc) == ks->ks_gid && (ks->ks_mode & S_IWGRP) != 0) ||
270 1.14 elad (ks->ks_mode & S_IWOTH) != 0 ||
271 1.19 elad kauth_authorize_generic(uc, KAUTH_GENERIC_ISSUSER, NULL) == 0)
272 1.3 thorpej return (0);
273 1.3 thorpej return (EPERM);
274 1.3 thorpej }
275 1.3 thorpej
276 1.1 christos static struct ksem *
277 1.13 cube ksem_lookup_byid(semid_t id)
278 1.13 cube {
279 1.13 cube struct ksem *ks;
280 1.13 cube
281 1.21 ad KASSERT(mutex_owned(&ksem_mutex));
282 1.13 cube LIST_FOREACH(ks, &ksem_hash[SEM_HASH(id)], ks_hash) {
283 1.13 cube if (ks->ks_id == id)
284 1.13 cube return ks;
285 1.13 cube }
286 1.13 cube return NULL;
287 1.13 cube }
288 1.13 cube
289 1.13 cube static struct ksem *
290 1.3 thorpej ksem_lookup_byname(const char *name)
291 1.1 christos {
292 1.1 christos struct ksem *ks;
293 1.1 christos
294 1.21 ad KASSERT(mutex_owned(&ksem_mutex));
295 1.3 thorpej LIST_FOREACH(ks, &ksem_head, ks_entry) {
296 1.3 thorpej if (strcmp(ks->ks_name, name) == 0) {
297 1.20 ad mutex_enter(&ks->ks_interlock);
298 1.1 christos return (ks);
299 1.3 thorpej }
300 1.3 thorpej }
301 1.1 christos return (NULL);
302 1.1 christos }
303 1.1 christos
304 1.1 christos static int
305 1.15 ad ksem_create(struct lwp *l, const char *name, struct ksem **ksret,
306 1.3 thorpej mode_t mode, unsigned int value)
307 1.1 christos {
308 1.1 christos struct ksem *ret;
309 1.14 elad kauth_cred_t uc;
310 1.1 christos size_t len;
311 1.1 christos
312 1.15 ad uc = l->l_cred;
313 1.1 christos if (value > SEM_VALUE_MAX)
314 1.1 christos return (EINVAL);
315 1.21 ad ret = kmem_zalloc(sizeof(*ret), KM_SLEEP);
316 1.1 christos if (name != NULL) {
317 1.1 christos len = strlen(name);
318 1.1 christos if (len > SEM_MAX_NAMELEN) {
319 1.21 ad kmem_free(ret, sizeof(*ret));
320 1.1 christos return (ENAMETOOLONG);
321 1.1 christos }
322 1.1 christos /* name must start with a '/' but not contain one. */
323 1.1 christos if (*name != '/' || len < 2 || strchr(name + 1, '/') != NULL) {
324 1.21 ad kmem_free(ret, sizeof(*ret));
325 1.1 christos return (EINVAL);
326 1.1 christos }
327 1.21 ad ret->ks_namelen = len + 1;
328 1.21 ad ret->ks_name = kmem_alloc(ret->ks_namelen, KM_SLEEP);
329 1.6 itojun strlcpy(ret->ks_name, name, len + 1);
330 1.3 thorpej } else
331 1.1 christos ret->ks_name = NULL;
332 1.1 christos ret->ks_mode = mode;
333 1.1 christos ret->ks_value = value;
334 1.1 christos ret->ks_ref = 1;
335 1.1 christos ret->ks_waiters = 0;
336 1.14 elad ret->ks_uid = kauth_cred_geteuid(uc);
337 1.14 elad ret->ks_gid = kauth_cred_getegid(uc);
338 1.20 ad mutex_init(&ret->ks_interlock, MUTEX_DEFAULT, IPL_NONE);
339 1.20 ad cv_init(&ret->ks_cv, "psem");
340 1.3 thorpej
341 1.20 ad mutex_enter(&ksem_mutex);
342 1.22 rmind if (nsems >= sem_max) {
343 1.20 ad mutex_exit(&ksem_mutex);
344 1.3 thorpej if (ret->ks_name != NULL)
345 1.21 ad kmem_free(ret->ks_name, ret->ks_namelen);
346 1.21 ad kmem_free(ret, sizeof(*ret));
347 1.3 thorpej return (ENFILE);
348 1.1 christos }
349 1.3 thorpej nsems++;
350 1.13 cube while (ksem_lookup_byid(ksem_counter) != NULL) {
351 1.13 cube ksem_counter++;
352 1.13 cube /* 0 is a special value for libpthread */
353 1.13 cube if (ksem_counter == 0)
354 1.13 cube ksem_counter++;
355 1.13 cube }
356 1.13 cube ret->ks_id = ksem_counter;
357 1.13 cube LIST_INSERT_HEAD(&ksem_hash[SEM_HASH(ret->ks_id)], ret, ks_hash);
358 1.20 ad mutex_exit(&ksem_mutex);
359 1.3 thorpej
360 1.3 thorpej *ksret = ret;
361 1.3 thorpej return (0);
362 1.1 christos }
363 1.1 christos
364 1.1 christos int
365 1.23 dsl sys__ksem_init(struct lwp *l, const struct sys__ksem_init_args *uap, register_t *retval)
366 1.1 christos {
367 1.23 dsl /* {
368 1.1 christos unsigned int value;
369 1.1 christos semid_t *idp;
370 1.23 dsl } */
371 1.13 cube
372 1.13 cube return do_ksem_init(l, SCARG(uap, value), SCARG(uap, idp), copyout);
373 1.13 cube }
374 1.13 cube
375 1.13 cube int
376 1.13 cube do_ksem_init(struct lwp *l, unsigned int value, semid_t *idp,
377 1.13 cube copyout_t docopyout)
378 1.13 cube {
379 1.1 christos struct ksem *ks;
380 1.1 christos semid_t id;
381 1.1 christos int error;
382 1.1 christos
383 1.3 thorpej /* Note the mode does not matter for anonymous semaphores. */
384 1.15 ad error = ksem_create(l, NULL, &ks, 0, value);
385 1.1 christos if (error)
386 1.1 christos return (error);
387 1.1 christos id = SEM_TO_ID(ks);
388 1.13 cube error = (*docopyout)(&id, idp, sizeof(id));
389 1.1 christos if (error) {
390 1.20 ad mutex_enter(&ks->ks_interlock);
391 1.3 thorpej ksem_delref(ks);
392 1.1 christos return (error);
393 1.1 christos }
394 1.3 thorpej
395 1.3 thorpej ksem_add_proc(l->l_proc, ks);
396 1.3 thorpej
397 1.3 thorpej return (0);
398 1.1 christos }
399 1.1 christos
400 1.1 christos int
401 1.23 dsl sys__ksem_open(struct lwp *l, const struct sys__ksem_open_args *uap, register_t *retval)
402 1.1 christos {
403 1.23 dsl /* {
404 1.1 christos const char *name;
405 1.1 christos int oflag;
406 1.1 christos mode_t mode;
407 1.1 christos unsigned int value;
408 1.10 perry semid_t *idp;
409 1.23 dsl } */
410 1.13 cube
411 1.13 cube return do_ksem_open(l, SCARG(uap, name), SCARG(uap, oflag),
412 1.13 cube SCARG(uap, mode), SCARG(uap, value), SCARG(uap, idp), copyout);
413 1.13 cube }
414 1.13 cube
415 1.13 cube int
416 1.13 cube do_ksem_open(struct lwp *l, const char *semname, int oflag, mode_t mode,
417 1.13 cube unsigned int value, semid_t *idp, copyout_t docopyout)
418 1.13 cube {
419 1.1 christos char name[SEM_MAX_NAMELEN + 1];
420 1.1 christos size_t done;
421 1.1 christos int error;
422 1.1 christos struct ksem *ksnew, *ks;
423 1.1 christos semid_t id;
424 1.1 christos
425 1.13 cube error = copyinstr(semname, name, sizeof(name), &done);
426 1.1 christos if (error)
427 1.1 christos return (error);
428 1.1 christos
429 1.1 christos ksnew = NULL;
430 1.20 ad mutex_enter(&ksem_mutex);
431 1.1 christos ks = ksem_lookup_byname(name);
432 1.3 thorpej
433 1.3 thorpej /* Found one? */
434 1.3 thorpej if (ks != NULL) {
435 1.3 thorpej /* Check for exclusive create. */
436 1.13 cube if (oflag & O_EXCL) {
437 1.20 ad mutex_exit(&ks->ks_interlock);
438 1.20 ad mutex_exit(&ksem_mutex);
439 1.3 thorpej return (EEXIST);
440 1.1 christos }
441 1.3 thorpej found_one:
442 1.1 christos /*
443 1.3 thorpej * Verify permissions. If we can access it, add
444 1.3 thorpej * this process's reference.
445 1.1 christos */
446 1.21 ad KASSERT(mutex_owned(&ks->ks_interlock));
447 1.15 ad error = ksem_perm(l, ks);
448 1.3 thorpej if (error == 0)
449 1.3 thorpej ksem_addref(ks);
450 1.20 ad mutex_exit(&ks->ks_interlock);
451 1.20 ad mutex_exit(&ksem_mutex);
452 1.1 christos if (error)
453 1.1 christos return (error);
454 1.3 thorpej
455 1.1 christos id = SEM_TO_ID(ks);
456 1.13 cube error = (*docopyout)(&id, idp, sizeof(id));
457 1.1 christos if (error) {
458 1.20 ad mutex_enter(&ks->ks_interlock);
459 1.3 thorpej ksem_delref(ks);
460 1.1 christos return (error);
461 1.1 christos }
462 1.3 thorpej
463 1.3 thorpej ksem_add_proc(l->l_proc, ks);
464 1.3 thorpej
465 1.3 thorpej return (0);
466 1.3 thorpej }
467 1.3 thorpej
468 1.3 thorpej /*
469 1.3 thorpej * didn't ask for creation? error.
470 1.3 thorpej */
471 1.13 cube if ((oflag & O_CREAT) == 0) {
472 1.20 ad mutex_exit(&ksem_mutex);
473 1.3 thorpej return (ENOENT);
474 1.1 christos }
475 1.1 christos
476 1.3 thorpej /*
477 1.3 thorpej * We may block during creation, so drop the lock.
478 1.3 thorpej */
479 1.20 ad mutex_exit(&ksem_mutex);
480 1.15 ad error = ksem_create(l, name, &ksnew, mode, value);
481 1.3 thorpej if (error != 0)
482 1.3 thorpej return (error);
483 1.3 thorpej
484 1.3 thorpej id = SEM_TO_ID(ksnew);
485 1.13 cube error = (*docopyout)(&id, idp, sizeof(id));
486 1.3 thorpej if (error) {
487 1.21 ad kmem_free(ksnew->ks_name, ksnew->ks_namelen);
488 1.3 thorpej ksnew->ks_name = NULL;
489 1.1 christos
490 1.20 ad mutex_enter(&ksnew->ks_interlock);
491 1.3 thorpej ksem_delref(ksnew);
492 1.3 thorpej return (error);
493 1.3 thorpej }
494 1.1 christos
495 1.3 thorpej /*
496 1.3 thorpej * We need to make sure we haven't lost a race while
497 1.3 thorpej * allocating during creation.
498 1.3 thorpej */
499 1.20 ad mutex_enter(&ksem_mutex);
500 1.3 thorpej if ((ks = ksem_lookup_byname(name)) != NULL) {
501 1.13 cube if (oflag & O_EXCL) {
502 1.20 ad mutex_exit(&ks->ks_interlock);
503 1.20 ad mutex_exit(&ksem_mutex);
504 1.1 christos
505 1.21 ad kmem_free(ksnew->ks_name, ksnew->ks_namelen);
506 1.3 thorpej ksnew->ks_name = NULL;
507 1.1 christos
508 1.20 ad mutex_enter(&ksnew->ks_interlock);
509 1.3 thorpej ksem_delref(ksnew);
510 1.3 thorpej return (EEXIST);
511 1.3 thorpej }
512 1.3 thorpej goto found_one;
513 1.3 thorpej } else {
514 1.3 thorpej /* ksnew already has its initial reference. */
515 1.10 perry LIST_INSERT_HEAD(&ksem_head, ksnew, ks_entry);
516 1.20 ad mutex_exit(&ksem_mutex);
517 1.1 christos
518 1.3 thorpej ksem_add_proc(l->l_proc, ksnew);
519 1.1 christos }
520 1.3 thorpej return (error);
521 1.1 christos }
522 1.1 christos
523 1.3 thorpej /* We must have a read lock on the ksem_proc list! */
524 1.3 thorpej static struct ksem *
525 1.3 thorpej ksem_lookup_proc(struct ksem_proc *kp, semid_t id)
526 1.1 christos {
527 1.3 thorpej struct ksem_ref *ksr;
528 1.1 christos
529 1.3 thorpej LIST_FOREACH(ksr, &kp->kp_ksems, ksr_list) {
530 1.13 cube if (id == SEM_TO_ID(ksr->ksr_ksem)) {
531 1.20 ad mutex_enter(&ksr->ksr_ksem->ks_interlock);
532 1.3 thorpej return (ksr->ksr_ksem);
533 1.3 thorpej }
534 1.1 christos }
535 1.3 thorpej
536 1.3 thorpej return (NULL);
537 1.1 christos }
538 1.1 christos
539 1.1 christos int
540 1.23 dsl sys__ksem_unlink(struct lwp *l, const struct sys__ksem_unlink_args *uap, register_t *retval)
541 1.1 christos {
542 1.23 dsl /* {
543 1.1 christos const char *name;
544 1.23 dsl } */
545 1.3 thorpej char name[SEM_MAX_NAMELEN + 1], *cp;
546 1.21 ad size_t done, len;
547 1.1 christos struct ksem *ks;
548 1.1 christos int error;
549 1.1 christos
550 1.1 christos error = copyinstr(SCARG(uap, name), name, sizeof(name), &done);
551 1.1 christos if (error)
552 1.1 christos return error;
553 1.1 christos
554 1.20 ad mutex_enter(&ksem_mutex);
555 1.1 christos ks = ksem_lookup_byname(name);
556 1.3 thorpej if (ks == NULL) {
557 1.20 ad mutex_exit(&ksem_mutex);
558 1.3 thorpej return (ENOENT);
559 1.1 christos }
560 1.3 thorpej
561 1.21 ad KASSERT(mutex_owned(&ks->ks_interlock));
562 1.3 thorpej
563 1.3 thorpej LIST_REMOVE(ks, ks_entry);
564 1.3 thorpej cp = ks->ks_name;
565 1.21 ad len = ks->ks_namelen;
566 1.3 thorpej ks->ks_name = NULL;
567 1.3 thorpej
568 1.20 ad mutex_exit(&ksem_mutex);
569 1.3 thorpej
570 1.3 thorpej if (ks->ks_ref == 0)
571 1.3 thorpej ksem_free(ks);
572 1.3 thorpej else
573 1.20 ad mutex_exit(&ks->ks_interlock);
574 1.3 thorpej
575 1.21 ad kmem_free(cp, len);
576 1.3 thorpej
577 1.3 thorpej return (0);
578 1.1 christos }
579 1.1 christos
580 1.1 christos int
581 1.23 dsl sys__ksem_close(struct lwp *l, const struct sys__ksem_close_args *uap, register_t *retval)
582 1.1 christos {
583 1.23 dsl /* {
584 1.1 christos semid_t id;
585 1.23 dsl } */
586 1.3 thorpej struct ksem_proc *kp;
587 1.3 thorpej struct ksem_ref *ksr;
588 1.1 christos struct ksem *ks;
589 1.1 christos
590 1.16 thorpej kp = proc_getspecific(l->l_proc, ksem_specificdata_key);
591 1.16 thorpej if (kp == NULL)
592 1.3 thorpej return (EINVAL);
593 1.3 thorpej
594 1.20 ad rw_enter(&kp->kp_lock, RW_WRITER);
595 1.3 thorpej
596 1.3 thorpej ks = ksem_lookup_proc(kp, SCARG(uap, id));
597 1.3 thorpej if (ks == NULL) {
598 1.20 ad rw_exit(&kp->kp_lock);
599 1.3 thorpej return (EINVAL);
600 1.3 thorpej }
601 1.3 thorpej
602 1.21 ad KASSERT(mutex_owned(&ks->ks_interlock));
603 1.3 thorpej if (ks->ks_name == NULL) {
604 1.20 ad mutex_exit(&ks->ks_interlock);
605 1.20 ad rw_exit(&kp->kp_lock);
606 1.3 thorpej return (EINVAL);
607 1.3 thorpej }
608 1.3 thorpej
609 1.3 thorpej ksr = ksem_drop_proc(kp, ks);
610 1.20 ad rw_exit(&kp->kp_lock);
611 1.21 ad kmem_free(ksr, sizeof(*ksr));
612 1.3 thorpej
613 1.3 thorpej return (0);
614 1.1 christos }
615 1.1 christos
616 1.1 christos int
617 1.23 dsl sys__ksem_post(struct lwp *l, const struct sys__ksem_post_args *uap, register_t *retval)
618 1.1 christos {
619 1.23 dsl /* {
620 1.1 christos semid_t id;
621 1.23 dsl } */
622 1.3 thorpej struct ksem_proc *kp;
623 1.1 christos struct ksem *ks;
624 1.1 christos int error;
625 1.1 christos
626 1.16 thorpej kp = proc_getspecific(l->l_proc, ksem_specificdata_key);
627 1.16 thorpej if (kp == NULL)
628 1.3 thorpej return (EINVAL);
629 1.3 thorpej
630 1.20 ad rw_enter(&kp->kp_lock, RW_READER);
631 1.3 thorpej ks = ksem_lookup_proc(kp, SCARG(uap, id));
632 1.20 ad rw_exit(&kp->kp_lock);
633 1.3 thorpej if (ks == NULL)
634 1.3 thorpej return (EINVAL);
635 1.3 thorpej
636 1.21 ad KASSERT(mutex_owned(&ks->ks_interlock));
637 1.1 christos if (ks->ks_value == SEM_VALUE_MAX) {
638 1.1 christos error = EOVERFLOW;
639 1.3 thorpej goto out;
640 1.1 christos }
641 1.1 christos ++ks->ks_value;
642 1.3 thorpej if (ks->ks_waiters)
643 1.20 ad cv_broadcast(&ks->ks_cv);
644 1.1 christos error = 0;
645 1.3 thorpej out:
646 1.20 ad mutex_exit(&ks->ks_interlock);
647 1.3 thorpej return (error);
648 1.3 thorpej }
649 1.3 thorpej
650 1.3 thorpej static int
651 1.3 thorpej ksem_wait(struct lwp *l, semid_t id, int tryflag)
652 1.3 thorpej {
653 1.3 thorpej struct ksem_proc *kp;
654 1.3 thorpej struct ksem *ks;
655 1.3 thorpej int error;
656 1.3 thorpej
657 1.16 thorpej kp = proc_getspecific(l->l_proc, ksem_specificdata_key);
658 1.16 thorpej if (kp == NULL)
659 1.3 thorpej return (EINVAL);
660 1.3 thorpej
661 1.20 ad rw_enter(&kp->kp_lock, RW_READER);
662 1.3 thorpej ks = ksem_lookup_proc(kp, id);
663 1.20 ad rw_exit(&kp->kp_lock);
664 1.3 thorpej if (ks == NULL)
665 1.3 thorpej return (EINVAL);
666 1.3 thorpej
667 1.21 ad KASSERT(mutex_owned(&ks->ks_interlock));
668 1.3 thorpej ksem_addref(ks);
669 1.3 thorpej while (ks->ks_value == 0) {
670 1.3 thorpej ks->ks_waiters++;
671 1.20 ad if (tryflag)
672 1.20 ad error = EAGAIN;
673 1.20 ad else
674 1.20 ad error = cv_wait_sig(&ks->ks_cv, &ks->ks_interlock);
675 1.3 thorpej ks->ks_waiters--;
676 1.3 thorpej if (error)
677 1.3 thorpej goto out;
678 1.3 thorpej }
679 1.3 thorpej ks->ks_value--;
680 1.3 thorpej error = 0;
681 1.3 thorpej out:
682 1.3 thorpej ksem_delref(ks);
683 1.1 christos return (error);
684 1.1 christos }
685 1.1 christos
686 1.1 christos int
687 1.23 dsl sys__ksem_wait(struct lwp *l, const struct sys__ksem_wait_args *uap, register_t *retval)
688 1.1 christos {
689 1.23 dsl /* {
690 1.1 christos semid_t id;
691 1.23 dsl } */
692 1.1 christos
693 1.1 christos return ksem_wait(l, SCARG(uap, id), 0);
694 1.1 christos }
695 1.1 christos
696 1.1 christos int
697 1.23 dsl sys__ksem_trywait(struct lwp *l, const struct sys__ksem_trywait_args *uap, register_t *retval)
698 1.1 christos {
699 1.23 dsl /* {
700 1.1 christos semid_t id;
701 1.23 dsl } */
702 1.1 christos
703 1.1 christos return ksem_wait(l, SCARG(uap, id), 1);
704 1.1 christos }
705 1.1 christos
706 1.1 christos int
707 1.23 dsl sys__ksem_getvalue(struct lwp *l, const struct sys__ksem_getvalue_args *uap, register_t *retval)
708 1.1 christos {
709 1.23 dsl /* {
710 1.1 christos semid_t id;
711 1.1 christos unsigned int *value;
712 1.23 dsl } */
713 1.3 thorpej struct ksem_proc *kp;
714 1.1 christos struct ksem *ks;
715 1.1 christos unsigned int val;
716 1.1 christos
717 1.16 thorpej kp = proc_getspecific(l->l_proc, ksem_specificdata_key);
718 1.16 thorpej if (kp == NULL)
719 1.3 thorpej return (EINVAL);
720 1.3 thorpej
721 1.20 ad rw_enter(&kp->kp_lock, RW_READER);
722 1.3 thorpej ks = ksem_lookup_proc(kp, SCARG(uap, id));
723 1.20 ad rw_exit(&kp->kp_lock);
724 1.3 thorpej if (ks == NULL)
725 1.1 christos return (EINVAL);
726 1.3 thorpej
727 1.21 ad KASSERT(mutex_owned(&ks->ks_interlock));
728 1.1 christos val = ks->ks_value;
729 1.20 ad mutex_exit(&ks->ks_interlock);
730 1.3 thorpej
731 1.3 thorpej return (copyout(&val, SCARG(uap, value), sizeof(val)));
732 1.1 christos }
733 1.1 christos
734 1.1 christos int
735 1.23 dsl sys__ksem_destroy(struct lwp *l, const struct sys__ksem_destroy_args *uap, register_t *retval)
736 1.1 christos {
737 1.23 dsl /* {
738 1.1 christos semid_t id;
739 1.23 dsl } */
740 1.3 thorpej struct ksem_proc *kp;
741 1.3 thorpej struct ksem_ref *ksr;
742 1.1 christos struct ksem *ks;
743 1.1 christos
744 1.16 thorpej kp = proc_getspecific(l->l_proc, ksem_specificdata_key);
745 1.16 thorpej if (kp == NULL)
746 1.3 thorpej return (EINVAL);
747 1.3 thorpej
748 1.20 ad rw_enter(&kp->kp_lock, RW_WRITER);
749 1.3 thorpej
750 1.3 thorpej ks = ksem_lookup_proc(kp, SCARG(uap, id));
751 1.3 thorpej if (ks == NULL) {
752 1.20 ad rw_exit(&kp->kp_lock);
753 1.3 thorpej return (EINVAL);
754 1.3 thorpej }
755 1.3 thorpej
756 1.21 ad KASSERT(mutex_owned(&ks->ks_interlock));
757 1.3 thorpej
758 1.3 thorpej /*
759 1.3 thorpej * XXX This misses named semaphores which have been unlink'd,
760 1.3 thorpej * XXX but since behavior of destroying a named semaphore is
761 1.3 thorpej * XXX undefined, this is technically allowed.
762 1.3 thorpej */
763 1.3 thorpej if (ks->ks_name != NULL) {
764 1.20 ad mutex_exit(&ks->ks_interlock);
765 1.20 ad rw_exit(&kp->kp_lock);
766 1.3 thorpej return (EINVAL);
767 1.3 thorpej }
768 1.3 thorpej
769 1.3 thorpej if (ks->ks_waiters) {
770 1.20 ad mutex_exit(&ks->ks_interlock);
771 1.20 ad rw_exit(&kp->kp_lock);
772 1.3 thorpej return (EBUSY);
773 1.3 thorpej }
774 1.3 thorpej
775 1.3 thorpej ksr = ksem_drop_proc(kp, ks);
776 1.20 ad rw_exit(&kp->kp_lock);
777 1.21 ad kmem_free(ksr, sizeof(*ksr));
778 1.3 thorpej
779 1.3 thorpej return (0);
780 1.3 thorpej }
781 1.3 thorpej
782 1.3 thorpej static void
783 1.3 thorpej ksem_forkhook(struct proc *p2, struct proc *p1)
784 1.3 thorpej {
785 1.3 thorpej struct ksem_proc *kp1, *kp2;
786 1.3 thorpej struct ksem_ref *ksr, *ksr1;
787 1.3 thorpej
788 1.16 thorpej kp1 = proc_getspecific(p1, ksem_specificdata_key);
789 1.16 thorpej if (kp1 == NULL)
790 1.3 thorpej return;
791 1.3 thorpej
792 1.16 thorpej kp2 = ksem_proc_alloc();
793 1.3 thorpej
794 1.20 ad rw_enter(&kp1->kp_lock, RW_READER);
795 1.3 thorpej
796 1.3 thorpej if (!LIST_EMPTY(&kp1->kp_ksems)) {
797 1.3 thorpej LIST_FOREACH(ksr, &kp1->kp_ksems, ksr_list) {
798 1.21 ad ksr1 = kmem_alloc(sizeof(*ksr), KM_SLEEP);
799 1.3 thorpej ksr1->ksr_ksem = ksr->ksr_ksem;
800 1.20 ad mutex_enter(&ksr->ksr_ksem->ks_interlock);
801 1.3 thorpej ksem_addref(ksr->ksr_ksem);
802 1.20 ad mutex_exit(&ksr->ksr_ksem->ks_interlock);
803 1.3 thorpej LIST_INSERT_HEAD(&kp2->kp_ksems, ksr1, ksr_list);
804 1.3 thorpej }
805 1.1 christos }
806 1.3 thorpej
807 1.20 ad rw_exit(&kp1->kp_lock);
808 1.16 thorpej proc_setspecific(p2, ksem_specificdata_key, kp2);
809 1.1 christos }
810 1.1 christos
811 1.1 christos static void
812 1.18 yamt ksem_exechook(struct proc *p, void *arg)
813 1.1 christos {
814 1.3 thorpej struct ksem_proc *kp;
815 1.1 christos
816 1.16 thorpej kp = proc_getspecific(p, ksem_specificdata_key);
817 1.16 thorpej if (kp != NULL) {
818 1.16 thorpej proc_setspecific(p, ksem_specificdata_key, NULL);
819 1.16 thorpej ksem_proc_dtor(kp);
820 1.1 christos }
821 1.1 christos }
822 1.1 christos
823 1.1 christos void
824 1.1 christos ksem_init(void)
825 1.1 christos {
826 1.16 thorpej int i, error;
827 1.3 thorpej
828 1.20 ad mutex_init(&ksem_mutex, MUTEX_DEFAULT, IPL_NONE);
829 1.16 thorpej exechook_establish(ksem_exechook, NULL);
830 1.3 thorpej forkhook_establish(ksem_forkhook);
831 1.13 cube
832 1.13 cube for (i = 0; i < SEM_HASHTBL_SIZE; i++)
833 1.13 cube LIST_INIT(&ksem_hash[i]);
834 1.16 thorpej
835 1.16 thorpej error = proc_specific_key_create(&ksem_specificdata_key,
836 1.16 thorpej ksem_proc_dtor);
837 1.16 thorpej KASSERT(error == 0);
838 1.1 christos }
839 1.22 rmind
840 1.22 rmind /*
841 1.22 rmind * Sysctl initialization and nodes.
842 1.22 rmind */
843 1.22 rmind
844 1.22 rmind SYSCTL_SETUP(sysctl_posix_sem_setup, "sysctl kern.posix subtree setup")
845 1.22 rmind {
846 1.22 rmind const struct sysctlnode *node = NULL;
847 1.22 rmind
848 1.22 rmind sysctl_createv(clog, 0, NULL, NULL,
849 1.22 rmind CTLFLAG_PERMANENT,
850 1.22 rmind CTLTYPE_NODE, "kern", NULL,
851 1.22 rmind NULL, 0, NULL, 0,
852 1.22 rmind CTL_KERN, CTL_EOL);
853 1.22 rmind sysctl_createv(clog, 0, NULL, &node,
854 1.22 rmind CTLFLAG_PERMANENT,
855 1.22 rmind CTLTYPE_NODE, "posix",
856 1.22 rmind SYSCTL_DESCR("POSIX options"),
857 1.22 rmind NULL, 0, NULL, 0,
858 1.22 rmind CTL_KERN, CTL_CREATE, CTL_EOL);
859 1.22 rmind
860 1.22 rmind if (node == NULL)
861 1.22 rmind return;
862 1.22 rmind
863 1.22 rmind sysctl_createv(clog, 0, &node, NULL,
864 1.22 rmind CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
865 1.22 rmind CTLTYPE_INT, "semmax",
866 1.22 rmind SYSCTL_DESCR("Maximal number of semaphores"),
867 1.22 rmind NULL, 0, &sem_max, 0,
868 1.22 rmind CTL_CREATE, CTL_EOL);
869 1.22 rmind }
870