1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)kern_descrip.c 7.21 (Berkeley) 03/17/91 8 */ 9 10 #include "param.h" 11 #include "systm.h" 12 #include "user.h" 13 #include "filedesc.h" 14 #include "kernel.h" 15 #include "vnode.h" 16 #include "proc.h" 17 #include "file.h" 18 #include "socket.h" 19 #include "socketvar.h" 20 #include "stat.h" 21 #include "ioctl.h" 22 #include "fcntl.h" 23 #include "malloc.h" 24 #include "syslog.h" 25 26 /* 27 * Descriptor management. 28 */ 29 30 /* 31 * System calls on descriptors. 32 */ 33 /* ARGSUSED */ 34 getdtablesize(p, uap, retval) 35 struct proc *p; 36 struct args *uap; 37 int *retval; 38 { 39 40 *retval = p->p_rlimit[RLIMIT_OFILE].rlim_cur; 41 return (0); 42 } 43 44 /* 45 * Duplicate a file descriptor. 46 */ 47 /* ARGSUSED */ 48 dup(p, uap, retval) 49 struct proc *p; 50 struct args { 51 int i; 52 } *uap; 53 int *retval; 54 { 55 register struct filedesc *fdp = p->p_fd; 56 struct file *fp; 57 int fd, error; 58 59 /* 60 * XXX Compatibility 61 */ 62 if (uap->i &~ 077) { uap->i &= 077; return (dup2(p, uap, retval)); } 63 64 if ((unsigned)uap->i >= fdp->fd_nfiles || 65 (fp = OFILE(fdp, uap->i)) == NULL) 66 return (EBADF); 67 if (error = fdalloc(p, 0, &fd)) 68 return (error); 69 OFILE(fdp, fd) = fp; 70 OFILEFLAGS(fdp, fd) = OFILEFLAGS(fdp, uap->i) &~ UF_EXCLOSE; 71 fp->f_count++; 72 if (fd > fdp->fd_lastfile) 73 fdp->fd_lastfile = fd; 74 *retval = fd; 75 return (0); 76 } 77 78 /* 79 * Duplicate a file descriptor to a particular value. 80 */ 81 /* ARGSUSED */ 82 dup2(p, uap, retval) 83 struct proc *p; 84 register struct args { 85 int i; 86 int j; 87 } *uap; 88 int *retval; 89 { 90 register struct filedesc *fdp = p->p_fd; 91 register struct file *fp; 92 int i, error; 93 94 if ((unsigned)uap->i >= fdp->fd_nfiles || 95 (fp = OFILE(fdp, uap->i)) == NULL || 96 (unsigned)uap->j >= p->p_rlimit[RLIMIT_OFILE].rlim_cur) 97 return (EBADF); 98 *retval = uap->j; 99 if (uap->i == uap->j) 100 return (0); 101 if ((unsigned)uap->j >= fdp->fd_nfiles) { 102 if (error = fdalloc(p, uap->j, &i)) 103 return (error); 104 if (uap->j != i) 105 panic("dup2: fdalloc"); 106 } else if (OFILE(fdp, uap->j)) { 107 if (OFILEFLAGS(fdp, uap->j) & UF_MAPPED) 108 (void) munmapfd(p, uap->j); 109 error = closef(OFILE(fdp, uap->j), p); 110 } 111 OFILE(fdp, uap->j) = fp; 112 OFILEFLAGS(fdp, uap->j) = OFILEFLAGS(fdp, uap->i) &~ UF_EXCLOSE; 113 fp->f_count++; 114 if (uap->j > fdp->fd_lastfile) 115 fdp->fd_lastfile = uap->j; 116 /* 117 * dup2() must succeed even though the close had an error. 118 */ 119 error = 0; /* XXX */ 120 return (error); 121 } 122 123 /* 124 * The file control system call. 125 */ 126 /* ARGSUSED */ 127 fcntl(p, uap, retval) 128 struct proc *p; 129 register struct args { 130 int fd; 131 int cmd; 132 int arg; 133 } *uap; 134 int *retval; 135 { 136 register struct filedesc *fdp = p->p_fd; 137 register struct file *fp; 138 register char *pop; 139 struct vnode *vp; 140 int i, error, flags = F_POSIX; 141 struct flock fl; 142 143 if ((unsigned)uap->fd >= fdp->fd_nfiles || 144 (fp = OFILE(fdp, uap->fd)) == NULL) 145 return (EBADF); 146 pop = &OFILEFLAGS(fdp, uap->fd); 147 switch(uap->cmd) { 148 case F_DUPFD: 149 if ((unsigned)uap->arg >= p->p_rlimit[RLIMIT_OFILE].rlim_cur) 150 return (EINVAL); 151 if (error = fdalloc(p, uap->arg, &i)) 152 return (error); 153 OFILE(fdp, i) = fp; 154 OFILEFLAGS(fdp, i) = *pop &~ UF_EXCLOSE; 155 fp->f_count++; 156 if (i > fdp->fd_lastfile) 157 fdp->fd_lastfile = i; 158 *retval = i; 159 return (0); 160 161 case F_GETFD: 162 *retval = *pop & 1; 163 return (0); 164 165 case F_SETFD: 166 *pop = (*pop &~ 1) | (uap->arg & 1); 167 return (0); 168 169 case F_GETFL: 170 *retval = OFLAGS(fp->f_flag); 171 return (0); 172 173 case F_SETFL: 174 fp->f_flag &= ~FCNTLFLAGS; 175 fp->f_flag |= FFLAGS(uap->arg) & FCNTLFLAGS; 176 if (error = fset(fp, FNDELAY, fp->f_flag & FNDELAY)) 177 return (error); 178 if (error = fset(fp, FASYNC, fp->f_flag & FASYNC)) 179 (void) fset(fp, FNDELAY, 0); 180 return (error); 181 182 case F_GETOWN: 183 return (fgetown(fp, retval)); 184 185 case F_SETOWN: 186 return (fsetown(fp, uap->arg)); 187 188 case F_SETLKW: 189 flags |= F_WAIT; 190 /* Fall into F_SETLK */ 191 192 case F_SETLK: 193 if (fp->f_type != DTYPE_VNODE) 194 return (EBADF); 195 vp = (struct vnode *)fp->f_data; 196 /* Copy in the lock structure */ 197 error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl)); 198 if (error) 199 return (error); 200 if (fl.l_whence == SEEK_CUR) 201 fl.l_start += fp->f_offset; 202 switch (fl.l_type) { 203 204 case F_RDLCK: 205 if ((fp->f_flag & FREAD) == 0) 206 return (EBADF); 207 return (VOP_ADVLOCK(vp, p, F_SETLK, &fl, flags)); 208 209 case F_WRLCK: 210 if ((fp->f_flag & FWRITE) == 0) 211 return (EBADF); 212 return (VOP_ADVLOCK(vp, p, F_SETLK, &fl, flags)); 213 214 case F_UNLCK: 215 return (VOP_ADVLOCK(vp, p, F_UNLCK, &fl, F_POSIX)); 216 217 default: 218 return (EINVAL); 219 } 220 221 case F_GETLK: 222 if (fp->f_type != DTYPE_VNODE) 223 return (EBADF); 224 vp = (struct vnode *)fp->f_data; 225 /* Copy in the lock structure */ 226 error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl)); 227 if (error) 228 return (error); 229 if (fl.l_whence == SEEK_CUR) 230 fl.l_start += fp->f_offset; 231 if (error = VOP_ADVLOCK(vp, p, F_GETLK, &fl, F_POSIX)) 232 return (error); 233 return (copyout((caddr_t)&fl, (caddr_t)uap->arg, sizeof (fl))); 234 235 default: 236 return (EINVAL); 237 } 238 /* NOTREACHED */ 239 } 240 241 fset(fp, bit, value) 242 struct file *fp; 243 int bit, value; 244 { 245 246 if (value) 247 fp->f_flag |= bit; 248 else 249 fp->f_flag &= ~bit; 250 return (fioctl(fp, (int)(bit == FNDELAY ? FIONBIO : FIOASYNC), 251 (caddr_t)&value)); 252 } 253 254 fgetown(fp, valuep) 255 struct file *fp; 256 int *valuep; 257 { 258 int error; 259 260 switch (fp->f_type) { 261 262 case DTYPE_SOCKET: 263 *valuep = ((struct socket *)fp->f_data)->so_pgid; 264 return (0); 265 266 default: 267 error = fioctl(fp, (int)TIOCGPGRP, (caddr_t)valuep); 268 *valuep = -*valuep; 269 return (error); 270 } 271 } 272 273 fsetown(fp, value) 274 struct file *fp; 275 int value; 276 { 277 278 if (fp->f_type == DTYPE_SOCKET) { 279 ((struct socket *)fp->f_data)->so_pgid = value; 280 return (0); 281 } 282 if (value > 0) { 283 struct proc *p = pfind(value); 284 if (p == 0) 285 return (ESRCH); 286 value = p->p_pgrp->pg_id; 287 } else 288 value = -value; 289 return (fioctl(fp, (int)TIOCSPGRP, (caddr_t)&value)); 290 } 291 292 fioctl(fp, cmd, value) 293 struct file *fp; 294 int cmd; 295 caddr_t value; 296 { 297 298 return ((*fp->f_ops->fo_ioctl)(fp, cmd, value)); 299 } 300 301 /* 302 * Close a file descriptor. 303 */ 304 /* ARGSUSED */ 305 close(p, uap, retval) 306 struct proc *p; 307 struct args { 308 int fd; 309 } *uap; 310 int *retval; 311 { 312 register struct filedesc *fdp = p->p_fd; 313 register struct file *fp; 314 register int fd = uap->fd; 315 register u_char *pf; 316 317 if ((unsigned)fd >= fdp->fd_nfiles || 318 (fp = OFILE(fdp, fd)) == NULL) 319 return (EBADF); 320 pf = (u_char *)&OFILEFLAGS(fdp, fd); 321 if (*pf & UF_MAPPED) 322 (void) munmapfd(p, fd); 323 OFILE(fdp, fd) = NULL; 324 while (fdp->fd_lastfile >= 0 && OFILE(fdp, fdp->fd_lastfile) == NULL) 325 fdp->fd_lastfile--; 326 if (fd < fdp->fd_freefile) 327 fdp->fd_freefile = fd; 328 *pf = 0; 329 return (closef(fp, p)); 330 } 331 332 /* 333 * Return status information about a file descriptor. 334 */ 335 /* ARGSUSED */ 336 fstat(p, uap, retval) 337 struct proc *p; 338 register struct args { 339 int fd; 340 struct stat *sb; 341 } *uap; 342 int *retval; 343 { 344 register struct filedesc *fdp = p->p_fd; 345 register struct file *fp; 346 struct stat ub; 347 int error; 348 349 if ((unsigned)uap->fd >= fdp->fd_nfiles || 350 (fp = OFILE(fdp, uap->fd)) == NULL) 351 return (EBADF); 352 switch (fp->f_type) { 353 354 case DTYPE_VNODE: 355 error = vn_stat((struct vnode *)fp->f_data, &ub); 356 break; 357 358 case DTYPE_SOCKET: 359 error = soo_stat((struct socket *)fp->f_data, &ub); 360 break; 361 362 default: 363 panic("fstat"); 364 /*NOTREACHED*/ 365 } 366 if (error == 0) 367 error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub)); 368 return (error); 369 } 370 371 /* 372 * Allocate a file descriptor for the process. 373 */ 374 int fdexpand; 375 376 fdalloc(p, want, result) 377 struct proc *p; 378 int want; 379 int *result; 380 { 381 register struct filedesc *fdp = p->p_fd; 382 register int i; 383 int lim, last, nfiles; 384 struct file **newofile; 385 char *newofileflags; 386 387 /* 388 * Search for a free descriptor starting at the higher 389 * of want or fd_freefile. If that fails, consider 390 * expanding the ofile array. 391 */ 392 lim = p->p_rlimit[RLIMIT_OFILE].rlim_cur; 393 for (;;) { 394 last = min(fdp->fd_nfiles, lim); 395 if ((i = want) < fdp->fd_freefile) 396 i = fdp->fd_freefile; 397 for (; i < last; i++) { 398 if (OFILE(fdp, i) == NULL) { 399 OFILEFLAGS(fdp, i) = 0; 400 if (i > fdp->fd_lastfile) 401 fdp->fd_lastfile = i; 402 if (fdp->fd_freefile <= want) 403 fdp->fd_freefile = i; 404 *result = i; 405 return (0); 406 } 407 } 408 409 /* 410 * No space in current array. Expand? 411 */ 412 if (fdp->fd_nfiles >= lim) 413 return (EMFILE); 414 nfiles = 2 * fdp->fd_nfiles; 415 MALLOC(newofile, struct file **, nfiles * OFILESIZE, 416 M_FILEDESC, M_WAITOK); 417 newofileflags = (char *) &newofile[nfiles]; 418 /* 419 * Copy the existing ofile and ofileflags arrays 420 * and zero the new portion of each array. 421 */ 422 bcopy(fdp->fd_ofiles, newofile, 423 (i = sizeof(struct file *) * fdp->fd_nfiles)); 424 bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i); 425 bcopy(fdp->fd_ofileflags, newofileflags, 426 (i = sizeof(char) * fdp->fd_nfiles)); 427 bzero(newofileflags + i, nfiles * sizeof(char) - i); 428 FREE(fdp->fd_ofiles, M_FILEDESC); 429 fdp->fd_ofiles = newofile; 430 fdp->fd_ofileflags = newofileflags; 431 fdp->fd_nfiles = nfiles; 432 fdexpand++; 433 } 434 } 435 436 /* 437 * Check to see whether n user file descriptors are available. 438 */ 439 fdavail(p, n) 440 struct proc *p; 441 register int n; 442 { 443 register struct filedesc *fdp = p->p_fd; 444 register int i; 445 446 if ((i = p->p_rlimit[RLIMIT_OFILE].rlim_cur - fdp->fd_nfiles) > 0 && 447 (n -= i) <= 0) 448 return (1); 449 for (i = fdp->fd_freefile; i < fdp->fd_nfiles; i++) 450 if (OFILE(fdp, i) == NULL && --n <= 0) 451 return (1); 452 for (i = 0; i < fdp->fd_freefile; i++) 453 if (OFILE(fdp, i) == NULL && --n <= 0) 454 return (1); 455 return (0); 456 } 457 458 struct file *lastf; 459 /* 460 * Create a new open file structure and allocate 461 * a file decriptor for the process that refers to it. 462 */ 463 falloc(p, resultfp, resultfd) 464 register struct proc *p; 465 struct file **resultfp; 466 int *resultfd; 467 { 468 register struct file *fp; 469 int error, i; 470 471 if (error = fdalloc(p, 0, &i)) 472 return (error); 473 if (lastf == 0) 474 lastf = file; 475 for (fp = lastf; fp < fileNFILE; fp++) 476 if (fp->f_count == 0) 477 goto slot; 478 for (fp = file; fp < lastf; fp++) 479 if (fp->f_count == 0) 480 goto slot; 481 tablefull("file"); 482 return (ENFILE); 483 slot: 484 OFILE(p->p_fd, i) = fp; 485 fp->f_count = 1; 486 fp->f_data = 0; 487 fp->f_offset = 0; 488 fp->f_cred = p->p_ucred; 489 crhold(fp->f_cred); 490 lastf = fp + 1; 491 if (resultfp) 492 *resultfp = fp; 493 if (resultfd) 494 *resultfd = i; 495 return (0); 496 } 497 498 /* 499 * Copy a filedesc structure. 500 */ 501 struct filedesc * 502 fdcopy(p) 503 struct proc *p; 504 { 505 register struct filedesc *fdp = p->p_fd; 506 register struct filedesc *newfdp; 507 register struct file *fp; 508 register int i; 509 510 MALLOC(newfdp, struct filedesc *, sizeof(*fdp), M_FILEDESC, M_WAITOK); 511 bcopy(fdp, newfdp, sizeof(*fdp)); 512 VREF(newfdp->fd_cdir); 513 if (newfdp->fd_rdir) 514 VREF(newfdp->fd_rdir); 515 newfdp->fd_refcnt = 1; 516 517 /* 518 * Compute the smallest multiple of NOEXTENT needed 519 * for the file descriptors currently in use, 520 * allowing the table to shrink. 521 */ 522 i = newfdp->fd_nfiles; 523 while (i > NOEXTENT * 2 && i >= (fdp->fd_lastfile + 1) / 2) 524 i /= 2; 525 newfdp->fd_nfiles = i; 526 MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE, 527 M_FILEDESC, M_WAITOK); 528 newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i]; 529 bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **)); 530 bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char)); 531 for (i = 0; i <= newfdp->fd_lastfile; i++) 532 if (fp = OFILE(newfdp, i)) 533 fp->f_count++; 534 return (newfdp); 535 } 536 537 /* 538 * Release a filedesc structure. 539 */ 540 fdfree(p) 541 struct proc *p; 542 { 543 register struct filedesc *fdp = p->p_fd; 544 struct file *fp; 545 register int i; 546 547 if (--fdp->fd_refcnt > 0) 548 return; 549 for (i = 0; i <= fdp->fd_lastfile; i++) 550 if (fp = OFILE(fdp, i)) 551 (void) closef(fp, p); 552 FREE(fdp->fd_ofiles, M_FILEDESC); 553 vrele(fdp->fd_cdir); 554 if (fdp->fd_rdir) 555 vrele(fdp->fd_rdir); 556 FREE(fdp, M_FILEDESC); 557 } 558 559 /* 560 * Internal form of close. 561 * Decrement reference count on file structure. 562 */ 563 closef(fp, p) 564 register struct file *fp; 565 struct proc *p; 566 { 567 struct vnode *vp; 568 struct flock lf; 569 int error; 570 571 if (fp == NULL) 572 return (0); 573 /* 574 * POSIX record locking dictates that any close releases ALL 575 * locks owned by this process. This is handled by setting 576 * a flag in the unlock to free ONLY locks obeying POSIX 577 * semantics, and not to free BSD-style file locks. 578 */ 579 if (fp->f_type == DTYPE_VNODE) { 580 lf.l_whence = SEEK_SET; 581 lf.l_start = 0; 582 lf.l_len = 0; 583 lf.l_type = F_UNLCK; 584 vp = (struct vnode *)fp->f_data; 585 (void) VOP_ADVLOCK(vp, p, F_UNLCK, &lf, F_POSIX); 586 } 587 if (--fp->f_count > 0) 588 return (0); 589 if (fp->f_count < 0) 590 panic("closef: count < 0"); 591 if (fp->f_type == DTYPE_VNODE) 592 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK); 593 error = (*fp->f_ops->fo_close)(fp); 594 crfree(fp->f_cred); 595 fp->f_count = 0; 596 return (error); 597 } 598 599 /* 600 * Apply an advisory lock on a file descriptor. 601 * 602 * Just attempt to get a record lock of the requested type on 603 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0). 604 */ 605 606 /* ARGSUSED */ 607 flock(p, uap, retval) 608 struct proc *p; 609 register struct args { 610 int fd; 611 int how; 612 } *uap; 613 int *retval; 614 { 615 register struct filedesc *fdp = p->p_fd; 616 register struct file *fp; 617 struct vnode *vp; 618 struct flock lf; 619 int error; 620 621 if ((unsigned)uap->fd >= fdp->fd_nfiles || 622 (fp = OFILE(fdp, uap->fd)) == NULL) 623 return (EBADF); 624 if (fp->f_type != DTYPE_VNODE) 625 return (EOPNOTSUPP); 626 vp = (struct vnode *)fp->f_data; 627 lf.l_whence = SEEK_SET; 628 lf.l_start = 0; 629 lf.l_len = 0; 630 if (uap->how & LOCK_UN) { 631 lf.l_type = F_UNLCK; 632 return (VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK)); 633 } 634 if (uap->how & LOCK_EX) 635 lf.l_type = F_WRLCK; 636 else if (uap->how & LOCK_SH) 637 lf.l_type = F_RDLCK; 638 else 639 return (EBADF); 640 if (uap->how & LOCK_NB) 641 return (VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK)); 642 return (VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK|F_WAIT)); 643 } 644 645 /* 646 * File Descriptor pseudo-device driver (/dev/fd/). 647 * 648 * Opening minor device N dup()s the file (if any) connected to file 649 * descriptor N belonging to the calling process. Note that this driver 650 * consists of only the ``open()'' routine, because all subsequent 651 * references to this file will be direct to the other driver. 652 */ 653 /* ARGSUSED */ 654 fdopen(dev, mode, type) 655 dev_t dev; 656 int mode, type; 657 { 658 659 /* 660 * XXX Kludge: set curproc->p_dupfd to contain the value of the 661 * the file descriptor being sought for duplication. The error 662 * return ensures that the vnode for this device will be released 663 * by vn_open. Open will detect this special error and take the 664 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN 665 * will simply report the error. 666 */ 667 curproc->p_dupfd = minor(dev); /* XXX */ 668 return (ENODEV); 669 } 670 671 /* 672 * Duplicate the specified descriptor to a free descriptor. 673 */ 674 dupfdopen(fdp, indx, dfd, mode) 675 register struct filedesc *fdp; 676 register int indx, dfd; 677 int mode; 678 { 679 register struct file *wfp; 680 struct file *fp; 681 682 /* 683 * If the to-be-dup'd fd number is greater than the allowed number 684 * of file descriptors, or the fd to be dup'd has already been 685 * closed, reject. Note, check for new == old is necessary as 686 * falloc could allocate an already closed to-be-dup'd descriptor 687 * as the new descriptor. 688 */ 689 fp = OFILE(fdp, indx); 690 if ((u_int)dfd >= fdp->fd_nfiles || (wfp = OFILE(fdp, dfd)) == NULL || 691 fp == wfp) 692 return (EBADF); 693 694 /* 695 * Check that the mode the file is being opened for is a subset 696 * of the mode of the existing descriptor. 697 */ 698 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) 699 return (EACCES); 700 OFILE(fdp, indx) = wfp; 701 OFILEFLAGS(fdp, indx) = OFILEFLAGS(fdp, dfd); 702 wfp->f_count++; 703 if (indx > fdp->fd_lastfile) 704 fdp->fd_lastfile = indx; 705 return (0); 706 } 707