uipc_sem.c revision 1.32 1 1.32 rmind /* $NetBSD: uipc_sem.c,v 1.32 2011/04/14 00:32:23 rmind Exp $ */
2 1.3 thorpej
3 1.3 thorpej /*-
4 1.30 rmind * Copyright (c) 2011 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.30 rmind * by Mindaugas Rasiukevicius.
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.30 rmind /*
59 1.30 rmind * Implementation of POSIX semaphore.
60 1.30 rmind */
61 1.30 rmind
62 1.9 lukem #include <sys/cdefs.h>
63 1.32 rmind __KERNEL_RCSID(0, "$NetBSD: uipc_sem.c,v 1.32 2011/04/14 00:32:23 rmind Exp $");
64 1.1 christos
65 1.1 christos #include <sys/param.h>
66 1.1 christos #include <sys/systm.h>
67 1.1 christos #include <sys/kernel.h>
68 1.1 christos #include <sys/proc.h>
69 1.1 christos #include <sys/ksem.h>
70 1.1 christos #include <sys/syscall.h>
71 1.1 christos #include <sys/stat.h>
72 1.21 ad #include <sys/kmem.h>
73 1.1 christos #include <sys/fcntl.h>
74 1.30 rmind #include <sys/file.h>
75 1.30 rmind #include <sys/filedesc.h>
76 1.14 elad #include <sys/kauth.h>
77 1.27 ad #include <sys/module.h>
78 1.1 christos #include <sys/mount.h>
79 1.27 ad #include <sys/syscall.h>
80 1.1 christos #include <sys/syscallargs.h>
81 1.27 ad #include <sys/syscallvar.h>
82 1.1 christos
83 1.30 rmind MODULE(MODULE_CLASS_MISC, ksem, NULL);
84 1.30 rmind
85 1.30 rmind #define SEM_MAX_NAMELEN 14
86 1.30 rmind #define SEM_VALUE_MAX (~0U)
87 1.1 christos
88 1.30 rmind #define KS_UNLINKED 0x01
89 1.4 thorpej
90 1.30 rmind typedef struct ksem {
91 1.30 rmind LIST_ENTRY(ksem) ks_entry; /* global list entry */
92 1.30 rmind kmutex_t ks_lock; /* lock on this ksem */
93 1.30 rmind kcondvar_t ks_cv; /* condition variable */
94 1.30 rmind u_int ks_ref; /* number of references */
95 1.30 rmind u_int ks_value; /* current value */
96 1.30 rmind u_int ks_waiters; /* number of waiters */
97 1.30 rmind char * ks_name; /* name, if named */
98 1.30 rmind size_t ks_namelen; /* length of name */
99 1.30 rmind int ks_flags; /* for KS_UNLINKED */
100 1.30 rmind mode_t ks_mode; /* protection bits */
101 1.30 rmind uid_t ks_uid; /* creator uid */
102 1.30 rmind gid_t ks_gid; /* creator gid */
103 1.30 rmind } ksem_t;
104 1.30 rmind
105 1.30 rmind static kmutex_t ksem_lock __cacheline_aligned;
106 1.30 rmind static LIST_HEAD(,ksem) ksem_head __cacheline_aligned;
107 1.30 rmind static u_int nsems __cacheline_aligned;
108 1.30 rmind
109 1.30 rmind static int ksem_sysinit(void);
110 1.30 rmind static int ksem_sysfini(bool);
111 1.30 rmind static int ksem_modcmd(modcmd_t, void *);
112 1.30 rmind static int ksem_close_fop(file_t *);
113 1.30 rmind
114 1.30 rmind static const struct fileops semops = {
115 1.30 rmind .fo_read = fbadop_read,
116 1.30 rmind .fo_write = fbadop_write,
117 1.30 rmind .fo_ioctl = fbadop_ioctl,
118 1.30 rmind .fo_fcntl = fnullop_fcntl,
119 1.30 rmind .fo_poll = fnullop_poll,
120 1.30 rmind .fo_stat = fbadop_stat,
121 1.30 rmind .fo_close = ksem_close_fop,
122 1.30 rmind .fo_kqfilter = fnullop_kqfilter,
123 1.30 rmind .fo_restart = fnullop_restart,
124 1.30 rmind };
125 1.27 ad
126 1.27 ad static const struct syscall_package ksem_syscalls[] = {
127 1.27 ad { SYS__ksem_init, 0, (sy_call_t *)sys__ksem_init },
128 1.27 ad { SYS__ksem_open, 0, (sy_call_t *)sys__ksem_open },
129 1.27 ad { SYS__ksem_unlink, 0, (sy_call_t *)sys__ksem_unlink },
130 1.27 ad { SYS__ksem_close, 0, (sy_call_t *)sys__ksem_close },
131 1.27 ad { SYS__ksem_post, 0, (sy_call_t *)sys__ksem_post },
132 1.27 ad { SYS__ksem_wait, 0, (sy_call_t *)sys__ksem_wait },
133 1.27 ad { SYS__ksem_trywait, 0, (sy_call_t *)sys__ksem_trywait },
134 1.27 ad { SYS__ksem_getvalue, 0, (sy_call_t *)sys__ksem_getvalue },
135 1.27 ad { SYS__ksem_destroy, 0, (sy_call_t *)sys__ksem_destroy },
136 1.27 ad { 0, 0, NULL },
137 1.27 ad };
138 1.1 christos
139 1.30 rmind static int
140 1.30 rmind ksem_sysinit(void)
141 1.3 thorpej {
142 1.30 rmind int error;
143 1.1 christos
144 1.30 rmind nsems = 0;
145 1.30 rmind mutex_init(&ksem_lock, MUTEX_DEFAULT, IPL_NONE);
146 1.30 rmind LIST_INIT(&ksem_head);
147 1.20 ad
148 1.30 rmind error = syscall_establish(NULL, ksem_syscalls);
149 1.30 rmind if (error) {
150 1.30 rmind (void)ksem_sysfini(false);
151 1.3 thorpej }
152 1.30 rmind return error;
153 1.3 thorpej }
154 1.1 christos
155 1.30 rmind static int
156 1.30 rmind ksem_sysfini(bool interface)
157 1.1 christos {
158 1.30 rmind int error;
159 1.1 christos
160 1.30 rmind if (interface) {
161 1.30 rmind error = syscall_disestablish(NULL, ksem_syscalls);
162 1.30 rmind if (error != 0) {
163 1.30 rmind return error;
164 1.30 rmind }
165 1.30 rmind if (nsems != 0) {
166 1.30 rmind error = syscall_establish(NULL, ksem_syscalls);
167 1.30 rmind KASSERT(error == 0);
168 1.30 rmind return EBUSY;
169 1.30 rmind }
170 1.3 thorpej }
171 1.30 rmind mutex_destroy(&ksem_lock);
172 1.30 rmind return 0;
173 1.3 thorpej }
174 1.3 thorpej
175 1.30 rmind static int
176 1.30 rmind ksem_modcmd(modcmd_t cmd, void *arg)
177 1.3 thorpej {
178 1.3 thorpej
179 1.30 rmind switch (cmd) {
180 1.30 rmind case MODULE_CMD_INIT:
181 1.30 rmind return ksem_sysinit();
182 1.3 thorpej
183 1.30 rmind case MODULE_CMD_FINI:
184 1.30 rmind return ksem_sysfini(true);
185 1.1 christos
186 1.30 rmind default:
187 1.30 rmind return ENOTTY;
188 1.16 thorpej }
189 1.16 thorpej }
190 1.16 thorpej
191 1.30 rmind static ksem_t *
192 1.30 rmind ksem_lookup(const char *name)
193 1.3 thorpej {
194 1.30 rmind ksem_t *ks;
195 1.3 thorpej
196 1.30 rmind KASSERT(mutex_owned(&ksem_lock));
197 1.3 thorpej
198 1.30 rmind LIST_FOREACH(ks, &ksem_head, ks_entry) {
199 1.30 rmind if (strcmp(ks->ks_name, name) == 0) {
200 1.30 rmind mutex_enter(&ks->ks_lock);
201 1.30 rmind return ks;
202 1.3 thorpej }
203 1.1 christos }
204 1.30 rmind return NULL;
205 1.1 christos }
206 1.1 christos
207 1.3 thorpej static int
208 1.30 rmind ksem_perm(lwp_t *l, ksem_t *ks)
209 1.3 thorpej {
210 1.30 rmind kauth_cred_t uc = l->l_cred;
211 1.30 rmind mode_t mode = ks->ks_mode;
212 1.3 thorpej
213 1.30 rmind KASSERT(mutex_owned(&ks->ks_lock));
214 1.30 rmind if ((kauth_cred_geteuid(uc) == ks->ks_uid && (mode & S_IWUSR) != 0) ||
215 1.30 rmind (kauth_cred_getegid(uc) == ks->ks_gid && (mode & S_IWGRP) != 0) ||
216 1.30 rmind (mode & S_IWOTH) != 0 ||
217 1.19 elad kauth_authorize_generic(uc, KAUTH_GENERIC_ISSUSER, NULL) == 0)
218 1.30 rmind return 0;
219 1.30 rmind
220 1.30 rmind return EACCES;
221 1.3 thorpej }
222 1.3 thorpej
223 1.30 rmind /*
224 1.30 rmind * ksem_get: get the semaphore from the descriptor.
225 1.30 rmind *
226 1.30 rmind * => locks the semaphore, if found.
227 1.30 rmind * => holds a reference on the file descriptor.
228 1.30 rmind */
229 1.30 rmind static int
230 1.30 rmind ksem_get(int fd, ksem_t **ksret)
231 1.13 cube {
232 1.30 rmind ksem_t *ks;
233 1.30 rmind file_t *fp;
234 1.13 cube
235 1.30 rmind fp = fd_getfile(fd);
236 1.30 rmind if (__predict_false(fp == NULL)) {
237 1.30 rmind return EBADF;
238 1.30 rmind }
239 1.30 rmind if (__predict_false(fp->f_type != DTYPE_SEM)) {
240 1.30 rmind fd_putfile(fd);
241 1.30 rmind return EBADF;
242 1.13 cube }
243 1.30 rmind ks = fp->f_data;
244 1.30 rmind mutex_enter(&ks->ks_lock);
245 1.13 cube
246 1.30 rmind *ksret = ks;
247 1.30 rmind return 0;
248 1.1 christos }
249 1.1 christos
250 1.30 rmind /*
251 1.30 rmind * ksem_create: allocate and setup a new semaphore structure.
252 1.30 rmind */
253 1.1 christos static int
254 1.30 rmind ksem_create(lwp_t *l, const char *name, ksem_t **ksret, mode_t mode, u_int val)
255 1.1 christos {
256 1.30 rmind ksem_t *ks;
257 1.14 elad kauth_cred_t uc;
258 1.30 rmind char *kname;
259 1.1 christos size_t len;
260 1.1 christos
261 1.30 rmind /* Pre-check for the limit. */
262 1.30 rmind if (nsems >= ksem_max) {
263 1.30 rmind return ENFILE;
264 1.30 rmind }
265 1.30 rmind
266 1.30 rmind if (val > SEM_VALUE_MAX) {
267 1.30 rmind return EINVAL;
268 1.30 rmind }
269 1.30 rmind
270 1.1 christos if (name != NULL) {
271 1.1 christos len = strlen(name);
272 1.1 christos if (len > SEM_MAX_NAMELEN) {
273 1.30 rmind return ENAMETOOLONG;
274 1.1 christos }
275 1.30 rmind /* Name must start with a '/' but not contain one. */
276 1.1 christos if (*name != '/' || len < 2 || strchr(name + 1, '/') != NULL) {
277 1.30 rmind return EINVAL;
278 1.1 christos }
279 1.30 rmind kname = kmem_alloc(++len, KM_SLEEP);
280 1.30 rmind strlcpy(kname, name, len);
281 1.30 rmind } else {
282 1.30 rmind kname = NULL;
283 1.30 rmind len = 0;
284 1.30 rmind }
285 1.30 rmind
286 1.30 rmind ks = kmem_zalloc(sizeof(ksem_t), KM_SLEEP);
287 1.30 rmind mutex_init(&ks->ks_lock, MUTEX_DEFAULT, IPL_NONE);
288 1.30 rmind cv_init(&ks->ks_cv, "psem");
289 1.30 rmind ks->ks_name = kname;
290 1.30 rmind ks->ks_namelen = len;
291 1.30 rmind ks->ks_mode = mode;
292 1.30 rmind ks->ks_value = val;
293 1.30 rmind ks->ks_ref = 1;
294 1.30 rmind
295 1.30 rmind uc = l->l_cred;
296 1.30 rmind ks->ks_uid = kauth_cred_geteuid(uc);
297 1.30 rmind ks->ks_gid = kauth_cred_getegid(uc);
298 1.30 rmind
299 1.30 rmind *ksret = ks;
300 1.30 rmind return 0;
301 1.30 rmind }
302 1.30 rmind
303 1.30 rmind static void
304 1.30 rmind ksem_free(ksem_t *ks)
305 1.30 rmind {
306 1.3 thorpej
307 1.30 rmind if (ks->ks_name) {
308 1.30 rmind KASSERT(ks->ks_namelen > 0);
309 1.30 rmind kmem_free(ks->ks_name, ks->ks_namelen);
310 1.13 cube }
311 1.30 rmind mutex_destroy(&ks->ks_lock);
312 1.30 rmind cv_destroy(&ks->ks_cv);
313 1.30 rmind kmem_free(ks, sizeof(ksem_t));
314 1.1 christos }
315 1.1 christos
316 1.1 christos int
317 1.30 rmind sys__ksem_init(struct lwp *l, const struct sys__ksem_init_args *uap,
318 1.30 rmind register_t *retval)
319 1.1 christos {
320 1.23 dsl /* {
321 1.1 christos unsigned int value;
322 1.29 ad intptr_t *idp;
323 1.23 dsl } */
324 1.13 cube
325 1.13 cube return do_ksem_init(l, SCARG(uap, value), SCARG(uap, idp), copyout);
326 1.13 cube }
327 1.13 cube
328 1.13 cube int
329 1.30 rmind do_ksem_init(lwp_t *l, u_int val, intptr_t *idp, copyout_t docopyout)
330 1.13 cube {
331 1.30 rmind proc_t *p = l->l_proc;
332 1.30 rmind ksem_t *ks;
333 1.30 rmind file_t *fp;
334 1.29 ad intptr_t id;
335 1.30 rmind int fd, error;
336 1.1 christos
337 1.30 rmind error = fd_allocfile(&fp, &fd);
338 1.1 christos if (error) {
339 1.30 rmind return error;
340 1.1 christos }
341 1.30 rmind fp->f_type = DTYPE_SEM;
342 1.30 rmind fp->f_flag = FREAD | FWRITE;
343 1.30 rmind fp->f_ops = &semops;
344 1.3 thorpej
345 1.30 rmind id = (intptr_t)fd;
346 1.30 rmind error = (*docopyout)(&id, idp, sizeof(*idp));
347 1.30 rmind if (error) {
348 1.30 rmind fd_abort(p, fp, fd);
349 1.30 rmind return error;
350 1.30 rmind }
351 1.3 thorpej
352 1.30 rmind /* Note the mode does not matter for anonymous semaphores. */
353 1.30 rmind error = ksem_create(l, NULL, &ks, 0, val);
354 1.30 rmind if (error) {
355 1.30 rmind fd_abort(p, fp, fd);
356 1.30 rmind return error;
357 1.30 rmind }
358 1.30 rmind fp->f_data = ks;
359 1.30 rmind fd_affix(p, fp, fd);
360 1.30 rmind return error;
361 1.1 christos }
362 1.1 christos
363 1.1 christos int
364 1.30 rmind sys__ksem_open(struct lwp *l, const struct sys__ksem_open_args *uap,
365 1.30 rmind register_t *retval)
366 1.1 christos {
367 1.23 dsl /* {
368 1.1 christos const char *name;
369 1.1 christos int oflag;
370 1.1 christos mode_t mode;
371 1.1 christos unsigned int value;
372 1.29 ad intptr_t *idp;
373 1.23 dsl } */
374 1.13 cube
375 1.13 cube return do_ksem_open(l, SCARG(uap, name), SCARG(uap, oflag),
376 1.13 cube SCARG(uap, mode), SCARG(uap, value), SCARG(uap, idp), copyout);
377 1.13 cube }
378 1.13 cube
379 1.13 cube int
380 1.13 cube do_ksem_open(struct lwp *l, const char *semname, int oflag, mode_t mode,
381 1.29 ad unsigned int value, intptr_t *idp, copyout_t docopyout)
382 1.13 cube {
383 1.1 christos char name[SEM_MAX_NAMELEN + 1];
384 1.30 rmind proc_t *p = l->l_proc;
385 1.30 rmind ksem_t *ksnew = NULL, *ks;
386 1.30 rmind file_t *fp;
387 1.29 ad intptr_t id;
388 1.30 rmind int fd, error;
389 1.1 christos
390 1.30 rmind error = copyinstr(semname, name, sizeof(name), NULL);
391 1.30 rmind if (error) {
392 1.30 rmind return error;
393 1.30 rmind }
394 1.30 rmind error = fd_allocfile(&fp, &fd);
395 1.30 rmind if (error) {
396 1.30 rmind return error;
397 1.30 rmind }
398 1.30 rmind fp->f_type = DTYPE_SEM;
399 1.30 rmind fp->f_flag = FREAD | FWRITE;
400 1.30 rmind fp->f_ops = &semops;
401 1.30 rmind
402 1.30 rmind /*
403 1.30 rmind * The ID (file descriptor number) can be stored early.
404 1.30 rmind * Note that zero is a special value for libpthread.
405 1.30 rmind */
406 1.30 rmind id = (intptr_t)fd;
407 1.30 rmind error = (*docopyout)(&id, idp, sizeof(*idp));
408 1.30 rmind if (error) {
409 1.30 rmind goto err;
410 1.30 rmind }
411 1.30 rmind
412 1.30 rmind if (oflag & O_CREAT) {
413 1.30 rmind /* Create a new semaphore. */
414 1.30 rmind error = ksem_create(l, name, &ksnew, mode, value);
415 1.30 rmind if (error) {
416 1.30 rmind goto err;
417 1.30 rmind }
418 1.30 rmind KASSERT(ksnew != NULL);
419 1.30 rmind }
420 1.1 christos
421 1.30 rmind /* Lookup for a semaphore with such name. */
422 1.30 rmind mutex_enter(&ksem_lock);
423 1.30 rmind ks = ksem_lookup(name);
424 1.30 rmind if (ks) {
425 1.30 rmind KASSERT(mutex_owned(&ks->ks_lock));
426 1.30 rmind mutex_exit(&ksem_lock);
427 1.3 thorpej
428 1.3 thorpej /* Check for exclusive create. */
429 1.13 cube if (oflag & O_EXCL) {
430 1.30 rmind mutex_exit(&ks->ks_lock);
431 1.30 rmind error = EEXIST;
432 1.30 rmind goto err;
433 1.1 christos }
434 1.1 christos /*
435 1.30 rmind * Verify permissions. If we can access it,
436 1.30 rmind * add the reference of this thread.
437 1.1 christos */
438 1.15 ad error = ksem_perm(l, ks);
439 1.30 rmind if (error == 0) {
440 1.30 rmind ks->ks_ref++;
441 1.30 rmind }
442 1.30 rmind mutex_exit(&ks->ks_lock);
443 1.1 christos if (error) {
444 1.30 rmind goto err;
445 1.30 rmind }
446 1.30 rmind } else {
447 1.30 rmind /* Fail if not found and not creating. */
448 1.30 rmind if ((oflag & O_CREAT) == 0) {
449 1.30 rmind mutex_exit(&ksem_lock);
450 1.30 rmind KASSERT(ksnew == NULL);
451 1.31 rmind error = ENOENT;
452 1.31 rmind goto err;
453 1.1 christos }
454 1.3 thorpej
455 1.30 rmind /* Check for the limit locked. */
456 1.30 rmind if (nsems >= ksem_max) {
457 1.30 rmind mutex_exit(&ksem_lock);
458 1.30 rmind error = ENFILE;
459 1.30 rmind goto err;
460 1.30 rmind }
461 1.3 thorpej
462 1.30 rmind /*
463 1.32 rmind * Finally, insert semaphore into the list.
464 1.30 rmind * Note: it already has the initial reference.
465 1.30 rmind */
466 1.30 rmind ks = ksnew;
467 1.30 rmind LIST_INSERT_HEAD(&ksem_head, ks, ks_entry);
468 1.30 rmind nsems++;
469 1.30 rmind mutex_exit(&ksem_lock);
470 1.30 rmind
471 1.30 rmind ksnew = NULL;
472 1.30 rmind }
473 1.30 rmind KASSERT(ks != NULL);
474 1.30 rmind fp->f_data = ks;
475 1.30 rmind fd_affix(p, fp, fd);
476 1.30 rmind err:
477 1.30 rmind if (error) {
478 1.30 rmind fd_abort(p, fp, fd);
479 1.3 thorpej }
480 1.30 rmind if (ksnew) {
481 1.30 rmind ksem_free(ksnew);
482 1.1 christos }
483 1.30 rmind return error;
484 1.30 rmind }
485 1.1 christos
486 1.30 rmind int
487 1.30 rmind sys__ksem_close(struct lwp *l, const struct sys__ksem_close_args *uap,
488 1.30 rmind register_t *retval)
489 1.30 rmind {
490 1.30 rmind /* {
491 1.30 rmind intptr_t id;
492 1.30 rmind } */
493 1.30 rmind struct sys_close_args cuap;
494 1.30 rmind SCARG(&cuap, fd) = SCARG(uap, id);
495 1.30 rmind return sys_close(l, (const void *)&cuap, retval);
496 1.1 christos }
497 1.1 christos
498 1.30 rmind static int
499 1.30 rmind ksem_close_fop(file_t *fp)
500 1.1 christos {
501 1.30 rmind ksem_t *ks = fp->f_data;
502 1.30 rmind bool destroy = false;
503 1.1 christos
504 1.30 rmind mutex_enter(&ks->ks_lock);
505 1.30 rmind KASSERT(ks->ks_ref > 0);
506 1.30 rmind if (--ks->ks_ref == 0) {
507 1.30 rmind /*
508 1.30 rmind * Destroy if the last reference and semaphore is unnamed,
509 1.30 rmind * or unlinked (for named semaphore).
510 1.30 rmind */
511 1.30 rmind destroy = (ks->ks_flags & KS_UNLINKED) || (ks->ks_name == NULL);
512 1.1 christos }
513 1.30 rmind mutex_exit(&ks->ks_lock);
514 1.3 thorpej
515 1.30 rmind if (destroy) {
516 1.30 rmind ksem_free(ks);
517 1.30 rmind }
518 1.30 rmind return 0;
519 1.1 christos }
520 1.1 christos
521 1.1 christos int
522 1.30 rmind sys__ksem_unlink(struct lwp *l, const struct sys__ksem_unlink_args *uap,
523 1.30 rmind register_t *retval)
524 1.1 christos {
525 1.23 dsl /* {
526 1.1 christos const char *name;
527 1.23 dsl } */
528 1.30 rmind char name[SEM_MAX_NAMELEN + 1];
529 1.30 rmind ksem_t *ks;
530 1.30 rmind u_int refcnt;
531 1.1 christos int error;
532 1.1 christos
533 1.30 rmind error = copyinstr(SCARG(uap, name), name, sizeof(name), NULL);
534 1.1 christos if (error)
535 1.1 christos return error;
536 1.1 christos
537 1.30 rmind mutex_enter(&ksem_lock);
538 1.30 rmind ks = ksem_lookup(name);
539 1.3 thorpej if (ks == NULL) {
540 1.30 rmind mutex_exit(&ksem_lock);
541 1.30 rmind return ENOENT;
542 1.1 christos }
543 1.30 rmind KASSERT(mutex_owned(&ks->ks_lock));
544 1.3 thorpej
545 1.30 rmind /* Verify permissions. */
546 1.30 rmind error = ksem_perm(l, ks);
547 1.30 rmind if (error) {
548 1.30 rmind mutex_exit(&ks->ks_lock);
549 1.30 rmind mutex_exit(&ksem_lock);
550 1.30 rmind return error;
551 1.30 rmind }
552 1.3 thorpej
553 1.31 rmind /* Remove from the global list. */
554 1.3 thorpej LIST_REMOVE(ks, ks_entry);
555 1.30 rmind nsems--;
556 1.31 rmind mutex_exit(&ksem_lock);
557 1.3 thorpej
558 1.30 rmind refcnt = ks->ks_ref;
559 1.30 rmind if (refcnt) {
560 1.30 rmind /* Mark as unlinked, if there are references. */
561 1.30 rmind ks->ks_flags |= KS_UNLINKED;
562 1.30 rmind }
563 1.30 rmind mutex_exit(&ks->ks_lock);
564 1.3 thorpej
565 1.30 rmind if (refcnt == 0) {
566 1.3 thorpej ksem_free(ks);
567 1.30 rmind }
568 1.30 rmind return 0;
569 1.1 christos }
570 1.1 christos
571 1.1 christos int
572 1.30 rmind sys__ksem_post(struct lwp *l, const struct sys__ksem_post_args *uap,
573 1.30 rmind register_t *retval)
574 1.1 christos {
575 1.23 dsl /* {
576 1.29 ad intptr_t id;
577 1.23 dsl } */
578 1.30 rmind int fd = (int)SCARG(uap, id), error;
579 1.30 rmind ksem_t *ks;
580 1.1 christos
581 1.30 rmind error = ksem_get(fd, &ks);
582 1.30 rmind if (error) {
583 1.30 rmind return error;
584 1.3 thorpej }
585 1.30 rmind KASSERT(mutex_owned(&ks->ks_lock));
586 1.1 christos if (ks->ks_value == SEM_VALUE_MAX) {
587 1.1 christos error = EOVERFLOW;
588 1.3 thorpej goto out;
589 1.1 christos }
590 1.30 rmind ks->ks_value++;
591 1.30 rmind if (ks->ks_waiters) {
592 1.20 ad cv_broadcast(&ks->ks_cv);
593 1.30 rmind }
594 1.30 rmind out:
595 1.30 rmind mutex_exit(&ks->ks_lock);
596 1.30 rmind fd_putfile(fd);
597 1.30 rmind return error;
598 1.3 thorpej }
599 1.3 thorpej
600 1.3 thorpej static int
601 1.30 rmind ksem_wait(lwp_t *l, intptr_t id, bool try)
602 1.3 thorpej {
603 1.30 rmind int fd = (int)id, error;
604 1.30 rmind ksem_t *ks;
605 1.3 thorpej
606 1.30 rmind error = ksem_get(fd, &ks);
607 1.30 rmind if (error) {
608 1.30 rmind return error;
609 1.30 rmind }
610 1.30 rmind KASSERT(mutex_owned(&ks->ks_lock));
611 1.3 thorpej while (ks->ks_value == 0) {
612 1.3 thorpej ks->ks_waiters++;
613 1.30 rmind error = try ? EAGAIN : cv_wait_sig(&ks->ks_cv, &ks->ks_lock);
614 1.3 thorpej ks->ks_waiters--;
615 1.3 thorpej if (error)
616 1.3 thorpej goto out;
617 1.3 thorpej }
618 1.3 thorpej ks->ks_value--;
619 1.30 rmind out:
620 1.30 rmind mutex_exit(&ks->ks_lock);
621 1.30 rmind fd_putfile(fd);
622 1.30 rmind return error;
623 1.1 christos }
624 1.1 christos
625 1.1 christos int
626 1.30 rmind sys__ksem_wait(struct lwp *l, const struct sys__ksem_wait_args *uap,
627 1.30 rmind register_t *retval)
628 1.1 christos {
629 1.23 dsl /* {
630 1.29 ad intptr_t id;
631 1.23 dsl } */
632 1.1 christos
633 1.30 rmind return ksem_wait(l, SCARG(uap, id), false);
634 1.1 christos }
635 1.1 christos
636 1.1 christos int
637 1.30 rmind sys__ksem_trywait(struct lwp *l, const struct sys__ksem_trywait_args *uap,
638 1.30 rmind register_t *retval)
639 1.1 christos {
640 1.23 dsl /* {
641 1.29 ad intptr_t id;
642 1.23 dsl } */
643 1.1 christos
644 1.30 rmind return ksem_wait(l, SCARG(uap, id), true);
645 1.1 christos }
646 1.1 christos
647 1.1 christos int
648 1.30 rmind sys__ksem_getvalue(struct lwp *l, const struct sys__ksem_getvalue_args *uap,
649 1.30 rmind register_t *retval)
650 1.1 christos {
651 1.23 dsl /* {
652 1.29 ad intptr_t id;
653 1.1 christos unsigned int *value;
654 1.23 dsl } */
655 1.30 rmind int fd = (int)SCARG(uap, id), error;
656 1.30 rmind ksem_t *ks;
657 1.1 christos unsigned int val;
658 1.1 christos
659 1.30 rmind error = ksem_get(fd, &ks);
660 1.30 rmind if (error) {
661 1.30 rmind return error;
662 1.30 rmind }
663 1.30 rmind KASSERT(mutex_owned(&ks->ks_lock));
664 1.1 christos val = ks->ks_value;
665 1.30 rmind mutex_exit(&ks->ks_lock);
666 1.30 rmind fd_putfile(fd);
667 1.3 thorpej
668 1.30 rmind return copyout(&val, SCARG(uap, value), sizeof(val));
669 1.1 christos }
670 1.1 christos
671 1.1 christos int
672 1.30 rmind sys__ksem_destroy(struct lwp *l, const struct sys__ksem_destroy_args *uap,
673 1.30 rmind register_t *retval)
674 1.1 christos {
675 1.23 dsl /* {
676 1.29 ad intptr_t id;
677 1.23 dsl } */
678 1.30 rmind int fd = (int)SCARG(uap, id), error;
679 1.30 rmind ksem_t *ks;
680 1.1 christos
681 1.30 rmind error = ksem_get(fd, &ks);
682 1.30 rmind if (error) {
683 1.30 rmind return error;
684 1.3 thorpej }
685 1.30 rmind KASSERT(mutex_owned(&ks->ks_lock));
686 1.3 thorpej
687 1.30 rmind /* Operation is only for unnamed semaphores. */
688 1.3 thorpej if (ks->ks_name != NULL) {
689 1.30 rmind error = EINVAL;
690 1.30 rmind goto out;
691 1.3 thorpej }
692 1.30 rmind /* Cannot destroy if there are waiters. */
693 1.3 thorpej if (ks->ks_waiters) {
694 1.30 rmind error = EBUSY;
695 1.30 rmind goto out;
696 1.3 thorpej }
697 1.30 rmind out:
698 1.30 rmind mutex_exit(&ks->ks_lock);
699 1.30 rmind if (error) {
700 1.32 rmind fd_putfile(fd);
701 1.27 ad return error;
702 1.27 ad }
703 1.32 rmind return fd_close(fd);
704 1.22 rmind }
705