1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/* from S5R4 1.22 */
/*
* Indirect driver for controlling tty.
*/
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/conf.h>
#include <sys/proc.h>
#include <sys/tty.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/cred.h>
#include <sys/uio.h>
#include <sys/session.h>
#include <sys/ddi.h>
#include <sys/debug.h>
#include <sys/stat.h>
#include <sys/sunddi.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/modctl.h>
#include <sys/fs/snode.h>
#include <sys/file.h>
#define IS_STREAM(dev) (devopsp[getmajor(dev)]->devo_cb_ops->cb_str != NULL)
int syopen(dev_t *, int, int, cred_t *);
int syclose(dev_t, int, int, cred_t *);
int syread(dev_t, struct uio *, cred_t *);
int sywrite(dev_t, struct uio *, cred_t *);
int sypoll(dev_t, short, int, short *, struct pollhead **);
int syioctl(dev_t, int, intptr_t, int, cred_t *, int *);
static int sy_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
static int sy_attach(dev_info_t *, ddi_attach_cmd_t);
static dev_info_t *sy_dip; /* private copy of devinfo pointer */
struct cb_ops sy_cb_ops = {
syopen, /* open */
syclose, /* close */
nodev, /* strategy */
nodev, /* print */
nodev, /* dump */
syread, /* read */
sywrite, /* write */
syioctl, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
sypoll, /* poll */
ddi_prop_op, /* cb_prop_op */
0, /* streamtab */
D_NEW | D_MP /* Driver compatibility flag */
};
struct dev_ops sy_ops = {
DEVO_REV, /* devo_rev, */
0, /* refcnt */
sy_info, /* info */
nulldev, /* identify */
nulldev, /* probe */
sy_attach, /* attach */
nodev, /* detach */
nodev, /* reset */
&sy_cb_ops, /* driver operations */
(struct bus_ops *)0, /* bus operations */
NULL, /* power */
ddi_quiesce_not_needed, /* quiesce */
};
extern int nodev(void);
extern int nulldev(void);
extern int dseekneg_flag;
extern struct mod_ops mod_driverops;
extern struct dev_ops sy_ops;
/*
* Module linkage information for the kernel.
*/
static struct modldrv modldrv = {
&mod_driverops, /* Type of module. This one is a pseudo driver */
"Indirect driver for tty 'sy'",
&sy_ops, /* driver ops */
};
static struct modlinkage modlinkage = {
MODREV_1,
&modldrv,
NULL
};
int
_init(void)
{
return (mod_install(&modlinkage));
}
int
_fini(void)
{
return (mod_remove(&modlinkage));
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
/* ARGSUSED */
static int
sy_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
{
if (ddi_create_minor_node(devi, "tty", S_IFCHR,
0, DDI_PSEUDO, 0) == DDI_FAILURE) {
ddi_remove_minor_node(devi, NULL);
return (-1);
}
sy_dip = devi;
return (DDI_SUCCESS);
}
/* ARGSUSED */
static int
sy_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
{
dev_t dev = (dev_t)arg;
int error;
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
if (sy_dip == NULL) {
*result = (void *)NULL;
error = DDI_FAILURE;
} else {
*result = (void *) sy_dip;
error = DDI_SUCCESS;
}
break;
case DDI_INFO_DEVT2INSTANCE:
if (getminor(dev) != 0) {
*result = (void *)-1;
error = DDI_FAILURE;
} else {
*result = (void *)0;
error = DDI_SUCCESS;
}
break;
default:
error = DDI_FAILURE;
}
return (error);
}
/* ARGSUSED */
int
syopen(dev_t *devp, int flag, int otyp, struct cred *cr)
{
dev_t ttyd;
vnode_t *ttyvp;
sess_t *sp;
int error;
if ((sp = tty_hold()) == NULL)
return (EINTR);
if (sp->s_dev == NODEV) {
tty_rele(sp);
return (ENXIO);
}
ttyd = sp->s_dev;
ttyvp = sp->s_vp;
/*
* Open the control terminal. The control terminal may be
* opened multiple times and it is closed in freectty().
* The multi-open, single-clone means that no cloning
* can happen via this open, hence the assertion.
*/
error = VOP_OPEN(&ttyvp, FNOCTTY | flag, cr, NULL);
if (error == 0) {
struct snode *csp;
/*
* XXX: This driver binds a single minor number to the
* current controlling tty of the process issueing the
* open / close. If we implement a traditional close
* for this driver then specfs will only invoke this driver
* on the last close of our one minor number - which is not
* what we want. Since we already get the open / close
* semantic that we want from makectty and freectty, we reach
* back into the common snode and decrease the open count so
* that the specfs filtering of all but the last close
* does not get in our way. To clean this up, a new cb_flag
* that causes specfs to call the driver on each close
* should be considered.
*/
ASSERT(ttyd == ttyvp->v_rdev);
ASSERT(vn_matchops(ttyvp, spec_getvnodeops()));
csp = VTOS(VTOS(ttyvp)->s_commonvp);
mutex_enter(&csp->s_lock);
ASSERT(csp->s_count > 1);
csp->s_count--;
mutex_exit(&csp->s_lock);
}
tty_rele(sp);
return (error);
}
/* ARGSUSED */
int
syclose(dev_t dev, int flag, int otyp, struct cred *cr)
{
return (0);
}
/* ARGSUSED */
int
syread(dev_t dev, struct uio *uiop, struct cred *cr)
{
sess_t *sp;
int error;
if ((sp = tty_hold()) == NULL)
return (EINTR);
if (sp->s_dev == NODEV) {
tty_rele(sp);
return (ENXIO);
}
error = VOP_READ(sp->s_vp, uiop, 0, cr, NULL);
tty_rele(sp);
return (error);
}
/* ARGSUSED */
int
sywrite(dev_t dev, struct uio *uiop, struct cred *cr)
{
sess_t *sp;
int error;
if ((sp = tty_hold()) == NULL)
return (EINTR);
if (sp->s_dev == NODEV) {
tty_rele(sp);
return (ENXIO);
}
error = VOP_WRITE(sp->s_vp, uiop, 0, cr, NULL);
tty_rele(sp);
return (error);
}
/* ARGSUSED */
int
syioctl(dev_t dev, int cmd, intptr_t arg, int mode, struct cred *cr,
int *rvalp)
{
sess_t *sp;
int error;
if (cmd == TIOCNOTTY) {
/*
* we can't allow this ioctl. the reason is that it
* attempts to remove the ctty for a session. to do
* this the ctty can't be in use but we grab a hold on
* the current ctty (via tty_hold) to perform this ioctl.
* if we were to allow this ioctl to pass through we
* would deadlock with ourselves.
*/
return (EINVAL);
}
if ((sp = tty_hold()) == NULL)
return (EINTR);
if (sp->s_dev == NODEV) {
tty_rele(sp);
return (ENXIO);
}
error = VOP_IOCTL(sp->s_vp, cmd, arg, mode, cr, rvalp, NULL);
tty_rele(sp);
return (error);
}
/* ARGSUSED */
int
sypoll(dev_t dev, short events, int anyyet, short *reventsp,
struct pollhead **phpp)
{
sess_t *sp;
int error;
if ((sp = tty_hold()) == NULL)
return (EINTR);
if (sp->s_dev == NODEV) {
tty_rele(sp);
return (ENXIO);
}
error = VOP_POLL(sp->s_vp, events, anyyet, reventsp, phpp, NULL);
tty_rele(sp);
return (error);
}
|