root/maint/gnulib/tests/test-spawn-pipe-main.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. test_pipe
  2. main

   1 /* Test of create_pipe_bidi/wait_subprocess.
   2    Copyright (C) 2009-2021 Free Software Foundation, Inc.
   3 
   4    This program is free software; you can redistribute it and/or modify
   5    it under the terms of the GNU General Public License as published by
   6    the Free Software Foundation; either version 3, or (at your option)
   7    any later version.
   8 
   9    This program is distributed in the hope that it will be useful,
  10    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12    GNU General Public License for more details.
  13 
  14    You should have received a copy of the GNU General Public License
  15    along with this program; if not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 #include <config.h>
  18 
  19 #include "spawn-pipe.h"
  20 #include "wait-process.h"
  21 
  22 #include <stdbool.h>
  23 #include <stdio.h>
  24 #include <stdlib.h>
  25 #include <string.h>
  26 #include <unistd.h>
  27 
  28 /* Depending on arguments, this test intentionally closes stderr or
  29    starts life with stderr closed.  So, we arrange to have fd 10
  30    (outside the range of interesting fd's during the test) set up to
  31    duplicate the original stderr.  */
  32 
  33 #define BACKUP_STDERR_FILENO 10
  34 #define ASSERT_STREAM myerr
  35 #include "macros.h"
  36 
  37 static FILE *myerr;
  38 
  39 /* Create a bi-directional pipe to a test child, and validate that the
  40    child program returns the expected output.
  41    PROG is the program to run in the child process.
  42    STDERR_CLOSED is true if we have already closed fd 2.  */
  43 static void
  44 test_pipe (const char *prog, bool stderr_closed)
     /* [previous][next][first][last][top][bottom][index][help] */
  45 {
  46   int fd[2];
  47   const char *argv[3];
  48   pid_t pid;
  49   char buffer[2] = { 'a', 't' };
  50 
  51   /* Set up child.  */
  52   argv[0] = prog;
  53   argv[1] = (stderr_closed ? "1" : "0");
  54   argv[2] = NULL;
  55   pid = create_pipe_bidi (prog, prog, argv, NULL, false, true, true, fd);
  56   ASSERT (0 <= pid);
  57   ASSERT (STDERR_FILENO < fd[0]);
  58   ASSERT (STDERR_FILENO < fd[1]);
  59 
  60   /* Push child's input.  */
  61   ASSERT (write (fd[1], buffer, 1) == 1);
  62   ASSERT (close (fd[1]) == 0);
  63 
  64   /* Get child's output.  */
  65   ASSERT (read (fd[0], buffer, 2) == 1);
  66 
  67   /* Wait for child.  */
  68   ASSERT (wait_subprocess (pid, prog, true, false, true, true, NULL) == 0);
  69   ASSERT (close (fd[0]) == 0);
  70 
  71   /* Check the result.  */
  72   ASSERT (buffer[0] == 'b');
  73   ASSERT (buffer[1] == 't');
  74 }
  75 
  76 int
  77 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  78 {
  79   int test;
  80   int fd;
  81 
  82   if (argc != 3)
  83     {
  84       fprintf (stderr, "%s: need 2 arguments\n", argv[0]);
  85       return 2;
  86     }
  87   /* We might close fd 2 later, so save it in fd 10.  */
  88   if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
  89       || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
  90     return 2;
  91 
  92   /* Selectively close various standard fds, to verify the child process is
  93      not impacted by this.  */
  94   test = atoi (argv[2]);
  95   switch (test)
  96     {
  97     case 0:
  98       break;
  99     case 1:
 100       close (0);
 101       break;
 102     case 2:
 103       close (1);
 104       break;
 105     case 3:
 106       close (0);
 107       close (1);
 108       break;
 109     case 4:
 110       close (2);
 111       break;
 112     case 5:
 113       close (0);
 114       close (2);
 115       break;
 116     case 6:
 117       close (1);
 118       close (2);
 119       break;
 120     case 7:
 121       close (0);
 122       close (1);
 123       close (2);
 124       break;
 125     default:
 126       ASSERT (false);
 127     }
 128 
 129   /* Plug any file descriptor leaks inherited from outside world before
 130      starting, so that child has a clean slate (at least for the fds that we
 131      might be manipulating).  */
 132   for (fd = 3; fd < 7; fd++)
 133     close (fd);
 134 
 135   test_pipe (argv[1], test >= 4);
 136 
 137   return 0;
 138 }

/* [previous][next][first][last][top][bottom][index][help] */