From 8dc63ddc326ec54709c580a400536fcc4ef62622 Mon Sep 17 00:00:00 2001 From: midipix Date: Mon, 19 Feb 2024 01:54:51 +0000 Subject: library api's: _util_ (utility helper interfaces) namespace overhaul. --- src/driver/slbt_host_params.c | 2 +- src/fallback/slbt_archive_import_mri.c | 8 +- src/helper/slbt_archive_import.c | 69 -------------- src/helper/slbt_copy_file.c | 76 ---------------- src/helper/slbt_dump_machine.c | 135 ---------------------------- src/helper/slbt_map_input.c | 67 -------------- src/helper/slbt_realpath.c | 101 --------------------- src/internal/slibtool_lconf_impl.c | 4 +- src/logic/linkcmd/slbt_linkcmd_archive.c | 2 +- src/logic/linkcmd/slbt_linkcmd_dsolib.c | 2 +- src/logic/linkcmd/slbt_linkcmd_executable.c | 2 +- src/logic/slbt_exec_install.c | 10 +-- src/logic/slbt_exec_link.c | 2 +- src/output/slbt_output_fdcwd.c | 4 +- src/util/slbt_archive_import.c | 69 ++++++++++++++ src/util/slbt_copy_file.c | 76 ++++++++++++++++ src/util/slbt_dump_machine.c | 135 ++++++++++++++++++++++++++++ src/util/slbt_map_input.c | 67 ++++++++++++++ src/util/slbt_realpath.c | 101 +++++++++++++++++++++ 19 files changed, 466 insertions(+), 466 deletions(-) delete mode 100644 src/helper/slbt_archive_import.c delete mode 100644 src/helper/slbt_copy_file.c delete mode 100644 src/helper/slbt_dump_machine.c delete mode 100644 src/helper/slbt_map_input.c delete mode 100644 src/helper/slbt_realpath.c create mode 100644 src/util/slbt_archive_import.c create mode 100644 src/util/slbt_copy_file.c create mode 100644 src/util/slbt_dump_machine.c create mode 100644 src/util/slbt_map_input.c create mode 100644 src/util/slbt_realpath.c (limited to 'src') diff --git a/src/driver/slbt_host_params.c b/src/driver/slbt_host_params.c index d708028..dd4e787 100644 --- a/src/driver/slbt_host_params.c +++ b/src/driver/slbt_host_params.c @@ -209,7 +209,7 @@ slbt_hidden int slbt_init_host_params( host->host = drvhost->machine; cfgmeta->host = cfgnmachine; - } else if (slbt_dump_machine(cctx->cargv[0],buf,sizeof(buf)) < 0) { + } else if (slbt_util_dump_machine(cctx->cargv[0],buf,sizeof(buf)) < 0) { if (dctx) slbt_dprintf( slbt_driver_fderr(dctx), diff --git a/src/fallback/slbt_archive_import_mri.c b/src/fallback/slbt_archive_import_mri.c index a171f5c..a65578c 100644 --- a/src/fallback/slbt_archive_import_mri.c +++ b/src/fallback/slbt_archive_import_mri.c @@ -42,7 +42,7 @@ static char * slbt_mri_argument( if (arg[0] == '/') { target = arg; } else { - if (slbt_realpath( + if (slbt_util_realpath( fdat,".",O_DIRECTORY, mricwd,sizeof(mricwd)) < 0) return 0; @@ -81,7 +81,7 @@ static char * slbt_mri_argument( return lnk; } -static void slbt_archive_import_child( +static void slbt_util_import_archive_child( char * program, int fd[2]) { @@ -99,7 +99,7 @@ static void slbt_archive_import_child( _exit(EXIT_FAILURE); } -int slbt_archive_import_mri( +int slbt_util_import_archive_mri( const struct slbt_driver_ctx * dctx, struct slbt_exec_ctx * ectx, char * dstarchive, @@ -140,7 +140,7 @@ int slbt_archive_import_mri( /* child */ if (pid == 0) - slbt_archive_import_child( + slbt_util_import_archive_child( program, fd); diff --git a/src/helper/slbt_archive_import.c b/src/helper/slbt_archive_import.c deleted file mode 100644 index d339f78..0000000 --- a/src/helper/slbt_archive_import.c +++ /dev/null @@ -1,69 +0,0 @@ -/*******************************************************************/ -/* slibtool: a skinny libtool implementation, written in C */ -/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */ -/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */ -/*******************************************************************/ - -#include -#include "slibtool_driver_impl.h" -#include "slibtool_symlink_impl.h" -#include "slibtool_errinfo_impl.h" - -/* legacy fallback, no longer in use */ -extern int slbt_archive_import_mri( - const struct slbt_driver_ctx * dctx, - struct slbt_exec_ctx * ectx, - char * dstarchive, - char * srcarchive); - -/* use slibtool's in-memory archive merging facility */ -static int slbt_archive_import_impl( - const struct slbt_driver_ctx * dctx, - struct slbt_exec_ctx * ectx, - char * dstarchive, - char * srcarchive) -{ - int ret; - struct slbt_archive_ctx * arctxv[3] = {0,0,0}; - struct slbt_archive_ctx * arctx; - - (void)ectx; - - if (slbt_ar_get_archive_ctx(dctx,dstarchive,&arctxv[0]) < 0) - return SLBT_NESTED_ERROR(dctx); - - if (slbt_ar_get_archive_ctx(dctx,srcarchive,&arctxv[1]) < 0) { - slbt_ar_free_archive_ctx(arctxv[0]); - return SLBT_NESTED_ERROR(dctx); - } - - ret = slbt_ar_merge_archives(arctxv,&arctx); - - slbt_ar_free_archive_ctx(arctxv[0]); - slbt_ar_free_archive_ctx(arctxv[1]); - - if (ret == 0) { - ret = slbt_ar_store_archive(arctx,dstarchive,0644); - slbt_ar_free_archive_ctx(arctx); - } - - return (ret < 0) ? SLBT_NESTED_ERROR(dctx) : 0; -} - - -int slbt_archive_import( - const struct slbt_driver_ctx * dctx, - struct slbt_exec_ctx * ectx, - char * dstarchive, - char * srcarchive) -{ - if (slbt_symlink_is_a_placeholder( - slbt_driver_fdcwd(dctx), - srcarchive)) - return 0; - - return slbt_archive_import_impl( - dctx,ectx, - dstarchive, - srcarchive); -} diff --git a/src/helper/slbt_copy_file.c b/src/helper/slbt_copy_file.c deleted file mode 100644 index 8f4d264..0000000 --- a/src/helper/slbt_copy_file.c +++ /dev/null @@ -1,76 +0,0 @@ -/*******************************************************************/ -/* slibtool: a skinny libtool implementation, written in C */ -/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */ -/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */ -/*******************************************************************/ - -#include -#include "slibtool_driver_impl.h" -#include "slibtool_spawn_impl.h" -#include "slibtool_symlink_impl.h" -#include "slibtool_errinfo_impl.h" - -int slbt_copy_file( - const struct slbt_driver_ctx * dctx, - struct slbt_exec_ctx * ectx, - char * src, - char * dst) -{ - int fdcwd; - char ** oargv; - char * oprogram; - char * cp[4]; - int ret; - - /* fdcwd */ - fdcwd = slbt_driver_fdcwd(dctx); - - /* placeholder? */ - if (slbt_symlink_is_a_placeholder(fdcwd,src)) - return 0; - - /* cp argv */ - cp[0] = "cp"; - cp[1] = src; - cp[2] = dst; - cp[3] = 0; - - /* alternate argument vector */ - oprogram = ectx->program; - oargv = ectx->argv; - ectx->argv = cp; - ectx->program = "cp"; - - /* step output */ - if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT)) { - if (dctx->cctx->mode == SLBT_MODE_LINK) { - if (slbt_output_link(dctx,ectx)) { - ectx->argv = oargv; - ectx->program = oprogram; - return SLBT_NESTED_ERROR(dctx); - } - } else { - if (slbt_output_install(dctx,ectx)) { - ectx->argv = oargv; - ectx->program = oprogram; - return SLBT_NESTED_ERROR(dctx); - } - } - } - - /* cp spawn */ - if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0)) { - ret = SLBT_SPAWN_ERROR(dctx); - - } else if (ectx->exitcode) { - ret = SLBT_CUSTOM_ERROR( - dctx, - SLBT_ERR_COPY_ERROR); - } else { - ret = 0; - } - - ectx->argv = oargv; - ectx->program = oprogram; - return ret; -} diff --git a/src/helper/slbt_dump_machine.c b/src/helper/slbt_dump_machine.c deleted file mode 100644 index 40ef387..0000000 --- a/src/helper/slbt_dump_machine.c +++ /dev/null @@ -1,135 +0,0 @@ -/*******************************************************************/ -/* slibtool: a skinny libtool implementation, written in C */ -/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */ -/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */ -/*******************************************************************/ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include "slibtool_spawn_impl.h" -#include "slibtool_snprintf_impl.h" - -static void slbt_dump_machine_child( - char * program, - int fd[2]) -{ - char * compiler; - char * argv[3]; - - close(fd[0]); - - if ((compiler = strrchr(program,'/'))) - compiler++; - else - compiler = program; - - argv[0] = compiler; - argv[1] = "-dumpmachine"; - argv[2] = 0; - - if ((fd[0] = openat(AT_FDCWD,"/dev/null",O_RDONLY,0)) >= 0) - if (dup2(fd[0],0) == 0) - if (dup2(fd[1],1) == 1) - execvp(program,argv); - - _exit(EXIT_FAILURE); -} - -int slbt_dump_machine( - const char * compiler, - char * machine, - size_t buflen) -{ - ssize_t ret; - pid_t pid; - pid_t rpid; - int code; - int fd[2]; - char * mark; - char program[PATH_MAX]; - - /* setup */ - if (!machine || !buflen || !--buflen) { - errno = EINVAL; - return -1; - } - - if (slbt_snprintf(program,sizeof(program), - "%s",compiler) < 0) - return -1; - - /* fork */ - if (pipe(fd)) - return -1; - - if ((pid = fork()) < 0) { - close(fd[0]); - close(fd[1]); - return -1; - } - - /* child */ - if (pid == 0) - slbt_dump_machine_child( - program, - fd); - - /* parent */ - close(fd[1]); - - mark = machine; - - for (; buflen; ) { - ret = read(fd[0],mark,buflen); - - while ((ret < 0) && (errno == EINTR)) - ret = read(fd[0],mark,buflen); - - if (ret > 0) { - buflen -= ret; - mark += ret; - - } else if (ret == 0) { - close(fd[0]); - buflen = 0; - - } else { - close(fd[0]); - return -1; - } - } - - /* execve verification */ - rpid = waitpid( - pid, - &code, - 0); - - if ((rpid != pid) || code) { - errno = ESTALE; - return -1; - } - - /* newline verification */ - if ((mark == machine) || (*--mark != '\n')) { - errno = ERANGE; - return -1; - } - - *mark = 0; - - /* portbld <--> unknown synonym? */ - if ((mark = strstr(machine,"-portbld-"))) - memcpy(mark,"-unknown",8); - - /* all done */ - return 0; -} diff --git a/src/helper/slbt_map_input.c b/src/helper/slbt_map_input.c deleted file mode 100644 index ca0b06b..0000000 --- a/src/helper/slbt_map_input.c +++ /dev/null @@ -1,67 +0,0 @@ -/*******************************************************************/ -/* slibtool: a skinny libtool implementation, written in C */ -/* Copyright (C) 2015--2024 SysDeer Technologies, LLC */ -/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */ -/*******************************************************************/ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include "slibtool_driver_impl.h" -#include "slibtool_errinfo_impl.h" - -int slbt_map_input( - const struct slbt_driver_ctx * dctx, - int fd, - const char * path, - int prot, - struct slbt_input * map) -{ - int ret; - struct stat st; - bool fnew; - int fdcwd; - - fdcwd = slbt_driver_fdcwd(dctx); - - if ((fnew = (fd < 0))) - fd = openat(fdcwd,path,O_RDONLY | O_CLOEXEC); - - if (fd < 0) - return SLBT_SYSTEM_ERROR(dctx,path); - - if ((ret = fstat(fd,&st) < 0) && fnew) - close(fd); - - else if ((st.st_size == 0) && fnew) - close(fd); - - if (ret < 0) - return SLBT_SYSTEM_ERROR(dctx,path); - - if (st.st_size == 0) { - map->size = 0; - map->addr = 0; - } else { - map->size = st.st_size; - map->addr = mmap(0,map->size,prot,MAP_PRIVATE,fd,0); - } - - if (fnew) - close(fd); - - return (map->addr == MAP_FAILED) - ? SLBT_SYSTEM_ERROR(dctx,path) - : 0; -} - -int slbt_unmap_input(struct slbt_input * map) -{ - return map->size ? munmap(map->addr,map->size) : 0; -} diff --git a/src/helper/slbt_realpath.c b/src/helper/slbt_realpath.c deleted file mode 100644 index 936a9eb..0000000 --- a/src/helper/slbt_realpath.c +++ /dev/null @@ -1,101 +0,0 @@ -/*******************************************************************/ -/* slibtool: a skinny libtool implementation, written in C */ -/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */ -/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */ -/*******************************************************************/ - -#include -#include -#include -#include -#include -#include - -#include "slibtool_driver_impl.h" -#include "slibtool_readlink_impl.h" - -#ifdef HAVE_SYS_SYSCALL_H -#include -#endif - -#ifdef _MIDIPIX_ABI -#include -#endif - -#ifndef ENOTSUP -#define ENOTSUP EOPNOTSUPP -#endif - -#ifdef SYS___realpathat -extern long syscall(int, ...); -#endif - -int slbt_realpath( - int fdat, - const char * path, - int options, - char * buf, - size_t buflen) -{ - int ret; - int fd; - int fdproc; - struct stat st; - struct stat stproc; - char procfspath[36]; - - /* common validation */ - if (!buf || (options & O_CREAT)) { - errno = EINVAL; - return -1; - } - - /* framework-based wrapper */ -#ifdef _MIDIPIX_ABI - return __fs_rpath(fdat,path,options,buf,buflen); -#endif - -#ifdef SYS___realpathat - return syscall(SYS___realpathat,fdat,path,buf,buflen,0); -#endif - - /* buflen */ - if (buflen < PATH_MAX) { - errno = ENOBUFS; - return -1; - } - - /* AT_FDCWD */ - if (fdat == AT_FDCWD) { - return realpath(path,buf) ? 0 : -1; - } - - /* /proc/self/fd */ - if ((fd = openat(fdat,path,options,0)) < 0) - return -1; - - sprintf(procfspath,"/proc/self/fd/%d",fd); - - if (slbt_readlinkat(fdat,procfspath,buf,buflen)) { - close(fd); - return -1; - } - - if ((fdproc = openat(AT_FDCWD,buf,options|O_NOFOLLOW,0)) < 0) { - close(fd); - errno = ELOOP; - return -1; - } - - ret = fstat(fd,&st) || fstat(fdproc,&stproc); - - close(fd); - close(fdproc); - - if (ret || (st.st_dev != stproc.st_dev) || (st.st_ino != stproc.st_ino)) { - errno = ENOTSUP; - return -1; - } - - return 0; -} diff --git a/src/internal/slibtool_lconf_impl.c b/src/internal/slibtool_lconf_impl.c index 32f192d..dc73bc3 100644 --- a/src/internal/slibtool_lconf_impl.c +++ b/src/internal/slibtool_lconf_impl.c @@ -387,7 +387,7 @@ static int slbt_lconf_trace_result_plain( fderr = slbt_driver_fderr(dctx); - cpath = !(slbt_realpath(fdat,lconf,0,path,sizeof(path))) + cpath = !(slbt_util_realpath(fdat,lconf,0,path,sizeof(path))) ? path : lconf; switch (err) { @@ -435,7 +435,7 @@ static int slbt_lconf_trace_result_annotated( fderr = slbt_driver_fderr(dctx); - cpath = !(slbt_realpath(fdat,lconf,0,path,sizeof(path))) + cpath = !(slbt_util_realpath(fdat,lconf,0,path,sizeof(path))) ? path : lconf; switch (err) { diff --git a/src/logic/linkcmd/slbt_linkcmd_archive.c b/src/logic/linkcmd/slbt_linkcmd_archive.c index e3ac657..9fb1778 100644 --- a/src/logic/linkcmd/slbt_linkcmd_archive.c +++ b/src/logic/linkcmd/slbt_linkcmd_archive.c @@ -165,7 +165,7 @@ slbt_hidden int slbt_exec_link_create_archive( /* input objects associated with .la archives */ for (parg=ectx->cargv; *parg; parg++) if (slbt_adjust_wrapper_argument(*parg,true)) - if (slbt_archive_import(dctx,ectx,output,*parg)) + if (slbt_util_import_archive(dctx,ectx,output,*parg)) return SLBT_NESTED_ERROR(dctx); return 0; diff --git a/src/logic/linkcmd/slbt_linkcmd_dsolib.c b/src/logic/linkcmd/slbt_linkcmd_dsolib.c index ed9aa6c..8ae9395 100644 --- a/src/logic/linkcmd/slbt_linkcmd_dsolib.c +++ b/src/logic/linkcmd/slbt_linkcmd_dsolib.c @@ -243,7 +243,7 @@ slbt_hidden int slbt_exec_link_create_library( } /* cwd */ - if (slbt_realpath(fdcwd,".",O_DIRECTORY,cwd,sizeof(cwd))) + if (slbt_util_realpath(fdcwd,".",O_DIRECTORY,cwd,sizeof(cwd))) return SLBT_SYSTEM_ERROR(dctx,0); /* .libs/libfoo.so --> -L.libs -lfoo */ diff --git a/src/logic/linkcmd/slbt_linkcmd_executable.c b/src/logic/linkcmd/slbt_linkcmd_executable.c index a4c3b81..3a47a49 100644 --- a/src/logic/linkcmd/slbt_linkcmd_executable.c +++ b/src/logic/linkcmd/slbt_linkcmd_executable.c @@ -136,7 +136,7 @@ slbt_hidden int slbt_exec_link_create_executable( verinfo = slbt_source_version(); /* cwd, DL_PATH fixup */ - if (slbt_realpath(fdcwd,".",O_DIRECTORY,cwd,sizeof(cwd))) + if (slbt_util_realpath(fdcwd,".",O_DIRECTORY,cwd,sizeof(cwd))) return SLBT_SYSTEM_ERROR(dctx,0); slbt_emit_fdwrap_dl_path_fixup( diff --git a/src/logic/slbt_exec_install.c b/src/logic/slbt_exec_install.c index 395e972..7b30eb4 100644 --- a/src/logic/slbt_exec_install.c +++ b/src/logic/slbt_exec_install.c @@ -191,7 +191,7 @@ static int slbt_exec_install_import_libraries( dctx->cctx->asettings.impsuffix); /* copy: .libs/libfoo.x.y.z.lib.a --> dstdir */ - if (slbt_copy_file(dctx,ectx,srcbuf,dstdir)) + if (slbt_util_copy_file(dctx,ectx,srcbuf,dstdir)) return SLBT_NESTED_ERROR(dctx); /* .libs/libfoo.x.lib.a */ @@ -200,7 +200,7 @@ static int slbt_exec_install_import_libraries( dctx->cctx->asettings.impsuffix); /* copy: .libs/libfoo.x.lib.a --> dstdir */ - if (slbt_copy_file(dctx,ectx,srcbuf,dstdir)) + if (slbt_util_copy_file(dctx,ectx,srcbuf,dstdir)) return SLBT_NESTED_ERROR(dctx); /* /dstdir/libfoo.lib.a */ @@ -310,7 +310,7 @@ static int slbt_exec_install_library_wrapper( slbt_unmap_file(mapinfo); /* cp libfoo.la.slibtool.instal /dstdir/libfoo.la */ - if (slbt_copy_file(dctx,ectx,clainame,instname)) + if (slbt_util_copy_file(dctx,ectx,clainame,instname)) return SLBT_NESTED_ERROR(dctx); return 0; @@ -555,7 +555,7 @@ static int slbt_exec_install_entry( farchive = false; if (farchive) - if (slbt_copy_file(dctx,ectx, + if (slbt_util_copy_file(dctx,ectx, srcfile, dest ? (char *)dest->arg : *dst)) return SLBT_NESTED_ERROR(dctx); @@ -682,7 +682,7 @@ static int slbt_exec_install_entry( if (fpe) { /* copy: .libs/libfoo.so.x.y.z --> libfoo.so.x */ - if (slbt_copy_file( + if (slbt_util_copy_file( dctx,ectx, srcfile, dlnkname)) diff --git a/src/logic/slbt_exec_link.c b/src/logic/slbt_exec_link.c index 3b6bc6f..a08abd4 100644 --- a/src/logic/slbt_exec_link.c +++ b/src/logic/slbt_exec_link.c @@ -119,7 +119,7 @@ static int slbt_exec_link_create_library_symlink( if (fmajor && (dctx->cctx->drvflags & SLBT_DRIVER_IMAGE_PE)) - return slbt_copy_file( + return slbt_util_copy_file( dctx,ectx, target,lnkname); else diff --git a/src/output/slbt_output_fdcwd.c b/src/output/slbt_output_fdcwd.c index 705e6d1..fa7e8a7 100644 --- a/src/output/slbt_output_fdcwd.c +++ b/src/output/slbt_output_fdcwd.c @@ -36,7 +36,7 @@ static int slbt_output_fdcwd_plain(const struct slbt_driver_ctx * dctx) sprintf(scwd,"%d",fdcwd); } - if (slbt_realpath(fdcwd,".",0,path,sizeof(path)) < 0) { + if (slbt_util_realpath(fdcwd,".",0,path,sizeof(path)) < 0) { ferror = 1; memset(path,0,sizeof(path)); strerror_r(errno,path,sizeof(path)); @@ -72,7 +72,7 @@ static int slbt_output_fdcwd_annotated(const struct slbt_driver_ctx * dctx) sprintf(scwd,"%d",fdcwd); } - if (slbt_realpath(fdcwd,".",0,path,sizeof(path)) < 0) { + if (slbt_util_realpath(fdcwd,".",0,path,sizeof(path)) < 0) { ferror = 1; memset(path,0,sizeof(path)); strerror_r(errno,path,sizeof(path)); diff --git a/src/util/slbt_archive_import.c b/src/util/slbt_archive_import.c new file mode 100644 index 0000000..5657fcc --- /dev/null +++ b/src/util/slbt_archive_import.c @@ -0,0 +1,69 @@ +/*******************************************************************/ +/* slibtool: a skinny libtool implementation, written in C */ +/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */ +/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */ +/*******************************************************************/ + +#include +#include "slibtool_driver_impl.h" +#include "slibtool_symlink_impl.h" +#include "slibtool_errinfo_impl.h" + +/* legacy fallback, no longer in use */ +extern int slbt_util_import_archive_mri( + const struct slbt_driver_ctx * dctx, + struct slbt_exec_ctx * ectx, + char * dstarchive, + char * srcarchive); + +/* use slibtool's in-memory archive merging facility */ +static int slbt_util_import_archive_impl( + const struct slbt_driver_ctx * dctx, + struct slbt_exec_ctx * ectx, + char * dstarchive, + char * srcarchive) +{ + int ret; + struct slbt_archive_ctx * arctxv[3] = {0,0,0}; + struct slbt_archive_ctx * arctx; + + (void)ectx; + + if (slbt_ar_get_archive_ctx(dctx,dstarchive,&arctxv[0]) < 0) + return SLBT_NESTED_ERROR(dctx); + + if (slbt_ar_get_archive_ctx(dctx,srcarchive,&arctxv[1]) < 0) { + slbt_ar_free_archive_ctx(arctxv[0]); + return SLBT_NESTED_ERROR(dctx); + } + + ret = slbt_ar_merge_archives(arctxv,&arctx); + + slbt_ar_free_archive_ctx(arctxv[0]); + slbt_ar_free_archive_ctx(arctxv[1]); + + if (ret == 0) { + ret = slbt_ar_store_archive(arctx,dstarchive,0644); + slbt_ar_free_archive_ctx(arctx); + } + + return (ret < 0) ? SLBT_NESTED_ERROR(dctx) : 0; +} + + +int slbt_util_import_archive( + const struct slbt_driver_ctx * dctx, + struct slbt_exec_ctx * ectx, + char * dstarchive, + char * srcarchive) +{ + if (slbt_symlink_is_a_placeholder( + slbt_driver_fdcwd(dctx), + srcarchive)) + return 0; + + return slbt_util_import_archive_impl( + dctx,ectx, + dstarchive, + srcarchive); +} diff --git a/src/util/slbt_copy_file.c b/src/util/slbt_copy_file.c new file mode 100644 index 0000000..30d3f03 --- /dev/null +++ b/src/util/slbt_copy_file.c @@ -0,0 +1,76 @@ +/*******************************************************************/ +/* slibtool: a skinny libtool implementation, written in C */ +/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */ +/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */ +/*******************************************************************/ + +#include +#include "slibtool_driver_impl.h" +#include "slibtool_spawn_impl.h" +#include "slibtool_symlink_impl.h" +#include "slibtool_errinfo_impl.h" + +int slbt_util_copy_file( + const struct slbt_driver_ctx * dctx, + struct slbt_exec_ctx * ectx, + char * src, + char * dst) +{ + int fdcwd; + char ** oargv; + char * oprogram; + char * cp[4]; + int ret; + + /* fdcwd */ + fdcwd = slbt_driver_fdcwd(dctx); + + /* placeholder? */ + if (slbt_symlink_is_a_placeholder(fdcwd,src)) + return 0; + + /* cp argv */ + cp[0] = "cp"; + cp[1] = src; + cp[2] = dst; + cp[3] = 0; + + /* alternate argument vector */ + oprogram = ectx->program; + oargv = ectx->argv; + ectx->argv = cp; + ectx->program = "cp"; + + /* step output */ + if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT)) { + if (dctx->cctx->mode == SLBT_MODE_LINK) { + if (slbt_output_link(dctx,ectx)) { + ectx->argv = oargv; + ectx->program = oprogram; + return SLBT_NESTED_ERROR(dctx); + } + } else { + if (slbt_output_install(dctx,ectx)) { + ectx->argv = oargv; + ectx->program = oprogram; + return SLBT_NESTED_ERROR(dctx); + } + } + } + + /* cp spawn */ + if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0)) { + ret = SLBT_SPAWN_ERROR(dctx); + + } else if (ectx->exitcode) { + ret = SLBT_CUSTOM_ERROR( + dctx, + SLBT_ERR_COPY_ERROR); + } else { + ret = 0; + } + + ectx->argv = oargv; + ectx->program = oprogram; + return ret; +} diff --git a/src/util/slbt_dump_machine.c b/src/util/slbt_dump_machine.c new file mode 100644 index 0000000..bf86315 --- /dev/null +++ b/src/util/slbt_dump_machine.c @@ -0,0 +1,135 @@ +/*******************************************************************/ +/* slibtool: a skinny libtool implementation, written in C */ +/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */ +/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */ +/*******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "slibtool_spawn_impl.h" +#include "slibtool_snprintf_impl.h" + +static void slbt_util_dump_machine_child( + char * program, + int fd[2]) +{ + char * compiler; + char * argv[3]; + + close(fd[0]); + + if ((compiler = strrchr(program,'/'))) + compiler++; + else + compiler = program; + + argv[0] = compiler; + argv[1] = "-dumpmachine"; + argv[2] = 0; + + if ((fd[0] = openat(AT_FDCWD,"/dev/null",O_RDONLY,0)) >= 0) + if (dup2(fd[0],0) == 0) + if (dup2(fd[1],1) == 1) + execvp(program,argv); + + _exit(EXIT_FAILURE); +} + +int slbt_util_dump_machine( + const char * compiler, + char * machine, + size_t buflen) +{ + ssize_t ret; + pid_t pid; + pid_t rpid; + int code; + int fd[2]; + char * mark; + char program[PATH_MAX]; + + /* setup */ + if (!machine || !buflen || !--buflen) { + errno = EINVAL; + return -1; + } + + if (slbt_snprintf(program,sizeof(program), + "%s",compiler) < 0) + return -1; + + /* fork */ + if (pipe(fd)) + return -1; + + if ((pid = fork()) < 0) { + close(fd[0]); + close(fd[1]); + return -1; + } + + /* child */ + if (pid == 0) + slbt_util_dump_machine_child( + program, + fd); + + /* parent */ + close(fd[1]); + + mark = machine; + + for (; buflen; ) { + ret = read(fd[0],mark,buflen); + + while ((ret < 0) && (errno == EINTR)) + ret = read(fd[0],mark,buflen); + + if (ret > 0) { + buflen -= ret; + mark += ret; + + } else if (ret == 0) { + close(fd[0]); + buflen = 0; + + } else { + close(fd[0]); + return -1; + } + } + + /* execve verification */ + rpid = waitpid( + pid, + &code, + 0); + + if ((rpid != pid) || code) { + errno = ESTALE; + return -1; + } + + /* newline verification */ + if ((mark == machine) || (*--mark != '\n')) { + errno = ERANGE; + return -1; + } + + *mark = 0; + + /* portbld <--> unknown synonym? */ + if ((mark = strstr(machine,"-portbld-"))) + memcpy(mark,"-unknown",8); + + /* all done */ + return 0; +} diff --git a/src/util/slbt_map_input.c b/src/util/slbt_map_input.c new file mode 100644 index 0000000..ca0b06b --- /dev/null +++ b/src/util/slbt_map_input.c @@ -0,0 +1,67 @@ +/*******************************************************************/ +/* slibtool: a skinny libtool implementation, written in C */ +/* Copyright (C) 2015--2024 SysDeer Technologies, LLC */ +/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */ +/*******************************************************************/ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include "slibtool_driver_impl.h" +#include "slibtool_errinfo_impl.h" + +int slbt_map_input( + const struct slbt_driver_ctx * dctx, + int fd, + const char * path, + int prot, + struct slbt_input * map) +{ + int ret; + struct stat st; + bool fnew; + int fdcwd; + + fdcwd = slbt_driver_fdcwd(dctx); + + if ((fnew = (fd < 0))) + fd = openat(fdcwd,path,O_RDONLY | O_CLOEXEC); + + if (fd < 0) + return SLBT_SYSTEM_ERROR(dctx,path); + + if ((ret = fstat(fd,&st) < 0) && fnew) + close(fd); + + else if ((st.st_size == 0) && fnew) + close(fd); + + if (ret < 0) + return SLBT_SYSTEM_ERROR(dctx,path); + + if (st.st_size == 0) { + map->size = 0; + map->addr = 0; + } else { + map->size = st.st_size; + map->addr = mmap(0,map->size,prot,MAP_PRIVATE,fd,0); + } + + if (fnew) + close(fd); + + return (map->addr == MAP_FAILED) + ? SLBT_SYSTEM_ERROR(dctx,path) + : 0; +} + +int slbt_unmap_input(struct slbt_input * map) +{ + return map->size ? munmap(map->addr,map->size) : 0; +} diff --git a/src/util/slbt_realpath.c b/src/util/slbt_realpath.c new file mode 100644 index 0000000..3929e04 --- /dev/null +++ b/src/util/slbt_realpath.c @@ -0,0 +1,101 @@ +/*******************************************************************/ +/* slibtool: a skinny libtool implementation, written in C */ +/* Copyright (C) 2016--2024 SysDeer Technologies, LLC */ +/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */ +/*******************************************************************/ + +#include +#include +#include +#include +#include +#include + +#include "slibtool_driver_impl.h" +#include "slibtool_readlink_impl.h" + +#ifdef HAVE_SYS_SYSCALL_H +#include +#endif + +#ifdef _MIDIPIX_ABI +#include +#endif + +#ifndef ENOTSUP +#define ENOTSUP EOPNOTSUPP +#endif + +#ifdef SYS___realpathat +extern long syscall(int, ...); +#endif + +int slbt_util_realpath( + int fdat, + const char * path, + int options, + char * buf, + size_t buflen) +{ + int ret; + int fd; + int fdproc; + struct stat st; + struct stat stproc; + char procfspath[36]; + + /* common validation */ + if (!buf || (options & O_CREAT)) { + errno = EINVAL; + return -1; + } + + /* framework-based wrapper */ +#ifdef _MIDIPIX_ABI + return __fs_rpath(fdat,path,options,buf,buflen); +#endif + +#ifdef SYS___realpathat + return syscall(SYS___realpathat,fdat,path,buf,buflen,0); +#endif + + /* buflen */ + if (buflen < PATH_MAX) { + errno = ENOBUFS; + return -1; + } + + /* AT_FDCWD */ + if (fdat == AT_FDCWD) { + return realpath(path,buf) ? 0 : -1; + } + + /* /proc/self/fd */ + if ((fd = openat(fdat,path,options,0)) < 0) + return -1; + + sprintf(procfspath,"/proc/self/fd/%d",fd); + + if (slbt_readlinkat(fdat,procfspath,buf,buflen)) { + close(fd); + return -1; + } + + if ((fdproc = openat(AT_FDCWD,buf,options|O_NOFOLLOW,0)) < 0) { + close(fd); + errno = ELOOP; + return -1; + } + + ret = fstat(fd,&st) || fstat(fdproc,&stproc); + + close(fd); + close(fdproc); + + if (ret || (st.st_dev != stproc.st_dev) || (st.st_ino != stproc.st_ino)) { + errno = ENOTSUP; + return -1; + } + + return 0; +} -- cgit v1.2.3