[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_kthread.c

Version: -  FREEBSD  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  cheribsd  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1  -  FREEBSD-LIBC  -  FREEBSD8-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  3  -  10 

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

Cache object: fe051c3ffb42739a4cafba648e0b03e1


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.