date and time created 82/05/26 15:16:03 by sam
[unix-history] / usr / src / sys / kern / uipc_pipe.c
CommitLineData
668cc26d 1/* uipc_pipe.c 4.11 82/03/13 */
f498603d
BJ
2
3#include "../h/param.h"
4#include "../h/dir.h"
5#include "../h/user.h"
6#include "../h/mbuf.h"
f498603d
BJ
7#include "../h/protosw.h"
8#include "../h/socket.h"
9#include "../h/socketvar.h"
0ef33f87 10#include "../net/in_systm.h" /* XXX */
f498603d 11
2b4b57cd 12int piusrreq();
f498603d
BJ
13#define PIPSIZ 4096
14
15/*
16 * Code for pipes and other loopback protocols (single machine, that is.)
17 */
18struct protosw pipeproto = {
4c078bb2 19 SOCK_STREAM, PF_UNIX, 0, PR_CONNREQUIRED|PR_WANTRCVD,
f498603d 20 0, 0, 0, 0,
4c078bb2 21 piusrreq,
f498603d
BJ
22 0, 0, 0, 0
23};
24
25/*
26 * Connect a pipe from wso to rso. The protocol control block
27 * for a pipe is used to store a pointer to the matching socket.
f498603d 28 */
2b4b57cd 29piconnect(wso, rso)
f498603d
BJ
30 struct socket *wso, *rso;
31{
32
2b4b57cd 33COUNT(PICONNECT);
b725360e 34 if (m_reserve(PIPSIZ*2/MSIZE) == 0) {
f498603d
BJ
35 u.u_error = ENOBUFS;
36 return (0);
37 }
38 wso->so_proto = rso->so_proto = &pipeproto;
39 wso->so_pcb = (caddr_t)rso;
40 rso->so_pcb = (caddr_t)wso;
b1cf15af
BJ
41 wso->so_snd.sb_hiwat = PIPSIZ;
42 wso->so_snd.sb_mbmax = 2*PIPSIZ;
f498603d 43 wso->so_state |= SS_ISCONNECTED|SS_CANTRCVMORE;
b1cf15af
BJ
44 rso->so_rcv.sb_hiwat = 0;
45 rso->so_rcv.sb_mbmax = 0;
f498603d
BJ
46 rso->so_state |= SS_ISCONNECTED|SS_CANTSENDMORE;
47 return (1);
48}
49
f498603d
BJ
50/*
51 * User requests on pipes and other internally implemented
52 * structures.
53 */
ae921915 54/*ARGSUSED*/
2b4b57cd 55piusrreq(so, req, m, addr)
f498603d
BJ
56 struct socket *so;
57 int req;
58 struct mbuf *m;
59 caddr_t addr;
60{
61 struct socket *so2 = (struct socket *)so->so_pcb;
62
2b4b57cd 63COUNT(PIUSRREQ);
f498603d
BJ
64 switch (req) {
65
66 case PRU_ATTACH:
18eff705
BJ
67 break;
68
f498603d 69 case PRU_DETACH:
18eff705 70 so->so_pcb = 0;
f498603d
BJ
71 break;
72
73 case PRU_CONNECT:
2b4b57cd 74 case PRU_ACCEPT:
f498603d
BJ
75 return (EOPNOTSUPP);
76
77 case PRU_DISCONNECT:
78 if (so2 == 0)
79 return (ENOTCONN);
80 so->so_pcb = 0;
880e5ee0 81 so2->so_pcb = 0;
ae921915 82 soisdisconnected(so);
18eff705 83 soisdisconnected(so2);
f498603d
BJ
84 break;
85
f498603d 86 case PRU_SHUTDOWN:
2b4b57cd
BJ
87 socantsendmore(so);
88 if (so2)
89 socantrcvmore(so2);
f498603d
BJ
90 break;
91
92 case PRU_RCVD:
b1cf15af
BJ
93 if (so2 == 0)
94 break;
95#define rcv (&so->so_rcv)
96#define snd (&so2->so_snd)
97 /*
98 * Transfer resources back to send port
99 * and wakeup any waiting to write.
100 */
101 snd->sb_mbmax += rcv->sb_mbmax - rcv->sb_mbcnt;
102 rcv->sb_mbmax = rcv->sb_mbcnt;
103 snd->sb_hiwat += rcv->sb_hiwat - rcv->sb_cc;
104 rcv->sb_hiwat = rcv->sb_cc;
105 sbwakeup(snd);
106#undef snd
107#undef rcv
f498603d
BJ
108 break;
109
110 case PRU_SEND:
b1cf15af
BJ
111#define rcv (&so2->so_rcv)
112#define snd (&so->so_snd)
880e5ee0
BJ
113 if (so2 == 0)
114 panic("pipe send");
b1cf15af
BJ
115 /*
116 * Send to paired receive port, and then
117 * give it enough resources to hold what it already has.
118 * Wake up readers.
119 */
120 sbappend(rcv, m);
121 snd->sb_mbmax -= rcv->sb_mbcnt - rcv->sb_mbmax;
122 rcv->sb_mbmax = rcv->sb_mbcnt;
123 snd->sb_hiwat -= rcv->sb_cc - rcv->sb_hiwat;
124 rcv->sb_hiwat = rcv->sb_cc;
125 sbwakeup(rcv);
126#undef snd
127#undef rcv
f498603d
BJ
128 break;
129
130 case PRU_ABORT:
131 return (EOPNOTSUPP);
132
133 case PRU_CONTROL:
134 return (EOPNOTSUPP);
135
136 default:
2b4b57cd 137 panic("piusrreq");
f498603d
BJ
138 }
139 return (0);
140}
18eff705 141
668cc26d 142#ifdef notdef
18eff705
BJ
143psndrcv(snd, rcv)
144 struct sockbuf *snd, *rcv;
145{
146
147 printf("snd: (cc,hiwat,mbcnt,mbmax) (%d,%d,%d,%d) ",
148 snd->sb_cc, snd->sb_hiwat, snd->sb_mbcnt, snd->sb_mbmax);
149 printf("m %x, m->m_len %d\n", snd->sb_mb, snd->sb_mb ? snd->sb_mb->m_len : 0);
150 printf("rcv: (cc,hiwat,mbcnt,mbmax) (%d,%d,%d,%d) ",
151 rcv->sb_cc, rcv->sb_hiwat, rcv->sb_mbcnt, rcv->sb_mbmax);
152 printf("m %x, m->m_len %d\n", rcv->sb_mb, rcv->sb_mb ? rcv->sb_mb->m_len : 0);
153}
668cc26d 154#endif