Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,41 @@ An implementation of [openkal](https://github.com/mcpplibs/openkal) for Windows.

```toml
[dependencies]
openkal = "0.26.0"
openkal = "0.26.1"

[target.'cfg(windows)'.dependencies]
openkal-windows = "0.23.1"
openkal-windows = "0.23.2"
```

Its purpose is as much to test the specification as to be used. openkal was
drawn against systems that share an ancestry; a third implementation on a system
that shares none of it is what turns "this interface is not Unix's shape" from a
claim into an observation.

## What 0.23.2 changes

**A started program is named as its starter spelled its directories** (mcpp#1032).
A start named the image's directory and the working directory by the final paths
the system reports for their handles, which are physical: under a substituted
drive a program started through `kal_process_spawn` read its module's name and its
working directory physically, while the same program started directly read them
under the drive its starter used, and an environment that authorises by spelling
refused it. A directory is now named, in order, by the name it was preopened under;
within the working directory, by the working directory's name and the rest of the
path; and otherwise by the final path, as before. A name from the first two is used
only when it opens the same object (volume serial number and file index), so a
directory renamed since it was preopened is named by its final path. No name is
kept for a handle (clause 7.1). `tests/a_started_program_is_named_as_its_starter_spelled_it.cpp`
substitutes a drive and observes both directories.

**A start that states no preference leaves the environment as it was given**
(openkal 0.26.1, clause 7.17). A start removed every `KAL_RESOURCES` variable the
caller gave and wrote this process's own preference where it was not balanced; the
variable is now written only for a start that states a preference. The low memory
priority a starter sets for economy follows the same preference: the one the given
environment carries, so a caller that composes an environment naming economy holds
a program that does not read the preference to it.

## What 0.23.1 corrects

A program for whose arguments and variables there is no memory, or whose command
Expand Down
2 changes: 1 addition & 1 deletion mcpp.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
namespace = "mcpplibs"
name = "openkal-windows"
version = "0.23.1"
version = "0.23.2"
description = "An implementation of openkal for Windows, written on the Win32 interfaces and the object manager beneath them, using no C runtime symbol."
license = "Apache-2.0"

Expand Down
14 changes: 14 additions & 0 deletions src/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,20 @@ void listing_give(listing* s, bool ended) {

} // namespace

namespace okw {
// The two words of a node's identity (the volume's serial number and the file's
// index on it), by the enquiry `fill' makes: what a start compares to know that
// a name it gives opens the directory a handle is (process.cpp, `given_name').
bool node_identity(void* h, kal_u64 out[2]) {
kal_node_info info{};
info.self_size = sizeof info;
if (fill(h, KAL_INFO_IDENTITY, &info) != kal_ok || !(info.present & KAL_INFO_IDENTITY)) return false;
out[0] = info.identity[0];
out[1] = info.identity[1];
return true;
}
} // namespace okw

extern "C" {

kal_uintptr kal_fs_preopen_count(void) { kal_uintptr n = 0; table(&n); return n; }
Expand Down
159 changes: 139 additions & 20 deletions src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <openkal/process.h>
#include <openkal/memory.h>
#include <openkal/random.h>
#include <openkal/fs.h>

namespace okw { bool node_identity(void* h, kal_u64 out[2]); } // fs.cpp

// A program image that has been started.
//
Expand Down Expand Up @@ -55,6 +58,102 @@ okw_uptr plain_name(wchar_t* s, okw_uptr n) {
return n - drop;
}

// THE NAME A DIRECTORY IS GIVEN AT A START (0.23.2, mcpp#1032).
//
// The final path the system reports for a handle is the physical one. Under a
// substituted drive a started program read its own module path and its working
// directory physically, while the same program started directly read them as
// its starter spelled them, and an environment that authorises by spelling ---
// a sandbox that admits one drive letter --- refused the compiler a build tool
// started. A name kept for every handle is the resolver clause 7.1 excludes, so
// a directory is named from what this process was given, in three steps:
//
// 1. a preopen, by the name it was preopened under;
// 2. a directory within this process's working directory (whose final path
// begins the directory's at a separator), by the working directory's name
// and the rest of the final path --- the name a program here reaches it by
// from where it was started;
// 3. otherwise the final path, as before.
//
// A name from the first two is used only when it opens the same object (the
// volume's serial number and the file's index); a directory renamed or moved
// since it was preopened is named by step 3.
okw_uptr utf8_to_wide(const char* s, okw_uptr len, wchar_t* out, okw_uptr cap) {
if (len == 0) return 0;
const int produced = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s, static_cast<int>(len),
out, static_cast<int>(cap - 1));
if (produced <= 0) return 0;
for (int i = 0; i < produced; ++i) if (out[i] == L'/') out[i] = L'\\';
okw_uptr n = static_cast<okw_uptr>(produced);
// A volume's preopen is named `X:', which as a path is the drive's current
// directory; the volume itself is `X:\'.
if (n == 2 && out[1] == L':' && n + 1 < cap) out[n++] = L'\\';
out[n] = 0;
return n;
}

bool same_directory(HANDLE dir, const wchar_t* name) {
kal_u64 a[2], b[2];
if (!okw::node_identity(dir, a)) return false;
HANDLE h = CreateFileW(name, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
if (h == INVALID_HANDLE_VALUE) return false;
const bool same = okw::node_identity(h, b) && a[0] == b[0] && a[1] == b[1];
CloseHandle(h);
return same;
}

wchar_t lower(wchar_t c) { return (c >= L'A' && c <= L'Z') ? static_cast<wchar_t>(c + 32) : c; }

// `out' holds kMaxName characters. Returns the length written, 0 on failure.
okw_uptr given_name(HANDLE dir, wchar_t* out) {
// Step 1: a preopen, by its own name.
for (kal_uintptr i = 0; i < kal_fs_preopen_count(); ++i) {
kal_dir d{}; char name[1024]; kal_uintptr len = 0;
if (kal_fs_preopen(i, &d, name, sizeof name, &len) != kal_ok || len == 0 || len >= sizeof name) continue;
if (okw::unpack(d.h) != dir) continue;
const okw_uptr n = utf8_to_wide(name, len, out, okw::kMaxName);
if (n && same_directory(dir, out)) return n;
break;
}
// Step 3's name, which step 2 also reads.
const DWORD n = GetFinalPathNameByHandleW(dir, out, okw::kMaxName - 1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
if (n == 0 || n >= okw::kMaxName - 1) return 0;
out[n] = 0;
okw_uptr final_len = plain_name(out, n);
// Step 2: within the working directory, by its name and the rest.
kal_dir work{}; char wname[1024]; kal_uintptr wlen = 0;
if (kal_fs_preopen(0, &work, wname, sizeof wname, &wlen) == kal_ok && wlen > 0 && wlen < sizeof wname) {
HANDLE wh = okw::unpack(work.h);
// On the stack, as every name buffer of this implementation: a
// thread-local one is emulated TLS under mingw (__emutls_get_address),
// which the implementation must not reference.
wchar_t wfinal[okw::kMaxName];
wchar_t candidate[okw::kMaxName];
const DWORD wn = wh ? GetFinalPathNameByHandleW(wh, wfinal, okw::kMaxName - 1,
FILE_NAME_NORMALIZED | VOLUME_NAME_DOS) : 0;
if (wn != 0 && wn < okw::kMaxName - 1) {
wfinal[wn] = 0;
const okw_uptr wf = plain_name(wfinal, wn);
bool within = wf > 0 && wf < final_len && (out[wf] == L'\\' || wfinal[wf - 1] == L'\\');
for (okw_uptr k = 0; within && k < wf; ++k) within = lower(out[k]) == lower(wfinal[k]);
const okw_uptr base = within ? utf8_to_wide(wname, wlen, candidate, okw::kMaxName) : 0;
okw_uptr at = base;
if (at && candidate[at - 1] != L'\\' && out[wf] != L'\\') candidate[at++] = L'\\';
if (at && candidate[at - 1] == L'\\' && out[wf] == L'\\') --at;
for (okw_uptr k = wf; at && k < final_len && at + 1 < okw::kMaxName; ++k) candidate[at++] = out[k];
if (at && at + 1 < okw::kMaxName) {
candidate[at] = 0;
if (same_directory(dir, candidate)) {
for (okw_uptr k = 0; k <= at; ++k) out[k] = candidate[k];
return at;
}
}
}
}
return final_len;
}

// A SPAWN INHERITS ONLY THE HANDLES IT PLACED. Version 0.13.
//
// `CreateProcessW' with inheritance enabled gives the started program EVERY
Expand Down Expand Up @@ -420,9 +519,36 @@ bool named(const wchar_t* s, okw_uptr n, const char* key) {
}
return k < n && s[k] == L'=';
}
// The preference of a start that states none: the one the environment it is
// given carries (openkal 0.26.1, clause 7.17) --- the caller's own where the
// caller passes its environment on, else what the composed one names, the
// default where it names none. Read as env.cpp reads KAL_RESOURCES.
int carried_preference(const char** envp, const kal_uintptr* lens, kal_uintptr envc) {
if (envc == 0) return kal_env_resources();
constexpr okw_uptr kName = sizeof "KAL_RESOURCES=" - 1;
auto is = [](const char* v, kal_uintptr n, const char* word) {
kal_uintptr k = 0;
for (; word[k]; ++k) if (k >= n || v[k] != word[k]) return false;
return k == n;
};
for (kal_uintptr i = 0; i < envc; ++i) {
if (!named(envp[i], lens[i], "KAL_RESOURCES")) continue;
const char* v = envp[i] + kName;
const kal_uintptr n = lens[i] - kName;
return is(v, n, "performance") ? KAL_RESOURCES_PERFORMANCE
: is(v, n, "economy") ? KAL_RESOURCES_ECONOMY
: KAL_RESOURCES_BALANCED;
}
return KAL_RESOURCES_BALANCED;
}
// KAL_RESOURCES is this start's only where the start states a preference; one
// that states none leaves the caller's variable where it is, so the preference
// is inherited through the environment the caller gives (openkal 0.26.1, clause
// 7.17) and a program that is not an openkal program receives that environment.
template <class C>
bool conveyed_here(const C* s, okw_uptr n) {
return named(s, n, "KAL_PREOPENS") || named(s, n, "KAL_GRANT_RIGHTS") || named(s, n, "KAL_RESOURCES");
bool conveyed_here(const C* s, okw_uptr n, bool resources) {
return named(s, n, "KAL_PREOPENS") || named(s, n, "KAL_GRANT_RIGHTS")
|| (resources && named(s, n, "KAL_RESOURCES"));
}

char* put_decimal(char* o, unsigned long long v) {
Expand Down Expand Up @@ -549,12 +675,12 @@ struct environment {
bool ok = true;
if (envc > 0) {
for (kal_uintptr i = 0; i < envc && ok; ++i)
if (!conveyed_here(envp[i], lens[i])) ok = put_utf8(envp[i], lens[i]);
if (!conveyed_here(envp[i], lens[i], resources != nullptr)) ok = put_utf8(envp[i], lens[i]);
} else if (own) {
for (const wchar_t* p = own; *p;) {
okw_uptr n = 0;
while (p[n]) ++n;
if (!conveyed_here(p, n)) { for (okw_uptr k = 0; k <= n; ++k) w[at++] = p[k]; }
if (!conveyed_here(p, n, resources != nullptr)) { for (okw_uptr k = 0; k <= n; ++k) w[at++] = p[k]; }
p += n + 1;
}
FreeEnvironmentStringsW(own);
Expand Down Expand Up @@ -701,27 +827,21 @@ int kal_process_spawn(const kal_spawn* how,
~releaser() { if (p) kal_free(p, sizeof(scratch), alignof(scratch)); }
} guard{ work };
wchar_t* image = work->image;
const DWORD n = GetFinalPathNameByHandleW(dir, image, okw::kMaxName - 2,
FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
if (n == 0 || n >= okw::kMaxName - 2) return okw::translate_win32(GetLastError());
const okw_uptr n = given_name(dir, image);
if (n == 0 || n >= okw::kMaxName - 2) return n == 0 ? okw::translate_win32(GetLastError()) : kal_err_invalid;
okw_uptr at = n;
image[at++] = L'\\';
if (image[at - 1] != L'\\') image[at++] = L'\\';
okw::wide_name relative(path, path_len);
if (!relative.ok) return kal_err_invalid;
for (okw_uptr i = 0; i < relative.string.length / 2u; ++i) {
if (at + 2 >= okw::kMaxName) return kal_err_invalid;
image[at++] = relative.buffer[i];
}
image[at] = 0;
(void)plain_name(image, at);

// The same enquiry the image path comes from, upon the other directory.
// The same naming the image's directory has, upon the other directory.
wchar_t* cwd = work->cwd;
const DWORD cn = GetFinalPathNameByHandleW(run, cwd, okw::kMaxName - 1,
FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
if (cn == 0 || cn >= okw::kMaxName - 1) return okw::translate_win32(GetLastError());
cwd[cn] = 0;
(void)plain_name(cwd, cn);
if (given_name(run, cwd) == 0) return okw::translate_win32(GetLastError());

// The vector, unaltered, including its first element.
wchar_t* line = work->line;
Expand Down Expand Up @@ -754,14 +874,13 @@ int kal_process_spawn(const kal_spawn* how,
// they carry, the preference), and then the block is the caller's, less
// any value of those names, and this start's. Made inside the start's
// window below, because the grants' handles are made there.
const int own_preference = kal_env_resources();
const int asked_preference = preference == KAL_SPAWN_RESOURCES_PERFORMANCE ? KAL_RESOURCES_PERFORMANCE
: preference == KAL_SPAWN_RESOURCES_ECONOMY ? KAL_RESOURCES_ECONOMY
: preference == KAL_SPAWN_RESOURCES_BALANCED ? KAL_RESOURCES_BALANCED
: own_preference;
const char* resources = asked_preference == KAL_RESOURCES_PERFORMANCE ? "performance"
: asked_preference == KAL_RESOURCES_ECONOMY ? "economy"
: preference != 0 ? "balanced" : nullptr;
: carried_preference(envp, envp_lens, envc);
const char* resources = preference == KAL_SPAWN_RESOURCES_PERFORMANCE ? "performance"
: preference == KAL_SPAWN_RESOURCES_ECONOMY ? "economy"
: preference == KAL_SPAWN_RESOURCES_BALANCED ? "balanced" : nullptr;

// THE NULL DEVICE (openkal 0.22): this system's `NUL', opened for this
// start and released after it; the started program holds its own handle.
Expand Down
Loading
Loading