1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1993 Jan-Simon Pendry 3df8bae1dSRodney W. Grimes * Copyright (c) 1993 4df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 5df8bae1dSRodney W. Grimes * 6df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 7df8bae1dSRodney W. Grimes * Jan-Simon Pendry. 8df8bae1dSRodney W. Grimes * 9df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 10df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 11df8bae1dSRodney W. Grimes * are met: 12df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 14df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 15df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 16df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 17df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 18df8bae1dSRodney W. Grimes * must display the following acknowledgement: 19df8bae1dSRodney W. Grimes * This product includes software developed by the University of 20df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 21df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 22df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 23df8bae1dSRodney W. Grimes * without specific prior written permission. 24df8bae1dSRodney W. Grimes * 25df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35df8bae1dSRodney W. Grimes * SUCH DAMAGE. 36df8bae1dSRodney W. Grimes * 37996c772fSJohn Dyson * @(#)procfs_status.c 8.4 (Berkeley) 6/15/94 38df8bae1dSRodney W. Grimes * 39996c772fSJohn Dyson * From: 40c3aac50fSPeter Wemm * $FreeBSD$ 41df8bae1dSRodney W. Grimes */ 42df8bae1dSRodney W. Grimes 43df8bae1dSRodney W. Grimes #include <sys/param.h> 44df8bae1dSRodney W. Grimes #include <sys/systm.h> 456153cb20SPoul-Henning Kamp #include <sys/malloc.h> 46df8bae1dSRodney W. Grimes #include <sys/proc.h> 4775c13541SPoul-Henning Kamp #include <sys/jail.h> 48df8bae1dSRodney W. Grimes #include <sys/vnode.h> 49df8bae1dSRodney W. Grimes #include <sys/tty.h> 50df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 51df8bae1dSRodney W. Grimes #include <miscfs/procfs/procfs.h> 52df8bae1dSRodney W. Grimes 5363a99273SMarcel Moolenaar #include <vm/vm.h> 5463a99273SMarcel Moolenaar #include <vm/pmap.h> 5563a99273SMarcel Moolenaar #include <vm/vm_param.h> 5663a99273SMarcel Moolenaar #include <sys/exec.h> 5763a99273SMarcel Moolenaar 58b8c8516aSEivind Eklund #define DOCHECK() do { if (ps >= psbuf+sizeof(psbuf)) goto bailout; } while (0) 59df8bae1dSRodney W. Grimes int 60df8bae1dSRodney W. Grimes procfs_dostatus(curp, p, pfs, uio) 61df8bae1dSRodney W. Grimes struct proc *curp; 62df8bae1dSRodney W. Grimes struct proc *p; 63df8bae1dSRodney W. Grimes struct pfsnode *pfs; 64df8bae1dSRodney W. Grimes struct uio *uio; 65df8bae1dSRodney W. Grimes { 66df8bae1dSRodney W. Grimes struct session *sess; 67df8bae1dSRodney W. Grimes struct tty *tp; 68df8bae1dSRodney W. Grimes struct ucred *cr; 69df8bae1dSRodney W. Grimes char *ps; 70df8bae1dSRodney W. Grimes char *sep; 71df8bae1dSRodney W. Grimes int pid, ppid, pgid, sid; 72df8bae1dSRodney W. Grimes int i; 73df8bae1dSRodney W. Grimes int xlen; 74df8bae1dSRodney W. Grimes int error; 75b8c8516aSEivind Eklund char psbuf[256]; /* XXX - conservative */ 76df8bae1dSRodney W. Grimes 77df8bae1dSRodney W. Grimes if (uio->uio_rw != UIO_READ) 78df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 79df8bae1dSRodney W. Grimes 80df8bae1dSRodney W. Grimes pid = p->p_pid; 8130ac5d0fSJohn Baldwin PROC_LOCK(p); 82e0f9d286SPeter Wemm ppid = p->p_pptr ? p->p_pptr->p_pid : 0; 8330ac5d0fSJohn Baldwin PROC_UNLOCK(p); 84df8bae1dSRodney W. Grimes pgid = p->p_pgrp->pg_id; 85df8bae1dSRodney W. Grimes sess = p->p_pgrp->pg_session; 86df8bae1dSRodney W. Grimes sid = sess->s_leader ? sess->s_leader->p_pid : 0; 87df8bae1dSRodney W. Grimes 881d08058fSWolfram Schneider /* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg 891d08058fSWolfram Schneider euid ruid rgid,egid,groups[1 .. NGROUPS] 901d08058fSWolfram Schneider */ 91b8c8516aSEivind Eklund KASSERT(sizeof(psbuf) > MAXCOMLEN, 92b8c8516aSEivind Eklund ("Too short buffer for new MAXCOMLEN")); 93b8c8516aSEivind Eklund 94df8bae1dSRodney W. Grimes ps = psbuf; 95df8bae1dSRodney W. Grimes bcopy(p->p_comm, ps, MAXCOMLEN); 96df8bae1dSRodney W. Grimes ps[MAXCOMLEN] = '\0'; 97df8bae1dSRodney W. Grimes ps += strlen(ps); 98b8c8516aSEivind Eklund DOCHECK(); 99b8c8516aSEivind Eklund ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, 100b8c8516aSEivind Eklund " %d %d %d %d ", pid, ppid, pgid, sid); 101b8c8516aSEivind Eklund DOCHECK(); 102df8bae1dSRodney W. Grimes if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp)) 103b8c8516aSEivind Eklund ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, 104b8c8516aSEivind Eklund "%d,%d ", major(tp->t_dev), minor(tp->t_dev)); 105df8bae1dSRodney W. Grimes else 106b8c8516aSEivind Eklund ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, 107b8c8516aSEivind Eklund "%d,%d ", -1, -1); 108b8c8516aSEivind Eklund DOCHECK(); 109df8bae1dSRodney W. Grimes 110df8bae1dSRodney W. Grimes sep = ""; 111df8bae1dSRodney W. Grimes if (sess->s_ttyvp) { 112b8c8516aSEivind Eklund ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "%sctty", sep); 113df8bae1dSRodney W. Grimes sep = ","; 114b8c8516aSEivind Eklund DOCHECK(); 115df8bae1dSRodney W. Grimes } 116df8bae1dSRodney W. Grimes if (SESS_LEADER(p)) { 117b8c8516aSEivind Eklund ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "%ssldr", sep); 118df8bae1dSRodney W. Grimes sep = ","; 119b8c8516aSEivind Eklund DOCHECK(); 120df8bae1dSRodney W. Grimes } 121b8c8516aSEivind Eklund if (*sep != ',') { 122b8c8516aSEivind Eklund ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "noflags"); 123b8c8516aSEivind Eklund DOCHECK(); 124b8c8516aSEivind Eklund } 125df8bae1dSRodney W. Grimes 1269ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 127b9393356SJohn Baldwin if (p->p_sflag & PS_INMEM) { 128df8bae1dSRodney W. Grimes struct timeval ut, st; 129df8bae1dSRodney W. Grimes 1309d3a4425SDmitrij Tejblum calcru(p, &ut, &st, (struct timeval *) NULL); 1319ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 132b8c8516aSEivind Eklund ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, 133b8c8516aSEivind Eklund " %ld,%ld %ld,%ld %ld,%ld", 1349d3a4425SDmitrij Tejblum p->p_stats->p_start.tv_sec, 1359d3a4425SDmitrij Tejblum p->p_stats->p_start.tv_usec, 1369d3a4425SDmitrij Tejblum ut.tv_sec, ut.tv_usec, 1379d3a4425SDmitrij Tejblum st.tv_sec, st.tv_usec); 138b9393356SJohn Baldwin } else { 1399ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 140b8c8516aSEivind Eklund ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, 141b8c8516aSEivind Eklund " -1,-1 -1,-1 -1,-1"); 142b9393356SJohn Baldwin } 143b8c8516aSEivind Eklund DOCHECK(); 144df8bae1dSRodney W. Grimes 145b8c8516aSEivind Eklund ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " %s", 146df8bae1dSRodney W. Grimes (p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan"); 147b8c8516aSEivind Eklund DOCHECK(); 148df8bae1dSRodney W. Grimes 149df8bae1dSRodney W. Grimes cr = p->p_ucred; 150df8bae1dSRodney W. Grimes 151b8c8516aSEivind Eklund ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " %lu %lu %lu", 152ac1e407bSBruce Evans (u_long)cr->cr_uid, 153ac1e407bSBruce Evans (u_long)p->p_cred->p_ruid, 154ac1e407bSBruce Evans (u_long)p->p_cred->p_rgid); 155b8c8516aSEivind Eklund DOCHECK(); 1561d08058fSWolfram Schneider 1571d08058fSWolfram Schneider /* egid (p->p_cred->p_svgid) is equal to cr_ngroups[0] 1581d08058fSWolfram Schneider see also getegid(2) in /sys/kern/kern_prot.c */ 1591d08058fSWolfram Schneider 160b8c8516aSEivind Eklund for (i = 0; i < cr->cr_ngroups; i++) { 161b8c8516aSEivind Eklund ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, 162b8c8516aSEivind Eklund ",%lu", (u_long)cr->cr_groups[i]); 163b8c8516aSEivind Eklund DOCHECK(); 164b8c8516aSEivind Eklund } 16575c13541SPoul-Henning Kamp 16691421ba2SRobert Watson if (jailed(p->p_ucred)) 167b8c8516aSEivind Eklund ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, 16891421ba2SRobert Watson " %s", p->p_ucred->cr_prison->pr_host); 16975c13541SPoul-Henning Kamp else 170b8c8516aSEivind Eklund ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " -"); 171b8c8516aSEivind Eklund DOCHECK(); 172b8c8516aSEivind Eklund ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "\n"); 173b8c8516aSEivind Eklund DOCHECK(); 174df8bae1dSRodney W. Grimes 175df8bae1dSRodney W. Grimes xlen = ps - psbuf; 176df8bae1dSRodney W. Grimes xlen -= uio->uio_offset; 177df8bae1dSRodney W. Grimes ps = psbuf + uio->uio_offset; 178996c772fSJohn Dyson xlen = imin(xlen, uio->uio_resid); 179df8bae1dSRodney W. Grimes if (xlen <= 0) 180df8bae1dSRodney W. Grimes error = 0; 181df8bae1dSRodney W. Grimes else 182df8bae1dSRodney W. Grimes error = uiomove(ps, xlen, uio); 183df8bae1dSRodney W. Grimes 184df8bae1dSRodney W. Grimes return (error); 185b8c8516aSEivind Eklund 186b8c8516aSEivind Eklund bailout: 187b8c8516aSEivind Eklund return (ENOMEM); 188df8bae1dSRodney W. Grimes } 18975ba7757SPeter Wemm 19075ba7757SPeter Wemm int 19175ba7757SPeter Wemm procfs_docmdline(curp, p, pfs, uio) 19275ba7757SPeter Wemm struct proc *curp; 19375ba7757SPeter Wemm struct proc *p; 19475ba7757SPeter Wemm struct pfsnode *pfs; 19575ba7757SPeter Wemm struct uio *uio; 19675ba7757SPeter Wemm { 19775ba7757SPeter Wemm char *ps; 19875ba7757SPeter Wemm int xlen; 19975ba7757SPeter Wemm int error; 2006153cb20SPoul-Henning Kamp char *buf, *bp; 2016153cb20SPoul-Henning Kamp int buflen; 20263a99273SMarcel Moolenaar struct ps_strings pstr; 20363a99273SMarcel Moolenaar int i; 20463a99273SMarcel Moolenaar size_t bytes_left, done; 20563a99273SMarcel Moolenaar 20675ba7757SPeter Wemm if (uio->uio_rw != UIO_READ) 20775ba7757SPeter Wemm return (EOPNOTSUPP); 20875ba7757SPeter Wemm 20975ba7757SPeter Wemm /* 2106153cb20SPoul-Henning Kamp * If we are using the ps/cmdline caching, use that. Otherwise 2116153cb20SPoul-Henning Kamp * revert back to the old way which only implements full cmdline 2126153cb20SPoul-Henning Kamp * for the currept process and just p->p_comm for all other 2136153cb20SPoul-Henning Kamp * processes. 21463a99273SMarcel Moolenaar * Note that if the argv is no longer available, we deliberately 21563a99273SMarcel Moolenaar * don't fall back on p->p_comm or return an error: the authentic 21663a99273SMarcel Moolenaar * Linux behaviour is to return zero-length in this case. 21775ba7757SPeter Wemm */ 21863a99273SMarcel Moolenaar 219387d2c03SRobert Watson if (p->p_args && (ps_argsopen || !p_can(curp, p, P_CAN_SEE, NULL))) { 2206153cb20SPoul-Henning Kamp bp = p->p_args->ar_args; 2216153cb20SPoul-Henning Kamp buflen = p->p_args->ar_length; 2226153cb20SPoul-Henning Kamp buf = 0; 2236153cb20SPoul-Henning Kamp } else if (p != curp) { 2246153cb20SPoul-Henning Kamp bp = p->p_comm; 2256153cb20SPoul-Henning Kamp buflen = MAXCOMLEN; 2266153cb20SPoul-Henning Kamp buf = 0; 2276153cb20SPoul-Henning Kamp } else { 2286153cb20SPoul-Henning Kamp buflen = 256; 2296153cb20SPoul-Henning Kamp MALLOC(buf, char *, buflen + 1, M_TEMP, M_WAITOK); 2306153cb20SPoul-Henning Kamp bp = buf; 2316153cb20SPoul-Henning Kamp ps = buf; 23263a99273SMarcel Moolenaar error = copyin((void*)PS_STRINGS, &pstr, sizeof(pstr)); 2336153cb20SPoul-Henning Kamp if (error) { 2346153cb20SPoul-Henning Kamp FREE(buf, M_TEMP); 23563a99273SMarcel Moolenaar return (error); 2366153cb20SPoul-Henning Kamp } 2376153cb20SPoul-Henning Kamp bytes_left = buflen; 23863a99273SMarcel Moolenaar for (i = 0; bytes_left && (i < pstr.ps_nargvstr); i++) { 23963a99273SMarcel Moolenaar error = copyinstr(pstr.ps_argvstr[i], ps, 24063a99273SMarcel Moolenaar bytes_left, &done); 24163a99273SMarcel Moolenaar /* If too long or malformed, just truncate */ 24263a99273SMarcel Moolenaar if (error) { 24363a99273SMarcel Moolenaar error = 0; 24463a99273SMarcel Moolenaar break; 24563a99273SMarcel Moolenaar } 24663a99273SMarcel Moolenaar ps += done; 24763a99273SMarcel Moolenaar bytes_left -= done; 24863a99273SMarcel Moolenaar } 2496153cb20SPoul-Henning Kamp buflen = ps - buf; 25063a99273SMarcel Moolenaar } 25175ba7757SPeter Wemm 2526153cb20SPoul-Henning Kamp buflen -= uio->uio_offset; 2536153cb20SPoul-Henning Kamp ps = bp + uio->uio_offset; 2546153cb20SPoul-Henning Kamp xlen = min(buflen, uio->uio_resid); 25575ba7757SPeter Wemm if (xlen <= 0) 25675ba7757SPeter Wemm error = 0; 25775ba7757SPeter Wemm else 25875ba7757SPeter Wemm error = uiomove(ps, xlen, uio); 2596153cb20SPoul-Henning Kamp if (buf) 2606153cb20SPoul-Henning Kamp FREE(buf, M_TEMP); 26175ba7757SPeter Wemm return (error); 26275ba7757SPeter Wemm } 263