Skip to content

Commit 8f4d864

Browse files
committed
src: add flags for controlling process behavior
1 parent 4bd8e0e commit 8f4d864

File tree

3 files changed

+77
-51
lines changed

3 files changed

+77
-51
lines changed

src/node.cc

+56-47
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,8 @@ static std::atomic_bool init_called{false};
842842

843843
int InitializeNodeWithArgs(std::vector<std::string>* argv,
844844
std::vector<std::string>* exec_argv,
845-
std::vector<std::string>* errors) {
845+
std::vector<std::string>* errors,
846+
ProcessFlags::Flags flags) {
846847
// Make sure InitializeNodeWithArgs() is called only once.
847848
CHECK(!init_called.exchange(true));
848849

@@ -853,7 +854,8 @@ int InitializeNodeWithArgs(std::vector<std::string>* argv,
853854
binding::RegisterBuiltinModules();
854855

855856
// Make inherited handles noninheritable.
856-
uv_disable_stdio_inheritance();
857+
if (!(flags & ProcessFlags::kEnableStdioInheritance))
858+
uv_disable_stdio_inheritance();
857859

858860
// Cache the original command line to be
859861
// used in diagnostic reports.
@@ -869,67 +871,73 @@ int InitializeNodeWithArgs(std::vector<std::string>* argv,
869871
HandleEnvOptions(per_process::cli_options->per_isolate->per_env);
870872

871873
#if !defined(NODE_WITHOUT_NODE_OPTIONS)
872-
std::string node_options;
874+
if (!(flags & ProcessFlags::kDisableNodeOptionsEnv)) {
875+
std::string node_options;
873876

874-
if (credentials::SafeGetenv("NODE_OPTIONS", &node_options)) {
875-
std::vector<std::string> env_argv =
876-
ParseNodeOptionsEnvVar(node_options, errors);
877+
if (credentials::SafeGetenv("NODE_OPTIONS", &node_options)) {
878+
std::vector<std::string> env_argv =
879+
ParseNodeOptionsEnvVar(node_options, errors);
877880

878-
if (!errors->empty()) return 9;
881+
if (!errors->empty()) return 9;
879882

880-
// [0] is expected to be the program name, fill it in from the real argv.
881-
env_argv.insert(env_argv.begin(), argv->at(0));
883+
// [0] is expected to be the program name, fill it in from the real argv.
884+
env_argv.insert(env_argv.begin(), argv->at(0));
882885

883-
const int exit_code = ProcessGlobalArgs(&env_argv,
884-
nullptr,
885-
errors,
886-
kAllowedInEnvironment);
887-
if (exit_code != 0) return exit_code;
886+
const int exit_code = ProcessGlobalArgs(&env_argv,
887+
nullptr,
888+
errors,
889+
kAllowedInEnvironment);
890+
if (exit_code != 0) return exit_code;
891+
}
888892
}
889893
#endif
890894

891-
const int exit_code = ProcessGlobalArgs(argv,
892-
exec_argv,
893-
errors,
894-
kDisallowedInEnvironment);
895-
if (exit_code != 0) return exit_code;
895+
if (!(flags & ProcessFlags::kDisableCLIOptions)) {
896+
const int exit_code = ProcessGlobalArgs(argv,
897+
exec_argv,
898+
errors,
899+
kDisallowedInEnvironment);
900+
if (exit_code != 0) return exit_code;
901+
}
896902

897903
// Set the process.title immediately after processing argv if --title is set.
898904
if (!per_process::cli_options->title.empty())
899905
uv_set_process_title(per_process::cli_options->title.c_str());
900906

901907
#if defined(NODE_HAVE_I18N_SUPPORT)
902-
// If the parameter isn't given, use the env variable.
903-
if (per_process::cli_options->icu_data_dir.empty())
904-
credentials::SafeGetenv("NODE_ICU_DATA",
905-
&per_process::cli_options->icu_data_dir);
908+
if (!(flags & ProcessFlags::kNoICU)) {
909+
// If the parameter isn't given, use the env variable.
910+
if (per_process::cli_options->icu_data_dir.empty())
911+
credentials::SafeGetenv("NODE_ICU_DATA",
912+
&per_process::cli_options->icu_data_dir);
906913

907914
#ifdef NODE_ICU_DEFAULT_DATA_DIR
908-
// If neither the CLI option nor the environment variable was specified,
909-
// fall back to the configured default
910-
if (per_process::cli_options->icu_data_dir.empty()) {
911-
// Check whether the NODE_ICU_DEFAULT_DATA_DIR contains the right data
912-
// file and can be read.
913-
static const char full_path[] =
914-
NODE_ICU_DEFAULT_DATA_DIR "/" U_ICUDATA_NAME ".dat";
915-
916-
FILE* f = fopen(full_path, "rb");
917-
918-
if (f != nullptr) {
919-
fclose(f);
920-
per_process::cli_options->icu_data_dir = NODE_ICU_DEFAULT_DATA_DIR;
915+
// If neither the CLI option nor the environment variable was specified,
916+
// fall back to the configured default
917+
if (per_process::cli_options->icu_data_dir.empty()) {
918+
// Check whether the NODE_ICU_DEFAULT_DATA_DIR contains the right data
919+
// file and can be read.
920+
static const char full_path[] =
921+
NODE_ICU_DEFAULT_DATA_DIR "/" U_ICUDATA_NAME ".dat";
922+
923+
FILE* f = fopen(full_path, "rb");
924+
925+
if (f != nullptr) {
926+
fclose(f);
927+
per_process::cli_options->icu_data_dir = NODE_ICU_DEFAULT_DATA_DIR;
928+
}
921929
}
922-
}
923930
#endif // NODE_ICU_DEFAULT_DATA_DIR
924931

925-
// Initialize ICU.
926-
// If icu_data_dir is empty here, it will load the 'minimal' data.
927-
if (!i18n::InitializeICUDirectory(per_process::cli_options->icu_data_dir)) {
928-
errors->push_back("could not initialize ICU "
929-
"(check NODE_ICU_DATA or --icu-data-dir parameters)\n");
930-
return 9;
932+
// Initialize ICU.
933+
// If icu_data_dir is empty here, it will load the 'minimal' data.
934+
if (!i18n::InitializeICUDirectory(per_process::cli_options->icu_data_dir)) {
935+
errors->push_back("could not initialize ICU "
936+
"(check NODE_ICU_DATA or --icu-data-dir parameters)\n");
937+
return 9;
938+
}
939+
per_process::metadata.versions.InitializeIntlVersions();
931940
}
932-
per_process::metadata.versions.InitializeIntlVersions();
933941

934942
# ifndef __POSIX__
935943
std::string tz;
@@ -956,7 +964,8 @@ InitializationResult InitializeOncePerProcess(int argc, char** argv) {
956964
InitializationResult InitializeOncePerProcess(
957965
int argc,
958966
char** argv,
959-
InitializationSettingsFlags flags) {
967+
InitializationSettingsFlags flags,
968+
ProcessFlags::Flags process_flags) {
960969
uint64_t init_flags = flags;
961970
if (init_flags & kDefaultInitialization) {
962971
init_flags = init_flags | kInitializeV8 | kInitOpenSSL | kRunPlatformInit;
@@ -982,8 +991,8 @@ InitializationResult InitializeOncePerProcess(
982991

983992
// This needs to run *before* V8::Initialize().
984993
{
985-
result.exit_code =
986-
InitializeNodeWithArgs(&(result.args), &(result.exec_args), &errors);
994+
result.exit_code = InitializeNodeWithArgs(
995+
&(result.args), &(result.exec_args), &errors, process_flags);
987996
for (const std::string& error : errors)
988997
fprintf(stderr, "%s: %s\n", result.args.at(0).c_str(), error.c_str());
989998
if (result.exit_code != 0) {

src/node.h

+19-3
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,20 @@ namespace node {
214214
class IsolateData;
215215
class Environment;
216216

217+
namespace ProcessFlags {
218+
enum Flags : uint64_t {
219+
kNoFlags = 0,
220+
// Enable stdio inheritance, which is disabled by default.
221+
kEnableStdioInheritance = 1 << 0,
222+
// Disable reading the NODE_OPTIONS environment variable.
223+
kDisableNodeOptionsEnv = 1 << 1,
224+
// Do not parse CLI options.
225+
kDisableCLIOptions = 1 << 2,
226+
// Do not initialize ICU.
227+
kNoICU = 1 << 3,
228+
};
229+
} // namespace ProcessFlags
230+
217231
// TODO(addaleax): Officially deprecate this and replace it with something
218232
// better suited for a public embedder API.
219233
NODE_EXTERN int Start(int argc, char* argv[]);
@@ -226,9 +240,11 @@ NODE_EXTERN int Stop(Environment* env);
226240
// from argv, fill exec_argv, and possibly add errors resulting from parsing
227241
// the arguments to `errors`. The return value is a suggested exit code for the
228242
// program; If it is 0, then initializing Node.js succeeded.
229-
NODE_EXTERN int InitializeNodeWithArgs(std::vector<std::string>* argv,
230-
std::vector<std::string>* exec_argv,
231-
std::vector<std::string>* errors);
243+
NODE_EXTERN int InitializeNodeWithArgs(
244+
std::vector<std::string>* argv,
245+
std::vector<std::string>* exec_argv,
246+
std::vector<std::string>* errors,
247+
ProcessFlags::Flags flags = ProcessFlags::kNoFlags);
232248

233249
enum OptionEnvvarSettings {
234250
kAllowedInEnvironment,

src/node_internals.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ InitializationResult InitializeOncePerProcess(int argc, char** argv);
324324
InitializationResult InitializeOncePerProcess(
325325
int argc,
326326
char** argv,
327-
InitializationSettingsFlags flags);
327+
InitializationSettingsFlags flags,
328+
ProcessFlags::Flags process_flags = ProcessFlags::kNoFlags);
328329
void TearDownOncePerProcess();
329330
void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s);
330331
void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s);

0 commit comments

Comments
 (0)