xref: /freebsd/sys/fs/procfs/procfs_status.c (revision f591779bb575703cbb6674d324afb4bd212b8cbb)
1df8bae1dSRodney W. Grimes /*
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  * 3. All advertising materials mentioning features or use of this software
18df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
19df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
20df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
21df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  *
37996c772fSJohn Dyson  *	@(#)procfs_status.c	8.4 (Berkeley) 6/15/94
38df8bae1dSRodney W. Grimes  *
39996c772fSJohn Dyson  * From:
40a21759a1SBruce Evans  *	$Id: procfs_status.c,v 3.1 1993/12/15 09:40:17 jsp Exp $
41c3aac50fSPeter Wemm  * $FreeBSD$
42df8bae1dSRodney W. Grimes  */
43df8bae1dSRodney W. Grimes 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46fb919e4dSMark Murray #include <sys/exec.h>
47fb919e4dSMark Murray #include <sys/lock.h>
48fb919e4dSMark Murray #include <sys/mutex.h>
4901137630SRobert Watson #include <sys/jail.h>
5001137630SRobert Watson #include <sys/malloc.h>
51f591779bSSeigo Tanimura #include <sys/mutex.h>
52f591779bSSeigo Tanimura #include <sys/sx.h>
53fb919e4dSMark Murray #include <sys/proc.h>
54df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
553a669c52SDag-Erling Smørgrav #include <sys/sbuf.h>
56fb919e4dSMark Murray #include <sys/tty.h>
57df8bae1dSRodney W. Grimes 
5863a99273SMarcel Moolenaar #include <vm/vm.h>
5963a99273SMarcel Moolenaar #include <vm/pmap.h>
6063a99273SMarcel Moolenaar #include <vm/vm_param.h>
61fb919e4dSMark Murray 
623a669c52SDag-Erling Smørgrav #include <fs/pseudofs/pseudofs.h>
6399d300a1SRuslan Ermilov #include <fs/procfs/procfs.h>
6463a99273SMarcel Moolenaar 
65df8bae1dSRodney W. Grimes int
663a669c52SDag-Erling Smørgrav procfs_doprocstatus(PFS_FILL_ARGS)
67df8bae1dSRodney W. Grimes {
68df8bae1dSRodney W. Grimes 	struct session *sess;
69a21759a1SBruce Evans 	struct thread *tdfirst;
70df8bae1dSRodney W. Grimes 	struct tty *tp;
71df8bae1dSRodney W. Grimes 	struct ucred *cr;
723a669c52SDag-Erling Smørgrav 	char *pc;
73df8bae1dSRodney W. Grimes 	char *sep;
74df8bae1dSRodney W. Grimes 	int pid, ppid, pgid, sid;
75df8bae1dSRodney W. Grimes 	int i;
76df8bae1dSRodney W. Grimes 
77df8bae1dSRodney W. Grimes 	pid = p->p_pid;
7830ac5d0fSJohn Baldwin 	PROC_LOCK(p);
79e0f9d286SPeter Wemm 	ppid = p->p_pptr ? p->p_pptr->p_pid : 0;
80df8bae1dSRodney W. Grimes 	pgid = p->p_pgrp->pg_id;
81df8bae1dSRodney W. Grimes 	sess = p->p_pgrp->pg_session;
82f591779bSSeigo Tanimura 	SESS_LOCK(sess);
83df8bae1dSRodney W. Grimes 	sid = sess->s_leader ? sess->s_leader->p_pid : 0;
84df8bae1dSRodney W. Grimes 
851d08058fSWolfram Schneider /* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg
861d08058fSWolfram Schneider                                 euid ruid rgid,egid,groups[1 .. NGROUPS]
871d08058fSWolfram Schneider */
88b8c8516aSEivind Eklund 
893273a63eSMike Barcroft 	pc = p->p_comm;
903273a63eSMike Barcroft 	do {
913273a63eSMike Barcroft 		if (*pc < 33 || *pc > 126 || *pc == '\\')
923a669c52SDag-Erling Smørgrav 			sbuf_printf(sb, "\\%03o", *pc);
933273a63eSMike Barcroft 		else
943a669c52SDag-Erling Smørgrav 			sbuf_putc(sb, *pc);
953a669c52SDag-Erling Smørgrav 	} while (*++pc);
963a669c52SDag-Erling Smørgrav 	sbuf_printf(sb, " %d %d %d %d ", pid, ppid, pgid, sid);
97df8bae1dSRodney W. Grimes 	if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp))
983a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "%d,%d ", major(tp->t_dev), minor(tp->t_dev));
99df8bae1dSRodney W. Grimes 	else
1003a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "%d,%d ", -1, -1);
101df8bae1dSRodney W. Grimes 
102df8bae1dSRodney W. Grimes 	sep = "";
103df8bae1dSRodney W. Grimes 	if (sess->s_ttyvp) {
1043a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "%sctty", sep);
105df8bae1dSRodney W. Grimes 		sep = ",";
106df8bae1dSRodney W. Grimes 	}
107df8bae1dSRodney W. Grimes 	if (SESS_LEADER(p)) {
1083a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "%ssldr", sep);
109df8bae1dSRodney W. Grimes 		sep = ",";
110df8bae1dSRodney W. Grimes 	}
111f591779bSSeigo Tanimura 	SESS_UNLOCK(sess);
112f591779bSSeigo Tanimura 	PROC_UNLOCK(p);
113b8c8516aSEivind Eklund 	if (*sep != ',') {
1143a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "noflags");
115b8c8516aSEivind Eklund 	}
116df8bae1dSRodney W. Grimes 
1179ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
118b9393356SJohn Baldwin 	if (p->p_sflag & PS_INMEM) {
119df8bae1dSRodney W. Grimes 		struct timeval ut, st;
120df8bae1dSRodney W. Grimes 
1219d3a4425SDmitrij Tejblum 		calcru(p, &ut, &st, (struct timeval *) NULL);
1229ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1233a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, " %lld,%ld %ld,%ld %ld,%ld",
1244ff021c6SPeter Wemm 		    (long long)p->p_stats->p_start.tv_sec,
1259d3a4425SDmitrij Tejblum 		    p->p_stats->p_start.tv_usec,
1263a669c52SDag-Erling Smørgrav 		    ut.tv_sec, ut.tv_usec,
1273a669c52SDag-Erling Smørgrav 		    st.tv_sec, st.tv_usec);
128b9393356SJohn Baldwin 	} else {
1299ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1303a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, " -1,-1 -1,-1 -1,-1");
131b9393356SJohn Baldwin 	}
132df8bae1dSRodney W. Grimes 
133a21759a1SBruce Evans 	if (p->p_flag & P_KSES)
1343a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, " %s", "-kse- ");
135a21759a1SBruce Evans 	else {
136a21759a1SBruce Evans 		tdfirst = FIRST_THREAD_IN_PROC(p);	/* XXX diff from td? */
1373a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, " %s",
138a21759a1SBruce Evans 		    (tdfirst->td_wchan && tdfirst->td_wmesg) ?
139a21759a1SBruce Evans 		    tdfirst->td_wmesg : "nochan");
140b40ce416SJulian Elischer 	}
141df8bae1dSRodney W. Grimes 
142df8bae1dSRodney W. Grimes 	cr = p->p_ucred;
143df8bae1dSRodney W. Grimes 
1443a669c52SDag-Erling Smørgrav 	sbuf_printf(sb, " %lu %lu %lu",
145ac1e407bSBruce Evans 		(u_long)cr->cr_uid,
146b1fc0ec1SRobert Watson 		(u_long)cr->cr_ruid,
147b1fc0ec1SRobert Watson 		(u_long)cr->cr_rgid);
1481d08058fSWolfram Schneider 
149b1fc0ec1SRobert Watson 	/* egid (cr->cr_svgid) is equal to cr_ngroups[0]
1501d08058fSWolfram Schneider 	   see also getegid(2) in /sys/kern/kern_prot.c */
1511d08058fSWolfram Schneider 
152b8c8516aSEivind Eklund 	for (i = 0; i < cr->cr_ngroups; i++) {
1533a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, ",%lu", (u_long)cr->cr_groups[i]);
154b8c8516aSEivind Eklund 	}
15575c13541SPoul-Henning Kamp 
15601137630SRobert Watson 	if (jailed(p->p_ucred)) {
15701137630SRobert Watson 		mtx_lock(&p->p_ucred->cr_prison->pr_mtx);
1583a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, " %s", p->p_ucred->cr_prison->pr_host);
15901137630SRobert Watson 		mtx_unlock(&p->p_ucred->cr_prison->pr_mtx);
16001137630SRobert Watson 	} else {
1613a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, " -");
16201137630SRobert Watson 	}
1633a669c52SDag-Erling Smørgrav 	sbuf_printf(sb, "\n");
164df8bae1dSRodney W. Grimes 
1653a669c52SDag-Erling Smørgrav 	return (0);
166df8bae1dSRodney W. Grimes }
16775ba7757SPeter Wemm 
16875ba7757SPeter Wemm int
1693a669c52SDag-Erling Smørgrav procfs_doproccmdline(PFS_FILL_ARGS)
17075ba7757SPeter Wemm {
17163a99273SMarcel Moolenaar 	struct ps_strings pstr;
1723a669c52SDag-Erling Smørgrav 	int error, i;
17375ba7757SPeter Wemm 
17475ba7757SPeter Wemm 	/*
1756153cb20SPoul-Henning Kamp 	 * If we are using the ps/cmdline caching, use that.  Otherwise
1766153cb20SPoul-Henning Kamp 	 * revert back to the old way which only implements full cmdline
1776153cb20SPoul-Henning Kamp 	 * for the currept process and just p->p_comm for all other
1786153cb20SPoul-Henning Kamp 	 * processes.
17963a99273SMarcel Moolenaar 	 * Note that if the argv is no longer available, we deliberately
18063a99273SMarcel Moolenaar 	 * don't fall back on p->p_comm or return an error: the authentic
18163a99273SMarcel Moolenaar 	 * Linux behaviour is to return zero-length in this case.
18275ba7757SPeter Wemm 	 */
18363a99273SMarcel Moolenaar 
1843a669c52SDag-Erling Smørgrav 	if (p->p_args && (ps_argsopen || !p_cansee(td->td_proc, p))) {
1853a669c52SDag-Erling Smørgrav 		sbuf_bcpy(sb, p->p_args->ar_args, p->p_args->ar_length);
1863a669c52SDag-Erling Smørgrav 	} else if (p != td->td_proc) {
1873a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "%.*s", MAXCOMLEN, p->p_comm);
1886153cb20SPoul-Henning Kamp 	} else {
18963a99273SMarcel Moolenaar 		error = copyin((void*)PS_STRINGS, &pstr, sizeof(pstr));
1903a669c52SDag-Erling Smørgrav 		if (error)
19163a99273SMarcel Moolenaar 			return (error);
1923a669c52SDag-Erling Smørgrav 		for (i = 0; i < pstr.ps_nargvstr; i++) {
1933a669c52SDag-Erling Smørgrav 			sbuf_copyin(sb, pstr.ps_argvstr[i], 0);
1943a669c52SDag-Erling Smørgrav 			sbuf_printf(sb, "%c", '\0');
1956153cb20SPoul-Henning Kamp 		}
19663a99273SMarcel Moolenaar 	}
19775ba7757SPeter Wemm 
1983a669c52SDag-Erling Smørgrav 	return (0);
19975ba7757SPeter Wemm }
200