1139776Simp/*-
2230132Suqs * Copyright (c) 2001 Dag-Erling Sm��rgrav
387321Sdes * Copyright (c) 1993 Jan-Simon Pendry
487321Sdes * Copyright (c) 1993
587321Sdes *	The Regents of the University of California.  All rights reserved.
687321Sdes *
787321Sdes * This code is derived from software contributed to Berkeley by
887321Sdes * Jan-Simon Pendry.
987321Sdes *
1087321Sdes * Redistribution and use in source and binary forms, with or without
1187321Sdes * modification, are permitted provided that the following conditions
1287321Sdes * are met:
1387321Sdes * 1. Redistributions of source code must retain the above copyright
1487321Sdes *    notice, this list of conditions and the following disclaimer.
1587321Sdes * 2. Redistributions in binary form must reproduce the above copyright
1687321Sdes *    notice, this list of conditions and the following disclaimer in the
1787321Sdes *    documentation and/or other materials provided with the distribution.
1887321Sdes * 3. All advertising materials mentioning features or use of this software
1987321Sdes *    must display the following acknowledgement:
2087321Sdes *	This product includes software developed by the University of
2187321Sdes *	California, Berkeley and its contributors.
2287321Sdes * 4. Neither the name of the University nor the names of its contributors
2387321Sdes *    may be used to endorse or promote products derived from this software
2487321Sdes *    without specific prior written permission.
2587321Sdes *
2687321Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2787321Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2887321Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2987321Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3087321Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3187321Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3287321Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3387321Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3487321Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3587321Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3687321Sdes * SUCH DAMAGE.
3787321Sdes *
3887321Sdes *	@(#)procfs_vfsops.c	8.7 (Berkeley) 5/10/95
3987321Sdes *
4087321Sdes * $FreeBSD$
4187321Sdes */
4287321Sdes
4387321Sdes#include <sys/param.h>
4487321Sdes#include <sys/queue.h>
4587321Sdes#include <sys/exec.h>
4687321Sdes#include <sys/lock.h>
4787321Sdes#include <sys/kernel.h>
4887321Sdes#include <sys/malloc.h>
4987321Sdes#include <sys/mount.h>
5087321Sdes#include <sys/mutex.h>
5187321Sdes#include <sys/proc.h>
5287321Sdes#include <sys/sbuf.h>
5387321Sdes#include <sys/sysproto.h>
5487321Sdes#include <sys/systm.h>
5587321Sdes#include <sys/vnode.h>
5687321Sdes
5787321Sdes#include <vm/vm.h>
5887321Sdes#include <vm/pmap.h>
5987321Sdes#include <vm/vm_param.h>
6087321Sdes
6187321Sdes#include <fs/pseudofs/pseudofs.h>
6287321Sdes#include <fs/procfs/procfs.h>
6387321Sdes
6487321Sdes/*
6587321Sdes * Filler function for proc/pid/self
6687321Sdes */
6787538Sdesint
6887321Sdesprocfs_doprocfile(PFS_FILL_ARGS)
6987321Sdes{
70224915Skib	char *fullpath;
71224915Skib	char *freepath;
72166548Skib	struct vnode *textvp;
73224915Skib	int error;
7487321Sdes
75224915Skib	freepath = NULL;
76184652Sjhb	PROC_LOCK(p);
77166548Skib	textvp = p->p_textvp;
78184652Sjhb	vhold(textvp);
79184652Sjhb	PROC_UNLOCK(p);
80224915Skib	error = vn_fullpath(td, textvp, &fullpath, &freepath);
81166548Skib	vdrop(textvp);
82224915Skib	if (error == 0)
83224915Skib		sbuf_printf(sb, "%s", fullpath);
84224915Skib	if (freepath != NULL)
8587321Sdes		free(freepath, M_TEMP);
86224915Skib	return (error);
8787321Sdes}
8887321Sdes
8987321Sdes/*
9087321Sdes * Filler function for proc/curproc
9187321Sdes */
9287538Sdesint
9387321Sdesprocfs_docurproc(PFS_FILL_ARGS)
9487321Sdes{
9587321Sdes	sbuf_printf(sb, "%ld", (long)td->td_proc->p_pid);
9687321Sdes	return (0);
9787321Sdes}
9887321Sdes
9987321Sdes/*
10087321Sdes * Adjust mode for some nodes that need it
10187321Sdes */
10287321Sdesint
10387321Sdesprocfs_attr(PFS_ATTR_ARGS)
10487321Sdes{
105113617Sjhb
10687321Sdes	/* XXX inefficient, split into separate functions */
107159283Sghelmer	if (strcmp(pn->pn_name, "ctl") == 0 ||
10887321Sdes	    strcmp(pn->pn_name, "note") == 0 ||
10987321Sdes	    strcmp(pn->pn_name, "notepg") == 0)
11087321Sdes		vap->va_mode = 0200;
11187321Sdes	else if (strcmp(pn->pn_name, "mem") == 0 ||
11287321Sdes	    strcmp(pn->pn_name, "regs") == 0 ||
11387321Sdes	    strcmp(pn->pn_name, "dbregs") == 0 ||
114197428Skib	    strcmp(pn->pn_name, "fpregs") == 0 ||
115197428Skib	    strcmp(pn->pn_name, "osrel") == 0)
11687321Sdes		vap->va_mode = 0600;
11787321Sdes
118188677Sdes	if (p != NULL) {
119188677Sdes		PROC_LOCK_ASSERT(p, MA_OWNED);
120159283Sghelmer
121188677Sdes		if ((p->p_flag & P_SUGID) && pn->pn_type != pfstype_procdir)
122188677Sdes			vap->va_mode = 0;
123188677Sdes	}
124123247Sdes
12587321Sdes	return (0);
12687321Sdes}
12787321Sdes
12887321Sdes/*
12987321Sdes * Visibility: some files only exist for non-system processes
13087321Sdes * Non-static because linprocfs uses it.
13187321Sdes */
13287321Sdesint
13387321Sdesprocfs_notsystem(PFS_VIS_ARGS)
13487321Sdes{
135113617Sjhb	PROC_LOCK_ASSERT(p, MA_OWNED);
13687321Sdes	return ((p->p_flag & P_SYSTEM) == 0);
13787321Sdes}
13887321Sdes
13987321Sdes/*
14087321Sdes * Visibility: some files are only visible to process that can debug
14187321Sdes * the target process.
14287321Sdes */
14387321Sdesint
14487321Sdesprocfs_candebug(PFS_VIS_ARGS)
14587321Sdes{
14696886Sjhb	PROC_LOCK_ASSERT(p, MA_OWNED);
147123247Sdes	return ((p->p_flag & P_SYSTEM) == 0 && p_candebug(td, p) == 0);
14887321Sdes}
14987321Sdes
15087321Sdes/*
15187321Sdes * Constructor
15287321Sdes */
15387321Sdesstatic int
15487321Sdesprocfs_init(PFS_INIT_ARGS)
15587321Sdes{
15687321Sdes	struct pfs_node *root;
15787321Sdes	struct pfs_node *dir;
15887321Sdes	struct pfs_node *node;
15987321Sdes
16087321Sdes	root = pi->pi_root;
16187321Sdes
162105560Sphk	pfs_create_link(root, "curproc", procfs_docurproc,
163167482Sdes	    NULL, NULL, NULL, 0);
164123247Sdes
16587321Sdes	dir = pfs_create_dir(root, "pid",
166167482Sdes	    procfs_attr, NULL, NULL, PFS_PROCDEP);
167105560Sphk	pfs_create_file(dir, "cmdline", procfs_doproccmdline,
168167482Sdes	    NULL, NULL, NULL, PFS_RD);
169105560Sphk	pfs_create_file(dir, "ctl", procfs_doprocctl,
170167482Sdes	    procfs_attr, NULL, NULL, PFS_WR);
171105560Sphk	pfs_create_file(dir, "dbregs", procfs_doprocdbregs,
172167482Sdes	    procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
173105560Sphk	pfs_create_file(dir, "etype", procfs_doproctype,
174167482Sdes	    NULL, NULL, NULL, PFS_RD);
175105560Sphk	pfs_create_file(dir, "fpregs", procfs_doprocfpregs,
176167482Sdes	    procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
177105560Sphk	pfs_create_file(dir, "map", procfs_doprocmap,
178167482Sdes	    NULL, procfs_notsystem, NULL, PFS_RD);
179105560Sphk	node = pfs_create_file(dir, "mem", procfs_doprocmem,
180167482Sdes	    procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
181105560Sphk	node->pn_ioctl = procfs_ioctl;
182105560Sphk	node->pn_close = procfs_close;
183105560Sphk	pfs_create_file(dir, "note", procfs_doprocnote,
184167482Sdes	    procfs_attr, procfs_candebug, NULL, PFS_WR);
185105560Sphk	pfs_create_file(dir, "notepg", procfs_doprocnote,
186167482Sdes	    procfs_attr, procfs_candebug, NULL, PFS_WR);
187105560Sphk	pfs_create_file(dir, "regs", procfs_doprocregs,
188167482Sdes	    procfs_attr, procfs_candebug, NULL, PFS_RDWR|PFS_RAW);
189105560Sphk	pfs_create_file(dir, "rlimit", procfs_doprocrlimit,
190167482Sdes	    NULL, NULL, NULL, PFS_RD);
191105560Sphk	pfs_create_file(dir, "status", procfs_doprocstatus,
192167482Sdes	    NULL, NULL, NULL, PFS_RD);
193197428Skib	pfs_create_file(dir, "osrel", procfs_doosrel,
194197428Skib	    procfs_attr, procfs_candebug, NULL, PFS_RDWR);
195123247Sdes
196105560Sphk	pfs_create_link(dir, "file", procfs_doprocfile,
197167482Sdes	    NULL, procfs_notsystem, NULL, 0);
198123247Sdes
19987321Sdes	return (0);
20087321Sdes}
20187321Sdes
20287321Sdes/*
20387321Sdes * Destructor
20487321Sdes */
20587321Sdesstatic int
20687321Sdesprocfs_uninit(PFS_INIT_ARGS)
20787321Sdes{
20887321Sdes	/* nothing to do, pseudofs will GC */
20987321Sdes	return (0);
21087321Sdes}
21187321Sdes
212232278SmmPSEUDOFS(procfs, 1, PR_ALLOW_MOUNT_PROCFS);
213