clean up event handling so that events that do longjmp's don't turn
[unix-history] / usr / src / sys / kern / uipc_pipe.c
CommitLineData
4a2ddbf1 1/* uipc_pipe.c 4.13 82/06/20 */
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
de48daf3 33 /* when we reserve memory this routine may fail */
f498603d
BJ
34 wso->so_proto = rso->so_proto = &pipeproto;
35 wso->so_pcb = (caddr_t)rso;
36 rso->so_pcb = (caddr_t)wso;
b1cf15af
BJ
37 wso->so_snd.sb_hiwat = PIPSIZ;
38 wso->so_snd.sb_mbmax = 2*PIPSIZ;
f498603d 39 wso->so_state |= SS_ISCONNECTED|SS_CANTRCVMORE;
b1cf15af
BJ
40 rso->so_rcv.sb_hiwat = 0;
41 rso->so_rcv.sb_mbmax = 0;
f498603d
BJ
42 rso->so_state |= SS_ISCONNECTED|SS_CANTSENDMORE;
43 return (1);
44}
45
f498603d
BJ
46/*
47 * User requests on pipes and other internally implemented
48 * structures.
49 */
ae921915 50/*ARGSUSED*/
2b4b57cd 51piusrreq(so, req, m, addr)
f498603d
BJ
52 struct socket *so;
53 int req;
54 struct mbuf *m;
55 caddr_t addr;
56{
57 struct socket *so2 = (struct socket *)so->so_pcb;
58
59 switch (req) {
60
61 case PRU_ATTACH:
18eff705
BJ
62 break;
63
f498603d 64 case PRU_DETACH:
18eff705 65 so->so_pcb = 0;
f498603d
BJ
66 break;
67
68 case PRU_CONNECT:
2b4b57cd 69 case PRU_ACCEPT:
f498603d
BJ
70 return (EOPNOTSUPP);
71
72 case PRU_DISCONNECT:
73 if (so2 == 0)
74 return (ENOTCONN);
75 so->so_pcb = 0;
880e5ee0 76 so2->so_pcb = 0;
ae921915 77 soisdisconnected(so);
18eff705 78 soisdisconnected(so2);
f498603d
BJ
79 break;
80
f498603d 81 case PRU_SHUTDOWN:
2b4b57cd
BJ
82 socantsendmore(so);
83 if (so2)
84 socantrcvmore(so2);
f498603d
BJ
85 break;
86
87 case PRU_RCVD:
b1cf15af
BJ
88 if (so2 == 0)
89 break;
90#define rcv (&so->so_rcv)
91#define snd (&so2->so_snd)
92 /*
93 * Transfer resources back to send port
94 * and wakeup any waiting to write.
95 */
96 snd->sb_mbmax += rcv->sb_mbmax - rcv->sb_mbcnt;
97 rcv->sb_mbmax = rcv->sb_mbcnt;
98 snd->sb_hiwat += rcv->sb_hiwat - rcv->sb_cc;
99 rcv->sb_hiwat = rcv->sb_cc;
100 sbwakeup(snd);
101#undef snd
102#undef rcv
f498603d
BJ
103 break;
104
105 case PRU_SEND:
b1cf15af
BJ
106#define rcv (&so2->so_rcv)
107#define snd (&so->so_snd)
880e5ee0
BJ
108 if (so2 == 0)
109 panic("pipe send");
b1cf15af
BJ
110 /*
111 * Send to paired receive port, and then
112 * give it enough resources to hold what it already has.
113 * Wake up readers.
114 */
115 sbappend(rcv, m);
116 snd->sb_mbmax -= rcv->sb_mbcnt - rcv->sb_mbmax;
117 rcv->sb_mbmax = rcv->sb_mbcnt;
118 snd->sb_hiwat -= rcv->sb_cc - rcv->sb_hiwat;
119 rcv->sb_hiwat = rcv->sb_cc;
120 sbwakeup(rcv);
121#undef snd
122#undef rcv
f498603d
BJ
123 break;
124
125 case PRU_ABORT:
126 return (EOPNOTSUPP);
127
128 case PRU_CONTROL:
129 return (EOPNOTSUPP);
130
131 default:
2b4b57cd 132 panic("piusrreq");
f498603d
BJ
133 }
134 return (0);
135}