This source file includes following definitions.
- parent_main
- child_main
- cleanup_then_die
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
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))
69 {
70 perror ("cannot seek in data file");
71 return 1;
72 }
73
74
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
103 unlink (DATA_FILENAME);
104
105 return 0;
106 }
107
108 static int
109 child_main (void)
110 {
111
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
127 unlink (DATA_FILENAME);
128
129
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
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
153 exitstatus = child_main ();
154 }
155 return exitstatus;
156 }