procfs_status.c revision 113620
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 3.1 1993/12/15 09:40:17 jsp Exp $
41 * $FreeBSD: head/sys/fs/procfs/procfs_status.c 113620 2003-04-17 22:16:58Z jhb $
42 */
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/exec.h>
47#include <sys/lock.h>
48#include <sys/mutex.h>
49#include <sys/jail.h>
50#include <sys/malloc.h>
51#include <sys/mutex.h>
52#include <sys/sx.h>
53#include <sys/proc.h>
54#include <sys/resourcevar.h>
55#include <sys/sbuf.h>
56#include <sys/sysent.h>
57#include <sys/tty.h>
58
59#include <vm/vm.h>
60#include <vm/pmap.h>
61#include <vm/vm_param.h>
62
63#include <fs/pseudofs/pseudofs.h>
64#include <fs/procfs/procfs.h>
65
66int
67procfs_doprocstatus(PFS_FILL_ARGS)
68{
69	struct session *sess;
70	struct thread *tdfirst;
71	struct tty *tp;
72	struct ucred *cr;
73	const char *wmesg;
74	char *pc;
75	char *sep;
76	int pid, ppid, pgid, sid;
77	int i;
78
79	pid = p->p_pid;
80	PROC_LOCK(p);
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	SESS_LOCK(sess);
85	sid = sess->s_leader ? sess->s_leader->p_pid : 0;
86
87/* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg
88                                euid ruid rgid,egid,groups[1 .. NGROUPS]
89*/
90
91	pc = p->p_comm;
92	do {
93		if (*pc < 33 || *pc > 126 || *pc == '\\')
94			sbuf_printf(sb, "\\%03o", *pc);
95		else
96			sbuf_putc(sb, *pc);
97	} while (*++pc);
98	sbuf_printf(sb, " %d %d %d %d ", pid, ppid, pgid, sid);
99	if ((p->p_flag & P_CONTROLT) && (tp = sess->s_ttyp))
100		sbuf_printf(sb, "%d,%d ", major(tp->t_dev), minor(tp->t_dev));
101	else
102		sbuf_printf(sb, "%d,%d ", -1, -1);
103
104	sep = "";
105	if (sess->s_ttyvp) {
106		sbuf_printf(sb, "%sctty", sep);
107		sep = ",";
108	}
109	if (SESS_LEADER(p)) {
110		sbuf_printf(sb, "%ssldr", sep);
111		sep = ",";
112	}
113	SESS_UNLOCK(sess);
114	if (*sep != ',') {
115		sbuf_printf(sb, "noflags");
116	}
117
118	mtx_lock_spin(&sched_lock);
119	if (p->p_flag & P_THREADED)
120		wmesg = "-kse- ";
121	else {
122		tdfirst = FIRST_THREAD_IN_PROC(p);
123		if (tdfirst->td_wchan != NULL) {
124			KASSERT(tdfirst->td_wmesg != NULL,
125			    ("wchan %p has no wmesg", tdfirst->td_wchan));
126			wmesg = tdfirst->td_wmesg;
127		} else
128			wmesg = "nochan";
129	}
130
131	if (p->p_sflag & PS_INMEM) {
132		struct timeval ut, st;
133
134		calcru(p, &ut, &st, (struct timeval *) NULL);
135		mtx_unlock_spin(&sched_lock);
136		sbuf_printf(sb, " %lld,%ld %ld,%ld %ld,%ld",
137		    (long long)p->p_stats->p_start.tv_sec,
138		    p->p_stats->p_start.tv_usec,
139		    ut.tv_sec, ut.tv_usec,
140		    st.tv_sec, st.tv_usec);
141	} else {
142		mtx_unlock_spin(&sched_lock);
143		sbuf_printf(sb, " -1,-1 -1,-1 -1,-1");
144	}
145
146	sbuf_printf(sb, " %s", wmesg);
147
148	cr = p->p_ucred;
149
150	sbuf_printf(sb, " %lu %lu %lu",
151		(u_long)cr->cr_uid,
152		(u_long)cr->cr_ruid,
153		(u_long)cr->cr_rgid);
154
155	/* egid (cr->cr_svgid) is equal to cr_ngroups[0]
156	   see also getegid(2) in /sys/kern/kern_prot.c */
157
158	for (i = 0; i < cr->cr_ngroups; i++) {
159		sbuf_printf(sb, ",%lu", (u_long)cr->cr_groups[i]);
160	}
161
162	if (jailed(p->p_ucred)) {
163		mtx_lock(&p->p_ucred->cr_prison->pr_mtx);
164		sbuf_printf(sb, " %s", p->p_ucred->cr_prison->pr_host);
165		mtx_unlock(&p->p_ucred->cr_prison->pr_mtx);
166	} else {
167		sbuf_printf(sb, " -");
168	}
169	PROC_UNLOCK(p);
170	sbuf_printf(sb, "\n");
171
172	return (0);
173}
174
175int
176procfs_doproccmdline(PFS_FILL_ARGS)
177{
178	struct ps_strings pstr;
179	int error, i;
180
181	/*
182	 * If we are using the ps/cmdline caching, use that.  Otherwise
183	 * revert back to the old way which only implements full cmdline
184	 * for the currept process and just p->p_comm for all other
185	 * processes.
186	 * Note that if the argv is no longer available, we deliberately
187	 * don't fall back on p->p_comm or return an error: the authentic
188	 * Linux behaviour is to return zero-length in this case.
189	 */
190
191	PROC_LOCK(p);
192	if (p->p_args && (ps_argsopen || !p_cansee(td, p))) {
193		sbuf_bcpy(sb, p->p_args->ar_args, p->p_args->ar_length);
194		PROC_UNLOCK(p);
195		return (0);
196	}
197	PROC_UNLOCK(p);
198	if (p != td->td_proc) {
199		sbuf_printf(sb, "%.*s", MAXCOMLEN, p->p_comm);
200	} else {
201		error = copyin((void *)p->p_sysent->sv_psstrings, &pstr,
202		    sizeof(pstr));
203		if (error)
204			return (error);
205		for (i = 0; i < pstr.ps_nargvstr; i++) {
206			sbuf_copyin(sb, pstr.ps_argvstr[i], 0);
207			sbuf_printf(sb, "%c", '\0');
208		}
209	}
210
211	return (0);
212}
213