tty_tty.c revision 179519
1268899Sbapt/*-
2234949Sbapt * Copyright (c) 2003 Poul-Henning Kamp.  All rights reserved.
3234949Sbapt *
4234949Sbapt * Redistribution and use in source and binary forms, with or without
5234949Sbapt * modification, are permitted provided that the following conditions
6234949Sbapt * are met:
7234949Sbapt * 1. Redistributions of source code must retain the above copyright
8234949Sbapt *    notice, this list of conditions and the following disclaimer.
9234949Sbapt * 2. Redistributions in binary form must reproduce the above copyright
10234949Sbapt *    notice, this list of conditions and the following disclaimer in the
11234949Sbapt *    documentation and/or other materials provided with the distribution.
12234949Sbapt *
13234949Sbapt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14234949Sbapt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15234949Sbapt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16234949Sbapt * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17234949Sbapt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18234949Sbapt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19234949Sbapt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20234949Sbapt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21234949Sbapt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22234949Sbapt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23234949Sbapt * SUCH DAMAGE.
24234949Sbapt */
25234949Sbapt
26234949Sbapt#include <sys/cdefs.h>
27234949Sbapt__FBSDID("$FreeBSD: head/sys/kern/tty_tty.c 179519 2008-06-03 12:38:00Z ed $");
28234949Sbapt
29234949Sbapt#include <sys/param.h>
30234949Sbapt#include <sys/systm.h>
31234949Sbapt#include <sys/conf.h>
32234949Sbapt#include <sys/kernel.h>
33234949Sbapt#include <sys/proc.h>
34234949Sbapt#include <sys/sx.h>
35234949Sbapt#include <sys/vnode.h>
36234949Sbapt
37234949Sbapt#include <fs/devfs/devfs.h>
38234949Sbapt#include <fs/devfs/devfs_int.h>
39234949Sbapt
40234949Sbaptstatic	d_open_t	cttyopen;
41234949Sbapt
42234949Sbaptstatic struct cdevsw ctty_cdevsw = {
43234949Sbapt	.d_version =	D_VERSION,
44234949Sbapt	.d_open =	cttyopen,
45268899Sbapt	.d_name =	"ctty",
46234949Sbapt};
47268899Sbapt
48234949Sbaptstatic struct cdev *ctty;
49234949Sbapt
50234949Sbaptstatic	int
51234949Sbaptcttyopen(struct cdev *dev, int flag, int mode, struct thread *td)
52234949Sbapt{
53234949Sbapt
54234949Sbapt	return (ENXIO);
55234949Sbapt}
56234949Sbapt
57234949Sbaptstatic void
58234949Sbaptctty_clone(void *arg, struct ucred *cred, char *name, int namelen,
59234949Sbapt    struct cdev **dev)
60234949Sbapt{
61234949Sbapt
62234949Sbapt	if (*dev != NULL)
63234949Sbapt		return;
64234949Sbapt	if (strcmp(name, "tty"))
65234949Sbapt		return;
66234949Sbapt	sx_sunlock(&clone_drain_lock);
67234949Sbapt	sx_slock(&proctree_lock);
68234949Sbapt	sx_slock(&clone_drain_lock);
69234949Sbapt	dev_lock();
70234949Sbapt	if (!(curthread->td_proc->p_flag & P_CONTROLT))
71234949Sbapt		*dev = ctty;
72234949Sbapt	else if (curthread->td_proc->p_session->s_ttyvp == NULL)
73234949Sbapt		*dev = ctty;
74234949Sbapt	else if (curthread->td_proc->p_session->s_ttyvp->v_type == VBAD ||
75234949Sbapt	    curthread->td_proc->p_session->s_ttyvp->v_rdev == NULL) {
76234949Sbapt		/* e.g. s_ttyvp was revoked */
77234949Sbapt		*dev = ctty;
78234949Sbapt	} else
79234949Sbapt		*dev = curthread->td_proc->p_session->s_ttyvp->v_rdev;
80234949Sbapt	dev_refl(*dev);
81234949Sbapt	dev_unlock();
82234949Sbapt	sx_sunlock(&proctree_lock);
83234949Sbapt}
84234949Sbapt
85234949Sbaptstatic void
86234949Sbaptctty_drvinit(void *unused)
87234949Sbapt{
88234949Sbapt
89234949Sbapt	EVENTHANDLER_REGISTER(dev_clone, ctty_clone, 0, 1000);
90234949Sbapt	ctty = make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "ctty");
91234949Sbapt}
92234949Sbapt
93234949SbaptSYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,ctty_drvinit,NULL);
94234949Sbapt