|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// This file contains process bootstrappers that can only be |
| 4 | +// run in the main thread |
| 5 | + |
| 6 | +const { |
| 7 | + errnoException |
| 8 | +} = require('internal/errors'); |
| 9 | + |
| 10 | +const { |
| 11 | + setupProcessStdio, |
| 12 | + getMainThreadStdio |
| 13 | +} = require('internal/process/stdio'); |
| 14 | + |
| 15 | +const assert = require('assert').strict; |
| 16 | + |
| 17 | +function setupStdio() { |
| 18 | + setupProcessStdio(getMainThreadStdio()); |
| 19 | +} |
| 20 | + |
| 21 | +// Non-POSIX platforms like Windows don't have certain methods. |
| 22 | +// Workers also lack these methods since they change process-global state. |
| 23 | +function setupProcessMethods(_chdir, _umask, _initgroups, _setegid, |
| 24 | + _seteuid, _setgid, _setuid, _setgroups) { |
| 25 | + if (_setgid !== undefined) { |
| 26 | + setupPosixMethods(_initgroups, _setegid, _seteuid, |
| 27 | + _setgid, _setuid, _setgroups); |
| 28 | + } |
| 29 | + |
| 30 | + process.chdir = function chdir(...args) { |
| 31 | + return _chdir(...args); |
| 32 | + }; |
| 33 | + |
| 34 | + process.umask = function umask(...args) { |
| 35 | + return _umask(...args); |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +function setupPosixMethods(_initgroups, _setegid, _seteuid, |
| 40 | + _setgid, _setuid, _setgroups) { |
| 41 | + |
| 42 | + process.initgroups = function initgroups(...args) { |
| 43 | + return _initgroups(...args); |
| 44 | + }; |
| 45 | + |
| 46 | + process.setegid = function setegid(...args) { |
| 47 | + return _setegid(...args); |
| 48 | + }; |
| 49 | + |
| 50 | + process.seteuid = function seteuid(...args) { |
| 51 | + return _seteuid(...args); |
| 52 | + }; |
| 53 | + |
| 54 | + process.setgid = function setgid(...args) { |
| 55 | + return _setgid(...args); |
| 56 | + }; |
| 57 | + |
| 58 | + process.setuid = function setuid(...args) { |
| 59 | + return _setuid(...args); |
| 60 | + }; |
| 61 | + |
| 62 | + process.setgroups = function setgroups(...args) { |
| 63 | + return _setgroups(...args); |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +// Worker threads don't receive signals. |
| 68 | +function setupSignalHandlers() { |
| 69 | + const constants = process.binding('constants').os.signals; |
| 70 | + const signalWraps = Object.create(null); |
| 71 | + let Signal; |
| 72 | + |
| 73 | + function isSignal(event) { |
| 74 | + return typeof event === 'string' && constants[event] !== undefined; |
| 75 | + } |
| 76 | + |
| 77 | + // Detect presence of a listener for the special signal types |
| 78 | + process.on('newListener', function(type) { |
| 79 | + if (isSignal(type) && signalWraps[type] === undefined) { |
| 80 | + if (Signal === undefined) |
| 81 | + Signal = process.binding('signal_wrap').Signal; |
| 82 | + const wrap = new Signal(); |
| 83 | + |
| 84 | + wrap.unref(); |
| 85 | + |
| 86 | + wrap.onsignal = process.emit.bind(process, type, type); |
| 87 | + |
| 88 | + const signum = constants[type]; |
| 89 | + const err = wrap.start(signum); |
| 90 | + if (err) { |
| 91 | + wrap.close(); |
| 92 | + throw errnoException(err, 'uv_signal_start'); |
| 93 | + } |
| 94 | + |
| 95 | + signalWraps[type] = wrap; |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + process.on('removeListener', function(type) { |
| 100 | + if (signalWraps[type] !== undefined && this.listenerCount(type) === 0) { |
| 101 | + signalWraps[type].close(); |
| 102 | + delete signalWraps[type]; |
| 103 | + } |
| 104 | + }); |
| 105 | +} |
| 106 | + |
| 107 | +function setupChildProcessIpcChannel() { |
| 108 | + // If we were spawned with env NODE_CHANNEL_FD then load that up and |
| 109 | + // start parsing data from that stream. |
| 110 | + if (process.env.NODE_CHANNEL_FD) { |
| 111 | + const fd = parseInt(process.env.NODE_CHANNEL_FD, 10); |
| 112 | + assert(fd >= 0); |
| 113 | + |
| 114 | + // Make sure it's not accidentally inherited by child processes. |
| 115 | + delete process.env.NODE_CHANNEL_FD; |
| 116 | + |
| 117 | + require('child_process')._forkChild(fd); |
| 118 | + assert(process.send); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +module.exports = { |
| 123 | + setupStdio, |
| 124 | + setupProcessMethods, |
| 125 | + setupSignalHandlers, |
| 126 | + setupChildProcessIpcChannel |
| 127 | +}; |
0 commit comments