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. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)procfs_status.c 8.4 (Berkeley) 6/15/94 34 * 35 * From: 36 * $FreeBSD: src/sys/miscfs/procfs/procfs_status.c,v 1.20.2.4 2002/01/22 17:22:59 nectar Exp $ 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/uio.h> 42 #include <sys/malloc.h> 43 #include <sys/proc.h> 44 #include <sys/priv.h> 45 #include <sys/jail.h> 46 #include <sys/vnode.h> 47 #include <sys/tty.h> 48 #include <sys/resourcevar.h> 49 #include <vfs/procfs/procfs.h> 50 51 #include <vm/vm.h> 52 #include <vm/pmap.h> 53 #include <vm/vm_param.h> 54 #include <sys/exec.h> 55 56 #define DOCHECK() do { \ 57 if (ps >= psbuf+sizeof(psbuf)) { \ 58 error = ENOMEM; \ 59 goto bailout; \ 60 } \ 61 } while (0) 62 63 int 64 procfs_dostatus(struct proc *curp, struct lwp *lp, struct pfsnode *pfs, 65 struct uio *uio) 66 { 67 struct proc *p = lp->lwp_proc; 68 struct session *sess; 69 struct tty *tp; 70 struct ucred *cr; 71 char *ps; 72 char *sep; 73 int pid, ppid, pgid, sid; 74 size_t xlen; 75 int i; 76 int error; 77 char psbuf[256]; /* XXX - conservative */ 78 79 if (uio->uio_rw != UIO_READ) 80 return (EOPNOTSUPP); 81 82 pid = p->p_pid; 83 ppid = p->p_pptr ? p->p_pptr->p_pid : 0; 84 pgid = p->p_pgrp->pg_id; 85 sess = p->p_pgrp->pg_session; 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 KASSERT(sizeof(psbuf) > MAXCOMLEN, 92 ("Too short buffer for new MAXCOMLEN")); 93 94 ps = psbuf; 95 bcopy(p->p_comm, ps, MAXCOMLEN); 96 ps[MAXCOMLEN] = '\0'; 97 ps += strlen(ps); 98 DOCHECK(); 99 ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, 100 " %d %d %d %d ", pid, ppid, pgid, sid); 101 DOCHECK(); 102 if ((p->p_flags & P_CONTROLT) && (tp = sess->s_ttyp)) 103 ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, 104 "%d,%d ", major(tp->t_dev), minor(tp->t_dev)); 105 else 106 ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, 107 "%d,%d ", -1, -1); 108 DOCHECK(); 109 110 sep = ""; 111 if (sess->s_ttyvp) { 112 ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, "%sctty", sep); 113 sep = ","; 114 DOCHECK(); 115 } 116 if (SESS_LEADER(p)) { 117 ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, "%ssldr", sep); 118 sep = ","; 119 DOCHECK(); 120 } 121 if (*sep != ',') { 122 ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, "noflags"); 123 DOCHECK(); 124 } 125 126 if (p->p_flags & P_SWAPPEDOUT) { 127 ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, 128 " -1,-1 -1,-1 -1,-1"); 129 } else { 130 struct rusage ru; 131 132 calcru_proc(p, &ru); 133 ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, 134 " %ld,%ld %ld,%ld %ld,%ld", 135 p->p_start.tv_sec, 136 p->p_start.tv_usec, 137 ru.ru_utime.tv_sec, ru.ru_utime.tv_usec, 138 ru.ru_stime.tv_sec, ru.ru_stime.tv_usec); 139 } 140 DOCHECK(); 141 142 ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, " %s", 143 (lp->lwp_wchan && lp->lwp_wmesg) ? lp->lwp_wmesg : "nochan"); 144 DOCHECK(); 145 146 cr = p->p_ucred; 147 148 ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, " %lu %lu %lu", 149 (u_long)cr->cr_uid, 150 (u_long)p->p_ucred->cr_ruid, 151 (u_long)p->p_ucred->cr_rgid); 152 DOCHECK(); 153 154 /* egid (p->p_ucred->cr_svgid) is equal to cr_ngroups[0] 155 see also getegid(2) in /sys/kern/kern_prot.c */ 156 157 for (i = 0; i < cr->cr_ngroups; i++) { 158 ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, 159 ",%lu", (u_long)cr->cr_groups[i]); 160 DOCHECK(); 161 } 162 163 if (p->p_ucred->cr_prison) 164 ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, 165 " %s", p->p_ucred->cr_prison->pr_host); 166 else 167 ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, " -"); 168 DOCHECK(); 169 ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, "\n"); 170 DOCHECK(); 171 172 xlen = ps - psbuf; 173 error = uiomove_frombuf(psbuf, xlen, uio); 174 175 bailout: 176 return (error); 177 } 178 179 int 180 procfs_docmdline(struct proc *curp, struct lwp *lp, struct pfsnode *pfs, 181 struct uio *uio) 182 { 183 struct proc *p = lp->lwp_proc; 184 char *ps; 185 int error; 186 char *buf, *bp; 187 struct ps_strings pstr; 188 char **ps_argvstr; 189 int i; 190 size_t bytes_left, done; 191 size_t buflen; 192 193 if (uio->uio_rw != UIO_READ) 194 return (EOPNOTSUPP); 195 196 /* 197 * If we are using the ps/cmdline caching, use that. Otherwise 198 * revert back to the old way which only implements full cmdline 199 * for the currept process and just p->p_comm for all other 200 * processes. 201 * Note that if the argv is no longer available, we deliberately 202 * don't fall back on p->p_comm or return an error: the authentic 203 * Linux behaviour is to return zero-length in this case. 204 */ 205 if (p->p_upmap != NULL && p->p_upmap->proc_title[0] && 206 (ps_argsopen || (CHECKIO(curp, p) && 207 (p->p_flags & P_INEXEC) == 0 && 208 !p_trespass(curp->p_ucred, p->p_ucred)) 209 )) { 210 /* 211 * Args set via writable user process mmap. 212 * We must calculate the string length manually 213 * because the user data can change at any time. 214 */ 215 bp = p->p_upmap->proc_title; 216 for (buflen = 0; buflen < UPMAP_MAXPROCTITLE - 1; ++buflen) { 217 if (bp[buflen] == 0) 218 break; 219 } 220 buf = NULL; 221 } else if (p->p_args && 222 (ps_argsopen || (CHECKIO(curp, p) && 223 (p->p_flags & P_INEXEC) == 0 && 224 !p_trespass(curp->p_ucred, p->p_ucred)) 225 )) { 226 bp = p->p_args->ar_args; 227 buflen = p->p_args->ar_length; 228 buf = NULL; 229 } else if (p != curp) { 230 bp = p->p_comm; 231 buflen = MAXCOMLEN; 232 buf = NULL; 233 } else { 234 buflen = 256; 235 buf = kmalloc(buflen + 1, M_TEMP, M_WAITOK); 236 bp = buf; 237 ps = buf; 238 error = copyin((void*)PS_STRINGS, &pstr, sizeof(pstr)); 239 240 if (error) { 241 kfree(buf, M_TEMP); 242 return (error); 243 } 244 if (pstr.ps_nargvstr < 0) { 245 kfree(buf, M_TEMP); 246 return (EINVAL); 247 } 248 if (pstr.ps_nargvstr > ARG_MAX) { 249 kfree(buf, M_TEMP); 250 return (E2BIG); 251 } 252 ps_argvstr = kmalloc(pstr.ps_nargvstr * sizeof(char *), 253 M_TEMP, M_WAITOK); 254 error = copyin((void *)pstr.ps_argvstr, ps_argvstr, 255 pstr.ps_nargvstr * sizeof(char *)); 256 if (error) { 257 kfree(ps_argvstr, M_TEMP); 258 kfree(buf, M_TEMP); 259 return (error); 260 } 261 bytes_left = buflen; 262 for (i = 0; bytes_left && (i < pstr.ps_nargvstr); i++) { 263 error = copyinstr(ps_argvstr[i], ps, 264 bytes_left, &done); 265 /* If too long or malformed, just truncate */ 266 if (error) { 267 error = 0; 268 break; 269 } 270 ps += done; 271 bytes_left -= done; 272 } 273 buflen = ps - buf; 274 kfree(ps_argvstr, M_TEMP); 275 } 276 277 error = uiomove_frombuf(bp, buflen, uio); 278 if (buf) 279 kfree(buf, M_TEMP); 280 return (error); 281 } 282