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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
/*
* Copyright (c) 1995 Ugen J.S.Antsilevich
*
* Redistribution and use in source forms, with and without modification,
* are permitted provided that this entire comment appears intact.
*
* Redistribution in binary form may occur without any restrictions.
* Obviously, it would be nice if you gave credit where credit is due
* but requiring it would be too onerous.
*
* This software is provided ``AS IS'' without any warranties of any kind.
*
* Snoop stuff.
*/
#include "snp.h"
#if (defined NSNP) && (NSNP > 0)
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ioctl.h>
#include <sys/ioctl_compat.h> /* Oooh..We need O/NTTYDISC */
#include <sys/proc.h>
#define TTYDEFCHARS
#include <sys/tty.h>
#undef TTYDEFCHARS
#include <sys/file.h>
#include <sys/conf.h>
#include <sys/uio.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/snoop.h>
#ifdef ST_PTY
/*
* This should be same as in "kern/tty_pty.c"
*/
#include "pty.h"
#if NPTY == 1
#undef NPTY
#define NPTY 32
#endif
extern struct tty pt_tty[];
#endif /* ST_PTY */
#ifdef ST_SIO
/*
* This should be same as "i386/isa/sio.c"
*/
#include "sio.h"
extern struct tty sio_tty[];
#endif /* ST_SIO */
#ifdef ST_VTY
/*
* This should match "i386/isa/sc.c"
*/
#if !defined(MAXCONS)
#define MAXCONS 16
#endif
extern struct tty sccons[];
#endif /* ST_VTY */
/*
* This is local structure to hold
* data for all tty arrays we serve.
*/
typedef struct tty tty_arr[];
struct tty_tab {
int lt_max;
tty_arr *lt_tab;
};
static struct tty_tab tty_tabs[]={
#ifdef ST_PTY
{ NPTY , &pt_tty },
#else
{ -1 , NULL },
#endif
#ifdef ST_VTY
{ MAXCONS, &sccons },
#else
{ -1 , NULL },
#endif
#ifdef ST_SIO
{ NSIO , &sio_tty }
#else
{ -1 , NULL }
#endif
};
#ifndef MIN
#define MIN(a,b) (((a)<(b))?(a):(b))
#endif
static struct snoop snoopsw[NSNP];
int
snpread(dev, uio, flag)
dev_t dev;
struct uio *uio;
int flag;
{
int unit = minor(dev),s;
struct snoop *snp = &snoopsw[unit];
int len,n,nblen,error = 0;
caddr_t from;
char *nbuf;
#ifdef DIAGNOSTIC
if ((snp->snp_len+snp->snp_base)>snp->snp_blen)
panic("snoop buffer error");
#endif
if (snp->snp_unit==-1)
return (EIO);
snp->snp_flags &= ~SNOOP_RWAIT;
do {
if (snp->snp_len == 0) {
if (snp->snp_flags & SNOOP_NBIO) {
return EWOULDBLOCK;
}
snp->snp_flags|=SNOOP_RWAIT;
tsleep((caddr_t)snp,(PZERO+1)|PCATCH,"snoopread",0);
}
} while (snp->snp_len == 0);
n = uio->uio_resid;
while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) {
len = MIN(uio->uio_resid,snp->snp_len);
from = (caddr_t)(snp->snp_buf+snp->snp_base);
if (len == 0)
break;
error = uiomove(from,len,uio);
snp->snp_base+=len;
snp->snp_len-=len;
}
if (n)
snp->snp_flags &= ~SNOOP_OFLOW;
s=spltty();
nblen=snp->snp_blen;
if (((nblen/2)>=SNOOP_MINLEN) && (nblen/2)>=snp->snp_len) {
while (((nblen/2)>=snp->snp_len) && ((nblen/2)>=SNOOP_MINLEN))
nblen=nblen/2;
if (nbuf=malloc(nblen,M_TTYS,M_NOWAIT)) {
bcopy(snp->snp_buf+snp->snp_base,nbuf,snp->snp_len);
free(snp->snp_buf,M_TTYS);
snp->snp_buf=nbuf;
snp->snp_blen=nblen;
snp->snp_base=0;
}
}
splx(s);
return error;
}
int
snpin(snp,buf,n)
struct snoop *snp;
char *buf;
int n;
{
int s_free,s_tail;
int s,len,nblen;
caddr_t from,to;
char *nbuf;
struct tty_tab *l_tty;
struct tty *tp;
if (n == 0)
return 0;
#ifdef DIAGNOSTIC
if (n < 0)
panic("bad snoop char count");
#endif
if (snp->snp_flags&SNOOP_OFLOW) {
printf("Snoop: buffer overflow \n");
/*
* On overflow we just repeat the standart
* close procedure...yes , this is waste of space but..
* Then next read from device will fail if one would
* recall he is snooping and retry...
*/
snp->snp_flags &= ~SNOOP_OPEN;
snp->snp_base = 0;
snp->snp_len = 0;
snp->snp_blen=0;
free(snp->snp_buf,M_TTYS);
/*
* If line disc. changed we do not touch
* this pointer,SLIP/PPP will change it anyway.
*/
if (snp->snp_unit==-1)
goto oflow_notty;
l_tty=&(tty_tabs[snp->snp_type]);
tp=&((*l_tty->lt_tab)[snp->snp_unit]);
if (tp->t_sc == snp && (tp->t_state&TS_SNOOP) &&
(tp->t_line == OTTYDISC || tp->t_line == NTTYDISC)) {
tp->t_sc = NULL;
tp->t_state &=~TS_SNOOP;
};
snp->snp_unit=-1;
oflow_notty:
selwakeup(&snp->snp_sel);
snp->snp_sel.si_pid = 0;
return (0);
}
s_free = snp->snp_blen - ( snp->snp_len + snp->snp_base );
s_tail = snp->snp_blen - snp->snp_len;
if (n > s_free) {
s=spltty();
nblen=snp->snp_blen;
while ((n > s_free) && ((nblen*2)<=SNOOP_MAXLEN)) {
nblen=snp->snp_blen*2;
s_free = nblen - ( snp->snp_len + snp->snp_base );
}
if ((n<=s_free) && (nbuf=malloc(nblen,M_TTYS,M_NOWAIT))) {
bcopy(snp->snp_buf+snp->snp_base,nbuf,snp->snp_len);
free(snp->snp_buf,M_TTYS);
snp->snp_buf=nbuf;
snp->snp_blen=nblen;
snp->snp_base=0;
} else {
snp->snp_flags|=SNOOP_OFLOW;
if (snp->snp_flags & SNOOP_RWAIT) {
snp->snp_flags &= ~SNOOP_RWAIT;
wakeup((caddr_t)snp);
}
splx(s);
return 0;
}
splx(s);
}
if (n > s_tail) {
from=(caddr_t)(snp->snp_buf+snp->snp_base);
to=(caddr_t)(snp->snp_buf);
len=snp->snp_len;
bcopy(from,to,len);
snp->snp_base=0;
}
to=(caddr_t)(snp->snp_buf+snp->snp_base+snp->snp_len);
bcopy(buf,to,n);
snp->snp_len+=n;
if (snp->snp_flags & SNOOP_RWAIT) {
snp->snp_flags &= ~SNOOP_RWAIT;
wakeup((caddr_t)snp);
}
selwakeup(&snp->snp_sel);
snp->snp_sel.si_pid = 0;
return n;
}
int
snpopen(dev, flag, mode, p)
dev_t dev;
int flag, mode;
struct proc *p;
{
struct snoop *snp;
register int unit, error;
if (error = suser(p->p_ucred, &p->p_acflag))
return (error);
if ((unit = minor(dev)) >= NSNP)
return (ENXIO);
snp = &snoopsw[unit];
if (snp->snp_flags & SNOOP_OPEN)
return (ENXIO);
snp->snp_flags |= SNOOP_OPEN;
snp->snp_buf=malloc(SNOOP_MINLEN,M_TTYS,M_WAITOK);
snp->snp_blen=SNOOP_MINLEN;
snp->snp_base=0;
snp->snp_len=0;
/*
* unit == -1 is for inactive snoop
* devices.
*/
snp->snp_unit=-1;
return (0);
}
int
snpclose(dev, flag)
dev_t dev;
int flag;
{
register int unit = minor(dev);
struct snoop *snp = &snoopsw[unit];
struct tty *tp;
struct tty_tab *l_tty;
snp->snp_flags &= ~SNOOP_OPEN;
snp->snp_base = 0;
snp->snp_len = 0;
snp->snp_blen=0;
free(snp->snp_buf,M_TTYS);
/*
* If line disc. changed we do not touch
* this pointer,SLIP/PPP will change it anyway.
*/
if (snp->snp_unit==-1)
goto close_notty;
l_tty=&tty_tabs[snp->snp_type];
tp=&((*l_tty->lt_tab)[snp->snp_unit]);
if (tp->t_sc == snp && (tp->t_state&TS_SNOOP) &&
(tp->t_line == OTTYDISC || tp->t_line == NTTYDISC)) {
tp->t_sc = NULL;
tp->t_state &=~TS_SNOOP;
};
snp->snp_unit=-1;
close_notty:
selwakeup(&snp->snp_sel);
snp->snp_sel.si_pid = 0;
return (0);
}
int
snpioctl(dev, cmd, data, flag)
dev_t dev;
int cmd;
caddr_t data;
int flag;
{
int unit = minor(dev),s;
int tunit,ttype;
struct snoop *snp = &snoopsw[unit];
struct tty *tp,*tpo;
struct tty_tab *l_tty,*l_otty;
switch (cmd) {
case SNPSTTY:
tunit=((struct snptty *)data)->st_unit;
ttype=((struct snptty *)data)->st_type;
if (ttype < 0 || ttype > ST_MAXTYPE)
return (EINVAL);
l_tty = &tty_tabs[ttype];
if (l_tty->lt_tab == NULL)
return (EINVAL);
if (tunit < 0 || tunit >= l_tty->lt_max)
return (EINVAL);
tp=&((*l_tty->lt_tab)[tunit]);
if (tp->t_sc!=(caddr_t)snp && (tp->t_state&TS_SNOOP))
return (EBUSY);
if (tp->t_line!=OTTYDISC && tp->t_line!=NTTYDISC)
return (EBUSY);
s=spltty();
if (snp->snp_unit != -1) {
l_otty=&tty_tabs[snp->snp_type];
tpo=&((*l_otty->lt_tab)[snp->snp_unit]);
tpo->t_state &=~TS_SNOOP;
}
tp->t_sc=(caddr_t)snp;
tp->t_state |= TS_SNOOP;
snp->snp_unit = tunit;
splx(s);
break;
case SNPGTTY:
((struct snptty *)data)->st_unit = snp->snp_unit;
((struct snptty *)data)->st_type = snp->snp_type;
break;
case FIONBIO:
if (*(int *)data)
snp->snp_flags |= SNOOP_NBIO;
else
snp->snp_flags &= ~SNOOP_NBIO;
break;
case FIOASYNC:
if (*(int *)data)
snp->snp_flags |= SNOOP_ASYNC;
else
snp->snp_flags &= ~SNOOP_ASYNC;
break;
case FIONREAD:
s = spltty();
if (snp->snp_unit!=-1)
*(int *)data = snp->snp_len;
else
*(int *)data = 0;
splx(s);
break;
default:
return (ENOTTY);
}
return (0);
}
int
snpselect(dev, rw, p)
dev_t dev;
int rw;
struct proc *p;
{
int unit = minor(dev), s;
struct snoop *snp = &snoopsw[unit];
if (rw != FREAD) {
return 0;
}
if (snp->snp_len > 0) {
return 1;
}
selrecord(p, &snp->snp_sel);
return 0;
}
#endif
|