(root)/
gettext-0.22.4/
gettext-tools/
gnulib-tests/
test-posix_spawn-dup2-stdin.c
       1  /* Test of posix_spawn() function: writing to a subprocess.
       2     Copyright (C) 2008-2023 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 of the License, or
       7     (at your option) 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  /* Written by Bruno Haible <bruno@clisp.org>, 2008.  */
      18  
      19  #include <config.h>
      20  
      21  #include <spawn.h>
      22  
      23  #include <errno.h>
      24  #include <fcntl.h>
      25  #include <signal.h>
      26  #include <stdio.h>
      27  #include <stdlib.h>
      28  #include <string.h>
      29  #include <unistd.h>
      30  #include <sys/types.h>
      31  #include <sys/wait.h>
      32  
      33  #define CHILD_PROGRAM_FILENAME "test-posix_spawn-dup2-stdin.sh"
      34  
      35  static int
      36  fd_safer (int fd)
      37  {
      38    if (0 <= fd && fd <= 2)
      39      {
      40        int f = fd_safer (dup (fd));
      41        int e = errno;
      42        close (fd);
      43        errno = e;
      44        fd = f;
      45      }
      46  
      47    return fd;
      48  }
      49  
      50  int
      51  main ()
      52  {
      53    char *argv[3] = { (char *) BOURNE_SHELL, (char *) CHILD_PROGRAM_FILENAME, NULL };
      54    int ofd[2];
      55    sigset_t blocked_signals;
      56    sigset_t fatal_signal_set;
      57    posix_spawn_file_actions_t actions;
      58    bool actions_allocated;
      59    posix_spawnattr_t attrs;
      60    bool attrs_allocated;
      61    int err;
      62    pid_t child;
      63    int fd;
      64    FILE *fp;
      65    int written;
      66    int status;
      67    int exitstatus;
      68  
      69    if (pipe (ofd) < 0 || (ofd[1] = fd_safer (ofd[1])) < 0)
      70      {
      71        perror ("cannot create pipe");
      72        exit (1);
      73      }
      74    sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
      75    sigemptyset (&fatal_signal_set);
      76    sigaddset (&fatal_signal_set, SIGINT);
      77    sigaddset (&fatal_signal_set, SIGTERM);
      78    #ifdef SIGHUP
      79    sigaddset (&fatal_signal_set, SIGHUP);
      80    #endif
      81    #ifdef SIGPIPE
      82    sigaddset (&fatal_signal_set, SIGPIPE);
      83    #endif
      84    sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
      85    actions_allocated = false;
      86    attrs_allocated = false;
      87    if ((err = posix_spawn_file_actions_init (&actions)) != 0
      88        || (actions_allocated = true,
      89            (err = posix_spawn_file_actions_adddup2 (&actions, ofd[0], STDIN_FILENO)) != 0
      90            || (err = posix_spawn_file_actions_addclose (&actions, ofd[0])) != 0
      91            || (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 0
      92            || (err = posix_spawnattr_init (&attrs)) != 0
      93            || (attrs_allocated = true,
      94                #if defined _WIN32 && !defined __CYGWIN__
      95                0
      96                #else
      97                (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
      98                || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0
      99                #endif
     100               )
     101            || (err = posix_spawnp (&child, BOURNE_SHELL, &actions, &attrs, argv, environ)) != 0))
     102      {
     103        if (actions_allocated)
     104          posix_spawn_file_actions_destroy (&actions);
     105        if (attrs_allocated)
     106          posix_spawnattr_destroy (&attrs);
     107        sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
     108        errno = err;
     109        perror ("subprocess failed");
     110        exit (1);
     111      }
     112    posix_spawn_file_actions_destroy (&actions);
     113    posix_spawnattr_destroy (&attrs);
     114    sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
     115    close (ofd[0]);
     116    fd = ofd[1];
     117    fp = fdopen (fd, "w");
     118    if (fp == NULL)
     119      {
     120        fprintf (stderr, "fdopen() failed\n");
     121        exit (1);
     122      }
     123    written = fwrite ("Halle Potta\n", 1, 12, fp);
     124    if (written < 12)
     125      {
     126        fprintf (stderr, "could not write input\n");
     127        exit (1);
     128      }
     129    fclose (fp);
     130    status = 0;
     131    while (waitpid (child, &status, 0) != child)
     132      ;
     133    if (!WIFEXITED (status))
     134      {
     135        fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
     136        exit (1);
     137      }
     138    exitstatus = WEXITSTATUS (status);
     139    if (exitstatus != 0)
     140      {
     141        fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
     142        exit (1);
     143      }
     144    return 0;
     145  }