| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent df94cfb commit b450917
14 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,9 +3,7 @@ | |||
| 3 | 3 | // This file is invoked by `node::RunBootstrapping()` in `src/node.cc`, and is | |
| 4 | 4 | // responsible for setting up node.js core before executing main scripts | |
| 5 | 5 | // under `lib/internal/main/`. | |
| 6 | - // This file is currently run to bootstrap both the main thread and the worker | ||
| 7 | - // threads. Some setups are conditional, controlled with isMainThread and | ||
| 8 | - // ownsProcessState. | ||
| 6 | + // | ||
| 9 | 7 | // This file is expected not to perform any asynchronous operations itself | |
| 10 | 8 | // when being executed - those should be done in either | |
| 11 | 9 | // `lib/internal/bootstrap/pre_execution.js` or in main scripts. The majority | |
@@ -22,16 +20,21 @@ | |||
| 22 | 20 | // module loaders, including `process.binding()`, `process._linkedBinding()`, | |
| 23 | 21 | // `internalBinding()` and `NativeModule`. | |
| 24 | 22 | // | |
| 25 | - // After this file is run, one of the main scripts under `lib/internal/main/` | ||
| 26 | - // will be selected by C++ to start the actual execution. The main scripts may | ||
| 27 | - // run additional setups exported by `lib/internal/bootstrap/pre_execution.js`, | ||
| 28 | - // depending on the execution mode. | ||
| 23 | + // This file is run to bootstrap both the main thread and the worker threads. | ||
| 24 | + // After this file is run, certain properties are setup according to the | ||
| 25 | + // configuration of the Node.js instance using the files in | ||
| 26 | + // `lib/internal/bootstrap/switches/`. | ||
| 27 | + // | ||
| 28 | + // Then, depending on how the Node.js instance is launched, one of the main | ||
| 29 | + // scripts in `lib/internal/main` will be selected by C++ to start the actual | ||
| 30 | + // execution. They may run additional setups exported by | ||
| 31 | + // `lib/internal/bootstrap/pre_execution.js` depending on the runtime states. | ||
| 29 | 32 | ||
| 30 | 33 | 'use strict'; | |
| 31 | 34 | ||
| 32 | 35 | // This file is compiled as if it's wrapped in a function with arguments | |
| 33 | 36 | // passed by node::RunBootstrapping() | |
| 34 | - /* global process, require, internalBinding, isMainThread, ownsProcessState */ | ||
| 37 | + /* global process, require, internalBinding */ | ||
| 35 | 38 | ||
| 36 | 39 | setupPrepareStackTrace(); | |
| 37 | 40 | ||
@@ -54,48 +57,12 @@ setupBuffer(); | |||
| 54 | 57 | process.domain = null; | |
| 55 | 58 | process._exiting = false; | |
| 56 | 59 | ||
| 57 | - // Bootstrappers for all threads, including worker threads and main thread | ||
| 58 | - const perThreadSetup = require('internal/process/per_thread'); | ||
| 59 | - // Bootstrappers for the main thread only | ||
| 60 | - let mainThreadSetup; | ||
| 61 | - // Bootstrappers for the worker threads only | ||
| 62 | - let workerThreadSetup; | ||
| 63 | - if (ownsProcessState) { | ||
| 64 | - mainThreadSetup = require( | ||
| 65 | - 'internal/process/main_thread_only' | ||
| 66 | - ); | ||
| 67 | - } else { | ||
| 68 | - workerThreadSetup = require( | ||
| 69 | - 'internal/process/worker_thread_only' | ||
| 70 | - ); | ||
| 71 | - } | ||
| 72 | - | ||
| 73 | 60 | // process.config is serialized config.gypi | |
| 74 | 61 | process.config = JSONParse(internalBinding('native_module').config); | |
| 75 | 62 | ||
| 63 | + // Bootstrappers for all threads, including worker threads and main thread | ||
| 64 | + const perThreadSetup = require('internal/process/per_thread'); | ||
| 76 | 65 | const rawMethods = internalBinding('process_methods'); | |
| 77 | - // Set up methods and events on the process object for the main thread | ||
| 78 | - if (isMainThread) { | ||
| 79 | - process.abort = rawMethods.abort; | ||
| 80 | - const wrapped = mainThreadSetup.wrapProcessMethods(rawMethods); | ||
| 81 | - process.umask = wrapped.umask; | ||
| 82 | - process.chdir = wrapped.chdir; | ||
| 83 | - process.cwd = wrapped.cwd; | ||
| 84 | - | ||
| 85 | - // TODO(joyeecheung): deprecate and remove these underscore methods | ||
| 86 | - process._debugProcess = rawMethods._debugProcess; | ||
| 87 | - process._debugEnd = rawMethods._debugEnd; | ||
| 88 | - process._startProfilerIdleNotifier = | ||
| 89 | - rawMethods._startProfilerIdleNotifier; | ||
| 90 | - process._stopProfilerIdleNotifier = rawMethods._stopProfilerIdleNotifier; | ||
| 91 | - } else { | ||
| 92 | - const wrapped = workerThreadSetup.wrapProcessMethods(rawMethods); | ||
| 93 | - | ||
| 94 | - process.abort = workerThreadSetup.unavailable('process.abort()'); | ||
| 95 | - process.chdir = workerThreadSetup.unavailable('process.chdir()'); | ||
| 96 | - process.umask = wrapped.umask; | ||
| 97 | - process.cwd = rawMethods.cwd; | ||
| 98 | - } | ||
| 99 | 66 | ||
| 100 | 67 | // Set up methods on the process object for all threads | |
| 101 | 68 | { | |
@@ -119,6 +86,11 @@ if (isMainThread) { | |||
| 119 | 86 | process.memoryUsage = wrapped.memoryUsage; | |
| 120 | 87 | process.kill = wrapped.kill; | |
| 121 | 88 | process.exit = wrapped.exit; | |
| 89 | + | ||
| 90 | + process.openStdin = function() { | ||
| 91 | + process.stdin.resume(); | ||
| 92 | + return process.stdin; | ||
| 93 | + }; | ||
| 122 | 94 | } | |
| 123 | 95 | ||
| 124 | 96 | const credentials = internalBinding('credentials'); | |
@@ -128,34 +100,6 @@ if (credentials.implementsPosixCredentials) { | |||
| 128 | 100 | process.getgid = credentials.getgid; | |
| 129 | 101 | process.getegid = credentials.getegid; | |
| 130 | 102 | process.getgroups = credentials.getgroups; | |
| 131 | - | ||
| 132 | - if (ownsProcessState) { | ||
| 133 | - const wrapped = mainThreadSetup.wrapPosixCredentialSetters(credentials); | ||
| 134 | - process.initgroups = wrapped.initgroups; | ||
| 135 | - process.setgroups = wrapped.setgroups; | ||
| 136 | - process.setegid = wrapped.setegid; | ||
| 137 | - process.seteuid = wrapped.seteuid; | ||
| 138 | - process.setgid = wrapped.setgid; | ||
| 139 | - process.setuid = wrapped.setuid; | ||
| 140 | - } else { | ||
| 141 | - process.initgroups = | ||
| 142 | - workerThreadSetup.unavailable('process.initgroups()'); | ||
| 143 | - process.setgroups = workerThreadSetup.unavailable('process.setgroups()'); | ||
| 144 | - process.setegid = workerThreadSetup.unavailable('process.setegid()'); | ||
| 145 | - process.seteuid = workerThreadSetup.unavailable('process.seteuid()'); | ||
| 146 | - process.setgid = workerThreadSetup.unavailable('process.setgid()'); | ||
| 147 | - process.setuid = workerThreadSetup.unavailable('process.setuid()'); | ||
| 148 | - } | ||
| 149 | - } | ||
| 150 | - | ||
| 151 | - if (isMainThread) { | ||
| 152 | - const { getStdout, getStdin, getStderr } = | ||
| 153 | - require('internal/process/stdio').getMainThreadStdio(); | ||
| 154 | - setupProcessStdio(getStdout, getStdin, getStderr); | ||
| 155 | - } else { | ||
| 156 | - const { getStdout, getStdin, getStderr } = | ||
| 157 | - workerThreadSetup.createStdioGetters(); | ||
| 158 | - setupProcessStdio(getStdout, getStdin, getStderr); | ||
| 159 | 103 | } | |
| 160 | 104 | ||
| 161 | 105 | // Setup the callbacks that node::AsyncWrap will call when there are hooks to | |
@@ -343,31 +287,6 @@ function setupProcessObject() { | |||
| 343 | 287 | }); | |
| 344 | 288 | } | |
| 345 | 289 | ||
| 346 | - function setupProcessStdio(getStdout, getStdin, getStderr) { | ||
| 347 | - ObjectDefineProperty(process, 'stdout', { | ||
| 348 | - configurable: true, | ||
| 349 | - enumerable: true, | ||
| 350 | - get: getStdout | ||
| 351 | - }); | ||
| 352 | - | ||
| 353 | - ObjectDefineProperty(process, 'stderr', { | ||
| 354 | - configurable: true, | ||
| 355 | - enumerable: true, | ||
| 356 | - get: getStderr | ||
| 357 | - }); | ||
| 358 | - | ||
| 359 | - ObjectDefineProperty(process, 'stdin', { | ||
| 360 | - configurable: true, | ||
| 361 | - enumerable: true, | ||
| 362 | - get: getStdin | ||
| 363 | - }); | ||
| 364 | - | ||
| 365 | - process.openStdin = function() { | ||
| 366 | - process.stdin.resume(); | ||
| 367 | - return process.stdin; | ||
| 368 | - }; | ||
| 369 | - } | ||
| 370 | - | ||
| 371 | 290 | function setupGlobalProxy() { | |
| 372 | 291 | ObjectDefineProperty(global, SymbolToStringTag, { | |
| 373 | 292 | value: 'global', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,9 +37,6 @@ function prepareMainThreadExecution(expandArgv1 = false) { | |||
| 37 | 37 | ||
| 38 | 38 | setupDebugEnv(); | |
| 39 | 39 | ||
| 40 | - // Only main thread receives signals. | ||
| 41 | - setupSignalHandlers(); | ||
| 42 | - | ||
| 43 | 40 | // Process initial diagnostic reporting configuration, if present. | |
| 44 | 41 | initializeReport(); | |
| 45 | 42 | initializeReportSignalHandlers(); // Main-thread-only. | |
@@ -175,20 +172,7 @@ function setupDebugEnv() { | |||
| 175 | 172 | } | |
| 176 | 173 | } | |
| 177 | 174 | ||
| 178 | - function setupSignalHandlers() { | ||
| 179 | - const { | ||
| 180 | - createSignalHandlers | ||
| 181 | - } = require('internal/process/main_thread_only'); | ||
| 182 | - const { | ||
| 183 | - startListeningIfSignal, | ||
| 184 | - stopListeningIfSignal | ||
| 185 | - } = createSignalHandlers(); | ||
| 186 | - process.on('newListener', startListeningIfSignal); | ||
| 187 | - process.on('removeListener', stopListeningIfSignal); | ||
| 188 | - } | ||
| 189 | - | ||
| 190 | - // This has to be called after both initializeReport() and | ||
| 191 | - // setupSignalHandlers() are called | ||
| 175 | + // This has to be called after initializeReport() is called | ||
| 192 | 176 | function initializeReportSignalHandlers() { | |
| 193 | 177 | if (!getOptionValue('--experimental-report')) { | |
| 194 | 178 | return; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,18 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const credentials = internalBinding('credentials'); | ||
| 4 | + | ||
| 5 | + if (credentials.implementsPosixCredentials) { | ||
| 6 | + // TODO: this should be detached from ERR_WORKER_UNSUPPORTED_OPERATION | ||
| 7 | + const { unavailable } = require('internal/process/worker_thread_only'); | ||
| 8 | + | ||
| 9 | + process.initgroups = unavailable('process.initgroups()'); | ||
| 10 | + process.setgroups = unavailable('process.setgroups()'); | ||
| 11 | + process.setegid = unavailable('process.setegid()'); | ||
| 12 | + process.seteuid = unavailable('process.seteuid()'); | ||
| 13 | + process.setgid = unavailable('process.setgid()'); | ||
| 14 | + process.setuid = unavailable('process.setuid()'); | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + // ---- keep the attachment of the wrappers above so that it's easier to ---- | ||
| 18 | + // ---- compare the setups side-by-side ----- | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,96 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const credentials = internalBinding('credentials'); | ||
| 4 | + | ||
| 5 | + if (credentials.implementsPosixCredentials) { | ||
| 6 | + const wrapped = wrapPosixCredentialSetters(credentials); | ||
| 7 | + | ||
| 8 | + process.initgroups = wrapped.initgroups; | ||
| 9 | + process.setgroups = wrapped.setgroups; | ||
| 10 | + process.setegid = wrapped.setegid; | ||
| 11 | + process.seteuid = wrapped.seteuid; | ||
| 12 | + process.setgid = wrapped.setgid; | ||
| 13 | + process.setuid = wrapped.setuid; | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + // ---- keep the attachment of the wrappers above so that it's easier to ---- | ||
| 17 | + // ---- compare the setups side-by-side ----- | ||
| 18 | + | ||
| 19 | + function wrapPosixCredentialSetters(credentials) { | ||
| 20 | + const { | ||
| 21 | + ArrayIsArray, | ||
| 22 | + } = primordials; | ||
| 23 | + const { | ||
| 24 | + codes: { | ||
| 25 | + ERR_INVALID_ARG_TYPE, | ||
| 26 | + ERR_UNKNOWN_CREDENTIAL | ||
| 27 | + } | ||
| 28 | + } = require('internal/errors'); | ||
| 29 | + const { | ||
| 30 | + validateUint32 | ||
| 31 | + } = require('internal/validators'); | ||
| 32 | + | ||
| 33 | + const { | ||
| 34 | + initgroups: _initgroups, | ||
| 35 | + setgroups: _setgroups, | ||
| 36 | + setegid: _setegid, | ||
| 37 | + seteuid: _seteuid, | ||
| 38 | + setgid: _setgid, | ||
| 39 | + setuid: _setuid | ||
| 40 | + } = credentials; | ||
| 41 | + | ||
| 42 | + function initgroups(user, extraGroup) { | ||
| 43 | + validateId(user, 'user'); | ||
| 44 | + validateId(extraGroup, 'extraGroup'); | ||
| 45 | + // Result is 0 on success, 1 if user is unknown, 2 if group is unknown. | ||
| 46 | + const result = _initgroups(user, extraGroup); | ||
| 47 | + if (result === 1) { | ||
| 48 | + throw new ERR_UNKNOWN_CREDENTIAL('User', user); | ||
| 49 | + } else if (result === 2) { | ||
| 50 | + throw new ERR_UNKNOWN_CREDENTIAL('Group', extraGroup); | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + function setgroups(groups) { | ||
| 55 | + if (!ArrayIsArray(groups)) { | ||
| 56 | + throw new ERR_INVALID_ARG_TYPE('groups', 'Array', groups); | ||
| 57 | + } | ||
| 58 | + for (let i = 0; i < groups.length; i++) { | ||
| 59 | + validateId(groups[i], `groups[${i}]`); | ||
| 60 | + } | ||
| 61 | + // Result is 0 on success. A positive integer indicates that the | ||
| 62 | + // corresponding group was not found. | ||
| 63 | + const result = _setgroups(groups); | ||
| 64 | + if (result > 0) { | ||
| 65 | + throw new ERR_UNKNOWN_CREDENTIAL('Group', groups[result - 1]); | ||
| 66 | + } | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + function wrapIdSetter(type, method) { | ||
| 70 | + return function(id) { | ||
| 71 | + validateId(id, 'id'); | ||
| 72 | + // Result is 0 on success, 1 if credential is unknown. | ||
| 73 | + const result = method(id); | ||
| 74 | + if (result === 1) { | ||
| 75 | + throw new ERR_UNKNOWN_CREDENTIAL(type, id); | ||
| 76 | + } | ||
| 77 | + }; | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + function validateId(id, name) { | ||
| 81 | + if (typeof id === 'number') { | ||
| 82 | + validateUint32(id, name); | ||
| 83 | + } else if (typeof id !== 'string') { | ||
| 84 | + throw new ERR_INVALID_ARG_TYPE(name, ['number', 'string'], id); | ||
| 85 | + } | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + return { | ||
| 89 | + initgroups, | ||
| 90 | + setgroups, | ||
| 91 | + setegid: wrapIdSetter('Group', _setegid), | ||
| 92 | + seteuid: wrapIdSetter('User', _seteuid), | ||
| 93 | + setgid: wrapIdSetter('Group', _setgid), | ||
| 94 | + setuid: wrapIdSetter('User', _setuid) | ||
| 95 | + }; | ||
| 96 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments