]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_proc.c
714f85f85fddd3b799793c1a5e9cf5974889c88d
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1982, 1986, 1989, 1991, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * @(#)kern_proc.c 8.4 (Berkeley) 1/4/94
59 * 04-Aug-97 Umesh Vaishampayan (umeshv@apple.com)
60 * Added current_proc_EXTERNAL() function for the use of kernel
63 * 05-Jun-95 Mac Gillon (mgillon) at NeXT
64 * New version based on 3.3NS and 4.4
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/kernel.h>
71 #include <sys/proc_internal.h>
74 #include <sys/file_internal.h>
75 #include <ufs/ufs/quota.h>
77 #include <sys/malloc.h>
79 #include <sys/ioctl.h>
81 #include <sys/signalvar.h>
82 #include <sys/syslog.h>
83 #include <sys/kernel_types.h>
86 * Structure associated with user cacheing.
89 LIST_ENTRY(uidinfo
) ui_hash
;
93 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
94 LIST_HEAD(uihashhead
, uidinfo
) *uihashtbl
;
95 u_long uihash
; /* size of hash table - 1 */
100 struct pidhashhead
*pidhashtbl
;
102 struct pgrphashhead
*pgrphashtbl
;
104 struct proclist allproc
;
105 struct proclist zombproc
;
106 extern struct tty cons
;
108 /* Name to give to core files */
109 __private_extern__
char corefilename
[MAXPATHLEN
+1] = {"/cores/core.%P"};
111 static void orphanpg(struct pgrp
*pg
);
114 * Initialize global process hashing structures.
121 LIST_INIT(&zombproc
);
122 pidhashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pidhash
);
123 pgrphashtbl
= hashinit(maxproc
/ 4, M_PROC
, &pgrphash
);
124 uihashtbl
= hashinit(maxproc
/ 16, M_PROC
, &uihash
);
128 * Change the count associated with number of processes
129 * a given user is using.
132 chgproccnt(uid
, diff
)
136 register struct uidinfo
*uip
;
137 register struct uihashhead
*uipp
;
140 for (uip
= uipp
->lh_first
; uip
!= 0; uip
= uip
->ui_hash
.le_next
)
141 if (uip
->ui_uid
== uid
)
144 uip
->ui_proccnt
+= diff
;
145 if (uip
->ui_proccnt
> 0)
146 return (uip
->ui_proccnt
);
147 if (uip
->ui_proccnt
< 0)
148 panic("chgproccnt: procs < 0");
149 LIST_REMOVE(uip
, ui_hash
);
150 FREE_ZONE(uip
, sizeof *uip
, M_PROC
);
156 panic("chgproccnt: lost user");
158 MALLOC_ZONE(uip
, struct uidinfo
*, sizeof(*uip
), M_PROC
, M_WAITOK
);
160 panic("chgproccnt: M_PROC zone depleted");
161 LIST_INSERT_HEAD(uipp
, uip
, ui_hash
);
163 uip
->ui_proccnt
= diff
;
168 * Is p an inferior of the current process?
172 register struct proc
*p
;
175 for (; p
!= current_proc(); p
= p
->p_pptr
)
181 * Is p an inferior of t ?
184 isinferior(struct proc
*p
, struct proc
*t
)
187 /* if p==t they are not inferior */
190 for (; p
!= t
; p
= p
->p_pptr
)
197 proc_isinferior(int pid1
, int pid2
)
202 if (((p
= pfind(pid1
)) != (struct proc
*)0 ) && ((t
= pfind(pid2
)) != (struct proc
*)0))
203 return (isinferior(p
, t
));
214 proc_rele(__unused proc_t p
)
222 return(current_proc());
226 proc_findref(int pid
)
228 boolean_t funnel_state
;
231 funnel_state
= thread_funnel_set(kernel_flock
,TRUE
);
234 if (p
!= proc_refinternal(p
, 1))
237 thread_funnel_set(kernel_flock
, funnel_state
);
242 proc_dropref(proc_t p
)
245 proc_dropinternal(p
, 0);
250 proc_refinternal(proc_t p
, int funneled
)
254 boolean_t funnel_state
= TRUE
; /* need to init just to avoid warnings and build failure */
257 funnel_state
= thread_funnel_set(kernel_flock
,TRUE
);
259 if ((p
!= PROC_NULL
) &&(p
->p_stat
!= SZOMB
) && ((p
->p_lflag
& (P_LREFDRAINWAIT
| P_LREFDRAIN
| P_LREFDEAD
)) == 0))
265 thread_funnel_set(kernel_flock
,funnel_state
);
270 proc_dropinternal(proc_t p
, int funneled
)
272 boolean_t funnel_state
= TRUE
; /* need to init just to avoid warnings and build failure */
275 funnel_state
= thread_funnel_set(kernel_flock
,TRUE
);
277 if (p
->p_internalref
> 0) {
279 if ((p
->p_internalref
== 0) && ((p
->p_lflag
& P_LREFDRAINWAIT
) == P_LREFDRAINWAIT
)) {
280 p
->p_lflag
&= ~P_LREFDRAINWAIT
;
281 wakeup(&p
->p_internalref
);
284 printf("proc_dropreg -ve ref\n");
287 thread_funnel_set(kernel_flock
,funnel_state
);
300 if (p
->p_pptr
!= (struct proc
*)0)
301 return(p
->p_pptr
->p_pid
);
308 struct proc
*p
= current_proc();
316 struct proc
*p
= current_proc();
318 return(p
->p_pptr
->p_pid
);
324 proc_name(int pid
, char * buf
, int size
)
328 if ((p
= pfind(pid
))!= (struct proc
*)0) {
329 strncpy(buf
, &p
->p_comm
[0], size
);
335 proc_selfname(char * buf
, int size
)
339 if ((p
= current_proc())!= (struct proc
*)0) {
340 strncpy(buf
, &p
->p_comm
[0], size
);
346 proc_signal(int pid
, int signum
)
350 if ((p
= pfind(pid
))!= (struct proc
*)0) {
356 proc_issignal(int pid
, sigset_t mask
)
360 if ((p
= pfind(pid
))!= (struct proc
*)0) {
361 return(proc_pendingsignals(p
, mask
));
367 proc_noremotehang(proc_t p
)
372 retval
= p
->p_flag
& P_NOREMOTEHANG
;
373 return(retval
? 1: 0);
378 proc_exiting(proc_t p
)
383 retval
= p
->p_flag
& P_WEXIT
;
384 return(retval
? 1: 0);
389 proc_forcequota(proc_t p
)
394 retval
= p
->p_flag
& P_FORCEQUOTA
;
395 return(retval
? 1: 0);
405 retval
= p
->p_flag
& P_TBE
;
406 return(retval
? 1: 0);
413 return(suser(p
->p_ucred
, NULL
));
425 proc_is64bit(proc_t p
)
427 return(IS_64BIT_PROCESS(p
));
430 /* LP64todo - figure out how to identify 64-bit processes if NULL procp */
432 IS_64BIT_PROCESS(proc_t p
)
434 if (p
&& (p
->p_flag
& P_LP64
))
442 * Locate a process by number
448 register struct proc
*p
;
453 for (p
= PIDHASH(pid
)->lh_first
; p
!= 0; p
= p
->p_hash
.le_next
)
460 * Locate a zombie by PID
462 __private_extern__
struct proc
*
466 register struct proc
*p
;
468 for (p
= zombproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
)
475 * Locate a process group by number
481 register struct pgrp
*pgrp
;
483 for (pgrp
= PGRPHASH(pgid
)->lh_first
; pgrp
!= 0; pgrp
= pgrp
->pg_hash
.le_next
)
484 if (pgrp
->pg_id
== pgid
)
491 * Move p to a new or existing process group (and session)
494 enterpgrp(p
, pgid
, mksess
)
495 register struct proc
*p
;
499 register struct pgrp
*pgrp
= pgfind(pgid
);
502 if (pgrp
!= NULL
&& mksess
) /* firewalls */
503 panic("enterpgrp: setsid into non-empty pgrp");
505 panic("enterpgrp: session leader attempted setpgrp");
508 pid_t savepid
= p
->p_pid
;
514 if (p
->p_pid
!= pgid
)
515 panic("enterpgrp: new pgrp and pid != pgid");
517 MALLOC_ZONE(pgrp
, struct pgrp
*, sizeof(struct pgrp
), M_PGRP
,
520 panic("enterpgrp: M_PGRP zone depleted");
521 if ((np
= pfind(savepid
)) == NULL
|| np
!= p
) {
522 FREE_ZONE(pgrp
, sizeof(struct pgrp
), M_PGRP
);
526 register struct session
*sess
;
531 MALLOC_ZONE(sess
, struct session
*,
532 sizeof(struct session
), M_SESSION
, M_WAITOK
);
534 panic("enterpgrp: M_SESSION zone depleted");
536 sess
->s_sid
= p
->p_pid
;
538 sess
->s_ttyvp
= NULL
;
540 bcopy(p
->p_session
->s_login
, sess
->s_login
,
541 sizeof(sess
->s_login
));
542 p
->p_flag
&= ~P_CONTROLT
;
543 pgrp
->pg_session
= sess
;
545 if (p
!= current_proc())
546 panic("enterpgrp: mksession and p != curproc");
549 pgrp
->pg_session
= p
->p_session
;
550 pgrp
->pg_session
->s_count
++;
553 LIST_INIT(&pgrp
->pg_members
);
554 LIST_INSERT_HEAD(PGRPHASH(pgid
), pgrp
, pg_hash
);
556 } else if (pgrp
== p
->p_pgrp
)
560 * Adjust eligibility of affected pgrps to participate in job control.
561 * Increment eligibility counts before decrementing, otherwise we
562 * could reach 0 spuriously during the first call.
565 fixjobc(p
, p
->p_pgrp
, 0);
567 LIST_REMOVE(p
, p_pglist
);
568 if (p
->p_pgrp
->pg_members
.lh_first
== 0)
571 LIST_INSERT_HEAD(&pgrp
->pg_members
, p
, p_pglist
);
576 * remove process from process group
580 register struct proc
*p
;
583 LIST_REMOVE(p
, p_pglist
);
584 if (p
->p_pgrp
->pg_members
.lh_first
== 0)
591 * delete a process group
595 register struct pgrp
*pgrp
;
598 int removettypgrp
= 0;
600 ttyp
= pgrp
->pg_session
->s_ttyp
;
601 if (pgrp
->pg_session
->s_ttyp
!= NULL
&&
602 pgrp
->pg_session
->s_ttyp
->t_pgrp
== pgrp
) {
603 pgrp
->pg_session
->s_ttyp
->t_pgrp
= NULL
;
606 LIST_REMOVE(pgrp
, pg_hash
);
607 if (--pgrp
->pg_session
->s_count
== 0) {
608 if (removettypgrp
&& (ttyp
== &cons
) && (ttyp
->t_session
== pgrp
->pg_session
))
610 FREE_ZONE(pgrp
->pg_session
, sizeof(struct session
), M_SESSION
);
612 FREE_ZONE(pgrp
, sizeof *pgrp
, M_PGRP
);
617 struct session
*sess
;
619 if (--sess
->s_count
== 0)
620 FREE_ZONE(sess
, sizeof (struct session
), M_SESSION
);
624 * Adjust pgrp jobc counters when specified process changes process group.
625 * We count the number of processes in each process group that "qualify"
626 * the group for terminal job control (those with a parent in a different
627 * process group of the same session). If that count reaches zero, the
628 * process group becomes orphaned. Check both the specified process'
629 * process group and that of its children.
630 * entering == 0 => p is leaving specified group.
631 * entering == 1 => p is entering specified group.
634 fixjobc(struct proc
*p
, struct pgrp
*pgrp
, int entering
)
636 register struct pgrp
*hispgrp
;
637 register struct session
*mysession
= pgrp
->pg_session
;
640 * Check p's parent to see whether p qualifies its own process
641 * group; if so, adjust count for p's process group.
643 if ((hispgrp
= p
->p_pptr
->p_pgrp
) != pgrp
&&
644 hispgrp
->pg_session
== mysession
) {
647 else if (--pgrp
->pg_jobc
== 0)
652 * Check this process' children to see whether they qualify
653 * their process groups; if so, adjust counts for children's
656 for (p
= p
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
)
657 if ((hispgrp
= p
->p_pgrp
) != pgrp
&&
658 hispgrp
->pg_session
== mysession
&&
659 p
->p_stat
!= SZOMB
) {
662 else if (--hispgrp
->pg_jobc
== 0)
668 * A process group has become orphaned;
669 * if there are any stopped processes in the group,
670 * hang-up all process in that group.
673 orphanpg(struct pgrp
*pg
)
675 register struct proc
*p
;
677 for (p
= pg
->pg_members
.lh_first
; p
!= 0; p
= p
->p_pglist
.le_next
) {
678 if (p
->p_stat
== SSTOP
) {
679 for (p
= pg
->pg_members
.lh_first
; p
!= 0;
680 p
= p
->p_pglist
.le_next
) {
691 void pgrpdump(void); /* forward declare here (called from debugger) */
700 for (i
= 0; i
<= pgrphash
; i
++) {
701 if ((pgrp
= pgrphashtbl
[i
].lh_first
) != NULL
) {
702 printf("\tindx %d\n", i
);
703 for (; pgrp
!= 0; pgrp
= pgrp
->pg_hash
.le_next
) {
704 printf("\tpgrp 0x%08x, pgid %d, sess %p, sesscnt %d, mem %p\n",
705 pgrp
, pgrp
->pg_id
, pgrp
->pg_session
,
706 pgrp
->pg_session
->s_count
,
707 pgrp
->pg_members
.lh_first
);
708 for (p
= pgrp
->pg_members
.lh_first
; p
!= 0;
709 p
= p
->p_pglist
.le_next
) {
710 printf("\t\tpid %d addr 0x%08x pgrp 0x%08x\n",
711 p
->p_pid
, p
, p
->p_pgrp
);
719 /* XXX should be __private_extern__ */
721 proc_is_classic(struct proc
*p
)
723 return (p
->p_flag
& P_CLASSIC
) ? 1 : 0;
726 /* XXX Why does this function exist? Need to kill it off... */
728 current_proc_EXTERNAL(void)
730 return (current_proc());
734 * proc_core_name(name, uid, pid)
735 * Expand the name described in corefilename, using name, uid, and pid.
736 * corefilename is a printf-like string, with three format specifiers:
737 * %N name of process ("name")
738 * %P process id (pid)
740 * For example, "%N.core" is the default; they can be disabled completely
741 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
742 * This is controlled by the sysctl variable kern.corefile (see above).
744 __private_extern__
char *
745 proc_core_name(const char *name
, uid_t uid
, pid_t pid
)
747 const char *format
, *appendstr
;
749 char id_buf
[11]; /* Buffer for pid/uid -- max 4B */
752 format
= corefilename
;
753 MALLOC(temp
, char *, MAXPATHLEN
, M_TEMP
, M_NOWAIT
| M_ZERO
);
756 for (i
= 0, n
= 0; n
< MAXPATHLEN
&& format
[i
]; i
++) {
758 case '%': /* Format character */
764 case 'N': /* process name */
767 case 'P': /* process id */
768 sprintf(id_buf
, "%u", pid
);
771 case 'U': /* user id */
772 sprintf(id_buf
, "%u", uid
);
778 "Unknown format character %c in `%s'\n",
781 l
= strlen(appendstr
);
782 if ((n
+ l
) >= MAXPATHLEN
)
784 bcopy(appendstr
, temp
+ n
, l
);
788 temp
[n
++] = format
[i
];
791 if (format
[i
] != '\0')
795 log(LOG_ERR
, "pid %ld (%s), uid (%lu): corename is too long\n",
796 (long)pid
, name
, (u_long
)uid
);