Commit | Line | Data |
---|---|---|
c5935bca | 1 | /* uipc_usrreq.c 6.13 85/06/02 */ |
d6213d15 | 2 | |
94368568 JB |
3 | #include "param.h" |
4 | #include "dir.h" | |
5 | #include "user.h" | |
6 | #include "mbuf.h" | |
7 | #include "domain.h" | |
8 | #include "protosw.h" | |
9 | #include "socket.h" | |
10 | #include "socketvar.h" | |
11 | #include "unpcb.h" | |
12 | #include "un.h" | |
13 | #include "inode.h" | |
14 | #include "file.h" | |
15 | #include "stat.h" | |
d6213d15 BJ |
16 | |
17 | /* | |
18 | * Unix communications domain. | |
88a7a62a SL |
19 | * |
20 | * TODO: | |
21 | * SEQPACKET, RDM | |
8ecea7bc | 22 | * rethink name space problems |
88a7a62a | 23 | * need a proper out-of-band |
d6213d15 | 24 | */ |
8ecea7bc | 25 | struct sockaddr sun_noname = { AF_UNIX }; |
0abd16a9 | 26 | ino_t unp_ino; /* fake inode numbers */ |
d6213d15 BJ |
27 | |
28 | /*ARGSUSED*/ | |
88a7a62a | 29 | uipc_usrreq(so, req, m, nam, rights) |
d6213d15 BJ |
30 | struct socket *so; |
31 | int req; | |
88a7a62a | 32 | struct mbuf *m, *nam, *rights; |
d6213d15 BJ |
33 | { |
34 | struct unpcb *unp = sotounpcb(so); | |
35 | register struct socket *so2; | |
36 | int error = 0; | |
37 | ||
88a7a62a SL |
38 | if (req != PRU_SEND && rights && rights->m_len) { |
39 | error = EOPNOTSUPP; | |
40 | goto release; | |
41 | } | |
42 | if (unp == 0 && req != PRU_ATTACH) { | |
43 | error = EINVAL; | |
44 | goto release; | |
45 | } | |
d6213d15 BJ |
46 | switch (req) { |
47 | ||
48 | case PRU_ATTACH: | |
49 | if (unp) { | |
4f083fd7 | 50 | error = EISCONN; |
d6213d15 BJ |
51 | break; |
52 | } | |
fca5570f | 53 | error = unp_attach(so); |
d6213d15 BJ |
54 | break; |
55 | ||
56 | case PRU_DETACH: | |
57 | unp_detach(unp); | |
58 | break; | |
59 | ||
4f083fd7 SL |
60 | case PRU_BIND: |
61 | error = unp_bind(unp, nam); | |
62 | break; | |
63 | ||
64 | case PRU_LISTEN: | |
65 | if (unp->unp_inode == 0) | |
66 | error = EINVAL; | |
67 | break; | |
68 | ||
d6213d15 | 69 | case PRU_CONNECT: |
fca5570f | 70 | error = unp_connect(so, nam); |
d6213d15 BJ |
71 | break; |
72 | ||
88a7a62a | 73 | case PRU_CONNECT2: |
5a48956d SL |
74 | error = unp_connect2(so, (struct mbuf *)0, |
75 | (struct socket *)nam); | |
88a7a62a SL |
76 | break; |
77 | ||
d6213d15 BJ |
78 | case PRU_DISCONNECT: |
79 | unp_disconnect(unp); | |
80 | break; | |
81 | ||
4f083fd7 SL |
82 | case PRU_ACCEPT: |
83 | nam->m_len = unp->unp_remaddr->m_len; | |
84 | bcopy(mtod(unp->unp_remaddr, caddr_t), | |
85 | mtod(nam, caddr_t), (unsigned)nam->m_len); | |
d6213d15 BJ |
86 | break; |
87 | ||
88 | case PRU_SHUTDOWN: | |
89 | socantsendmore(so); | |
90 | unp_usrclosed(unp); | |
91 | break; | |
d6213d15 BJ |
92 | |
93 | case PRU_RCVD: | |
94 | switch (so->so_type) { | |
95 | ||
96 | case SOCK_DGRAM: | |
97 | panic("uipc 1"); | |
5fe6f9d1 | 98 | /*NOTREACHED*/ |
d6213d15 | 99 | |
5fe6f9d1 | 100 | case SOCK_STREAM: |
d6213d15 BJ |
101 | #define rcv (&so->so_rcv) |
102 | #define snd (&so2->so_snd) | |
103 | if (unp->unp_conn == 0) | |
104 | break; | |
105 | so2 = unp->unp_conn->unp_socket; | |
106 | /* | |
107 | * Transfer resources back to send port | |
108 | * and wakeup any waiting to write. | |
109 | */ | |
110 | snd->sb_mbmax += rcv->sb_mbmax - rcv->sb_mbcnt; | |
111 | rcv->sb_mbmax = rcv->sb_mbcnt; | |
112 | snd->sb_hiwat += rcv->sb_hiwat - rcv->sb_cc; | |
113 | rcv->sb_hiwat = rcv->sb_cc; | |
9c40f33e | 114 | sowwakeup(so2); |
d6213d15 BJ |
115 | #undef snd |
116 | #undef rcv | |
d6213d15 BJ |
117 | break; |
118 | ||
119 | default: | |
120 | panic("uipc 2"); | |
121 | } | |
122 | break; | |
123 | ||
124 | case PRU_SEND: | |
125 | switch (so->so_type) { | |
126 | ||
127 | case SOCK_DGRAM: | |
fca5570f | 128 | if (nam) { |
d6213d15 BJ |
129 | if (unp->unp_conn) { |
130 | error = EISCONN; | |
131 | break; | |
132 | } | |
fca5570f | 133 | error = unp_connect(so, nam); |
d6213d15 BJ |
134 | if (error) |
135 | break; | |
136 | } else { | |
137 | if (unp->unp_conn == 0) { | |
138 | error = ENOTCONN; | |
139 | break; | |
140 | } | |
141 | } | |
142 | so2 = unp->unp_conn->unp_socket; | |
4f083fd7 | 143 | /* BEGIN XXX */ |
88a7a62a SL |
144 | if (rights) { |
145 | error = unp_internalize(rights); | |
146 | if (error) | |
147 | break; | |
148 | } | |
149 | if (sbspace(&so2->so_rcv) > 0) { | |
8ecea7bc SL |
150 | /* |
151 | * There's no record of source socket's | |
152 | * name, so send null name for the moment. | |
153 | */ | |
9c40f33e MK |
154 | if (sbappendaddr(&so2->so_rcv, |
155 | &sun_noname, m, rights)) { | |
156 | sorwakeup(so2); | |
157 | m = 0; | |
158 | } | |
88a7a62a | 159 | } |
4f083fd7 | 160 | /* END XXX */ |
fca5570f | 161 | if (nam) |
4f083fd7 | 162 | unp_disconnect(unp); |
d6213d15 BJ |
163 | break; |
164 | ||
165 | case SOCK_STREAM: | |
166 | #define rcv (&so2->so_rcv) | |
167 | #define snd (&so->so_snd) | |
88a7a62a SL |
168 | if (rights && rights->m_len) { |
169 | error = EOPNOTSUPP; | |
170 | break; | |
171 | } | |
0abd16a9 MK |
172 | if (so->so_state & SS_CANTSENDMORE) |
173 | return (EPIPE); | |
d6213d15 BJ |
174 | if (unp->unp_conn == 0) |
175 | panic("uipc 3"); | |
176 | so2 = unp->unp_conn->unp_socket; | |
177 | /* | |
178 | * Send to paired receive port, and then | |
179 | * give it enough resources to hold what it already has. | |
180 | * Wake up readers. | |
181 | */ | |
182 | sbappend(rcv, m); | |
183 | snd->sb_mbmax -= rcv->sb_mbcnt - rcv->sb_mbmax; | |
184 | rcv->sb_mbmax = rcv->sb_mbcnt; | |
185 | snd->sb_hiwat -= rcv->sb_cc - rcv->sb_hiwat; | |
186 | rcv->sb_hiwat = rcv->sb_cc; | |
9c40f33e MK |
187 | sorwakeup(so2); |
188 | m = 0; | |
d6213d15 BJ |
189 | #undef snd |
190 | #undef rcv | |
191 | break; | |
192 | ||
193 | default: | |
194 | panic("uipc 4"); | |
195 | } | |
196 | break; | |
197 | ||
198 | case PRU_ABORT: | |
199 | unp_drop(unp, ECONNABORTED); | |
200 | break; | |
201 | ||
202 | /* SOME AS YET UNIMPLEMENTED HOOKS */ | |
203 | case PRU_CONTROL: | |
1116b929 | 204 | return (EOPNOTSUPP); |
d6213d15 | 205 | |
d6213d15 | 206 | /* END UNIMPLEMENTED HOOKS */ |
bfd4e82c MK |
207 | case PRU_SENSE: |
208 | ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; | |
209 | if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) { | |
210 | so2 = unp->unp_conn->unp_socket; | |
211 | ((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc; | |
212 | } | |
0abd16a9 MK |
213 | ((struct stat *) m)->st_dev = NODEV; |
214 | ((struct stat *) m)->st_ino = unp_ino++; | |
bfd4e82c | 215 | return (0); |
d6213d15 BJ |
216 | |
217 | case PRU_RCVOOB: | |
913b2f19 | 218 | return (EOPNOTSUPP); |
d6213d15 BJ |
219 | |
220 | case PRU_SENDOOB: | |
9c40f33e | 221 | error = EOPNOTSUPP; |
d6213d15 BJ |
222 | break; |
223 | ||
224 | case PRU_SOCKADDR: | |
225 | break; | |
226 | ||
a7343092 SL |
227 | case PRU_PEERADDR: |
228 | break; | |
229 | ||
d6213d15 BJ |
230 | case PRU_SLOWTIMO: |
231 | break; | |
232 | ||
233 | default: | |
234 | panic("piusrreq"); | |
235 | } | |
88a7a62a SL |
236 | release: |
237 | if (m) | |
238 | m_freem(m); | |
e14b8185 | 239 | return (error); |
d6213d15 BJ |
240 | } |
241 | ||
bfd4e82c MK |
242 | /* |
243 | * We assign all buffering for stream sockets to the source, | |
244 | * as that is where the flow control is implemented. | |
245 | * Datagram sockets really use the sendspace as the maximum datagram size, | |
246 | * and don't really want to reserve the sendspace. Their recvspace should | |
247 | * be large enough for at least one max-size datagram plus address. | |
248 | */ | |
249 | #define PIPSIZ 4096 | |
250 | int unpst_sendspace = PIPSIZ; | |
251 | int unpst_recvspace = 0; | |
252 | int unpdg_sendspace = 2*1024; /* really max datagram size */ | |
253 | int unpdg_recvspace = 4*1024; | |
d6213d15 | 254 | |
4f083fd7 | 255 | unp_attach(so) |
d6213d15 | 256 | struct socket *so; |
d6213d15 | 257 | { |
4f083fd7 | 258 | register struct mbuf *m; |
d6213d15 | 259 | register struct unpcb *unp; |
d6213d15 BJ |
260 | int error; |
261 | ||
bfd4e82c MK |
262 | switch (so->so_type) { |
263 | ||
264 | case SOCK_STREAM: | |
265 | error = soreserve(so, unpst_sendspace, unpst_recvspace); | |
266 | break; | |
267 | ||
268 | case SOCK_DGRAM: | |
269 | error = soreserve(so, unpdg_sendspace, unpdg_recvspace); | |
270 | break; | |
271 | } | |
d6213d15 | 272 | if (error) |
5fe6f9d1 | 273 | return (error); |
cce93e4b | 274 | m = m_getclr(M_DONTWAIT, MT_PCB); |
5fe6f9d1 SL |
275 | if (m == NULL) |
276 | return (ENOBUFS); | |
d6213d15 BJ |
277 | unp = mtod(m, struct unpcb *); |
278 | so->so_pcb = (caddr_t)unp; | |
279 | unp->unp_socket = so; | |
d6213d15 | 280 | return (0); |
d6213d15 BJ |
281 | } |
282 | ||
d6213d15 | 283 | unp_detach(unp) |
4f083fd7 | 284 | register struct unpcb *unp; |
d6213d15 BJ |
285 | { |
286 | ||
287 | if (unp->unp_inode) { | |
148703ec | 288 | unp->unp_inode->i_socket = 0; |
d6213d15 BJ |
289 | irele(unp->unp_inode); |
290 | unp->unp_inode = 0; | |
291 | } | |
292 | if (unp->unp_conn) | |
293 | unp_disconnect(unp); | |
294 | while (unp->unp_refs) | |
295 | unp_drop(unp->unp_refs, ECONNRESET); | |
296 | soisdisconnected(unp->unp_socket); | |
297 | unp->unp_socket->so_pcb = 0; | |
4f083fd7 SL |
298 | m_freem(unp->unp_remaddr); |
299 | (void) m_free(dtom(unp)); | |
d6213d15 BJ |
300 | } |
301 | ||
4f083fd7 | 302 | unp_bind(unp, nam) |
d6213d15 | 303 | struct unpcb *unp; |
4f083fd7 | 304 | struct mbuf *nam; |
d6213d15 | 305 | { |
4f083fd7 | 306 | struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); |
d6213d15 | 307 | register struct inode *ip; |
715baff1 | 308 | register struct nameidata *ndp = &u.u_nd; |
4f083fd7 | 309 | int error; |
d6213d15 | 310 | |
715baff1 | 311 | ndp->ni_dirp = soun->sun_path; |
88a7a62a SL |
312 | if (nam->m_len == MLEN) |
313 | return (EINVAL); | |
314 | *(mtod(nam, caddr_t) + nam->m_len) = 0; | |
315 | /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ | |
715baff1 KM |
316 | ndp->ni_nameiop = CREATE | FOLLOW; |
317 | ndp->ni_segflg = UIO_SYSSPACE; | |
318 | ip = namei(ndp); | |
d6213d15 BJ |
319 | if (ip) { |
320 | iput(ip); | |
5fe6f9d1 | 321 | return (EADDRINUSE); |
d6213d15 | 322 | } |
f3bb1ae7 SL |
323 | if (error = u.u_error) { |
324 | u.u_error = 0; /* XXX */ | |
325 | return (error); | |
326 | } | |
715baff1 | 327 | ip = maknode(IFSOCK | 0777, ndp); |
d6213d15 BJ |
328 | if (ip == NULL) { |
329 | error = u.u_error; /* XXX */ | |
330 | u.u_error = 0; /* XXX */ | |
331 | return (error); | |
332 | } | |
333 | ip->i_socket = unp->unp_socket; | |
334 | unp->unp_inode = ip; | |
335 | iunlock(ip); /* but keep reference */ | |
336 | return (0); | |
337 | } | |
338 | ||
4f083fd7 | 339 | unp_connect(so, nam) |
d6213d15 | 340 | struct socket *so; |
4f083fd7 | 341 | struct mbuf *nam; |
d6213d15 | 342 | { |
4f083fd7 | 343 | register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); |
4f083fd7 | 344 | register struct inode *ip; |
d6213d15 | 345 | int error; |
88a7a62a | 346 | register struct socket *so2; |
715baff1 | 347 | register struct nameidata *ndp = &u.u_nd; |
d6213d15 | 348 | |
715baff1 | 349 | ndp->ni_dirp = soun->sun_path; |
88a7a62a SL |
350 | if (nam->m_len + (nam->m_off - MMINOFF) == MLEN) |
351 | return (EMSGSIZE); | |
352 | *(mtod(nam, caddr_t) + nam->m_len) = 0; | |
715baff1 KM |
353 | ndp->ni_nameiop = LOOKUP | FOLLOW; |
354 | ndp->ni_segflg = UIO_SYSSPACE; | |
355 | ip = namei(ndp); | |
d6213d15 BJ |
356 | if (ip == 0) { |
357 | error = u.u_error; | |
358 | u.u_error = 0; | |
5fe6f9d1 | 359 | return (error); /* XXX */ |
d6213d15 | 360 | } |
9c40f33e MK |
361 | if (access(ip, IWRITE)) { |
362 | error = u.u_error; | |
363 | u.u_error = 0; /* XXX */ | |
364 | goto bad; | |
365 | } | |
d6213d15 BJ |
366 | if ((ip->i_mode&IFMT) != IFSOCK) { |
367 | error = ENOTSOCK; | |
368 | goto bad; | |
369 | } | |
370 | so2 = ip->i_socket; | |
371 | if (so2 == 0) { | |
372 | error = ECONNREFUSED; | |
373 | goto bad; | |
374 | } | |
5a48956d SL |
375 | if (so->so_type != so2->so_type) { |
376 | error = EPROTOTYPE; | |
377 | goto bad; | |
378 | } | |
379 | if (so->so_proto->pr_flags & PR_CONNREQUIRED && | |
380 | ((so2->so_options&SO_ACCEPTCONN) == 0 || | |
381 | (so2 = sonewconn(so2)) == 0)) { | |
382 | error = ECONNREFUSED; | |
383 | goto bad; | |
384 | } | |
88a7a62a SL |
385 | error = unp_connect2(so, nam, so2); |
386 | bad: | |
387 | iput(ip); | |
388 | return (error); | |
389 | } | |
390 | ||
391 | unp_connect2(so, sonam, so2) | |
392 | register struct socket *so; | |
393 | struct mbuf *sonam; | |
394 | register struct socket *so2; | |
395 | { | |
396 | register struct unpcb *unp = sotounpcb(so); | |
397 | register struct unpcb *unp2; | |
398 | ||
399 | if (so2->so_type != so->so_type) | |
400 | return (EPROTOTYPE); | |
ccffacbd SL |
401 | unp2 = sotounpcb(so2); |
402 | unp->unp_conn = unp2; | |
d6213d15 BJ |
403 | switch (so->so_type) { |
404 | ||
405 | case SOCK_DGRAM: | |
d6213d15 BJ |
406 | unp->unp_nextref = unp2->unp_refs; |
407 | unp2->unp_refs = unp; | |
9c40f33e | 408 | soisconnected(so); |
d6213d15 BJ |
409 | break; |
410 | ||
411 | case SOCK_STREAM: | |
4f083fd7 | 412 | unp2->unp_conn = unp; |
88a7a62a SL |
413 | if (sonam) |
414 | unp2->unp_remaddr = m_copy(sonam, 0, (int)M_COPYALL); | |
ccffacbd SL |
415 | soisconnected(so2); |
416 | soisconnected(so); | |
d6213d15 BJ |
417 | break; |
418 | ||
419 | default: | |
88a7a62a | 420 | panic("unp_connect2"); |
d6213d15 | 421 | } |
d6213d15 | 422 | return (0); |
d6213d15 | 423 | } |
4f083fd7 SL |
424 | |
425 | unp_disconnect(unp) | |
426 | struct unpcb *unp; | |
427 | { | |
428 | register struct unpcb *unp2 = unp->unp_conn; | |
429 | ||
430 | if (unp2 == 0) | |
431 | return; | |
432 | unp->unp_conn = 0; | |
4f083fd7 SL |
433 | switch (unp->unp_socket->so_type) { |
434 | ||
435 | case SOCK_DGRAM: | |
436 | if (unp2->unp_refs == unp) | |
437 | unp2->unp_refs = unp->unp_nextref; | |
438 | else { | |
439 | unp2 = unp2->unp_refs; | |
440 | for (;;) { | |
441 | if (unp2 == 0) | |
442 | panic("unp_disconnect"); | |
443 | if (unp2->unp_nextref == unp) | |
444 | break; | |
445 | unp2 = unp2->unp_nextref; | |
446 | } | |
447 | unp2->unp_nextref = unp->unp_nextref; | |
448 | } | |
449 | unp->unp_nextref = 0; | |
c5935bca | 450 | unp->unp_socket->so_state &= ~SS_ISCONNECTED; |
4f083fd7 SL |
451 | break; |
452 | ||
453 | case SOCK_STREAM: | |
ccffacbd | 454 | soisdisconnected(unp->unp_socket); |
4f083fd7 SL |
455 | unp2->unp_conn = 0; |
456 | soisdisconnected(unp2->unp_socket); | |
4f083fd7 SL |
457 | break; |
458 | } | |
459 | } | |
460 | ||
88a7a62a | 461 | #ifdef notdef |
4f083fd7 SL |
462 | unp_abort(unp) |
463 | struct unpcb *unp; | |
464 | { | |
465 | ||
466 | unp_detach(unp); | |
467 | } | |
88a7a62a | 468 | #endif |
4f083fd7 SL |
469 | |
470 | /*ARGSUSED*/ | |
471 | unp_usrclosed(unp) | |
472 | struct unpcb *unp; | |
473 | { | |
474 | ||
475 | } | |
476 | ||
477 | unp_drop(unp, errno) | |
478 | struct unpcb *unp; | |
479 | int errno; | |
480 | { | |
d12671e0 | 481 | struct socket *so = unp->unp_socket; |
4f083fd7 | 482 | |
d12671e0 | 483 | so->so_error = errno; |
4f083fd7 | 484 | unp_disconnect(unp); |
d12671e0 MK |
485 | if (so->so_head) { |
486 | so->so_pcb = (caddr_t) 0; | |
cd9b8206 | 487 | m_freem(unp->unp_remaddr); |
d12671e0 MK |
488 | (void) m_free(dtom(unp)); |
489 | sofree(so); | |
490 | } | |
4f083fd7 SL |
491 | } |
492 | ||
88a7a62a | 493 | #ifdef notdef |
4f083fd7 SL |
494 | unp_drain() |
495 | { | |
496 | ||
497 | } | |
88a7a62a SL |
498 | #endif |
499 | ||
500 | unp_externalize(rights) | |
501 | struct mbuf *rights; | |
502 | { | |
503 | int newfds = rights->m_len / sizeof (int); | |
504 | register int i; | |
505 | register struct file **rp = mtod(rights, struct file **); | |
506 | register struct file *fp; | |
507 | int f; | |
508 | ||
509 | if (newfds > ufavail()) { | |
510 | for (i = 0; i < newfds; i++) { | |
511 | fp = *rp; | |
512 | unp_discard(fp); | |
513 | *rp++ = 0; | |
514 | } | |
515 | return (EMSGSIZE); | |
516 | } | |
517 | for (i = 0; i < newfds; i++) { | |
518 | f = ufalloc(0); | |
519 | if (f < 0) | |
520 | panic("unp_externalize"); | |
521 | fp = *rp; | |
522 | u.u_ofile[f] = fp; | |
523 | fp->f_msgcount--; | |
bb3b4851 | 524 | *(int *)rp++ = f; |
88a7a62a SL |
525 | } |
526 | return (0); | |
527 | } | |
528 | ||
529 | unp_internalize(rights) | |
530 | struct mbuf *rights; | |
531 | { | |
532 | register struct file **rp; | |
533 | int oldfds = rights->m_len / sizeof (int); | |
534 | register int i; | |
535 | register struct file *fp; | |
536 | ||
537 | rp = mtod(rights, struct file **); | |
44eb2da3 | 538 | for (i = 0; i < oldfds; i++) |
88a7a62a SL |
539 | if (getf(*(int *)rp++) == 0) |
540 | return (EBADF); | |
541 | rp = mtod(rights, struct file **); | |
44eb2da3 | 542 | for (i = 0; i < oldfds; i++) { |
88a7a62a SL |
543 | fp = getf(*(int *)rp); |
544 | *rp++ = fp; | |
545 | fp->f_count++; | |
546 | fp->f_msgcount++; | |
547 | } | |
548 | return (0); | |
549 | } | |
550 | ||
551 | int unp_defer, unp_gcing; | |
552 | int unp_mark(); | |
261a8548 | 553 | extern struct domain unixdomain; |
88a7a62a SL |
554 | |
555 | unp_gc() | |
556 | { | |
557 | register struct file *fp; | |
558 | register struct socket *so; | |
559 | ||
560 | if (unp_gcing) | |
561 | return; | |
562 | unp_gcing = 1; | |
563 | restart: | |
564 | unp_defer = 0; | |
565 | for (fp = file; fp < fileNFILE; fp++) | |
566 | fp->f_flag &= ~(FMARK|FDEFER); | |
567 | do { | |
568 | for (fp = file; fp < fileNFILE; fp++) { | |
569 | if (fp->f_count == 0) | |
570 | continue; | |
571 | if (fp->f_flag & FDEFER) { | |
572 | fp->f_flag &= ~FDEFER; | |
573 | unp_defer--; | |
574 | } else { | |
575 | if (fp->f_flag & FMARK) | |
576 | continue; | |
577 | if (fp->f_count == fp->f_msgcount) | |
578 | continue; | |
579 | fp->f_flag |= FMARK; | |
580 | } | |
581 | if (fp->f_type != DTYPE_SOCKET) | |
582 | continue; | |
583 | so = (struct socket *)fp->f_data; | |
261a8548 | 584 | if (so->so_proto->pr_domain != &unixdomain || |
c5935bca | 585 | (so->so_proto->pr_flags&PR_RIGHTS) == 0) |
88a7a62a SL |
586 | continue; |
587 | if (so->so_rcv.sb_flags & SB_LOCK) { | |
588 | sbwait(&so->so_rcv); | |
589 | goto restart; | |
590 | } | |
591 | unp_scan(so->so_rcv.sb_mb, unp_mark); | |
592 | } | |
593 | } while (unp_defer); | |
594 | for (fp = file; fp < fileNFILE; fp++) { | |
595 | if (fp->f_count == 0) | |
596 | continue; | |
597 | if (fp->f_count == fp->f_msgcount && (fp->f_flag&FMARK)==0) { | |
598 | if (fp->f_type != DTYPE_SOCKET) | |
599 | panic("unp_gc"); | |
600 | (void) soshutdown((struct socket *)fp->f_data, 0); | |
601 | } | |
602 | } | |
603 | unp_gcing = 0; | |
604 | } | |
605 | ||
261a8548 MK |
606 | unp_dispose(m) |
607 | struct mbuf *m; | |
608 | { | |
609 | int unp_discard(); | |
610 | ||
148703ec MK |
611 | if (m) |
612 | unp_scan(m, unp_discard); | |
261a8548 MK |
613 | } |
614 | ||
615 | unp_scan(m0, op) | |
616 | register struct mbuf *m0; | |
88a7a62a SL |
617 | int (*op)(); |
618 | { | |
261a8548 | 619 | register struct mbuf *m; |
88a7a62a SL |
620 | register struct file **rp; |
621 | register int i; | |
148703ec | 622 | int qfds; |
261a8548 MK |
623 | |
624 | while (m0) { | |
625 | for (m = m0; m; m = m->m_next) | |
626 | if (m->m_type == MT_RIGHTS && m->m_len) { | |
627 | qfds = m->m_len / sizeof (struct file *); | |
628 | rp = mtod(m, struct file **); | |
629 | for (i = 0; i < qfds; i++) | |
630 | (*op)(*rp++); | |
631 | break; /* XXX, but saves time */ | |
632 | } | |
148703ec | 633 | m0 = m0->m_act; |
88a7a62a | 634 | } |
88a7a62a SL |
635 | } |
636 | ||
637 | unp_mark(fp) | |
638 | struct file *fp; | |
639 | { | |
640 | ||
641 | if (fp->f_flag & FMARK) | |
642 | return; | |
643 | unp_defer++; | |
644 | fp->f_flag |= (FMARK|FDEFER); | |
645 | } | |
646 | ||
647 | unp_discard(fp) | |
648 | struct file *fp; | |
649 | { | |
650 | ||
651 | fp->f_msgcount--; | |
44eb2da3 | 652 | closef(fp); |
88a7a62a | 653 | } |