2005-01-06 23:35:40 +00:00
|
|
|
/*-
|
2003-04-01 00:30:30 +00:00
|
|
|
* Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice unmodified, this list of conditions, and the following
|
|
|
|
* disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2006-09-22 15:04:28 +00:00
|
|
|
#include "opt_compat.h"
|
2006-07-13 06:26:43 +00:00
|
|
|
#include "opt_posix.h"
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
2006-11-06 13:42:10 +00:00
|
|
|
#include <sys/priv.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/proc.h>
|
2006-11-11 16:46:31 +00:00
|
|
|
#include <sys/posix4.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/resourcevar.h>
|
2003-04-11 19:24:37 +00:00
|
|
|
#include <sys/sched.h>
|
2004-09-07 06:33:39 +00:00
|
|
|
#include <sys/sysctl.h>
|
2004-09-05 02:09:54 +00:00
|
|
|
#include <sys/smp.h>
|
2007-06-07 19:45:19 +00:00
|
|
|
#include <sys/syscallsubr.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/sysent.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/sysproto.h>
|
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/ucontext.h>
|
|
|
|
#include <sys/thr.h>
|
2006-07-11 05:34:35 +00:00
|
|
|
#include <sys/rtprio.h>
|
2005-10-26 06:55:46 +00:00
|
|
|
#include <sys/umtx.h>
|
|
|
|
#include <sys/limits.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
|
|
|
|
#include <machine/frame.h>
|
|
|
|
|
2007-08-16 05:26:42 +00:00
|
|
|
#include <security/audit/audit.h>
|
|
|
|
|
2006-09-22 15:04:28 +00:00
|
|
|
#ifdef COMPAT_IA32
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
suword_lwpid(void *addr, lwpid_t lwpid)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2008-11-22 12:36:15 +00:00
|
|
|
if (SV_CURPROC_FLAG(SV_LP64))
|
2006-09-22 15:04:28 +00:00
|
|
|
error = suword(addr, lwpid);
|
|
|
|
else
|
|
|
|
error = suword32(addr, lwpid);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define suword_lwpid suword
|
|
|
|
#endif
|
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
extern int max_threads_per_proc;
|
2008-10-29 12:11:48 +00:00
|
|
|
extern int max_threads_hits;
|
2004-09-07 06:33:39 +00:00
|
|
|
|
2005-04-23 02:36:07 +00:00
|
|
|
static int create_thread(struct thread *td, mcontext_t *ctx,
|
|
|
|
void (*start_func)(void *), void *arg,
|
|
|
|
char *stack_base, size_t stack_size,
|
|
|
|
char *tls_base,
|
|
|
|
long *child_tid, long *parent_tid,
|
2006-09-21 04:18:46 +00:00
|
|
|
int flags, struct rtprio *rtp);
|
2005-04-23 02:36:07 +00:00
|
|
|
|
2003-04-01 00:30:30 +00:00
|
|
|
/*
|
|
|
|
* System call interface.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
thr_create(struct thread *td, struct thr_create_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* ucontext_t *ctx, long *id, int flags */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
|
|
|
ucontext_t ctx;
|
|
|
|
int error;
|
2005-04-23 02:36:07 +00:00
|
|
|
|
|
|
|
if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
error = create_thread(td, &ctx.uc_mcontext, NULL, NULL,
|
2006-07-11 05:34:35 +00:00
|
|
|
NULL, 0, NULL, uap->id, NULL, uap->flags, NULL);
|
2005-04-23 02:36:07 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_new(struct thread *td, struct thr_new_args *uap)
|
|
|
|
/* struct thr_param * */
|
|
|
|
{
|
|
|
|
struct thr_param param;
|
|
|
|
int error;
|
|
|
|
|
2006-09-22 15:04:28 +00:00
|
|
|
if (uap->param_size < 0 || uap->param_size > sizeof(param))
|
2005-04-23 02:36:07 +00:00
|
|
|
return (EINVAL);
|
2006-09-21 04:18:46 +00:00
|
|
|
bzero(¶m, sizeof(param));
|
2006-09-22 15:04:28 +00:00
|
|
|
if ((error = copyin(uap->param, ¶m, uap->param_size)))
|
2005-04-23 02:36:07 +00:00
|
|
|
return (error);
|
2006-09-22 15:04:28 +00:00
|
|
|
return (kern_thr_new(td, ¶m));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kern_thr_new(struct thread *td, struct thr_param *param)
|
|
|
|
{
|
|
|
|
struct rtprio rtp, *rtpp;
|
|
|
|
int error;
|
|
|
|
|
2006-09-21 04:18:46 +00:00
|
|
|
rtpp = NULL;
|
2006-09-22 15:04:28 +00:00
|
|
|
if (param->rtp != 0) {
|
|
|
|
error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
|
2008-10-13 21:04:52 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
2006-09-21 04:18:46 +00:00
|
|
|
rtpp = &rtp;
|
2006-07-11 05:34:35 +00:00
|
|
|
}
|
2006-09-22 15:04:28 +00:00
|
|
|
error = create_thread(td, NULL, param->start_func, param->arg,
|
|
|
|
param->stack_base, param->stack_size, param->tls_base,
|
|
|
|
param->child_tid, param->parent_tid, param->flags,
|
2006-09-21 04:18:46 +00:00
|
|
|
rtpp);
|
2005-04-23 02:36:07 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
create_thread(struct thread *td, mcontext_t *ctx,
|
|
|
|
void (*start_func)(void *), void *arg,
|
|
|
|
char *stack_base, size_t stack_size,
|
|
|
|
char *tls_base,
|
|
|
|
long *child_tid, long *parent_tid,
|
2006-09-21 04:18:46 +00:00
|
|
|
int flags, struct rtprio *rtp)
|
2005-04-23 02:36:07 +00:00
|
|
|
{
|
|
|
|
stack_t stack;
|
|
|
|
struct thread *newtd;
|
2004-09-05 02:09:54 +00:00
|
|
|
struct proc *p;
|
2006-07-10 23:14:07 +00:00
|
|
|
int error;
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
p = td->td_proc;
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2005-04-23 02:36:07 +00:00
|
|
|
/* Have race condition but it is cheap. */
|
2008-10-29 12:11:48 +00:00
|
|
|
if (p->p_numthreads >= max_threads_per_proc) {
|
|
|
|
++max_threads_hits;
|
2004-09-05 02:09:54 +00:00
|
|
|
return (EPROCLIM);
|
2008-10-29 12:11:48 +00:00
|
|
|
}
|
2005-04-23 02:36:07 +00:00
|
|
|
|
2006-09-21 04:18:46 +00:00
|
|
|
if (rtp != NULL) {
|
|
|
|
switch(rtp->type) {
|
|
|
|
case RTP_PRIO_REALTIME:
|
|
|
|
case RTP_PRIO_FIFO:
|
2006-07-11 08:19:57 +00:00
|
|
|
/* Only root can set scheduler policy */
|
2006-11-06 13:42:10 +00:00
|
|
|
if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
|
2006-07-11 05:34:35 +00:00
|
|
|
return (EPERM);
|
2006-09-21 04:18:46 +00:00
|
|
|
if (rtp->prio > RTP_PRIO_MAX)
|
2006-07-11 05:34:35 +00:00
|
|
|
return (EINVAL);
|
2006-07-11 08:19:57 +00:00
|
|
|
break;
|
2006-09-21 04:18:46 +00:00
|
|
|
case RTP_PRIO_NORMAL:
|
|
|
|
rtp->prio = 0;
|
2006-07-11 08:19:57 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
2006-07-11 05:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-06 06:34:57 +00:00
|
|
|
/* Initialize our td */
|
2004-09-05 02:09:54 +00:00
|
|
|
newtd = thread_alloc();
|
2007-11-05 11:36:16 +00:00
|
|
|
if (newtd == NULL)
|
|
|
|
return (ENOMEM);
|
2005-04-23 02:36:07 +00:00
|
|
|
|
2003-04-01 00:30:30 +00:00
|
|
|
/*
|
2005-04-23 02:36:07 +00:00
|
|
|
* Try the copyout as soon as we allocate the td so we don't
|
|
|
|
* have to tear things down in a failure case below.
|
|
|
|
* Here we copy out tid to two places, one for child and one
|
|
|
|
* for parent, because pthread can create a detached thread,
|
|
|
|
* if parent wants to safely access child tid, it has to provide
|
|
|
|
* its storage, because child thread may exit quickly and
|
|
|
|
* memory is freed before parent thread can access it.
|
2003-04-01 00:30:30 +00:00
|
|
|
*/
|
2005-04-23 02:36:07 +00:00
|
|
|
if ((child_tid != NULL &&
|
2006-09-22 15:04:28 +00:00
|
|
|
suword_lwpid(child_tid, newtd->td_tid)) ||
|
2005-04-23 02:36:07 +00:00
|
|
|
(parent_tid != NULL &&
|
2006-09-22 15:04:28 +00:00
|
|
|
suword_lwpid(parent_tid, newtd->td_tid))) {
|
|
|
|
thread_free(newtd);
|
|
|
|
return (EFAULT);
|
2003-04-01 00:30:30 +00:00
|
|
|
}
|
2006-09-22 15:04:28 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
bzero(&newtd->td_startzero,
|
2004-11-20 23:00:59 +00:00
|
|
|
__rangeof(struct thread, td_startzero, td_endzero));
|
2004-09-05 02:09:54 +00:00
|
|
|
bcopy(&td->td_startcopy, &newtd->td_startcopy,
|
2004-11-20 23:00:59 +00:00
|
|
|
__rangeof(struct thread, td_startcopy, td_endcopy));
|
2004-09-05 02:09:54 +00:00
|
|
|
newtd->td_proc = td->td_proc;
|
|
|
|
newtd->td_ucred = crhold(td->td_ucred);
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
cpu_set_upcall(newtd, td);
|
2005-04-23 02:36:07 +00:00
|
|
|
|
|
|
|
if (ctx != NULL) { /* old way to set user context */
|
|
|
|
error = set_mcontext(newtd, ctx);
|
|
|
|
if (error != 0) {
|
|
|
|
thread_free(newtd);
|
|
|
|
crfree(td->td_ucred);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Set up our machine context. */
|
|
|
|
stack.ss_sp = stack_base;
|
|
|
|
stack.ss_size = stack_size;
|
|
|
|
/* Set upcall address to user thread entry function. */
|
|
|
|
cpu_set_upcall_kse(newtd, start_func, arg, &stack);
|
|
|
|
/* Setup user TLS address and TLS pointer register. */
|
2005-07-10 23:31:11 +00:00
|
|
|
error = cpu_set_user_tls(newtd, tls_base);
|
|
|
|
if (error != 0) {
|
|
|
|
thread_free(newtd);
|
|
|
|
crfree(td->td_ucred);
|
|
|
|
return (error);
|
|
|
|
}
|
2004-07-02 00:40:07 +00:00
|
|
|
}
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2006-07-10 23:14:07 +00:00
|
|
|
PROC_LOCK(td->td_proc);
|
2006-10-26 21:42:22 +00:00
|
|
|
td->td_proc->p_flag |= P_HADTHREADS;
|
|
|
|
newtd->td_sigmask = td->td_sigmask;
|
|
|
|
thread_link(newtd, p);
|
2007-11-15 06:35:26 +00:00
|
|
|
bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_lock(td);
|
2004-09-05 02:09:54 +00:00
|
|
|
/* let the scheduler know about these things. */
|
|
|
|
sched_fork_thread(td, newtd);
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_unlock(td);
|
2008-03-21 08:23:25 +00:00
|
|
|
if (P_SHOULDSTOP(p))
|
|
|
|
newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
PROC_UNLOCK(p);
|
|
|
|
thread_lock(newtd);
|
2006-10-26 21:42:22 +00:00
|
|
|
if (rtp != NULL) {
|
|
|
|
if (!(td->td_pri_class == PRI_TIMESHARE &&
|
|
|
|
rtp->type == RTP_PRIO_NORMAL)) {
|
|
|
|
rtp_to_pri(rtp, newtd);
|
|
|
|
sched_prio(newtd, newtd->td_user_pri);
|
|
|
|
} /* ignore timesharing class */
|
|
|
|
}
|
2004-09-05 02:09:54 +00:00
|
|
|
TD_SET_CAN_RUN(newtd);
|
2008-03-23 02:03:06 +00:00
|
|
|
sched_add(newtd, SRQ_BORING);
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_unlock(newtd);
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2009-02-26 15:51:54 +00:00
|
|
|
return (0);
|
2003-04-01 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_self(struct thread *td, struct thr_self_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* long *id */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2006-09-22 15:04:28 +00:00
|
|
|
error = suword_lwpid(uap->id, (unsigned)td->td_tid);
|
|
|
|
if (error == -1)
|
|
|
|
return (EFAULT);
|
2003-04-01 00:30:30 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_exit(struct thread *td, struct thr_exit_args *uap)
|
2004-10-06 14:23:00 +00:00
|
|
|
/* long *state */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
|
|
|
struct proc *p;
|
|
|
|
|
|
|
|
p = td->td_proc;
|
|
|
|
|
2004-10-06 14:23:00 +00:00
|
|
|
/* Signal userland that it can free the stack. */
|
2005-10-26 06:55:46 +00:00
|
|
|
if ((void *)uap->state != NULL) {
|
2006-09-22 15:04:28 +00:00
|
|
|
suword_lwpid(uap->state, 1);
|
2008-04-29 05:48:05 +00:00
|
|
|
kern_umtx_wake(td, uap->state, INT_MAX, 0);
|
2005-10-26 06:55:46 +00:00
|
|
|
}
|
2004-10-06 14:23:00 +00:00
|
|
|
|
2003-04-01 00:30:30 +00:00
|
|
|
PROC_LOCK(p);
|
1. Change prototype of trapsignal and sendsig to use ksiginfo_t *, most
changes in MD code are trivial, before this change, trapsignal and
sendsig use discrete parameters, now they uses member fields of
ksiginfo_t structure. For sendsig, this change allows us to pass
POSIX realtime signal value to user code.
2. Remove cpu_thread_siginfo, it is no longer needed because we now always
generate ksiginfo_t data and feed it to libpthread.
3. Add p_sigqueue to proc structure to hold shared signals which were
blocked by all threads in the proc.
4. Add td_sigqueue to thread structure to hold all signals delivered to
thread.
5. i386 and amd64 now return POSIX standard si_code, other arches will
be fixed.
6. In this sigqueue implementation, pending signal set is kept as before,
an extra siginfo list holds additional siginfo_t data for signals.
kernel code uses psignal() still behavior as before, it won't be failed
even under memory pressure, only exception is when deleting a signal,
we should call sigqueue_delete to remove signal from sigqueue but
not SIGDELSET. Current there is no kernel code will deliver a signal
with additional data, so kernel should be as stable as before,
a ksiginfo can carry more information, for example, allow signal to
be delivered but throw away siginfo data if memory is not enough.
SIGKILL and SIGSTOP have fast path in sigqueue_add, because they can
not be caught or masked.
The sigqueue() syscall allows user code to queue a signal to target
process, if resource is unavailable, EAGAIN will be returned as
specification said.
Just before thread exits, signal queue memory will be freed by
sigqueue_flush.
Current, all signals are allowed to be queued, not only realtime signals.
Earlier patch reviewed by: jhb, deischen
Tested on: i386, amd64
2005-10-14 12:43:47 +00:00
|
|
|
sigqueue_flush(&td->td_sigqueue);
|
2008-11-05 03:01:23 +00:00
|
|
|
PROC_SLOCK(p);
|
2003-04-01 00:30:30 +00:00
|
|
|
|
|
|
|
/*
|
2004-09-05 02:09:54 +00:00
|
|
|
* Shutting down last thread in the proc. This will actually
|
|
|
|
* call exit() in the trampoline when it returns.
|
2003-04-01 00:30:30 +00:00
|
|
|
*/
|
2004-09-05 02:09:54 +00:00
|
|
|
if (p->p_numthreads != 1) {
|
2006-02-10 03:34:29 +00:00
|
|
|
thread_stopped(p);
|
2004-09-05 02:09:54 +00:00
|
|
|
thread_exit();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2008-11-05 03:01:23 +00:00
|
|
|
PROC_SUNLOCK(p);
|
2004-09-05 02:09:54 +00:00
|
|
|
PROC_UNLOCK(p);
|
2003-04-01 00:30:30 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_kill(struct thread *td, struct thr_kill_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* long id, int sig */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
|
|
|
struct thread *ttd;
|
|
|
|
struct proc *p;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
p = td->td_proc;
|
|
|
|
error = 0;
|
|
|
|
PROC_LOCK(p);
|
2006-01-07 03:15:21 +00:00
|
|
|
if (uap->id == -1) {
|
|
|
|
if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
|
|
|
|
error = EINVAL;
|
|
|
|
} else {
|
|
|
|
error = ESRCH;
|
|
|
|
FOREACH_THREAD_IN_PROC(p, ttd) {
|
|
|
|
if (ttd != td) {
|
|
|
|
error = 0;
|
|
|
|
if (uap->sig == 0)
|
|
|
|
break;
|
|
|
|
tdsignal(p, ttd, uap->sig, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (uap->id != td->td_tid)
|
|
|
|
ttd = thread_find(p, uap->id);
|
|
|
|
else
|
|
|
|
ttd = td;
|
|
|
|
if (ttd == NULL)
|
|
|
|
error = ESRCH;
|
|
|
|
else if (uap->sig == 0)
|
|
|
|
;
|
|
|
|
else if (!_SIG_VALID(uap->sig))
|
|
|
|
error = EINVAL;
|
|
|
|
else
|
|
|
|
tdsignal(p, ttd, uap->sig, NULL);
|
2003-04-01 00:30:30 +00:00
|
|
|
}
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (error);
|
|
|
|
}
|
2004-03-27 14:30:43 +00:00
|
|
|
|
2007-08-16 05:26:42 +00:00
|
|
|
int
|
|
|
|
thr_kill2(struct thread *td, struct thr_kill2_args *uap)
|
|
|
|
/* pid_t pid, long id, int sig */
|
|
|
|
{
|
|
|
|
struct thread *ttd;
|
|
|
|
struct proc *p;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
AUDIT_ARG(signum, uap->sig);
|
|
|
|
|
|
|
|
if (uap->pid == td->td_proc->p_pid) {
|
|
|
|
p = td->td_proc;
|
|
|
|
PROC_LOCK(p);
|
|
|
|
} else if ((p = pfind(uap->pid)) == NULL) {
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
AUDIT_ARG(process, p);
|
|
|
|
|
|
|
|
error = p_cansignal(td, p, uap->sig);
|
|
|
|
if (error == 0) {
|
|
|
|
if (uap->id == -1) {
|
|
|
|
if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
|
|
|
|
error = EINVAL;
|
|
|
|
} else {
|
|
|
|
error = ESRCH;
|
|
|
|
FOREACH_THREAD_IN_PROC(p, ttd) {
|
|
|
|
if (ttd != td) {
|
|
|
|
error = 0;
|
|
|
|
if (uap->sig == 0)
|
|
|
|
break;
|
|
|
|
tdsignal(p, ttd, uap->sig, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (uap->id != td->td_tid)
|
|
|
|
ttd = thread_find(p, uap->id);
|
|
|
|
else
|
|
|
|
ttd = td;
|
|
|
|
if (ttd == NULL)
|
|
|
|
error = ESRCH;
|
|
|
|
else if (uap->sig == 0)
|
|
|
|
;
|
|
|
|
else if (!_SIG_VALID(uap->sig))
|
|
|
|
error = EINVAL;
|
|
|
|
else
|
|
|
|
tdsignal(p, ttd, uap->sig, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-03-27 14:30:43 +00:00
|
|
|
int
|
|
|
|
thr_suspend(struct thread *td, struct thr_suspend_args *uap)
|
|
|
|
/* const struct timespec *timeout */
|
|
|
|
{
|
2006-09-22 15:04:28 +00:00
|
|
|
struct timespec ts, *tsp;
|
2004-03-27 14:30:43 +00:00
|
|
|
int error;
|
|
|
|
|
2006-09-22 15:04:28 +00:00
|
|
|
tsp = NULL;
|
2004-03-27 14:30:43 +00:00
|
|
|
if (uap->timeout != NULL) {
|
|
|
|
error = copyin((const void *)uap->timeout, (void *)&ts,
|
|
|
|
sizeof(struct timespec));
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
2006-09-22 15:04:28 +00:00
|
|
|
tsp = &ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (kern_thr_suspend(td, tsp));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kern_thr_suspend(struct thread *td, struct timespec *tsp)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
int error = 0, hz = 0;
|
|
|
|
|
|
|
|
if (tsp != NULL) {
|
|
|
|
if (tsp->tv_nsec < 0 || tsp->tv_nsec > 1000000000)
|
2004-03-27 14:30:43 +00:00
|
|
|
return (EINVAL);
|
2006-09-22 15:04:28 +00:00
|
|
|
if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
|
2004-03-27 14:30:43 +00:00
|
|
|
return (ETIMEDOUT);
|
2006-09-22 15:04:28 +00:00
|
|
|
TIMESPEC_TO_TIMEVAL(&tv, tsp);
|
2004-03-27 14:30:43 +00:00
|
|
|
hz = tvtohz(&tv);
|
|
|
|
}
|
2006-12-04 14:15:12 +00:00
|
|
|
|
|
|
|
if (td->td_pflags & TDP_WAKEUP) {
|
|
|
|
td->td_pflags &= ~TDP_WAKEUP;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-03-27 14:30:43 +00:00
|
|
|
PROC_LOCK(td->td_proc);
|
2004-07-12 15:28:31 +00:00
|
|
|
if ((td->td_flags & TDF_THRWAKEUP) == 0)
|
2006-04-17 18:20:38 +00:00
|
|
|
error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr",
|
|
|
|
hz);
|
2004-12-01 13:50:04 +00:00
|
|
|
if (td->td_flags & TDF_THRWAKEUP) {
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_lock(td);
|
2004-12-01 13:50:04 +00:00
|
|
|
td->td_flags &= ~TDF_THRWAKEUP;
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_unlock(td);
|
2004-12-01 13:50:04 +00:00
|
|
|
PROC_UNLOCK(td->td_proc);
|
|
|
|
return (0);
|
|
|
|
}
|
2004-03-27 14:30:43 +00:00
|
|
|
PROC_UNLOCK(td->td_proc);
|
2004-12-01 13:50:04 +00:00
|
|
|
if (error == EWOULDBLOCK)
|
|
|
|
error = ETIMEDOUT;
|
|
|
|
else if (error == ERESTART) {
|
|
|
|
if (hz != 0)
|
|
|
|
error = EINTR;
|
|
|
|
}
|
|
|
|
return (error);
|
2004-03-27 14:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_wake(struct thread *td, struct thr_wake_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* long id */
|
2004-03-27 14:30:43 +00:00
|
|
|
{
|
2005-11-03 01:34:08 +00:00
|
|
|
struct proc *p;
|
2004-07-02 00:40:07 +00:00
|
|
|
struct thread *ttd;
|
2004-03-27 14:30:43 +00:00
|
|
|
|
2006-12-04 14:15:12 +00:00
|
|
|
if (uap->id == td->td_tid) {
|
|
|
|
td->td_pflags |= TDP_WAKEUP;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2005-11-03 01:34:08 +00:00
|
|
|
p = td->td_proc;
|
|
|
|
PROC_LOCK(p);
|
|
|
|
ttd = thread_find(p, uap->id);
|
2004-03-27 14:30:43 +00:00
|
|
|
if (ttd == NULL) {
|
2005-11-03 01:34:08 +00:00
|
|
|
PROC_UNLOCK(p);
|
2004-03-27 14:30:43 +00:00
|
|
|
return (ESRCH);
|
|
|
|
}
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_lock(ttd);
|
2004-07-02 00:40:07 +00:00
|
|
|
ttd->td_flags |= TDF_THRWAKEUP;
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_unlock(ttd);
|
2004-12-01 13:50:04 +00:00
|
|
|
wakeup((void *)ttd);
|
2005-11-03 01:34:08 +00:00
|
|
|
PROC_UNLOCK(p);
|
2004-03-27 14:30:43 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2006-02-05 02:18:46 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
thr_set_name(struct thread *td, struct thr_set_name_args *uap)
|
|
|
|
{
|
|
|
|
struct proc *p = td->td_proc;
|
|
|
|
char name[MAXCOMLEN + 1];
|
|
|
|
struct thread *ttd;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
name[0] = '\0';
|
|
|
|
if (uap->name != NULL) {
|
|
|
|
error = copyinstr(uap->name, name, sizeof(name),
|
|
|
|
NULL);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
PROC_LOCK(p);
|
|
|
|
if (uap->id == td->td_tid)
|
|
|
|
ttd = td;
|
|
|
|
else
|
|
|
|
ttd = thread_find(p, uap->id);
|
|
|
|
if (ttd != NULL)
|
|
|
|
strcpy(ttd->td_name, name);
|
|
|
|
else
|
|
|
|
error = ESRCH;
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (error);
|
|
|
|
}
|