Module: PTY

Defined in:
pty.c

Defined Under Namespace

Classes: ChildExited

Class Method Summary collapse

Class Method Details

.check(pid, raise = false) ⇒ Process::Status? .check(pid, true) ⇒ nil, raises PTY::ChildExited

Checks the status of the child process specified by pid. Returns nil if the process is still alive.

If the process is not alive, and raise was true, a PTY::ChildExited exception will be raised. Otherwise it will return a Process::Status instance.

pid

The process id of the process to check

raise

If true and the process identified by pid is no longer alive a PTY::ChildExited is raised.

Overloads:

  • .check(pid, raise = false) ⇒ Process::Status?

    Returns:

    • (Process::Status, nil)
  • .check(pid, true) ⇒ nil, raises PTY::ChildExited

    Returns:



730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
# File 'pty.c', line 730

static VALUE
pty_check(int argc, VALUE *argv, VALUE self)
{
    VALUE pid, exc;
    rb_pid_t cpid;
    int status;
    const int flag =
#ifdef WNOHANG
        WNOHANG|
#endif
#ifdef WUNTRACED
        WUNTRACED|
#endif
        0;

    rb_scan_args(argc, argv, "11", &pid, &exc);
    cpid = rb_waitpid(NUM2PIDT(pid), &status, flag);
    if (cpid == -1 || cpid == 0) return Qnil;

    if (!RTEST(exc)) return rb_last_status_get();
    raise_from_check(cpid, status);

    UNREACHABLE_RETURN(Qnil);
}

.openArray .open {|(master_io, slave_file)| ... } ⇒ Object

Allocates a pty (pseudo-terminal).

In the block form, yields an array of two elements (master_io, slave_file) and the value of the block is returned from open.

The IO and File are both closed after the block completes if they haven’t been already closed.

PTY.open {|master, slave|
  p master      #=> #<IO:masterpty:/dev/pts/1>
  p slave      #=> #<File:/dev/pts/1>
  p slave.path #=> "/dev/pts/1"
}

In the non-block form, returns a two element array, [master_io, slave_file].

master, slave = PTY.open
# do something with master for IO, or the slave file

The arguments in both forms are:

master_io

the master of the pty, as an IO.

slave_file

the slave of the pty, as a File. The path to the

terminal device is available via slave_file.path

IO#raw! is usable to disable newline conversions:

require 'io/console'
PTY.open {|m, s|
  s.raw!
  # ...
}

Overloads:

  • .openArray

    Returns:

    • (Array)
  • .open {|(master_io, slave_file)| ... } ⇒ Object

    Yields:

    • ((master_io, slave_file))


561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'pty.c', line 561

static VALUE
pty_open(VALUE klass)
{
    int master_fd, slave_fd;
    char slavename[DEVICELEN];

    getDevice(&master_fd, &slave_fd, slavename, 1);

    VALUE master_path = rb_obj_freeze(rb_sprintf("masterpty:%s", slavename));
    VALUE master_io = rb_io_open_descriptor(rb_cIO, master_fd, FMODE_READWRITE | FMODE_SYNC | FMODE_DUPLEX, master_path, RUBY_IO_TIMEOUT_DEFAULT, NULL);

    VALUE slave_path = rb_obj_freeze(rb_str_new_cstr(slavename));
    VALUE slave_file = rb_io_open_descriptor(rb_cFile, slave_fd, FMODE_READWRITE | FMODE_SYNC | FMODE_DUPLEX | FMODE_TTY, slave_path, RUBY_IO_TIMEOUT_DEFAULT, NULL);

    VALUE assoc = rb_assoc_new(master_io, slave_file);

    if (rb_block_given_p()) {
        return rb_ensure(rb_yield, assoc, pty_close_pty, assoc);
    }

    return assoc;
}

.spawn([env,]) {|r, w, pid| ... } ⇒ Object .spawn([env,]) ⇒ Array .spawn([env,], arguments, ...) {|r, w, pid| ... } ⇒ Object .spawn([env,], arguments, ...) ⇒ Array

Spawns the specified command on a newly allocated pty. You can also use the alias ::getpty.

The command’s controlling tty is set to the slave device of the pty and its standard input/output/error is redirected to the slave device.

env is an optional hash that provides additional environment variables to the spawned pty.

# sets FOO to "bar"
PTY.spawn({"FOO"=>"bar"}, "printenv", "FOO") do |r, w, pid|
  p r.read #=> "bar\r\n"
ensure
  r.close; w.close; Process.wait(pid)
end
# unsets FOO
PTY.spawn({"FOO"=>nil}, "printenv", "FOO") do |r, w, pid|
  p r.read #=> ""
ensure
  r.close; w.close; Process.wait(pid)
end

command and command_line are the full commands to run, given a String. Any additional arguments will be passed to the command.

Return values

In the non-block form this returns an array of size three, [r, w, pid].

In the block form these same values will be yielded to the block:

r

A readable IO that contains the command’s standard output and standard error

w

A writable IO that is the command’s standard input

pid

The process identifier for the command.

Clean up

This method does not clean up like closing IOs or waiting for child process, except that the process is detached in the block form to prevent it from becoming a zombie (see Process.detach). Any other cleanup is the responsibility of the caller. If waiting for pid, be sure to close both r and w before doing so; doing it in the reverse order may cause deadlock on some OSes.

Overloads:

  • .spawn([env,]) {|r, w, pid| ... } ⇒ Object

    Yields:

    • (r, w, pid)
  • .spawn([env,]) ⇒ Array

    Returns:

    • (Array)
  • .spawn([env,], arguments, ...) {|r, w, pid| ... } ⇒ Object

    Yields:

    • (r, w, pid)
  • .spawn([env,], arguments, ...) ⇒ Array

    Returns:

    • (Array)


649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
# File 'pty.c', line 649

static VALUE
pty_getpty(int argc, VALUE *argv, VALUE self)
{
    VALUE res;
    struct pty_info info;
    char SlaveName[DEVICELEN];

    establishShell(argc, argv, &info, SlaveName);

    VALUE pty_path = rb_obj_freeze(rb_str_new_cstr(SlaveName));
    VALUE rport = rb_io_open_descriptor(
        rb_cFile, info.fd, FMODE_READABLE, pty_path, RUBY_IO_TIMEOUT_DEFAULT, NULL
    );

    int wpty_fd = rb_cloexec_dup(info.fd);
    if (wpty_fd == -1) {
        rb_sys_fail("dup()");
    }
    VALUE wport = rb_io_open_descriptor(
        rb_cFile, wpty_fd, FMODE_WRITABLE | FMODE_TRUNC | FMODE_CREATE | FMODE_SYNC,
        pty_path, RUBY_IO_TIMEOUT_DEFAULT, NULL
    );

    res = rb_ary_new2(3);
    rb_ary_store(res, 0, rport);
    rb_ary_store(res, 1, wport);
    rb_ary_store(res,2,PIDT2NUM(info.child_pid));

    if (rb_block_given_p()) {
        rb_ensure(rb_yield, res, pty_detach_process, (VALUE)&info);
        return Qnil;
    }
    return res;
}