webui: support mount master mode

This commit is contained in:
weishu
2024-02-29 13:55:02 +08:00
parent f459dfad54
commit 922703d2ff
2 changed files with 10 additions and 5 deletions

View File

@@ -16,6 +16,7 @@ Spawns a **root** shell and runs a command within that shell, passing the `stdou
- `options` `<Object>`
- `cwd` - Current working directory of the child process
- `env` - Environment key-value pairs
- `mm` `boolean` - Mount master mode, true to enter global mount namespace
```javascript
import { exec } from 'kernelsu';
@@ -38,6 +39,7 @@ Returns a `ChildProcess`, Instances of the ChildProcess represent spawned child
- `options` `<Object>`:
- `cwd` `<string>` - Current working directory of the child process
- `env` `<Object>` - Environment key-value pairs
- `mm` `boolean` - Mount master mode, true to enter global mount namespace
Example of running `ls -lh /data`, capturing `stdout`, `stderr`, and the exit code:

View File

@@ -36,7 +36,7 @@ class WebViewInterface(val context: Context, private val webView: WebView) {
exec(cmd, null, callbackFunc)
}
private fun processOptions(sb: StringBuilder, options: String?) {
private fun processOptions(sb: StringBuilder, options: String?): JSONObject {
val opts = if (options == null) JSONObject() else {
JSONObject(options)
}
@@ -51,6 +51,7 @@ class WebViewInterface(val context: Context, private val webView: WebView) {
sb.append("export ${key}=${env.getString(key)};")
}
}
return opts
}
@JavascriptInterface
@@ -60,10 +61,11 @@ class WebViewInterface(val context: Context, private val webView: WebView) {
callbackFunc: String
) {
val finalCommand = StringBuilder()
processOptions(finalCommand, options)
val opts = processOptions(finalCommand, options)
finalCommand.append(cmd)
val shell = createRootShell()
val mountMaster = opts.optBoolean("mm", false)
val shell = createRootShell(mountMaster)
val result = shell.newJob().add(finalCommand.toString()).to(ArrayList(), ArrayList()).exec()
val stdout = result.out.joinToString(separator = "\n")
val stderr = result.err.joinToString(separator = "\n")
@@ -83,7 +85,7 @@ class WebViewInterface(val context: Context, private val webView: WebView) {
fun spawn(command: String, args: String, options: String?, callbackFunc: String) {
val finalCommand = StringBuilder()
processOptions(finalCommand, options)
val opts = processOptions(finalCommand, options)
if (!TextUtils.isEmpty(args)) {
finalCommand.append(command).append(" ")
@@ -97,7 +99,8 @@ class WebViewInterface(val context: Context, private val webView: WebView) {
finalCommand.append(command)
}
val shell = createRootShell()
val mountMaster = opts.optBoolean("mm", false)
val shell = createRootShell(mountMaster)
val emitData = fun(name: String, data: String) {
val jsCode =