1/*	$OpenBSD: kern_proc.c,v 1.98 2024/05/20 10:32:20 claudio Exp $	*/
2/*	$NetBSD: kern_proc.c,v 1.14 1996/02/09 18:59:41 christos Exp $	*/
3
4/*
5 * Copyright (c) 1982, 1986, 1989, 1991, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	@(#)kern_proc.c	8.4 (Berkeley) 1/4/94
33 */
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/proc.h>
38#include <sys/wait.h>
39#include <sys/rwlock.h>
40#include <sys/malloc.h>
41#include <sys/tty.h>
42#include <sys/signalvar.h>
43#include <sys/pool.h>
44#include <sys/vnode.h>
45
46/*
47 *  Locks used to protect struct members in this file:
48 *	I	immutable after creation
49 *	U	uidinfolk
50 */
51
52struct rwlock uidinfolk;
53#define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
54LIST_HEAD(uihashhead, uidinfo) *uihashtbl;	/* [U] */
55u_long uihash;				/* [I] size of hash table - 1 */
56
57/*
58 * Other process lists
59 */
60struct tidhashhead *tidhashtbl;
61u_long tidhash;
62struct pidhashhead *pidhashtbl;
63u_long pidhash;
64struct pgrphashhead *pgrphashtbl;
65u_long pgrphash;
66struct processlist allprocess;
67struct processlist zombprocess;
68struct proclist allproc;
69
70struct pool proc_pool;
71struct pool process_pool;
72struct pool rusage_pool;
73struct pool ucred_pool;
74struct pool pgrp_pool;
75struct pool session_pool;
76
77void	pgdelete(struct pgrp *);
78void	fixjobc(struct process *, struct pgrp *, int);
79
80static void orphanpg(struct pgrp *);
81#ifdef DEBUG
82void pgrpdump(void);
83#endif
84
85/*
86 * Initialize global process hashing structures.
87 */
88void
89procinit(void)
90{
91	LIST_INIT(&allprocess);
92	LIST_INIT(&zombprocess);
93	LIST_INIT(&allproc);
94
95	rw_init(&uidinfolk, "uidinfo");
96
97	tidhashtbl = hashinit(maxthread / 4, M_PROC, M_NOWAIT, &tidhash);
98	pidhashtbl = hashinit(maxprocess / 4, M_PROC, M_NOWAIT, &pidhash);
99	pgrphashtbl = hashinit(maxprocess / 4, M_PROC, M_NOWAIT, &pgrphash);
100	uihashtbl = hashinit(maxprocess / 16, M_PROC, M_NOWAIT, &uihash);
101	if (!tidhashtbl || !pidhashtbl || !pgrphashtbl || !uihashtbl)
102		panic("procinit: malloc");
103
104	pool_init(&proc_pool, sizeof(struct proc), 0, IPL_NONE,
105	    PR_WAITOK, "procpl", NULL);
106	pool_init(&process_pool, sizeof(struct process), 0, IPL_NONE,
107	    PR_WAITOK, "processpl", NULL);
108	pool_init(&rusage_pool, sizeof(struct rusage), 0, IPL_NONE,
109	    PR_WAITOK, "zombiepl", NULL);
110	pool_init(&ucred_pool, sizeof(struct ucred), 0, IPL_MPFLOOR,
111	    0, "ucredpl", NULL);
112	pool_init(&pgrp_pool, sizeof(struct pgrp), 0, IPL_NONE,
113	    PR_WAITOK, "pgrppl", NULL);
114	pool_init(&session_pool, sizeof(struct session), 0, IPL_NONE,
115	    PR_WAITOK, "sessionpl", NULL);
116}
117
118/*
119 * This returns with `uidinfolk' held: caller must call uid_release()
120 * after making whatever change they needed.
121 */
122struct uidinfo *
123uid_find(uid_t uid)
124{
125	struct uidinfo *uip, *nuip;
126	struct uihashhead *uipp;
127
128	uipp = UIHASH(uid);
129	rw_enter_write(&uidinfolk);
130	LIST_FOREACH(uip, uipp, ui_hash)
131		if (uip->ui_uid == uid)
132			break;
133	if (uip)
134		return (uip);
135	rw_exit_write(&uidinfolk);
136	nuip = malloc(sizeof(*nuip), M_PROC, M_WAITOK|M_ZERO);
137	rw_enter_write(&uidinfolk);
138	LIST_FOREACH(uip, uipp, ui_hash)
139		if (uip->ui_uid == uid)
140			break;
141	if (uip) {
142		free(nuip, M_PROC, sizeof(*nuip));
143		return (uip);
144	}
145	nuip->ui_uid = uid;
146	LIST_INSERT_HEAD(uipp, nuip, ui_hash);
147
148	return (nuip);
149}
150
151void
152uid_release(struct uidinfo *uip)
153{
154	rw_exit_write(&uidinfolk);
155}
156
157/*
158 * Change the count associated with number of threads
159 * a given user is using.
160 */
161int
162chgproccnt(uid_t uid, int diff)
163{
164	struct uidinfo *uip;
165	long count;
166
167	uip = uid_find(uid);
168	count = (uip->ui_proccnt += diff);
169	uid_release(uip);
170	if (count < 0)
171		panic("chgproccnt: procs < 0");
172	return count;
173}
174
175/*
176 * Is pr an inferior of parent?
177 */
178int
179inferior(struct process *pr, struct process *parent)
180{
181
182	for (; pr != parent; pr = pr->ps_pptr)
183		if (pr->ps_pid == 0 || pr->ps_pid == 1)
184			return (0);
185	return (1);
186}
187
188/*
189 * Locate a proc (thread) by number
190 */
191struct proc *
192tfind(pid_t tid)
193{
194	struct proc *p;
195
196	LIST_FOREACH(p, TIDHASH(tid), p_hash)
197		if (p->p_tid == tid)
198			return (p);
199	return (NULL);
200}
201
202/*
203 * Locate a thread by userspace id, from a given process.
204 */
205struct proc *
206tfind_user(pid_t tid, struct process *pr)
207{
208	struct proc *p;
209
210	if (tid < THREAD_PID_OFFSET)
211		return NULL;
212	p = tfind(tid - THREAD_PID_OFFSET);
213
214	/* verify we found a thread in the correct process */
215	if (p != NULL && p->p_p != pr)
216		p = NULL;
217	return p;
218}
219
220/*
221 * Locate a process by number
222 */
223struct process *
224prfind(pid_t pid)
225{
226	struct process *pr;
227
228	LIST_FOREACH(pr, PIDHASH(pid), ps_hash)
229		if (pr->ps_pid == pid)
230			return (pr);
231	return (NULL);
232}
233
234/*
235 * Locate a process group by number
236 */
237struct pgrp *
238pgfind(pid_t pgid)
239{
240	struct pgrp *pgrp;
241
242	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash)
243		if (pgrp->pg_id == pgid)
244			return (pgrp);
245	return (NULL);
246}
247
248/*
249 * Locate a zombie process
250 */
251struct process *
252zombiefind(pid_t pid)
253{
254	struct process *pr;
255
256	LIST_FOREACH(pr, &zombprocess, ps_list)
257		if (pr->ps_pid == pid)
258			return (pr);
259	return (NULL);
260}
261
262/*
263 * Move process to a new process group.  If a session is provided
264 * then it's a new session to contain this process group; otherwise
265 * the process is staying within its existing session.
266 */
267void
268enternewpgrp(struct process *pr, struct pgrp *pgrp, struct session *newsess)
269{
270#ifdef DIAGNOSTIC
271	if (SESS_LEADER(pr))
272		panic("%s: session leader attempted setpgrp", __func__);
273#endif
274
275	if (newsess != NULL) {
276		/*
277		 * New session.  Initialize it completely
278		 */
279		timeout_set(&newsess->s_verauthto, zapverauth, newsess);
280		newsess->s_leader = pr;
281		newsess->s_count = 1;
282		newsess->s_ttyvp = NULL;
283		newsess->s_ttyp = NULL;
284		memcpy(newsess->s_login, pr->ps_session->s_login,
285		    sizeof(newsess->s_login));
286		atomic_clearbits_int(&pr->ps_flags, PS_CONTROLT);
287		pgrp->pg_session = newsess;
288#ifdef DIAGNOSTIC
289		if (pr != curproc->p_p)
290			panic("%s: mksession but not curproc", __func__);
291#endif
292	} else {
293		pgrp->pg_session = pr->ps_session;
294		pgrp->pg_session->s_count++;
295	}
296	pgrp->pg_id = pr->ps_pid;
297	LIST_INIT(&pgrp->pg_members);
298	LIST_INIT(&pgrp->pg_sigiolst);
299	LIST_INSERT_HEAD(PGRPHASH(pr->ps_pid), pgrp, pg_hash);
300	pgrp->pg_jobc = 0;
301
302	enterthispgrp(pr, pgrp);
303}
304
305/*
306 * move process to an existing process group
307 */
308void
309enterthispgrp(struct process *pr, struct pgrp *pgrp)
310{
311	struct pgrp *savepgrp = pr->ps_pgrp;
312
313	/*
314	 * Adjust eligibility of affected pgrps to participate in job control.
315	 * Increment eligibility counts before decrementing, otherwise we
316	 * could reach 0 spuriously during the first call.
317	 */
318	fixjobc(pr, pgrp, 1);
319	fixjobc(pr, savepgrp, 0);
320
321	LIST_REMOVE(pr, ps_pglist);
322	pr->ps_pgrp = pgrp;
323	LIST_INSERT_HEAD(&pgrp->pg_members, pr, ps_pglist);
324	if (LIST_EMPTY(&savepgrp->pg_members))
325		pgdelete(savepgrp);
326}
327
328/*
329 * remove process from process group
330 */
331void
332leavepgrp(struct process *pr)
333{
334
335	if (pr->ps_session->s_verauthppid == pr->ps_pid)
336		zapverauth(pr->ps_session);
337	LIST_REMOVE(pr, ps_pglist);
338	if (LIST_EMPTY(&pr->ps_pgrp->pg_members))
339		pgdelete(pr->ps_pgrp);
340	pr->ps_pgrp = NULL;
341}
342
343/*
344 * delete a process group
345 */
346void
347pgdelete(struct pgrp *pgrp)
348{
349	sigio_freelist(&pgrp->pg_sigiolst);
350
351	if (pgrp->pg_session->s_ttyp != NULL &&
352	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
353		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
354	LIST_REMOVE(pgrp, pg_hash);
355	SESSRELE(pgrp->pg_session);
356	pool_put(&pgrp_pool, pgrp);
357}
358
359void
360zapverauth(void *v)
361{
362	struct session *sess = v;
363	sess->s_verauthuid = 0;
364	sess->s_verauthppid = 0;
365}
366
367/*
368 * Adjust pgrp jobc counters when specified process changes process group.
369 * We count the number of processes in each process group that "qualify"
370 * the group for terminal job control (those with a parent in a different
371 * process group of the same session).  If that count reaches zero, the
372 * process group becomes orphaned.  Check both the specified process'
373 * process group and that of its children.
374 * entering == 0 => pr is leaving specified group.
375 * entering == 1 => pr is entering specified group.
376 * XXX need proctree lock
377 */
378void
379fixjobc(struct process *pr, struct pgrp *pgrp, int entering)
380{
381	struct pgrp *hispgrp;
382	struct session *mysession = pgrp->pg_session;
383
384	/*
385	 * Check pr's parent to see whether pr qualifies its own process
386	 * group; if so, adjust count for pr's process group.
387	 */
388	if ((hispgrp = pr->ps_pptr->ps_pgrp) != pgrp &&
389	    hispgrp->pg_session == mysession) {
390		if (entering)
391			pgrp->pg_jobc++;
392		else if (--pgrp->pg_jobc == 0)
393			orphanpg(pgrp);
394	}
395
396	/*
397	 * Check this process' children to see whether they qualify
398	 * their process groups; if so, adjust counts for children's
399	 * process groups.
400	 */
401	LIST_FOREACH(pr, &pr->ps_children, ps_sibling)
402		if ((hispgrp = pr->ps_pgrp) != pgrp &&
403		    hispgrp->pg_session == mysession &&
404		    (pr->ps_flags & PS_ZOMBIE) == 0) {
405			if (entering)
406				hispgrp->pg_jobc++;
407			else if (--hispgrp->pg_jobc == 0)
408				orphanpg(hispgrp);
409		}
410}
411
412void
413killjobc(struct process *pr)
414{
415	if (SESS_LEADER(pr)) {
416		struct session *sp = pr->ps_session;
417
418		if (sp->s_ttyvp) {
419			struct vnode *ovp;
420
421			/*
422			 * Controlling process.
423			 * Signal foreground pgrp,
424			 * drain controlling terminal
425			 * and revoke access to controlling terminal.
426			 */
427			if (sp->s_ttyp->t_session == sp) {
428				if (sp->s_ttyp->t_pgrp)
429					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
430				ttywait(sp->s_ttyp);
431				/*
432				 * The tty could have been revoked
433				 * if we blocked.
434				 */
435				if (sp->s_ttyvp)
436					VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
437			}
438			ovp = sp->s_ttyvp;
439			sp->s_ttyvp = NULL;
440			if (ovp)
441				vrele(ovp);
442			/*
443			 * s_ttyp is not zero'd; we use this to
444			 * indicate that the session once had a
445			 * controlling terminal.  (for logging and
446			 * informational purposes)
447			 */
448		}
449		sp->s_leader = NULL;
450	}
451	fixjobc(pr, pr->ps_pgrp, 0);
452}
453
454/*
455 * A process group has become orphaned;
456 * if there are any stopped processes in the group,
457 * hang-up all process in that group.
458 */
459static void
460orphanpg(struct pgrp *pg)
461{
462	struct process *pr;
463
464	LIST_FOREACH(pr, &pg->pg_members, ps_pglist) {
465		if (pr->ps_mainproc->p_stat == SSTOP) {
466			LIST_FOREACH(pr, &pg->pg_members, ps_pglist) {
467				prsignal(pr, SIGHUP);
468				prsignal(pr, SIGCONT);
469			}
470			return;
471		}
472	}
473}
474
475#ifdef DDB
476void
477proc_printit(struct proc *p, const char *modif,
478    int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2))))
479{
480	static const char *const pstat[] = {
481		"idle", "run", "sleep", "stop", "zombie", "dead", "onproc"
482	};
483	char pstbuf[5];
484	const char *pst = pstbuf;
485
486
487	if (p->p_stat < 1 || p->p_stat > sizeof(pstat) / sizeof(pstat[0]))
488		snprintf(pstbuf, sizeof(pstbuf), "%d", p->p_stat);
489	else
490		pst = pstat[(int)p->p_stat - 1];
491
492	(*pr)("PROC (%s) tid=%d pid=%d tcnt=%d stat=%s\n", p->p_p->ps_comm,
493	    p->p_tid, p->p_p->ps_pid, p->p_p->ps_threadcnt, pst);
494	(*pr)("    flags process=%b proc=%b\n",
495	    p->p_p->ps_flags, PS_BITS, p->p_flag, P_BITS);
496	(*pr)("    runpri=%u, usrpri=%u, slppri=%u, nice=%d\n",
497	    p->p_runpri, p->p_usrpri, p->p_slppri, p->p_p->ps_nice);
498	(*pr)("    wchan=%p, wmesg=%s, ps_single=%p scnt=%d ecnt=%d\n",
499	    p->p_wchan, (p->p_wchan && p->p_wmesg) ?  p->p_wmesg : "",
500	    p->p_p->ps_single, p->p_p->ps_singlecnt, p->p_p->ps_exitcnt);
501	(*pr)("    forw=%p, list=%p,%p\n",
502	    TAILQ_NEXT(p, p_runq), p->p_list.le_next, p->p_list.le_prev);
503	(*pr)("    process=%p user=%p, vmspace=%p\n",
504	    p->p_p, p->p_addr, p->p_vmspace);
505	(*pr)("    estcpu=%u, cpticks=%d, pctcpu=%u.%u, "
506	    "user=%u, sys=%u, intr=%u\n",
507	    p->p_estcpu, p->p_cpticks, p->p_pctcpu / 100, p->p_pctcpu % 100,
508	    p->p_uticks, p->p_sticks, p->p_iticks);
509}
510#include <machine/db_machdep.h>
511
512#include <ddb/db_output.h>
513
514void
515db_kill_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
516{
517	struct process *pr;
518	struct proc *p;
519
520	pr = prfind(addr);
521	if (pr == NULL) {
522		db_printf("%ld: No such process", addr);
523		return;
524	}
525
526	p = TAILQ_FIRST(&pr->ps_threads);
527
528	/* Send uncatchable SIGABRT for coredump */
529	sigabort(p);
530}
531
532void
533db_show_all_procs(db_expr_t addr, int haddr, db_expr_t count, char *modif)
534{
535	char *mode;
536	int skipzomb = 0;
537	int has_kernel_lock = 0;
538	struct proc *p;
539	struct process *pr, *ppr;
540
541	if (modif[0] == 0)
542		modif[0] = 'n';			/* default == normal mode */
543
544	mode = "mawno";
545	while (*mode && *mode != modif[0])
546		mode++;
547	if (*mode == 0 || *mode == 'm') {
548		db_printf("usage: show all procs [/a] [/n] [/w]\n");
549		db_printf("\t/a == show process address info\n");
550		db_printf("\t/n == show normal process info [default]\n");
551		db_printf("\t/w == show process pgrp/wait info\n");
552		db_printf("\t/o == show normal info for non-idle SONPROC\n");
553		return;
554	}
555
556	pr = LIST_FIRST(&allprocess);
557
558	switch (*mode) {
559
560	case 'a':
561		db_printf("    TID  %-9s  %18s  %18s  %18s\n",
562		    "COMMAND", "STRUCT PROC *", "UAREA *", "VMSPACE/VM_MAP");
563		break;
564	case 'n':
565		db_printf("   PID  %6s  %5s  %5s  S  %10s  %-12s  %-15s\n",
566		    "TID", "PPID", "UID", "FLAGS", "WAIT", "COMMAND");
567		break;
568	case 'w':
569		db_printf("    TID  %-15s  %-5s  %18s  %s\n",
570		    "COMMAND", "PGRP", "WAIT-CHANNEL", "WAIT-MSG");
571		break;
572	case 'o':
573		skipzomb = 1;
574		db_printf("    TID  %5s  %5s  %10s %10s  %3s  %-30s\n",
575		    "PID", "UID", "PRFLAGS", "PFLAGS", "CPU", "COMMAND");
576		break;
577	}
578
579	while (pr != NULL) {
580		ppr = pr->ps_pptr;
581
582		TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) {
583#ifdef MULTIPROCESSOR
584			if (__mp_lock_held(&kernel_lock, p->p_cpu))
585				has_kernel_lock = 1;
586			else
587				has_kernel_lock = 0;
588#endif
589			if (p->p_stat) {
590				if (*mode == 'o') {
591					if (p->p_stat != SONPROC)
592						continue;
593					if (p->p_cpu != NULL && p->p_cpu->
594					    ci_schedstate.spc_idleproc == p)
595						continue;
596				}
597
598				if (*mode == 'n') {
599					db_printf("%c%5d  ", (p == curproc ?
600					    '*' : ' '), pr->ps_pid);
601				} else {
602					db_printf("%c%6d  ", (p == curproc ?
603					    '*' : ' '), p->p_tid);
604				}
605
606				switch (*mode) {
607
608				case 'a':
609					db_printf("%-9.9s  %18p  %18p  %18p\n",
610					    pr->ps_comm, p, p->p_addr, p->p_vmspace);
611					break;
612
613				case 'n':
614					db_printf("%6d  %5d  %5d  %d  %#10x  "
615					    "%-12.12s  %-15s\n",
616					    p->p_tid, ppr ? ppr->ps_pid : -1,
617					    pr->ps_ucred->cr_ruid, p->p_stat,
618					    p->p_flag | pr->ps_flags,
619					    (p->p_wchan && p->p_wmesg) ?
620						p->p_wmesg : "", pr->ps_comm);
621					break;
622
623				case 'w':
624					db_printf("%-15s  %-5d  %18p  %s\n",
625					    pr->ps_comm, (pr->ps_pgrp ?
626						pr->ps_pgrp->pg_id : -1),
627					    p->p_wchan,
628					    (p->p_wchan && p->p_wmesg) ?
629						p->p_wmesg : "");
630					break;
631
632				case 'o':
633					db_printf("%5d  %5d  %#10x %#10x  %3d"
634					    "%c %-31s\n",
635					    pr->ps_pid, pr->ps_ucred->cr_ruid,
636					    pr->ps_flags, p->p_flag,
637					    CPU_INFO_UNIT(p->p_cpu),
638					    has_kernel_lock ? 'K' : ' ',
639					    pr->ps_comm);
640					break;
641
642				}
643			}
644		}
645		pr = LIST_NEXT(pr, ps_list);
646		if (pr == NULL && skipzomb == 0) {
647			skipzomb = 1;
648			pr = LIST_FIRST(&zombprocess);
649		}
650	}
651}
652#endif
653
654#ifdef DEBUG
655void
656pgrpdump(void)
657{
658	struct pgrp *pgrp;
659	struct process *pr;
660	int i;
661
662	for (i = 0; i <= pgrphash; i++) {
663		if (!LIST_EMPTY(&pgrphashtbl[i])) {
664			printf("\tindx %d\n", i);
665			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
666				printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
667				    pgrp, pgrp->pg_id, pgrp->pg_session,
668				    pgrp->pg_session->s_count,
669				    LIST_FIRST(&pgrp->pg_members));
670				LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) {
671					printf("\t\tpid %d addr %p pgrp %p\n",
672					    pr->ps_pid, pr, pr->ps_pgrp);
673				}
674			}
675		}
676	}
677}
678#endif /* DEBUG */
679