summaryrefslogtreecommitdiff
path: root/src/libenvexec/env_dump.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2020-11-24 21:45:56 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2020-11-24 21:45:56 +0000
commit18e43565574b700befc832ed4d25d25e40951f68 (patch)
tree2c97774819e99132b10dc60403f43e2034e395f9 /src/libenvexec/env_dump.c
parent265092c55d40f362a521eee97676e0d51ef17800 (diff)
downloadskalibs-18e43565574b700befc832ed4d25d25e40951f68.tar.xz
Complete revamp of the pathexec functions
- pathexec_run is now called exec_ae a for provided file name (default: argv[0]) e for provided envp (default: environ) - pathexec is now called mexec. m for merge environment. Option letters are: a for provided file name (default: argv[0]) e for provided envp (default: environ) f for provided envp *and* length of the envp m for provided modif string plus its length (the length is always needed because the modifs are null-terminated) n for provided modif string, length *and* number of modifs - functions have a foo0 version for _exit(0) when argv[0] is null - functions have a xfoo version to die if the exec fails - and a xfoo0 - Compatibility #defines and #includes are there until the next major bump
Diffstat (limited to 'src/libenvexec/env_dump.c')
-rw-r--r--src/libenvexec/env_dump.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/libenvexec/env_dump.c b/src/libenvexec/env_dump.c
new file mode 100644
index 0000000..10f5913
--- /dev/null
+++ b/src/libenvexec/env_dump.c
@@ -0,0 +1,54 @@
+/* ISC license. */
+
+#include <skalibs/nonposix.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <skalibs/bytestr.h>
+#include <skalibs/env.h>
+#include <skalibs/djbunix.h>
+
+ /* XXX: breaks layering, but really openat() should be supported everywhere */
+#include <skalibs/unix-transactional.h>
+
+#define SUFFIX ":envdump:XXXXXX"
+
+int env_dump (char const *dir, mode_t mode, char const *const *envp)
+{
+ int fd ;
+ size_t dirlen = strlen(dir) ;
+ char tmpdir[dirlen + sizeof(SUFFIX)] ;
+ memcpy(tmpdir, dir, dirlen) ;
+ memcpy(tmpdir + dirlen, SUFFIX, sizeof(SUFFIX)) ;
+ if (!mkdtemp(tmpdir)) return 0 ;
+ fd = open_read(tmpdir) ;
+ if (fd == -1) goto err ;
+ for (; *envp ; envp++)
+ {
+ size_t len = str_chr(*envp, '=') ;
+ size_t vallen = strlen(*envp + len + 1) ;
+ char fn[len + 1] ;
+ memcpy(fn, *envp, len) ;
+ fn[len] = 0 ;
+ len = openwritenclose_at(fd, fn, *envp + len + 1, vallen) ;
+ if (len < vallen) goto cerr ;
+ }
+ fd_close(fd) ;
+ if (chmod(tmpdir, mode) == -1) goto err ;
+ if (rename(tmpdir, dir) == -1) goto err ;
+ return 1 ;
+
+ cerr:
+ fd_close(fd) ;
+ err:
+ {
+ int e = errno ;
+ rm_rf(tmpdir) ;
+ errno = e ;
+ }
+ return 0 ;
+}