Introduce posix_spawn_file_actions_addchdir_np()

This commit is contained in:
Justine Tunney 2023-10-11 20:26:28 -07:00
parent f92ad74e6b
commit 3a1f887928
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
25 changed files with 446 additions and 748 deletions

View file

@ -54,6 +54,35 @@ textwindows static const char *FixNtMagicPath(const char *path,
return path;
}
textwindows size_t __normntpath(char16_t *p, size_t n) {
size_t i, j;
for (j = i = 0; i < n; ++i) {
int c = p[i];
if (c == '/') {
c = '\\';
}
if (j > 1 && c == '\\' && p[j - 1] == '\\') {
// matched "^/" or "//" but not "^//"
} else if ((j && p[j - 1] == '\\') && //
c == '.' && //
(i + 1 == n || IsSlash(p[i + 1]))) {
// matched "/./" or "/.$"
i += !(i + 1 == n);
} else if ((j && p[j - 1] == '\\') && //
c == '.' && //
(i + 1 < n && p[i + 1] == '.') && //
(i + 2 == n || IsSlash(p[i + 2]))) {
// matched "/../" or "/..$"
while (j && p[j - 1] == '\\') --j;
while (j && p[j - 1] != '\\') --j;
} else {
p[j++] = c;
}
}
p[j] = 0;
return j;
}
textwindows int __mkntpath(const char *path,
char16_t path16[hasatleast PATH_MAX]) {
return __mkntpath2(path, path16, -1);
@ -78,7 +107,6 @@ textwindows int __mkntpath(const char *path,
*/
textwindows int __mkntpath2(const char *path,
char16_t path16[hasatleast PATH_MAX], int flags) {
// 1. Need +1 for NUL-terminator
// 2. Need +1 for UTF-16 overflow
// 3. Need ≥2 for SetCurrentDirectory trailing slash requirement
@ -165,32 +193,7 @@ textwindows int __mkntpath2(const char *path,
// normalize path
// we need it because \\?\... paths have to be normalized
// we don't remove the trailing slash since it is special
size_t i, j;
for (j = i = 0; i < n; ++i) {
int c = p[i];
if (c == '/') {
c = '\\';
}
if (j > 1 && c == '\\' && p[j - 1] == '\\') {
// matched "^/" or "//" but not "^//"
} else if ((j && p[j - 1] == '\\') && //
c == '.' && //
(i + 1 == n || IsSlash(p[i + 1]))) {
// matched "/./" or "/.$"
i += !(i + 1 == n);
} else if ((j && p[j - 1] == '\\') && //
c == '.' && //
(i + 1 < n && p[i + 1] == '.') && //
(i + 2 == n || IsSlash(p[i + 2]))) {
// matched "/../" or "/..$"
while (j && p[j - 1] == '\\') --j;
while (j && p[j - 1] != '\\') --j;
} else {
p[j++] = c;
}
}
p[j] = 0;
n = j;
n = __normntpath(p, n);
// our path is now stored at `path16` with length `n`
n = x + m + n;

View file

@ -27,37 +27,35 @@
#include "libc/sysv/consts/at.h"
#include "libc/sysv/errfuns.h"
static int __mkntpathat_impl(int dirfd, const char *path, int flags,
char16_t file[hasatleast PATH_MAX]) {
static textwindows int __mkntpathath_impl(int64_t dirhand, const char *path,
int flags,
char16_t file[hasatleast PATH_MAX]) {
size_t n;
char16_t dir[PATH_MAX];
uint32_t dirlen, filelen;
if (!isutf8(path, -1)) return eilseq(); // thwart overlong nul in conversion
if ((filelen = __mkntpath2(path, file, flags)) == -1) return -1;
if (!filelen) return enoent();
if (file[0] != u'\\' && dirfd != AT_FDCWD) { // ProTip: \\?\C:\foo
if (!__isfdkind(dirfd, kFdFile)) return ebadf();
dirlen = GetFinalPathNameByHandle(g_fds.p[dirfd].handle, dir, ARRAYLEN(dir),
if (file[0] != u'\\' && dirhand != AT_FDCWD) { // ProTip: \\?\C:\foo
dirlen = GetFinalPathNameByHandle(dirhand, dir, ARRAYLEN(dir),
kNtFileNameNormalized | kNtVolumeNameDos);
if (!dirlen) return __winerr();
if (dirlen + 1 + filelen + 1 > ARRAYLEN(dir)) {
STRACE("path too long: %#.*hs\\%#.*hs", dirlen, dir, filelen, file);
return enametoolong();
}
if (dirlen + 1 + filelen + 1 > ARRAYLEN(dir)) return enametoolong();
dir[dirlen] = u'\\';
memcpy(dir + dirlen + 1, file, (filelen + 1) * sizeof(char16_t));
memcpy(file, dir, (dirlen + 1 + filelen + 1) * sizeof(char16_t));
return dirlen + 1 + filelen;
memcpy(file, dir, ((n = dirlen + 1 + filelen) + 1) * sizeof(char16_t));
return __normntpath(file, n);
} else {
return filelen;
}
}
int __mkntpathat(int dirfd, const char *path, int flags,
char16_t file[hasatleast PATH_MAX]) {
textwindows int __mkntpathath(int64_t dirhand, const char *path, int flags,
char16_t file[hasatleast PATH_MAX]) {
// convert the path.
int len;
if ((len = __mkntpathat_impl(dirfd, path, flags, file)) == -1) {
if ((len = __mkntpathath_impl(dirhand, path, flags, file)) == -1) {
return -1;
}
@ -78,3 +76,16 @@ int __mkntpathat(int dirfd, const char *path, int flags,
return len;
}
textwindows int __mkntpathat(int dirfd, const char *path, int flags,
char16_t file[hasatleast PATH_MAX]) {
int64_t dirhand;
if (dirfd == AT_FDCWD) {
dirhand = AT_FDCWD;
} else if (__isfdkind(dirfd, kFdFile)) {
dirhand = g_fds.p[dirfd].handle;
} else {
return ebadf();
}
return __mkntpathath(dirhand, path, flags, file);
}

View file

@ -73,7 +73,7 @@ static void ntspawn_free(void *ptr) {
* @asyncsignalsafe
*/
textwindows int ntspawn(
const char *prog, char *const argv[], char *const envp[],
int64_t dirhand, const char *prog, char *const argv[], char *const envp[],
char *const extravars[], uint32_t dwCreationFlags,
const char16_t *opt_lpCurrentDirectory, int64_t opt_hParentProcess,
int64_t *opt_lpExplicitHandleList, uint32_t dwExplicitHandleCount,
@ -82,7 +82,8 @@ textwindows int ntspawn(
int rc = -1;
struct SpawnBlock *sb;
BLOCK_SIGNALS;
if ((sb = ntspawn_malloc(sizeof(*sb))) && __mkntpath(prog, sb->path) != -1) {
if ((sb = ntspawn_malloc(sizeof(*sb))) &&
__mkntpathath(dirhand, prog, 0, sb->path) != -1) {
if (!mkntcmdline(sb->cmdline, argv) &&
!mkntenvblock(sb->envblock, envp, extravars, sb->envbuf)) {
bool32 ok;
@ -133,6 +134,8 @@ textwindows int ntspawn(
STRACE("CreateProcess() failed w/ %d", GetLastError());
if (GetLastError() == kNtErrorSharingViolation) {
etxtbsy();
} else if (GetLastError() == kNtErrorInvalidName) {
enoent();
}
}
rc = __fix_enotdir(rc, sb->path);

View file

@ -24,6 +24,7 @@
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/promises.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/nexgen32e/vendor.internal.h"
@ -267,11 +268,13 @@ int pledge(const char *promises, const char *execpromises) {
// if bits are missing in execpromises that exist in promises
// then execpromises wouldn't be a monotonic access reduction
// this check only matters when exec / execnative are allowed
if ((ipromises & ~iexecpromises) &&
(~ipromises & (1ul << PROMISE_EXEC))) {
bool notsubset = ((ipromises & ~iexecpromises) &&
(~ipromises & (1ul << PROMISE_EXEC)));
if (notsubset && execpromises) {
STRACE("execpromises must be a subset of promises");
rc = einval();
} else {
if (notsubset) iexecpromises = ipromises;
rc = sys_pledge_linux(ipromises, __pledge_mode);
if (rc > -4096u) errno = -rc, rc = -1;
}

View file

@ -10,9 +10,11 @@ bool isregularfile_nt(const char *);
bool issymlink_nt(const char *);
bool32 ntsetprivilege(int64_t, const char16_t *, uint32_t);
char16_t *__create_pipe_name(char16_t *);
size_t __normntpath(char16_t *, size_t);
int __mkntpath(const char *, char16_t[hasatleast PATH_MAX]);
int __mkntpath2(const char *, char16_t[hasatleast PATH_MAX], int);
int __mkntpathat(int, const char *, int, char16_t[hasatleast PATH_MAX]);
int __mkntpathath(int64_t, const char *, int, char16_t[hasatleast PATH_MAX]);
int ntaccesscheck(const char16_t *, uint32_t) paramsnonnull();
int sys_pause_nt(void);
int64_t __fix_enotdir(int64_t, char16_t *);