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-inherit1"
35 #define DATA_FILENAME "test-posix_spawn-inh1-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 = 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
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
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
110 unlink (DATA_FILENAME);
111
112 return 0;
113 }
114
115 static int
116 child_main (void)
117 {
118
119 fwrite ("Potta", 1, 5, stdout);
120
121
122
123 return 0;
124 }
125
126 static void
127 cleanup_then_die (int sig)
128 {
129
130 unlink (DATA_FILENAME);
131
132
133 signal (sig, SIG_DFL);
134 raise (sig);
135 }
136
137 int
138 main (int argc, char *argv[])
139 {
140 int exitstatus;
141
142 if (!(argc > 1 && strcmp (argv[1], "-child") == 0))
143 {
144
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
156 exitstatus = child_main ();
157 }
158 return exitstatus;
159 }