18eb8bab992e3998c33770b0cdb16059a8b918a06sewardj/* Test that an error from posix_spawn() is correctly propagated to the
28eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   parent. */
38eb8bab992e3998c33770b0cdb16059a8b918a06sewardj
48eb8bab992e3998c33770b0cdb16059a8b918a06sewardj#include <errno.h>
58eb8bab992e3998c33770b0cdb16059a8b918a06sewardj#include <stdio.h>
68eb8bab992e3998c33770b0cdb16059a8b918a06sewardj#include <stdlib.h>
78eb8bab992e3998c33770b0cdb16059a8b918a06sewardj#include <unistd.h>
88eb8bab992e3998c33770b0cdb16059a8b918a06sewardj#include <spawn.h>
98eb8bab992e3998c33770b0cdb16059a8b918a06sewardj
108eb8bab992e3998c33770b0cdb16059a8b918a06sewardjint main(void)
118eb8bab992e3998c33770b0cdb16059a8b918a06sewardj{
128eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   int res = 1;
138eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   int err;
148eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   posix_spawn_file_actions_t file_actions;
158eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   char *argv_exe[] = {"true", NULL};
168eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   char *envv_exe[] = {NULL};
178eb8bab992e3998c33770b0cdb16059a8b918a06sewardj
188eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   err = posix_spawn_file_actions_init(&file_actions);
198eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   if (err != 0) {
208eb8bab992e3998c33770b0cdb16059a8b918a06sewardj      errno = err;
218eb8bab992e3998c33770b0cdb16059a8b918a06sewardj      perror("posix_spawn_file_actions_init");
228eb8bab992e3998c33770b0cdb16059a8b918a06sewardj      return 1;
238eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   }
248eb8bab992e3998c33770b0cdb16059a8b918a06sewardj
258eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   err = posix_spawn_file_actions_adddup2(&file_actions, 3, 4);
268eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   if (err != 0) {
278eb8bab992e3998c33770b0cdb16059a8b918a06sewardj      errno = err;
288eb8bab992e3998c33770b0cdb16059a8b918a06sewardj      perror("posix_spawn_file_actions_adddup2");
298eb8bab992e3998c33770b0cdb16059a8b918a06sewardj      goto out;
308eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   }
318eb8bab992e3998c33770b0cdb16059a8b918a06sewardj
328eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   /* The following call to posix_spawn() should fail because the requested
338eb8bab992e3998c33770b0cdb16059a8b918a06sewardj      dup2 action cannot be performed. */
348eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   err = posix_spawn(NULL, "/bin/true", &file_actions, NULL, argv_exe,
358eb8bab992e3998c33770b0cdb16059a8b918a06sewardj                     envv_exe);
368eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   if (err != 0) {
378eb8bab992e3998c33770b0cdb16059a8b918a06sewardj      errno = err;
388eb8bab992e3998c33770b0cdb16059a8b918a06sewardj      perror("posix_spawn");
398eb8bab992e3998c33770b0cdb16059a8b918a06sewardj      goto out;
408eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   }
418eb8bab992e3998c33770b0cdb16059a8b918a06sewardj
428eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   res = 0;
438eb8bab992e3998c33770b0cdb16059a8b918a06sewardj
448eb8bab992e3998c33770b0cdb16059a8b918a06sewardjout:
458eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   err = posix_spawn_file_actions_destroy(&file_actions);
468eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   if (err != 0) {
478eb8bab992e3998c33770b0cdb16059a8b918a06sewardj      errno = err;
488eb8bab992e3998c33770b0cdb16059a8b918a06sewardj      perror("posix_spawn_file_actions_destroy");
498eb8bab992e3998c33770b0cdb16059a8b918a06sewardj      res = 1;
508eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   }
518eb8bab992e3998c33770b0cdb16059a8b918a06sewardj
528eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   return res;
538eb8bab992e3998c33770b0cdb16059a8b918a06sewardj}
548eb8bab992e3998c33770b0cdb16059a8b918a06sewardj
55