xref: /freebsd/sys/fs/procfs/procfs_status.c (revision 315ee00f)
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  *	@(#)procfs_status.c	8.4 (Berkeley) 6/15/94
36  *
37  * From:
38  *	$Id: procfs_status.c,v 3.1 1993/12/15 09:40:17 jsp Exp $
39  */
40 
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/systm.h>
44 #include <sys/exec.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/jail.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50 #include <sys/sx.h>
51 #include <sys/proc.h>
52 #include <sys/resourcevar.h>
53 #include <sys/sbuf.h>
54 #include <sys/tty.h>
55 
56 #include <vm/vm.h>
57 #include <vm/pmap.h>
58 #include <vm/vm_param.h>
59 
60 #include <fs/pseudofs/pseudofs.h>
61 #include <fs/procfs/procfs.h>
62 
63 int
64 procfs_doprocstatus(PFS_FILL_ARGS)
65 {
66 	struct session *sess;
67 	struct thread *tdfirst;
68 	struct tty *tp;
69 	struct ucred *cr;
70 	const char *wmesg;
71 	char *pc;
72 	char *sep;
73 	struct timeval boottime;
74 	int pid, ppid, pgid, sid;
75 	int i;
76 
77 	pid = p->p_pid;
78 	PROC_LOCK(p);
79 	ppid = p->p_pptr ? p->p_pptr->p_pid : 0;
80 	pgid = p->p_pgrp->pg_id;
81 	sess = p->p_pgrp->pg_session;
82 	SESS_LOCK(sess);
83 	sid = sess->s_leader ? sess->s_leader->p_pid : 0;
84 
85 /* comm pid ppid pgid sid tty ctty,sldr start ut st wmsg
86 				euid ruid rgid,egid,groups[1 .. ngroups]
87 */
88 
89 	pc = p->p_comm;
90 	do {
91 		if (*pc < 33 || *pc > 126 || *pc == '\\')
92 			sbuf_printf(sb, "\\%03o", *pc);
93 		else
94 			sbuf_putc(sb, *pc);
95 	} while (*++pc);
96 	sbuf_printf(sb, " %d %d %d %d ", pid, ppid, pgid, sid);
97 	if ((p->p_flag & P_CONTROLT) && (tp = sess->s_ttyp))
98 		sbuf_printf(sb, "%s ", devtoname(tp->t_dev));
99 	else
100 		sbuf_printf(sb, "- ");
101 
102 	sep = "";
103 	if (sess->s_ttyvp) {
104 		sbuf_printf(sb, "%sctty", sep);
105 		sep = ",";
106 	}
107 	if (SESS_LEADER(p)) {
108 		sbuf_printf(sb, "%ssldr", sep);
109 		sep = ",";
110 	}
111 	SESS_UNLOCK(sess);
112 	if (*sep != ',') {
113 		sbuf_printf(sb, "noflags");
114 	}
115 
116 	tdfirst = FIRST_THREAD_IN_PROC(p);
117 	thread_lock(tdfirst);
118 	if (tdfirst->td_wchan != NULL) {
119 		KASSERT(tdfirst->td_wmesg != NULL,
120 		    ("wchan %p has no wmesg", tdfirst->td_wchan));
121 		wmesg = tdfirst->td_wmesg;
122 	} else
123 		wmesg = "nochan";
124 	thread_unlock(tdfirst);
125 
126 	if (p->p_flag & P_INMEM) {
127 		struct timeval start, ut, st;
128 
129 		PROC_STATLOCK(p);
130 		calcru(p, &ut, &st);
131 		PROC_STATUNLOCK(p);
132 		start = p->p_stats->p_start;
133 		getboottime(&boottime);
134 		timevaladd(&start, &boottime);
135 		sbuf_printf(sb, " %jd,%ld %jd,%ld %jd,%ld",
136 		    (intmax_t)start.tv_sec, start.tv_usec,
137 		    (intmax_t)ut.tv_sec, ut.tv_usec,
138 		    (intmax_t)st.tv_sec, st.tv_usec);
139 	} else
140 		sbuf_printf(sb, " -1,-1 -1,-1 -1,-1");
141 
142 	sbuf_printf(sb, " %s", wmesg);
143 
144 	cr = p->p_ucred;
145 
146 	sbuf_printf(sb, " %lu %lu %lu",
147 		(u_long)cr->cr_uid,
148 		(u_long)cr->cr_ruid,
149 		(u_long)cr->cr_rgid);
150 
151 	/* egid (cr->cr_svgid) is equal to cr_ngroups[0]
152 	   see also getegid(2) in /sys/kern/kern_prot.c */
153 
154 	for (i = 0; i < cr->cr_ngroups; i++) {
155 		sbuf_printf(sb, ",%lu", (u_long)cr->cr_groups[i]);
156 	}
157 
158 	if (jailed(cr)) {
159 		mtx_lock(&cr->cr_prison->pr_mtx);
160 		sbuf_printf(sb, " %s",
161 		    prison_name(td->td_ucred->cr_prison, cr->cr_prison));
162 		mtx_unlock(&cr->cr_prison->pr_mtx);
163 	} else {
164 		sbuf_printf(sb, " -");
165 	}
166 	PROC_UNLOCK(p);
167 	sbuf_printf(sb, "\n");
168 
169 	return (0);
170 }
171 
172 int
173 procfs_doproccmdline(PFS_FILL_ARGS)
174 {
175 
176 	/*
177 	 * If we are using the ps/cmdline caching, use that.  Otherwise
178 	 * read argv from the process space.
179 	 * Note that if the argv is no longer available, we deliberately
180 	 * don't fall back on p->p_comm or return an error: the authentic
181 	 * Linux behaviour is to return zero-length in this case.
182 	 */
183 
184 	PROC_LOCK(p);
185 	if (p->p_args && p_cansee(td, p) == 0) {
186 		sbuf_bcpy(sb, p->p_args->ar_args, p->p_args->ar_length);
187 		PROC_UNLOCK(p);
188 		return (0);
189 	}
190 
191 	if ((p->p_flag & P_SYSTEM) != 0) {
192 		PROC_UNLOCK(p);
193 		return (0);
194 	}
195 
196 	PROC_UNLOCK(p);
197 
198 	return (proc_getargv(td, p, sb));
199 }
200