File: test_wait.c

package info (click to toggle)
firebuild 0.8.5.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 2,552 kB
  • sloc: cpp: 13,693; ansic: 6,086; python: 2,313; sh: 261; makefile: 40; awk: 33
file content (251 lines) | stat: -rw-r--r-- 6,253 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright (c) 2022 Firebuild Inc.
 * All rights reserved.
 *
 * Free for personal use and commercial trial.
 * Non-trial commercial use requires licenses available from https://firebuild.com.
 * Modification and redistribution are permitted, but commercial use of derivative
 * works is subject to the same requirements of this license
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* Test for #185: a system(), pclose(), wait(), waitpid() has to
 * wait not only for the process to terminate, but also for the
 * supervisor to do the administration, check the state of the
 * files as produced by that child. If we allowed the parent to
 * continue, it might modify the files and thus the wrong actions
 * would be recorded for the child, causing problems when we
 * shortcut it the next time. */

#include <fcntl.h>
#if __linux__
#include <features.h>
#endif
#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
#if __has_include(<sys/pidfd.h>)
#include <sys/pidfd.h>
#endif
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#if !defined(__GLIBC_PREREQ)
#define __GLIBC_PREREQ(a, b) 0
#endif

extern char **environ;

#define TOSTR(x) TOSTR2(x)
#define TOSTR2(x) #x
#define LOC "[" __FILE__ ":" TOSTR(__LINE__) "]"

int main() {
  int fd;
  FILE *f, *f2;
  pid_t pid;
  siginfo_t info;

  /* Test waiting at system() */
  if ((fd = creat("test_wait_system.txt", 0600)) < 0) {
    perror("open" LOC);
    exit(1);
  }
  if (close(fd) != 0) {
    perror("close" LOC);
    exit(1);
  }
  if (system("exec touch test_wait_system.txt") != 0) {
    perror("system" LOC);
    exit(1);
  }
  if (unlink("test_wait_system.txt") != 0) {
    perror("unlink" LOC);
    exit(1);
  }

  /* Test waiting at pclose() */
  if ((fd = creat("test_wait_pclose.txt", 0600)) < 0) {
    perror("open" LOC);
    exit(1);
  }
  if (close(fd) != 0) {
    perror("close" LOC);
    exit(1);
  }
  if ((f = popen("exec touch test_wait_pclose.txt", "w")) == NULL) {
    perror("popen" LOC);
    exit(1);
  }
  /* Run popen again to excercise supervisor tracking f to be closed in the new child. */
  if ((f2 = popen("exec touch test_wait_pclose.txt", "r")) == NULL) {
    perror("popen" LOC);
    exit(1);
  }
  if (pclose(f) != 0) {
    perror("pclose" LOC);
    exit(1);
  }
  if (pclose(f2) != 0) {
    perror("pclose" LOC);
    exit(1);
  }
  if (unlink("test_wait_pclose.txt") != 0) {
    perror("unlink" LOC);
    exit(1);
  }

  /* Test waiting at wait() */
  if ((fd = creat("test_wait_wait.txt", 0600)) < 0) {
    perror("open" LOC);
    exit(1);
  }
  if (close(fd) != 0) {
    perror("close" LOC);
    exit(1);
  }
#if __GLIBC_PREREQ(2, 39)
  if (pidfd_spawn(&pid, "/usr/bin/touch",
#else
  if (posix_spawn(&pid, "/usr/bin/touch",
#endif
                  NULL, NULL,
                  (char *const[]){ "touch", "test_wait_wait.txt", NULL },
                  environ) != 0) {
    perror("posix_spawn" LOC);
    exit(1);
  }
#if __GLIBC_PREREQ(2, 39)
  pid = pidfd_getpid(pid);
#endif
  if (wait(NULL) != pid) {
    perror("wait" LOC);
    exit(1);
  }
  if (unlink("test_wait_wait.txt") != 0) {
    perror("unlink" LOC);
    exit(1);
  }

  /* Test waiting at waitpid() */
  if ((fd = creat("test_wait_waitpid.txt", 0600)) < 0) {
    perror("open" LOC);
    exit(1);
  }
  if (close(fd) != 0) {
    perror("close" LOC);
    exit(1);
  }
#if __GLIBC_PREREQ(2, 39)
  if (pidfd_spawnp(&pid, "touch",
#else
  if (posix_spawnp(&pid, "touch",
#endif
                  NULL, NULL,
                  (char *const[]){ "touch", "test_wait_waitpid.txt", NULL },
                  environ) != 0) {
    perror("posix_spawn" LOC);
    exit(1);
  }
#if __GLIBC_PREREQ(2, 39)
  pid = pidfd_getpid(pid);
#endif
  if (waitpid(pid, NULL, 0) != pid) {
    perror("waitpid" LOC);
    exit(1);
  }
  if (unlink("test_wait_waitpid.txt") != 0) {
    perror("unlink" LOC);
    exit(1);
  }

  /* Test waiting at wait3() */
  if ((fd = creat("test_wait_wait3.txt", 0600)) < 0) {
    perror("open" LOC);
    exit(1);
  }
  if (close(fd) != 0) {
    perror("close" LOC);
    exit(1);
  }
  if (posix_spawn(&pid, "/usr/bin/touch",
                  NULL, NULL,
                  (char *const[]){ "touch", "test_wait_wait3.txt", NULL },
                  environ) != 0) {
    perror("posix_spawn" LOC);
    exit(1);
  }
  if (wait3(NULL, 0, NULL) != pid) {
    perror("wait3" LOC);
    exit(1);
  }
  if (unlink("test_wait_wait3.txt") != 0) {
    perror("unlink" LOC);
    exit(1);
  }

  /* Test waiting at wait4() */
  if ((fd = creat("test_wait_wait4.txt", 0600)) < 0) {
    perror("open" LOC);
    exit(1);
  }
  if (close(fd) != 0) {
    perror("close" LOC);
    exit(1);
  }
  if (posix_spawn(&pid, "/usr/bin/touch",
                  NULL, NULL,
                  (char *const[]){ "touch", "test_wait_wait4.txt", NULL },
                  environ) != 0) {
    perror("posix_spawn" LOC);
    exit(1);
  }
  if (wait4(pid, NULL, 0, NULL) != pid) {
    perror("wait4" LOC);
    exit(1);
  }
  if (unlink("test_wait_wait4.txt") != 0) {
    perror("unlink" LOC);
    exit(1);
  }

  /* Test waiting at waitid() */
  if ((fd = creat("test_wait_waitid.txt", 0600)) < 0) {
    perror("open" LOC);
    exit(1);
  }
  if (close(fd) != 0) {
    perror("close" LOC);
    exit(1);
  }
  if (posix_spawn(&pid, "/usr/bin/touch",
                  NULL, NULL,
                  (char *const[]){"touch", "test_wait_waitid.txt", NULL},
                  environ) != 0) {
    perror("posix_spawn" LOC);
    exit(1);
  }
  if (waitid(P_PID, pid, &info, WEXITED) != 0) {
    perror("waitid" LOC);
    exit(1);
  }
  if (info.si_pid != pid) {
    fprintf(stderr, "waitid returned unexpected pid" LOC);
    exit(1);
  }
  if (unlink("test_wait_waitid.txt") != 0) {
    perror("unlink" LOC);
    exit(1);
  }

  return 0;
}