3490 |
3490 |
return finish_writeconv(p->fptr, p->noalloc);
|
3491 |
3491 |
}
|
3492 |
3492 |
|
|
3493 |
static VALUE
|
|
3494 |
nogvl_close(void *ptr)
|
|
3495 |
{
|
|
3496 |
int *fd = ptr;
|
|
3497 |
|
|
3498 |
return (VALUE)close(*fd);
|
|
3499 |
}
|
|
3500 |
|
|
3501 |
static int
|
|
3502 |
maygvl_close(int fd, int keepgvl)
|
|
3503 |
{
|
|
3504 |
if (keepgvl)
|
|
3505 |
return close(fd);
|
|
3506 |
|
|
3507 |
/*
|
|
3508 |
* close() may block for certain file types (NFS, SO_LINGER sockets,
|
|
3509 |
* inotify), so let other threads run.
|
|
3510 |
*/
|
|
3511 |
return (int)rb_thread_blocking_region(nogvl_close, &fd, RUBY_UBF_IO, 0);
|
|
3512 |
}
|
|
3513 |
|
|
3514 |
static VALUE
|
|
3515 |
nogvl_fclose(void *ptr)
|
|
3516 |
{
|
|
3517 |
FILE *file = ptr;
|
|
3518 |
|
|
3519 |
return (VALUE)fclose(file);
|
|
3520 |
}
|
|
3521 |
|
|
3522 |
static int
|
|
3523 |
maygvl_fclose(FILE *file, int keepgvl)
|
|
3524 |
{
|
|
3525 |
if (keepgvl)
|
|
3526 |
return fclose(file);
|
|
3527 |
|
|
3528 |
return (int)rb_thread_blocking_region(nogvl_fclose, file, RUBY_UBF_IO, 0);
|
|
3529 |
}
|
|
3530 |
|
|
3531 |
|
3493 |
3532 |
static void
|
3494 |
3533 |
fptr_finalize(rb_io_t *fptr, int noraise)
|
3495 |
3534 |
{
|
3496 |
3535 |
VALUE err = Qnil;
|
|
3536 |
int fd = fptr->fd;
|
|
3537 |
FILE *stdio_file = fptr->stdio_file;
|
|
3538 |
|
3497 |
3539 |
if (fptr->writeconv) {
|
3498 |
3540 |
if (fptr->write_lock && !noraise) {
|
3499 |
3541 |
struct finish_writeconv_arg arg;
|
... | ... | |
3515 |
3557 |
err = INT2NUM(errno);
|
3516 |
3558 |
}
|
3517 |
3559 |
}
|
3518 |
|
if (IS_PREP_STDIO(fptr) || fptr->fd <= 2) {
|
3519 |
|
goto skip_fd_close;
|
|
3560 |
fptr->fd = -1;
|
|
3561 |
fptr->stdio_file = 0;
|
|
3562 |
if (!noraise)
|
|
3563 |
rb_thread_fd_close(fd);
|
|
3564 |
if (IS_PREP_STDIO(fptr) || fd <= 2) {
|
|
3565 |
/* need to keep FILE objects of stdin, stdout and stderr */
|
3520 |
3566 |
}
|
3521 |
|
if (fptr->stdio_file) {
|
3522 |
|
/* fptr->stdio_file is deallocated anyway
|
|
3567 |
else if (stdio_file) {
|
|
3568 |
/* stdio_file is deallocated anyway
|
3523 |
3569 |
* even if fclose failed. */
|
3524 |
|
if (fclose(fptr->stdio_file) < 0 && NIL_P(err))
|
|
3570 |
if ((maygvl_fclose(stdio_file, noraise) < 0) && NIL_P(err))
|
3525 |
3571 |
err = noraise ? Qtrue : INT2NUM(errno);
|
3526 |
3572 |
}
|
3527 |
|
else if (0 <= fptr->fd) {
|
|
3573 |
else if (0 <= fd) {
|
3528 |
3574 |
/* fptr->fd may be closed even if close fails.
|
3529 |
3575 |
* POSIX doesn't specify it.
|
3530 |
3576 |
* We assumes it is closed. */
|
3531 |
|
if (close(fptr->fd) < 0 && NIL_P(err))
|
|
3577 |
if ((maygvl_close(fd, noraise) < 0) && NIL_P(err))
|
3532 |
3578 |
err = noraise ? Qtrue : INT2NUM(errno);
|
3533 |
3579 |
}
|
3534 |
|
skip_fd_close:
|
3535 |
|
fptr->fd = -1;
|
3536 |
|
fptr->stdio_file = 0;
|
3537 |
3580 |
fptr->mode &= ~(FMODE_READABLE|FMODE_WRITABLE);
|
3538 |
3581 |
|
3539 |
3582 |
if (!NIL_P(err) && !noraise) {
|
... | ... | |
3629 |
3672 |
rb_io_close(VALUE io)
|
3630 |
3673 |
{
|
3631 |
3674 |
rb_io_t *fptr;
|
3632 |
|
int fd;
|
3633 |
3675 |
VALUE write_io;
|
3634 |
3676 |
rb_io_t *write_fptr;
|
3635 |
3677 |
|
... | ... | |
3645 |
3687 |
if (!fptr) return Qnil;
|
3646 |
3688 |
if (fptr->fd < 0) return Qnil;
|
3647 |
3689 |
|
3648 |
|
fd = fptr->fd;
|
3649 |
3690 |
rb_io_fptr_cleanup(fptr, FALSE);
|
3650 |
|
rb_thread_fd_close(fd);
|
3651 |
3691 |
|
3652 |
3692 |
if (fptr->pid) {
|
3653 |
3693 |
rb_syswait(fptr->pid);
|
... | ... | |
5785 |
5825 |
rb_sys_fail_path(orig->pathv);
|
5786 |
5826 |
}
|
5787 |
5827 |
else {
|
5788 |
|
fclose(fptr->stdio_file);
|
|
5828 |
maygvl_fclose(fptr->stdio_file, 0);
|
5789 |
5829 |
fptr->stdio_file = 0;
|
5790 |
5830 |
fptr->fd = -1;
|
5791 |
5831 |
if (dup2(fd2, fd) < 0)
|