
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <spawn.h>
#include <signal.h>

extern "C" int responsibility_spawnattrs_setdisclaim(posix_spawnattr_t attrs, int disclaim);

int main(int argc, char *const *argv, char *const *envp, const char **apple)
{
    if (argc < 2) {
        printf("missing arguments");
        return 1;
    }

    posix_spawnattr_t attr;
    posix_spawnattr_init(&attr);

    sigset_t no_signals;
    sigset_t all_signals;
    sigemptyset(&no_signals);
    sigfillset(&all_signals);
    posix_spawnattr_setsigmask(&attr, &no_signals);
    posix_spawnattr_setsigdefault(&attr, &all_signals);

    short flags = POSIX_SPAWN_SETEXEC | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK;
    posix_spawnattr_setflags(&attr, flags);

    responsibility_spawnattrs_setdisclaim(&attr, 1);

    pid_t pid = 0;
    posix_spawnp(&pid, argv[1], 0, &attr, argv+1, envp);

    posix_spawnattr_destroy(&attr);

    return 0;
}