2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 2001 Dag-Erling Smørgrav
5 * Copyright (c) 1993 Jan-Simon Pendry
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
40 * @(#)procfs_vfsops.c 8.7 (Berkeley) 5/10/95
45 #include <sys/param.h>
46 #include <sys/queue.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/mutex.h>
55 #include <sys/sysproto.h>
56 #include <sys/systm.h>
57 #include <sys/vnode.h>
61 #include <vm/vm_param.h>
63 #include <fs/pseudofs/pseudofs.h>
64 #include <fs/procfs/procfs.h>
67 * Filler function for proc/pid/file
70 procfs_doprocfile(PFS_FILL_ARGS)
82 error = vn_fullpath(td, textvp, &fullpath, &freepath);
85 sbuf_printf(sb, "%s", fullpath);
87 free(freepath, M_TEMP);
92 * Filler function for proc/curproc
95 procfs_docurproc(PFS_FILL_ARGS)
97 sbuf_printf(sb, "%ld", (long)td->td_proc->p_pid);
102 * Adjust mode for some nodes that need it
105 procfs_attr(PFS_ATTR_ARGS)
108 /* XXX inefficient, split into separate functions */
109 if (strcmp(pn->pn_name, "note") == 0 ||
110 strcmp(pn->pn_name, "notepg") == 0)
112 else if (strcmp(pn->pn_name, "mem") == 0 ||
113 strcmp(pn->pn_name, "regs") == 0 ||
114 strcmp(pn->pn_name, "dbregs") == 0 ||
115 strcmp(pn->pn_name, "fpregs") == 0 ||
116 strcmp(pn->pn_name, "osrel") == 0)
120 PROC_LOCK_ASSERT(p, MA_OWNED);
122 if ((p->p_flag & P_SUGID) && pn->pn_type != pfstype_procdir)
130 * Visibility: some files only exist for non-system processes
131 * Non-static because linprocfs uses it.
134 procfs_notsystem(PFS_VIS_ARGS)
136 PROC_LOCK_ASSERT(p, MA_OWNED);
137 return ((p->p_flag & P_SYSTEM) == 0);
141 * Visibility: some files are only visible to process that can debug
142 * the target process.
145 procfs_candebug(PFS_VIS_ARGS)
147 PROC_LOCK_ASSERT(p, MA_OWNED);
148 return ((p->p_flag & P_SYSTEM) == 0 && p_candebug(td, p) == 0);
155 procfs_init(PFS_INIT_ARGS)
157 struct pfs_node *root;
158 struct pfs_node *dir;
159 struct pfs_node *node;
163 pfs_create_link(root, "curproc", procfs_docurproc,
164 NULL, NULL, NULL, 0);
166 dir = pfs_create_dir(root, "pid",
167 procfs_attr, NULL, NULL, PFS_PROCDEP);
168 pfs_create_file(dir, "cmdline", procfs_doproccmdline,
169 NULL, NULL, NULL, PFS_RD);
170 pfs_create_file(dir, "dbregs", procfs_doprocdbregs,
171 procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
172 pfs_create_file(dir, "etype", procfs_doproctype,
173 NULL, NULL, NULL, PFS_RD);
174 pfs_create_file(dir, "fpregs", procfs_doprocfpregs,
175 procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
176 pfs_create_file(dir, "map", procfs_doprocmap,
177 NULL, procfs_notsystem, NULL, PFS_RD);
178 node = pfs_create_file(dir, "mem", procfs_doprocmem,
179 procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
180 node->pn_ioctl = procfs_ioctl;
181 node->pn_close = procfs_close;
182 pfs_create_file(dir, "note", procfs_doprocnote,
183 procfs_attr, procfs_candebug, NULL, PFS_WR);
184 pfs_create_file(dir, "notepg", procfs_doprocnote,
185 procfs_attr, procfs_candebug, NULL, PFS_WR);
186 pfs_create_file(dir, "regs", procfs_doprocregs,
187 procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
188 pfs_create_file(dir, "rlimit", procfs_doprocrlimit,
189 NULL, NULL, NULL, PFS_RD);
190 pfs_create_file(dir, "status", procfs_doprocstatus,
191 NULL, NULL, NULL, PFS_RD);
192 pfs_create_file(dir, "osrel", procfs_doosrel,
193 procfs_attr, procfs_candebug, NULL, PFS_RDWR);
195 pfs_create_link(dir, "file", procfs_doprocfile,
196 NULL, procfs_notsystem, NULL, 0);
205 procfs_uninit(PFS_INIT_ARGS)
207 /* nothing to do, pseudofs will GC */
211 PSEUDOFS(procfs, 1, PR_ALLOW_MOUNT_PROCFS);