2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1993 Jan-Simon Pendry
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
35 * @(#)procfs_status.c 8.4 (Berkeley) 6/15/94
38 * $Id: procfs_status.c,v 3.1 1993/12/15 09:40:17 jsp Exp $
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/systm.h>
47 #include <sys/mutex.h>
49 #include <sys/malloc.h>
50 #include <sys/mutex.h>
53 #include <sys/resourcevar.h>
59 #include <vm/vm_param.h>
61 #include <fs/pseudofs/pseudofs.h>
62 #include <fs/procfs/procfs.h>
65 procfs_doprocstatus(PFS_FILL_ARGS)
68 struct thread *tdfirst;
74 struct timeval boottime;
75 int pid, ppid, pgid, sid;
80 ppid = p->p_pptr ? p->p_pptr->p_pid : 0;
81 pgid = p->p_pgrp->pg_id;
82 sess = p->p_pgrp->pg_session;
84 sid = sess->s_leader ? sess->s_leader->p_pid : 0;
86 /* comm pid ppid pgid sid tty ctty,sldr start ut st wmsg
87 euid ruid rgid,egid,groups[1 .. ngroups]
92 if (*pc < 33 || *pc > 126 || *pc == '\\')
93 sbuf_printf(sb, "\\%03o", *pc);
97 sbuf_printf(sb, " %d %d %d %d ", pid, ppid, pgid, sid);
98 if ((p->p_flag & P_CONTROLT) && (tp = sess->s_ttyp))
99 sbuf_printf(sb, "%s ", devtoname(tp->t_dev));
101 sbuf_printf(sb, "- ");
105 sbuf_printf(sb, "%sctty", sep);
108 if (SESS_LEADER(p)) {
109 sbuf_printf(sb, "%ssldr", sep);
114 sbuf_printf(sb, "noflags");
117 tdfirst = FIRST_THREAD_IN_PROC(p);
118 thread_lock(tdfirst);
119 if (tdfirst->td_wchan != NULL) {
120 KASSERT(tdfirst->td_wmesg != NULL,
121 ("wchan %p has no wmesg", tdfirst->td_wchan));
122 wmesg = tdfirst->td_wmesg;
125 thread_unlock(tdfirst);
127 if (p->p_flag & P_INMEM) {
128 struct timeval start, ut, st;
133 start = p->p_stats->p_start;
134 getboottime(&boottime);
135 timevaladd(&start, &boottime);
136 sbuf_printf(sb, " %jd,%ld %jd,%ld %jd,%ld",
137 (intmax_t)start.tv_sec, start.tv_usec,
138 (intmax_t)ut.tv_sec, ut.tv_usec,
139 (intmax_t)st.tv_sec, st.tv_usec);
141 sbuf_printf(sb, " -1,-1 -1,-1 -1,-1");
143 sbuf_printf(sb, " %s", wmesg);
147 sbuf_printf(sb, " %lu %lu %lu",
150 (u_long)cr->cr_rgid);
152 /* egid (cr->cr_svgid) is equal to cr_ngroups[0]
153 see also getegid(2) in /sys/kern/kern_prot.c */
155 for (i = 0; i < cr->cr_ngroups; i++) {
156 sbuf_printf(sb, ",%lu", (u_long)cr->cr_groups[i]);
160 mtx_lock(&cr->cr_prison->pr_mtx);
161 sbuf_printf(sb, " %s",
162 prison_name(td->td_ucred->cr_prison, cr->cr_prison));
163 mtx_unlock(&cr->cr_prison->pr_mtx);
165 sbuf_printf(sb, " -");
168 sbuf_printf(sb, "\n");
174 procfs_doproccmdline(PFS_FILL_ARGS)
178 * If we are using the ps/cmdline caching, use that. Otherwise
179 * read argv from the process space.
180 * Note that if the argv is no longer available, we deliberately
181 * don't fall back on p->p_comm or return an error: the authentic
182 * Linux behaviour is to return zero-length in this case.
186 if (p->p_args && p_cansee(td, p) == 0) {
187 sbuf_bcpy(sb, p->p_args->ar_args, p->p_args->ar_length);
192 if ((p->p_flag & P_SYSTEM) != 0) {
199 return (proc_getargv(td, p, sb));