xref: /freebsd/sys/fs/procfs/procfs_status.c (revision 3a669c52a81fa52a7c3aa5ac06ef6f1da5981db9)
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:
40c3aac50fSPeter Wemm  * $FreeBSD$
41df8bae1dSRodney W. Grimes  */
42df8bae1dSRodney W. Grimes 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44df8bae1dSRodney W. Grimes #include <sys/systm.h>
45fb919e4dSMark Murray #include <sys/exec.h>
46fb919e4dSMark Murray #include <sys/lock.h>
47fb919e4dSMark Murray #include <sys/mutex.h>
4801137630SRobert Watson #include <sys/jail.h>
4901137630SRobert Watson #include <sys/malloc.h>
50fb919e4dSMark Murray #include <sys/proc.h>
51df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
523a669c52SDag-Erling Smørgrav #include <sys/sbuf.h>
53fb919e4dSMark Murray #include <sys/tty.h>
54df8bae1dSRodney W. Grimes 
5563a99273SMarcel Moolenaar #include <vm/vm.h>
5663a99273SMarcel Moolenaar #include <vm/pmap.h>
5763a99273SMarcel Moolenaar #include <vm/vm_param.h>
58fb919e4dSMark Murray 
593a669c52SDag-Erling Smørgrav #include <fs/pseudofs/pseudofs.h>
6099d300a1SRuslan Ermilov #include <fs/procfs/procfs.h>
6163a99273SMarcel Moolenaar 
62df8bae1dSRodney W. Grimes int
633a669c52SDag-Erling Smørgrav procfs_doprocstatus(PFS_FILL_ARGS)
64df8bae1dSRodney W. Grimes {
65df8bae1dSRodney W. Grimes 	struct session *sess;
66df8bae1dSRodney W. Grimes 	struct tty *tp;
67df8bae1dSRodney W. Grimes 	struct ucred *cr;
683a669c52SDag-Erling Smørgrav 	char *pc;
69df8bae1dSRodney W. Grimes 	char *sep;
70df8bae1dSRodney W. Grimes 	int pid, ppid, pgid, sid;
71df8bae1dSRodney W. Grimes 	int i;
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes 	pid = p->p_pid;
7430ac5d0fSJohn Baldwin 	PROC_LOCK(p);
75e0f9d286SPeter Wemm 	ppid = p->p_pptr ? p->p_pptr->p_pid : 0;
7630ac5d0fSJohn Baldwin 	PROC_UNLOCK(p);
77df8bae1dSRodney W. Grimes 	pgid = p->p_pgrp->pg_id;
78df8bae1dSRodney W. Grimes 	sess = p->p_pgrp->pg_session;
79df8bae1dSRodney W. Grimes 	sid = sess->s_leader ? sess->s_leader->p_pid : 0;
80df8bae1dSRodney W. Grimes 
811d08058fSWolfram Schneider /* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg
821d08058fSWolfram Schneider                                 euid ruid rgid,egid,groups[1 .. NGROUPS]
831d08058fSWolfram Schneider */
84b8c8516aSEivind Eklund 
853273a63eSMike Barcroft 	pc = p->p_comm;
863273a63eSMike Barcroft 	do {
873273a63eSMike Barcroft 		if (*pc < 33 || *pc > 126 || *pc == '\\')
883a669c52SDag-Erling Smørgrav 			sbuf_printf(sb, "\\%03o", *pc);
893273a63eSMike Barcroft 		else
903a669c52SDag-Erling Smørgrav 			sbuf_putc(sb, *pc);
913a669c52SDag-Erling Smørgrav 	} while (*++pc);
923a669c52SDag-Erling Smørgrav 	sbuf_printf(sb, " %d %d %d %d ", pid, ppid, pgid, sid);
93df8bae1dSRodney W. Grimes 	if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp))
943a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "%d,%d ", major(tp->t_dev), minor(tp->t_dev));
95df8bae1dSRodney W. Grimes 	else
963a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "%d,%d ", -1, -1);
97df8bae1dSRodney W. Grimes 
98df8bae1dSRodney W. Grimes 	sep = "";
99df8bae1dSRodney W. Grimes 	if (sess->s_ttyvp) {
1003a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "%sctty", sep);
101df8bae1dSRodney W. Grimes 		sep = ",";
102df8bae1dSRodney W. Grimes 	}
103df8bae1dSRodney W. Grimes 	if (SESS_LEADER(p)) {
1043a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "%ssldr", sep);
105df8bae1dSRodney W. Grimes 		sep = ",";
106df8bae1dSRodney W. Grimes 	}
107b8c8516aSEivind Eklund 	if (*sep != ',') {
1083a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "noflags");
109b8c8516aSEivind Eklund 	}
110df8bae1dSRodney W. Grimes 
1119ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
112b9393356SJohn Baldwin 	if (p->p_sflag & PS_INMEM) {
113df8bae1dSRodney W. Grimes 		struct timeval ut, st;
114df8bae1dSRodney W. Grimes 
1159d3a4425SDmitrij Tejblum 		calcru(p, &ut, &st, (struct timeval *) NULL);
1169ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1173a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, " %lld,%ld %ld,%ld %ld,%ld",
1184ff021c6SPeter Wemm 		    (long long)p->p_stats->p_start.tv_sec,
1199d3a4425SDmitrij Tejblum 		    p->p_stats->p_start.tv_usec,
1203a669c52SDag-Erling Smørgrav 		    ut.tv_sec, ut.tv_usec,
1213a669c52SDag-Erling Smørgrav 		    st.tv_sec, st.tv_usec);
122b9393356SJohn Baldwin 	} else {
1239ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
1243a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, " -1,-1 -1,-1 -1,-1");
125b9393356SJohn Baldwin 	}
126df8bae1dSRodney W. Grimes 
127b40ce416SJulian Elischer 	if (p->p_flag & P_KSES) {
1283a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, " %s", "-kse- ");
129b40ce416SJulian Elischer 	} else {
1303a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, " %s",
131b40ce416SJulian Elischer 			(p->p_thread.td_wchan && p->p_thread.td_wmesg) ?
132b40ce416SJulian Elischer 			    p->p_thread.td_wmesg : "nochan");
133b40ce416SJulian Elischer 	}
134df8bae1dSRodney W. Grimes 
135df8bae1dSRodney W. Grimes 	cr = p->p_ucred;
136df8bae1dSRodney W. Grimes 
1373a669c52SDag-Erling Smørgrav 	sbuf_printf(sb, " %lu %lu %lu",
138ac1e407bSBruce Evans 		(u_long)cr->cr_uid,
139b1fc0ec1SRobert Watson 		(u_long)cr->cr_ruid,
140b1fc0ec1SRobert Watson 		(u_long)cr->cr_rgid);
1411d08058fSWolfram Schneider 
142b1fc0ec1SRobert Watson 	/* egid (cr->cr_svgid) is equal to cr_ngroups[0]
1431d08058fSWolfram Schneider 	   see also getegid(2) in /sys/kern/kern_prot.c */
1441d08058fSWolfram Schneider 
145b8c8516aSEivind Eklund 	for (i = 0; i < cr->cr_ngroups; i++) {
1463a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, ",%lu", (u_long)cr->cr_groups[i]);
147b8c8516aSEivind Eklund 	}
14875c13541SPoul-Henning Kamp 
14901137630SRobert Watson 	if (jailed(p->p_ucred)) {
15001137630SRobert Watson 		mtx_lock(&p->p_ucred->cr_prison->pr_mtx);
1513a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, " %s", p->p_ucred->cr_prison->pr_host);
15201137630SRobert Watson 		mtx_unlock(&p->p_ucred->cr_prison->pr_mtx);
15301137630SRobert Watson 	} else {
1543a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, " -");
15501137630SRobert Watson 	}
1563a669c52SDag-Erling Smørgrav 	sbuf_printf(sb, "\n");
157df8bae1dSRodney W. Grimes 
1583a669c52SDag-Erling Smørgrav 	return (0);
159df8bae1dSRodney W. Grimes }
16075ba7757SPeter Wemm 
16175ba7757SPeter Wemm int
1623a669c52SDag-Erling Smørgrav procfs_doproccmdline(PFS_FILL_ARGS)
16375ba7757SPeter Wemm {
16463a99273SMarcel Moolenaar 	struct ps_strings pstr;
1653a669c52SDag-Erling Smørgrav 	int error, i;
16675ba7757SPeter Wemm 
16775ba7757SPeter Wemm 	/*
1686153cb20SPoul-Henning Kamp 	 * If we are using the ps/cmdline caching, use that.  Otherwise
1696153cb20SPoul-Henning Kamp 	 * revert back to the old way which only implements full cmdline
1706153cb20SPoul-Henning Kamp 	 * for the currept process and just p->p_comm for all other
1716153cb20SPoul-Henning Kamp 	 * processes.
17263a99273SMarcel Moolenaar 	 * Note that if the argv is no longer available, we deliberately
17363a99273SMarcel Moolenaar 	 * don't fall back on p->p_comm or return an error: the authentic
17463a99273SMarcel Moolenaar 	 * Linux behaviour is to return zero-length in this case.
17575ba7757SPeter Wemm 	 */
17663a99273SMarcel Moolenaar 
1773a669c52SDag-Erling Smørgrav 	if (p->p_args && (ps_argsopen || !p_cansee(td->td_proc, p))) {
1783a669c52SDag-Erling Smørgrav 		sbuf_bcpy(sb, p->p_args->ar_args, p->p_args->ar_length);
1793a669c52SDag-Erling Smørgrav 	} else if (p != td->td_proc) {
1803a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "%.*s", MAXCOMLEN, p->p_comm);
1816153cb20SPoul-Henning Kamp 	} else {
18263a99273SMarcel Moolenaar 		error = copyin((void*)PS_STRINGS, &pstr, sizeof(pstr));
1833a669c52SDag-Erling Smørgrav 		if (error)
18463a99273SMarcel Moolenaar 			return (error);
1853a669c52SDag-Erling Smørgrav 		for (i = 0; i < pstr.ps_nargvstr; i++) {
1863a669c52SDag-Erling Smørgrav 			sbuf_copyin(sb, pstr.ps_argvstr[i], 0);
1873a669c52SDag-Erling Smørgrav 			sbuf_printf(sb, "%c", '\0');
1886153cb20SPoul-Henning Kamp 		}
18963a99273SMarcel Moolenaar 	}
19075ba7757SPeter Wemm 
1913a669c52SDag-Erling Smørgrav 	return (0);
19275ba7757SPeter Wemm }
193