Search code examples
cgccpipeposixcodelite

posix_spawn and pipes (stdin, stdout, stderr), inconsistent error


Reading man pages and several stackoverflow/stackexchange pages, I am working on posix_spawn and pipes. However, I ran into a problem. Following code, three pipes, (stdin, stdout, stderr) look work fine. Problem is that rv = write(stdin_pipe[PIPE_TO]... returns an error- "No such file or directory". Weird thing is this code works fine (no error) if run in codelite (an ide). I am not sure whether this code is good or not. Could it result in memory leak or some disaster.

I appreciate any suggestion. My intention for this code is that throw some string to grep 1, then capture stdout.

#include <spawn.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>

#define PIPE_TO 1
#define PIPE_FROM 0

int main(int argc, char** argv, char** envv){
  int   exit_code;
  int   stdout_pipe[2], stderr_pipe[2], stdin_pipe[2];
  posix_spawn_file_actions_t    *action, act;
  char  *args[4];
  char  *argsmem[] = {"sh", "-c"};
  int   rv;
  char  inbuf[] = "1\na\n3\nb\n5\n6\n7\n8\n9\n10\n11\n12\n\0";
  char  cmd[] = "grep 1";
  action = &act;

  if(pipe(stdout_pipe) || pipe(stderr_pipe) || pipe(stdin_pipe))
      printf("pipe returned an error.\n");

  posix_spawn_file_actions_init(action);
  posix_spawn_file_actions_addclose(action, stdout_pipe[PIPE_FROM]);
  posix_spawn_file_actions_addclose(action, stderr_pipe[PIPE_FROM]);
  posix_spawn_file_actions_addclose(action, stdin_pipe[PIPE_TO]);

  posix_spawn_file_actions_adddup2(action, stdout_pipe[PIPE_TO], STDOUT_FILENO);
  posix_spawn_file_actions_adddup2(action, stderr_pipe[PIPE_TO], STDERR_FILENO);
  posix_spawn_file_actions_adddup2(action, stdin_pipe[PIPE_FROM], STDIN_FILENO);

  posix_spawn_file_actions_addclose(action, stdout_pipe[PIPE_TO]);
  posix_spawn_file_actions_addclose(action, stderr_pipe[PIPE_TO]);
  posix_spawn_file_actions_addclose(action, stdin_pipe[PIPE_FROM]);

  args[0] = argsmem[0];
  args[1] = argsmem[1];
  args[2] = cmd;
  args[3] = NULL;

  pid_t   pid;

  if(posix_spawnp(&pid, args[0], action, NULL, args, NULL))
      printf("posix_spawnp failed with error: %s\n", strerror(errno));

  rv = write(stdin_pipe[PIPE_TO], inbuf, strlen(inbuf));
  printf("rv = %d, errno = %d %s\n", rv, errno, strerror(errno));
  close(stdin_pipe[PIPE_TO]);

  close(stdout_pipe[PIPE_TO]);
  close(stderr_pipe[PIPE_TO]);
  close(stdin_pipe[PIPE_FROM]);

  char  buffer[1001];
  struct pollfd plist[2];
  int   rval, bytes_read;
  int   i;

  plist[0].fd = stdout_pipe[PIPE_FROM];
  plist[0].events = POLLIN;
  plist[1].fd = stderr_pipe[PIPE_FROM];
  plist[1].events = POLLIN;

  while(rval = poll(plist, 2, -1)){
    if(plist[0].revents & POLLIN){
      i = 0;
      while(bytes_read = read(stdout_pipe[PIPE_FROM], buffer, 1000)){
        i += bytes_read;
        buffer[bytes_read] = '\0';
        printf("%s", buffer);
        }
      printf("\nread %d bytes from stdout.\n", i);
      }
    else if(plist[1].revents & POLLIN){
      i = 0;
      while(bytes_read = read(stderr_pipe[PIPE_FROM], buffer, 1000)){
        i += bytes_read;
        buffer[bytes_read] = '\0';
        printf("%s", buffer);
        }
      printf("\nread %d bytes from stderr.\n", i);
      }
    else break;
    }

  waitpid(pid, &exit_code, 0);
  printf("exit code: %d\n", exit_code);

  posix_spawn_file_actions_destroy(action);
  }

My system is Linux, Debian amd64 testing. Fiddling with inbuf and cmd, it looks like both stdout and stderr are good.

I have tried several setups (all debian amd64 testing), without solution. I do not want to expect any future disaster.


Solution

  •   rv = write(stdin_pipe[PIPE_TO], inbuf, strlen(inbuf));
      printf("rv = %d, errno = %d %s\n", rv, errno, strerror(errno));
    

    is wrong. Testing errno only makes sense if the system call fails, that is rv == -1. Notice also that a successful system call does not reset errno to 0, so ENOENT ("No such file or directory") is a leftover from some previous error (BTW, write cannot possibly return ENOENT).

    In this case, the culprit is posix_spawnp. It didn't fail, of course, but it had some failing calls while looking for sh in various directories. I am not familiar with codelite, but it is quite possible that it's PATH is different, and sh is found in the very first directory.