@cotal-ai/workspace 0.101.0 → 0.101.2
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 +5 -4
- package/build/Release/darwin-x64/build.json +5 -4
- package/build/Release/win32-arm64/build.json +6 -5
- package/build/Release/win32-arm64/creation.node +0 -0
- package/build/Release/win32-x64/build.json +6 -5
- package/build/Release/win32-x64/creation.node +0 -0
- package/dist/connect.d.ts +4 -1
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +4 -1
- package/dist/connect.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/mesh-target.d.ts +3 -2
- package/dist/mesh-target.d.ts.map +1 -1
- package/dist/mesh-target.js +23 -9
- package/dist/mesh-target.js.map +1 -1
- package/dist/pid.d.ts.map +1 -1
- package/dist/pid.js +7 -1
- package/dist/pid.js.map +1 -1
- package/dist/render.js +1 -1
- package/dist/render.js.map +1 -1
- package/dist/service-custody.js +2 -2
- package/dist/service-custody.js.map +1 -1
- package/dist/service-identity.js +4 -4
- package/dist/service-identity.js.map +1 -1
- package/dist/service-stop.js +2 -2
- package/dist/service-stop.js.map +1 -1
- 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 +3 -3
- package/native/win32-process.c +317 -14
- package/native/win32-system-id.cpp +32 -0
- package/package.json +2 -2
- package/scripts/build-native.mjs +10 -4
- package/scripts/native-contract.mjs +1 -1
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.101.
|
|
4
|
+
"version": "0.101.2",
|
|
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.101.
|
|
21
|
+
"@cotal-ai/core": "0.101.2",
|
|
22
22
|
"ajv": "^8.20.0",
|
|
23
23
|
"string-width": "^8.2.1",
|
|
24
24
|
"yaml": "^2.9.0",
|
package/scripts/build-native.mjs
CHANGED
|
@@ -17,7 +17,7 @@ if (existsSync(join(dest, nativeFile(platform))) || existsSync(join(dest, "build
|
|
|
17
17
|
verifyNative(platform, arch, dest);
|
|
18
18
|
process.exit(0);
|
|
19
19
|
}
|
|
20
|
-
if (platform === "win32") assert(process.env.VCToolsVersion && process.env.WindowsSDKVersion, "build Windows identity in the matching MSVC Developer Command Prompt");
|
|
20
|
+
if (platform === "win32") assert(process.env.VCToolsVersion && process.env.WindowsSDKVersion && process.env.WindowsSdkDir, "build Windows identity in the matching MSVC Developer Command Prompt");
|
|
21
21
|
const temp = mkdtempSync(join(tmpdir(), `cotal-${platform}-native-`));
|
|
22
22
|
try {
|
|
23
23
|
const download = async (path, expected, output) => {
|
|
@@ -42,9 +42,15 @@ try {
|
|
|
42
42
|
const include = join(sdkDir, `node-v${sdk.version}`, "include/node");
|
|
43
43
|
const output = join(buildDir, nativeFile(platform));
|
|
44
44
|
if (platform === "win32") {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
// The system ID reader is C++/WinRT, whose headers ship in the Windows SDK off the default include path.
|
|
46
|
+
const cppwinrt = join(process.env.WindowsSdkDir, "Include", process.env.WindowsSDKVersion, "cppwinrt");
|
|
47
|
+
assert(existsSync(join(cppwinrt, "winrt", "base.h")), `C++/WinRT headers not found at ${cppwinrt}`);
|
|
48
|
+
const compile = ["/nologo", "/c", "/O2", "/MT", "/W4", "/WX"];
|
|
49
|
+
execFileSync("cl.exe", [...compile, "/std:c11", `/I${include}`, `/Fo${join(buildDir, "creation.obj")}`, join(root, "native/win32-process.c")], { cwd: buildDir, stdio: "inherit" });
|
|
50
|
+
execFileSync("cl.exe", [...compile, "/std:c++20", "/EHsc", "/permissive-", "/external:anglebrackets", "/external:W0", `/I${cppwinrt}`,
|
|
51
|
+
`/Fo${join(buildDir, "system-id.obj")}`, join(root, "native/win32-system-id.cpp")], { cwd: buildDir, stdio: "inherit" });
|
|
52
|
+
execFileSync("link.exe", ["/nologo", "/DLL", "/Brepro", `/OUT:${output}`, join(buildDir, "creation.obj"), join(buildDir, "system-id.obj"),
|
|
53
|
+
library, "kernel32.lib", "advapi32.lib", "windowsapp.lib"], { cwd: buildDir, stdio: "inherit" });
|
|
48
54
|
if (process.argv.includes("--test-controls")) {
|
|
49
55
|
const testDir = join(root, "build/test", `win32-${arch}`);
|
|
50
56
|
mkdirSync(testDir, { recursive: true });
|
|
@@ -5,7 +5,7 @@ import { join } from "node:path";
|
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
6
|
|
|
7
7
|
export const root = fileURLToPath(new URL("..", import.meta.url));
|
|
8
|
-
export const sourceFiles = ["native/win32-process.c", "native/darwin-process.c", "native/node-sdk.json", "scripts/build-native.mjs", "scripts/native-contract.mjs"];
|
|
8
|
+
export const sourceFiles = ["native/win32-process.c", "native/win32-system-id.cpp", "native/darwin-process.c", "native/node-sdk.json", "scripts/build-native.mjs", "scripts/native-contract.mjs"];
|
|
9
9
|
export const sha256 = (bytes) => createHash("sha256").update(bytes).digest("hex");
|
|
10
10
|
export const sourceHashes = () => Object.fromEntries(sourceFiles.map((path) => [path, sha256(readFileSync(join(root, path)))]));
|
|
11
11
|
export const sdk = JSON.parse(readFileSync(join(root, "native/node-sdk.json"), "utf8"));
|