xref: /dragonfly/sys/vfs/procfs/procfs_status.c (revision 7ced307156514cbd160672bba63420da0e492a05)
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/caps.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
procfs_dostatus(struct proc * curp,struct lwp * lp,struct pfsnode * pfs,struct uio * uio)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           {
127                     struct rusage ru;
128 
129                     calcru_proc(p, &ru);
130                     ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps,
131                         " %ld,%ld %ld,%ld %ld,%ld",
132                         p->p_start.tv_sec,
133                         p->p_start.tv_usec,
134                         ru.ru_utime.tv_sec, ru.ru_utime.tv_usec,
135                         ru.ru_stime.tv_sec, ru.ru_stime.tv_usec);
136           }
137           DOCHECK();
138 
139           ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, " %s",
140                     (lp->lwp_wchan && lp->lwp_wmesg) ? lp->lwp_wmesg : "nochan");
141           DOCHECK();
142 
143           cr = p->p_ucred;
144 
145           ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, " %lu %lu %lu",
146                     (u_long)cr->cr_uid,
147                     (u_long)p->p_ucred->cr_ruid,
148                     (u_long)p->p_ucred->cr_rgid);
149           DOCHECK();
150 
151           /* egid (p->p_ucred->cr_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 += ksnprintf(ps, psbuf + sizeof(psbuf) - ps,
156                         ",%lu", (u_long)cr->cr_groups[i]);
157                     DOCHECK();
158           }
159 
160           if (p->p_ucred->cr_prison)
161                     ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps,
162                         " %s", p->p_ucred->cr_prison->pr_host);
163           else
164                     ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, " -");
165           DOCHECK();
166           ps += ksnprintf(ps, psbuf + sizeof(psbuf) - ps, "\n");
167           DOCHECK();
168 
169           xlen = ps - psbuf;
170           error = uiomove_frombuf(psbuf, xlen, uio);
171 
172 bailout:
173           return (error);
174 }
175 
176 int
procfs_docmdline(struct proc * curp,struct lwp * lp,struct pfsnode * pfs,struct uio * uio)177 procfs_docmdline(struct proc *curp, struct lwp *lp, struct pfsnode *pfs,
178                      struct uio *uio)
179 {
180           struct proc *p = lp->lwp_proc;
181           char *ps;
182           int error;
183           char *buf, *bp;
184           struct ps_strings pstr;
185           char **ps_argvstr;
186           int i;
187           size_t bytes_left, done;
188           size_t buflen;
189 
190           if (uio->uio_rw != UIO_READ)
191                     return (EOPNOTSUPP);
192 
193           /*
194            * If we are using the ps/cmdline caching, use that.  Otherwise
195            * revert back to the old way which only implements full cmdline
196            * for the currept process and just p->p_comm for all other
197            * processes.
198            * Note that if the argv is no longer available, we deliberately
199            * don't fall back on p->p_comm or return an error: the authentic
200            * Linux behaviour is to return zero-length in this case.
201            */
202           if (lp->lwp_lpmap != NULL && lp->lwp_lpmap->thread_title[0] &&
203               (ps_argsopen || (CHECKIO(curp, p) &&
204                                    (p->p_flags & P_INEXEC) == 0 &&
205                                    !p_trespass(curp->p_ucred, p->p_ucred))
206               )) {
207                     /*
208                      * Args set via writable thread mmap.
209                      *
210                      * We must calculate the string length manually
211                      * because the user data can change at any time.
212                      */
213                     bp = lp->lwp_lpmap->thread_title;
214                     for (buflen = 0; buflen < UPMAP_MAXPROCTITLE - 1; ++buflen) {
215                               if (bp[buflen] == 0)
216                                         break;
217                     }
218                     buf = NULL;
219           } else if (p->p_upmap != NULL && p->p_upmap->proc_title[0] &&
220                        (ps_argsopen || (CHECKIO(curp, p) &&
221                                             (p->p_flags & P_INEXEC) == 0 &&
222                                             !p_trespass(curp->p_ucred, p->p_ucred))
223               )) {
224                     /*
225                      * Args set via writable user process mmap.
226                      *
227                      * We must calculate the string length manually
228                      * because the user data can change at any time.
229                      */
230                     bp = p->p_upmap->proc_title;
231                     for (buflen = 0; buflen < UPMAP_MAXPROCTITLE - 1; ++buflen) {
232                               if (bp[buflen] == 0)
233                                         break;
234                     }
235                     buf = NULL;
236           } else if (p->p_args &&
237                        (ps_argsopen || (CHECKIO(curp, p) &&
238                                             (p->p_flags & P_INEXEC) == 0 &&
239                                             !p_trespass(curp->p_ucred, p->p_ucred))
240                        )) {
241                     bp = p->p_args->ar_args;
242                     buflen = p->p_args->ar_length;
243                     buf = NULL;
244           } else if (p != curp) {
245                     bp = p->p_comm;
246                     buflen = MAXCOMLEN;
247                     buf = NULL;
248           } else {
249                     buflen = 256;
250                     buf = kmalloc(buflen + 1, M_TEMP, M_WAITOK);
251                     bp = buf;
252                     ps = buf;
253                     error = copyin((void*)PS_STRINGS, &pstr, sizeof(pstr));
254 
255                     if (error) {
256                               kfree(buf, M_TEMP);
257                               return (error);
258                     }
259                     if (pstr.ps_nargvstr < 0) {
260                               kfree(buf, M_TEMP);
261                               return (EINVAL);
262                     }
263                     if (pstr.ps_nargvstr > ARG_MAX) {
264                               kfree(buf, M_TEMP);
265                               return (E2BIG);
266                     }
267                     ps_argvstr = kmalloc(pstr.ps_nargvstr * sizeof(char *),
268                                              M_TEMP, M_WAITOK);
269                     error = copyin((void *)pstr.ps_argvstr, ps_argvstr,
270                                      pstr.ps_nargvstr * sizeof(char *));
271                     if (error) {
272                               kfree(ps_argvstr, M_TEMP);
273                               kfree(buf, M_TEMP);
274                               return (error);
275                     }
276                     bytes_left = buflen;
277                     for (i = 0; bytes_left && (i < pstr.ps_nargvstr); i++) {
278                               error = copyinstr(ps_argvstr[i], ps,
279                                                     bytes_left, &done);
280                               /* If too long or malformed, just truncate */
281                               if (error) {
282                                         error = 0;
283                                         break;
284                               }
285                               ps += done;
286                               bytes_left -= done;
287                     }
288                     buflen = ps - buf;
289                     kfree(ps_argvstr, M_TEMP);
290           }
291 
292           error = uiomove_frombuf(bp, buflen, uio);
293           if (buf)
294                     kfree(buf, M_TEMP);
295           return (error);
296 }
297