xref: /freebsd/sys/fs/procfs/procfs_status.c (revision 09c00166e46fe394db30efb6fc982761b2fb7b82)
1d167cf6fSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1993 Jan-Simon Pendry
3df8bae1dSRodney W. Grimes  * Copyright (c) 1993
4df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
7df8bae1dSRodney W. Grimes  * Jan-Simon Pendry.
8df8bae1dSRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
10df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
11df8bae1dSRodney W. Grimes  * are met:
12df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
14df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
16df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33996c772fSJohn Dyson  *	@(#)procfs_status.c	8.4 (Berkeley) 6/15/94
34df8bae1dSRodney W. Grimes  *
35996c772fSJohn Dyson  * From:
36a21759a1SBruce Evans  *	$Id: procfs_status.c,v 3.1 1993/12/15 09:40:17 jsp Exp $
37c3aac50fSPeter Wemm  * $FreeBSD$
38df8bae1dSRodney W. Grimes  */
39df8bae1dSRodney W. Grimes 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
4187ccef7bSDag-Erling Smørgrav #include <sys/kernel.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43fb919e4dSMark Murray #include <sys/exec.h>
44fb919e4dSMark Murray #include <sys/lock.h>
45fb919e4dSMark Murray #include <sys/mutex.h>
4601137630SRobert Watson #include <sys/jail.h>
4701137630SRobert Watson #include <sys/malloc.h>
48f591779bSSeigo Tanimura #include <sys/mutex.h>
49f591779bSSeigo Tanimura #include <sys/sx.h>
50fb919e4dSMark Murray #include <sys/proc.h>
51df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
523a669c52SDag-Erling Smørgrav #include <sys/sbuf.h>
5305ba50f5SJake Burkholder #include <sys/sysent.h>
54fb919e4dSMark Murray #include <sys/tty.h>
55df8bae1dSRodney W. Grimes 
5663a99273SMarcel Moolenaar #include <vm/vm.h>
5763a99273SMarcel Moolenaar #include <vm/pmap.h>
5863a99273SMarcel Moolenaar #include <vm/vm_param.h>
59fb919e4dSMark Murray 
603a669c52SDag-Erling Smørgrav #include <fs/pseudofs/pseudofs.h>
6199d300a1SRuslan Ermilov #include <fs/procfs/procfs.h>
6263a99273SMarcel Moolenaar 
63df8bae1dSRodney W. Grimes int
643a669c52SDag-Erling Smørgrav procfs_doprocstatus(PFS_FILL_ARGS)
65df8bae1dSRodney W. Grimes {
66df8bae1dSRodney W. Grimes 	struct session *sess;
67a21759a1SBruce Evans 	struct thread *tdfirst;
68df8bae1dSRodney W. Grimes 	struct tty *tp;
69df8bae1dSRodney W. Grimes 	struct ucred *cr;
70f3640361SJohn Baldwin 	const char *wmesg;
713a669c52SDag-Erling Smørgrav 	char *pc;
72df8bae1dSRodney W. Grimes 	char *sep;
73df8bae1dSRodney W. Grimes 	int pid, ppid, pgid, sid;
74df8bae1dSRodney W. Grimes 	int i;
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes 	pid = p->p_pid;
7730ac5d0fSJohn Baldwin 	PROC_LOCK(p);
78e0f9d286SPeter Wemm 	ppid = p->p_pptr ? p->p_pptr->p_pid : 0;
79df8bae1dSRodney W. Grimes 	pgid = p->p_pgrp->pg_id;
80df8bae1dSRodney W. Grimes 	sess = p->p_pgrp->pg_session;
81f591779bSSeigo Tanimura 	SESS_LOCK(sess);
82df8bae1dSRodney W. Grimes 	sid = sess->s_leader ? sess->s_leader->p_pid : 0;
83df8bae1dSRodney W. Grimes 
8446d7d4a3SPoul-Henning Kamp /* comm pid ppid pgid sid tty ctty,sldr start ut st wmsg
851d08058fSWolfram Schneider 				euid ruid rgid,egid,groups[1 .. NGROUPS]
861d08058fSWolfram Schneider */
87b8c8516aSEivind Eklund 
883273a63eSMike Barcroft 	pc = p->p_comm;
893273a63eSMike Barcroft 	do {
903273a63eSMike Barcroft 		if (*pc < 33 || *pc > 126 || *pc == '\\')
913a669c52SDag-Erling Smørgrav 			sbuf_printf(sb, "\\%03o", *pc);
923273a63eSMike Barcroft 		else
933a669c52SDag-Erling Smørgrav 			sbuf_putc(sb, *pc);
943a669c52SDag-Erling Smørgrav 	} while (*++pc);
953a669c52SDag-Erling Smørgrav 	sbuf_printf(sb, " %d %d %d %d ", pid, ppid, pgid, sid);
96df8bae1dSRodney W. Grimes 	if ((p->p_flag & P_CONTROLT) && (tp = sess->s_ttyp))
9746d7d4a3SPoul-Henning Kamp 		sbuf_printf(sb, "%s ", devtoname(tp->t_dev));
98df8bae1dSRodney W. Grimes 	else
9946d7d4a3SPoul-Henning Kamp 		sbuf_printf(sb, "- ");
100df8bae1dSRodney W. Grimes 
101df8bae1dSRodney W. Grimes 	sep = "";
102df8bae1dSRodney W. Grimes 	if (sess->s_ttyvp) {
1033a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "%sctty", sep);
104df8bae1dSRodney W. Grimes 		sep = ",";
105df8bae1dSRodney W. Grimes 	}
106df8bae1dSRodney W. Grimes 	if (SESS_LEADER(p)) {
1073a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "%ssldr", sep);
108df8bae1dSRodney W. Grimes 		sep = ",";
109df8bae1dSRodney W. Grimes 	}
110f591779bSSeigo Tanimura 	SESS_UNLOCK(sess);
111b8c8516aSEivind Eklund 	if (*sep != ',') {
1123a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "noflags");
113b8c8516aSEivind Eklund 	}
114df8bae1dSRodney W. Grimes 
1159ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
1160e2a4d3aSDavid Xu 	if (p->p_flag & P_SA)
117f3640361SJohn Baldwin 		wmesg = "-kse- ";
118f3640361SJohn Baldwin 	else {
119f3640361SJohn Baldwin 		tdfirst = FIRST_THREAD_IN_PROC(p);
120f3640361SJohn Baldwin 		if (tdfirst->td_wchan != NULL) {
121f3640361SJohn Baldwin 			KASSERT(tdfirst->td_wmesg != NULL,
122f3640361SJohn Baldwin 			    ("wchan %p has no wmesg", tdfirst->td_wchan));
123f3640361SJohn Baldwin 			wmesg = tdfirst->td_wmesg;
124f3640361SJohn Baldwin 		} else
125f3640361SJohn Baldwin 			wmesg = "nochan";
126f3640361SJohn Baldwin 	}
12778c85e8dSJohn Baldwin 	mtx_unlock_spin(&sched_lock);
128f3640361SJohn Baldwin 
129b9393356SJohn Baldwin 	if (p->p_sflag & PS_INMEM) {
13087ccef7bSDag-Erling Smørgrav 		struct timeval start, ut, st;
131df8bae1dSRodney W. Grimes 
13278c85e8dSJohn Baldwin 		calcru(p, &ut, &st);
13387ccef7bSDag-Erling Smørgrav 		start = p->p_stats->p_start;
13487ccef7bSDag-Erling Smørgrav 		timevaladd(&start, &boottime);
13509c00166STom Rhodes 		sbuf_printf(sb, " %jd,%ld %jd,%ld %jd,%ld",
13609c00166STom Rhodes 		    (intmax_t)start.tv_sec, start.tv_usec,
13709c00166STom Rhodes 		    (intmax_t)ut.tv_sec, ut.tv_usec,
13809c00166STom Rhodes 		    (intmax_t)st.tv_sec, st.tv_usec);
139b9393356SJohn Baldwin 	} else {
1403a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, " -1,-1 -1,-1 -1,-1");
141b9393356SJohn Baldwin 	}
142df8bae1dSRodney W. Grimes 
143f3640361SJohn Baldwin 	sbuf_printf(sb, " %s", wmesg);
144df8bae1dSRodney W. Grimes 
145df8bae1dSRodney W. Grimes 	cr = p->p_ucred;
146df8bae1dSRodney W. Grimes 
1473a669c52SDag-Erling Smørgrav 	sbuf_printf(sb, " %lu %lu %lu",
148ac1e407bSBruce Evans 		(u_long)cr->cr_uid,
149b1fc0ec1SRobert Watson 		(u_long)cr->cr_ruid,
150b1fc0ec1SRobert Watson 		(u_long)cr->cr_rgid);
1511d08058fSWolfram Schneider 
152b1fc0ec1SRobert Watson 	/* egid (cr->cr_svgid) is equal to cr_ngroups[0]
1531d08058fSWolfram Schneider 	   see also getegid(2) in /sys/kern/kern_prot.c */
1541d08058fSWolfram Schneider 
155b8c8516aSEivind Eklund 	for (i = 0; i < cr->cr_ngroups; i++) {
1563a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, ",%lu", (u_long)cr->cr_groups[i]);
157b8c8516aSEivind Eklund 	}
15875c13541SPoul-Henning Kamp 
15901137630SRobert Watson 	if (jailed(p->p_ucred)) {
16001137630SRobert Watson 		mtx_lock(&p->p_ucred->cr_prison->pr_mtx);
1613a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, " %s", p->p_ucred->cr_prison->pr_host);
16201137630SRobert Watson 		mtx_unlock(&p->p_ucred->cr_prison->pr_mtx);
16301137630SRobert Watson 	} else {
1643a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, " -");
16501137630SRobert Watson 	}
166590ae816SJohn Baldwin 	PROC_UNLOCK(p);
1673a669c52SDag-Erling Smørgrav 	sbuf_printf(sb, "\n");
168df8bae1dSRodney W. Grimes 
1693a669c52SDag-Erling Smørgrav 	return (0);
170df8bae1dSRodney W. Grimes }
17175ba7757SPeter Wemm 
17275ba7757SPeter Wemm int
1733a669c52SDag-Erling Smørgrav procfs_doproccmdline(PFS_FILL_ARGS)
17475ba7757SPeter Wemm {
17563a99273SMarcel Moolenaar 	struct ps_strings pstr;
176691b3b0dSColin Percival 	char **ps_argvstr;
1773a669c52SDag-Erling Smørgrav 	int error, i;
17875ba7757SPeter Wemm 
17975ba7757SPeter Wemm 	/*
1806153cb20SPoul-Henning Kamp 	 * If we are using the ps/cmdline caching, use that.  Otherwise
1816153cb20SPoul-Henning Kamp 	 * revert back to the old way which only implements full cmdline
1826153cb20SPoul-Henning Kamp 	 * for the currept process and just p->p_comm for all other
1836153cb20SPoul-Henning Kamp 	 * processes.
18463a99273SMarcel Moolenaar 	 * Note that if the argv is no longer available, we deliberately
18563a99273SMarcel Moolenaar 	 * don't fall back on p->p_comm or return an error: the authentic
18663a99273SMarcel Moolenaar 	 * Linux behaviour is to return zero-length in this case.
18775ba7757SPeter Wemm 	 */
18863a99273SMarcel Moolenaar 
189e9b192b7SAlfred Perlstein 	PROC_LOCK(p);
190c5b7c33bSPawel Jakub Dawidek 	if (p->p_args && p_cansee(td, p) == 0) {
1913a669c52SDag-Erling Smørgrav 		sbuf_bcpy(sb, p->p_args->ar_args, p->p_args->ar_length);
192e9b192b7SAlfred Perlstein 		PROC_UNLOCK(p);
193590ae816SJohn Baldwin 		return (0);
194590ae816SJohn Baldwin 	}
195590ae816SJohn Baldwin 	PROC_UNLOCK(p);
196590ae816SJohn Baldwin 	if (p != td->td_proc) {
1973a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "%.*s", MAXCOMLEN, p->p_comm);
1986153cb20SPoul-Henning Kamp 	} else {
19905ba50f5SJake Burkholder 		error = copyin((void *)p->p_sysent->sv_psstrings, &pstr,
20005ba50f5SJake Burkholder 		    sizeof(pstr));
2013a669c52SDag-Erling Smørgrav 		if (error)
20263a99273SMarcel Moolenaar 			return (error);
203691b3b0dSColin Percival 		if (pstr.ps_nargvstr > ARG_MAX)
204691b3b0dSColin Percival 			return (E2BIG);
205691b3b0dSColin Percival 		ps_argvstr = malloc(pstr.ps_nargvstr * sizeof(char *),
206691b3b0dSColin Percival 		    M_TEMP, M_WAITOK);
207691b3b0dSColin Percival 		error = copyin((void *)pstr.ps_argvstr, ps_argvstr,
208691b3b0dSColin Percival 		    pstr.ps_nargvstr * sizeof(char *));
209691b3b0dSColin Percival 		if (error) {
210691b3b0dSColin Percival 			free(ps_argvstr, M_TEMP);
211691b3b0dSColin Percival 			return (error);
212691b3b0dSColin Percival 		}
2133a669c52SDag-Erling Smørgrav 		for (i = 0; i < pstr.ps_nargvstr; i++) {
214691b3b0dSColin Percival 			sbuf_copyin(sb, ps_argvstr[i], 0);
2153a669c52SDag-Erling Smørgrav 			sbuf_printf(sb, "%c", '\0');
2166153cb20SPoul-Henning Kamp 		}
217691b3b0dSColin Percival 		free(ps_argvstr, M_TEMP);
21863a99273SMarcel Moolenaar 	}
21975ba7757SPeter Wemm 
2203a669c52SDag-Erling Smørgrav 	return (0);
22175ba7757SPeter Wemm }
222