summaryrefslogtreecommitdiff
path: root/lib/internal/process/main_thread_only.js
blob: ab56500744ba0d5b64079366050fdeab76339dce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';

// This file contains process bootstrappers that can only be
// run in the main thread

const {
  ArrayIsArray,
} = primordials;

const {
  errnoException,
  codes: {
    ERR_INVALID_ARG_TYPE,
    ERR_UNKNOWN_CREDENTIAL
  }
} = require('internal/errors');
const {
  parseMode,
  validateUint32,
  validateString
} = require('internal/validators');

const { signals } = internalBinding('constants').os;

// The execution of this function itself should not cause any side effects.
function wrapProcessMethods(binding) {
  // Cache the working directory to prevent lots of lookups. If the working
  // directory is changed by `chdir`, it'll be updated.
  let cachedCwd = '';

  function chdir(directory) {
    validateString(directory, 'directory');
    binding.chdir(directory);
    // Mark cache that it requires an update.
    cachedCwd = '';
  }

  function umask(mask) {
    if (mask !== undefined) {
      mask = parseMode(mask, 'mask');
    }
    return binding.umask(mask);
  }

  function cwd() {
    if (cachedCwd === '')
      cachedCwd = binding.cwd();
    return cachedCwd;
  }

  return {
    chdir,
    umask,
    cwd
  };
}

function wrapPosixCredentialSetters(credentials) {
  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)
  };
}

let Signal;
function isSignal(event) {
  return typeof event === 'string' && signals[event] !== undefined;
}

// Worker threads don't receive signals.
function createSignalHandlers() {
  const signalWraps = new Map();

  // Detect presence of a listener for the special signal types
  function startListeningIfSignal(type) {
    if (isSignal(type) && !signalWraps.has(type)) {
      if (Signal === undefined)
        Signal = internalBinding('signal_wrap').Signal;
      const wrap = new Signal();

      wrap.unref();

      wrap.onsignal = process.emit.bind(process, type, type);

      const signum = signals[type];
      const err = wrap.start(signum);
      if (err) {
        wrap.close();
        throw errnoException(err, 'uv_signal_start');
      }

      signalWraps.set(type, wrap);
    }
  }

  function stopListeningIfSignal(type) {
    const wrap = signalWraps.get(type);
    if (wrap !== undefined && process.listenerCount(type) === 0) {
      wrap.close();
      signalWraps.delete(type);
    }
  }

  return {
    startListeningIfSignal,
    stopListeningIfSignal
  };
}

module.exports = {
  wrapProcessMethods,
  createSignalHandlers,
  wrapPosixCredentialSetters
};