xref: /freebsd/sys/fs/procfs/procfs.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/exec.h>
44 #include <sys/lock.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/proc.h>
50 #include <sys/sbuf.h>
51 #include <sys/sysproto.h>
52 #include <sys/systm.h>
53 #include <sys/vnode.h>
54 
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_param.h>
58 
59 #include <fs/pseudofs/pseudofs.h>
60 #include <fs/procfs/procfs.h>
61 
62 /*
63  * Filler function for proc/pid/file
64  */
65 int
66 procfs_doprocfile(PFS_FILL_ARGS)
67 {
68 	char *fullpath, *freepath, *binpath;
69 	int error;
70 
71 	freepath = NULL;
72 	binpath = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
73 	PROC_LOCK(p);
74 	error = proc_get_binpath(p, binpath, &fullpath, &freepath);
75 	if (error == 0)
76 		sbuf_cat(sb, fullpath);
77 	free(binpath, M_TEMP);
78 	free(freepath, M_TEMP);
79 	return (error);
80 }
81 
82 /*
83  * Filler function for proc/curproc
84  */
85 int
86 procfs_docurproc(PFS_FILL_ARGS)
87 {
88 	sbuf_printf(sb, "%ld", (long)td->td_proc->p_pid);
89 	return (0);
90 }
91 
92 static int
93 procfs_attr(PFS_ATTR_ARGS, int mode) {
94 	vap->va_mode = mode;
95 	if (p != NULL) {
96 		PROC_LOCK_ASSERT(p, MA_OWNED);
97 
98 		if ((p->p_flag & P_SUGID) && pn->pn_type != pfstype_procdir)
99 			vap->va_mode = 0;
100 	}
101 
102 	return (0);
103 }
104 
105 int
106 procfs_attr_all_rx(PFS_ATTR_ARGS)
107 {
108 
109 	return (procfs_attr(td, p, pn, vap, 0555));
110 }
111 
112 int
113 procfs_attr_rw(PFS_ATTR_ARGS)
114 {
115 
116 	return (procfs_attr(td, p, pn, vap, 0600));
117 }
118 
119 int
120 procfs_attr_w(PFS_ATTR_ARGS)
121 {
122 
123 	return (procfs_attr(td, p, pn, vap, 0200));
124 }
125 
126 /*
127  * Visibility: some files only exist for non-system processes
128  * Non-static because linprocfs uses it.
129  */
130 int
131 procfs_notsystem(PFS_VIS_ARGS)
132 {
133 	PROC_LOCK_ASSERT(p, MA_OWNED);
134 	return ((p->p_flag & P_SYSTEM) == 0);
135 }
136 
137 /*
138  * Visibility: some files are only visible to process that can debug
139  * the target process.
140  */
141 int
142 procfs_candebug(PFS_VIS_ARGS)
143 {
144 	PROC_LOCK_ASSERT(p, MA_OWNED);
145 	return ((p->p_flag & P_SYSTEM) == 0 && p_candebug(td, p) == 0);
146 }
147 
148 /*
149  * Constructor
150  */
151 static int
152 procfs_init(PFS_INIT_ARGS)
153 {
154 	struct pfs_node *root;
155 	struct pfs_node *dir;
156 
157 	root = pi->pi_root;
158 
159 	pfs_create_link(root, "curproc", procfs_docurproc,
160 	    NULL, NULL, NULL, 0);
161 	pfs_create_link(root, "self", procfs_docurproc,
162 	    NULL, NULL, NULL, 0);
163 
164 	dir = pfs_create_dir(root, "pid",
165 	    procfs_attr_all_rx, NULL, NULL, PFS_PROCDEP);
166 	pfs_create_file(dir, "cmdline", procfs_doproccmdline,
167 	    NULL, NULL, NULL, PFS_RD);
168 	pfs_create_file(dir, "dbregs", procfs_doprocdbregs,
169 	    procfs_attr_rw, procfs_candebug, NULL, PFS_RDWR | PFS_RAW);
170 	pfs_create_file(dir, "etype", procfs_doproctype,
171 	    NULL, NULL, NULL, PFS_RD);
172 	pfs_create_file(dir, "fpregs", procfs_doprocfpregs,
173 	    procfs_attr_rw, procfs_candebug, NULL, PFS_RDWR | PFS_RAW);
174 	pfs_create_file(dir, "map", procfs_doprocmap,
175 	    NULL, procfs_notsystem, NULL, PFS_RD);
176 	pfs_create_file(dir, "mem", procfs_doprocmem,
177 	    procfs_attr_rw, procfs_candebug, NULL, PFS_RDWR | PFS_RAW);
178 	pfs_create_file(dir, "note", procfs_doprocnote,
179 	    procfs_attr_w, procfs_candebug, NULL, PFS_WR);
180 	pfs_create_file(dir, "notepg", procfs_doprocnote,
181 	    procfs_attr_w, procfs_candebug, NULL, PFS_WR);
182 	pfs_create_file(dir, "regs", procfs_doprocregs,
183 	    procfs_attr_rw, procfs_candebug, NULL, PFS_RDWR | PFS_RAW);
184 	pfs_create_file(dir, "rlimit", procfs_doprocrlimit,
185 	    NULL, NULL, NULL, PFS_RD);
186 	pfs_create_file(dir, "status", procfs_doprocstatus,
187 	    NULL, NULL, NULL, PFS_RD);
188 	pfs_create_file(dir, "osrel", procfs_doosrel,
189 	    procfs_attr_rw, procfs_candebug, NULL, PFS_RDWR);
190 
191 	pfs_create_link(dir, "file", procfs_doprocfile,
192 	    NULL, procfs_notsystem, NULL, 0);
193 	pfs_create_link(dir, "exe", procfs_doprocfile,
194 	    NULL, procfs_notsystem, NULL, 0);
195 
196 	return (0);
197 }
198 
199 /*
200  * Destructor
201  */
202 static int
203 procfs_uninit(PFS_INIT_ARGS)
204 {
205 	/* nothing to do, pseudofs will GC */
206 	return (0);
207 }
208 
209 PSEUDOFS(procfs, 1, VFCF_JAIL);
210