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/proc.h> 46 #include <sys/jail.h> 47 #include <sys/vnode.h> 48 #include <sys/tty.h> 49 #include <sys/resourcevar.h> 50 #include <miscfs/procfs/procfs.h> 51 52 #include <vm/vm.h> 53 #include <vm/pmap.h> 54 #include <vm/vm_param.h> 55 #include <sys/exec.h> 56 57 int 58 procfs_dostatus(curp, p, pfs, uio) 59 struct proc *curp; 60 struct proc *p; 61 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 ps = psbuf; 88 bcopy(p->p_comm, ps, MAXCOMLEN); 89 ps[MAXCOMLEN] = '\0'; 90 ps += strlen(ps); 91 ps += sprintf(ps, " %d %d %d %d ", pid, ppid, pgid, sid); 92 93 if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp)) 94 ps += sprintf(ps, "%d,%d ", major(tp->t_dev), minor(tp->t_dev)); 95 else 96 ps += sprintf(ps, "%d,%d ", -1, -1); 97 98 sep = ""; 99 if (sess->s_ttyvp) { 100 ps += sprintf(ps, "%sctty", sep); 101 sep = ","; 102 } 103 if (SESS_LEADER(p)) { 104 ps += sprintf(ps, "%ssldr", sep); 105 sep = ","; 106 } 107 if (*sep != ',') 108 ps += sprintf(ps, "noflags"); 109 110 if (p->p_flag & P_INMEM) { 111 struct timeval ut, st; 112 113 calcru(p, &ut, &st, (struct timeval *) NULL); 114 ps += sprintf(ps, " %ld,%ld %ld,%ld %ld,%ld", 115 p->p_stats->p_start.tv_sec, 116 p->p_stats->p_start.tv_usec, 117 ut.tv_sec, ut.tv_usec, 118 st.tv_sec, st.tv_usec); 119 } else 120 ps += sprintf(ps, " -1,-1 -1,-1 -1,-1"); 121 122 ps += sprintf(ps, " %s", 123 (p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan"); 124 125 cr = p->p_ucred; 126 127 ps += sprintf(ps, " %lu %lu %lu", 128 (u_long)cr->cr_uid, 129 (u_long)p->p_cred->p_ruid, 130 (u_long)p->p_cred->p_rgid); 131 132 /* egid (p->p_cred->p_svgid) is equal to cr_ngroups[0] 133 see also getegid(2) in /sys/kern/kern_prot.c */ 134 135 for (i = 0; i < cr->cr_ngroups; i++) 136 ps += sprintf(ps, ",%lu", (u_long)cr->cr_groups[i]); 137 138 if (p->p_prison) 139 ps += sprintf(ps, " %s", p->p_prison->pr_host); 140 else 141 ps += sprintf(ps, " -"); 142 ps += sprintf(ps, "\n"); 143 144 xlen = ps - psbuf; 145 xlen -= uio->uio_offset; 146 ps = psbuf + uio->uio_offset; 147 xlen = imin(xlen, uio->uio_resid); 148 if (xlen <= 0) 149 error = 0; 150 else 151 error = uiomove(ps, xlen, uio); 152 153 return (error); 154 } 155 156 int 157 procfs_docmdline(curp, p, pfs, uio) 158 struct proc *curp; 159 struct proc *p; 160 struct pfsnode *pfs; 161 struct uio *uio; 162 { 163 char *ps; 164 int xlen; 165 int error; 166 char psbuf[256]; 167 168 struct ps_strings pstr; 169 int i; 170 size_t bytes_left, done; 171 172 if (uio->uio_rw != UIO_READ) 173 return (EOPNOTSUPP); 174 175 /* 176 * This is a hack: the correct behaviour is only implemented for 177 * the case of the current process enquiring about its own argv 178 * (due to the difficulty of accessing other processes' address space). 179 * For other cases, we cop out and just return argv[0] from p->p_comm. 180 * Note that if the argv is no longer available, we deliberately 181 * don't fall back on p->p_comm or return an error: the authentic 182 * Linux behaviour is to return zero-length in this case. 183 */ 184 185 if (curproc == p) { 186 error = copyin((void*)PS_STRINGS, &pstr, sizeof(pstr)); 187 if (error) 188 return (error); 189 bytes_left = sizeof(psbuf); 190 ps = psbuf; 191 for (i = 0; bytes_left && (i < pstr.ps_nargvstr); i++) { 192 error = copyinstr(pstr.ps_argvstr[i], ps, 193 bytes_left, &done); 194 /* If too long or malformed, just truncate */ 195 if (error) { 196 error = 0; 197 break; 198 } 199 ps += done; 200 bytes_left -= done; 201 } 202 } 203 else { 204 ps = psbuf; 205 bcopy(p->p_comm, ps, MAXCOMLEN); 206 ps[MAXCOMLEN] = '\0'; 207 ps += strlen(ps); 208 } 209 210 xlen = ps - psbuf; 211 xlen -= uio->uio_offset; 212 ps = psbuf + uio->uio_offset; 213 xlen = min(xlen, uio->uio_resid); 214 if (xlen <= 0) 215 error = 0; 216 else 217 error = uiomove(ps, xlen, uio); 218 return (error); 219 } 220