Skip to content

Commit 1c99f20

Browse files
committed
feat(local-terminal): decide availability by a runtime PTY probe
The gate was `deviceInfo.deviceType === '2in1'` on the ArkTS side. That was a form-factor guess, and it could not see the failure that matters: a device that reports 2in1, loads the addon fine, and still gets `forkpty(3) failed.`. Now the backend probes for real at boot — allocate a pty (openpty), then fork a shell into it — and offers the local terminal only if that worked: openpty fails -> the sandbox exposes no usable /dev/ptmx or devpts forkpty fails -> the pty is fine, creating the child was refused both succeed -> the terminal works here That is more permissive than the device type (a phone has the same sandbox as a 2-in-1, so it is enabled exactly when it can really fork a shell) and stricter (a broken 2-in-1 no longer advertises a tab that dies on connect). Measured: 2-in-1 emulator -> enabled (openpty + forkpty ok), session runs. Phone emulator -> openpty(3) failed., disabled; the report shows the cause — /dev/ptmx stat = FAIL EACCES (app domain denied the pty node entirely), and even `spawnSync /bin/sh` is EACCES there. Because a hidden feature leaves nothing to print into, and on a real ROM the sandbox is unreadable from outside, the probe result is published to <dataDir>/local-terminal.json and the ArkTS side echoes it to hilog (`hdc shell hilog -x | grep 'lt[0-9]:'`) — the one channel that survives a sealed device. Also consolidates the pty diagnosis into app/server/local-terminal.js, shared by the probe and a failed session spawn, and drops the now-unused deviceInfo import.
1 parent 7b89732 commit 1c99f20

6 files changed

Lines changed: 521 additions & 121 deletions

File tree

‎build-src/replace/app/lib/view.js‎

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import fsFunctions from '../common/fs-functions.js'
1717
import copy from 'json-deep-copy'
1818
import { createToken } from './jwt.js'
1919
import { logDir } from '../server/session-log.js'
20+
import { localTerminalCapability } from '../server/local-terminal.js'
2021
import { resolve } from 'path'
21-
import fs from 'fs'
2222

2323
const defaultAIPreset = {
2424
baseURLAI: 'https://ai.electerm.org/api/ai',
@@ -34,62 +34,34 @@ function buildServer () {
3434
}
3535

3636
/**
37-
* Can this build/device serve a local terminal?
37+
* Can this device serve a local terminal?
3838
*
39-
* Two independent gates, and both have to pass:
39+
* Asked of a runtime probe, not of the device type or of a file listing —
40+
* ../server/local-terminal.js allocates a pty and forks a shell in this very
41+
* process and reports what happened. The gate used to be
42+
* `deviceInfo.deviceType === '2in1'` on the ArkTS side; that was a form-factor
43+
* guess that also could not detect a device whose sandbox refuses the fork. The
44+
* sandbox is what decides, and it is the same sandbox on a phone as on a 2-in-1,
45+
* so any device that can really fork a shell gets the feature.
4046
*
41-
* 1. DISABLE_LOCAL_TERMINAL must be unset. BackendLauncher.ets sets it on
42-
* phone/tablet from deviceInfo.deviceType. That is a product gate: the
43-
* shell inside the PTY works on any form factor, but its home is the app's
44-
* sandbox data dir (session-local.js), so on a phone the terminal would
45-
* open onto an app-private directory with nothing useful to do in it.
46-
* Also settable from the build env (build-src/web/build.mjs) to force the
47-
* feature off for a local desktop run.
48-
* 2. The addon has to be loadable. Two candidate locations, in the order
49-
* node-pty resolves them:
50-
* a. ELECTERM_PTY_ADDON — an absolute path into the app's *lib*
51-
* directory, exported by node_ctl.c from the directory libnode.so
52-
* itself was found in. This is the one that works: OHOS's linker
53-
* only loads native code from the app's lib dirs, so a .node file
54-
* inside resfile fails before relocation (musl reports the useless
55-
* "No error information"). scripts/prepare-node-pty.sh stages the
56-
* addon there alongside its resfile copy.
57-
* b. The resfile sibling of app.bundle.mjs, where node-pty's own
58-
* loadNativeModule() looks first — kept as the fallback for a
59-
* desktop run, where (a) is never set.
60-
* Staging is designed to fail soft — a failed run degrades to "no local
61-
* terminal" instead of breaking the build — so check rather than assume.
47+
* DISABLE_LOCAL_TERMINAL is still honoured as the one manual override (build
48+
* env, or a hand-run backend) — it is checked inside the probe.
6249
*
6350
* Reporting availability is not cosmetic. The client reads hasNodePty to decide
6451
* whether to render the local-terminal tab button, the sys-menu entry and the
6552
* 'local' bookmark type at all; advertising a terminal that only fails later at
6653
* spawn time leaves tabs stuck on "connecting", which is much worse than never
6754
* offering it.
6855
*/
69-
function localTerminalAvailable () {
70-
if (process.env.DISABLE_LOCAL_TERMINAL) {
71-
return false
72-
}
73-
const candidates = [
74-
process.env.ELECTERM_PTY_ADDON,
75-
new URL('./node_modules/node-pty/build/Release/pty.node', import.meta.url)
76-
]
77-
return candidates.some(p => {
78-
if (!p) {
79-
return false
80-
}
81-
try {
82-
return fs.existsSync(p)
83-
} catch (_) {
84-
return false
85-
}
86-
})
56+
async function localTerminalAvailable () {
57+
const cap = await localTerminalCapability()
58+
return cap.available
8759
}
8860

8961
export async function index (req, res) {
9062
const server = process.env.SERVER || (isDev ? buildServer() : '')
9163
const cdn = process.env.CDN || server
92-
const hasNodePty = localTerminalAvailable()
64+
const hasNodePty = await localTerminalAvailable()
9365
// All session types the app knows about. 'local' joins the list only when the
9466
// local terminal is genuinely usable — the bookmark form filters its type
9567
// radios through this list, so an unavailable local terminal is never offered.

0 commit comments

Comments
 (0)