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
|
/*
* 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.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#if __linux__
#include <features.h>
#endif
#include <spawn.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#if !defined(__GLIBC_PREREQ)
#define __GLIBC_PREREQ(a, b) 0
#endif
/* Performs a posix_spawnp() / waitpid() pair.
* The command to execute and its parameters are taken from the command line, one by one.
* Returns 0 (unless an error occurred), not the command's exit code. */
#ifdef __APPLE__
extern char **environ;
#endif
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "need at least 1 argument\n");
return 1;
}
posix_spawnattr_t attributes;
posix_spawnattr_init(&attributes);
#ifdef __APPLE__
posix_spawnattr_setflags(&attributes, POSIX_SPAWN_CLOEXEC_DEFAULT);
#endif
/* Test with all kinds of file_actions */
posix_spawn_file_actions_t file_actions;
posix_spawn_file_actions_init(&file_actions);
posix_spawn_file_actions_addopen(&file_actions, 96, "integration.bats", O_RDWR, 0);
posix_spawn_file_actions_addopen(&file_actions, 97, ".", O_RDONLY, 0);
posix_spawn_file_actions_adddup2(&file_actions, 97, 98);
posix_spawn_file_actions_adddup2(&file_actions, 1, 1);
posix_spawn_file_actions_addclose(&file_actions, 97);
#if __GLIBC_PREREQ(2, 29)
posix_spawn_file_actions_addchdir_np(&file_actions, ".");
posix_spawn_file_actions_addfchdir_np(&file_actions,
#ifdef __APPLE__
open(".", O_RDONLY, 0));
#else
98);
#endif
#endif
#if __GLIBC_PREREQ(2, 34)
posix_spawn_file_actions_addclosefrom_np(&file_actions, 94);
#endif
#ifdef __APPLE__
posix_spawn_file_actions_addinherit_np(&file_actions, 0);
posix_spawn_file_actions_addinherit_np(&file_actions, 1);
posix_spawn_file_actions_addinherit_np(&file_actions, 2);
#endif
pid_t pid;
int ret = posix_spawnp(&pid, argv[1], &file_actions, &attributes, argv + 1, environ);
if (ret) {
errno = ret;
perror("posix_spawnp");
return 1;
}
posix_spawnattr_destroy(&attributes);
posix_spawn_file_actions_destroy(&file_actions);
if (waitpid(pid, NULL, 0) < 0) {
perror("waitpid");
return 1;
}
return 0;
}
|