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