@vscode/native-watchdog 1.4.6

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Microsoft Corporation. All rights reserved.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE
package/README.md ADDED
@@ -0,0 +1,47 @@
1
+
2
+
3
+
4
+
5
+
6
+
7
+ # Watchdog node module
8
+ Kills the current process when another process is no longer running. This helps when the JavaScript event loop might be busy and never recovering e.g. an accidental while true loop.
9
+
10
+ This is implemented by launching a separate thread from C++ which periodically checks if the given process is still running. Typically, one would watch the parent process id. The watched process is checked every 1s and if it is no longer running, the current process will exit after 6 seconds with the exit code 87.
11
+
12
+ ## Installing
13
+
14
+ ```sh
15
+ npm install native-watchdog
16
+ ```
17
+
18
+ ## Using
19
+
20
+ ```javascript
21
+ var watchdog = require('native-watchdog');
22
+
23
+ watchdog.start(pid);
24
+ ```
25
+
26
+ ## Developing
27
+ * `npm install -g node-gyp`
28
+ * `node-gyp configure`
29
+ * `node-gyp build` (for debugging use `node-gyp build --debug`)
30
+ * `npm test` (for debugging change `index.js` to load the node module from the `Debug` folder and press `F5`)
31
+
32
+ ## Contributing
33
+
34
+ This project welcomes contributions and suggestions. Most contributions require you to agree to a
35
+ Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
36
+ the rights to use your contribution. For details, visit https://cla.microsoft.com.
37
+
38
+ When you submit a pull request, a CLA-bot will automatically determine whether you need to provide
39
+ a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions
40
+ provided by the bot. You will only need to do this once across all repos using our CLA.
41
+
42
+ This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
43
+ For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
44
+ contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
45
+
46
+ ## License
47
+ [MIT](https://github.com/Microsoft/node-native-watchdog/blob/master/LICENSE)
package/SECURITY.md ADDED
@@ -0,0 +1,41 @@
1
+ <!-- BEGIN MICROSOFT SECURITY.MD V0.0.5 BLOCK -->
2
+
3
+ ## Security
4
+
5
+ Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/Microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [our GitHub organizations](https://opensource.microsoft.com/).
6
+
7
+ If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://docs.microsoft.com/en-us/previous-versions/tn-archive/cc751383(v=technet.10)), please report it to us as described below.
8
+
9
+ ## Reporting Security Issues
10
+
11
+ **Please do not report security vulnerabilities through public GitHub issues.**
12
+
13
+ Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://msrc.microsoft.com/create-report).
14
+
15
+ If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://www.microsoft.com/en-us/msrc/pgp-key-msrc).
16
+
17
+ You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc).
18
+
19
+ Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:
20
+
21
+ * Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)
22
+ * Full paths of source file(s) related to the manifestation of the issue
23
+ * The location of the affected source code (tag/branch/commit or direct URL)
24
+ * Any special configuration required to reproduce the issue
25
+ * Step-by-step instructions to reproduce the issue
26
+ * Proof-of-concept or exploit code (if possible)
27
+ * Impact of the issue, including how an attacker might exploit the issue
28
+
29
+ This information will help us triage your report more quickly.
30
+
31
+ If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://microsoft.com/msrc/bounty) page for more details about our active programs.
32
+
33
+ ## Preferred Languages
34
+
35
+ We prefer all communications to be in English.
36
+
37
+ ## Policy
38
+
39
+ Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://www.microsoft.com/en-us/msrc/cvd).
40
+
41
+ <!-- END MICROSOFT SECURITY.MD BLOCK -->
package/binding.gyp ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "targets": [
3
+ {
4
+ "target_name": "watchdog",
5
+ "sources": ["src/watchdog.cc"],
6
+ "cflags": ["-O2", "-fstack-protector-strong"],
7
+ "msvs_configuration_attributes": {"SpectreMitigation": "Spectre"},
8
+ "msvs_settings": {
9
+ "VCCLCompilerTool": {
10
+ "AdditionalOptions": [
11
+ "/guard:cf",
12
+ "/sdl",
13
+ "/W3",
14
+ "/we4146",
15
+ "/we4244",
16
+ "/we4267",
17
+ "/ZH:SHA_256",
18
+ ]
19
+ },
20
+ "VCLinkerTool": {"AdditionalOptions": ["/DYNAMICBASE", "/guard:cf"]},
21
+ },
22
+ }
23
+ ]
24
+ }
package/index.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See LICENSE in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+
6
+ /**
7
+ * Start monitoring for a certain pid to exist.
8
+ * If the process indicated by pid ceases to execute,
9
+ * the current process will exit in 6 seconds with exit code 87
10
+ */
11
+ export function start(pid: number): void;
12
+
13
+ /**
14
+ * Exit the current process immediately with a certain exitCode.
15
+ */
16
+ export function exit(exitCode: number): void;
package/index.js ADDED
@@ -0,0 +1,23 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See LICENSE in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+
6
+ var watchdog = require('./build/Release/watchdog');
7
+
8
+ var hasStarted = false;
9
+
10
+ exports.start = function(pid) {
11
+ if (typeof pid !== 'number' || Math.round(pid) !== pid) {
12
+ throw new Error(`Expected integer pid!`);
13
+ }
14
+ if (hasStarted) {
15
+ throw new Error(`Can only monitor a single process!`);
16
+ }
17
+ hasStarted = true;
18
+ watchdog.start(pid);
19
+ }
20
+
21
+ exports.exit = function(code) {
22
+ watchdog.exit(code || 0);
23
+ }
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@vscode/native-watchdog",
3
+ "version": "1.4.6",
4
+ "description": "A native module that kills the current process if the event loop is unresponsive",
5
+ "main": "index.js",
6
+ "typings": "index.d.ts",
7
+ "scripts": {
8
+ "test": "node test/test.js"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/Microsoft/node-native-watchdog.git"
13
+ },
14
+ "author": "Microsoft Corporation",
15
+ "license": "MIT",
16
+ "bugs": {
17
+ "url": "https://github.com/Microsoft/node-native-watchdog/issues"
18
+ },
19
+ "homepage": "https://github.com/Microsoft/node-native-watchdog#readme"
20
+ }
package/src/common.h ADDED
@@ -0,0 +1,60 @@
1
+ #include <node_api.h>
2
+
3
+ // Empty value so that macros here are able to return NULL or void
4
+ #define NAPI_RETVAL_NOTHING // Intentionally blank #define
5
+
6
+ #define GET_AND_THROW_LAST_ERROR(env) \
7
+ do \
8
+ { \
9
+ const napi_extended_error_info *error_info; \
10
+ napi_get_last_error_info((env), &error_info); \
11
+ bool is_pending; \
12
+ napi_is_exception_pending((env), &is_pending); \
13
+ /* If an exception is already pending, don't rethrow it */ \
14
+ if (!is_pending) \
15
+ { \
16
+ const char *error_message = error_info->error_message != NULL ? error_info->error_message : "empty error message"; \
17
+ napi_throw_error((env), NULL, error_message); \
18
+ } \
19
+ } while (0)
20
+
21
+ #define NAPI_ASSERT_BASE(env, assertion, message, ret_val) \
22
+ do \
23
+ { \
24
+ if (!(assertion)) \
25
+ { \
26
+ napi_throw_error( \
27
+ (env), \
28
+ NULL, \
29
+ "assertion (" #assertion ") failed: " message); \
30
+ return ret_val; \
31
+ } \
32
+ } while (0)
33
+
34
+ // Returns NULL on failed assertion.
35
+ // This is meant to be used inside napi_callback methods.
36
+ #define NAPI_ASSERT(env, assertion, message) \
37
+ NAPI_ASSERT_BASE(env, assertion, message, NULL)
38
+
39
+ #define NAPI_CALL_BASE(env, the_call, ret_val) \
40
+ do \
41
+ { \
42
+ if ((the_call) != napi_ok) \
43
+ { \
44
+ GET_AND_THROW_LAST_ERROR((env)); \
45
+ return ret_val; \
46
+ } \
47
+ } while (0)
48
+
49
+ // Returns NULL if the_call doesn't return napi_ok.
50
+ #define NAPI_CALL(env, the_call) \
51
+ NAPI_CALL_BASE(env, the_call, NULL)
52
+
53
+ // Returns empty if the_call doesn't return napi_ok.
54
+ #define NAPI_CALL_RETURN_VOID(env, the_call) \
55
+ NAPI_CALL_BASE(env, the_call, NAPI_RETVAL_NOTHING)
56
+
57
+ #define DECLARE_NAPI_PROPERTY(name, func) \
58
+ { \
59
+ (name), NULL, (func), NULL, NULL, NULL, napi_default, NULL \
60
+ }
@@ -0,0 +1,122 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See LICENSE in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+
6
+ #include "common.h"
7
+ #include <time.h>
8
+ #include <stdlib.h>
9
+ #include <uv.h>
10
+
11
+ #if defined(_WIN32) || defined(__WIN32)
12
+ #ifndef WIN32
13
+ #define WIN32
14
+ #endif
15
+ #endif
16
+
17
+ #if !defined(WIN32)
18
+ #include <unistd.h>
19
+ #include <signal.h>
20
+ #include <sys/types.h>
21
+ #else
22
+ #include <windows.h>
23
+ #endif
24
+
25
+ namespace
26
+ {
27
+
28
+ int64_t w_parentpid = 0; // id of the parent process
29
+ uv_thread_t w_monitor_thread_id; // id of the monitor thread
30
+
31
+ bool w_processIsRunning(int64_t pid)
32
+ {
33
+ #if defined(WIN32)
34
+ HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, static_cast<DWORD>(pid));
35
+ DWORD ret = WaitForSingleObject(process, 0);
36
+ CloseHandle(process);
37
+ return (ret == WAIT_TIMEOUT);
38
+ #else
39
+ return (kill(static_cast<pid_t>(pid), 0) == 0);
40
+ #endif
41
+ }
42
+
43
+ void w_sleep(int seconds)
44
+ {
45
+ #if defined(WIN32)
46
+ Sleep(seconds * 1000);
47
+ #else
48
+ sleep(seconds);
49
+ #endif
50
+ }
51
+
52
+ void w_monitor(void *arg)
53
+ {
54
+ while (true)
55
+ {
56
+ if (!w_processIsRunning(w_parentpid))
57
+ {
58
+ w_sleep(5);
59
+ exit(87);
60
+ }
61
+ w_sleep(1);
62
+ }
63
+ }
64
+
65
+ } // namespace
66
+
67
+ napi_value Start(napi_env env, napi_callback_info info)
68
+ {
69
+ size_t argc = 1;
70
+ napi_value argv[1];
71
+ NAPI_CALL(env,
72
+ napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
73
+
74
+ NAPI_ASSERT(env, argc == 1, "Wrong number of arguments, expected 1.");
75
+
76
+ napi_valuetype t;
77
+ NAPI_CALL(env, napi_typeof(env, argv[0], &t));
78
+ NAPI_ASSERT(env, t == napi_number,
79
+ "Wrong argument, number expected.");
80
+
81
+ NAPI_CALL(env, napi_get_value_int64(env, argv[0], &w_parentpid));
82
+
83
+ uv_thread_create(&w_monitor_thread_id, w_monitor, NULL);
84
+
85
+ return nullptr;
86
+ }
87
+
88
+ napi_value Exit(napi_env env, napi_callback_info info)
89
+ {
90
+ size_t argc = 1;
91
+ napi_value argv[1];
92
+ NAPI_CALL(env,
93
+ napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
94
+
95
+ NAPI_ASSERT(env, argc == 1, "Wrong number of arguments, expected 1.");
96
+
97
+ napi_valuetype t;
98
+ NAPI_CALL(env, napi_typeof(env, argv[0], &t));
99
+ NAPI_ASSERT(env, t == napi_number,
100
+ "Wrong argument, number expected.");
101
+
102
+ int32_t code;
103
+ NAPI_CALL(env, napi_get_value_int32(env, argv[0], &code));
104
+
105
+ exit(code);
106
+
107
+ return nullptr;
108
+ }
109
+
110
+ napi_value Init(napi_env env, napi_value exports)
111
+ {
112
+ napi_property_descriptor properties[] = {
113
+ DECLARE_NAPI_PROPERTY("start", Start),
114
+ DECLARE_NAPI_PROPERTY("exit", Exit)};
115
+
116
+ NAPI_CALL(env, napi_define_properties(
117
+ env, exports, sizeof(properties) / sizeof(*properties), properties));
118
+
119
+ return exports;
120
+ }
121
+
122
+ NAPI_MODULE(NODE_GYP_MODULE_NAME, Init);