4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
34 * Indirect driver for controlling tty.
36 #include <sys/types.h>
37 #include <sys/errno.h>
41 #include <sys/stream.h>
42 #include <sys/strsubr.h>
45 #include <sys/session.h>
47 #include <sys/debug.h>
49 #include <sys/sunddi.h>
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/modctl.h>
53 #include <sys/fs/snode.h>
56 #define IS_STREAM(dev) (devopsp[getmajor(dev)]->devo_cb_ops->cb_str != NULL)
58 int syopen(dev_t
*, int, int, cred_t
*);
59 int syclose(dev_t
, int, int, cred_t
*);
60 int syread(dev_t
, struct uio
*, cred_t
*);
61 int sywrite(dev_t
, struct uio
*, cred_t
*);
62 int sypoll(dev_t
, short, int, short *, struct pollhead
**);
63 int syioctl(dev_t
, int, intptr_t, int, cred_t
*, int *);
65 static int sy_info(dev_info_t
*, ddi_info_cmd_t
, void *, void **);
66 static int sy_attach(dev_info_t
*, ddi_attach_cmd_t
);
67 static dev_info_t
*sy_dip
; /* private copy of devinfo pointer */
69 struct cb_ops sy_cb_ops
= {
83 ddi_prop_op
, /* cb_prop_op */
85 D_NEW
| D_MP
/* Driver compatibility flag */
89 struct dev_ops sy_ops
= {
91 DEVO_REV
, /* devo_rev, */
94 nulldev
, /* identify */
96 sy_attach
, /* attach */
99 &sy_cb_ops
, /* driver operations */
100 NULL
, /* bus operations */
102 ddi_quiesce_not_needed
, /* quiesce */
106 extern int nodev(void);
107 extern int nulldev(void);
108 extern int dseekneg_flag
;
109 extern struct mod_ops mod_driverops
;
110 extern struct dev_ops sy_ops
;
113 * Module linkage information for the kernel.
116 static struct modldrv modldrv
= {
117 &mod_driverops
, /* Type of module. This one is a pseudo driver */
118 "Indirect driver for tty 'sy'",
119 &sy_ops
, /* driver ops */
122 static struct modlinkage modlinkage
= {
132 return (mod_install(&modlinkage
));
139 return (mod_remove(&modlinkage
));
143 _info(struct modinfo
*modinfop
)
145 return (mod_info(&modlinkage
, modinfop
));
150 sy_attach(dev_info_t
*devi
, ddi_attach_cmd_t cmd
)
152 if (ddi_create_minor_node(devi
, "tty", S_IFCHR
,
153 0, DDI_PSEUDO
, 0) == DDI_FAILURE
) {
154 ddi_remove_minor_node(devi
, NULL
);
158 return (DDI_SUCCESS
);
163 sy_info(dev_info_t
*dip
, ddi_info_cmd_t infocmd
, void *arg
, void **result
)
165 dev_t dev
= (dev_t
)arg
;
169 case DDI_INFO_DEVT2DEVINFO
:
170 if (sy_dip
== NULL
) {
174 *result
= (void *) sy_dip
;
178 case DDI_INFO_DEVT2INSTANCE
:
179 if (getminor(dev
) != 0) {
180 *result
= (void *)-1;
196 syopen(dev_t
*devp
, int flag
, int otyp
, struct cred
*cr
)
203 if ((sp
= tty_hold()) == NULL
)
206 if (sp
->s_dev
== NODEV
) {
215 * Open the control terminal. The control terminal may be
216 * opened multiple times and it is closed in freectty().
217 * The multi-open, single-clone means that no cloning
218 * can happen via this open, hence the assertion.
220 error
= fop_open(&ttyvp
, FNOCTTY
| flag
, cr
, NULL
);
225 * XXX: This driver binds a single minor number to the
226 * current controlling tty of the process issueing the
227 * open / close. If we implement a traditional close
228 * for this driver then specfs will only invoke this driver
229 * on the last close of our one minor number - which is not
230 * what we want. Since we already get the open / close
231 * semantic that we want from makectty and freectty, we reach
232 * back into the common snode and decrease the open count so
233 * that the specfs filtering of all but the last close
234 * does not get in our way. To clean this up, a new cb_flag
235 * that causes specfs to call the driver on each close
236 * should be considered.
238 ASSERT(ttyd
== ttyvp
->v_rdev
);
239 ASSERT(vn_matchops(ttyvp
, spec_getvnodeops()));
240 csp
= VTOS(VTOS(ttyvp
)->s_commonvp
);
241 mutex_enter(&csp
->s_lock
);
242 ASSERT(csp
->s_count
> 1);
244 mutex_exit(&csp
->s_lock
);
253 syclose(dev_t dev
, int flag
, int otyp
, struct cred
*cr
)
260 syread(dev_t dev
, struct uio
*uiop
, struct cred
*cr
)
265 if ((sp
= tty_hold()) == NULL
)
268 if (sp
->s_dev
== NODEV
) {
273 error
= fop_read(sp
->s_vp
, uiop
, 0, cr
, NULL
);
281 sywrite(dev_t dev
, struct uio
*uiop
, struct cred
*cr
)
286 if ((sp
= tty_hold()) == NULL
)
289 if (sp
->s_dev
== NODEV
) {
294 error
= fop_write(sp
->s_vp
, uiop
, 0, cr
, NULL
);
303 syioctl(dev_t dev
, int cmd
, intptr_t arg
, int mode
, struct cred
*cr
,
309 if (cmd
== TIOCNOTTY
) {
311 * we can't allow this ioctl. the reason is that it
312 * attempts to remove the ctty for a session. to do
313 * this the ctty can't be in use but we grab a hold on
314 * the current ctty (via tty_hold) to perform this ioctl.
315 * if we were to allow this ioctl to pass through we
316 * would deadlock with ourselves.
321 if ((sp
= tty_hold()) == NULL
)
324 if (sp
->s_dev
== NODEV
) {
329 error
= fop_ioctl(sp
->s_vp
, cmd
, arg
, mode
, cr
, rvalp
, NULL
);
339 sypoll(dev_t dev
, short events
, int anyyet
, short *reventsp
,
340 struct pollhead
**phpp
)
345 if ((sp
= tty_hold()) == NULL
)
348 if (sp
->s_dev
== NODEV
) {
353 error
= fop_poll(sp
->s_vp
, events
, anyyet
, reventsp
, phpp
, NULL
);