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