577b82d7c09078fea0c159430b85ce934e04e3df
[dragonfly.git] / sys / kern / kern_kthread.c
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: src/sys/kern/kern_kthread.c,v 1.5.2.3 2001/12/25 01:51:14 dillon Exp $
27  * $DragonFly: src/sys/kern/kern_kthread.c,v 1.3 2003/06/22 04:30:42 dillon Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/proc.h>
33 #include <sys/kthread.h>
34 #include <sys/ptrace.h>
35 #include <sys/resourcevar.h>
36 #include <sys/signalvar.h>
37 #include <sys/unistd.h>
38 #include <sys/wait.h>
39
40 #include <machine/stdarg.h>
41
42 /*
43  * Start a kernel process.  This is called after a fork() call in
44  * mi_startup() in the file kern/init_main.c.
45  *
46  * This function is used to start "internal" daemons and intended
47  * to be called from SYSINIT().
48  */
49 void
50 kproc_start(udata)
51         const void *udata;
52 {
53         const struct kproc_desc *kp = udata;
54         int error;
55
56         error = kthread_create((void (*)(void *))kp->func, NULL,
57                     kp->global_procpp, kp->arg0);
58         if (error)
59                 panic("kproc_start: %s: error %d", kp->arg0, error);
60 }
61
62 /*
63  * Create a kernel process/thread/whatever.  It shares it's address space
64  * with proc0 - ie: kernel only.
65  */
66 int
67 kthread_create(void (*func)(void *), void *arg,
68     struct proc **newpp, const char *fmt, ...)
69 {
70         int error;
71         va_list ap;
72         struct proc *p2;
73
74         if (!proc0.p_stats /* || proc0.p_stats->p_start.tv_sec == 0 */) {
75                 panic("kthread_create called too soon");
76         }
77
78         error = fork1(&proc0, RFMEM | RFFDG | RFPROC, &p2);
79         if (error)
80                 return error;
81
82         /* save a global descriptor, if desired */
83         if (newpp != NULL)
84                 *newpp = p2;
85
86         /* this is a non-swapped system process */
87         p2->p_flag |= P_INMEM | P_SYSTEM;
88         p2->p_procsig->ps_flag |= PS_NOCLDWAIT;
89         PHOLD(p2);
90
91         /* set up arg0 for 'ps', et al */
92         va_start(ap, fmt);
93         vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
94         va_end(ap);
95
96         /* call the processes' main()... */
97         cpu_set_fork_handler(p2, func, arg);
98         start_forked_proc(&proc0, p2);
99
100         return 0;
101 }
102
103 void
104 kthread_exit(int ecode)
105 {
106         proc_reparent(curproc, initproc);
107         exit1(curproc, W_EXITCODE(ecode, 0));
108 }
109
110 /*
111  * Advise a kernel process to suspend (or resume) in its main loop.
112  * Participation is voluntary.
113  */
114 int
115 suspend_kproc(struct proc *p, int timo)
116 {
117         /*
118          * Make sure this is indeed a system process and we can safely
119          * use the p_siglist field.
120          */
121         if ((p->p_flag & P_SYSTEM) == 0)
122                 return (EINVAL);
123         SIGADDSET(p->p_siglist, SIGSTOP);
124         wakeup(p);
125         return tsleep((caddr_t)&p->p_siglist, PPAUSE, "suspkp", timo);
126 }
127
128 int
129 resume_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
142 void
143 kproc_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 }