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 * $FreeBSD$ 41 */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/malloc.h> 46 #include <sys/proc.h> 47 #include <sys/jail.h> 48 #include <sys/vnode.h> 49 #include <sys/tty.h> 50 #include <sys/resourcevar.h> 51 #include <miscfs/procfs/procfs.h> 52 53 #include <vm/vm.h> 54 #include <vm/pmap.h> 55 #include <vm/vm_param.h> 56 #include <sys/exec.h> 57 58 int 59 procfs_dostatus(curp, p, pfs, uio) 60 struct proc *curp; 61 struct proc *p; 62 struct pfsnode *pfs; 63 struct uio *uio; 64 { 65 struct session *sess; 66 struct tty *tp; 67 struct ucred *cr; 68 char *ps; 69 char *sep; 70 int pid, ppid, pgid, sid; 71 int i; 72 int xlen; 73 int error; 74 char psbuf[256]; /* XXX - conservative */ 75 76 if (uio->uio_rw != UIO_READ) 77 return (EOPNOTSUPP); 78 79 pid = p->p_pid; 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; 83 sid = sess->s_leader ? sess->s_leader->p_pid : 0; 84 85 /* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg 86 euid ruid rgid,egid,groups[1 .. NGROUPS] 87 */ 88 ps = psbuf; 89 bcopy(p->p_comm, ps, MAXCOMLEN); 90 ps[MAXCOMLEN] = '\0'; 91 ps += strlen(ps); 92 ps += sprintf(ps, " %d %d %d %d ", pid, ppid, pgid, sid); 93 94 if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp)) 95 ps += sprintf(ps, "%d,%d ", major(tp->t_dev), minor(tp->t_dev)); 96 else 97 ps += sprintf(ps, "%d,%d ", -1, -1); 98 99 sep = ""; 100 if (sess->s_ttyvp) { 101 ps += sprintf(ps, "%sctty", sep); 102 sep = ","; 103 } 104 if (SESS_LEADER(p)) { 105 ps += sprintf(ps, "%ssldr", sep); 106 sep = ","; 107 } 108 if (*sep != ',') 109 ps += sprintf(ps, "noflags"); 110 111 if (p->p_flag & P_INMEM) { 112 struct timeval ut, st; 113 114 calcru(p, &ut, &st, (struct timeval *) NULL); 115 ps += sprintf(ps, " %ld,%ld %ld,%ld %ld,%ld", 116 p->p_stats->p_start.tv_sec, 117 p->p_stats->p_start.tv_usec, 118 ut.tv_sec, ut.tv_usec, 119 st.tv_sec, st.tv_usec); 120 } else 121 ps += sprintf(ps, " -1,-1 -1,-1 -1,-1"); 122 123 ps += sprintf(ps, " %s", 124 (p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan"); 125 126 cr = p->p_ucred; 127 128 ps += sprintf(ps, " %lu %lu %lu", 129 (u_long)cr->cr_uid, 130 (u_long)p->p_cred->p_ruid, 131 (u_long)p->p_cred->p_rgid); 132 133 /* egid (p->p_cred->p_svgid) is equal to cr_ngroups[0] 134 see also getegid(2) in /sys/kern/kern_prot.c */ 135 136 for (i = 0; i < cr->cr_ngroups; i++) 137 ps += sprintf(ps, ",%lu", (u_long)cr->cr_groups[i]); 138 139 if (p->p_prison) 140 ps += sprintf(ps, " %s", p->p_prison->pr_host); 141 else 142 ps += sprintf(ps, " -"); 143 ps += sprintf(ps, "\n"); 144 145 xlen = ps - psbuf; 146 xlen -= uio->uio_offset; 147 ps = psbuf + uio->uio_offset; 148 xlen = imin(xlen, uio->uio_resid); 149 if (xlen <= 0) 150 error = 0; 151 else 152 error = uiomove(ps, xlen, uio); 153 154 return (error); 155 } 156 157 int 158 procfs_docmdline(curp, p, pfs, uio) 159 struct proc *curp; 160 struct proc *p; 161 struct pfsnode *pfs; 162 struct uio *uio; 163 { 164 char *ps; 165 int xlen; 166 int error; 167 char *buf, *bp; 168 int buflen; 169 struct ps_strings pstr; 170 int i; 171 size_t bytes_left, done; 172 173 if (uio->uio_rw != UIO_READ) 174 return (EOPNOTSUPP); 175 176 /* 177 * If we are using the ps/cmdline caching, use that. Otherwise 178 * revert back to the old way which only implements full cmdline 179 * for the currept process and just p->p_comm for all other 180 * processes. 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 if (p->p_args && (ps_argsopen ||!p_trespass(curp, p))) { 187 bp = p->p_args->ar_args; 188 buflen = p->p_args->ar_length; 189 buf = 0; 190 } else if (p != curp) { 191 bp = p->p_comm; 192 buflen = MAXCOMLEN; 193 buf = 0; 194 } else { 195 buflen = 256; 196 MALLOC(buf, char *, buflen + 1, M_TEMP, M_WAITOK); 197 bp = buf; 198 ps = buf; 199 error = copyin((void*)PS_STRINGS, &pstr, sizeof(pstr)); 200 if (error) { 201 FREE(buf, M_TEMP); 202 return (error); 203 } 204 bytes_left = buflen; 205 for (i = 0; bytes_left && (i < pstr.ps_nargvstr); i++) { 206 error = copyinstr(pstr.ps_argvstr[i], ps, 207 bytes_left, &done); 208 /* If too long or malformed, just truncate */ 209 if (error) { 210 error = 0; 211 break; 212 } 213 ps += done; 214 bytes_left -= done; 215 } 216 buflen = ps - buf; 217 } 218 219 buflen -= uio->uio_offset; 220 ps = bp + uio->uio_offset; 221 xlen = min(buflen, uio->uio_resid); 222 if (xlen <= 0) 223 error = 0; 224 else 225 error = uiomove(ps, xlen, uio); 226 if (buf) 227 FREE(buf, M_TEMP); 228 return (error); 229 } 230