nix icon indicating copy to clipboard operation
nix copied to clipboard

Expose the RawFd wrapped by struct PollFd

Open mlang opened this issue 5 years ago • 3 comments

mlang avatar Oct 30 '19 10:10 mlang

Could you please tell me a little bit more about what you're trying to accomplish?

asomers avatar Oct 30 '19 17:10 asomers

I am trying to figure out which fd produced an event. Maybe I am overlooking something blatantly obvious, but how would you know which descriptor had some activity?

  let mut fds = [PollFd::new(pty.master, PollFlags::POLLIN),
                 PollFd::new(STDIN_FILENO, PollFlags::POLLIN)];

'polling: loop {
    if poll(&mut fds, 1000)? > 0 {
      for fd in fds.iter() {
        if let Some(flags) = fd.revents() {
          if flags.contains(PollFlags::POLLIN) {
            let other = if fd.raw_fd() == pty.master {
              STDOUT_FILENO
            } else {
              pty.master
            };
            let mut buf = MaybeUninit::<[u8; 512]>::uninit();
            if let Ok(size) = read(fd.raw_fd(), unsafe { &mut *buf.as_mut_ptr() }) {
              let slice = &unsafe { buf.assume_init() }[0..size];
              write(other, slice)?;
            }
          } else if flags.contains(PollFlags::POLLHUP) {
            break 'polling
          }
        }
      }
    }
  }

-- CYa, ⡍⠁⠗⠊⠕

mlang avatar Nov 01 '19 19:11 mlang

PollFd implements AsRawFd so you can access the file descriptor by calling fd.as_raw_fd().

zopsicle avatar May 01 '22 17:05 zopsicle

Thanks for your contributing!

That issue has already been fixed, so I am gonna close this PR, I am sorry about this.

SteveLauC avatar Dec 11 '23 02:12 SteveLauC