pipe + fork + exec 書いてみた

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

void
strwrite(
    int const fd,
    char const* const str)
{
  if(write(fd, str, strlen(str)) < 0) {
    perror("write");
    exit(1);
  }
}

pid_t
process_open(
    char const* const path,
    int const stdin_fd,
    int const stdout_fd,
    int const*   close_on_child_fds,
    size_t const close_on_child_fds_length,
    int const*   close_on_parent_fds,
    size_t const close_on_parent_fds_length)
{
  pid_t const child_pid = fork();

  switch(child_pid) {
    case -1: // error
      perror("fork");
      exit(1);
    case 0: // child
      for(size_t i = 0; i < close_on_child_fds_length; ++i) { close(close_on_child_fds[i]); }
      dup2(stdin_fd,  STDIN_FILENO ); close(stdin_fd);
      dup2(stdout_fd, STDOUT_FILENO); close(stdout_fd);

      execl(path, path, NULL);
      perror(path);
      exit(1);
    default: // parent
      for(size_t i = 0; i < close_on_parent_fds_length; ++i) { close(close_on_parent_fds[i]); }
  }

  return child_pid;
}


int main()
{
  int write_pipefd[2];
  int read_pipefd[2];
  if (pipe(write_pipefd) < 0 || pipe(read_pipefd)) {
    perror("pipe");
    exit(1);
  }

  process_open("/usr/local/bin/lua",
      write_pipefd[0], read_pipefd[1],
      (const int[]){write_pipefd[1], read_pipefd[0]}, 2,
      (const int[]){write_pipefd[0], read_pipefd[1]}, 2);

  strwrite(write_pipefd[1], "print(_VERSION)");
  close(write_pipefd[1]);

  char buf[256] = {0};
  if (read(read_pipefd[0], buf, sizeof(buf)) < 0) {
    perror("read");
    exit(1);
  }

  printf("got [%s]\n", buf);
}