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