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