Bump to m4 1.4.19
[platform/upstream/m4.git] / tests / test-posix_spawn-inherit0.c
1 /* Test of posix_spawn() function with an inherited file descriptor 0.
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 reading, 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-inherit0"
35 #define DATA_FILENAME "test-posix_spawn-inh0-data.tmp"
36
37 static int
38 parent_main (void)
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 = fopen (DATA_FILENAME, "wb");
49   if (fp == NULL)
50     {
51       perror ("cannot create data file");
52       return 1;
53     }
54   fwrite ("Halle Potta", 1, 11, fp);
55   if (fflush (fp) || fclose (fp))
56     {
57       perror ("cannot prepare data file");
58       return 1;
59     }
60
61   /* Open the data file for reading.  */
62   fp = freopen (DATA_FILENAME, "rb", stdin);
63   if (fp == NULL)
64     {
65       perror ("cannot open data file");
66       return 1;
67     }
68   if (fflush (fp) || fseek (fp, 6, SEEK_SET)) /* needs gnulib module 'fflush' */
69     {
70       perror ("cannot seek in data file");
71       return 1;
72     }
73
74   /* Test whether the child reads from fd 0 at the current file position.  */
75   if ((err = posix_spawn (&child, CHILD_PROGRAM_FILENAME, NULL, NULL, argv, environ)) != 0)
76     {
77       errno = err;
78       perror ("subprocess failed");
79       return 1;
80     }
81   status = 0;
82   while (waitpid (child, &status, 0) != child)
83     ;
84   if (!WIFEXITED (status))
85     {
86       fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
87       return 1;
88     }
89   exitstatus = WEXITSTATUS (status);
90   if (exitstatus != 0)
91     {
92       fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
93       return 1;
94     }
95
96   if (fclose (fp))
97     {
98       perror ("cannot close data file");
99       return 1;
100     }
101
102   /* Clean up data file.  */
103   unlink (DATA_FILENAME);
104
105   return 0;
106 }
107
108 static int
109 child_main (void)
110 {
111   /* Read from STDIN_FILENO.  */
112   char buf[1024];
113   int nread = fread (buf, 1, sizeof (buf), stdin);
114   if (!(nread == 5 && memcmp (buf, "Potta", 5) == 0))
115     {
116       fprintf (stderr, "child: read %d bytes, expected %d bytes\n", nread, 5);
117       return 1;
118     }
119
120   return 0;
121 }
122
123 static void
124 cleanup_then_die (int sig)
125 {
126   /* Clean up data file.  */
127   unlink (DATA_FILENAME);
128
129   /* Re-raise the signal and die from it.  */
130   signal (sig, SIG_DFL);
131   raise (sig);
132 }
133
134 int
135 main (int argc, char *argv[])
136 {
137   int exitstatus;
138
139   if (!(argc > 1 && strcmp (argv[1], "-child") == 0))
140     {
141       /* This is the parent process.  */
142       signal (SIGINT, cleanup_then_die);
143       signal (SIGTERM, cleanup_then_die);
144       #ifdef SIGHUP
145       signal (SIGHUP, cleanup_then_die);
146       #endif
147
148       exitstatus = parent_main ();
149     }
150   else
151     {
152       /* This is the child process.  */
153       exitstatus = child_main ();
154     }
155   return exitstatus;
156 }