altomaltes / cross / slibtool

Forked from cross/slibtool 2 years ago
Clone

Blame src/logic/slbt_exec_execute.c

e956c8
/*******************************************************************/
e956c8
/*  slibtool: a skinny libtool implementation, written in C        */
6803d8
/*  Copyright (C) 2016--2018  Z. Gilboa                            */
e956c8
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
e956c8
/*******************************************************************/
e956c8
e956c8
#include <string.h>
e956c8
#include <stdbool.h>
e956c8
#include <fcntl.h>
e956c8
#include <errno.h>
e956c8
#include <sys/stat.h>
e956c8
e956c8
#include <slibtool/slibtool.h>
e956c8
#include "slibtool_spawn_impl.h"
7ae5c1
#include "slibtool_driver_impl.h"
a47c7a
#include "slibtool_errinfo_impl.h"
e956c8
e956c8
int  slbt_exec_execute(
e956c8
	const struct slbt_driver_ctx *	dctx,
e956c8
	struct slbt_exec_ctx *		ectx)
e956c8
{
e956c8
	int			ret;
7ae5c1
	int			fdcwd;
e956c8
	char *			program;
e956c8
	char *			script;
e956c8
	char *			base;
e956c8
	char *			mark;
e956c8
	char			exeref [PATH_MAX];
e956c8
	char			wrapper[PATH_MAX];
1ed71a
	struct stat		st;
e956c8
	struct slbt_exec_ctx *	actx = 0;
e956c8
c4a389
	/* dry run */
c4a389
	if (dctx->cctx->drvflags & SLBT_DRIVER_DRY_RUN)
c4a389
		return 0;
c4a389
e956c8
	/* context */
e956c8
	if (ectx)
e956c8
		slbt_disable_placeholders(ectx);
e956c8
	else if ((ret = slbt_get_exec_ctx(dctx,&ectx)))
e956c8
		return ret;
e956c8
	else {
e956c8
		actx = ectx;
e956c8
		slbt_disable_placeholders(ectx);
e956c8
	}
e956c8
e956c8
	/* script, program */
e956c8
	program = ectx->cargv[0];
e956c8
	script  = ectx->cargv[1];
e956c8
Lucio Andrés Illanes Albornoz f27e85
	if (script) {
Lucio Andrés Illanes Albornoz f27e85
		/* wrapper */
430d5a
		if ((size_t)snprintf(wrapper,sizeof(wrapper),
430d5a
					"%s%s.exe.wrapper",
Lucio Andrés Illanes Albornoz f27e85
					(script[0] == '/') ? "" : "./",
Lucio Andrés Illanes Albornoz f27e85
					script)
Lucio Andrés Illanes Albornoz f27e85
				>= sizeof(wrapper)) {
Lucio Andrés Illanes Albornoz f27e85
			slbt_free_exec_ctx(actx);
Lucio Andrés Illanes Albornoz f27e85
			return SLBT_BUFFER_ERROR(dctx);
Lucio Andrés Illanes Albornoz f27e85
		}
e956c8
Lucio Andrés Illanes Albornoz f27e85
		/* exeref */
Lucio Andrés Illanes Albornoz f27e85
		if ((base = strrchr(script,'/')))
Lucio Andrés Illanes Albornoz f27e85
			base++;
Lucio Andrés Illanes Albornoz f27e85
		else
Lucio Andrés Illanes Albornoz f27e85
			base = script;
e956c8
Lucio Andrés Illanes Albornoz f27e85
		strcpy(exeref,script);
Lucio Andrés Illanes Albornoz f27e85
		mark = exeref + (base - script);
Lucio Andrés Illanes Albornoz f27e85
		sprintf(mark,".libs/%s",base);
e956c8
7ae5c1
		/* fdcwd */
7ae5c1
		fdcwd = slbt_driver_fdcwd(dctx);
7ae5c1
Lucio Andrés Illanes Albornoz f27e85
		/* swap vector */
7ae5c1
		if (!fstatat(fdcwd,script,&st,0) && !fstatat(fdcwd,wrapper,&st,0)) {
Lucio Andrés Illanes Albornoz f27e85
			ectx->cargv[0] = wrapper;
Lucio Andrés Illanes Albornoz f27e85
			ectx->cargv[1] = program;
Lucio Andrés Illanes Albornoz f27e85
			ectx->cargv[2] = exeref;
Lucio Andrés Illanes Albornoz f27e85
		} else {
Lucio Andrés Illanes Albornoz f27e85
			script = program;
Lucio Andrés Illanes Albornoz f27e85
		}
Lucio Andrés Illanes Albornoz f27e85
Lucio Andrés Illanes Albornoz f27e85
		/* execute mode */
Lucio Andrés Illanes Albornoz f27e85
		ectx->program = script;
Lucio Andrés Illanes Albornoz f27e85
		ectx->argv    = ectx->cargv;
1ed71a
	} else {
Lucio Andrés Illanes Albornoz f27e85
		ectx->program = program;
Lucio Andrés Illanes Albornoz f27e85
		ectx->argv    = ectx->cargv;
1ed71a
	}
e956c8
e956c8
	/* step output */
e956c8
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
e956c8
		if (slbt_output_execute(dctx,ectx)) {
e956c8
			slbt_free_exec_ctx(actx);
a47c7a
			return SLBT_NESTED_ERROR(dctx);
e956c8
		}
e956c8
1ed71a
	execvp(ectx->cargv[0],ectx->argv);
e956c8
e956c8
	slbt_free_exec_ctx(actx);
6beda1
	return SLBT_SYSTEM_ERROR(dctx,0);
e956c8
}