2012-11-16 21:25:35 +05:30
|
|
|
/*-
|
2020-04-24 15:14:19 +05:30
|
|
|
* Licensed under the SPDX BSD-2-Clause identifier.
|
|
|
|
* Use is subject to license terms, as specified in the LICENSE file.
|
2012-11-16 21:25:35 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "xbps_api_impl.h"
|
|
|
|
|
|
|
|
int
|
|
|
|
xbps_pkg_exec_buffer(struct xbps_handle *xhp,
|
|
|
|
const void *blob,
|
|
|
|
const size_t blobsiz,
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *pkgver,
|
2012-11-16 21:25:35 +05:30
|
|
|
const char *action,
|
|
|
|
bool update)
|
|
|
|
{
|
2019-08-13 17:26:32 +05:30
|
|
|
int i;
|
2012-11-16 21:25:35 +05:30
|
|
|
ssize_t ret;
|
2013-03-07 16:26:08 +05:30
|
|
|
const char *tmpdir, *version;
|
2019-10-27 14:05:55 +05:30
|
|
|
const char *shells[] = {
|
|
|
|
"/bin/sh",
|
|
|
|
"/bin/dash",
|
|
|
|
"/bin/bash",
|
|
|
|
NULL
|
|
|
|
};
|
2020-02-09 00:01:29 +05:30
|
|
|
char pkgname[XBPS_NAME_SIZE], *fpath;
|
2012-11-16 21:25:35 +05:30
|
|
|
int fd, rv;
|
|
|
|
|
|
|
|
assert(blob);
|
2013-03-05 08:38:42 +05:30
|
|
|
assert(pkgver);
|
2012-11-16 21:25:35 +05:30
|
|
|
assert(action);
|
|
|
|
|
2013-03-07 22:38:12 +05:30
|
|
|
if (xhp->target_arch) {
|
|
|
|
xbps_dbg_printf(xhp, "%s: not executing %s "
|
|
|
|
"install/remove action.\n", pkgver, action);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-17 16:38:25 +05:30
|
|
|
if (strcmp(xhp->rootdir, "/") == 0) {
|
|
|
|
tmpdir = getenv("TMPDIR");
|
|
|
|
if (tmpdir == NULL)
|
|
|
|
tmpdir = P_tmpdir;
|
2012-11-16 21:25:35 +05:30
|
|
|
|
2012-11-17 16:38:25 +05:30
|
|
|
fpath = xbps_xasprintf("%s/.xbps-script-XXXXXX", tmpdir);
|
|
|
|
} else {
|
|
|
|
fpath = strdup(".xbps-script-XXXXXX");
|
|
|
|
}
|
2012-11-16 21:25:35 +05:30
|
|
|
|
2012-11-19 19:29:42 +05:30
|
|
|
/* change cwd to rootdir to exec the script */
|
|
|
|
if (chdir(xhp->rootdir) == -1) {
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-11-16 21:25:35 +05:30
|
|
|
/* Create temp file to run script */
|
|
|
|
if ((fd = mkstemp(fpath)) == -1) {
|
2013-08-29 15:43:59 +05:30
|
|
|
rv = errno;
|
2012-11-16 21:25:35 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: mkstemp %s\n",
|
|
|
|
__func__, strerror(errno));
|
2013-08-29 15:43:59 +05:30
|
|
|
goto out;
|
2012-11-16 21:25:35 +05:30
|
|
|
}
|
|
|
|
/* write blob to our temp fd */
|
|
|
|
ret = write(fd, blob, blobsiz);
|
|
|
|
if (ret == -1) {
|
2013-08-29 15:43:59 +05:30
|
|
|
rv = errno;
|
2012-11-16 21:25:35 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: write %s\n",
|
|
|
|
__func__, strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
fchmod(fd, 0750);
|
2016-01-26 18:51:40 +05:30
|
|
|
#ifdef HAVE_FDATASYNC
|
2012-11-16 21:25:35 +05:30
|
|
|
fdatasync(fd);
|
2016-01-26 18:51:40 +05:30
|
|
|
#else
|
|
|
|
fsync(fd);
|
|
|
|
#endif
|
2012-11-16 21:25:35 +05:30
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* exec script */
|
2020-02-09 00:01:29 +05:30
|
|
|
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
|
|
|
|
abort();
|
|
|
|
}
|
2013-03-07 16:26:08 +05:30
|
|
|
version = xbps_pkg_version(pkgver);
|
|
|
|
assert(version);
|
|
|
|
|
2019-08-13 17:26:32 +05:30
|
|
|
// find a shell that can be used to execute the script.
|
|
|
|
for (i = 0; shells[i] != NULL; i++) {
|
|
|
|
if (access(shells[i], X_OK) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shells[i] != NULL) {
|
|
|
|
rv = xbps_file_exec(xhp, shells[i], fpath, action, pkgname, version,
|
|
|
|
update ? "yes" : "no",
|
|
|
|
"no", xhp->native_arch, NULL);
|
|
|
|
} else if (access("/bin/busybox", X_OK) == 0) {
|
|
|
|
rv = xbps_file_exec(xhp, "/bin/busybox", "sh", fpath, action, pkgname, version,
|
|
|
|
update ? "yes" : "no",
|
|
|
|
"no", xhp->native_arch, NULL);
|
|
|
|
} else if (access("/bin/busybox.static", X_OK) == 0) {
|
|
|
|
rv = xbps_file_exec(xhp, "/bin/busybox.static", "sh", fpath, action, pkgname, version,
|
|
|
|
update ? "yes" : "no",
|
|
|
|
"no", xhp->native_arch, NULL);
|
|
|
|
} else {
|
|
|
|
rv = -1;
|
|
|
|
}
|
2012-11-16 21:25:35 +05:30
|
|
|
|
|
|
|
out:
|
|
|
|
remove(fpath);
|
|
|
|
free(fpath);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
xbps_pkg_exec_script(struct xbps_handle *xhp,
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t d,
|
2012-11-16 21:25:35 +05:30
|
|
|
const char *script,
|
|
|
|
const char *action,
|
|
|
|
bool update)
|
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_data_t data;
|
2013-06-12 13:34:10 +05:30
|
|
|
void *buf;
|
|
|
|
size_t buflen;
|
2019-06-27 20:39:43 +05:30
|
|
|
const char *pkgver = NULL;
|
2013-06-12 13:34:10 +05:30
|
|
|
int rv;
|
2012-11-16 21:25:35 +05:30
|
|
|
|
|
|
|
assert(xhp);
|
|
|
|
assert(d);
|
|
|
|
assert(script);
|
|
|
|
assert(action);
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
data = xbps_dictionary_get(d, script);
|
2012-11-16 21:25:35 +05:30
|
|
|
if (data == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(d, "pkgver", &pkgver);
|
2012-11-16 21:25:35 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
buf = xbps_data_data(data);
|
|
|
|
buflen = xbps_data_size(data);
|
2013-06-12 13:34:10 +05:30
|
|
|
rv = xbps_pkg_exec_buffer(xhp, buf, buflen, pkgver, action, update);
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
return rv;
|
2012-11-16 21:25:35 +05:30
|
|
|
}
|