You're probably looking for "select"

Sid Shanker @RC 06/21/2018

The problem:

write_to_bash(...)

data = read_from_bash(...)

 

write_data_to_socket(data)

new_command = read_data_from_socket()
 

 

 

This is fine..right?

The kicker: "cd"

Idea 1: Write "echo 1" after every command

write_to_bash(...);

write_to_bash("echo 1");

data = read_from_bash(...);

// Ignore the "1" in the output

 

write_data_to_socket(data);

new_command = read_data_from_socket();
 

It breaks error codes!

$ garbage_command_that_dont_exist

$ echo 1

$ echo $? # == 0, this is WRONG

Making another process?

Fork into two processes, one that reads from bash only, one that reads from the socket only.

Enter: select

  • Remember, in Unix, everything is a file, including the network socket, and the handle to the "bash process"
  • select allows you to handle i/o on several file descriptors elegantly

socket_fd, bash_pipe_fd;

 

select([socket_fd, bash_pipe_fd])

 

if (IS_READY(socket_fd)) {
  handle_input_on_socket();
} else if (IS_READY(bash_pipe_fd)){

  handle_input_on_bash_pipe();
}

Thanks

  • Ben for the suggestion to use "select"
  • Andy and David for general wisdom
Made with Slides.com