49#include <sys/file.h> 50#include <sys/errno.h> 51#include <sys/malloc.h> 52#include <sys/un.h> 53#include <miscfs/fifofs/fifo.h> 54 55/* 56 * This structure is associated with the FIFO vnode and stores 57 * the state associated with the FIFO. 58 */ 59struct fifoinfo { 60 struct socket *fi_readsock; 61 struct socket *fi_writesock; 62 long fi_readers; 63 long fi_writers; 64}; 65 66static int fifo_ebadf __P((void)); 67static int fifo_print __P((struct vop_print_args *)); 68 69vop_t **fifo_vnodeop_p; 70static struct vnodeopv_entry_desc fifo_vnodeop_entries[] = { 71 { &vop_default_desc, (vop_t *)vn_default_error }, 72 { &vop_lookup_desc, (vop_t *)fifo_lookup }, /* lookup */ 73 { &vop_create_desc, (vop_t *)fifo_create }, /* create */ 74 { &vop_mknod_desc, (vop_t *)fifo_mknod }, /* mknod */ 75 { &vop_open_desc, (vop_t *)fifo_open }, /* open */ 76 { &vop_close_desc, (vop_t *)fifo_close }, /* close */ 77 { &vop_access_desc, (vop_t *)fifo_access }, /* access */ 78 { &vop_getattr_desc, (vop_t *)fifo_getattr }, /* getattr */ 79 { &vop_setattr_desc, (vop_t *)fifo_setattr }, /* setattr */ 80 { &vop_read_desc, (vop_t *)fifo_read }, /* read */ 81 { &vop_write_desc, (vop_t *)fifo_write }, /* write */ 82 { &vop_lease_desc, (vop_t *)fifo_lease_check }, /* lease */ 83 { &vop_ioctl_desc, (vop_t *)fifo_ioctl }, /* ioctl */ 84 { &vop_select_desc, (vop_t *)fifo_select }, /* select */ 85 { &vop_revoke_desc, (vop_t *)fifo_revoke }, /* revoke */ 86 { &vop_mmap_desc, (vop_t *)fifo_mmap }, /* mmap */ 87 { &vop_fsync_desc, (vop_t *)fifo_fsync }, /* fsync */ 88 { &vop_seek_desc, (vop_t *)fifo_seek }, /* seek */ 89 { &vop_remove_desc, (vop_t *)fifo_remove }, /* remove */ 90 { &vop_link_desc, (vop_t *)fifo_link }, /* link */ 91 { &vop_rename_desc, (vop_t *)fifo_rename }, /* rename */ 92 { &vop_mkdir_desc, (vop_t *)fifo_mkdir }, /* mkdir */ 93 { &vop_rmdir_desc, (vop_t *)fifo_rmdir }, /* rmdir */ 94 { &vop_symlink_desc, (vop_t *)fifo_symlink }, /* symlink */ 95 { &vop_readdir_desc, (vop_t *)fifo_readdir }, /* readdir */ 96 { &vop_readlink_desc, (vop_t *)fifo_readlink }, /* readlink */ 97 { &vop_abortop_desc, (vop_t *)fifo_abortop }, /* abortop */ 98 { &vop_inactive_desc, (vop_t *)fifo_inactive }, /* inactive */ 99 { &vop_reclaim_desc, (vop_t *)fifo_reclaim }, /* reclaim */ 100 { &vop_lock_desc, (vop_t *)fifo_lock }, /* lock */ 101 { &vop_unlock_desc, (vop_t *)fifo_unlock }, /* unlock */ 102 { &vop_bmap_desc, (vop_t *)fifo_bmap }, /* bmap */ 103 { &vop_strategy_desc, (vop_t *)fifo_strategy }, /* strategy */ 104 { &vop_print_desc, (vop_t *)fifo_print }, /* print */ 105 { &vop_islocked_desc, (vop_t *)fifo_islocked }, /* islocked */ 106 { &vop_pathconf_desc, (vop_t *)fifo_pathconf }, /* pathconf */ 107 { &vop_advlock_desc, (vop_t *)fifo_advlock }, /* advlock */ 108 { &vop_blkatoff_desc, (vop_t *)fifo_blkatoff }, /* blkatoff */ 109 { &vop_valloc_desc, (vop_t *)fifo_valloc }, /* valloc */ 110 { &vop_vfree_desc, (vop_t *)fifo_vfree }, /* vfree */ 111 { &vop_truncate_desc, (vop_t *)fifo_truncate }, /* truncate */ 112 { &vop_update_desc, (vop_t *)fifo_update }, /* update */ 113 { &vop_bwrite_desc, (vop_t *)fifo_bwrite }, /* bwrite */ 114 { NULL, NULL } 115}; 116static struct vnodeopv_desc fifo_vnodeop_opv_desc = 117 { &fifo_vnodeop_p, fifo_vnodeop_entries }; 118 119VNODEOP_SET(fifo_vnodeop_opv_desc); 120 121/* 122 * Trivial lookup routine that always fails. 123 */ 124/* ARGSUSED */ 125int 126fifo_lookup(ap) 127 struct vop_lookup_args /* { 128 struct vnode * a_dvp; 129 struct vnode ** a_vpp; 130 struct componentname * a_cnp; 131 } */ *ap; 132{ 133 134 *ap->a_vpp = NULL; 135 return (ENOTDIR); 136} 137 138/* 139 * Open called to set up a new instance of a fifo or 140 * to find an active instance of a fifo. 141 */ 142/* ARGSUSED */ 143int 144fifo_open(ap) 145 struct vop_open_args /* { 146 struct vnode *a_vp; 147 int a_mode; 148 struct ucred *a_cred; 149 struct proc *a_p; 150 } */ *ap; 151{ 152 struct vnode *vp = ap->a_vp; 153 struct fifoinfo *fip; 154 struct proc *p = ap->a_p; 155 struct socket *rso, *wso; 156 int error; 157 static char openstr[] = "fifo"; 158 159 if ((fip = vp->v_fifoinfo) == NULL) { 160 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK); 161 vp->v_fifoinfo = fip; 162 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, ap->a_p); 163 if (error) { 164 free(fip, M_VNODE); 165 vp->v_fifoinfo = NULL; 166 return (error); 167 } 168 fip->fi_readsock = rso; 169 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, ap->a_p); 170 if (error) { 171 (void)soclose(rso); 172 free(fip, M_VNODE); 173 vp->v_fifoinfo = NULL; 174 return (error); 175 } 176 fip->fi_writesock = wso; 177 error = unp_connect2(wso, rso); 178 if (error) { 179 (void)soclose(wso); 180 (void)soclose(rso); 181 free(fip, M_VNODE); 182 vp->v_fifoinfo = NULL; 183 return (error); 184 } 185 fip->fi_readers = fip->fi_writers = 0; 186 wso->so_state |= SS_CANTRCVMORE; 187 rso->so_state |= SS_CANTSENDMORE; 188 } 189 if (ap->a_mode & FREAD) { 190 fip->fi_readers++; 191 if (fip->fi_readers == 1) { 192 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE; 193 if (fip->fi_writers > 0) 194 wakeup((caddr_t)&fip->fi_writers); 195 } 196 } 197 if (ap->a_mode & FWRITE) { 198 fip->fi_writers++; 199 if (fip->fi_writers == 1) { 200 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE; 201 if (fip->fi_readers > 0) 202 wakeup((caddr_t)&fip->fi_readers); 203 } 204 } 205 if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) { 206 while (fip->fi_writers == 0) { 207 VOP_UNLOCK(vp, 0, p); 208 error = tsleep((caddr_t)&fip->fi_readers, 209 PCATCH | PSOCK, openstr, 0); 210 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 211 if (error) 212 goto bad; 213 } 214 } 215 if (ap->a_mode & FWRITE) { 216 if (ap->a_mode & O_NONBLOCK) { 217 if (fip->fi_readers == 0) { 218 error = ENXIO; 219 goto bad; 220 } 221 } else { 222 while (fip->fi_readers == 0) { 223 VOP_UNLOCK(vp, 0, p); 224 error = tsleep((caddr_t)&fip->fi_writers, 225 PCATCH | PSOCK, openstr, 0); 226 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 227 if (error) 228 goto bad; 229 } 230 } 231 } 232 return (0); 233bad: 234 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p); 235 return (error); 236} 237 238/* 239 * Vnode op for read 240 */ 241/* ARGSUSED */ 242int 243fifo_read(ap) 244 struct vop_read_args /* { 245 struct vnode *a_vp; 246 struct uio *a_uio; 247 int a_ioflag; 248 struct ucred *a_cred; 249 } */ *ap; 250{ 251 struct uio *uio = ap->a_uio; 252 struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock; 253 struct proc *p = uio->uio_procp; 254 int error, startresid; 255 256#ifdef DIAGNOSTIC 257 if (uio->uio_rw != UIO_READ) 258 panic("fifo_read mode"); 259#endif 260 if (uio->uio_resid == 0) 261 return (0); 262 if (ap->a_ioflag & IO_NDELAY) 263 rso->so_state |= SS_NBIO; 264 startresid = uio->uio_resid; 265 VOP_UNLOCK(ap->a_vp, 0, p); 266 error = soreceive(rso, (struct mbuf **)0, uio, (struct mbuf **)0, 267 (struct mbuf **)0, (int *)0); 268 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p); 269 /* 270 * Clear EOF indication after first such return. 271 */ 272 if (uio->uio_resid == startresid) 273 rso->so_state &= ~SS_CANTRCVMORE; 274 if (ap->a_ioflag & IO_NDELAY) 275 rso->so_state &= ~SS_NBIO; 276 return (error); 277} 278 279/* 280 * Vnode op for write 281 */ 282/* ARGSUSED */ 283int 284fifo_write(ap) 285 struct vop_write_args /* { 286 struct vnode *a_vp; 287 struct uio *a_uio; 288 int a_ioflag; 289 struct ucred *a_cred; 290 } */ *ap; 291{ 292 struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock; 293 struct proc *p = ap->a_uio->uio_procp; 294 int error; 295 296#ifdef DIAGNOSTIC 297 if (ap->a_uio->uio_rw != UIO_WRITE) 298 panic("fifo_write mode"); 299#endif 300 if (ap->a_ioflag & IO_NDELAY) 301 wso->so_state |= SS_NBIO; 302 VOP_UNLOCK(ap->a_vp, 0, p); 303 error = sosend(wso, (struct mbuf *)0, ap->a_uio, 0, (struct mbuf *)0, 0); 304 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p); 305 if (ap->a_ioflag & IO_NDELAY) 306 wso->so_state &= ~SS_NBIO; 307 return (error); 308} 309 310/* 311 * Device ioctl operation. 312 */ 313/* ARGSUSED */ 314int 315fifo_ioctl(ap) 316 struct vop_ioctl_args /* { 317 struct vnode *a_vp; 318 int a_command; 319 caddr_t a_data; 320 int a_fflag; 321 struct ucred *a_cred; 322 struct proc *a_p; 323 } */ *ap; 324{ 325 struct file filetmp; 326 int error; 327 328 if (ap->a_command == FIONBIO) 329 return (0); 330 if (ap->a_fflag & FREAD) { 331 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; 332 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p); 333 if (error) 334 return (error); 335 } 336 if (ap->a_fflag & FWRITE) { 337 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; 338 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p); 339 if (error) 340 return (error); 341 } 342 return (0); 343} 344 345/* ARGSUSED */ 346int 347fifo_select(ap) 348 struct vop_select_args /* { 349 struct vnode *a_vp; 350 int a_which; 351 int a_fflags; 352 struct ucred *a_cred; 353 struct proc *a_p; 354 } */ *ap; 355{ 356 struct file filetmp; 357 int ready; 358 359 if (ap->a_fflags & FREAD) { 360 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; 361 ready = soo_select(&filetmp, ap->a_which, ap->a_p); 362 if (ready) 363 return (ready); 364 } 365 if (ap->a_fflags & FWRITE) { 366 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; 367 ready = soo_select(&filetmp, ap->a_which, ap->a_p); 368 if (ready) 369 return (ready); 370 } 371 return (0); 372} 373 374int 375fifo_inactive(ap) 376 struct vop_inactive_args /* { 377 struct vnode *a_vp; 378 struct proc *a_p; 379 } */ *ap; 380{ 381 382 VOP_UNLOCK(ap->a_vp, 0, ap->a_p); 383 return (0); 384} 385 386/* 387 * This is a noop, simply returning what one has been given. 388 */ 389int 390fifo_bmap(ap) 391 struct vop_bmap_args /* { 392 struct vnode *a_vp; 393 daddr_t a_bn; 394 struct vnode **a_vpp; 395 daddr_t *a_bnp; 396 int *a_runp; 397 int *a_runb; 398 } */ *ap; 399{ 400 401 if (ap->a_vpp != NULL) 402 *ap->a_vpp = ap->a_vp; 403 if (ap->a_bnp != NULL) 404 *ap->a_bnp = ap->a_bn; 405 if (ap->a_runp != NULL) 406 *ap->a_runp = 0; 407 if (ap->a_runb != NULL) 408 *ap->a_runb = 0; 409 return (0); 410} 411 412/* 413 * Device close routine 414 */ 415/* ARGSUSED */ 416int 417fifo_close(ap) 418 struct vop_close_args /* { 419 struct vnode *a_vp; 420 int a_fflag; 421 struct ucred *a_cred; 422 struct proc *a_p; 423 } */ *ap; 424{ 425 register struct vnode *vp = ap->a_vp; 426 register struct fifoinfo *fip = vp->v_fifoinfo; 427 int error1, error2; 428 429 if (ap->a_fflag & FREAD) { 430 fip->fi_readers--; 431 if (fip->fi_readers == 0) 432 socantsendmore(fip->fi_writesock); 433 } 434 if (ap->a_fflag & FWRITE) { 435 fip->fi_writers--; 436 if (fip->fi_writers == 0) 437 socantrcvmore(fip->fi_readsock); 438 } 439 if (vp->v_usecount > 1) 440 return (0); 441 error1 = soclose(fip->fi_readsock); 442 error2 = soclose(fip->fi_writesock); 443 FREE(fip, M_VNODE); 444 vp->v_fifoinfo = NULL; 445 if (error1) 446 return (error1); 447 return (error2); 448} 449 450 451/* 452 * Print out internal contents of a fifo vnode. 453 */ 454int 455fifo_printinfo(vp) 456 struct vnode *vp; 457{ 458 register struct fifoinfo *fip = vp->v_fifoinfo; 459 460 printf(", fifo with %ld readers and %ld writers", 461 fip->fi_readers, fip->fi_writers); 462 return (0); 463} 464 465/* 466 * Print out the contents of a fifo vnode. 467 */ 468static int 469fifo_print(ap) 470 struct vop_print_args /* { 471 struct vnode *a_vp; 472 } */ *ap; 473{ 474 475 printf("tag VT_NON"); 476 fifo_printinfo(ap->a_vp); 477 printf("\n"); 478 return (0); 479} 480 481/* 482 * Return POSIX pathconf information applicable to fifo's. 483 */ 484int 485fifo_pathconf(ap) 486 struct vop_pathconf_args /* { 487 struct vnode *a_vp; 488 int a_name; 489 int *a_retval; 490 } */ *ap; 491{ 492 493 switch (ap->a_name) { 494 case _PC_LINK_MAX: 495 *ap->a_retval = LINK_MAX; 496 return (0); 497 case _PC_PIPE_BUF: 498 *ap->a_retval = PIPE_BUF; 499 return (0); 500 case _PC_CHOWN_RESTRICTED: 501 *ap->a_retval = 1; 502 return (0); 503 default: 504 return (EINVAL); 505 } 506 /* NOTREACHED */ 507} 508 509/* 510 * Fifo failed operation 511 */ 512static int 513fifo_ebadf() 514{ 515 516 return (EBADF); 517} 518 519/* 520 * Fifo advisory byte-level locks. 521 */ 522/* ARGSUSED */ 523int 524fifo_advlock(ap) 525 struct vop_advlock_args /* { 526 struct vnode *a_vp; 527 caddr_t a_id; 528 int a_op; 529 struct flock *a_fl; 530 int a_flags; 531 } */ *ap; 532{ 533 534 return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL); 535} 536 537/* 538 * Fifo bad operation 539 */ 540int 541fifo_badop() 542{ 543 544 panic("fifo_badop called"); 545 /* NOTREACHED */ 546}
| 50#include <sys/file.h> 51#include <sys/errno.h> 52#include <sys/malloc.h> 53#include <sys/un.h> 54#include <miscfs/fifofs/fifo.h> 55 56/* 57 * This structure is associated with the FIFO vnode and stores 58 * the state associated with the FIFO. 59 */ 60struct fifoinfo { 61 struct socket *fi_readsock; 62 struct socket *fi_writesock; 63 long fi_readers; 64 long fi_writers; 65}; 66 67static int fifo_ebadf __P((void)); 68static int fifo_print __P((struct vop_print_args *)); 69 70vop_t **fifo_vnodeop_p; 71static struct vnodeopv_entry_desc fifo_vnodeop_entries[] = { 72 { &vop_default_desc, (vop_t *)vn_default_error }, 73 { &vop_lookup_desc, (vop_t *)fifo_lookup }, /* lookup */ 74 { &vop_create_desc, (vop_t *)fifo_create }, /* create */ 75 { &vop_mknod_desc, (vop_t *)fifo_mknod }, /* mknod */ 76 { &vop_open_desc, (vop_t *)fifo_open }, /* open */ 77 { &vop_close_desc, (vop_t *)fifo_close }, /* close */ 78 { &vop_access_desc, (vop_t *)fifo_access }, /* access */ 79 { &vop_getattr_desc, (vop_t *)fifo_getattr }, /* getattr */ 80 { &vop_setattr_desc, (vop_t *)fifo_setattr }, /* setattr */ 81 { &vop_read_desc, (vop_t *)fifo_read }, /* read */ 82 { &vop_write_desc, (vop_t *)fifo_write }, /* write */ 83 { &vop_lease_desc, (vop_t *)fifo_lease_check }, /* lease */ 84 { &vop_ioctl_desc, (vop_t *)fifo_ioctl }, /* ioctl */ 85 { &vop_select_desc, (vop_t *)fifo_select }, /* select */ 86 { &vop_revoke_desc, (vop_t *)fifo_revoke }, /* revoke */ 87 { &vop_mmap_desc, (vop_t *)fifo_mmap }, /* mmap */ 88 { &vop_fsync_desc, (vop_t *)fifo_fsync }, /* fsync */ 89 { &vop_seek_desc, (vop_t *)fifo_seek }, /* seek */ 90 { &vop_remove_desc, (vop_t *)fifo_remove }, /* remove */ 91 { &vop_link_desc, (vop_t *)fifo_link }, /* link */ 92 { &vop_rename_desc, (vop_t *)fifo_rename }, /* rename */ 93 { &vop_mkdir_desc, (vop_t *)fifo_mkdir }, /* mkdir */ 94 { &vop_rmdir_desc, (vop_t *)fifo_rmdir }, /* rmdir */ 95 { &vop_symlink_desc, (vop_t *)fifo_symlink }, /* symlink */ 96 { &vop_readdir_desc, (vop_t *)fifo_readdir }, /* readdir */ 97 { &vop_readlink_desc, (vop_t *)fifo_readlink }, /* readlink */ 98 { &vop_abortop_desc, (vop_t *)fifo_abortop }, /* abortop */ 99 { &vop_inactive_desc, (vop_t *)fifo_inactive }, /* inactive */ 100 { &vop_reclaim_desc, (vop_t *)fifo_reclaim }, /* reclaim */ 101 { &vop_lock_desc, (vop_t *)fifo_lock }, /* lock */ 102 { &vop_unlock_desc, (vop_t *)fifo_unlock }, /* unlock */ 103 { &vop_bmap_desc, (vop_t *)fifo_bmap }, /* bmap */ 104 { &vop_strategy_desc, (vop_t *)fifo_strategy }, /* strategy */ 105 { &vop_print_desc, (vop_t *)fifo_print }, /* print */ 106 { &vop_islocked_desc, (vop_t *)fifo_islocked }, /* islocked */ 107 { &vop_pathconf_desc, (vop_t *)fifo_pathconf }, /* pathconf */ 108 { &vop_advlock_desc, (vop_t *)fifo_advlock }, /* advlock */ 109 { &vop_blkatoff_desc, (vop_t *)fifo_blkatoff }, /* blkatoff */ 110 { &vop_valloc_desc, (vop_t *)fifo_valloc }, /* valloc */ 111 { &vop_vfree_desc, (vop_t *)fifo_vfree }, /* vfree */ 112 { &vop_truncate_desc, (vop_t *)fifo_truncate }, /* truncate */ 113 { &vop_update_desc, (vop_t *)fifo_update }, /* update */ 114 { &vop_bwrite_desc, (vop_t *)fifo_bwrite }, /* bwrite */ 115 { NULL, NULL } 116}; 117static struct vnodeopv_desc fifo_vnodeop_opv_desc = 118 { &fifo_vnodeop_p, fifo_vnodeop_entries }; 119 120VNODEOP_SET(fifo_vnodeop_opv_desc); 121 122/* 123 * Trivial lookup routine that always fails. 124 */ 125/* ARGSUSED */ 126int 127fifo_lookup(ap) 128 struct vop_lookup_args /* { 129 struct vnode * a_dvp; 130 struct vnode ** a_vpp; 131 struct componentname * a_cnp; 132 } */ *ap; 133{ 134 135 *ap->a_vpp = NULL; 136 return (ENOTDIR); 137} 138 139/* 140 * Open called to set up a new instance of a fifo or 141 * to find an active instance of a fifo. 142 */ 143/* ARGSUSED */ 144int 145fifo_open(ap) 146 struct vop_open_args /* { 147 struct vnode *a_vp; 148 int a_mode; 149 struct ucred *a_cred; 150 struct proc *a_p; 151 } */ *ap; 152{ 153 struct vnode *vp = ap->a_vp; 154 struct fifoinfo *fip; 155 struct proc *p = ap->a_p; 156 struct socket *rso, *wso; 157 int error; 158 static char openstr[] = "fifo"; 159 160 if ((fip = vp->v_fifoinfo) == NULL) { 161 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK); 162 vp->v_fifoinfo = fip; 163 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, ap->a_p); 164 if (error) { 165 free(fip, M_VNODE); 166 vp->v_fifoinfo = NULL; 167 return (error); 168 } 169 fip->fi_readsock = rso; 170 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, ap->a_p); 171 if (error) { 172 (void)soclose(rso); 173 free(fip, M_VNODE); 174 vp->v_fifoinfo = NULL; 175 return (error); 176 } 177 fip->fi_writesock = wso; 178 error = unp_connect2(wso, rso); 179 if (error) { 180 (void)soclose(wso); 181 (void)soclose(rso); 182 free(fip, M_VNODE); 183 vp->v_fifoinfo = NULL; 184 return (error); 185 } 186 fip->fi_readers = fip->fi_writers = 0; 187 wso->so_state |= SS_CANTRCVMORE; 188 rso->so_state |= SS_CANTSENDMORE; 189 } 190 if (ap->a_mode & FREAD) { 191 fip->fi_readers++; 192 if (fip->fi_readers == 1) { 193 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE; 194 if (fip->fi_writers > 0) 195 wakeup((caddr_t)&fip->fi_writers); 196 } 197 } 198 if (ap->a_mode & FWRITE) { 199 fip->fi_writers++; 200 if (fip->fi_writers == 1) { 201 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE; 202 if (fip->fi_readers > 0) 203 wakeup((caddr_t)&fip->fi_readers); 204 } 205 } 206 if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) { 207 while (fip->fi_writers == 0) { 208 VOP_UNLOCK(vp, 0, p); 209 error = tsleep((caddr_t)&fip->fi_readers, 210 PCATCH | PSOCK, openstr, 0); 211 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 212 if (error) 213 goto bad; 214 } 215 } 216 if (ap->a_mode & FWRITE) { 217 if (ap->a_mode & O_NONBLOCK) { 218 if (fip->fi_readers == 0) { 219 error = ENXIO; 220 goto bad; 221 } 222 } else { 223 while (fip->fi_readers == 0) { 224 VOP_UNLOCK(vp, 0, p); 225 error = tsleep((caddr_t)&fip->fi_writers, 226 PCATCH | PSOCK, openstr, 0); 227 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 228 if (error) 229 goto bad; 230 } 231 } 232 } 233 return (0); 234bad: 235 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p); 236 return (error); 237} 238 239/* 240 * Vnode op for read 241 */ 242/* ARGSUSED */ 243int 244fifo_read(ap) 245 struct vop_read_args /* { 246 struct vnode *a_vp; 247 struct uio *a_uio; 248 int a_ioflag; 249 struct ucred *a_cred; 250 } */ *ap; 251{ 252 struct uio *uio = ap->a_uio; 253 struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock; 254 struct proc *p = uio->uio_procp; 255 int error, startresid; 256 257#ifdef DIAGNOSTIC 258 if (uio->uio_rw != UIO_READ) 259 panic("fifo_read mode"); 260#endif 261 if (uio->uio_resid == 0) 262 return (0); 263 if (ap->a_ioflag & IO_NDELAY) 264 rso->so_state |= SS_NBIO; 265 startresid = uio->uio_resid; 266 VOP_UNLOCK(ap->a_vp, 0, p); 267 error = soreceive(rso, (struct mbuf **)0, uio, (struct mbuf **)0, 268 (struct mbuf **)0, (int *)0); 269 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p); 270 /* 271 * Clear EOF indication after first such return. 272 */ 273 if (uio->uio_resid == startresid) 274 rso->so_state &= ~SS_CANTRCVMORE; 275 if (ap->a_ioflag & IO_NDELAY) 276 rso->so_state &= ~SS_NBIO; 277 return (error); 278} 279 280/* 281 * Vnode op for write 282 */ 283/* ARGSUSED */ 284int 285fifo_write(ap) 286 struct vop_write_args /* { 287 struct vnode *a_vp; 288 struct uio *a_uio; 289 int a_ioflag; 290 struct ucred *a_cred; 291 } */ *ap; 292{ 293 struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock; 294 struct proc *p = ap->a_uio->uio_procp; 295 int error; 296 297#ifdef DIAGNOSTIC 298 if (ap->a_uio->uio_rw != UIO_WRITE) 299 panic("fifo_write mode"); 300#endif 301 if (ap->a_ioflag & IO_NDELAY) 302 wso->so_state |= SS_NBIO; 303 VOP_UNLOCK(ap->a_vp, 0, p); 304 error = sosend(wso, (struct mbuf *)0, ap->a_uio, 0, (struct mbuf *)0, 0); 305 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p); 306 if (ap->a_ioflag & IO_NDELAY) 307 wso->so_state &= ~SS_NBIO; 308 return (error); 309} 310 311/* 312 * Device ioctl operation. 313 */ 314/* ARGSUSED */ 315int 316fifo_ioctl(ap) 317 struct vop_ioctl_args /* { 318 struct vnode *a_vp; 319 int a_command; 320 caddr_t a_data; 321 int a_fflag; 322 struct ucred *a_cred; 323 struct proc *a_p; 324 } */ *ap; 325{ 326 struct file filetmp; 327 int error; 328 329 if (ap->a_command == FIONBIO) 330 return (0); 331 if (ap->a_fflag & FREAD) { 332 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; 333 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p); 334 if (error) 335 return (error); 336 } 337 if (ap->a_fflag & FWRITE) { 338 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; 339 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p); 340 if (error) 341 return (error); 342 } 343 return (0); 344} 345 346/* ARGSUSED */ 347int 348fifo_select(ap) 349 struct vop_select_args /* { 350 struct vnode *a_vp; 351 int a_which; 352 int a_fflags; 353 struct ucred *a_cred; 354 struct proc *a_p; 355 } */ *ap; 356{ 357 struct file filetmp; 358 int ready; 359 360 if (ap->a_fflags & FREAD) { 361 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; 362 ready = soo_select(&filetmp, ap->a_which, ap->a_p); 363 if (ready) 364 return (ready); 365 } 366 if (ap->a_fflags & FWRITE) { 367 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; 368 ready = soo_select(&filetmp, ap->a_which, ap->a_p); 369 if (ready) 370 return (ready); 371 } 372 return (0); 373} 374 375int 376fifo_inactive(ap) 377 struct vop_inactive_args /* { 378 struct vnode *a_vp; 379 struct proc *a_p; 380 } */ *ap; 381{ 382 383 VOP_UNLOCK(ap->a_vp, 0, ap->a_p); 384 return (0); 385} 386 387/* 388 * This is a noop, simply returning what one has been given. 389 */ 390int 391fifo_bmap(ap) 392 struct vop_bmap_args /* { 393 struct vnode *a_vp; 394 daddr_t a_bn; 395 struct vnode **a_vpp; 396 daddr_t *a_bnp; 397 int *a_runp; 398 int *a_runb; 399 } */ *ap; 400{ 401 402 if (ap->a_vpp != NULL) 403 *ap->a_vpp = ap->a_vp; 404 if (ap->a_bnp != NULL) 405 *ap->a_bnp = ap->a_bn; 406 if (ap->a_runp != NULL) 407 *ap->a_runp = 0; 408 if (ap->a_runb != NULL) 409 *ap->a_runb = 0; 410 return (0); 411} 412 413/* 414 * Device close routine 415 */ 416/* ARGSUSED */ 417int 418fifo_close(ap) 419 struct vop_close_args /* { 420 struct vnode *a_vp; 421 int a_fflag; 422 struct ucred *a_cred; 423 struct proc *a_p; 424 } */ *ap; 425{ 426 register struct vnode *vp = ap->a_vp; 427 register struct fifoinfo *fip = vp->v_fifoinfo; 428 int error1, error2; 429 430 if (ap->a_fflag & FREAD) { 431 fip->fi_readers--; 432 if (fip->fi_readers == 0) 433 socantsendmore(fip->fi_writesock); 434 } 435 if (ap->a_fflag & FWRITE) { 436 fip->fi_writers--; 437 if (fip->fi_writers == 0) 438 socantrcvmore(fip->fi_readsock); 439 } 440 if (vp->v_usecount > 1) 441 return (0); 442 error1 = soclose(fip->fi_readsock); 443 error2 = soclose(fip->fi_writesock); 444 FREE(fip, M_VNODE); 445 vp->v_fifoinfo = NULL; 446 if (error1) 447 return (error1); 448 return (error2); 449} 450 451 452/* 453 * Print out internal contents of a fifo vnode. 454 */ 455int 456fifo_printinfo(vp) 457 struct vnode *vp; 458{ 459 register struct fifoinfo *fip = vp->v_fifoinfo; 460 461 printf(", fifo with %ld readers and %ld writers", 462 fip->fi_readers, fip->fi_writers); 463 return (0); 464} 465 466/* 467 * Print out the contents of a fifo vnode. 468 */ 469static int 470fifo_print(ap) 471 struct vop_print_args /* { 472 struct vnode *a_vp; 473 } */ *ap; 474{ 475 476 printf("tag VT_NON"); 477 fifo_printinfo(ap->a_vp); 478 printf("\n"); 479 return (0); 480} 481 482/* 483 * Return POSIX pathconf information applicable to fifo's. 484 */ 485int 486fifo_pathconf(ap) 487 struct vop_pathconf_args /* { 488 struct vnode *a_vp; 489 int a_name; 490 int *a_retval; 491 } */ *ap; 492{ 493 494 switch (ap->a_name) { 495 case _PC_LINK_MAX: 496 *ap->a_retval = LINK_MAX; 497 return (0); 498 case _PC_PIPE_BUF: 499 *ap->a_retval = PIPE_BUF; 500 return (0); 501 case _PC_CHOWN_RESTRICTED: 502 *ap->a_retval = 1; 503 return (0); 504 default: 505 return (EINVAL); 506 } 507 /* NOTREACHED */ 508} 509 510/* 511 * Fifo failed operation 512 */ 513static int 514fifo_ebadf() 515{ 516 517 return (EBADF); 518} 519 520/* 521 * Fifo advisory byte-level locks. 522 */ 523/* ARGSUSED */ 524int 525fifo_advlock(ap) 526 struct vop_advlock_args /* { 527 struct vnode *a_vp; 528 caddr_t a_id; 529 int a_op; 530 struct flock *a_fl; 531 int a_flags; 532 } */ *ap; 533{ 534 535 return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL); 536} 537 538/* 539 * Fifo bad operation 540 */ 541int 542fifo_badop() 543{ 544 545 panic("fifo_badop called"); 546 /* NOTREACHED */ 547}
|