Blame src/internal/slibtool_spawn_impl.h

9fc046
/*******************************************************************/
eac61a
/*  slibtool: a strong libtool implementation, written in C        */
49181b
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
9fc046
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
9fc046
/*******************************************************************/
9fc046
089c8b
#ifndef SLIBTOOL_SPAWN_IMPL_H
089c8b
#define SLIBTOOL_SPAWN_IMPL_H
089c8b
22fe25
#include <limits.h>
9fc046
#include <unistd.h>
86e387
#include <stdlib.h>
9fc046
#include <stdbool.h>
819200
#include <errno.h>
9fc046
#include <sys/wait.h>
9fc046
22fe25
#ifndef PATH_MAX
22fe25
#define PATH_MAX (_XOPEN_PATH_MAX < 4096) ? 4096 : _XOPEN_PATH_MAX
22fe25
#endif
22fe25
9fc046
#ifndef SLBT_USE_FORK
9fc046
#ifndef SLBT_USE_VFORK
9fc046
#ifndef SLBT_USE_POSIX_SPAWN
9fc046
#define SLBT_USE_POSIX_SPAWN
9fc046
#endif
9fc046
#endif
9fc046
#endif
9fc046
9fc046
#ifdef  SLBT_USE_POSIX_SPAWN
9fc046
#include <spawn.h>
9fc046
#endif
9fc046
9fc046
static inline int slbt_spawn(
9fc046
	struct slbt_exec_ctx *	ectx,
9fc046
	bool			fwait)
9fc046
{
9fc046
	pid_t	pid;
9fc046
9fc046
#ifdef SLBT_USE_POSIX_SPAWN
9fc046
9fc046
	if (posix_spawnp(
9fc046
			&pid,
9fc046
			ectx->program,
9fc046
			0,0,
9fc046
			ectx->argv,
e8bfe5
			ectx->envp))
9fc046
		pid = -1;
9fc046
9fc046
#else
9fc046
9fc046
#ifdef SLBT_USE_FORK
9fc046
	pid = fork();
9fc046
#else
9fc046
	pid = vfork();
9fc046
#endif
9fc046
9fc046
#endif
9fc046
86e387
	if (pid < 0) {
86e387
		ectx->pid      = pid;
86e387
		ectx->exitcode = errno;
9fc046
		return -1;
86e387
	}
9fc046
86e387
	if (pid == 0) {
86e387
		execvp(
9fc046
			ectx->program,
9fc046
			ectx->argv);
86e387
		_exit(errno);
86e387
	}
9fc046
819200
	errno     = 0;
9fc046
	ectx->pid = pid;
9fc046
9fc046
	if (fwait)
9fc046
		return waitpid(
9fc046
			pid,
9fc046
			&ectx->exitcode,
9fc046
			0);
9fc046
9fc046
	return 0;
9fc046
}
089c8b
089c8b
#endif