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