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