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