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$
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;
71	char *freepath;
72	struct vnode *textvp;
73	int error;
74
75	freepath = NULL;
76	PROC_LOCK(p);
77	textvp = p->p_textvp;
78	vhold(textvp);
79	PROC_UNLOCK(p);
80	error = vn_fullpath(td, textvp, &fullpath, &freepath);
81	vdrop(textvp);
82	if (error == 0)
83		sbuf_printf(sb, "%s", fullpath);
84	if (freepath != NULL)
85		free(freepath, M_TEMP);
86	return (error);
87}
88
89/*
90 * Filler function for proc/curproc
91 */
92int
93procfs_docurproc(PFS_FILL_ARGS)
94{
95	sbuf_printf(sb, "%ld", (long)td->td_proc->p_pid);
96	return (0);
97}
98
99/*
100 * Adjust mode for some nodes that need it
101 */
102int
103procfs_attr(PFS_ATTR_ARGS)
104{
105
106	/* XXX inefficient, split into separate functions */
107	if (strcmp(pn->pn_name, "ctl") == 0 ||
108	    strcmp(pn->pn_name, "note") == 0 ||
109	    strcmp(pn->pn_name, "notepg") == 0)
110		vap->va_mode = 0200;
111	else if (strcmp(pn->pn_name, "mem") == 0 ||
112	    strcmp(pn->pn_name, "regs") == 0 ||
113	    strcmp(pn->pn_name, "dbregs") == 0 ||
114	    strcmp(pn->pn_name, "fpregs") == 0 ||
115	    strcmp(pn->pn_name, "osrel") == 0)
116		vap->va_mode = 0600;
117
118	if (p != NULL) {
119		PROC_LOCK_ASSERT(p, MA_OWNED);
120
121		if ((p->p_flag & P_SUGID) && pn->pn_type != pfstype_procdir)
122			vap->va_mode = 0;
123	}
124
125	return (0);
126}
127
128/*
129 * Visibility: some files only exist for non-system processes
130 * Non-static because linprocfs uses it.
131 */
132int
133procfs_notsystem(PFS_VIS_ARGS)
134{
135	PROC_LOCK_ASSERT(p, MA_OWNED);
136	return ((p->p_flag & P_SYSTEM) == 0);
137}
138
139/*
140 * Visibility: some files are only visible to process that can debug
141 * the target process.
142 */
143int
144procfs_candebug(PFS_VIS_ARGS)
145{
146	PROC_LOCK_ASSERT(p, MA_OWNED);
147	return ((p->p_flag & P_SYSTEM) == 0 && p_candebug(td, p) == 0);
148}
149
150/*
151 * Constructor
152 */
153static int
154procfs_init(PFS_INIT_ARGS)
155{
156	struct pfs_node *root;
157	struct pfs_node *dir;
158	struct pfs_node *node;
159
160	root = pi->pi_root;
161
162	pfs_create_link(root, "curproc", procfs_docurproc,
163	    NULL, NULL, NULL, 0);
164
165	dir = pfs_create_dir(root, "pid",
166	    procfs_attr, NULL, NULL, PFS_PROCDEP);
167	pfs_create_file(dir, "cmdline", procfs_doproccmdline,
168	    NULL, NULL, NULL, PFS_RD);
169	pfs_create_file(dir, "ctl", procfs_doprocctl,
170	    procfs_attr, NULL, NULL, PFS_WR);
171	pfs_create_file(dir, "dbregs", procfs_doprocdbregs,
172	    procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
173	pfs_create_file(dir, "etype", procfs_doproctype,
174	    NULL, NULL, NULL, PFS_RD);
175	pfs_create_file(dir, "fpregs", procfs_doprocfpregs,
176	    procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
177	pfs_create_file(dir, "map", procfs_doprocmap,
178	    NULL, procfs_notsystem, NULL, PFS_RD);
179	node = pfs_create_file(dir, "mem", procfs_doprocmem,
180	    procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
181	node->pn_ioctl = procfs_ioctl;
182	node->pn_close = procfs_close;
183	pfs_create_file(dir, "note", procfs_doprocnote,
184	    procfs_attr, procfs_candebug, NULL, PFS_WR);
185	pfs_create_file(dir, "notepg", procfs_doprocnote,
186	    procfs_attr, procfs_candebug, NULL, PFS_WR);
187	pfs_create_file(dir, "regs", procfs_doprocregs,
188	    procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
189	pfs_create_file(dir, "rlimit", procfs_doprocrlimit,
190	    NULL, NULL, NULL, PFS_RD);
191	pfs_create_file(dir, "status", procfs_doprocstatus,
192	    NULL, NULL, NULL, PFS_RD);
193	pfs_create_file(dir, "osrel", procfs_doosrel,
194	    procfs_attr, procfs_candebug, NULL, PFS_RDWR);
195
196	pfs_create_link(dir, "file", procfs_doprocfile,
197	    NULL, procfs_notsystem, NULL, 0);
198
199	return (0);
200}
201
202/*
203 * Destructor
204 */
205static int
206procfs_uninit(PFS_INIT_ARGS)
207{
208	/* nothing to do, pseudofs will GC */
209	return (0);
210}
211
212PSEUDOFS(procfs, 1, PR_ALLOW_MOUNT_PROCFS);
213