]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/fs/procfs/procfs.c
[CDN-Patch] sys/fs/procfs: extend procfs to support proc/self, proc/pid/cwd, proc...
[FreeBSD/releng/8.1.git] / sys / fs / procfs / procfs.c
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 #include <sys/filedesc.h>
64 #include <sys/jail.h>
65
66 /*
67  * Filler function for proc/pid/cwd
68  * Copied verbatim from src/sys/compat/linprocfs/linprocfs.c,v 1.119
69  */
70 int
71 procfs_doproccwd(PFS_FILL_ARGS)
72 {
73         char *fullpath = "unknown";
74         char *freepath = NULL;
75
76         vn_fullpath(td, p->p_fd->fd_cdir, &fullpath, &freepath);
77         sbuf_printf(sb, "%s", fullpath);
78         if (freepath)
79                 free(freepath, M_TEMP);
80         return (0);
81 }
82
83 /*
84  * Filler function for proc/pid/root
85  * Copied verbatim from src/sys/compat/linprocfs/linprocfs.c,v 1.119
86  */
87 int
88 procfs_doprocroot(PFS_FILL_ARGS)
89 {
90         struct vnode *rvp;
91         char *fullpath = "unknown";
92         char *freepath = NULL;
93
94         rvp = jailed(p->p_ucred) ? p->p_fd->fd_jdir : p->p_fd->fd_rdir;
95         vn_fullpath(td, rvp, &fullpath, &freepath);
96         sbuf_printf(sb, "%s", fullpath);
97         if (freepath)
98                 free(freepath, M_TEMP);
99         return (0);
100 }
101
102 /*
103  * Filler function for proc/pid/self
104  */
105 int
106 procfs_doprocfile(PFS_FILL_ARGS)
107 {
108         char *fullpath = "unknown";
109         char *freepath = NULL;
110         struct vnode *textvp;
111
112         PROC_LOCK(p);
113         textvp = p->p_textvp;
114         vhold(textvp);
115         PROC_UNLOCK(p);
116         vn_fullpath(td, textvp, &fullpath, &freepath);
117         vdrop(textvp);
118         sbuf_printf(sb, "%s", fullpath);
119         if (freepath)
120                 free(freepath, M_TEMP);
121         return (0);
122 }
123
124 /*
125  * Filler function for proc/curproc
126  */
127 int
128 procfs_docurproc(PFS_FILL_ARGS)
129 {
130         sbuf_printf(sb, "%ld", (long)td->td_proc->p_pid);
131         return (0);
132 }
133
134 /*
135  * Adjust mode for some nodes that need it
136  */
137 int
138 procfs_attr(PFS_ATTR_ARGS)
139 {
140
141         /* XXX inefficient, split into separate functions */
142         if (strcmp(pn->pn_name, "ctl") == 0 ||
143             strcmp(pn->pn_name, "note") == 0 ||
144             strcmp(pn->pn_name, "notepg") == 0)
145                 vap->va_mode = 0200;
146         else if (strcmp(pn->pn_name, "mem") == 0 ||
147             strcmp(pn->pn_name, "regs") == 0 ||
148             strcmp(pn->pn_name, "dbregs") == 0 ||
149             strcmp(pn->pn_name, "fpregs") == 0 ||
150             strcmp(pn->pn_name, "osrel") == 0)
151                 vap->va_mode = 0600;
152
153         if (p != NULL) {
154                 PROC_LOCK_ASSERT(p, MA_OWNED);
155
156                 if ((p->p_flag & P_SUGID) && pn->pn_type != pfstype_procdir)
157                         vap->va_mode = 0;
158         }
159
160         return (0);
161 }
162
163 /*
164  * Visibility: some files only exist for non-system processes
165  * Non-static because linprocfs uses it.
166  */
167 int
168 procfs_notsystem(PFS_VIS_ARGS)
169 {
170         PROC_LOCK_ASSERT(p, MA_OWNED);
171         return ((p->p_flag & P_SYSTEM) == 0);
172 }
173
174 /*
175  * Visibility: some files are only visible to process that can debug
176  * the target process.
177  */
178 int
179 procfs_candebug(PFS_VIS_ARGS)
180 {
181         PROC_LOCK_ASSERT(p, MA_OWNED);
182         return ((p->p_flag & P_SYSTEM) == 0 && p_candebug(td, p) == 0);
183 }
184
185 /*
186  * Constructor
187  */
188 static int
189 procfs_init(PFS_INIT_ARGS)
190 {
191         struct pfs_node *root;
192         struct pfs_node *dir;
193         struct pfs_node *node;
194
195         root = pi->pi_root;
196
197         pfs_create_link(root, "curproc", procfs_docurproc,
198             NULL, NULL, NULL, 0);
199         pfs_create_link(root, "self", procfs_docurproc,
200             NULL, NULL, NULL, 0);
201
202         dir = pfs_create_dir(root, "pid",
203             procfs_attr, NULL, NULL, PFS_PROCDEP);
204         pfs_create_file(dir, "cmdline", procfs_doproccmdline,
205             NULL, NULL, NULL, PFS_RD);
206         pfs_create_file(dir, "ctl", procfs_doprocctl,
207             procfs_attr, NULL, NULL, PFS_WR);
208         pfs_create_file(dir, "dbregs", procfs_doprocdbregs,
209             procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
210         pfs_create_file(dir, "etype", procfs_doproctype,
211             NULL, NULL, NULL, PFS_RD);
212         pfs_create_file(dir, "fpregs", procfs_doprocfpregs,
213             procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
214         pfs_create_file(dir, "map", procfs_doprocmap,
215             NULL, procfs_notsystem, NULL, PFS_RD);
216         node = pfs_create_file(dir, "mem", procfs_doprocmem,
217             procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
218         node->pn_ioctl = procfs_ioctl;
219         node->pn_close = procfs_close;
220         pfs_create_file(dir, "note", procfs_doprocnote,
221             procfs_attr, procfs_candebug, NULL, PFS_WR);
222         pfs_create_file(dir, "notepg", procfs_doprocnote,
223             procfs_attr, procfs_candebug, NULL, PFS_WR);
224         pfs_create_file(dir, "regs", procfs_doprocregs,
225             procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
226         pfs_create_file(dir, "rlimit", procfs_doprocrlimit,
227             NULL, NULL, NULL, PFS_RD);
228         pfs_create_file(dir, "status", procfs_doprocstatus,
229             NULL, NULL, NULL, PFS_RD);
230         pfs_create_file(dir, "osrel", procfs_doosrel,
231             procfs_attr, procfs_candebug, NULL, PFS_RDWR);
232
233         pfs_create_link(dir, "file", procfs_doprocfile,
234             NULL, procfs_notsystem, NULL, 0);
235         pfs_create_link(dir, "exe", procfs_doprocfile,
236             NULL, procfs_notsystem, NULL, 0);
237
238         pfs_create_link(dir, "cwd", &procfs_doproccwd,
239             NULL, NULL, NULL, 0);
240         pfs_create_link(dir, "root", &procfs_doprocroot,
241             NULL, NULL, NULL, 0);
242
243         return (0);
244 }
245
246 /*
247  * Destructor
248  */
249 static int
250 procfs_uninit(PFS_INIT_ARGS)
251 {
252         /* nothing to do, pseudofs will GC */
253         return (0);
254 }
255
256 PSEUDOFS(procfs, 1);