@ng-native/desktop 0.0.0-stage → 0.23.0
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/LICENSE +21 -0
- package/README.md +51 -2
- package/app.plugin.cjs +17 -0
- package/bin/app-config.mjs +168 -0
- package/bin/autolink.mjs +224 -0
- package/bin/cli.mjs +158 -0
- package/bin/host.mjs +427 -0
- package/bin/ng-native-desktop.mjs +4 -0
- package/bin/package.mjs +971 -0
- package/bin/plist.mjs +68 -0
- package/bin/prebuilt.mjs +328 -0
- package/bin/toolchain.mjs +192 -0
- package/native/CMakeLists.txt +276 -0
- package/native/appkit/CMakeLists.txt +109 -0
- package/native/appkit/address-search.h +26 -0
- package/native/appkit/address-search.mm +277 -0
- package/native/appkit/animation-frames.h +14 -0
- package/native/appkit/animation-frames.mm +106 -0
- package/native/appkit/app-info.h +12 -0
- package/native/appkit/app-info.mm +98 -0
- package/native/appkit/app-updater.mm +128 -0
- package/native/appkit/app-windows.h +81 -0
- package/native/appkit/app-windows.mm +318 -0
- package/native/appkit/appkit-backend.h +73 -0
- package/native/appkit/appkit-backend.mm +276 -0
- package/native/appkit/apple-authentication.h +19 -0
- package/native/appkit/apple-authentication.mm +248 -0
- package/native/appkit/apple-button-view.h +10 -0
- package/native/appkit/apple-button-view.mm +37 -0
- package/native/appkit/apple-id-button.h +16 -0
- package/native/appkit/apple-id-button.mm +50 -0
- package/native/appkit/audio.h +16 -0
- package/native/appkit/audio.mm +275 -0
- package/native/appkit/battery.h +13 -0
- package/native/appkit/battery.mm +92 -0
- package/native/appkit/bookmarks.h +17 -0
- package/native/appkit/bookmarks.mm +145 -0
- package/native/appkit/calendar.h +25 -0
- package/native/appkit/calendar.mm +660 -0
- package/native/appkit/camera-view.h +15 -0
- package/native/appkit/camera-view.mm +130 -0
- package/native/appkit/camera.h +39 -0
- package/native/appkit/camera.mm +362 -0
- package/native/appkit/cg-canvas.h +17 -0
- package/native/appkit/cg-canvas.mm +518 -0
- package/native/appkit/component-view.h +17 -0
- package/native/appkit/crypto.h +19 -0
- package/native/appkit/crypto.mm +78 -0
- package/native/appkit/desktop-expo.h +29 -0
- package/native/appkit/desktop-expo.mm +385 -0
- package/native/appkit/expo-image-drawing.h +46 -0
- package/native/appkit/expo-image-drawing.mm +131 -0
- package/native/appkit/expo-image-view.h +15 -0
- package/native/appkit/expo-image-view.mm +182 -0
- package/native/appkit/expo-ui-renderer.h +68 -0
- package/native/appkit/expo-ui-renderer.mm +101 -0
- package/native/appkit/expo-ui-view.h +26 -0
- package/native/appkit/expo-ui-view.mm +134 -0
- package/native/appkit/expo-ui.swift +1284 -0
- package/native/appkit/file-system.h +16 -0
- package/native/appkit/file-system.mm +140 -0
- package/native/appkit/fonts.h +12 -0
- package/native/appkit/fonts.mm +48 -0
- package/native/appkit/geocoder.h +19 -0
- package/native/appkit/geocoder.mm +101 -0
- package/native/appkit/glass-effect.h +22 -0
- package/native/appkit/glass-effect.mm +63 -0
- package/native/appkit/glass-view.h +8 -0
- package/native/appkit/glass-view.mm +65 -0
- package/native/appkit/header-toolbar.h +60 -0
- package/native/appkit/header-toolbar.mm +629 -0
- package/native/appkit/host-view.h +54 -0
- package/native/appkit/host-view.mm +1200 -0
- package/native/appkit/image-manipulator.h +14 -0
- package/native/appkit/image-manipulator.mm +191 -0
- package/native/appkit/image-request.h +22 -0
- package/native/appkit/image-request.mm +62 -0
- package/native/appkit/image-view.h +8 -0
- package/native/appkit/image-view.mm +142 -0
- package/native/appkit/input-replay.h +25 -0
- package/native/appkit/input-replay.mm +418 -0
- package/native/appkit/language-model.h +14 -0
- package/native/appkit/language-model.mm +99 -0
- package/native/appkit/language-model.swift +472 -0
- package/native/appkit/local-authentication.h +15 -0
- package/native/appkit/local-authentication.mm +133 -0
- package/native/appkit/location.h +16 -0
- package/native/appkit/location.mm +214 -0
- package/native/appkit/main.mm +1515 -0
- package/native/appkit/map-view.h +15 -0
- package/native/appkit/map-view.mm +109 -0
- package/native/appkit/maps.h +35 -0
- package/native/appkit/maps.mm +343 -0
- package/native/appkit/menu-bar.h +22 -0
- package/native/appkit/menu-bar.mm +208 -0
- package/native/appkit/modal-focus.h +16 -0
- package/native/appkit/modal-focus.mm +26 -0
- package/native/appkit/modal-host-view.h +79 -0
- package/native/appkit/modal-host-view.mm +442 -0
- package/native/appkit/native-controls.h +22 -0
- package/native/appkit/native-controls.mm +431 -0
- package/native/appkit/network-state.h +12 -0
- package/native/appkit/network-state.mm +64 -0
- package/native/appkit/network.h +14 -0
- package/native/appkit/network.mm +396 -0
- package/native/appkit/notifications.h +19 -0
- package/native/appkit/notifications.mm +309 -0
- package/native/appkit/photo-library.h +33 -0
- package/native/appkit/photo-library.mm +548 -0
- package/native/appkit/pointer-buttons.h +32 -0
- package/native/appkit/presentation-label.h +15 -0
- package/native/appkit/presentation-label.mm +12 -0
- package/native/appkit/presented-screen.h +19 -0
- package/native/appkit/presented-screen.mm +181 -0
- package/native/appkit/printing.h +42 -0
- package/native/appkit/printing.mm +393 -0
- package/native/appkit/push.h +19 -0
- package/native/appkit/push.mm +81 -0
- package/native/appkit/red-box.h +26 -0
- package/native/appkit/red-box.mm +387 -0
- package/native/appkit/screen-capture.h +21 -0
- package/native/appkit/screen-capture.mm +92 -0
- package/native/appkit/scroll-view.h +13 -0
- package/native/appkit/scroll-view.mm +251 -0
- package/native/appkit/scrollers.h +25 -0
- package/native/appkit/scrollers.mm +43 -0
- package/native/appkit/search-field.h +34 -0
- package/native/appkit/search-field.mm +291 -0
- package/native/appkit/search-toolbar-item.h +19 -0
- package/native/appkit/search-toolbar-item.mm +269 -0
- package/native/appkit/share-picker.h +34 -0
- package/native/appkit/share-picker.mm +121 -0
- package/native/appkit/split-view.h +77 -0
- package/native/appkit/split-view.mm +1206 -0
- package/native/appkit/store-review.h +13 -0
- package/native/appkit/store-review.mm +28 -0
- package/native/appkit/surface-view.h +45 -0
- package/native/appkit/surface-view.mm +558 -0
- package/native/appkit/svg-view.h +17 -0
- package/native/appkit/svg-view.mm +57 -0
- package/native/appkit/symbol-drawing.h +24 -0
- package/native/appkit/symbol-drawing.mm +117 -0
- package/native/appkit/symbol-view.h +8 -0
- package/native/appkit/symbol-view.mm +33 -0
- package/native/appkit/system-access.h +34 -0
- package/native/appkit/system-access.mm +210 -0
- package/native/appkit/system.h +24 -0
- package/native/appkit/system.mm +403 -0
- package/native/appkit/test-launch.h +36 -0
- package/native/appkit/test-launch.mm +115 -0
- package/native/appkit/text-input-field.h +76 -0
- package/native/appkit/text-input-field.mm +444 -0
- package/native/appkit/text-layout.h +58 -0
- package/native/appkit/text-layout.mm +477 -0
- package/native/appkit/text-recognition.h +13 -0
- package/native/appkit/text-recognition.mm +102 -0
- package/native/appkit/tracking.h +13 -0
- package/native/appkit/tracking.mm +51 -0
- package/native/appkit/tray-and-dock.h +45 -0
- package/native/appkit/tray-and-dock.mm +130 -0
- package/native/appkit/undo.h +23 -0
- package/native/appkit/undo.mm +125 -0
- package/native/appkit/updates.h +17 -0
- package/native/appkit/updates.mm +71 -0
- package/native/appkit/video-view.h +10 -0
- package/native/appkit/video-view.mm +42 -0
- package/native/appkit/video.h +38 -0
- package/native/appkit/video.mm +404 -0
- package/native/appkit/web-browser.h +12 -0
- package/native/appkit/web-browser.mm +97 -0
- package/native/appkit/web-view-component.h +9 -0
- package/native/appkit/web-view-component.mm +56 -0
- package/native/appkit/web-view.h +31 -0
- package/native/appkit/web-view.mm +394 -0
- package/native/cmake/libraries/react-native-reanimated.cmake +45 -0
- package/native/cmake/libraries/react-native-worklets.cmake +46 -0
- package/native/cmake/libraries.cmake +109 -0
- package/native/cmake/linked-modules.cmake +112 -0
- package/native/cmake/react-native.cmake +175 -0
- package/native/cmake/runtime-dependencies.cmake +34 -0
- package/native/cmake/swift.cmake +43 -0
- package/native/cmake/third-party.cmake +74 -0
- package/native/core/include/ngn/absent-views.h +27 -0
- package/native/core/include/ngn/accessibility.h +140 -0
- package/native/core/include/ngn/address-search.h +69 -0
- package/native/core/include/ngn/animation-frames.h +39 -0
- package/native/core/include/ngn/app-info.h +52 -0
- package/native/core/include/ngn/app-menus.h +146 -0
- package/native/core/include/ngn/app-windows.h +181 -0
- package/native/core/include/ngn/apple-authentication.h +116 -0
- package/native/core/include/ngn/asset.h +28 -0
- package/native/core/include/ngn/audio.h +125 -0
- package/native/core/include/ngn/back-swipe.h +112 -0
- package/native/core/include/ngn/battery.h +48 -0
- package/native/core/include/ngn/bytes.h +59 -0
- package/native/core/include/ngn/calendar.h +180 -0
- package/native/core/include/ngn/camera.h +128 -0
- package/native/core/include/ngn/control-sizes.h +111 -0
- package/native/core/include/ngn/core-modules.h +105 -0
- package/native/core/include/ngn/crypto.h +38 -0
- package/native/core/include/ngn/default-menu.h +91 -0
- package/native/core/include/ngn/desktop-expo.h +193 -0
- package/native/core/include/ngn/desktop-modal.h +109 -0
- package/native/core/include/ngn/desktop-props.h +75 -0
- package/native/core/include/ngn/desktop-view-props.h +110 -0
- package/native/core/include/ngn/dev-server.h +33 -0
- package/native/core/include/ngn/download.h +24 -0
- package/native/core/include/ngn/expo-image.h +192 -0
- package/native/core/include/ngn/expo-ui.h +133 -0
- package/native/core/include/ngn/file-system.h +274 -0
- package/native/core/include/ngn/focus-order.h +63 -0
- package/native/core/include/ngn/fonts.h +64 -0
- package/native/core/include/ngn/gestures.h +305 -0
- package/native/core/include/ngn/glass.h +67 -0
- package/native/core/include/ngn/header-bar.h +112 -0
- package/native/core/include/ngn/host-state.h +243 -0
- package/native/core/include/ngn/image-manipulator.h +70 -0
- package/native/core/include/ngn/input-dispatcher.h +211 -0
- package/native/core/include/ngn/input-script.h +56 -0
- package/native/core/include/ngn/input.h +112 -0
- package/native/core/include/ngn/key-value-store.h +41 -0
- package/native/core/include/ngn/language-model.h +69 -0
- package/native/core/include/ngn/launch-frame.h +47 -0
- package/native/core/include/ngn/launch-settings.h +98 -0
- package/native/core/include/ngn/layout-animations.h +24 -0
- package/native/core/include/ngn/library-components.h +13 -0
- package/native/core/include/ngn/library-glue.h +53 -0
- package/native/core/include/ngn/live-regions.h +54 -0
- package/native/core/include/ngn/local-authentication.h +74 -0
- package/native/core/include/ngn/location.h +89 -0
- package/native/core/include/ngn/maps.h +226 -0
- package/native/core/include/ngn/module-registry.h +51 -0
- package/native/core/include/ngn/mounting-backend.h +96 -0
- package/native/core/include/ngn/mounting-manager.h +155 -0
- package/native/core/include/ngn/native-controls.h +161 -0
- package/native/core/include/ngn/native-module.h +109 -0
- package/native/core/include/ngn/network-state.h +44 -0
- package/native/core/include/ngn/network.h +100 -0
- package/native/core/include/ngn/notifications.h +185 -0
- package/native/core/include/ngn/photo-library.h +130 -0
- package/native/core/include/ngn/printing.h +69 -0
- package/native/core/include/ngn/processes.h +114 -0
- package/native/core/include/ngn/red-box.h +119 -0
- package/native/core/include/ngn/reload-thread.h +258 -0
- package/native/core/include/ngn/runtime.h +161 -0
- package/native/core/include/ngn/safe-areas.h +77 -0
- package/native/core/include/ngn/screen-capture.h +35 -0
- package/native/core/include/ngn/screen-presentation.h +85 -0
- package/native/core/include/ngn/screen-stacks.h +80 -0
- package/native/core/include/ngn/scrollers.h +106 -0
- package/native/core/include/ngn/sharing.h +39 -0
- package/native/core/include/ngn/spawn-rules.h +78 -0
- package/native/core/include/ngn/splash-screen.h +41 -0
- package/native/core/include/ngn/split-view.h +270 -0
- package/native/core/include/ngn/sqlite.h +38 -0
- package/native/core/include/ngn/store-review.h +26 -0
- package/native/core/include/ngn/surface-book.h +101 -0
- package/native/core/include/ngn/svg.h +393 -0
- package/native/core/include/ngn/symbol.h +90 -0
- package/native/core/include/ngn/system.h +102 -0
- package/native/core/include/ngn/tabs-host.h +171 -0
- package/native/core/include/ngn/text-input.h +125 -0
- package/native/core/include/ngn/text-measurer.h +38 -0
- package/native/core/include/ngn/text-recognition.h +55 -0
- package/native/core/include/ngn/tracking.h +34 -0
- package/native/core/include/ngn/tray-and-dock.h +107 -0
- package/native/core/include/ngn/undo.h +37 -0
- package/native/core/include/ngn/updates.h +253 -0
- package/native/core/include/ngn/video.h +126 -0
- package/native/core/include/ngn/view-box.h +126 -0
- package/native/core/include/ngn/view-transform.h +26 -0
- package/native/core/include/ngn/view-tree.h +85 -0
- package/native/core/include/ngn/web-browser.h +45 -0
- package/native/core/include/ngn/web-view.h +133 -0
- package/native/core/include/ngn/worklet-host.h +98 -0
- package/native/core/src/absent-views.cpp +41 -0
- package/native/core/src/accessibility.cpp +374 -0
- package/native/core/src/animation-frames.cpp +49 -0
- package/native/core/src/app-menus.cpp +352 -0
- package/native/core/src/app-windows.cpp +444 -0
- package/native/core/src/back-swipe.cpp +91 -0
- package/native/core/src/bytes.cpp +158 -0
- package/native/core/src/control-sizes.cpp +232 -0
- package/native/core/src/default-menu.cpp +96 -0
- package/native/core/src/desktop-modal.cpp +162 -0
- package/native/core/src/desktop-view-props.cpp +80 -0
- package/native/core/src/dev-server-helper.cpp +152 -0
- package/native/core/src/dev-server.cpp +103 -0
- package/native/core/src/download.cpp +53 -0
- package/native/core/src/expo-image.cpp +548 -0
- package/native/core/src/expo-ui.cpp +203 -0
- package/native/core/src/focus-order.cpp +441 -0
- package/native/core/src/gestures.cpp +1806 -0
- package/native/core/src/glass.cpp +49 -0
- package/native/core/src/header-bar.cpp +104 -0
- package/native/core/src/host-services.cpp +50 -0
- package/native/core/src/host-state.cpp +61 -0
- package/native/core/src/input-dispatcher.cpp +727 -0
- package/native/core/src/input-script.cpp +123 -0
- package/native/core/src/key-value-store.cpp +169 -0
- package/native/core/src/launch-settings.cpp +148 -0
- package/native/core/src/layout-animations.cpp +62 -0
- package/native/core/src/libraries/RNCWebViewSpec.cpp +80 -0
- package/native/core/src/libraries/react-native-reanimated.cpp +216 -0
- package/native/core/src/libraries/react-native-worklets.cpp +192 -0
- package/native/core/src/libraries/rnscreens.cpp +125 -0
- package/native/core/src/libraries/rnsvg.cpp +345 -0
- package/native/core/src/libraries/safeareacontext.cpp +70 -0
- package/native/core/src/libraries/worklets-idle-frames.js +70 -0
- package/native/core/src/library-glue.cpp +35 -0
- package/native/core/src/live-regions.cpp +122 -0
- package/native/core/src/module-registry.cpp +50 -0
- package/native/core/src/modules/accessibility-manager.cpp +98 -0
- package/native/core/src/modules/address-search.cpp +219 -0
- package/native/core/src/modules/alert-manager.cpp +140 -0
- package/native/core/src/modules/app-info.cpp +74 -0
- package/native/core/src/modules/app-modules.cpp +405 -0
- package/native/core/src/modules/apple-authentication.cpp +188 -0
- package/native/core/src/modules/asset.cpp +111 -0
- package/native/core/src/modules/async-storage.cpp +192 -0
- package/native/core/src/modules/audio.cpp +541 -0
- package/native/core/src/modules/battery.cpp +88 -0
- package/native/core/src/modules/calendar.cpp +457 -0
- package/native/core/src/modules/camera.cpp +221 -0
- package/native/core/src/modules/core-modules.cpp +62 -0
- package/native/core/src/modules/crypto.cpp +117 -0
- package/native/core/src/modules/desktop-expo.cpp +527 -0
- package/native/core/src/modules/file-system.cpp +941 -0
- package/native/core/src/modules/fonts.cpp +163 -0
- package/native/core/src/modules/image-loader.cpp +105 -0
- package/native/core/src/modules/image-manipulator.cpp +305 -0
- package/native/core/src/modules/language-model.cpp +195 -0
- package/native/core/src/modules/local-authentication.cpp +140 -0
- package/native/core/src/modules/location.cpp +186 -0
- package/native/core/src/modules/maps.cpp +500 -0
- package/native/core/src/modules/network-state.cpp +72 -0
- package/native/core/src/modules/networking.cpp +387 -0
- package/native/core/src/modules/notifications.cpp +549 -0
- package/native/core/src/modules/photo-library.cpp +371 -0
- package/native/core/src/modules/printing.cpp +201 -0
- package/native/core/src/modules/screen-capture.cpp +71 -0
- package/native/core/src/modules/sharing.cpp +59 -0
- package/native/core/src/modules/splash-screen.cpp +70 -0
- package/native/core/src/modules/sqlite.cpp +421 -0
- package/native/core/src/modules/store-review.cpp +23 -0
- package/native/core/src/modules/text-recognition.cpp +107 -0
- package/native/core/src/modules/tracking.cpp +46 -0
- package/native/core/src/modules/undo.cpp +59 -0
- package/native/core/src/modules/video.cpp +366 -0
- package/native/core/src/modules/web-browser.cpp +100 -0
- package/native/core/src/modules/web-socket.cpp +266 -0
- package/native/core/src/mounting-manager.cpp +380 -0
- package/native/core/src/native-controls.cpp +129 -0
- package/native/core/src/native-module.cpp +250 -0
- package/native/core/src/network.cpp +219 -0
- package/native/core/src/posix-launcher.cpp +366 -0
- package/native/core/src/processes.cpp +324 -0
- package/native/core/src/red-box-errors.js +83 -0
- package/native/core/src/red-box.cpp +306 -0
- package/native/core/src/runtime.cpp +422 -0
- package/native/core/src/safe-areas.cpp +130 -0
- package/native/core/src/screen-presentation.cpp +61 -0
- package/native/core/src/screen-stacks.cpp +254 -0
- package/native/core/src/scrollers.cpp +181 -0
- package/native/core/src/spawn-rules.cpp +292 -0
- package/native/core/src/split-view.cpp +343 -0
- package/native/core/src/svg.cpp +1883 -0
- package/native/core/src/symbol.cpp +94 -0
- package/native/core/src/tabs-host.cpp +234 -0
- package/native/core/src/text-input.cpp +126 -0
- package/native/core/src/text-layout-manager.cpp +63 -0
- package/native/core/src/tray-and-dock.cpp +154 -0
- package/native/core/src/updates.cpp +965 -0
- package/native/core/src/view-box.cpp +788 -0
- package/native/core/src/view-transform.cpp +45 -0
- package/native/core/src/view-tree.cpp +234 -0
- package/native/core/src/web-view.cpp +143 -0
- package/native/core/src/worklet-host.cpp +10 -0
- package/native/scripts/build-hermes.sh +44 -0
- package/native/scripts/build.sh +171 -0
- package/native/scripts/cache-dir.sh +18 -0
- package/native/scripts/codegen-library.sh +193 -0
- package/native/scripts/compiler-launcher.sh +17 -0
- package/native/scripts/fetch.sh +174 -0
- package/native/scripts/hermes-built.sh +10 -0
- package/native/scripts/host-dir.sh +16 -0
- package/native/scripts/macos-target.sh +8 -0
- package/native/scripts/make-prebuilt.mjs +360 -0
- package/native/scripts/prebuilt-host-cache.mjs +158 -0
- package/native/shim/openssl/sha.h +22 -0
- package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +37 -0
- package/package.json +70 -3
- package/prebuilt.json +33 -0
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
// ProcessLauncher over posix_spawn: no shell, the environment given and nothing else, a pipe for
|
|
2
|
+
// each standard stream read on threads of their own, and the exit reported once both pipes close.
|
|
3
|
+
// Stdin is non-blocking and never raises SIGPIPE: a write queues what the pipe cannot take yet, and
|
|
4
|
+
// a thread of the program's own drains it, so the JavaScript thread never waits on a program.
|
|
5
|
+
#include "ngn/processes.h"
|
|
6
|
+
|
|
7
|
+
#include <cerrno>
|
|
8
|
+
#include <csignal>
|
|
9
|
+
#include <cstring>
|
|
10
|
+
#include <deque>
|
|
11
|
+
#include <poll.h>
|
|
12
|
+
#include <fcntl.h>
|
|
13
|
+
#include <mutex>
|
|
14
|
+
#include <spawn.h>
|
|
15
|
+
#include <sys/wait.h>
|
|
16
|
+
#include <thread>
|
|
17
|
+
#include <unistd.h>
|
|
18
|
+
#include <pthread.h>
|
|
19
|
+
|
|
20
|
+
namespace {
|
|
21
|
+
|
|
22
|
+
// A write to a pipe whose reader has gone. macOS turns SIGPIPE off for the pipe with
|
|
23
|
+
// F_SETNOSIGPIPE; Linux has no such flag, so the signal is blocked on this thread for the write
|
|
24
|
+
// and, when the write raised it, taken off the thread before it is unblocked again.
|
|
25
|
+
ssize_t writeWithoutSigpipe(int fd, const char *data, size_t size) {
|
|
26
|
+
#ifdef F_SETNOSIGPIPE
|
|
27
|
+
return ::write(fd, data, size);
|
|
28
|
+
#else
|
|
29
|
+
sigset_t pipe;
|
|
30
|
+
sigemptyset(&pipe);
|
|
31
|
+
sigaddset(&pipe, SIGPIPE);
|
|
32
|
+
sigset_t pending;
|
|
33
|
+
sigpending(&pending);
|
|
34
|
+
bool alreadyPending = sigismember(&pending, SIGPIPE) == 1;
|
|
35
|
+
sigset_t old;
|
|
36
|
+
pthread_sigmask(SIG_BLOCK, &pipe, &old);
|
|
37
|
+
auto n = ::write(fd, data, size);
|
|
38
|
+
int saved = errno;
|
|
39
|
+
if (n < 0 && saved == EPIPE && !alreadyPending) {
|
|
40
|
+
timespec none{0, 0};
|
|
41
|
+
while (sigtimedwait(&pipe, nullptr, &none) == -1 && errno == EINTR) {
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
pthread_sigmask(SIG_SETMASK, &old, nullptr);
|
|
45
|
+
errno = saved;
|
|
46
|
+
return n;
|
|
47
|
+
#endif
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
} // namespace
|
|
51
|
+
|
|
52
|
+
namespace ngn {
|
|
53
|
+
|
|
54
|
+
namespace {
|
|
55
|
+
|
|
56
|
+
class PosixLauncher final : public ProcessLauncher, public std::enable_shared_from_this<PosixLauncher> {
|
|
57
|
+
public:
|
|
58
|
+
void setEvents(Events events) override {
|
|
59
|
+
std::lock_guard lock(mutex_);
|
|
60
|
+
events_ = std::make_shared<Events>(std::move(events));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
std::variant<int, std::string> spawn(
|
|
64
|
+
const std::string &path,
|
|
65
|
+
const std::vector<std::string> &args,
|
|
66
|
+
const std::optional<std::string> &cwd,
|
|
67
|
+
const std::map<std::string, std::string> &env) override {
|
|
68
|
+
int in[2] = {-1, -1}, out[2] = {-1, -1}, err[2] = {-1, -1};
|
|
69
|
+
if (pipe(in) != 0 || pipe(out) != 0 || pipe(err) != 0) {
|
|
70
|
+
int error = errno;
|
|
71
|
+
// The pipes made before the one that failed.
|
|
72
|
+
for (int fd : {in[0], in[1], out[0], out[1], err[0], err[1]}) {
|
|
73
|
+
if (fd != -1) {
|
|
74
|
+
close(fd);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return std::string("could not make pipes: ") + std::strerror(error);
|
|
78
|
+
}
|
|
79
|
+
for (int fd : {in[1], out[0], err[0]}) {
|
|
80
|
+
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
81
|
+
}
|
|
82
|
+
fcntl(in[1], F_SETFL, fcntl(in[1], F_GETFL) | O_NONBLOCK);
|
|
83
|
+
#ifdef F_SETNOSIGPIPE
|
|
84
|
+
// A write to a program that closed its stdin fails with EPIPE rather than ending the host.
|
|
85
|
+
fcntl(in[1], F_SETNOSIGPIPE, 1);
|
|
86
|
+
#endif
|
|
87
|
+
posix_spawn_file_actions_t actions;
|
|
88
|
+
posix_spawn_file_actions_init(&actions);
|
|
89
|
+
posix_spawn_file_actions_adddup2(&actions, in[0], 0);
|
|
90
|
+
posix_spawn_file_actions_adddup2(&actions, out[1], 1);
|
|
91
|
+
posix_spawn_file_actions_adddup2(&actions, err[1], 2);
|
|
92
|
+
// The originals closed once copied, where nothing closes them on exec (Linux): an extra reader
|
|
93
|
+
// of stdin keeps a write from failing, and an extra writer of an output delays its end.
|
|
94
|
+
for (int fd : {in[0], out[1], err[1]}) {
|
|
95
|
+
if (fd > 2) {
|
|
96
|
+
posix_spawn_file_actions_addclose(&actions, fd);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (cwd) {
|
|
100
|
+
posix_spawn_file_actions_addchdir_np(&actions, cwd->c_str());
|
|
101
|
+
}
|
|
102
|
+
posix_spawnattr_t attributes;
|
|
103
|
+
posix_spawnattr_init(&attributes);
|
|
104
|
+
sigset_t none, all;
|
|
105
|
+
sigemptyset(&none);
|
|
106
|
+
sigfillset(&all);
|
|
107
|
+
posix_spawnattr_setsigmask(&attributes, &none);
|
|
108
|
+
posix_spawnattr_setsigdefault(&attributes, &all);
|
|
109
|
+
short flags = POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF;
|
|
110
|
+
#ifdef __APPLE__
|
|
111
|
+
// Only the three streams reach the program, none of the host's other descriptors.
|
|
112
|
+
flags |= POSIX_SPAWN_CLOEXEC_DEFAULT;
|
|
113
|
+
#endif
|
|
114
|
+
posix_spawnattr_setflags(&attributes, flags);
|
|
115
|
+
|
|
116
|
+
std::vector<std::string> argStrings{path};
|
|
117
|
+
argStrings.insert(argStrings.end(), args.begin(), args.end());
|
|
118
|
+
std::vector<char *> argv;
|
|
119
|
+
for (auto &a : argStrings) {
|
|
120
|
+
argv.push_back(a.data());
|
|
121
|
+
}
|
|
122
|
+
argv.push_back(nullptr);
|
|
123
|
+
std::vector<std::string> envStrings;
|
|
124
|
+
for (const auto &[name, value] : env) {
|
|
125
|
+
envStrings.push_back(name + "=" + value);
|
|
126
|
+
}
|
|
127
|
+
std::vector<char *> envp;
|
|
128
|
+
for (auto &e : envStrings) {
|
|
129
|
+
envp.push_back(e.data());
|
|
130
|
+
}
|
|
131
|
+
envp.push_back(nullptr);
|
|
132
|
+
|
|
133
|
+
pid_t pid = 0;
|
|
134
|
+
int status = posix_spawn(&pid, path.c_str(), &actions, &attributes, argv.data(), envp.data());
|
|
135
|
+
posix_spawn_file_actions_destroy(&actions);
|
|
136
|
+
posix_spawnattr_destroy(&attributes);
|
|
137
|
+
close(in[0]);
|
|
138
|
+
close(out[1]);
|
|
139
|
+
close(err[1]);
|
|
140
|
+
if (status != 0) {
|
|
141
|
+
close(in[1]);
|
|
142
|
+
close(out[0]);
|
|
143
|
+
close(err[0]);
|
|
144
|
+
return path + " did not start: " + std::strerror(status);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
int handle;
|
|
148
|
+
std::shared_ptr<Events> events;
|
|
149
|
+
{
|
|
150
|
+
std::lock_guard lock(mutex_);
|
|
151
|
+
handle = ++next_;
|
|
152
|
+
running_[handle] = {pid, std::make_shared<Stdin>(in[1])};
|
|
153
|
+
events = events_;
|
|
154
|
+
}
|
|
155
|
+
auto self = shared_from_this();
|
|
156
|
+
std::thread([self, handle, pid, outFd = out[0], errFd = err[0], events] {
|
|
157
|
+
std::thread errReader([&] { read(errFd, handle, true, events); });
|
|
158
|
+
read(outFd, handle, false, events);
|
|
159
|
+
errReader.join();
|
|
160
|
+
int status = reapAfter(pid, [&] { self->finished(handle); });
|
|
161
|
+
if (events && events->exit) {
|
|
162
|
+
if (WIFSIGNALED(status)) {
|
|
163
|
+
events->exit(handle, std::nullopt, signalName(WTERMSIG(status)));
|
|
164
|
+
} else {
|
|
165
|
+
events->exit(handle, WEXITSTATUS(status), std::nullopt);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}).detach();
|
|
169
|
+
return handle;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
void write(int handle, const std::string &data) override {
|
|
173
|
+
if (auto in = stdinOf(handle)) {
|
|
174
|
+
in->write(data);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
void closeStdin(int handle) override {
|
|
179
|
+
if (auto in = stdinOf(handle)) {
|
|
180
|
+
in->close();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
int lastHandle() const override {
|
|
185
|
+
std::lock_guard lock(mutex_);
|
|
186
|
+
return next_;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
void killAll(int signal) override {
|
|
190
|
+
std::lock_guard lock(mutex_);
|
|
191
|
+
for (const auto &[handle, running] : running_) {
|
|
192
|
+
::kill(running.pid, signal);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
void kill(int handle, int signal) override {
|
|
197
|
+
std::lock_guard lock(mutex_);
|
|
198
|
+
if (auto it = running_.find(handle); it != running_.end()) {
|
|
199
|
+
::kill(it->second.pid, signal);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private:
|
|
204
|
+
// A program's stdin: what the pipe has not taken yet, written by a thread of its own that runs
|
|
205
|
+
// only while there is some. Closing waits for that to drain; a broken pipe drops the rest.
|
|
206
|
+
class Stdin : public std::enable_shared_from_this<Stdin> {
|
|
207
|
+
public:
|
|
208
|
+
explicit Stdin(int fd) : fd_(fd) {}
|
|
209
|
+
~Stdin() {
|
|
210
|
+
if (fd_ >= 0) {
|
|
211
|
+
::close(fd_);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
void write(const std::string &data) {
|
|
216
|
+
std::lock_guard lock(mutex_);
|
|
217
|
+
if (fd_ < 0 || closing_ || data.empty()) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
pending_.push_back(data);
|
|
221
|
+
flushLocked();
|
|
222
|
+
if (!pending_.empty() && !draining_) {
|
|
223
|
+
draining_ = true;
|
|
224
|
+
std::thread([self = shared_from_this()] { self->drain(); }).detach();
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
void close() {
|
|
229
|
+
std::lock_guard lock(mutex_);
|
|
230
|
+
closing_ = true;
|
|
231
|
+
if (pending_.empty() && fd_ >= 0) {
|
|
232
|
+
::close(fd_);
|
|
233
|
+
fd_ = -1;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private:
|
|
238
|
+
// Writes what the pipe takes now, without blocking.
|
|
239
|
+
void flushLocked() {
|
|
240
|
+
while (!pending_.empty() && fd_ >= 0) {
|
|
241
|
+
auto &front = pending_.front();
|
|
242
|
+
auto n = writeWithoutSigpipe(fd_, front.data() + offset_, front.size() - offset_);
|
|
243
|
+
if (n < 0 && errno == EINTR) {
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
if (n <= 0) {
|
|
250
|
+
// EPIPE: the program stopped reading. What is left can never be read.
|
|
251
|
+
pending_.clear();
|
|
252
|
+
offset_ = 0;
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
offset_ += static_cast<size_t>(n);
|
|
256
|
+
if (offset_ == front.size()) {
|
|
257
|
+
pending_.pop_front();
|
|
258
|
+
offset_ = 0;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
void drain() {
|
|
264
|
+
while (true) {
|
|
265
|
+
int fd;
|
|
266
|
+
{
|
|
267
|
+
std::lock_guard lock(mutex_);
|
|
268
|
+
flushLocked();
|
|
269
|
+
if (pending_.empty() || fd_ < 0) {
|
|
270
|
+
draining_ = false;
|
|
271
|
+
if (closing_ && fd_ >= 0) {
|
|
272
|
+
::close(fd_);
|
|
273
|
+
fd_ = -1;
|
|
274
|
+
}
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
fd = fd_;
|
|
278
|
+
}
|
|
279
|
+
// Waits for room in the pipe, or for the program to go, outside the lock.
|
|
280
|
+
pollfd ready{.fd = fd, .events = POLLOUT, .revents = 0};
|
|
281
|
+
poll(&ready, 1, 1000);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
std::mutex mutex_;
|
|
286
|
+
int fd_;
|
|
287
|
+
std::deque<std::string> pending_;
|
|
288
|
+
size_t offset_{0};
|
|
289
|
+
bool closing_{false};
|
|
290
|
+
bool draining_{false};
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
struct Running {
|
|
294
|
+
pid_t pid;
|
|
295
|
+
std::shared_ptr<Stdin> in;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
std::shared_ptr<Stdin> stdinOf(int handle) {
|
|
299
|
+
std::lock_guard lock(mutex_);
|
|
300
|
+
auto it = running_.find(handle);
|
|
301
|
+
return it == running_.end() ? nullptr : it->second.in;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
static void read(int fd, int handle, bool isStderr, const std::shared_ptr<Events> &events) {
|
|
305
|
+
char buffer[8192];
|
|
306
|
+
while (true) {
|
|
307
|
+
auto n = ::read(fd, buffer, sizeof buffer);
|
|
308
|
+
if (n < 0 && errno == EINTR) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (n <= 0) {
|
|
312
|
+
break;
|
|
313
|
+
}
|
|
314
|
+
if (events && events->output) {
|
|
315
|
+
events->output(handle, isStderr, std::string(buffer, static_cast<size_t>(n)));
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
close(fd);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
static std::string signalName(int signal) {
|
|
322
|
+
switch (signal) {
|
|
323
|
+
case SIGTERM: return "SIGTERM";
|
|
324
|
+
case SIGINT: return "SIGINT";
|
|
325
|
+
case SIGKILL: return "SIGKILL";
|
|
326
|
+
case SIGHUP: return "SIGHUP";
|
|
327
|
+
case SIGSEGV: return "SIGSEGV";
|
|
328
|
+
case SIGABRT: return "SIGABRT";
|
|
329
|
+
case SIGPIPE: return "SIGPIPE";
|
|
330
|
+
default: return "SIG" + std::to_string(signal);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
void finished(int handle) {
|
|
335
|
+
std::lock_guard lock(mutex_);
|
|
336
|
+
if (auto it = running_.find(handle); it != running_.end()) {
|
|
337
|
+
it->second.in->close();
|
|
338
|
+
running_.erase(it);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
mutable std::mutex mutex_;
|
|
343
|
+
std::shared_ptr<Events> events_;
|
|
344
|
+
std::map<int, Running> running_;
|
|
345
|
+
int next_{0};
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
} // namespace
|
|
349
|
+
|
|
350
|
+
int reapAfter(int pid, const std::function<void()> &forget) {
|
|
351
|
+
// WNOWAIT leaves the child a zombie, which keeps its pid from being reused until it is reaped.
|
|
352
|
+
siginfo_t info{};
|
|
353
|
+
while (waitid(P_PID, static_cast<id_t>(pid), &info, WEXITED | WNOWAIT) < 0 && errno == EINTR) {
|
|
354
|
+
}
|
|
355
|
+
forget();
|
|
356
|
+
int status = 0;
|
|
357
|
+
while (waitpid(pid, &status, 0) < 0 && errno == EINTR) {
|
|
358
|
+
}
|
|
359
|
+
return status;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
std::shared_ptr<ProcessLauncher> makePosixLauncher() {
|
|
363
|
+
return std::make_shared<PosixLauncher>();
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
} // namespace ngn
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
#include "ngn/processes.h"
|
|
2
|
+
|
|
3
|
+
#include <set>
|
|
4
|
+
|
|
5
|
+
#include <algorithm>
|
|
6
|
+
#include <cctype>
|
|
7
|
+
#include <csignal>
|
|
8
|
+
|
|
9
|
+
namespace ngn {
|
|
10
|
+
|
|
11
|
+
namespace {
|
|
12
|
+
|
|
13
|
+
std::vector<std::string> strings(const folly::dynamic &list) {
|
|
14
|
+
std::vector<std::string> out;
|
|
15
|
+
if (!list.isArray()) {
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
for (const auto &item : list) {
|
|
19
|
+
if (!item.isString()) {
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
out.push_back(item.asString());
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const folly::dynamic *field(const folly::dynamic &object, const char *name) {
|
|
28
|
+
if (!object.isObject()) {
|
|
29
|
+
return nullptr;
|
|
30
|
+
}
|
|
31
|
+
auto it = object.find(name);
|
|
32
|
+
return it == object.items().end() ? nullptr : &it->second;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
} // namespace
|
|
36
|
+
|
|
37
|
+
DeclaredCapabilities parseCapabilities(const folly::dynamic &capabilities) {
|
|
38
|
+
DeclaredCapabilities out;
|
|
39
|
+
if (const auto *spawn = field(capabilities, "spawn"); spawn && spawn->isArray()) {
|
|
40
|
+
for (const auto &entry : *spawn) {
|
|
41
|
+
const auto *program = field(entry, "program");
|
|
42
|
+
const auto *args = field(entry, "args");
|
|
43
|
+
if (program == nullptr || !program->isString() || (args != nullptr && !args->isNull() && !args->isArray())) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
out.spawn.push_back({program->asString(), args ? strings(*args) : std::vector<std::string>{}});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (const auto *env = field(capabilities, "env")) {
|
|
50
|
+
out.env = strings(*env);
|
|
51
|
+
}
|
|
52
|
+
if (const auto *paths = field(capabilities, "paths"); paths && paths->isArray()) {
|
|
53
|
+
out.paths = *paths;
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
std::optional<int> signalNamed(const std::string &name) {
|
|
59
|
+
if (name == "SIGTERM") {
|
|
60
|
+
return SIGTERM;
|
|
61
|
+
}
|
|
62
|
+
if (name == "SIGINT") {
|
|
63
|
+
return SIGINT;
|
|
64
|
+
}
|
|
65
|
+
if (name == "SIGKILL") {
|
|
66
|
+
return SIGKILL;
|
|
67
|
+
}
|
|
68
|
+
if (name == "SIGHUP") {
|
|
69
|
+
return SIGHUP;
|
|
70
|
+
}
|
|
71
|
+
return std::nullopt;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
std::shared_ptr<NativeModule> makeProcessesModule(std::shared_ptr<ProcessLauncher> launcher, ProcessesSetup setup) {
|
|
75
|
+
struct Processes : NativeModule {
|
|
76
|
+
std::vector<CompiledCapability> compiled;
|
|
77
|
+
std::map<std::string, std::optional<std::string>> resolved;
|
|
78
|
+
|
|
79
|
+
void emitEvent(folly::dynamic body) const {
|
|
80
|
+
emit("ngNativeProcesses", std::move(body));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// The programs this module started that have not exited: a reload stops them, since the
|
|
84
|
+
// JavaScript that held their handles is gone.
|
|
85
|
+
std::shared_ptr<ProcessLauncher> launcher;
|
|
86
|
+
std::mutex spawnedMutex;
|
|
87
|
+
std::set<int> spawned;
|
|
88
|
+
// Programs that exited before spawn() answered their handle, so started() never keeps them.
|
|
89
|
+
std::set<int> exitedEarly;
|
|
90
|
+
void exited(int handle) {
|
|
91
|
+
std::lock_guard lock(spawnedMutex);
|
|
92
|
+
if (spawned.erase(handle) == 0) {
|
|
93
|
+
exitedEarly.insert(handle);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
void started(int handle) {
|
|
97
|
+
std::lock_guard lock(spawnedMutex);
|
|
98
|
+
if (exitedEarly.erase(handle) == 0) {
|
|
99
|
+
spawned.insert(handle);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
~Processes() override {
|
|
103
|
+
std::lock_guard lock(spawnedMutex);
|
|
104
|
+
for (int handle : spawned) {
|
|
105
|
+
launcher->kill(handle, SIGTERM);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
Processes(std::shared_ptr<ProcessLauncher> launcher, ProcessesSetup setup)
|
|
110
|
+
: NativeModule("NgNativeProcesses"), launcher(launcher) {
|
|
111
|
+
if (setup.sandboxed) {
|
|
112
|
+
setup.capabilities.spawn.clear();
|
|
113
|
+
setup.capabilities.paths = folly::dynamic::array();
|
|
114
|
+
}
|
|
115
|
+
folly::dynamic declared = folly::dynamic::array();
|
|
116
|
+
for (const auto &capability : setup.capabilities.spawn) {
|
|
117
|
+
try {
|
|
118
|
+
compiled.push_back(compileCapability(capability));
|
|
119
|
+
} catch (const SpawnRuleError &) {
|
|
120
|
+
// A bad declaration the build should have caught: never allowed.
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (capability.program[0] != '/') {
|
|
124
|
+
resolved[capability.program] = resolveProgram(capability.program, setup.lookup);
|
|
125
|
+
}
|
|
126
|
+
folly::dynamic args = folly::dynamic::array();
|
|
127
|
+
for (const auto &arg : capability.args) {
|
|
128
|
+
args.push_back(arg);
|
|
129
|
+
}
|
|
130
|
+
declared.push_back(folly::dynamic::object("program", capability.program)("args", args));
|
|
131
|
+
}
|
|
132
|
+
auto envNames = setup.capabilities.env;
|
|
133
|
+
auto env = filterEnvironment(setup.environment, envNames);
|
|
134
|
+
constants([declared, envNames, env, resolved = resolved] {
|
|
135
|
+
folly::dynamic envObject = folly::dynamic::object();
|
|
136
|
+
for (const auto &[name, value] : env) {
|
|
137
|
+
envObject[name] = value;
|
|
138
|
+
}
|
|
139
|
+
folly::dynamic names = folly::dynamic::array();
|
|
140
|
+
for (const auto &name : envNames) {
|
|
141
|
+
names.push_back(name);
|
|
142
|
+
}
|
|
143
|
+
folly::dynamic pinned = folly::dynamic::object();
|
|
144
|
+
for (const auto &[name, path] : resolved) {
|
|
145
|
+
pinned[name] = path ? folly::dynamic(*path) : folly::dynamic(nullptr);
|
|
146
|
+
}
|
|
147
|
+
return folly::dynamic::object("env", envObject)("envNames", names)("spawn", declared)("resolved", pinned);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
promise("spawn", [this, launcher, envNames](const folly::dynamic &args, JsPromise promise) {
|
|
151
|
+
if (args.size() < 2 || !args[0].isString() || !args[1].isArray()) {
|
|
152
|
+
promise.reject("spawn takes a program and a list of arguments");
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
std::string program = args[0].asString();
|
|
156
|
+
std::vector<std::string> argv;
|
|
157
|
+
for (const auto &arg : args[1]) {
|
|
158
|
+
if (!arg.isString()) {
|
|
159
|
+
promise.reject("every argument must be a string");
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
argv.push_back(arg.asString());
|
|
163
|
+
}
|
|
164
|
+
auto path = matchSpawn(compiled, program, argv, resolved);
|
|
165
|
+
if (!path) {
|
|
166
|
+
promise.reject(program + " with these arguments matches no spawn capability in the app's declaration");
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
std::optional<std::string> cwd;
|
|
170
|
+
if (args.size() > 2 && args[2].isString()) {
|
|
171
|
+
cwd = args[2].asString();
|
|
172
|
+
if (!plainPath(*cwd)) {
|
|
173
|
+
promise.reject("the working directory must be an absolute, normalized path");
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
std::map<std::string, std::string> given;
|
|
178
|
+
if (args.size() > 3 && args[3].isObject()) {
|
|
179
|
+
for (const auto &[key, value] : args[3].items()) {
|
|
180
|
+
if (key.isString() && value.isString()) {
|
|
181
|
+
given[key.asString()] = value.asString();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
auto started = launcher->spawn(*path, argv, cwd, filterEnvironment(given, envNames));
|
|
186
|
+
if (const auto *handle = std::get_if<int>(&started)) {
|
|
187
|
+
this->started(*handle);
|
|
188
|
+
promise.resolve(*handle);
|
|
189
|
+
} else {
|
|
190
|
+
promise.reject(std::get<std::string>(started));
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
async("write", [launcher](const folly::dynamic &args, std::vector<JsCallback>) {
|
|
194
|
+
if (args.size() > 1 && args[0].isNumber() && args[1].isString()) {
|
|
195
|
+
launcher->write(static_cast<int>(args[0].asInt()), args[1].asString());
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
async("closeStdin", [launcher](const folly::dynamic &args, std::vector<JsCallback>) {
|
|
199
|
+
if (args.size() > 0 && args[0].isNumber()) {
|
|
200
|
+
launcher->closeStdin(static_cast<int>(args[0].asInt()));
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
async("kill", [launcher](const folly::dynamic &args, std::vector<JsCallback>) {
|
|
204
|
+
auto signal = args.size() > 1 && args[1].isString() ? signalNamed(args[1].asString()) : std::optional<int>(SIGTERM);
|
|
205
|
+
if (args.size() > 0 && args[0].isNumber() && signal) {
|
|
206
|
+
launcher->kill(static_cast<int>(args[0].asInt()), *signal);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
listeners();
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
auto module = std::make_shared<Processes>(launcher, std::move(setup));
|
|
213
|
+
std::weak_ptr<Processes> weak = module;
|
|
214
|
+
// A reload's module hears only the programs it spawned: the JavaScript holds an event for a
|
|
215
|
+
// handle spawn() has not answered yet, and would hold an older runtime's events forever.
|
|
216
|
+
int floor = launcher->lastHandle();
|
|
217
|
+
launcher->setEvents({
|
|
218
|
+
.output =
|
|
219
|
+
[weak, floor](int handle, bool isStderr, std::string data) {
|
|
220
|
+
if (auto self = weak.lock(); self && handle > floor) {
|
|
221
|
+
self->emitEvent(folly::dynamic::object("type", isStderr ? "stderr" : "stdout")("handle", handle)("data", data));
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
.exit =
|
|
225
|
+
[weak, floor](int handle, std::optional<int> code, std::optional<std::string> signal) {
|
|
226
|
+
if (auto self = weak.lock(); self && handle > floor) {
|
|
227
|
+
self->exited(handle);
|
|
228
|
+
self->emitEvent(folly::dynamic::object("type", "exit")("handle", handle)(
|
|
229
|
+
"code", code ? folly::dynamic(*code) : folly::dynamic(nullptr))(
|
|
230
|
+
"signal", signal ? folly::dynamic(*signal) : folly::dynamic(nullptr)));
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
});
|
|
234
|
+
return module;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
bool externalUrlAllowed(const std::string &url) {
|
|
238
|
+
auto colon = url.find(':');
|
|
239
|
+
if (colon == std::string::npos || colon == 0 || url.find('\0') != std::string::npos) {
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
std::string scheme = url.substr(0, colon);
|
|
243
|
+
if (!std::isalpha(static_cast<unsigned char>(scheme[0])) ||
|
|
244
|
+
!std::all_of(scheme.begin(), scheme.end(), [](unsigned char c) { return std::isalnum(c) || c == '+' || c == '-' || c == '.'; })) {
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
std::transform(scheme.begin(), scheme.end(), scheme.begin(), [](unsigned char c) { return std::tolower(c); });
|
|
248
|
+
return scheme != "file" && scheme != "javascript" && scheme != "data" && scheme != "vbscript" && scheme != "blob";
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
bool plainPath(const std::string &path) {
|
|
252
|
+
if (path.empty() || path[0] != '/' || path.find('\0') != std::string::npos) {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
if (path == "/") {
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
size_t start = 1;
|
|
259
|
+
while (start <= path.size()) {
|
|
260
|
+
auto slash = path.find('/', start);
|
|
261
|
+
auto segment = path.substr(start, slash == std::string::npos ? std::string::npos : slash - start);
|
|
262
|
+
if (segment.empty() || segment == "." || segment == "..") {
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
if (slash == std::string::npos) {
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
start = slash + 1;
|
|
269
|
+
}
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
std::shared_ptr<NativeModule> makeShellModule(std::shared_ptr<ShellOpener> opener, std::shared_ptr<FileAccess> files) {
|
|
274
|
+
struct Shell : NativeModule {
|
|
275
|
+
Shell(std::shared_ptr<ShellOpener> opener, std::shared_ptr<FileAccess> files) : NativeModule("NgNativeShell") {
|
|
276
|
+
auto answer = [](JsPromise promise) { return [promise](bool done) { promise.resolve(done); }; };
|
|
277
|
+
// A plain path inside the file boundary, with its pick held open; checked before anything
|
|
278
|
+
// looks at the path, so an answer never says whether a file outside is there.
|
|
279
|
+
auto reachable = [files](const folly::dynamic &args) -> std::optional<std::shared_ptr<FileAccess::Use>> {
|
|
280
|
+
if (args.size() < 1 || !args[0].isString() || !plainPath(args[0].asString())) {
|
|
281
|
+
return std::nullopt;
|
|
282
|
+
}
|
|
283
|
+
try {
|
|
284
|
+
return readableFile(files.get(), args[0].asString());
|
|
285
|
+
} catch (const FileRefused &) {
|
|
286
|
+
return std::nullopt;
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
promise("openExternal", [opener, answer](const folly::dynamic &args, JsPromise promise) {
|
|
290
|
+
if (args.size() < 1 || !args[0].isString() || !externalUrlAllowed(args[0].asString())) {
|
|
291
|
+
promise.resolve(false);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
opener->openUrl(args[0].asString(), answer(promise));
|
|
295
|
+
});
|
|
296
|
+
promise("openPath", [opener, reachable](const folly::dynamic &args, JsPromise promise) {
|
|
297
|
+
auto use = reachable(args);
|
|
298
|
+
if (!use) {
|
|
299
|
+
promise.resolve(false);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
// A document opens in its app; a program, an app or a script would run, past the spawn
|
|
303
|
+
// rules.
|
|
304
|
+
auto facts = opener->facts(args[0].asString());
|
|
305
|
+
if (!facts.exists || facts.executable || facts.runsCode) {
|
|
306
|
+
promise.resolve(false);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
opener->openPath(args[0].asString(), [promise, use = *use](bool done) { promise.resolve(done); });
|
|
310
|
+
});
|
|
311
|
+
promise("showItemInFolder", [opener, reachable](const folly::dynamic &args, JsPromise promise) {
|
|
312
|
+
auto use = reachable(args);
|
|
313
|
+
if (!use) {
|
|
314
|
+
promise.resolve(false);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
opener->showItemInFolder(args[0].asString(), [promise, use = *use](bool done) { promise.resolve(done); });
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
return std::make_shared<Shell>(std::move(opener), std::move(files));
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
} // namespace ngn
|