procfs.c revision 124219
1/*
2 * Copyright (c) 2001 Dag-Erling Sm�rgrav
3 * Copyright (c) 1993 Jan-Simon Pendry
4 * Copyright (c) 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Jan-Simon Pendry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	@(#)procfs_vfsops.c	8.7 (Berkeley) 5/10/95
39 *
40 * $FreeBSD: head/sys/fs/procfs/procfs.c 124219 2004-01-07 17:58:51Z rwatson $
41 */
42
43#include <sys/param.h>
44#include <sys/queue.h>
45#include <sys/exec.h>
46#include <sys/lock.h>
47#include <sys/kernel.h>
48#include <sys/malloc.h>
49#include <sys/mount.h>
50#include <sys/mutex.h>
51#include <sys/proc.h>
52#include <sys/sbuf.h>
53#include <sys/sysproto.h>
54#include <sys/systm.h>
55#include <sys/vnode.h>
56
57#include <vm/vm.h>
58#include <vm/pmap.h>
59#include <vm/vm_param.h>
60
61#include <fs/pseudofs/pseudofs.h>
62#include <fs/procfs/procfs.h>
63
64/*
65 * Filler function for proc/pid/self
66 */
67int
68procfs_doprocfile(PFS_FILL_ARGS)
69{
70	char *fullpath = "unknown";
71	char *freepath = NULL;
72
73	vn_lock(p->p_textvp, LK_EXCLUSIVE | LK_RETRY, td);
74	vn_fullpath(td, p->p_textvp, &fullpath, &freepath);
75	VOP_UNLOCK(p->p_textvp, 0, td);
76	sbuf_printf(sb, "%s", fullpath);
77	if (freepath)
78		free(freepath, M_TEMP);
79	return (0);
80}
81
82/*
83 * Filler function for proc/curproc
84 */
85int
86procfs_docurproc(PFS_FILL_ARGS)
87{
88	sbuf_printf(sb, "%ld", (long)td->td_proc->p_pid);
89	return (0);
90}
91
92/*
93 * Adjust mode for some nodes that need it
94 */
95int
96procfs_attr(PFS_ATTR_ARGS)
97{
98	PROC_LOCK_ASSERT(p, MA_OWNED);
99
100	/* XXX inefficient, split into separate functions */
101	if (p->p_flag & P_SUGID)
102		vap->va_mode = 0;
103	else if (strcmp(pn->pn_name, "ctl") == 0 ||
104	    strcmp(pn->pn_name, "note") == 0 ||
105	    strcmp(pn->pn_name, "notepg") == 0)
106		vap->va_mode = 0200;
107	else if (strcmp(pn->pn_name, "mem") == 0 ||
108	    strcmp(pn->pn_name, "regs") == 0 ||
109	    strcmp(pn->pn_name, "dbregs") == 0 ||
110	    strcmp(pn->pn_name, "fpregs") == 0)
111		vap->va_mode = 0600;
112
113	vap->va_uid = p->p_ucred->cr_uid;
114	vap->va_gid = p->p_ucred->cr_gid;
115
116	return (0);
117}
118
119/*
120 * Visibility: some files only exist for non-system processes
121 * Non-static because linprocfs uses it.
122 */
123int
124procfs_notsystem(PFS_VIS_ARGS)
125{
126	PROC_LOCK_ASSERT(p, MA_OWNED);
127	return ((p->p_flag & P_SYSTEM) == 0);
128}
129
130/*
131 * Visibility: some files are only visible to process that can debug
132 * the target process.
133 */
134int
135procfs_candebug(PFS_VIS_ARGS)
136{
137	PROC_LOCK_ASSERT(p, MA_OWNED);
138	return ((p->p_flag & P_SYSTEM) == 0 && p_candebug(td, p) == 0);
139}
140
141/*
142 * Constructor
143 */
144static int
145procfs_init(PFS_INIT_ARGS)
146{
147	struct pfs_node *root;
148	struct pfs_node *dir;
149	struct pfs_node *node;
150
151	root = pi->pi_root;
152
153	pfs_create_link(root, "curproc", procfs_docurproc,
154	    NULL, NULL, 0);
155
156	dir = pfs_create_dir(root, "pid",
157	    procfs_attr, NULL, PFS_PROCDEP);
158	pfs_create_file(dir, "cmdline", procfs_doproccmdline,
159	    NULL, NULL, PFS_RD);
160	pfs_create_file(dir, "ctl", procfs_doprocctl,
161	    procfs_attr, NULL, PFS_WR);
162	pfs_create_file(dir, "dbregs", procfs_doprocdbregs,
163	    procfs_attr, procfs_candebug, PFS_RDWR|PFS_RAW);
164	pfs_create_file(dir, "etype", procfs_doproctype,
165	    NULL, NULL, PFS_RD);
166	pfs_create_file(dir, "fpregs", procfs_doprocfpregs,
167	    procfs_attr, procfs_candebug, PFS_RDWR|PFS_RAW);
168	pfs_create_file(dir, "map", procfs_doprocmap,
169	    NULL, procfs_notsystem, PFS_RD);
170	node = pfs_create_file(dir, "mem", procfs_doprocmem,
171	    procfs_attr, procfs_candebug, PFS_RDWR|PFS_RAW);
172	node->pn_ioctl = procfs_ioctl;
173	node->pn_close = procfs_close;
174	pfs_create_file(dir, "note", procfs_doprocnote,
175	    procfs_attr, procfs_candebug, PFS_WR);
176	pfs_create_file(dir, "notepg", procfs_doprocnote,
177	    procfs_attr, procfs_candebug, PFS_WR);
178	pfs_create_file(dir, "regs", procfs_doprocregs,
179	    procfs_attr, procfs_candebug, PFS_RDWR|PFS_RAW);
180	pfs_create_file(dir, "rlimit", procfs_doprocrlimit,
181	    NULL, NULL, PFS_RD);
182	pfs_create_file(dir, "status", procfs_doprocstatus,
183	    NULL, NULL, PFS_RD);
184
185	pfs_create_link(dir, "file", procfs_doprocfile,
186	    NULL, procfs_notsystem, 0);
187
188	return (0);
189}
190
191/*
192 * Destructor
193 */
194static int
195procfs_uninit(PFS_INIT_ARGS)
196{
197	/* nothing to do, pseudofs will GC */
198	return (0);
199}
200
201PSEUDOFS(procfs, 1);
202