Merge from vendor branch FILE:
[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  * $DragonFly: src/sys/vfs/procfs/procfs_status.c,v 1.11 2005/11/14 18:50:13 dillon 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_SWAPPEDOUT) {
123                 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
124                     " -1,-1 -1,-1 -1,-1");
125         } else {
126                 struct timeval ut, st;
127
128                 calcru(p, &ut, &st, (struct timeval *) NULL);
129                 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
130                     " %ld,%ld %ld,%ld %ld,%ld",
131                     p->p_start.tv_sec,
132                     p->p_start.tv_usec,
133                     ut.tv_sec, ut.tv_usec,
134                     st.tv_sec, st.tv_usec);
135         }
136         DOCHECK();
137
138         ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " %s",
139                 (p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan");
140         DOCHECK();
141
142         cr = p->p_ucred;
143
144         ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " %lu %lu %lu", 
145                 (u_long)cr->cr_uid,
146                 (u_long)p->p_ucred->cr_ruid,
147                 (u_long)p->p_ucred->cr_rgid);
148         DOCHECK();
149
150         /* egid (p->p_ucred->cr_svgid) is equal to cr_ngroups[0] 
151            see also getegid(2) in /sys/kern/kern_prot.c */
152
153         for (i = 0; i < cr->cr_ngroups; i++) {
154                 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
155                     ",%lu", (u_long)cr->cr_groups[i]);
156                 DOCHECK();
157         }
158
159         if (p->p_ucred->cr_prison)
160                 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps,
161                     " %s", p->p_ucred->cr_prison->pr_host);
162         else
163                 ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, " -");
164         DOCHECK();
165         ps += snprintf(ps, psbuf + sizeof(psbuf) - ps, "\n");
166         DOCHECK();
167
168         xlen = ps - psbuf;
169         xlen -= uio->uio_offset;
170         ps = psbuf + uio->uio_offset;
171         xlen = imin(xlen, uio->uio_resid);
172         if (xlen <= 0)
173                 error = 0;
174         else
175                 error = uiomove_frombuf(ps, xlen, uio);
176
177         return (error);
178
179 bailout:
180         return (ENOMEM);
181 }
182
183 int
184 procfs_docmdline(struct proc *curp, struct proc *p, struct pfsnode *pfs,
185                  struct uio *uio)
186 {
187         char *ps;
188         int xlen;
189         int error;
190         char *buf, *bp;
191         int buflen;
192         struct ps_strings pstr;
193         char **ps_argvstr;
194         int i;
195         size_t bytes_left, done;
196
197         if (uio->uio_rw != UIO_READ)
198                 return (EOPNOTSUPP);
199         
200         /*
201          * If we are using the ps/cmdline caching, use that.  Otherwise
202          * revert back to the old way which only implements full cmdline
203          * for the currept process and just p->p_comm for all other
204          * processes.
205          * Note that if the argv is no longer available, we deliberately
206          * don't fall back on p->p_comm or return an error: the authentic
207          * Linux behaviour is to return zero-length in this case.
208          */
209
210         if (p->p_args &&
211             (ps_argsopen || (CHECKIO(curp, p) &&
212              (p->p_flag & P_INEXEC) == 0 && !p_trespass(curp->p_ucred, p->p_ucred)))
213          ) {
214                 bp = p->p_args->ar_args;
215                 buflen = p->p_args->ar_length;
216                 buf = 0;
217         } else if (p != curp) {
218                 bp = p->p_comm;
219                 buflen = MAXCOMLEN;
220                 buf = 0;
221         } else {
222                 buflen = 256;
223                 MALLOC(buf, char *, buflen + 1, M_TEMP, M_WAITOK);
224                 bp = buf;
225                 ps = buf;
226                 error = copyin((void*)PS_STRINGS, &pstr, sizeof(pstr));
227                 if (error) {
228                         FREE(buf, M_TEMP);
229                         return (error);
230                 }
231                 if (pstr.ps_nargvstr > ARG_MAX) {
232                         FREE(buf, M_TEMP);
233                         return (E2BIG);
234                 }
235                 MALLOC(ps_argvstr, char **, pstr.ps_nargvstr * sizeof(char *),
236                     M_TEMP, M_WAITOK);
237                 error = copyin((void *)pstr.ps_argvstr, ps_argvstr,
238                     pstr.ps_nargvstr * sizeof(char *));
239                 if (error) {
240                         FREE(ps_argvstr, M_TEMP);
241                         FREE(buf, M_TEMP);
242                         return (error);
243                 }
244                 bytes_left = buflen;
245                 for (i = 0; bytes_left && (i < pstr.ps_nargvstr); i++) {
246                         error = copyinstr(ps_argvstr[i], ps,
247                                           bytes_left, &done);
248                         /* If too long or malformed, just truncate */
249                         if (error) {
250                                 error = 0;
251                                 break;
252                         }
253                         ps += done;
254                         bytes_left -= done;
255                 }
256                 buflen = ps - buf;
257                 FREE(ps_argvstr, M_TEMP);
258         }
259
260         buflen -= uio->uio_offset;
261         ps = bp + uio->uio_offset;
262         xlen = min(buflen, uio->uio_resid);
263         if (xlen <= 0)
264                 error = 0;
265         else
266                 error = uiomove_frombuf(bp, buflen, uio);
267         if (buf)
268                 FREE(buf, M_TEMP);
269         return (error);
270 }