xref: /freebsd/sys/fs/procfs/procfs_status.c (revision 3becbb252c733d3d09172a34d7d00d27f84d0e86)
1 /*
2  * Copyright (c) 1993 Jan-Simon Pendry
3  * Copyright (c) 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)procfs_status.c	8.4 (Berkeley) 6/15/94
38  *
39  * From:
40  * $FreeBSD$
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/exec.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/jail.h>
49 #include <sys/malloc.h>
50 #include <sys/proc.h>
51 #include <sys/resourcevar.h>
52 #include <sys/sbuf.h>
53 #include <sys/tty.h>
54 
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_param.h>
58 
59 #include <fs/pseudofs/pseudofs.h>
60 #include <fs/procfs/procfs.h>
61 
62 int
63 procfs_doprocstatus(PFS_FILL_ARGS)
64 {
65 	struct session *sess;
66 	struct tty *tp;
67 	struct ucred *cr;
68 	char *pc;
69 	char *sep;
70 	int pid, ppid, pgid, sid;
71 	int i;
72 
73 	pid = p->p_pid;
74 	PROC_LOCK(p);
75 	ppid = p->p_pptr ? p->p_pptr->p_pid : 0;
76 	PROC_UNLOCK(p);
77 	pgid = p->p_pgrp->pg_id;
78 	sess = p->p_pgrp->pg_session;
79 	sid = sess->s_leader ? sess->s_leader->p_pid : 0;
80 
81 /* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg
82                                 euid ruid rgid,egid,groups[1 .. NGROUPS]
83 */
84 
85 	pc = p->p_comm;
86 	do {
87 		if (*pc < 33 || *pc > 126 || *pc == '\\')
88 			sbuf_printf(sb, "\\%03o", *pc);
89 		else
90 			sbuf_putc(sb, *pc);
91 	} while (*++pc);
92 	sbuf_printf(sb, " %d %d %d %d ", pid, ppid, pgid, sid);
93 	if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp))
94 		sbuf_printf(sb, "%d,%d ", major(tp->t_dev), minor(tp->t_dev));
95 	else
96 		sbuf_printf(sb, "%d,%d ", -1, -1);
97 
98 	sep = "";
99 	if (sess->s_ttyvp) {
100 		sbuf_printf(sb, "%sctty", sep);
101 		sep = ",";
102 	}
103 	if (SESS_LEADER(p)) {
104 		sbuf_printf(sb, "%ssldr", sep);
105 		sep = ",";
106 	}
107 	if (*sep != ',') {
108 		sbuf_printf(sb, "noflags");
109 	}
110 
111 	mtx_lock_spin(&sched_lock);
112 	if (p->p_sflag & PS_INMEM) {
113 		struct timeval ut, st;
114 
115 		calcru(p, &ut, &st, (struct timeval *) NULL);
116 		mtx_unlock_spin(&sched_lock);
117 		sbuf_printf(sb, " %lld,%ld %ld,%ld %ld,%ld",
118 		    (long long)p->p_stats->p_start.tv_sec,
119 		    p->p_stats->p_start.tv_usec,
120 		    ut.tv_sec, ut.tv_usec,
121 		    st.tv_sec, st.tv_usec);
122 	} else {
123 		mtx_unlock_spin(&sched_lock);
124 		sbuf_printf(sb, " -1,-1 -1,-1 -1,-1");
125 	}
126 
127 	if (p->p_flag & P_KSES) {
128 		sbuf_printf(sb, " %s", "-kse- ");
129 	} else {
130 		sbuf_printf(sb, " %s",
131 			(p->p_thread.td_wchan && p->p_thread.td_wmesg) ?
132 			    p->p_thread.td_wmesg : "nochan");
133 	}
134 
135 	cr = p->p_ucred;
136 
137 	sbuf_printf(sb, " %lu %lu %lu",
138 		(u_long)cr->cr_uid,
139 		(u_long)cr->cr_ruid,
140 		(u_long)cr->cr_rgid);
141 
142 	/* egid (cr->cr_svgid) is equal to cr_ngroups[0]
143 	   see also getegid(2) in /sys/kern/kern_prot.c */
144 
145 	for (i = 0; i < cr->cr_ngroups; i++) {
146 		sbuf_printf(sb, ",%lu", (u_long)cr->cr_groups[i]);
147 	}
148 
149 	if (jailed(p->p_ucred)) {
150 		mtx_lock(&p->p_ucred->cr_prison->pr_mtx);
151 		sbuf_printf(sb, " %s", p->p_ucred->cr_prison->pr_host);
152 		mtx_unlock(&p->p_ucred->cr_prison->pr_mtx);
153 	} else {
154 		sbuf_printf(sb, " -");
155 	}
156 	sbuf_printf(sb, "\n");
157 
158 	return (0);
159 }
160 
161 int
162 procfs_doproccmdline(PFS_FILL_ARGS)
163 {
164 	struct ps_strings pstr;
165 	int error, i;
166 
167 	/*
168 	 * If we are using the ps/cmdline caching, use that.  Otherwise
169 	 * revert back to the old way which only implements full cmdline
170 	 * for the currept process and just p->p_comm for all other
171 	 * processes.
172 	 * Note that if the argv is no longer available, we deliberately
173 	 * don't fall back on p->p_comm or return an error: the authentic
174 	 * Linux behaviour is to return zero-length in this case.
175 	 */
176 
177 	if (p->p_args && (ps_argsopen || !p_cansee(td->td_proc, p))) {
178 		sbuf_bcpy(sb, p->p_args->ar_args, p->p_args->ar_length);
179 	} else if (p != td->td_proc) {
180 		sbuf_printf(sb, "%.*s", MAXCOMLEN, p->p_comm);
181 	} else {
182 		error = copyin((void*)PS_STRINGS, &pstr, sizeof(pstr));
183 		if (error)
184 			return (error);
185 		for (i = 0; i < pstr.ps_nargvstr; i++) {
186 			sbuf_copyin(sb, pstr.ps_argvstr[i], 0);
187 			sbuf_printf(sb, "%c", '\0');
188 		}
189 	}
190 
191 	return (0);
192 }
193