The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


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

FreeBSD/Linux Kernel Cross Reference
sys/bsd/kern/kern_proc.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
    3  *
    4  * @APPLE_LICENSE_HEADER_START@
    5  * 
    6  * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
    7  * 
    8  * This file contains Original Code and/or Modifications of Original Code
    9  * as defined in and that are subject to the Apple Public Source License
   10  * Version 2.0 (the 'License'). You may not use this file except in
   11  * compliance with the License. Please obtain a copy of the License at
   12  * http://www.opensource.apple.com/apsl/ and read it before using this
   13  * file.
   14  * 
   15  * The Original Code and all software distributed under the License are
   16  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
   17  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
   18  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
   19  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
   20  * Please see the License for the specific language governing rights and
   21  * limitations under the License.
   22  * 
   23  * @APPLE_LICENSE_HEADER_END@
   24  */
   25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
   26 /*
   27  * Copyright (c) 1982, 1986, 1989, 1991, 1993
   28  *      The Regents of the University of California.  All rights reserved.
   29  *
   30  * Redistribution and use in source and binary forms, with or without
   31  * modification, are permitted provided that the following conditions
   32  * are met:
   33  * 1. Redistributions of source code must retain the above copyright
   34  *    notice, this list of conditions and the following disclaimer.
   35  * 2. Redistributions in binary form must reproduce the above copyright
   36  *    notice, this list of conditions and the following disclaimer in the
   37  *    documentation and/or other materials provided with the distribution.
   38  * 3. All advertising materials mentioning features or use of this software
   39  *    must display the following acknowledgement:
   40  *      This product includes software developed by the University of
   41  *      California, Berkeley and its contributors.
   42  * 4. Neither the name of the University nor the names of its contributors
   43  *    may be used to endorse or promote products derived from this software
   44  *    without specific prior written permission.
   45  *
   46  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   56  * SUCH DAMAGE.
   57  *
   58  *      @(#)kern_proc.c 8.4 (Berkeley) 1/4/94
   59  */
   60 /* HISTORY
   61  *  04-Aug-97  Umesh Vaishampayan (umeshv@apple.com)
   62  *      Added current_proc_EXTERNAL() function for the use of kernel
   63  *      lodable modules.
   64  *
   65  *  05-Jun-95 Mac Gillon (mgillon) at NeXT
   66  *      New version based on 3.3NS and 4.4
   67  */
   68 
   69 
   70 #include <sys/param.h>
   71 #include <sys/systm.h>
   72 #include <sys/kernel.h>
   73 #include <sys/proc.h>
   74 #include <sys/buf.h>
   75 #include <sys/acct.h>
   76 #include <sys/wait.h>
   77 #include <sys/file.h>
   78 #include <ufs/ufs/quota.h>
   79 #include <sys/uio.h>
   80 #include <sys/malloc.h>
   81 #include <sys/mbuf.h>
   82 #include <sys/ioctl.h>
   83 #include <sys/tty.h>
   84 #include <sys/signalvar.h>
   85 
   86 /*
   87  * Structure associated with user cacheing.
   88  */
   89 struct uidinfo {
   90         LIST_ENTRY(uidinfo) ui_hash;
   91         uid_t   ui_uid;
   92         long    ui_proccnt;
   93 };
   94 #define UIHASH(uid)     (&uihashtbl[(uid) & uihash])
   95 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
   96 u_long uihash;          /* size of hash table - 1 */
   97 
   98 /*
   99  * Other process lists
  100  */
  101 struct pidhashhead *pidhashtbl;
  102 u_long pidhash;
  103 struct pgrphashhead *pgrphashtbl;
  104 u_long pgrphash;
  105 struct proclist allproc;
  106 struct proclist zombproc;
  107 
  108 /*
  109  * Initialize global process hashing structures.
  110  */
  111 void
  112 procinit()
  113 {
  114 
  115         LIST_INIT(&allproc);
  116         LIST_INIT(&zombproc);
  117         pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
  118         pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
  119         uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash);
  120 }
  121 
  122 /*
  123  * Change the count associated with number of processes
  124  * a given user is using.
  125  */
  126 int
  127 chgproccnt(uid, diff)
  128         uid_t   uid;
  129         int     diff;
  130 {
  131         register struct uidinfo *uip;
  132         register struct uihashhead *uipp;
  133 
  134         uipp = UIHASH(uid);
  135         for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
  136                 if (uip->ui_uid == uid)
  137                         break;
  138         if (uip) {
  139                 uip->ui_proccnt += diff;
  140                 if (uip->ui_proccnt > 0)
  141                         return (uip->ui_proccnt);
  142                 if (uip->ui_proccnt < 0)
  143                         panic("chgproccnt: procs < 0");
  144                 LIST_REMOVE(uip, ui_hash);
  145                 FREE_ZONE(uip, sizeof *uip, M_PROC);
  146                 return (0);
  147         }
  148         if (diff <= 0) {
  149                 if (diff == 0)
  150                         return(0);
  151                 panic("chgproccnt: lost user");
  152         }
  153         MALLOC_ZONE(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
  154         LIST_INSERT_HEAD(uipp, uip, ui_hash);
  155         uip->ui_uid = uid;
  156         uip->ui_proccnt = diff;
  157         return (diff);
  158 }
  159 
  160 /*
  161  * Is p an inferior of the current process?
  162  */
  163 int
  164 inferior(p)
  165         register struct proc *p;
  166 {
  167 
  168         for (; p != current_proc(); p = p->p_pptr)
  169                 if (p->p_pid == 0)
  170                         return (0);
  171         return (1);
  172 }
  173 /*
  174  * Is p an inferior of t ?
  175  */
  176 int
  177 isinferior(p, t)
  178         register struct proc *p;
  179         register struct proc *t;
  180 {
  181 
  182         /* if p==t they are not inferior */
  183         if (p == t)
  184                 return(0);
  185         for (; p != t; p = p->p_pptr)
  186                 if (p->p_pid == 0)
  187                         return (0);
  188         return (1);
  189 }
  190 
  191 /*
  192  * Locate a process by number
  193  */
  194 struct proc *
  195 pfind(pid)
  196         register pid_t pid;
  197 {
  198         register struct proc *p;
  199 
  200         if (!pid)
  201                 return (kernproc);
  202 
  203         for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
  204                 if (p->p_pid == pid)
  205                         return (p);
  206         return (NULL);
  207 }
  208 
  209 /*
  210  * Locate a zombie by PID
  211  */
  212 __private_extern__ struct proc *
  213 pzfind(pid)
  214         register pid_t pid;
  215 {
  216         register struct proc *p;
  217 
  218         for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
  219                 if (p->p_pid == pid)
  220                         return (p);
  221         return (NULL);
  222 }
  223 
  224 /*
  225  * Locate a process group by number
  226  */
  227 struct pgrp *
  228 pgfind(pgid)
  229         register pid_t pgid;
  230 {
  231         register struct pgrp *pgrp;
  232 
  233         for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
  234                 if (pgrp->pg_id == pgid)
  235                         return (pgrp);
  236         return (NULL);
  237 }
  238 
  239 
  240 /*
  241  * Move p to a new or existing process group (and session)
  242  */
  243 int
  244 enterpgrp(p, pgid, mksess)
  245         register struct proc *p;
  246         pid_t pgid;
  247         int mksess;
  248 {
  249         register struct pgrp *pgrp = pgfind(pgid);
  250 
  251 #if DIAGNOSTIC
  252         if (pgrp != NULL && mksess)     /* firewalls */
  253                 panic("enterpgrp: setsid into non-empty pgrp");
  254         if (SESS_LEADER(p))
  255                 panic("enterpgrp: session leader attempted setpgrp");
  256 #endif
  257         if (pgrp == NULL) {
  258                 pid_t savepid = p->p_pid;
  259                 struct proc *np;
  260                 /*
  261                  * new process group
  262                  */
  263 #if DIAGNOSTIC
  264                 if (p->p_pid != pgid)
  265                         panic("enterpgrp: new pgrp and pid != pgid");
  266 #endif
  267                 MALLOC_ZONE(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
  268                     M_WAITOK);
  269                 if ((np = pfind(savepid)) == NULL || np != p) {
  270                         FREE_ZONE(pgrp, sizeof(struct pgrp), M_PGRP);
  271                         return (ESRCH);
  272                 }
  273                 if (mksess) {
  274                         register struct session *sess;
  275 
  276                         /*
  277                          * new session
  278                          */
  279                         MALLOC_ZONE(sess, struct session *,
  280                                 sizeof(struct session), M_SESSION, M_WAITOK);
  281                         sess->s_leader = p;
  282                         sess->s_sid = p->p_pid;
  283                         sess->s_count = 1;
  284                         sess->s_ttyvp = NULL;
  285                         sess->s_ttyp = NULL;
  286                         bcopy(p->p_session->s_login, sess->s_login,
  287                             sizeof(sess->s_login));
  288                         p->p_flag &= ~P_CONTROLT;
  289                         pgrp->pg_session = sess;
  290 #if DIAGNOSTIC
  291                         if (p != current_proc())
  292                                 panic("enterpgrp: mksession and p != curproc");
  293 #endif
  294                 } else {
  295                         pgrp->pg_session = p->p_session;
  296                         pgrp->pg_session->s_count++;
  297                 }
  298                 pgrp->pg_id = pgid;
  299                 LIST_INIT(&pgrp->pg_members);
  300                 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
  301                 pgrp->pg_jobc = 0;
  302         } else if (pgrp == p->p_pgrp)
  303                 return (0);
  304 
  305         /*
  306          * Adjust eligibility of affected pgrps to participate in job control.
  307          * Increment eligibility counts before decrementing, otherwise we
  308          * could reach 0 spuriously during the first call.
  309          */
  310         fixjobc(p, pgrp, 1);
  311         fixjobc(p, p->p_pgrp, 0);
  312 
  313         LIST_REMOVE(p, p_pglist);
  314         if (p->p_pgrp->pg_members.lh_first == 0)
  315                 pgdelete(p->p_pgrp);
  316         p->p_pgrp = pgrp;
  317         LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
  318         return (0);
  319 }
  320 
  321 /*
  322  * remove process from process group
  323  */
  324 int
  325 leavepgrp(p)
  326         register struct proc *p;
  327 {
  328 
  329         LIST_REMOVE(p, p_pglist);
  330         if (p->p_pgrp->pg_members.lh_first == 0)
  331                 pgdelete(p->p_pgrp);
  332         p->p_pgrp = 0;
  333         return (0);
  334 }
  335 
  336 /*
  337  * delete a process group
  338  */
  339 void
  340 pgdelete(pgrp)
  341         register struct pgrp *pgrp;
  342 {
  343 
  344         if (pgrp->pg_session->s_ttyp != NULL && 
  345             pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
  346                 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
  347         LIST_REMOVE(pgrp, pg_hash);
  348         if (--pgrp->pg_session->s_count == 0)
  349                 FREE_ZONE(pgrp->pg_session, sizeof(struct session), M_SESSION);
  350         FREE_ZONE(pgrp, sizeof *pgrp, M_PGRP);
  351 }
  352 
  353 void
  354 sessrele(sess)
  355         struct session *sess;
  356 {
  357         if (--sess->s_count == 0)
  358                 FREE_ZONE(sess, sizeof (struct session), M_SESSION);
  359 }
  360 
  361 static void orphanpg();
  362 
  363 /*
  364  * Adjust pgrp jobc counters when specified process changes process group.
  365  * We count the number of processes in each process group that "qualify"
  366  * the group for terminal job control (those with a parent in a different
  367  * process group of the same session).  If that count reaches zero, the
  368  * process group becomes orphaned.  Check both the specified process'
  369  * process group and that of its children.
  370  * entering == 0 => p is leaving specified group.
  371  * entering == 1 => p is entering specified group.
  372  */
  373 void
  374 fixjobc(p, pgrp, entering)
  375         register struct proc *p;
  376         register struct pgrp *pgrp;
  377         int entering;
  378 {
  379         register struct pgrp *hispgrp;
  380         register struct session *mysession = pgrp->pg_session;
  381 
  382         /*
  383          * Check p's parent to see whether p qualifies its own process
  384          * group; if so, adjust count for p's process group.
  385          */
  386         if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
  387             hispgrp->pg_session == mysession)
  388                 if (entering)
  389                         pgrp->pg_jobc++;
  390                 else if (--pgrp->pg_jobc == 0)
  391                         orphanpg(pgrp);
  392 
  393         /*
  394          * Check this process' children to see whether they qualify
  395          * their process groups; if so, adjust counts for children's
  396          * process groups.
  397          */
  398         for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next)
  399                 if ((hispgrp = p->p_pgrp) != pgrp &&
  400                     hispgrp->pg_session == mysession &&
  401                     p->p_stat != SZOMB)
  402                         if (entering)
  403                                 hispgrp->pg_jobc++;
  404                         else if (--hispgrp->pg_jobc == 0)
  405                                 orphanpg(hispgrp);
  406 }
  407 
  408 /* 
  409  * A process group has become orphaned;
  410  * if there are any stopped processes in the group,
  411  * hang-up all process in that group.
  412  */
  413 static void
  414 orphanpg(pg)
  415         struct pgrp *pg;
  416 {
  417         register struct proc *p;
  418 
  419         for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
  420                 if (p->p_stat == SSTOP) {
  421                         for (p = pg->pg_members.lh_first; p != 0;
  422                             p = p->p_pglist.le_next) {
  423                                 pt_setrunnable(p);
  424                                 psignal(p, SIGHUP);
  425                                 psignal(p, SIGCONT);
  426                         }
  427                         return;
  428                 }
  429         }
  430 }
  431 
  432 #ifdef DEBUG
  433 void
  434 pgrpdump()
  435 {
  436         register struct pgrp *pgrp;
  437         register struct proc *p;
  438         register i;
  439 
  440         for (i = 0; i <= pgrphash; i++) {
  441                 if (pgrp = pgrphashtbl[i].lh_first) {
  442                         printf("\tindx %d\n", i);
  443                         for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
  444                                 printf("\tpgrp 0x%08x, pgid %d, sess %p, sesscnt %d, mem %p\n",
  445                                     pgrp, pgrp->pg_id, pgrp->pg_session,
  446                                     pgrp->pg_session->s_count,
  447                                     pgrp->pg_members.lh_first);
  448                                 for (p = pgrp->pg_members.lh_first; p != 0;
  449                                     p = p->p_pglist.le_next) {
  450                                         printf("\t\tpid %d addr 0x%08x pgrp 0x%08x\n", 
  451                                             p->p_pid, p, p->p_pgrp);
  452                                 }
  453                         }
  454                 }
  455         }
  456 }
  457 #endif /* DEBUG */
  458 
  459 int
  460 proc_is_classic(struct proc *p)
  461 {
  462     return (p->p_flag & P_CLASSIC) ? 1 : 0;
  463 }
  464 
  465 struct proc * current_proc_EXTERNAL()
  466 {
  467         return (current_proc());
  468 }

Cache object: 002a16c09a31f464f6bd3bcdcf8c4fd0


[ 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.