kern_kthread.c revision 65557
1/*
2 * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/kern/kern_kthread.c 65557 2000-09-07 01:33:02Z jasone $
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/proc.h>
32#include <sys/kthread.h>
33#include <sys/resourcevar.h>
34#include <sys/signalvar.h>
35#include <sys/unistd.h>
36#include <sys/wait.h>
37
38#include <machine/stdarg.h>
39
40/*
41 * Start a kernel process.  This is called after a fork() call in
42 * mi_startup() in the file kern/init_main.c.
43 *
44 * This function is used to start "internal" daemons and intended
45 * to be called from SYSINIT().
46 */
47void
48kproc_start(udata)
49	const void *udata;
50{
51	const struct kproc_desc	*kp = udata;
52	int error;
53
54	error = kthread_create((void (*)(void *))kp->func, NULL,
55		    kp->global_procpp, 0, kp->arg0);
56	if (error)
57		panic("kproc_start: %s: error %d", kp->arg0, error);
58}
59
60/*
61 * Create a kernel process/thread/whatever.  It shares its address space
62 * with proc0 - ie: kernel only.
63 *
64 * func is the function to start.
65 * arg is the parameter to pass to function on first startup.
66 * newpp is the return value pointing to the thread's struct proc.
67 * flags are flags to fork1 (in unistd.h)
68 * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
69 */
70int
71kthread_create(void (*func)(void *), void *arg,
72    struct proc **newpp, int flags, const char *fmt, ...)
73{
74	int error;
75	va_list ap;
76	struct proc *p2;
77
78	if (!proc0.p_stats /* || proc0.p_stats->p_start.tv_sec == 0 */)
79		panic("kthread_create called too soon");
80
81	error = fork1(&proc0, RFMEM | RFFDG | RFPROC | flags, &p2);
82	if (error)
83		return error;
84
85	/* save a global descriptor, if desired */
86	if (newpp != NULL)
87		*newpp = p2;
88
89	/* this is a non-swapped system process */
90	p2->p_flag |= P_INMEM | P_SYSTEM;
91	p2->p_procsig->ps_flag |= PS_NOCLDWAIT;
92	PHOLD(p2);
93
94	/* set up arg0 for 'ps', et al */
95	va_start(ap, fmt);
96	vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
97	va_end(ap);
98
99	/* call the processes' main()... */
100	cpu_set_fork_handler(p2, func, arg);
101
102	return 0;
103}
104
105void
106kthread_exit(int ecode)
107{
108	exit1(curproc, W_EXITCODE(ecode, 0));
109}
110
111/*
112 * Advise a kernel process to suspend (or resume) in its main loop.
113 * Participation is voluntary.
114 */
115int
116suspend_kproc(struct proc *p, int timo)
117{
118	/*
119	 * Make sure this is indeed a system process and we can safely
120	 * use the p_siglist field.
121	 */
122	if ((p->p_flag & P_SYSTEM) == 0)
123		return (EINVAL);
124	SIGADDSET(p->p_siglist, SIGSTOP);
125	return tsleep((caddr_t)&p->p_siglist, PPAUSE, "suspkp", timo);
126}
127
128int
129resume_kproc(struct proc *p)
130{
131	/*
132	 * Make sure this is indeed a system process and we can safely
133	 * use the p_siglist field.
134	 */
135	if ((p->p_flag & P_SYSTEM) == 0)
136		return (EINVAL);
137	SIGDELSET(p->p_siglist, SIGSTOP);
138	wakeup((caddr_t)&p->p_siglist);
139	return (0);
140}
141
142void
143kproc_suspend_loop(struct proc *p)
144{
145	while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
146		wakeup((caddr_t)&p->p_siglist);
147		tsleep((caddr_t)&p->p_siglist, PPAUSE, "kpsusp", 0);
148	}
149}
150