-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bootstrap: use different scripts to setup different configurations
This patch splits the handling of `isMainThread` and `ownsProcessState` from conditionals in `lib/internal/bootstrap/node.js` into different scripts under `lib/internal/bootstrap/switches/`, and call them accordingly from C++ after `node.js` is run. This: - Creates a common denominator of the main thread and the worker thread bootstrap that can be snapshotted and shared by both. - Makes it possible to override the configurations on-the-fly. PR-URL: #30862 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
- Loading branch information
1 parent
df94cfb
commit b450917
Showing
14 changed files
with
568 additions
and
545 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
lib/internal/bootstrap/switches/does_not_own_process_state.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
const credentials = internalBinding('credentials'); | ||
|
||
if (credentials.implementsPosixCredentials) { | ||
// TODO: this should be detached from ERR_WORKER_UNSUPPORTED_OPERATION | ||
const { unavailable } = require('internal/process/worker_thread_only'); | ||
|
||
process.initgroups = unavailable('process.initgroups()'); | ||
process.setgroups = unavailable('process.setgroups()'); | ||
process.setegid = unavailable('process.setegid()'); | ||
process.seteuid = unavailable('process.seteuid()'); | ||
process.setgid = unavailable('process.setgid()'); | ||
process.setuid = unavailable('process.setuid()'); | ||
} | ||
|
||
// ---- keep the attachment of the wrappers above so that it's easier to ---- | ||
// ---- compare the setups side-by-side ----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
'use strict'; | ||
|
||
const credentials = internalBinding('credentials'); | ||
|
||
if (credentials.implementsPosixCredentials) { | ||
const wrapped = wrapPosixCredentialSetters(credentials); | ||
|
||
process.initgroups = wrapped.initgroups; | ||
process.setgroups = wrapped.setgroups; | ||
process.setegid = wrapped.setegid; | ||
process.seteuid = wrapped.seteuid; | ||
process.setgid = wrapped.setgid; | ||
process.setuid = wrapped.setuid; | ||
} | ||
|
||
// ---- keep the attachment of the wrappers above so that it's easier to ---- | ||
// ---- compare the setups side-by-side ----- | ||
|
||
function wrapPosixCredentialSetters(credentials) { | ||
const { | ||
ArrayIsArray, | ||
} = primordials; | ||
const { | ||
codes: { | ||
ERR_INVALID_ARG_TYPE, | ||
ERR_UNKNOWN_CREDENTIAL | ||
} | ||
} = require('internal/errors'); | ||
const { | ||
validateUint32 | ||
} = require('internal/validators'); | ||
|
||
const { | ||
initgroups: _initgroups, | ||
setgroups: _setgroups, | ||
setegid: _setegid, | ||
seteuid: _seteuid, | ||
setgid: _setgid, | ||
setuid: _setuid | ||
} = credentials; | ||
|
||
function initgroups(user, extraGroup) { | ||
validateId(user, 'user'); | ||
validateId(extraGroup, 'extraGroup'); | ||
// Result is 0 on success, 1 if user is unknown, 2 if group is unknown. | ||
const result = _initgroups(user, extraGroup); | ||
if (result === 1) { | ||
throw new ERR_UNKNOWN_CREDENTIAL('User', user); | ||
} else if (result === 2) { | ||
throw new ERR_UNKNOWN_CREDENTIAL('Group', extraGroup); | ||
} | ||
} | ||
|
||
function setgroups(groups) { | ||
if (!ArrayIsArray(groups)) { | ||
throw new ERR_INVALID_ARG_TYPE('groups', 'Array', groups); | ||
} | ||
for (let i = 0; i < groups.length; i++) { | ||
validateId(groups[i], `groups[${i}]`); | ||
} | ||
// Result is 0 on success. A positive integer indicates that the | ||
// corresponding group was not found. | ||
const result = _setgroups(groups); | ||
if (result > 0) { | ||
throw new ERR_UNKNOWN_CREDENTIAL('Group', groups[result - 1]); | ||
} | ||
} | ||
|
||
function wrapIdSetter(type, method) { | ||
return function(id) { | ||
validateId(id, 'id'); | ||
// Result is 0 on success, 1 if credential is unknown. | ||
const result = method(id); | ||
if (result === 1) { | ||
throw new ERR_UNKNOWN_CREDENTIAL(type, id); | ||
} | ||
}; | ||
} | ||
|
||
function validateId(id, name) { | ||
if (typeof id === 'number') { | ||
validateUint32(id, name); | ||
} else if (typeof id !== 'string') { | ||
throw new ERR_INVALID_ARG_TYPE(name, ['number', 'string'], id); | ||
} | ||
} | ||
|
||
return { | ||
initgroups, | ||
setgroups, | ||
setegid: wrapIdSetter('Group', _setegid), | ||
seteuid: wrapIdSetter('User', _seteuid), | ||
setgid: wrapIdSetter('Group', _setgid), | ||
setuid: wrapIdSetter('User', _setuid) | ||
}; | ||
} |
Oops, something went wrong.