@cotal-ai/workspace 0.100.2 → 0.101.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/Release/darwin-arm64/build.json +21 -0
- package/build/Release/darwin-arm64/process.node +0 -0
- package/build/Release/darwin-x64/build.json +21 -0
- package/build/Release/darwin-x64/process.node +0 -0
- package/build/Release/win32-arm64/build.json +7 -5
- package/build/Release/win32-arm64/creation.node +0 -0
- package/build/Release/win32-x64/build.json +7 -5
- package/build/Release/win32-x64/creation.node +0 -0
- package/dist/agent-grants.d.ts +21 -0
- package/dist/agent-grants.d.ts.map +1 -0
- package/dist/agent-grants.js +21 -0
- package/dist/agent-grants.js.map +1 -0
- package/dist/auth-paths.d.ts +34 -0
- package/dist/auth-paths.d.ts.map +1 -1
- package/dist/auth-paths.js +99 -3
- package/dist/auth-paths.js.map +1 -1
- package/dist/broker-display.d.ts +12 -0
- package/dist/broker-display.d.ts.map +1 -1
- package/dist/broker-display.js +14 -0
- package/dist/broker-display.js.map +1 -1
- package/dist/connect.d.ts +21 -7
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +152 -22
- package/dist/connect.js.map +1 -1
- package/dist/control-target.d.ts.map +1 -1
- package/dist/control-target.js +12 -10
- package/dist/control-target.js.map +1 -1
- package/dist/cotal-config.d.ts +7 -0
- package/dist/cotal-config.d.ts.map +1 -1
- package/dist/cotal-config.js +29 -2
- package/dist/cotal-config.js.map +1 -1
- package/dist/cotal-home.d.ts +23 -0
- package/dist/cotal-home.d.ts.map +1 -0
- package/dist/cotal-home.js +36 -0
- package/dist/cotal-home.js.map +1 -0
- package/dist/credential-actions.d.ts +17 -3
- package/dist/credential-actions.d.ts.map +1 -1
- package/dist/credential-actions.js +62 -10
- package/dist/credential-actions.js.map +1 -1
- package/dist/darwin-process.d.ts +20 -0
- package/dist/darwin-process.d.ts.map +1 -0
- package/dist/darwin-process.js +24 -0
- package/dist/darwin-process.js.map +1 -0
- package/dist/deprecated-daemon-entries.d.ts +3 -0
- package/dist/deprecated-daemon-entries.d.ts.map +1 -1
- package/dist/deprecated-daemon-entries.js +6 -1
- package/dist/deprecated-daemon-entries.js.map +1 -1
- package/dist/extensions.d.ts +4 -2
- package/dist/extensions.d.ts.map +1 -1
- package/dist/extensions.js +7 -2
- package/dist/extensions.js.map +1 -1
- package/dist/flags.d.ts +2 -0
- package/dist/flags.d.ts.map +1 -1
- package/dist/flags.js +1 -1
- package/dist/flags.js.map +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -1
- package/dist/index.js.map +1 -1
- package/dist/join-target.d.ts +3 -1
- package/dist/join-target.d.ts.map +1 -1
- package/dist/join-target.js +15 -3
- package/dist/join-target.js.map +1 -1
- package/dist/maintenance.d.ts +61 -2
- package/dist/maintenance.d.ts.map +1 -1
- package/dist/maintenance.js +153 -14
- package/dist/maintenance.js.map +1 -1
- package/dist/manager-name.d.ts +15 -0
- package/dist/manager-name.d.ts.map +1 -0
- package/dist/manager-name.js +54 -0
- package/dist/manager-name.js.map +1 -0
- package/dist/mesh-registry.d.ts +39 -35
- package/dist/mesh-registry.d.ts.map +1 -1
- package/dist/mesh-registry.js +57 -36
- package/dist/mesh-registry.js.map +1 -1
- package/dist/mesh-target.d.ts +120 -3
- package/dist/mesh-target.d.ts.map +1 -1
- package/dist/mesh-target.js +319 -48
- package/dist/mesh-target.js.map +1 -1
- package/dist/official-connectors.d.ts +3 -0
- package/dist/official-connectors.d.ts.map +1 -1
- package/dist/official-connectors.js +5 -0
- package/dist/official-connectors.js.map +1 -1
- package/dist/output.d.ts +34 -6
- package/dist/output.d.ts.map +1 -1
- package/dist/output.js +38 -5
- package/dist/output.js.map +1 -1
- package/dist/pid.d.ts +42 -7
- package/dist/pid.d.ts.map +1 -1
- package/dist/pid.js +196 -27
- package/dist/pid.js.map +1 -1
- package/dist/preferences.d.ts +25 -3
- package/dist/preferences.d.ts.map +1 -1
- package/dist/preferences.js +77 -14
- package/dist/preferences.js.map +1 -1
- package/dist/preflight.d.ts +17 -5
- package/dist/preflight.d.ts.map +1 -1
- package/dist/preflight.js +22 -32
- package/dist/preflight.js.map +1 -1
- package/dist/prepared-spaces.d.ts +20 -2
- package/dist/prepared-spaces.d.ts.map +1 -1
- package/dist/prepared-spaces.js +90 -15
- package/dist/prepared-spaces.js.map +1 -1
- package/dist/present.d.ts +2 -12
- package/dist/present.d.ts.map +1 -1
- package/dist/present.js +24 -40
- package/dist/present.js.map +1 -1
- package/dist/protected-records.d.ts +22 -3
- package/dist/protected-records.d.ts.map +1 -1
- package/dist/protected-records.js +198 -27
- package/dist/protected-records.js.map +1 -1
- package/dist/registration-policy.d.ts +2 -1
- package/dist/registration-policy.d.ts.map +1 -1
- package/dist/render.d.ts +2 -1
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +42 -11
- package/dist/render.js.map +1 -1
- package/dist/renewal.d.ts +4 -0
- package/dist/renewal.d.ts.map +1 -1
- package/dist/renewal.js +20 -1
- package/dist/renewal.js.map +1 -1
- package/dist/service-adapter.d.ts +1 -2
- package/dist/service-adapter.d.ts.map +1 -1
- package/dist/service-adapter.js +9 -4
- package/dist/service-adapter.js.map +1 -1
- package/dist/service-custody.js +2 -2
- package/dist/service-custody.js.map +1 -1
- package/dist/service-definition.d.ts +3 -0
- package/dist/service-definition.d.ts.map +1 -1
- package/dist/service-definition.js +56 -2
- package/dist/service-definition.js.map +1 -1
- package/dist/service-definition.schema.json +2 -1
- package/dist/service-identity.js +4 -4
- package/dist/service-identity.js.map +1 -1
- package/dist/service-stop.js +3 -3
- package/dist/service-stop.js.map +1 -1
- package/dist/service-target.d.ts.map +1 -1
- package/dist/service-target.js +9 -2
- package/dist/service-target.js.map +1 -1
- package/dist/signer-export.d.ts +1 -0
- package/dist/signer-export.d.ts.map +1 -1
- package/dist/signer-export.js +20 -4
- package/dist/signer-export.js.map +1 -1
- package/dist/space-streams.d.ts +18 -0
- package/dist/space-streams.d.ts.map +1 -0
- package/dist/space-streams.js +41 -0
- package/dist/space-streams.js.map +1 -0
- package/dist/space.d.ts +5 -0
- package/dist/space.d.ts.map +1 -1
- package/dist/space.js +5 -0
- package/dist/space.js.map +1 -1
- package/dist/store-entries.d.ts +6 -0
- package/dist/store-entries.d.ts.map +1 -0
- package/dist/store-entries.js +17 -0
- package/dist/store-entries.js.map +1 -0
- package/dist/system-rotation.d.ts +3 -1
- package/dist/system-rotation.d.ts.map +1 -1
- package/dist/system-rotation.js +19 -4
- package/dist/system-rotation.js.map +1 -1
- package/dist/text-wrap.d.ts +18 -0
- package/dist/text-wrap.d.ts.map +1 -0
- package/dist/text-wrap.js +60 -0
- package/dist/text-wrap.js.map +1 -0
- package/dist/win32-custodian.d.ts +2 -0
- package/dist/win32-custodian.d.ts.map +1 -0
- package/dist/win32-custodian.js +85 -0
- package/dist/win32-custodian.js.map +1 -0
- package/dist/win32-custody.d.ts +57 -0
- package/dist/win32-custody.d.ts.map +1 -0
- package/dist/win32-custody.js +220 -0
- package/dist/win32-custody.js.map +1 -0
- package/dist/win32-process.d.ts +38 -0
- package/dist/win32-process.d.ts.map +1 -1
- package/dist/win32-process.js +63 -5
- package/dist/win32-process.js.map +1 -1
- package/native/README.md +6 -4
- package/native/darwin-process.c +173 -0
- package/native/win32-process.c +317 -14
- package/native/win32-system-id.cpp +32 -0
- package/package.json +2 -2
- package/scripts/assert-shipped-natives.mjs +3 -3
- package/scripts/build-native.mjs +46 -26
- package/scripts/native-contract.mjs +36 -16
package/native/README.md
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Native process identity
|
|
2
2
|
|
|
3
|
-
`win32-process.c` uses the stable Node-API C interface and Windows
|
|
3
|
+
`win32-process.c` uses the stable Node-API C interface and documented Windows APIs from `kernel32` and `advapi32`. It returns a process's creation FILETIME as a decimal string and this account's SID from its token. For process custody it starts one program per process inside a new job object. The job kills every member when its last handle closes and allows no breakaway. `CreateProcessW` assigns the job at creation through `PROC_THREAD_ATTRIBUTE_JOB_LIST`, so no child starts outside it. It passes the child only its input and log handles through `PROC_THREAD_ATTRIBUTE_HANDLE_LIST`, so no member can hold the job open. The binding then lists the job's processes, reads the program's exit code and terminates the job. It has no runtime dependency or PowerShell fallback. `win32-system-id.cpp` reads the system ID Windows documents for an unpackaged app's publisher, through C++/WinRT's `SystemIdentification.GetSystemIdForPublisher`, and the module Windows derived it from. It is C++20 and links `windowsapp.lib`; the binding reports the ID and its source, and a caller refuses a source of none.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
`darwin-process.c` uses the same interface and the documented macOS kernel interfaces. `sysctl` with `KERN_PROC_PID` gives a process's start time in microseconds, its process group and whether it is stopped. `KERN_PROC_PGRP` lists the members of a process group. `gethostuuid` gives the machine's UUID and `kern.bootsessionuuid` gives this boot's UUID. It does not use `libproc`, whose header calls its interfaces private and subject to change, and it never runs `ps`.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Build on native Windows x64 and arm64 in the matching MSVC Developer Command Prompt, or on macOS x64 and arm64 with Xcode or its command line tools, with `pnpm --filter @cotal-ai/workspace build:native`. The script downloads only the checksum-pinned Node headers in `node-sdk.json`, and on Windows the import library. On Windows it takes the C++/WinRT headers from the Windows SDK the prompt selects. A macOS bundle targets macOS 11.0, the oldest macOS Node 22 supports, and resolves Node-API symbols from the loading `node`. The script records source, SDK, compiler and binary provenance beside the output. An existing binary must verify before reuse; stale output is an error.
|
|
8
|
+
|
|
9
|
+
Both Windows and both macOS architecture directories and their receipts must be assembled under `build/Release` before packing or publishing workspace; the shared native workflow builds the macOS pair on `macos-15-intel` and `macos-15` when a caller asks for it with `darwin: true`. Customer installation uses these prebuilt files and runs no compiler or native download. The native CI suite runs the same Node-API 8 binaries on Node 22, 24 and 26. Its PowerShell compatibility oracle is test-only and runs after the first cold production read. `smoke/darwin-process-native.smoke.ts` runs the macOS binding on a macOS runner, and `smoke/win32-job-native.smoke.ts` runs the Windows job custody on a Windows runner. `smoke/win32-custody.smoke.ts` checks the custodian handshake on every platform and runs the custodian itself on Windows.
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
#define _DARWIN_C_SOURCE
|
|
2
|
+
#define NAPI_VERSION 8
|
|
3
|
+
#include <node_api.h>
|
|
4
|
+
#include <errno.h>
|
|
5
|
+
#include <math.h>
|
|
6
|
+
#include <stdint.h>
|
|
7
|
+
#include <stdio.h>
|
|
8
|
+
#include <stdlib.h>
|
|
9
|
+
#include <string.h>
|
|
10
|
+
#include <sys/types.h>
|
|
11
|
+
#include <sys/proc.h>
|
|
12
|
+
#include <sys/sysctl.h>
|
|
13
|
+
#include <time.h>
|
|
14
|
+
#include <unistd.h>
|
|
15
|
+
#include <uuid/uuid.h>
|
|
16
|
+
|
|
17
|
+
static napi_value undefined_value(napi_env env) {
|
|
18
|
+
napi_value value;
|
|
19
|
+
if (napi_get_undefined(env, &value) != napi_ok) return NULL;
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static napi_value fail(napi_env env, const char *what, int error) {
|
|
24
|
+
char message[160];
|
|
25
|
+
snprintf(message, sizeof(message), "%s failed: %s", what, strerror(error));
|
|
26
|
+
napi_throw_error(env, NULL, message);
|
|
27
|
+
return NULL;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/* A positive integer within pid_t, or 0 for any other argument. */
|
|
31
|
+
static pid_t pid_argument(napi_env env, napi_callback_info info) {
|
|
32
|
+
size_t argc = 1;
|
|
33
|
+
napi_value arg;
|
|
34
|
+
double value;
|
|
35
|
+
if (napi_get_cb_info(env, info, &argc, &arg, NULL, NULL) != napi_ok || argc != 1 ||
|
|
36
|
+
napi_get_value_double(env, arg, &value) != napi_ok || !isfinite(value) ||
|
|
37
|
+
value < 1 || value > INT32_MAX || value != (double)(int32_t)value) return 0;
|
|
38
|
+
return (pid_t)value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static int set_number(napi_env env, napi_value object, const char *name, double number) {
|
|
42
|
+
napi_value value;
|
|
43
|
+
return napi_create_double(env, number, &value) == napi_ok && napi_set_named_property(env, object, name, value) == napi_ok;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static int set_boolean(napi_env env, napi_value object, const char *name, int flag) {
|
|
47
|
+
napi_value value;
|
|
48
|
+
return napi_get_boolean(env, flag != 0, &value) == napi_ok && napi_set_named_property(env, object, name, value) == napi_ok;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/* One process's kernel entry: its start time, its process group and whether it is stopped.
|
|
52
|
+
* Undefined when no process has that ID; a failed read throws. */
|
|
53
|
+
static napi_value process_info(napi_env env, napi_callback_info info) {
|
|
54
|
+
pid_t pid = pid_argument(env, info);
|
|
55
|
+
if (pid == 0) return undefined_value(env);
|
|
56
|
+
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
|
|
57
|
+
struct kinfo_proc entry;
|
|
58
|
+
size_t length = sizeof(entry);
|
|
59
|
+
memset(&entry, 0, sizeof(entry));
|
|
60
|
+
if (sysctl(mib, 4, &entry, &length, NULL, 0) != 0) return fail(env, "sysctl KERN_PROC_PID", errno);
|
|
61
|
+
if (length == 0) return undefined_value(env);
|
|
62
|
+
if (length != sizeof(entry) || entry.kp_proc.p_pid != pid) {
|
|
63
|
+
napi_throw_error(env, NULL, "sysctl KERN_PROC_PID returned another process entry");
|
|
64
|
+
return NULL;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// The start time is a timeval. Keep every microsecond through the JS boundary as a decimal string.
|
|
68
|
+
struct timeval start = entry.kp_proc.p_starttime;
|
|
69
|
+
if (start.tv_sec < 0 || start.tv_usec < 0 || start.tv_usec >= 1000000) {
|
|
70
|
+
napi_throw_error(env, NULL, "the process start time is out of range");
|
|
71
|
+
return NULL;
|
|
72
|
+
}
|
|
73
|
+
char token[32];
|
|
74
|
+
int written = snprintf(token, sizeof(token), "%llu", (unsigned long long)start.tv_sec * 1000000ULL + (unsigned long long)start.tv_usec);
|
|
75
|
+
if (written < 1 || written >= (int)sizeof(token)) {
|
|
76
|
+
napi_throw_error(env, NULL, "the process start time could not be formatted");
|
|
77
|
+
return NULL;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
napi_value result, text;
|
|
81
|
+
if (napi_create_object(env, &result) != napi_ok ||
|
|
82
|
+
napi_create_string_utf8(env, token, (size_t)written, &text) != napi_ok ||
|
|
83
|
+
napi_set_named_property(env, result, "token", text) != napi_ok ||
|
|
84
|
+
!set_number(env, result, "pgid", (double)entry.kp_eproc.e_pgid) ||
|
|
85
|
+
!set_boolean(env, result, "stopped", entry.kp_proc.p_stat == SSTOP)) return NULL;
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/* The processes in one process group that have not exited. A failed read throws, so an unread group
|
|
90
|
+
* is never reported empty. */
|
|
91
|
+
static napi_value group_members(napi_env env, napi_callback_info info) {
|
|
92
|
+
pid_t pgid = pid_argument(env, info);
|
|
93
|
+
if (pgid == 0) return undefined_value(env);
|
|
94
|
+
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PGRP, pgid};
|
|
95
|
+
struct kinfo_proc *entries = NULL;
|
|
96
|
+
size_t length = 0;
|
|
97
|
+
for (int attempt = 0;; attempt++) {
|
|
98
|
+
size_t needed = 0;
|
|
99
|
+
if (sysctl(mib, 4, NULL, &needed, NULL, 0) != 0) return fail(env, "sysctl KERN_PROC_PGRP", errno);
|
|
100
|
+
length = needed + 8 * sizeof(struct kinfo_proc);
|
|
101
|
+
entries = malloc(length);
|
|
102
|
+
if (entries == NULL) return fail(env, "process group allocation", ENOMEM);
|
|
103
|
+
if (sysctl(mib, 4, entries, &length, NULL, 0) == 0) break;
|
|
104
|
+
int error = errno;
|
|
105
|
+
free(entries);
|
|
106
|
+
entries = NULL;
|
|
107
|
+
// The group grew between the size query and the read; ask again, a bounded number of times.
|
|
108
|
+
if (error != ENOMEM || attempt == 7) return fail(env, "sysctl KERN_PROC_PGRP", error);
|
|
109
|
+
}
|
|
110
|
+
if (length % sizeof(struct kinfo_proc) != 0) {
|
|
111
|
+
free(entries);
|
|
112
|
+
napi_throw_error(env, NULL, "sysctl KERN_PROC_PGRP returned a partial process entry");
|
|
113
|
+
return NULL;
|
|
114
|
+
}
|
|
115
|
+
size_t count = length / sizeof(struct kinfo_proc);
|
|
116
|
+
napi_value result;
|
|
117
|
+
if (napi_create_array(env, &result) != napi_ok) {
|
|
118
|
+
free(entries);
|
|
119
|
+
return NULL;
|
|
120
|
+
}
|
|
121
|
+
uint32_t index = 0;
|
|
122
|
+
for (size_t i = 0; i < count; i++) {
|
|
123
|
+
if (entries[i].kp_eproc.e_pgid != pgid || entries[i].kp_proc.p_stat == SZOMB) continue;
|
|
124
|
+
napi_value member;
|
|
125
|
+
if (napi_create_double(env, (double)entries[i].kp_proc.p_pid, &member) != napi_ok ||
|
|
126
|
+
napi_set_element(env, result, index++, member) != napi_ok) {
|
|
127
|
+
free(entries);
|
|
128
|
+
return NULL;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
free(entries);
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/* The machine's hardware UUID and this boot's session UUID, as the kernel reports them. */
|
|
136
|
+
static napi_value host_identity(napi_env env, napi_callback_info info) {
|
|
137
|
+
(void)info;
|
|
138
|
+
uuid_t id;
|
|
139
|
+
struct timespec wait = {5, 0};
|
|
140
|
+
if (gethostuuid(id, &wait) != 0) return fail(env, "gethostuuid", errno);
|
|
141
|
+
if (uuid_is_null(id)) {
|
|
142
|
+
napi_throw_error(env, NULL, "gethostuuid returned the null UUID");
|
|
143
|
+
return NULL;
|
|
144
|
+
}
|
|
145
|
+
uuid_string_t machine;
|
|
146
|
+
uuid_unparse_lower(id, machine);
|
|
147
|
+
|
|
148
|
+
char boot[64];
|
|
149
|
+
size_t length = sizeof(boot);
|
|
150
|
+
if (sysctlbyname("kern.bootsessionuuid", boot, &length, NULL, 0) != 0) return fail(env, "sysctl kern.bootsessionuuid", errno);
|
|
151
|
+
if (length == 0 || length > sizeof(boot) || boot[length - 1] != '\0') {
|
|
152
|
+
napi_throw_error(env, NULL, "kern.bootsessionuuid is not a string");
|
|
153
|
+
return NULL;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
napi_value result, machine_value, boot_value;
|
|
157
|
+
if (napi_create_object(env, &result) != napi_ok ||
|
|
158
|
+
napi_create_string_utf8(env, machine, NAPI_AUTO_LENGTH, &machine_value) != napi_ok ||
|
|
159
|
+
napi_set_named_property(env, result, "machine", machine_value) != napi_ok ||
|
|
160
|
+
napi_create_string_utf8(env, boot, NAPI_AUTO_LENGTH, &boot_value) != napi_ok ||
|
|
161
|
+
napi_set_named_property(env, result, "boot", boot_value) != napi_ok) return NULL;
|
|
162
|
+
return result;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
NAPI_MODULE_INIT() {
|
|
166
|
+
napi_property_descriptor functions[] = {
|
|
167
|
+
{"processInfo", NULL, process_info, NULL, NULL, NULL, napi_enumerable, NULL},
|
|
168
|
+
{"groupMembers", NULL, group_members, NULL, NULL, NULL, napi_enumerable, NULL},
|
|
169
|
+
{"hostIdentity", NULL, host_identity, NULL, NULL, NULL, napi_enumerable, NULL},
|
|
170
|
+
};
|
|
171
|
+
if (napi_define_properties(env, exports, sizeof(functions) / sizeof(functions[0]), functions) != napi_ok) return NULL;
|
|
172
|
+
return exports;
|
|
173
|
+
}
|
package/native/win32-process.c
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
#define WIN32_LEAN_AND_MEAN
|
|
2
|
+
#define _WIN32_WINNT 0x0A00
|
|
2
3
|
#define NAPI_VERSION 8
|
|
3
4
|
#include <windows.h>
|
|
5
|
+
#include <sddl.h>
|
|
4
6
|
#include <node_api.h>
|
|
5
7
|
#include <math.h>
|
|
6
8
|
#include <stdint.h>
|
|
7
9
|
#include <stdio.h>
|
|
10
|
+
#include <string.h>
|
|
11
|
+
#include <wchar.h>
|
|
8
12
|
|
|
9
13
|
static napi_value unreadable(napi_env env) {
|
|
10
14
|
napi_value value;
|
|
@@ -12,34 +16,333 @@ static napi_value unreadable(napi_env env) {
|
|
|
12
16
|
return value;
|
|
13
17
|
}
|
|
14
18
|
|
|
15
|
-
static napi_value
|
|
19
|
+
static napi_value fail(napi_env env, const char *what) {
|
|
20
|
+
char message[200];
|
|
21
|
+
DWORD code = GetLastError();
|
|
22
|
+
snprintf(message, sizeof(message), "%s failed (Windows error %lu)", what, (unsigned long)code);
|
|
23
|
+
napi_throw_error(env, NULL, message);
|
|
24
|
+
return NULL;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static napi_value refuse(napi_env env, const char *message) {
|
|
28
|
+
napi_throw_error(env, NULL, message);
|
|
29
|
+
return NULL;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// FILETIME exceeds Number's exact integer range. Keep every bit through the JS boundary.
|
|
33
|
+
static napi_value filetime_token(napi_env env, FILETIME time) {
|
|
34
|
+
uint64_t ticks = ((uint64_t)time.dwHighDateTime << 32) | time.dwLowDateTime;
|
|
35
|
+
char token[21];
|
|
36
|
+
int length = snprintf(token, sizeof(token), "%llu", (unsigned long long)ticks);
|
|
37
|
+
if (length < 1 || length >= (int)sizeof(token)) return unreadable(env);
|
|
38
|
+
napi_value value;
|
|
39
|
+
if (napi_create_string_utf8(env, token, (size_t)length, &value) != napi_ok) return NULL;
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static int pid_arg(napi_env env, napi_callback_info info, DWORD *pid) {
|
|
16
44
|
size_t argc = 1;
|
|
17
45
|
napi_value arg;
|
|
18
|
-
double
|
|
46
|
+
double value;
|
|
19
47
|
if (napi_get_cb_info(env, info, &argc, &arg, NULL, NULL) != napi_ok || argc != 1 ||
|
|
20
|
-
napi_get_value_double(env, arg, &
|
|
21
|
-
|
|
48
|
+
napi_get_value_double(env, arg, &value) != napi_ok || !isfinite(value) ||
|
|
49
|
+
value < 1 || value > INT32_MAX || value != (double)(uint32_t)value) return 0;
|
|
50
|
+
*pid = (DWORD)value;
|
|
51
|
+
return 1;
|
|
52
|
+
}
|
|
22
53
|
|
|
23
|
-
|
|
54
|
+
static napi_value creation_token(napi_env env, napi_callback_info info) {
|
|
55
|
+
DWORD pid;
|
|
56
|
+
if (!pid_arg(env, info, &pid)) return unreadable(env);
|
|
57
|
+
HANDLE process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
|
|
24
58
|
if (process == NULL) return unreadable(env);
|
|
25
59
|
FILETIME creation, exit_time, kernel_time, user_time;
|
|
26
60
|
BOOL ok = GetProcessTimes(process, &creation, &exit_time, &kernel_time, &user_time);
|
|
27
61
|
CloseHandle(process);
|
|
28
62
|
if (!ok) return unreadable(env);
|
|
63
|
+
return filetime_token(env, creation);
|
|
64
|
+
}
|
|
29
65
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (
|
|
66
|
+
// The account this process runs as, as its string SID.
|
|
67
|
+
static napi_value user_sid(napi_env env, napi_callback_info info) {
|
|
68
|
+
(void)info;
|
|
69
|
+
HANDLE token;
|
|
70
|
+
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) return fail(env, "OpenProcessToken");
|
|
71
|
+
DWORD size = 0;
|
|
72
|
+
GetTokenInformation(token, TokenUser, NULL, 0, &size);
|
|
73
|
+
TOKEN_USER *user = size == 0 ? NULL : HeapAlloc(GetProcessHeap(), 0, size);
|
|
74
|
+
if (user == NULL || !GetTokenInformation(token, TokenUser, user, size, &size)) {
|
|
75
|
+
DWORD code = GetLastError();
|
|
76
|
+
if (user != NULL) HeapFree(GetProcessHeap(), 0, user);
|
|
77
|
+
CloseHandle(token);
|
|
78
|
+
SetLastError(code);
|
|
79
|
+
return fail(env, "GetTokenInformation");
|
|
80
|
+
}
|
|
81
|
+
CloseHandle(token);
|
|
82
|
+
wchar_t *text = NULL;
|
|
83
|
+
BOOL converted = ConvertSidToStringSidW(user->User.Sid, &text);
|
|
84
|
+
DWORD code = GetLastError();
|
|
85
|
+
HeapFree(GetProcessHeap(), 0, user);
|
|
86
|
+
if (!converted) { SetLastError(code); return fail(env, "ConvertSidToStringSidW"); }
|
|
35
87
|
napi_value value;
|
|
36
|
-
|
|
88
|
+
napi_status status = napi_create_string_utf16(env, (const char16_t *)text, NAPI_AUTO_LENGTH, &value);
|
|
89
|
+
LocalFree(text);
|
|
90
|
+
return status == napi_ok ? value : NULL;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Defined in win32-system-id.cpp: Windows' documented system ID for this app's publisher, through
|
|
94
|
+
// SystemIdentification.GetSystemIdForPublisher. Returns 0 or the failing HRESULT.
|
|
95
|
+
int32_t cotal_system_id(uint8_t *id, uint32_t capacity, uint32_t *length, int32_t *source);
|
|
96
|
+
|
|
97
|
+
// This device's system ID for an unpackaged app, as lowercase hex, and the module that produced it
|
|
98
|
+
// (SystemIdentificationSource: 0 none, 1 TPM, 2 UEFI, 3 registry). The caller judges the source.
|
|
99
|
+
static napi_value system_id(napi_env env, napi_callback_info info) {
|
|
100
|
+
(void)info;
|
|
101
|
+
uint8_t id[256];
|
|
102
|
+
uint32_t length = 0;
|
|
103
|
+
int32_t source = -1;
|
|
104
|
+
int32_t result = cotal_system_id(id, (uint32_t)sizeof(id), &length, &source);
|
|
105
|
+
if (result != 0) {
|
|
106
|
+
char message[96];
|
|
107
|
+
snprintf(message, sizeof(message), "GetSystemIdForPublisher failed (HRESULT 0x%08lX)", (unsigned long)(uint32_t)result);
|
|
108
|
+
return refuse(env, message);
|
|
109
|
+
}
|
|
110
|
+
if (length == 0 || length > sizeof(id)) return refuse(env, "the system ID is empty or longer than 256 bytes");
|
|
111
|
+
static const char digits[] = "0123456789abcdef";
|
|
112
|
+
char text[2 * sizeof(id)];
|
|
113
|
+
for (uint32_t i = 0; i < length; i++) {
|
|
114
|
+
text[2 * i] = digits[id[i] >> 4];
|
|
115
|
+
text[2 * i + 1] = digits[id[i] & 0x0F];
|
|
116
|
+
}
|
|
117
|
+
napi_value value, hex, kind;
|
|
118
|
+
if (napi_create_object(env, &value) != napi_ok ||
|
|
119
|
+
napi_create_string_utf8(env, text, 2 * (size_t)length, &hex) != napi_ok ||
|
|
120
|
+
napi_create_int32(env, source, &kind) != napi_ok ||
|
|
121
|
+
napi_set_named_property(env, value, "id", hex) != napi_ok ||
|
|
122
|
+
napi_set_named_property(env, value, "source", kind) != napi_ok) return NULL;
|
|
37
123
|
return value;
|
|
38
124
|
}
|
|
39
125
|
|
|
126
|
+
// One launched job per process. Its handle is never finalized from JS: closing the last handle
|
|
127
|
+
// terminates every member, so it lives until this process exits.
|
|
128
|
+
static HANDLE job = NULL;
|
|
129
|
+
static HANDLE root = NULL;
|
|
130
|
+
|
|
131
|
+
static wchar_t *string_property(napi_env env, napi_value object, const char *name, int block) {
|
|
132
|
+
napi_value value;
|
|
133
|
+
napi_valuetype type;
|
|
134
|
+
size_t length, copied;
|
|
135
|
+
if (napi_get_named_property(env, object, name, &value) != napi_ok || napi_typeof(env, value, &type) != napi_ok ||
|
|
136
|
+
type != napi_string || napi_get_value_string_utf16(env, value, NULL, 0, &length) != napi_ok || length == 0 || length > (block ? 1048576u : 32767u)) return NULL;
|
|
137
|
+
wchar_t *buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (length + 1) * sizeof(wchar_t));
|
|
138
|
+
if (buffer == NULL) return NULL;
|
|
139
|
+
if (napi_get_value_string_utf16(env, value, (char16_t *)buffer, length + 1, &copied) != napi_ok || copied != length) {
|
|
140
|
+
HeapFree(GetProcessHeap(), 0, buffer);
|
|
141
|
+
return NULL;
|
|
142
|
+
}
|
|
143
|
+
// A path or command line holds no NUL. An environment block is NUL-separated entries, each
|
|
144
|
+
// ending in NUL, and the terminator here makes its closing empty entry.
|
|
145
|
+
size_t first = wcslen(buffer);
|
|
146
|
+
int valid = block ? buffer[length - 1] == L'\0' && buffer[0] != L'\0' : first == length;
|
|
147
|
+
for (size_t i = 1; block && valid && i < length; i++) if (buffer[i] == L'\0' && buffer[i - 1] == L'\0') valid = 0;
|
|
148
|
+
if (!valid) {
|
|
149
|
+
HeapFree(GetProcessHeap(), 0, buffer);
|
|
150
|
+
return NULL;
|
|
151
|
+
}
|
|
152
|
+
return buffer;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static void free_strings(wchar_t **strings, int count) {
|
|
156
|
+
for (int i = 0; i < count; i++) if (strings[i] != NULL) HeapFree(GetProcessHeap(), 0, strings[i]);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
static napi_value launch_result(napi_env env, DWORD pid, FILETIME creation) {
|
|
160
|
+
napi_value result, pid_value, token;
|
|
161
|
+
if (napi_create_object(env, &result) != napi_ok || napi_create_uint32(env, pid, &pid_value) != napi_ok ||
|
|
162
|
+
(token = filetime_token(env, creation)) == NULL ||
|
|
163
|
+
napi_set_named_property(env, result, "pid", pid_value) != napi_ok ||
|
|
164
|
+
napi_set_named_property(env, result, "token", token) != napi_ok) return NULL;
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Start `application` inside a new job before it runs. The job kills every member when its last
|
|
169
|
+
// handle closes and allows no breakaway. The child inherits only its input and the log; the job
|
|
170
|
+
// handle stays private to this process.
|
|
171
|
+
static napi_value launch(napi_env env, napi_callback_info info) {
|
|
172
|
+
size_t argc = 1;
|
|
173
|
+
napi_value options;
|
|
174
|
+
napi_valuetype type;
|
|
175
|
+
if (napi_get_cb_info(env, info, &argc, &options, NULL, NULL) != napi_ok || argc != 1 ||
|
|
176
|
+
napi_typeof(env, options, &type) != napi_ok || type != napi_object) return refuse(env, "launch takes one options object");
|
|
177
|
+
if (job != NULL) return refuse(env, "this process already holds a launched job");
|
|
178
|
+
const char *names[5] = { "application", "commandLine", "cwd", "environment", "log" };
|
|
179
|
+
wchar_t *strings[5];
|
|
180
|
+
for (int i = 0; i < 5; i++) strings[i] = string_property(env, options, names[i], i == 3);
|
|
181
|
+
for (int i = 0; i < 5; i++) if (strings[i] == NULL) { free_strings(strings, 5); return refuse(env, "launch needs an application, command line, directory, environment block and log path"); }
|
|
182
|
+
|
|
183
|
+
const char *step = NULL;
|
|
184
|
+
DWORD code = ERROR_SUCCESS;
|
|
185
|
+
HANDLE created = NULL, input = INVALID_HANDLE_VALUE, output = INVALID_HANDLE_VALUE;
|
|
186
|
+
LPPROC_THREAD_ATTRIBUTE_LIST attributes = NULL;
|
|
187
|
+
PROCESS_INFORMATION child;
|
|
188
|
+
ZeroMemory(&child, sizeof(child));
|
|
189
|
+
FILETIME creation, exit_time, kernel_time, user_time;
|
|
190
|
+
JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits, applied;
|
|
191
|
+
ZeroMemory(&limits, sizeof(limits));
|
|
192
|
+
ZeroMemory(&applied, sizeof(applied));
|
|
193
|
+
limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
|
|
194
|
+
SECURITY_ATTRIBUTES inheritable = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
|
|
195
|
+
HANDLE inherited[2];
|
|
196
|
+
SIZE_T size = 0;
|
|
197
|
+
STARTUPINFOEXW startup;
|
|
198
|
+
ZeroMemory(&startup, sizeof(startup));
|
|
199
|
+
BOOL member = FALSE;
|
|
200
|
+
|
|
201
|
+
if ((created = CreateJobObjectW(NULL, NULL)) == NULL) { step = "CreateJobObjectW"; goto failed; }
|
|
202
|
+
if (!SetInformationJobObject(created, JobObjectExtendedLimitInformation, &limits, sizeof(limits))) { step = "SetInformationJobObject"; goto failed; }
|
|
203
|
+
if (!QueryInformationJobObject(created, JobObjectExtendedLimitInformation, &applied, sizeof(applied), NULL)) { step = "QueryInformationJobObject"; goto failed; }
|
|
204
|
+
if ((applied.BasicLimitInformation.LimitFlags & (JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)) != JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE) {
|
|
205
|
+
SetLastError(ERROR_INVALID_DATA); step = "job limit verification"; goto failed;
|
|
206
|
+
}
|
|
207
|
+
input = CreateFileW(L"NUL", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, &inheritable, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
208
|
+
if (input == INVALID_HANDLE_VALUE) { step = "CreateFileW(NUL)"; goto failed; }
|
|
209
|
+
output = CreateFileW(strings[4], FILE_APPEND_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, &inheritable, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
210
|
+
if (output == INVALID_HANDLE_VALUE) { step = "CreateFileW(log)"; goto failed; }
|
|
211
|
+
inherited[0] = input;
|
|
212
|
+
inherited[1] = output;
|
|
213
|
+
InitializeProcThreadAttributeList(NULL, 2, 0, &size);
|
|
214
|
+
if (size == 0 || (attributes = HeapAlloc(GetProcessHeap(), 0, size)) == NULL) { step = "attribute list allocation"; goto failed; }
|
|
215
|
+
if (!InitializeProcThreadAttributeList(attributes, 2, 0, &size)) { HeapFree(GetProcessHeap(), 0, attributes); attributes = NULL; step = "InitializeProcThreadAttributeList"; goto failed; }
|
|
216
|
+
if (!UpdateProcThreadAttribute(attributes, 0, PROC_THREAD_ATTRIBUTE_JOB_LIST, &created, sizeof(HANDLE), NULL, NULL)) { step = "UpdateProcThreadAttribute(job)"; goto failed; }
|
|
217
|
+
if (!UpdateProcThreadAttribute(attributes, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, inherited, sizeof(inherited), NULL, NULL)) { step = "UpdateProcThreadAttribute(handles)"; goto failed; }
|
|
218
|
+
startup.StartupInfo.cb = sizeof(startup);
|
|
219
|
+
// Detached and hidden, as a Node spawn with `detached` and `windowsHide` starts a program.
|
|
220
|
+
startup.StartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
|
|
221
|
+
startup.StartupInfo.wShowWindow = SW_HIDE;
|
|
222
|
+
startup.StartupInfo.hStdInput = input;
|
|
223
|
+
startup.StartupInfo.hStdOutput = output;
|
|
224
|
+
startup.StartupInfo.hStdError = output;
|
|
225
|
+
startup.lpAttributeList = attributes;
|
|
226
|
+
// Suspended, so membership and the creation pin are read before the child runs any code.
|
|
227
|
+
if (!CreateProcessW(strings[0], strings[1], NULL, NULL, TRUE,
|
|
228
|
+
EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT | DETACHED_PROCESS | CREATE_SUSPENDED,
|
|
229
|
+
strings[3], strings[2], &startup.StartupInfo, &child)) { step = "CreateProcessW"; goto failed; }
|
|
230
|
+
if (!IsProcessInJob(child.hProcess, created, &member)) { step = "IsProcessInJob"; goto failed; }
|
|
231
|
+
if (!member) { SetLastError(ERROR_INVALID_DATA); step = "job membership verification"; goto failed; }
|
|
232
|
+
if (!GetProcessTimes(child.hProcess, &creation, &exit_time, &kernel_time, &user_time)) { step = "GetProcessTimes"; goto failed; }
|
|
233
|
+
if (ResumeThread(child.hThread) == (DWORD)-1) { step = "ResumeThread"; goto failed; }
|
|
234
|
+
CloseHandle(child.hThread);
|
|
235
|
+
CloseHandle(input);
|
|
236
|
+
CloseHandle(output);
|
|
237
|
+
DeleteProcThreadAttributeList(attributes);
|
|
238
|
+
HeapFree(GetProcessHeap(), 0, attributes);
|
|
239
|
+
free_strings(strings, 5);
|
|
240
|
+
job = created;
|
|
241
|
+
root = child.hProcess;
|
|
242
|
+
return launch_result(env, child.dwProcessId, creation);
|
|
243
|
+
|
|
244
|
+
failed:
|
|
245
|
+
code = GetLastError();
|
|
246
|
+
if (child.hProcess != NULL) { TerminateProcess(child.hProcess, 1); CloseHandle(child.hProcess); }
|
|
247
|
+
if (child.hThread != NULL) CloseHandle(child.hThread);
|
|
248
|
+
if (input != INVALID_HANDLE_VALUE) CloseHandle(input);
|
|
249
|
+
if (output != INVALID_HANDLE_VALUE) CloseHandle(output);
|
|
250
|
+
if (attributes != NULL) { DeleteProcThreadAttributeList(attributes); HeapFree(GetProcessHeap(), 0, attributes); }
|
|
251
|
+
if (created != NULL) CloseHandle(created);
|
|
252
|
+
free_strings(strings, 5);
|
|
253
|
+
SetLastError(code);
|
|
254
|
+
return fail(env, step);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
static int launched(napi_env env) {
|
|
258
|
+
if (job != NULL) return 1;
|
|
259
|
+
napi_throw_error(env, NULL, "this process holds no launched job");
|
|
260
|
+
return 0;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// The launched process's exit code once it has exited, otherwise undefined.
|
|
264
|
+
static napi_value root_exit(napi_env env, napi_callback_info info) {
|
|
265
|
+
(void)info;
|
|
266
|
+
if (!launched(env)) return NULL;
|
|
267
|
+
DWORD wait = WaitForSingleObject(root, 0), code;
|
|
268
|
+
if (wait == WAIT_TIMEOUT) return unreadable(env);
|
|
269
|
+
if (wait != WAIT_OBJECT_0) return fail(env, "WaitForSingleObject");
|
|
270
|
+
if (!GetExitCodeProcess(root, &code)) return fail(env, "GetExitCodeProcess");
|
|
271
|
+
napi_value value;
|
|
272
|
+
if (napi_create_uint32(env, code, &value) != napi_ok) return NULL;
|
|
273
|
+
return value;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// The process IDs the job holds now.
|
|
277
|
+
static napi_value job_processes(napi_env env, napi_callback_info info) {
|
|
278
|
+
(void)info;
|
|
279
|
+
if (!launched(env)) return NULL;
|
|
280
|
+
DWORD capacity = 64;
|
|
281
|
+
for (int attempt = 0; attempt < 8; attempt++) {
|
|
282
|
+
DWORD bytes = (DWORD)(sizeof(JOBOBJECT_BASIC_PROCESS_ID_LIST) + (capacity - 1) * sizeof(ULONG_PTR));
|
|
283
|
+
JOBOBJECT_BASIC_PROCESS_ID_LIST *list = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bytes);
|
|
284
|
+
if (list == NULL) return refuse(env, "job process list allocation failed");
|
|
285
|
+
BOOL ok = QueryInformationJobObject(job, JobObjectBasicProcessIdList, list, bytes, NULL);
|
|
286
|
+
DWORD code = GetLastError();
|
|
287
|
+
if ((ok || code == ERROR_MORE_DATA) && list->NumberOfProcessIdsInList < list->NumberOfAssignedProcesses) {
|
|
288
|
+
capacity = list->NumberOfAssignedProcesses + 16;
|
|
289
|
+
HeapFree(GetProcessHeap(), 0, list);
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if (!ok) { HeapFree(GetProcessHeap(), 0, list); SetLastError(code); return fail(env, "QueryInformationJobObject"); }
|
|
293
|
+
napi_value result, pid;
|
|
294
|
+
if (napi_create_array_with_length(env, list->NumberOfProcessIdsInList, &result) != napi_ok) { HeapFree(GetProcessHeap(), 0, list); return NULL; }
|
|
295
|
+
for (DWORD i = 0; i < list->NumberOfProcessIdsInList; i++) {
|
|
296
|
+
if (napi_create_uint32(env, (uint32_t)list->ProcessIdList[i], &pid) != napi_ok || napi_set_element(env, result, i, pid) != napi_ok) {
|
|
297
|
+
HeapFree(GetProcessHeap(), 0, list);
|
|
298
|
+
return NULL;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
HeapFree(GetProcessHeap(), 0, list);
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
return refuse(env, "the job's process list kept growing");
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Whether a process is a member of the launched job; undefined when it cannot be opened.
|
|
308
|
+
static napi_value in_job(napi_env env, napi_callback_info info) {
|
|
309
|
+
DWORD pid;
|
|
310
|
+
if (!pid_arg(env, info, &pid)) return unreadable(env);
|
|
311
|
+
if (!launched(env)) return NULL;
|
|
312
|
+
HANDLE process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
|
|
313
|
+
if (process == NULL) return unreadable(env);
|
|
314
|
+
BOOL member = FALSE;
|
|
315
|
+
BOOL ok = IsProcessInJob(process, job, &member);
|
|
316
|
+
CloseHandle(process);
|
|
317
|
+
if (!ok) return fail(env, "IsProcessInJob");
|
|
318
|
+
napi_value value;
|
|
319
|
+
if (napi_get_boolean(env, member != FALSE, &value) != napi_ok) return NULL;
|
|
320
|
+
return value;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Terminate every process in the launched job with `code`.
|
|
324
|
+
static napi_value terminate(napi_env env, napi_callback_info info) {
|
|
325
|
+
size_t argc = 1;
|
|
326
|
+
napi_value arg;
|
|
327
|
+
uint32_t code;
|
|
328
|
+
if (napi_get_cb_info(env, info, &argc, &arg, NULL, NULL) != napi_ok || argc != 1 ||
|
|
329
|
+
napi_get_value_uint32(env, arg, &code) != napi_ok) return refuse(env, "terminate takes one exit code");
|
|
330
|
+
if (!launched(env)) return NULL;
|
|
331
|
+
if (!TerminateJobObject(job, code)) return fail(env, "TerminateJobObject");
|
|
332
|
+
return unreadable(env);
|
|
333
|
+
}
|
|
334
|
+
|
|
40
335
|
NAPI_MODULE_INIT() {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
336
|
+
napi_property_descriptor properties[] = {
|
|
337
|
+
{ "creationToken", NULL, creation_token, NULL, NULL, NULL, napi_enumerable, NULL },
|
|
338
|
+
{ "userSid", NULL, user_sid, NULL, NULL, NULL, napi_enumerable, NULL },
|
|
339
|
+
{ "systemId", NULL, system_id, NULL, NULL, NULL, napi_enumerable, NULL },
|
|
340
|
+
{ "launch", NULL, launch, NULL, NULL, NULL, napi_enumerable, NULL },
|
|
341
|
+
{ "rootExit", NULL, root_exit, NULL, NULL, NULL, napi_enumerable, NULL },
|
|
342
|
+
{ "jobProcesses", NULL, job_processes, NULL, NULL, NULL, napi_enumerable, NULL },
|
|
343
|
+
{ "inJob", NULL, in_job, NULL, NULL, NULL, napi_enumerable, NULL },
|
|
344
|
+
{ "terminate", NULL, terminate, NULL, NULL, NULL, napi_enumerable, NULL },
|
|
345
|
+
};
|
|
346
|
+
if (napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties) != napi_ok) return NULL;
|
|
44
347
|
return exports;
|
|
45
348
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Windows' documented system ID for an unpackaged app's publisher, for win32-process.c. C++/WinRT
|
|
2
|
+
// needs C++20 and links windowsapp.lib. On a thread with no COM apartment, C++/WinRT activates the
|
|
3
|
+
// API in the process's implicit multithreaded apartment, so Node's own thread is not initialized.
|
|
4
|
+
#include <winrt/base.h>
|
|
5
|
+
#include <winrt/Windows.Foundation.h>
|
|
6
|
+
#include <winrt/Windows.Security.Cryptography.h>
|
|
7
|
+
#include <winrt/Windows.Storage.Streams.h>
|
|
8
|
+
#include <winrt/Windows.System.Profile.h>
|
|
9
|
+
#include <cstdint>
|
|
10
|
+
#include <cstring>
|
|
11
|
+
|
|
12
|
+
// Copy the ID into `id` and its SystemIdentificationSource into `source`, and return 0, or return the
|
|
13
|
+
// failing HRESULT. An ID longer than `capacity` reports its length and copies nothing.
|
|
14
|
+
extern "C" int32_t cotal_system_id(uint8_t *id, uint32_t capacity, uint32_t *length, int32_t *source) noexcept {
|
|
15
|
+
try {
|
|
16
|
+
using namespace winrt::Windows::System::Profile;
|
|
17
|
+
SystemIdentificationInfo info = SystemIdentification::GetSystemIdForPublisher();
|
|
18
|
+
*source = static_cast<int32_t>(info.Source());
|
|
19
|
+
*length = 0;
|
|
20
|
+
auto buffer = info.Id();
|
|
21
|
+
if (!buffer) return 0;
|
|
22
|
+
winrt::com_array<uint8_t> bytes;
|
|
23
|
+
winrt::Windows::Security::Cryptography::CryptographicBuffer::CopyToByteArray(buffer, bytes);
|
|
24
|
+
*length = bytes.size();
|
|
25
|
+
if (bytes.size() <= capacity) std::memcpy(id, bytes.data(), bytes.size());
|
|
26
|
+
return 0;
|
|
27
|
+
} catch (winrt::hresult_error const &error) {
|
|
28
|
+
return static_cast<int32_t>(error.code());
|
|
29
|
+
} catch (...) {
|
|
30
|
+
return -2147467259; // E_FAIL
|
|
31
|
+
}
|
|
32
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cotal-ai/workspace",
|
|
3
3
|
"description": "Cotal machine-local workstation layer: the ~/.cotal mesh registry, target resolution, preflight, on-disk auth-path I/O, and the command-copy renderer — operator concerns over a local checkout, kept separate from the wire protocol in @cotal-ai/core.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.101.1",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@cotal-ai/core": "0.
|
|
21
|
+
"@cotal-ai/core": "0.101.1",
|
|
22
22
|
"ajv": "^8.20.0",
|
|
23
23
|
"string-width": "^8.2.1",
|
|
24
24
|
"yaml": "^2.9.0",
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
import { verifyNative } from "./native-contract.mjs";
|
|
3
3
|
|
|
4
4
|
try {
|
|
5
|
-
for (const arch of ["x64", "arm64"]) verifyNative(arch);
|
|
6
|
-
console.log("
|
|
5
|
+
for (const platform of ["win32", "darwin"]) for (const arch of ["x64", "arm64"]) verifyNative(platform, arch);
|
|
6
|
+
console.log("Native process identity: Windows and macOS bindings verified for both architectures");
|
|
7
7
|
} catch (cause) {
|
|
8
|
-
throw new Error("@cotal-ai/workspace requires source-matched Windows x64 and arm64 identity bindings before pack or publish", { cause });
|
|
8
|
+
throw new Error("@cotal-ai/workspace requires source-matched Windows and macOS x64 and arm64 identity bindings before pack or publish", { cause });
|
|
9
9
|
}
|