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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kthread.h>
33 #include <sys/lock.h>
34 #include <sys/mutex.h>
35 #include <sys/proc.h>
36 #include <sys/resourcevar.h>
37 #include <sys/signalvar.h>
38 #include <sys/sx.h>
39 #include <sys/unistd.h>
40 #include <sys/wait.h>
41 #include <sys/sched.h>
42
43 #include <machine/stdarg.h>
44
45 /*
46 * Start a kernel process. This is called after a fork() call in
47 * mi_startup() in the file kern/init_main.c.
48 *
49 * This function is used to start "internal" daemons and intended
50 * to be called from SYSINIT().
51 */
52 void
53 kproc_start(udata)
54 const void *udata;
55 {
56 const struct kproc_desc *kp = udata;
57 int error;
58
59 error = kthread_create((void (*)(void *))kp->func, NULL,
60 kp->global_procpp, 0, 0, "%s", kp->arg0);
61 if (error)
62 panic("kproc_start: %s: error %d", kp->arg0, error);
63 }
64
65 /*
66 * Create a kernel process/thread/whatever. It shares its address space
67 * with proc0 - ie: kernel only.
68 *
69 * func is the function to start.
70 * arg is the parameter to pass to function on first startup.
71 * newpp is the return value pointing to the thread's struct proc.
72 * flags are flags to fork1 (in unistd.h)
73 * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
74 */
75 int
76 kthread_create(void (*func)(void *), void *arg,
77 struct proc **newpp, int flags, int pages, const char *fmt, ...)
78 {
79 int error;
80 va_list ap;
81 struct thread *td;
82 struct proc *p2;
83
84 if (!proc0.p_stats)
85 panic("kthread_create called too soon");
86
87 error = fork1(&thread0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags,
88 pages, &p2);
89 if (error)
90 return error;
91
92 /* save a global descriptor, if desired */
93 if (newpp != NULL)
94 *newpp = p2;
95
96 /* this is a non-swapped system process */
97 PROC_LOCK(p2);
98 p2->p_flag |= P_SYSTEM | P_KTHREAD;
99 mtx_lock(&p2->p_sigacts->ps_mtx);
100 p2->p_sigacts->ps_flag |= PS_NOCLDWAIT;
101 mtx_unlock(&p2->p_sigacts->ps_mtx);
102 PROC_UNLOCK(p2);
103
104 /* set up arg0 for 'ps', et al */
105 va_start(ap, fmt);
106 vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
107 va_end(ap);
108
109 /* call the processes' main()... */
110 td = FIRST_THREAD_IN_PROC(p2);
111 cpu_set_fork_handler(td, func, arg);
112 thread_lock(td);
113 TD_SET_CAN_RUN(td);
114 sched_prio(td, PVM);
115 sched_user_prio(td, PUSER);
116
117 /* Delay putting it on the run queue until now. */
118 if (!(flags & RFSTOPPED))
119 sched_add(td, SRQ_BORING);
120 thread_unlock(td);
121
122 return 0;
123 }
124
125 void
126 kthread_exit(int ecode)
127 {
128 struct thread *td;
129 struct proc *p;
130
131 td = curthread;
132 p = td->td_proc;
133
134 /*
135 * Reparent curthread from proc0 to init so that the zombie
136 * is harvested.
137 */
138 sx_xlock(&proctree_lock);
139 PROC_LOCK(p);
140 proc_reparent(p, initproc);
141 PROC_UNLOCK(p);
142 sx_xunlock(&proctree_lock);
143
144 /*
145 * Wakeup anyone waiting for us to exit.
146 */
147 wakeup(p);
148
149 /* Buh-bye! */
150 exit1(td, W_EXITCODE(ecode, 0));
151 }
152
153 /*
154 * Advise a kernel process to suspend (or resume) in its main loop.
155 * Participation is voluntary.
156 */
157 int
158 kthread_suspend(struct proc *p, int timo)
159 {
160 /*
161 * Make sure this is indeed a system process and we can safely
162 * use the p_siglist field.
163 */
164 PROC_LOCK(p);
165 if ((p->p_flag & P_KTHREAD) == 0) {
166 PROC_UNLOCK(p);
167 return (EINVAL);
168 }
169 SIGADDSET(p->p_siglist, SIGSTOP);
170 wakeup(p);
171 return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP, "suspkt", timo);
172 }
173
174 int
175 kthread_resume(struct proc *p)
176 {
177 /*
178 * Make sure this is indeed a system process and we can safely
179 * use the p_siglist field.
180 */
181 PROC_LOCK(p);
182 if ((p->p_flag & P_KTHREAD) == 0) {
183 PROC_UNLOCK(p);
184 return (EINVAL);
185 }
186 SIGDELSET(p->p_siglist, SIGSTOP);
187 PROC_UNLOCK(p);
188 wakeup(&p->p_siglist);
189 return (0);
190 }
191
192 void
193 kthread_suspend_check(struct proc *p)
194 {
195 PROC_LOCK(p);
196 while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
197 wakeup(&p->p_siglist);
198 msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "ktsusp", 0);
199 }
200 PROC_UNLOCK(p);
201 }
Cache object: 81ca85b70fd6fd1973bb9d081a77f8c9
|