Implement multi-threading support for most of the API. Thanks to Stephen Belanger for reviewing this change in its original form, to Olivia Hugger for reviewing the documentation and some of the tests coming along with it, and to Alexey Orlenko and Timothy Gu for reviewing other parts of the tests. Refs: https://github.com/ayojs/ayo/pull/110 Refs: https://github.com/ayojs/ayo/pull/114 Refs: https://github.com/ayojs/ayo/pull/117 PR-URL: https://github.com/nodejs/node/pull/20876 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Shingo Inoue <leko.noor@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
110 lines
3.0 KiB
JavaScript
110 lines
3.0 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
ERR_INVALID_ARG_TYPE,
|
|
ERR_UNKNOWN_CREDENTIAL
|
|
} = require('internal/errors').codes;
|
|
const {
|
|
validateMode,
|
|
validateUint32
|
|
} = require('internal/validators');
|
|
const {
|
|
isMainThread
|
|
} = require('internal/worker');
|
|
|
|
function setupProcessMethods(_chdir, _cpuUsage, _hrtime, _memoryUsage,
|
|
_rawDebug, _umask, _initgroups, _setegid,
|
|
_seteuid, _setgid, _setuid, _setgroups) {
|
|
// Non-POSIX platforms like Windows don't have certain methods.
|
|
// Workers also lack these methods since they change process-global state.
|
|
if (!isMainThread)
|
|
return;
|
|
|
|
if (_setgid !== undefined) {
|
|
setupPosixMethods(_initgroups, _setegid, _seteuid,
|
|
_setgid, _setuid, _setgroups);
|
|
}
|
|
|
|
process.chdir = function chdir(directory) {
|
|
if (typeof directory !== 'string') {
|
|
throw new ERR_INVALID_ARG_TYPE('directory', 'string', directory);
|
|
}
|
|
return _chdir(directory);
|
|
};
|
|
|
|
process.umask = function umask(mask) {
|
|
if (mask === undefined) {
|
|
// Get the mask
|
|
return _umask(mask);
|
|
}
|
|
mask = validateMode(mask, 'mask');
|
|
return _umask(mask);
|
|
};
|
|
}
|
|
|
|
function setupPosixMethods(_initgroups, _setegid, _seteuid,
|
|
_setgid, _setuid, _setgroups) {
|
|
|
|
process.initgroups = 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);
|
|
}
|
|
};
|
|
|
|
process.setegid = function setegid(id) {
|
|
return execId(id, 'Group', _setegid);
|
|
};
|
|
|
|
process.seteuid = function seteuid(id) {
|
|
return execId(id, 'User', _seteuid);
|
|
};
|
|
|
|
process.setgid = function setgid(id) {
|
|
return execId(id, 'Group', _setgid);
|
|
};
|
|
|
|
process.setuid = function setuid(id) {
|
|
return execId(id, 'User', _setuid);
|
|
};
|
|
|
|
process.setgroups = function setgroups(groups) {
|
|
if (!Array.isArray(groups)) {
|
|
throw new ERR_INVALID_ARG_TYPE('groups', 'Array', groups);
|
|
}
|
|
for (var 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 execId(id, type, method) {
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
exports.setup = setupProcessMethods;
|