~kris/9p

9hist

ref: a22bb86fe02e00bb493e7aecba85f93508ade87e 9hist/port/stsplice.c -rw-r--r-- 1.3 KiB
a22bb86f — David du Colombier Plan 9 from Bell Labs 1992-07-29 34 years ago
                                                                                
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
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"io.h"
#include	"../port/error.h"

#define	splicedebug	1

#define	DPRINT	if(splicedebug)kprint

/*
 * Splice line discipline.
 */

static void spliceopen(Queue*, Stream*);
static void spliceclose(Queue*);
static void spliceoput(Queue*, Block*);
static void spliceiput(Queue*, Block*);
Qinfo spliceinfo =
{
	spliceiput,
	spliceoput,
	spliceopen,
	spliceclose,
	"splice"
};

static void
spliceopen(Queue *q, Stream *s)
{
	DPRINT("spliceopen q=0x%ux s=0x%ux\n", q, s);

	WR(q)->ptr = s;		/* pointer to ourselves */
	RD(q)->ptr = 0;		/* pointer to other side */
}

static void
spliceclose(Queue *q)
{
	DPRINT("spliceclose q=0x%ux us=0x%ux them=0x%ux\n",
		q, WR(q)->ptr, RD(q)->ptr);

	RD(q)->ptr = 0;
	WR(q)->ptr = 0;
}

static void
spliceoput(Queue *q, Block *bp)
{
	int fd;
	Chan *c;
	Stream *s;

	if(bp->type != M_CTL){
		PUTNEXT(q, bp);
		return;
	}
	fd = strtol((char *)bp->rptr, 0, 0);
	freeb(bp);
	if(RD(q)->ptr)
		error("stream already spliced");
	c = fdtochan(fd, ORDWR, 0);
	s = c->stream;
	if(s == 0)
		error("splice attempt on non-stream");
	pushq(s, &spliceinfo);
	RD(q)->ptr = s;
	RD(s->procq->next)->ptr = WR(q)->ptr;
}

static void
spliceiput(Queue *q, Block *bp)
{
	if(bp->type == M_HANGUP)
		PUTNEXT(q, bp);
	else
		FLOWCTL(((Stream *)q->ptr)->procq, bp);
}