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: src/sys/miscfs/procfs/procfs_status.c,v 1.20.2.4 2002/01/22 17:22:59 nectar Exp $ 41 * $DragonFly: src/sys/vfs/procfs/procfs_status.c,v 1.7 2004/06/20 22:29:10 hmp Exp $ 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/malloc.h> 47 #include <sys/proc.h> 48 #include <sys/jail.h> 49 #include <sys/vnode.h> 50 #include <sys/tty.h> 51 #include <sys/resourcevar.h> 52 #include <vfs/procfs/procfs.h> 53 54 #include <vm/vm.h> 55 #include <vm/pmap.h> 56 #include <vm/vm_param.h> 57 #include <sys/exec.h> 58 59 #define DOCHECK() do { if (ps >= psbuf+sizeof(psbuf)) goto bailout; } while (0) 60 int 61 procfs_dostatus(struct proc *curp, struct proc *p, struct pfsnode *pfs, 62 struct uio *uio) 63 { 64 struct session *sess; 65 struct tty *tp; 66 struct ucred *cr; 67 char *ps; 68 char *sep; 69 int pid, ppid, pgid, sid; 70 int i; 71 int xlen; 72 int error; 73 char psbuf[256]; /* XXX - conservative */ 74 75 if (uio->uio_rw != UIO_READ) 76 return (EOPNOTSUPP); 77 78 pid = p->p_pid; 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 sid = sess->s_leader ? sess->s_leader->p_pid : 0; 83 84 /* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg 85 euid ruid rgid,egid,groups[1 .. NGROUPS] 86 */ 87 KASSERT(sizeof(psbuf) > MAXCOMLEN, 88 ("Too short buffer for new MAXCOMLEN")); 89 90 ps = psbuf; 91 bcopy(p->p_comm, ps, MAXCOMLEN); 92 ps[MAXCOMLEN] = '\0'; 93 ps += strlen(ps); 94 DOCHECK(); 95 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, 96 " %d %d %d %d ", pid, ppid, pgid, sid); 97 DOCHECK(); 98 if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp)) 99 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, 100 "%d,%d ", major(tp->t_dev), minor(tp->t_dev)); 101 else 102 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, 103 "%d,%d ", -1, -1); 104 DOCHECK(); 105 106 sep = ""; 107 if (sess->s_ttyvp) { 108 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "%sctty", sep); 109 sep = ","; 110 DOCHECK(); 111 } 112 if (SESS_LEADER(p)) { 113 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "%ssldr", sep); 114 sep = ","; 115 DOCHECK(); 116 } 117 if (*sep != ',') { 118 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "noflags"); 119 DOCHECK(); 120 } 121 122 if (p->p_flag & P_INMEM) { 123 struct timeval ut, st; 124 125 calcru(p, &ut, &st, (struct timeval *) NULL); 126 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, 127 " %ld,%ld %ld,%ld %ld,%ld", 128 p->p_thread ? p->p_thread->td_start.tv_sec : 0, 129 p->p_thread ? p->p_thread->td_start.tv_usec : 0, 130 ut.tv_sec, ut.tv_usec, 131 st.tv_sec, st.tv_usec); 132 } else 133 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, 134 " -1,-1 -1,-1 -1,-1"); 135 DOCHECK(); 136 137 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " %s", 138 (p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan"); 139 DOCHECK(); 140 141 cr = p->p_ucred; 142 143 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " %lu %lu %lu", 144 (u_long)cr->cr_uid, 145 (u_long)p->p_ucred->cr_ruid, 146 (u_long)p->p_ucred->cr_rgid); 147 DOCHECK(); 148 149 /* egid (p->p_ucred->cr_svgid) is equal to cr_ngroups[0] 150 see also getegid(2) in /sys/kern/kern_prot.c */ 151 152 for (i = 0; i < cr->cr_ngroups; i++) { 153 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, 154 ",%lu", (u_long)cr->cr_groups[i]); 155 DOCHECK(); 156 } 157 158 if (p->p_ucred->cr_prison) 159 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, 160 " %s", p->p_ucred->cr_prison->pr_host); 161 else 162 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " -"); 163 DOCHECK(); 164 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "\n"); 165 DOCHECK(); 166 167 xlen = ps - psbuf; 168 xlen -= uio->uio_offset; 169 ps = psbuf + uio->uio_offset; 170 xlen = imin(xlen, uio->uio_resid); 171 if (xlen <= 0) 172 error = 0; 173 else 174 error = uiomove_frombuf(psbuf, ps - psbuf, uio); 175 176 return (error); 177 178 bailout: 179 return (ENOMEM); 180 } 181 182 int 183 procfs_docmdline(struct proc *curp, struct proc *p, struct pfsnode *pfs, 184 struct uio *uio) 185 { 186 char *ps; 187 int xlen; 188 int error; 189 char *buf, *bp; 190 int buflen; 191 struct ps_strings pstr; 192 int i; 193 size_t bytes_left, done; 194 195 if (uio->uio_rw != UIO_READ) 196 return (EOPNOTSUPP); 197 198 /* 199 * If we are using the ps/cmdline caching, use that. Otherwise 200 * revert back to the old way which only implements full cmdline 201 * for the currept process and just p->p_comm for all other 202 * processes. 203 * Note that if the argv is no longer available, we deliberately 204 * don't fall back on p->p_comm or return an error: the authentic 205 * Linux behaviour is to return zero-length in this case. 206 */ 207 208 if (p->p_args && 209 (ps_argsopen || (CHECKIO(curp, p) && 210 (p->p_flag & P_INEXEC) == 0 && !p_trespass(curp->p_ucred, p->p_ucred))) 211 ) { 212 bp = p->p_args->ar_args; 213 buflen = p->p_args->ar_length; 214 buf = 0; 215 } else if (p != curp) { 216 bp = p->p_comm; 217 buflen = MAXCOMLEN; 218 buf = 0; 219 } else { 220 buflen = 256; 221 MALLOC(buf, char *, buflen + 1, M_TEMP, M_WAITOK); 222 bp = buf; 223 ps = buf; 224 error = copyin((void*)PS_STRINGS, &pstr, sizeof(pstr)); 225 if (error) { 226 FREE(buf, M_TEMP); 227 return (error); 228 } 229 bytes_left = buflen; 230 for (i = 0; bytes_left && (i < pstr.ps_nargvstr); i++) { 231 error = copyinstr(pstr.ps_argvstr[i], ps, 232 bytes_left, &done); 233 /* If too long or malformed, just truncate */ 234 if (error) { 235 error = 0; 236 break; 237 } 238 ps += done; 239 bytes_left -= done; 240 } 241 buflen = ps - buf; 242 } 243 244 buflen -= uio->uio_offset; 245 ps = bp + uio->uio_offset; 246 xlen = min(buflen, uio->uio_resid); 247 if (xlen <= 0) 248 error = 0; 249 else 250 error = uiomove_frombuf(bp, buflen, uio); 251 if (buf) 252 FREE(buf, M_TEMP); 253 return (error); 254 } 255