Blob


1 #include "std.h"
2 #include "dat.h"
4 Logbuf confbuf;
6 void
7 confirmread(Req *r)
8 {
9 lbread(&confbuf, r);
10 }
12 void
13 confirmflush(Req *r)
14 {
15 lbflush(&confbuf, r);
16 }
18 int
19 confirmwrite(char *s)
20 {
21 char *t, *ans;
22 int allow;
23 ulong tag;
24 Attr *a;
25 Conv *c;
27 a = _parseattr(s);
28 if(a == nil){
29 werrstr("bad attr");
30 return -1;
31 }
32 if((t = _strfindattr(a, "tag")) == nil){
33 flog("bad confirm write: no tag");
34 werrstr("no tag");
35 return -1;
36 }
37 tag = strtoul(t, 0, 0);
38 if((ans = _strfindattr(a, "answer")) == nil){
39 flog("bad confirm write: no answer");
40 werrstr("no answer");
41 return -1;
42 }
43 if(strcmp(ans, "yes") == 0)
44 allow = 1;
45 else if(strcmp(ans, "no") == 0)
46 allow = 0;
47 else{
48 flog("bad confirm write: bad answer");
49 werrstr("bad answer");
50 return -1;
51 }
52 for(c=conv; c; c=c->next){
53 if(tag == c->tag){
54 nbsendul(c->keywait, allow);
55 break;
56 }
57 }
58 if(c == nil){
59 werrstr("tag not found");
60 return -1;
61 }
62 return 0;
63 }
65 int
66 confirmkey(Conv *c, Key *k)
67 {
68 int ret;
70 if(*confirminuse == 0)
71 return -1;
73 lbappend(&confbuf, "confirm tag=%lud %A %N", c->tag, k->attr, k->privattr);
74 flog("confirm %A %N", k->attr, k->privattr);
75 c->state = "keyconfirm";
76 ret = recvul(c->keywait);
77 flog("confirm=%d %A %N", ret, k->attr, k->privattr);
78 return ret;
79 }
81 Logbuf needkeybuf;
83 void
84 needkeyread(Req *r)
85 {
86 lbread(&needkeybuf, r);
87 }
89 void
90 needkeyflush(Req *r)
91 {
92 lbflush(&needkeybuf, r);
93 }
95 int
96 needkeywrite(char *s)
97 {
98 char *t;
99 ulong tag;
100 Attr *a;
101 Conv *c;
103 a = _parseattr(s);
104 if(a == nil){
105 werrstr("empty write");
106 return -1;
108 if((t = _strfindattr(a, "tag")) == nil){
109 werrstr("no tag");
110 freeattr(a);
111 return -1;
113 tag = strtoul(t, 0, 0);
114 for(c=conv; c; c=c->next)
115 if(c->tag == tag){
116 nbsendul(c->keywait, 0);
117 break;
119 if(c == nil){
120 werrstr("tag not found");
121 freeattr(a);
122 return -1;
124 freeattr(a);
125 return 0;
128 int
129 needkey(Conv *c, Attr *a)
131 ulong u;
133 if(c == nil || *needkeyinuse == 0)
134 return -1;
136 lbappend(&needkeybuf, "needkey tag=%lud %A", c->tag, a);
137 flog("needkey %A", a);
139 // Note: This code used to "return nbrecvul(c->keywait)."
140 // In Jan 2020 we changed nbrecvul to match Plan 9 and
141 // the man page and return 0 on "no data available" instead
142 // of -1. This new code with an explicit nbrecv preserves the
143 // code's old semantics, distinguishing a sent 0 from "no data".
144 // That said, this code seems to return -1 unconditionally:
145 // the c->keywait channel is unbuffered, and the only sending
146 // to it is done with an nbsendul, which won't block waiting for
147 // a receiver. So there is no sender for nbrecv to find here.
148 if(nbrecv(c->keywait, &u) < 0)
149 return -1;
150 return u;
153 int
154 badkey(Conv *c, Key *k, char *msg, Attr *a)
156 ulong u;
158 if(c == nil || *needkeyinuse == 0)
159 return -1;
161 lbappend(&needkeybuf, "badkey tag=%lud %A %N\n%s\n%A",
162 c->tag, k->attr, k->privattr, msg, a);
163 flog("badkey %A / %N / %s / %A",
164 k->attr, k->privattr, msg, a);
166 // Note: This code used to "return nbrecvul(c->keywait)."
167 // In Jan 2020 we changed nbrecvul to match Plan 9 and
168 // the man page and return 0 on "no data available" instead
169 // of -1. This new code with an explicit nbrecv preserves the
170 // code's old semantics, distinguishing a sent 0 from "no data".
171 // That said, this code seems to return -1 unconditionally:
172 // the c->keywait channel is unbuffered, and the only sending
173 // to it is done with an nbsendul, which won't block waiting for
174 // a receiver. So there is no sender for nbrecv to find here.
175 if(nbrecv(c->keywait, &u) < 0)
176 return -1;
177 return u;