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