root/maint/gnulib/tests/test-posix_spawn-inherit1.c

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

DEFINITIONS

This source file includes following definitions.
  1. parent_main
  2. child_main
  3. cleanup_then_die
  4. main

   1 /* Test of posix_spawn() function with an inherited file descriptor 1.
   2    Copyright (C) 2008-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 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>, 2020.  */
  18 
  19 /* Test whether passing a file descriptor open for writing, including the
  20    current file position, works.  */
  21 
  22 #include <config.h>
  23 
  24 #include <spawn.h>
  25 
  26 #include <errno.h>
  27 #include <signal.h>
  28 #include <stdio.h>
  29 #include <string.h>
  30 #include <unistd.h>
  31 #include <sys/types.h>
  32 #include <sys/wait.h>
  33 
  34 #define CHILD_PROGRAM_FILENAME "test-posix_spawn-inherit1"
  35 #define DATA_FILENAME "test-posix_spawn-inh1-data.tmp"
  36 
  37 static int
  38 parent_main (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  39 {
  40   FILE *fp;
  41   char *argv[3] = { CHILD_PROGRAM_FILENAME, "-child", NULL };
  42   int err;
  43   pid_t child;
  44   int status;
  45   int exitstatus;
  46 
  47   /* Create a data file with specific contents.  */
  48   fp = freopen (DATA_FILENAME, "wb", stdout);
  49   if (fp == NULL)
  50     {
  51       perror ("cannot create data file");
  52       return 1;
  53     }
  54   fwrite ("Halle X", 1, 7, fp);
  55   if (fflush (fp) || fseek (fp, 6, SEEK_SET))
  56     {
  57       perror ("cannot prepare data file");
  58       return 1;
  59     }
  60 
  61   /* Test whether the child writes to fd 1 at the current file position.  */
  62   if ((err = posix_spawn (&child, CHILD_PROGRAM_FILENAME, NULL, NULL, argv, environ)) != 0)
  63     {
  64       errno = err;
  65       perror ("subprocess failed");
  66       return 1;
  67     }
  68   status = 0;
  69   while (waitpid (child, &status, 0) != child)
  70     ;
  71   if (!WIFEXITED (status))
  72     {
  73       fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
  74       return 1;
  75     }
  76   exitstatus = WEXITSTATUS (status);
  77   if (exitstatus != 0)
  78     {
  79       fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
  80       return 1;
  81     }
  82 
  83   if (fclose (fp))
  84     {
  85       perror ("cannot close data file");
  86       return 1;
  87     }
  88 
  89   /* Check the contents of the data file.  */
  90   fp = fopen (DATA_FILENAME, "rb");
  91   if (fp == NULL)
  92     {
  93       perror ("cannot open data file");
  94       return 1;
  95     }
  96   char buf[1024];
  97   int nread = fread (buf, 1, sizeof (buf), fp);
  98   if (!(nread == 11 && memcmp (buf, "Halle Potta", 11) == 0))
  99     {
 100       fprintf (stderr, "data file wrong: has %d bytes, expected %d bytes\n", nread, 11);
 101       return 1;
 102     }
 103   if (fclose (fp))
 104     {
 105       perror ("cannot close data file");
 106       return 1;
 107     }
 108 
 109   /* Clean up data file.  */
 110   unlink (DATA_FILENAME);
 111 
 112   return 0;
 113 }
 114 
 115 static int
 116 child_main (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 117 {
 118   /* Write to STDOUT_FILENO.  */
 119   fwrite ("Potta", 1, 5, stdout);
 120   /* No 'fflush (stdout);' is needed.  It is implicit when the child process
 121      exits.  */
 122 
 123   return 0;
 124 }
 125 
 126 static void
 127 cleanup_then_die (int sig)
     /* [previous][next][first][last][top][bottom][index][help] */
 128 {
 129   /* Clean up data file.  */
 130   unlink (DATA_FILENAME);
 131 
 132   /* Re-raise the signal and die from it.  */
 133   signal (sig, SIG_DFL);
 134   raise (sig);
 135 }
 136 
 137 int
 138 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
 139 {
 140   int exitstatus;
 141 
 142   if (!(argc > 1 && strcmp (argv[1], "-child") == 0))
 143     {
 144       /* This is the parent process.  */
 145       signal (SIGINT, cleanup_then_die);
 146       signal (SIGTERM, cleanup_then_die);
 147       #ifdef SIGHUP
 148       signal (SIGHUP, cleanup_then_die);
 149       #endif
 150 
 151       exitstatus = parent_main ();
 152     }
 153   else
 154     {
 155       /* This is the child process.  */
 156       exitstatus = child_main ();
 157     }
 158   return exitstatus;
 159 }

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