procfs_status.c revision 46155
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 *	$Id: procfs_status.c,v 1.12 1999/01/05 03:53:06 peter Exp $
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
52int
53procfs_dostatus(curp, p, pfs, uio)
54	struct proc *curp;
55	struct proc *p;
56	struct pfsnode *pfs;
57	struct uio *uio;
58{
59	struct session *sess;
60	struct tty *tp;
61	struct ucred *cr;
62	char *ps;
63	char *sep;
64	int pid, ppid, pgid, sid;
65	int i;
66	int xlen;
67	int error;
68	char psbuf[256];		/* XXX - conservative */
69
70	if (uio->uio_rw != UIO_READ)
71		return (EOPNOTSUPP);
72
73	pid = p->p_pid;
74	ppid = p->p_pptr ? p->p_pptr->p_pid : 0,
75	pgid = p->p_pgrp->pg_id;
76	sess = p->p_pgrp->pg_session;
77	sid = sess->s_leader ? sess->s_leader->p_pid : 0;
78
79/* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg
80                                euid ruid rgid,egid,groups[1 .. NGROUPS]
81*/
82	ps = psbuf;
83	bcopy(p->p_comm, ps, MAXCOMLEN);
84	ps[MAXCOMLEN] = '\0';
85	ps += strlen(ps);
86	ps += sprintf(ps, " %d %d %d %d ", pid, ppid, pgid, sid);
87
88	if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp))
89		ps += sprintf(ps, "%d,%d ", major(tp->t_dev), minor(tp->t_dev));
90	else
91		ps += sprintf(ps, "%d,%d ", -1, -1);
92
93	sep = "";
94	if (sess->s_ttyvp) {
95		ps += sprintf(ps, "%sctty", sep);
96		sep = ",";
97	}
98	if (SESS_LEADER(p)) {
99		ps += sprintf(ps, "%ssldr", sep);
100		sep = ",";
101	}
102	if (*sep != ',')
103		ps += sprintf(ps, "noflags");
104
105	if (p->p_flag & P_INMEM)
106		ps += sprintf(ps, " %ld,%ld",
107			p->p_stats->p_start.tv_sec,
108			p->p_stats->p_start.tv_usec);
109	else
110		ps += sprintf(ps, " -1,-1");
111
112	{
113		struct timeval ut, st;
114
115		calcru(p, &ut, &st, (void *) 0);
116		ps += sprintf(ps, " %ld,%ld %ld,%ld",
117			ut.tv_sec,
118			ut.tv_usec,
119			st.tv_sec,
120			st.tv_usec);
121	}
122
123	ps += sprintf(ps, " %s",
124		(p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan");
125
126	cr = p->p_ucred;
127
128	ps += sprintf(ps, " %lu %lu %lu",
129		(u_long)cr->cr_uid,
130		(u_long)p->p_cred->p_ruid,
131		(u_long)p->p_cred->p_rgid);
132
133	/* egid (p->p_cred->p_svgid) is equal to cr_ngroups[0]
134	   see also getegid(2) in /sys/kern/kern_prot.c */
135
136	for (i = 0; i < cr->cr_ngroups; i++)
137		ps += sprintf(ps, ",%lu", (u_long)cr->cr_groups[i]);
138
139	if (p->p_prison)
140		ps += sprintf(ps, " %s", p->p_prison->pr_host);
141	else
142		ps += sprintf(ps, " -");
143	ps += sprintf(ps, "\n");
144
145	xlen = ps - psbuf;
146	xlen -= uio->uio_offset;
147	ps = psbuf + uio->uio_offset;
148	xlen = imin(xlen, uio->uio_resid);
149	if (xlen <= 0)
150		error = 0;
151	else
152		error = uiomove(ps, xlen, uio);
153
154	return (error);
155}
156
157int
158procfs_docmdline(curp, p, pfs, uio)
159	struct proc *curp;
160	struct proc *p;
161	struct pfsnode *pfs;
162	struct uio *uio;
163{
164	char *ps;
165	int xlen;
166	int error;
167	char psbuf[256];
168
169	if (uio->uio_rw != UIO_READ)
170		return (EOPNOTSUPP);
171
172	/*
173	 * For now, this is a hack.  To implement this fully would require
174	 * groping around in the process address space to follow argv etc.
175	 */
176	ps = psbuf;
177	bcopy(p->p_comm, ps, MAXCOMLEN);
178	ps[MAXCOMLEN] = '\0';
179	ps += strlen(ps);
180
181	ps += sprintf(ps, "\n");
182
183	xlen = ps - psbuf;
184	xlen -= uio->uio_offset;
185	ps = psbuf + uio->uio_offset;
186	xlen = min(xlen, uio->uio_resid);
187	if (xlen <= 0)
188		error = 0;
189	else
190		error = uiomove(ps, xlen, uio);
191	return (error);
192}
193