Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,11 @@
'deps/histogram/histogram.gyp:histogram',
],
}],
[ 'OS=="zos" and node_shared=="true"', {
'sources+': [
'src/zos_setlibpath.cc',
],
}],
[ 'error_on_warn=="true"', {
'cflags': ['-Werror'],
'xcode_settings': {
Expand Down Expand Up @@ -981,6 +986,17 @@
'Ws2_32',
],
}],
[ 'OS=="zos"', {
'sources+': [
'src/node_zos.h',
'src/node_zos.cc',
],
'conditions': [
[ 'node_shared=="true"', {
'ldflags+': [ '-Wl,-bedit=no' ],
}],
],
}],
[ 'node_use_openssl=="true"', {
'sources': [
'<@(node_crypto_sources)',
Expand Down Expand Up @@ -1501,6 +1517,11 @@
],

'conditions': [
[ 'OS=="zos"', {
'sources+': [
'src/zos_setlibpath.cc',
],
}],
['OS=="solaris"', {
'ldflags': [ '-I<(SHARED_INTERMEDIATE_DIR)' ]
}],
Expand Down Expand Up @@ -1669,6 +1690,12 @@
[ 'OS in "linux mac openharmony"', {
'defines': ['NODE_JS2C_USE_STRING_LITERALS'],
}],
[ 'OS=="zos"', {
'dependencies+': [
'<(static_zoslib_gyp):zoslib',
'<(static_zoslib_gyp):zoslib_alnewdel',
],
}],
[ 'debug_node=="true"', {
'cflags!': [ '-O3' ],
'cflags': [ '-g', '-O0' ],
Expand Down
42 changes: 42 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
#endif
#endif // NODE_USE_V8_WASM_TRAP_HANDLER

#if defined(__MVS__)
#include "node_zos.h"
#endif

// ========== global C headers ==========

#include <fcntl.h> // _O_RDWR
Expand Down Expand Up @@ -190,7 +194,11 @@ const char* conf_section_name = STRINGIFY(NODE_OPENSSL_CONF_NAME);
#ifdef __POSIX__
void SignalExit(int signo, siginfo_t* info, void* ucontext) {
ResetStdio();
#ifdef __MVS__
zosSignalExit(signo, info, ucontext);
#else
raise(signo);
#endif
}
#endif // __POSIX__

Expand Down Expand Up @@ -486,6 +494,9 @@ void RegisterSignalHandler(int signal,
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = handler;
sa.sa_flags = reset_handler ? SA_RESETHAND : 0;
#ifdef __MVS__
sa.sa_flags |= SA_ONSTACK | SA_SIGINFO;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of these changes could use comments that explain why specifically z/OS needs these and other OSes don't – e.g. SA_ONSTACK and SA_SIGINFO aren't z/OS-specific per se, so it's not obvious why the conditional is here (I might guess because of SIGABRT being listed below as a handled signal – but let's not have this be a silent assumption?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see https://www.ibm.com/docs/en/zos/3.1.0?topic=functions-sigaction-examine-change-signal-action.
For SA_SIGINFO, sa_sigaction is specified for the sigaction() call here. For SA_ONSTACK, it's to use the alternate signal stack created initially in zosStart(), to not interfere with the z/OS Language Environment runtime environment.

Would you like such comments added in the code, or in the commit's message?

#endif
sigfillset(&sa.sa_mask);
CHECK_EQ(sigaction(signal, &sa, nullptr), 0);
}
Expand Down Expand Up @@ -626,6 +637,9 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) {
if (!(flags & ProcessInitializationFlags::kNoDefaultSignalHandling)) {
RegisterSignalHandler(SIGINT, SignalExit, true);
RegisterSignalHandler(SIGTERM, SignalExit, true);
#ifdef __MVS__
RegisterSignalHandler(SIGABRT, SignalExit, true);
#endif
}

if (!(flags & ProcessInitializationFlags::kNoAdjustResourceLimits)) {
Expand Down Expand Up @@ -836,6 +850,10 @@ static ExitCode ProcessGlobalArgsInternal(std::vector<std::string>* args,
v8_args_as_char_ptr.resize(argc);
}

#ifdef __MVS__
V8::SetFlagsFromString("--nohard_abort", sizeof("--nohard_abort") - 1);
#endif

// Anything that's still in v8_argv is not a V8 or a node option.
for (size_t i = 1; i < v8_args_as_char_ptr.size(); i++)
errors->push_back("bad option: " + std::string(v8_args_as_char_ptr[i]));
Expand Down Expand Up @@ -1284,6 +1302,9 @@ InitializeOncePerProcessInternal(
if (!(flags & ProcessInitializationFlags::kNoPrintHelpOrVersionOutput)) {
if (per_process::cli_options->print_version) {
printf("%s\n", NODE_VERSION);
#if defined(__MVS__)
__build_version();
#endif
result->exit_code_ = ExitCode::kNoFailure;
result->early_return_ = true;
return result;
Expand Down Expand Up @@ -1481,6 +1502,9 @@ InitializeOncePerProcessInternal(
}
}
}
#ifdef __MVS__
zosCreateSignalHandler();
#endif

if (!(flags & ProcessInitializationFlags::kNoInitializeNodeV8Platform)) {
uv_thread_setname("node-MainThread");
Expand Down Expand Up @@ -1614,6 +1638,11 @@ void TearDownOncePerProcess() {
#if HAVE_OPENSSL
crypto::CleanupCachedRootCertificates();
#endif // HAVE_OPENSSL

#ifdef __MVS__
zosCancelSignalHandler();
zosDestroyThread();
#endif
}

ExitCode GenerateAndWriteSnapshotData(const SnapshotData** snapshot_data_ptr,
Expand Down Expand Up @@ -1807,6 +1836,13 @@ static ExitCode StartInternal(int argc, char** argv) {
// Hack around with the argv pointer. Used for process.title = "blah".
argv = uv_setup_args(argc, argv);

#ifdef __MVS__
ExitCode exit_code = zosStart();
if (exit_code != ExitCode::kNoFailure) {
return exit_code;
}
#endif

std::shared_ptr<InitializationResultImpl> result =
InitializeOncePerProcessInternal(
std::vector<std::string>(argv, argv + argc),
Expand All @@ -1816,6 +1852,9 @@ static ExitCode StartInternal(int argc, char** argv) {
FPrintF(stderr, "%s: %s\n", result->args().at(0), error);
}
if (result->early_return()) {
#ifdef __MVS__
zosDestroyThread();
#endif
return result->exit_code_enum();
}

Expand All @@ -1832,6 +1871,9 @@ static ExitCode StartInternal(int argc, char** argv) {
});

uv_loop_configure(uv_default_loop(), UV_METRICS_IDLE_TIME);
#ifdef __MVS__
zosIgnorePipeChildSignals();
#endif
std::string sea_config = per_process::cli_options->experimental_sea_config;
if (!sea_config.empty()) {
#if defined(DISABLE_SINGLE_EXECUTABLE_APPLICATION)
Expand Down
179 changes: 179 additions & 0 deletions src/node_zos.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#include "node_zos.h"
#include "env-inl.h"
#include "node_internals.h"
#include "node_watchdog.h"

#include <pthread.h>
#include <signal.h>
#include <sys/msg.h>

#ifndef NJS_PRODUCT_OWNER
#error "NJS_PRODUCT_OWNER must be defined"
#endif

#ifndef NJS_FEATURE_NAME
#error "NJS_FEATURE_NAME must be defined"
#endif

#ifndef NJS_PRODUCT_NAME
#error "NJS_PRODUCT_NAME must be defined"
#endif

#ifndef NJS_PID
#error "NJS_PID must be defined"
#endif

namespace {

uv_thread_t signalHandlerThread;
char* galtStack = nullptr;

void __zosAtExit() {
if (galtStack != nullptr) {
free(galtStack);
galtStack = nullptr;
}
}
} // namespace

namespace node {

void zosDestroyThread() {
uv_queue_work(nullptr, nullptr, nullptr, nullptr);
SigintWatchdogHelper::GetInstance()->Stop();
msgctl(uv_backend_fd(uv_default_loop()), IPC_RMID, nullptr);
// Tell zoslib that main process is terminating, for its diagnostics:
__mainTerminating();
}

static void SignalHandlerThread(void* data) {
while (1) {
CHECK_EQ(pause(), -1);
CHECK_EQ(errno, EINTR);
}
}

void zosCreateSignalHandler() {
uv_thread_create(&signalHandlerThread, SignalHandlerThread, nullptr);
// Block all signals on the main thread
sigset_t set;
sigfillset(&set);
CHECK_EQ(0, pthread_sigmask(SIG_BLOCK, &set, nullptr));
}

void zosCancelSignalHandler() {
if (pthread_cancel(signalHandlerThread) == -1) abort();
}

void zosSignalExit(int signo, siginfo_t* info, void* ucontext) {
zosDestroyThread();
// Use alternate stack in case LE uses main thread's sp upon termination.
struct sigaction new_action;
new_action.sa_flags = SA_ONSTACK;
new_action.sa_handler = SIG_DFL;
sigaction(signo, &new_action, nullptr);
if (signo == SIGABRT)
abort(); // from zoslib
else
raise(signo);
}

void zosIgnorePipeChildSignals() {
struct sigaction new_action;
new_action.sa_handler = SIG_IGN;
sigfillset(&new_action.sa_mask);
sigaction(SIGPIPE, &new_action, nullptr);
new_action.sa_handler = SIG_IGN;
sigfillset(&new_action.sa_mask);
sigaction(SIGCHLD, &new_action, nullptr);
}

static void RegisterProduct() {
std::string major_version = std::to_string(NODE_MAJOR_VERSION);
std::string product_owner = NJS_PRODUCT_OWNER;
std::string feature_name = NJS_FEATURE_NAME;
std::string product_name = NJS_PRODUCT_NAME;
std::string pid = NJS_PID;

std::string val;
if (credentials::SafeGetenv("NODE_SMF89_REGISTRATION_VERBOSE", &val))
printf("Product registration data:\n"
"Product Name: %s\n"
"PID Name: %s\n"
"Major Version: %s\n"
"Product Owner: %s\n"
"Feature Name: %s\n",
product_name.c_str(),
pid.c_str(),
major_version.c_str(),
product_owner.c_str(),
feature_name.c_str());

uint64_t rc = __registerProduct(major_version.c_str(),
product_owner.c_str(),
feature_name.c_str(),
product_name.c_str(),
pid.c_str());

if (rc && !credentials::SafeGetenv("NODE_SMF89_SUPPRESS_WARNING", &val)) {
fprintf(stderr,
"WARNING: Could not register product with IFAUSAGE, "
"rc = %lu\n",
rc);
if (const char* errorString = getIFAUsageErrorString(rc))
fprintf(stderr, "%s\n", errorString);

fprintf(stderr,
"WARNING: Product usage data may not be collected. For more "
"details on the product registration data, set environment "
"variable NODE_SMF89_REGISTRATION_VERBOSE=1. To suppress this "
"warning message set the environment variable "
"NODE_SMF89_SUPPRESS_WARNING=1.\n");
}
}

ExitCode zosStart() {
// Initialize environment variables in zoslib:
zoslib_config_t config{
.RUNTIME_LIMIT_ENVAR = "__NODERUNTIMELIMIT",
.CCSID_GUESS_BUF_SIZE_ENVAR = "__NODECCSIDGUESSBUFSIZE"};
if (__update_envar_names(&config) != 0) {
return ExitCode::kGenericUserError;
}

// Create an alternate stack for signal processing
const unsigned int STACK_SIZE = SIGSTKSZ + 1024 * 1024;
galtStack = reinterpret_cast<char*>(malloc(STACK_SIZE));
if (galtStack == nullptr) {
return ExitCode::kGenericUserError;
}
stack_t ss = {.ss_sp = galtStack, .ss_size = STACK_SIZE, .ss_flags = 0};
sigaltstack(&ss, 0);

if (__doLogMemoryUsage()) atexit(__zosAtExit);

return ExitCode::kNoFailure;
}

} // namespace node
40 changes: 40 additions & 0 deletions src/node_zos.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#ifndef SRC_NODE_ZOS_H_
#define SRC_NODE_ZOS_H_

#include "node_exit_code.h"

#include <csignal>

namespace node {

// used by node.cc:
ExitCode zosStart();
void zosCreateSignalHandler();
void zosCancelSignalHandler();
void zosSignalExit(int signo, siginfo_t* info, void* ucontext);
void zosIgnorePipeChildSignals();
void zosDestroyThread();

} // namespace node
#endif // SRC_NODE_ZOS_H_
Loading
Loading