sys_lwp.c revision 1.28 1 /* $NetBSD: sys_lwp.c,v 1.28 2007/11/07 00:37:23 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams, and Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Lightweight process (LWP) system calls. See kern_lwp.c for a description
41 * of LWPs.
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.28 2007/11/07 00:37:23 ad Exp $");
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/pool.h>
50 #include <sys/proc.h>
51 #include <sys/types.h>
52 #include <sys/syscallargs.h>
53 #include <sys/kauth.h>
54 #include <sys/kmem.h>
55 #include <sys/sleepq.h>
56
57 #include <uvm/uvm_extern.h>
58
59 #define LWP_UNPARK_MAX 1024
60
61 syncobj_t lwp_park_sobj = {
62 SOBJ_SLEEPQ_LIFO,
63 sleepq_unsleep,
64 sleepq_changepri,
65 sleepq_lendpri,
66 syncobj_noowner,
67 };
68
69 sleeptab_t lwp_park_tab;
70
71 void
72 lwp_sys_init(void)
73 {
74 sleeptab_init(&lwp_park_tab);
75 }
76
77 /* ARGSUSED */
78 int
79 sys__lwp_create(struct lwp *l, void *v, register_t *retval)
80 {
81 struct sys__lwp_create_args /* {
82 syscallarg(const ucontext_t *) ucp;
83 syscallarg(u_long) flags;
84 syscallarg(lwpid_t *) new_lwp;
85 } */ *uap = v;
86 struct proc *p = l->l_proc;
87 struct lwp *l2;
88 vaddr_t uaddr;
89 bool inmem;
90 ucontext_t *newuc;
91 int error, lid;
92
93 newuc = pool_get(&lwp_uc_pool, PR_WAITOK);
94
95 error = copyin(SCARG(uap, ucp), newuc, p->p_emul->e_ucsize);
96 if (error) {
97 pool_put(&lwp_uc_pool, newuc);
98 return error;
99 }
100
101 /* XXX check against resource limits */
102
103 inmem = uvm_uarea_alloc(&uaddr);
104 if (__predict_false(uaddr == 0)) {
105 pool_put(&lwp_uc_pool, newuc);
106 return ENOMEM;
107 }
108
109 error = lwp_create(l, p, uaddr, inmem, SCARG(uap, flags) & LWP_DETACHED,
110 NULL, 0, p->p_emul->e_startlwp, newuc, &l2, l->l_class);
111 if (error) {
112 uvm_uarea_free(uaddr, curcpu());
113 pool_put(&lwp_uc_pool, newuc);
114 return error;
115 }
116
117 lid = l2->l_lid;
118 error = copyout(&lid, SCARG(uap, new_lwp), sizeof(lid));
119 if (error) {
120 lwp_exit(l2);
121 pool_put(&lwp_uc_pool, newuc);
122 return error;
123 }
124
125 /*
126 * Set the new LWP running, unless the caller has requested that
127 * it be created in suspended state. If the process is stopping,
128 * then the LWP is created stopped.
129 */
130 mutex_enter(&p->p_smutex);
131 lwp_lock(l2);
132 if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0 &&
133 (l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) {
134 if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0)
135 l2->l_stat = LSSTOP;
136 else {
137 KASSERT(lwp_locked(l2, l2->l_cpu->ci_schedstate.spc_mutex));
138 p->p_nrlwps++;
139 l2->l_stat = LSRUN;
140 sched_enqueue(l2, false);
141 }
142 } else
143 l2->l_stat = LSSUSPENDED;
144 lwp_unlock(l2);
145 mutex_exit(&p->p_smutex);
146
147 return 0;
148 }
149
150 int
151 sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
152 {
153
154 lwp_exit(l);
155 return 0;
156 }
157
158 int
159 sys__lwp_self(struct lwp *l, void *v, register_t *retval)
160 {
161
162 *retval = l->l_lid;
163 return 0;
164 }
165
166 int
167 sys__lwp_getprivate(struct lwp *l, void *v, register_t *retval)
168 {
169
170 *retval = (uintptr_t)l->l_private;
171 return 0;
172 }
173
174 int
175 sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval)
176 {
177 struct sys__lwp_setprivate_args /* {
178 syscallarg(void *) ptr;
179 } */ *uap = v;
180
181 l->l_private = SCARG(uap, ptr);
182 return 0;
183 }
184
185 int
186 sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
187 {
188 struct sys__lwp_suspend_args /* {
189 syscallarg(lwpid_t) target;
190 } */ *uap = v;
191 struct proc *p = l->l_proc;
192 struct lwp *t;
193 int error;
194
195 mutex_enter(&p->p_smutex);
196 if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
197 mutex_exit(&p->p_smutex);
198 return ESRCH;
199 }
200
201 /*
202 * Check for deadlock, which is only possible when we're suspending
203 * ourself. XXX There is a short race here, as p_nrlwps is only
204 * incremented when an LWP suspends itself on the kernel/user
205 * boundary. It's still possible to kill -9 the process so we
206 * don't bother checking further.
207 */
208 lwp_lock(t);
209 if ((t == l && p->p_nrlwps == 1) ||
210 (l->l_flag & (LW_WCORE | LW_WEXIT)) != 0) {
211 lwp_unlock(t);
212 mutex_exit(&p->p_smutex);
213 return EDEADLK;
214 }
215
216 /*
217 * Suspend the LWP. XXX If it's on a different CPU, we should wait
218 * for it to be preempted, where it will put itself to sleep.
219 *
220 * Suspension of the current LWP will happen on return to userspace.
221 */
222 error = lwp_suspend(l, t);
223 if (error) {
224 mutex_exit(&p->p_smutex);
225 return error;
226 }
227
228 /*
229 * Wait for:
230 * o process exiting
231 * o target LWP suspended
232 * o target LWP not suspended and L_WSUSPEND clear
233 * o target LWP exited
234 */
235 for (;;) {
236 error = cv_wait_sig(&p->p_lwpcv, &p->p_smutex);
237 if (error) {
238 error = ERESTART;
239 break;
240 }
241 if (lwp_find(p, SCARG(uap, target)) == NULL) {
242 error = ESRCH;
243 break;
244 }
245 if ((l->l_flag | t->l_flag) & (LW_WCORE | LW_WEXIT)) {
246 error = ERESTART;
247 break;
248 }
249 if (t->l_stat == LSSUSPENDED ||
250 (t->l_flag & LW_WSUSPEND) == 0)
251 break;
252 }
253 mutex_exit(&p->p_smutex);
254
255 return error;
256 }
257
258 int
259 sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
260 {
261 struct sys__lwp_continue_args /* {
262 syscallarg(lwpid_t) target;
263 } */ *uap = v;
264 int error;
265 struct proc *p = l->l_proc;
266 struct lwp *t;
267
268 error = 0;
269
270 mutex_enter(&p->p_smutex);
271 if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
272 mutex_exit(&p->p_smutex);
273 return ESRCH;
274 }
275
276 lwp_lock(t);
277 lwp_continue(t);
278 mutex_exit(&p->p_smutex);
279
280 return error;
281 }
282
283 int
284 sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
285 {
286 struct sys__lwp_wakeup_args /* {
287 syscallarg(lwpid_t) target;
288 } */ *uap = v;
289 struct lwp *t;
290 struct proc *p;
291 int error;
292
293 p = l->l_proc;
294 mutex_enter(&p->p_smutex);
295
296 if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
297 mutex_exit(&p->p_smutex);
298 return ESRCH;
299 }
300
301 lwp_lock(t);
302 t->l_flag |= (LW_CANCELLED | LW_UNPARKED);
303
304 if (t->l_stat != LSSLEEP) {
305 lwp_unlock(t);
306 error = ENODEV;
307 } else if ((t->l_flag & LW_SINTR) == 0) {
308 lwp_unlock(t);
309 error = EBUSY;
310 } else {
311 /* Wake it up. lwp_unsleep() will release the LWP lock. */
312 lwp_unsleep(t);
313 error = 0;
314 }
315
316 mutex_exit(&p->p_smutex);
317
318 return error;
319 }
320
321 int
322 sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
323 {
324 struct sys__lwp_wait_args /* {
325 syscallarg(lwpid_t) wait_for;
326 syscallarg(lwpid_t *) departed;
327 } */ *uap = v;
328 struct proc *p = l->l_proc;
329 int error;
330 lwpid_t dep;
331
332 mutex_enter(&p->p_smutex);
333 error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
334 mutex_exit(&p->p_smutex);
335
336 if (error)
337 return error;
338
339 if (SCARG(uap, departed)) {
340 error = copyout(&dep, SCARG(uap, departed), sizeof(dep));
341 if (error)
342 return error;
343 }
344
345 return 0;
346 }
347
348 /* ARGSUSED */
349 int
350 sys__lwp_kill(struct lwp *l, void *v, register_t *retval)
351 {
352 struct sys__lwp_kill_args /* {
353 syscallarg(lwpid_t) target;
354 syscallarg(int) signo;
355 } */ *uap = v;
356 struct proc *p = l->l_proc;
357 struct lwp *t;
358 ksiginfo_t ksi;
359 int signo = SCARG(uap, signo);
360 int error = 0;
361
362 if ((u_int)signo >= NSIG)
363 return EINVAL;
364
365 KSI_INIT(&ksi);
366 ksi.ksi_signo = signo;
367 ksi.ksi_code = SI_USER;
368 ksi.ksi_pid = p->p_pid;
369 ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
370 ksi.ksi_lid = SCARG(uap, target);
371
372 mutex_enter(&proclist_mutex);
373 mutex_enter(&p->p_smutex);
374 if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
375 error = ESRCH;
376 else if (signo != 0)
377 kpsignal2(p, &ksi);
378 mutex_exit(&p->p_smutex);
379 mutex_exit(&proclist_mutex);
380
381 return error;
382 }
383
384 int
385 sys__lwp_detach(struct lwp *l, void *v, register_t *retval)
386 {
387 struct sys__lwp_detach_args /* {
388 syscallarg(lwpid_t) target;
389 } */ *uap = v;
390 struct proc *p;
391 struct lwp *t;
392 lwpid_t target;
393 int error;
394
395 target = SCARG(uap, target);
396 p = l->l_proc;
397
398 mutex_enter(&p->p_smutex);
399
400 if (l->l_lid == target)
401 t = l;
402 else {
403 /*
404 * We can't use lwp_find() here because the target might
405 * be a zombie.
406 */
407 LIST_FOREACH(t, &p->p_lwps, l_sibling)
408 if (t->l_lid == target)
409 break;
410 }
411
412 /*
413 * If the LWP is already detached, there's nothing to do.
414 * If it's a zombie, we need to clean up after it. LSZOMB
415 * is visible with the proc mutex held.
416 *
417 * After we have detached or released the LWP, kick any
418 * other LWPs that may be sitting in _lwp_wait(), waiting
419 * for the target LWP to exit.
420 */
421 if (t != NULL && t->l_stat != LSIDL) {
422 if ((t->l_prflag & LPR_DETACHED) == 0) {
423 p->p_ndlwps++;
424 t->l_prflag |= LPR_DETACHED;
425 if (t->l_stat == LSZOMB) {
426 /* Releases proc mutex. */
427 lwp_free(t, false, false);
428 return 0;
429 }
430 error = 0;
431
432 /*
433 * Have any LWPs sleeping in lwp_wait() recheck
434 * for deadlock.
435 */
436 cv_broadcast(&p->p_lwpcv);
437 } else
438 error = EINVAL;
439 } else
440 error = ESRCH;
441
442 mutex_exit(&p->p_smutex);
443
444 return error;
445 }
446
447 static inline wchan_t
448 lwp_park_wchan(struct proc *p, const void *hint)
449 {
450
451 return (wchan_t)((uintptr_t)p ^ (uintptr_t)hint);
452 }
453
454 int
455 lwp_unpark(lwpid_t target, const void *hint)
456 {
457 sleepq_t *sq;
458 wchan_t wchan;
459 int swapin;
460 proc_t *p;
461 lwp_t *t;
462
463 /*
464 * Easy case: search for the LWP on the sleep queue. If
465 * it's parked, remove it from the queue and set running.
466 */
467 p = curproc;
468 wchan = lwp_park_wchan(p, hint);
469 sq = sleeptab_lookup(&lwp_park_tab, wchan);
470
471 TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
472 if (t->l_proc == p && t->l_lid == target)
473 break;
474
475 if (__predict_true(t != NULL)) {
476 swapin = sleepq_remove(sq, t);
477 sleepq_unlock(sq);
478 if (swapin)
479 uvm_kick_scheduler();
480 return 0;
481 }
482
483 /*
484 * The LWP hasn't parked yet. Take the hit and mark the
485 * operation as pending.
486 */
487 sleepq_unlock(sq);
488
489 mutex_enter(&p->p_smutex);
490 if ((t = lwp_find(p, target)) == NULL) {
491 mutex_exit(&p->p_smutex);
492 return ESRCH;
493 }
494
495 /*
496 * It may not have parked yet, we may have raced, or it
497 * is parked on a different user sync object.
498 */
499 lwp_lock(t);
500 if (t->l_syncobj == &lwp_park_sobj) {
501 /* Releases the LWP lock. */
502 lwp_unsleep(t);
503 } else {
504 /*
505 * Set the operation pending. The next call to _lwp_park
506 * will return early.
507 */
508 t->l_flag |= LW_UNPARKED;
509 lwp_unlock(t);
510 }
511
512 mutex_exit(&p->p_smutex);
513 return 0;
514 }
515
516 int
517 lwp_park(struct timespec *ts, const void *hint)
518 {
519 struct timespec tsx;
520 sleepq_t *sq;
521 wchan_t wchan;
522 int timo, error;
523 lwp_t *l;
524
525 /* Fix up the given timeout value. */
526 if (ts != NULL) {
527 getnanotime(&tsx);
528 timespecsub(ts, &tsx, &tsx);
529 if (tsx.tv_sec < 0 || (tsx.tv_sec == 0 && tsx.tv_nsec <= 0))
530 return ETIMEDOUT;
531 if ((error = itimespecfix(&tsx)) != 0)
532 return error;
533 timo = tstohz(&tsx);
534 KASSERT(timo != 0);
535 } else
536 timo = 0;
537
538 /* Find and lock the sleep queue. */
539 l = curlwp;
540 wchan = lwp_park_wchan(l->l_proc, hint);
541 sq = sleeptab_lookup(&lwp_park_tab, wchan);
542
543 /*
544 * Before going the full route and blocking, check to see if an
545 * unpark op is pending.
546 */
547 lwp_lock(l);
548 if ((l->l_flag & (LW_CANCELLED | LW_UNPARKED)) != 0) {
549 l->l_flag &= ~(LW_CANCELLED | LW_UNPARKED);
550 lwp_unlock(l);
551 sleepq_unlock(sq);
552 return EALREADY;
553 }
554 lwp_unlock_to(l, sq->sq_mutex);
555 l->l_biglocks = 0;
556 sleepq_enqueue(sq, wchan, "parked", &lwp_park_sobj);
557 error = sleepq_block(timo, true);
558 switch (error) {
559 case EWOULDBLOCK:
560 error = ETIMEDOUT;
561 break;
562 case ERESTART:
563 error = EINTR;
564 break;
565 default:
566 /* nothing */
567 break;
568 }
569 return error;
570 }
571
572 /*
573 * 'park' an LWP waiting on a user-level synchronisation object. The LWP
574 * will remain parked until another LWP in the same process calls in and
575 * requests that it be unparked.
576 */
577 int
578 sys__lwp_park(struct lwp *l, void *v, register_t *retval)
579 {
580 struct sys__lwp_park_args /* {
581 syscallarg(const struct timespec *) ts;
582 syscallarg(lwpid_t) unpark;
583 syscallarg(const void *) hint;
584 syscallarg(const void *) unparkhint;
585 } */ *uap = v;
586 struct timespec ts, *tsp;
587 int error;
588
589 if (SCARG(uap, ts) == NULL)
590 tsp = NULL;
591 else {
592 error = copyin(SCARG(uap, ts), &ts, sizeof(ts));
593 if (error != 0)
594 return error;
595 tsp = &ts;
596 }
597
598 if (SCARG(uap, unpark) != 0) {
599 error = lwp_unpark(SCARG(uap, unpark), SCARG(uap, unparkhint));
600 if (error != 0)
601 return error;
602 }
603
604 return lwp_park(tsp, SCARG(uap, hint));
605 }
606
607 int
608 sys__lwp_unpark(struct lwp *l, void *v, register_t *retval)
609 {
610 struct sys__lwp_unpark_args /* {
611 syscallarg(lwpid_t) target;
612 syscallarg(const void *) hint;
613 } */ *uap = v;
614
615 return lwp_unpark(SCARG(uap, target), SCARG(uap, hint));
616 }
617
618 int
619 sys__lwp_unpark_all(struct lwp *l, void *v, register_t *retval)
620 {
621 struct sys__lwp_unpark_all_args /* {
622 syscallarg(const lwpid_t *) targets;
623 syscallarg(size_t) ntargets;
624 syscallarg(const void *) hint;
625 } */ *uap = v;
626 struct proc *p;
627 struct lwp *t;
628 sleepq_t *sq;
629 wchan_t wchan;
630 lwpid_t targets[32], *tp, *tpp, *tmax, target;
631 int swapin, error;
632 u_int ntargets;
633 size_t sz;
634
635 p = l->l_proc;
636 ntargets = SCARG(uap, ntargets);
637
638 if (SCARG(uap, targets) == NULL) {
639 /*
640 * Let the caller know how much we are willing to do, and
641 * let it unpark the LWPs in blocks.
642 */
643 *retval = LWP_UNPARK_MAX;
644 return 0;
645 }
646 if (ntargets > LWP_UNPARK_MAX || ntargets == 0)
647 return EINVAL;
648
649 /*
650 * Copy in the target array. If it's a small number of LWPs, then
651 * place the numbers on the stack.
652 */
653 sz = sizeof(target) * ntargets;
654 if (sz <= sizeof(targets))
655 tp = targets;
656 else {
657 KERNEL_LOCK(1, l); /* XXXSMP */
658 tp = kmem_alloc(sz, KM_SLEEP);
659 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
660 if (tp == NULL)
661 return ENOMEM;
662 }
663 error = copyin(SCARG(uap, targets), tp, sz);
664 if (error != 0) {
665 if (tp != targets) {
666 KERNEL_LOCK(1, l); /* XXXSMP */
667 kmem_free(tp, sz);
668 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
669 }
670 return error;
671 }
672
673 swapin = 0;
674 wchan = lwp_park_wchan(p, SCARG(uap, hint));
675 sq = sleeptab_lookup(&lwp_park_tab, wchan);
676
677 for (tmax = tp + ntargets, tpp = tp; tpp < tmax; tpp++) {
678 target = *tpp;
679
680 /*
681 * Easy case: search for the LWP on the sleep queue. If
682 * it's parked, remove it from the queue and set running.
683 */
684 TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
685 if (t->l_proc == p && t->l_lid == target)
686 break;
687
688 if (t != NULL) {
689 swapin |= sleepq_remove(sq, t);
690 continue;
691 }
692
693 /*
694 * The LWP hasn't parked yet. Take the hit and
695 * mark the operation as pending.
696 */
697 sleepq_unlock(sq);
698 mutex_enter(&p->p_smutex);
699 if ((t = lwp_find(p, target)) == NULL) {
700 mutex_exit(&p->p_smutex);
701 sleepq_lock(sq);
702 continue;
703 }
704 lwp_lock(t);
705
706 /*
707 * It may not have parked yet, we may have raced, or
708 * it is parked on a different user sync object.
709 */
710 if (t->l_syncobj == &lwp_park_sobj) {
711 /* Releases the LWP lock. */
712 lwp_unsleep(t);
713 } else {
714 /*
715 * Set the operation pending. The next call to
716 * _lwp_park will return early.
717 */
718 t->l_flag |= LW_UNPARKED;
719 lwp_unlock(t);
720 }
721
722 mutex_exit(&p->p_smutex);
723 sleepq_lock(sq);
724 }
725
726 sleepq_unlock(sq);
727 if (tp != targets) {
728 KERNEL_LOCK(1, l); /* XXXSMP */
729 kmem_free(tp, sz);
730 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
731 }
732 if (swapin)
733 uvm_kick_scheduler();
734
735 return 0;
736 }
737
738 int
739 sys__lwp_setname(struct lwp *l, void *v, register_t *retval)
740 {
741 struct sys__lwp_setname_args /* {
742 syscallarg(lwpid_t) target;
743 syscallarg(const char *) name;
744 } */ *uap = v;
745 char *name, *oname;
746 proc_t *p;
747 lwp_t *t;
748 int error;
749
750 name = kmem_alloc(MAXCOMLEN, KM_SLEEP);
751 if (name == NULL)
752 return ENOMEM;
753 error = copyinstr(SCARG(uap, name), name, MAXCOMLEN, NULL);
754 switch (error) {
755 case ENAMETOOLONG:
756 case 0:
757 name[MAXCOMLEN - 1] = '\0';
758 break;
759 default:
760 kmem_free(name, MAXCOMLEN);
761 return error;
762 }
763
764 p = curproc;
765 mutex_enter(&p->p_smutex);
766 if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
767 mutex_exit(&p->p_smutex);
768 kmem_free(name, MAXCOMLEN);
769 return ESRCH;
770 }
771 lwp_lock(t);
772 oname = t->l_name;
773 t->l_name = name;
774 lwp_unlock(t);
775 mutex_exit(&p->p_smutex);
776
777 if (oname != NULL)
778 kmem_free(oname, MAXCOMLEN);
779
780 return 0;
781 }
782
783 int
784 sys__lwp_getname(struct lwp *l, void *v, register_t *retval)
785 {
786 struct sys__lwp_getname_args /* {
787 syscallarg(lwpid_t) target;
788 syscallarg(char *) name;
789 syscallarg(size_t) len;
790 } */ *uap = v;
791 char name[MAXCOMLEN];
792 proc_t *p;
793 lwp_t *t;
794
795 p = curproc;
796 mutex_enter(&p->p_smutex);
797 if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
798 mutex_exit(&p->p_smutex);
799 kmem_free(name, MAXCOMLEN);
800 return ESRCH;
801 }
802 lwp_lock(t);
803 if (t->l_name == NULL)
804 name[0] = '\0';
805 else
806 strcpy(name, t->l_name);
807 lwp_unlock(t);
808 mutex_exit(&p->p_smutex);
809
810 return copyoutstr(name, SCARG(uap, name), SCARG(uap, len), NULL);
811 }
812