Skip to content

Commit 4921e8f

Browse files
committed
src: update signal handling and inspector support for z/OS
Address review comments: - Add comments explaining why the z/OS-specific code is needed: the SIGABRT handler (see zosSignalExit()) and the --nohard_abort V8 flag. - Remove the copyright headers from the new node_zos.{h,cc} and zos_setlibpath.cc files. Other changes: - Create the z/OS signal handler thread after blocking all signals on the main thread, so it starts with every signal blocked. It installs its own alternate signal stack before unblocking them, so no signal can be delivered on a thread without one. - Remove zosIgnorePipeChildSignals(). All signals are blocked on the main thread, and SIGPIPE is already ignored by ResetSignalHandlers(). - Zero-initialize the struct sigaction in zosSignalExit(). - Check the result of sigaltstack() and report alternate stack setup failures in zosStart(). - Check pthread_cancel() for any non-zero return, not just -1. - Do not append directories to LIBPATH if already present. - Handle SIGUSR1 for the inspector on z/OS, where the main thread blocks all signals. - Call RegisterProduct() from zosStart() so that Node.js is registered for Tailored Fit Pricing. Assisted-by: IBM Bob Signed-off-by: Gaby Baghdadi <baghdadi@ca.ibm.com>
1 parent c8ffe28 commit 4921e8f

5 files changed

Lines changed: 134 additions & 81 deletions

File tree

‎src/inspector_agent.cc‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
#include <climits> // PTHREAD_STACK_MIN
4242
#endif // __POSIX__
4343

44+
#ifdef __MVS__
45+
#include "node_zos.h"
46+
#endif // __MVS__
47+
4448
#include <algorithm>
4549
#include <cstring>
4650
#include <sstream>
@@ -90,6 +94,29 @@ static void StartIoThreadWakeup(int signo, siginfo_t* info, void* ucontext) {
9094
uv_sem_post(&start_io_thread_semaphore);
9195
}
9296

97+
#ifdef __MVS__
98+
// On z/OS, zosCreateSignalHandler() blocks all signals on the main thread, and
99+
// the signal handler thread in node_zos.cc keeps SIGUSR1 blocked (see
100+
// SignalHandlerThread()). This dedicated thread is therefore the one that
101+
// receives SIGUSR1: it installs the handler and unblocks the signal, so
102+
// delivery doesn't depend on the main thread.
103+
static void* SigUsr1Posting(void*) {
104+
uv_thread_setname("SigUsr1Posting");
105+
sigset_t sigmask;
106+
// The handler is registered with SA_ONSTACK, and the alternate stack is
107+
// per-thread.
108+
zosSetThreadAltStack();
109+
RegisterSignalHandler(SIGUSR1, StartIoThreadWakeup);
110+
sigemptyset(&sigmask);
111+
sigaddset(&sigmask, SIGUSR1);
112+
CHECK_EQ(0, pthread_sigmask(SIG_UNBLOCK, &sigmask, nullptr));
113+
while (true) {
114+
CHECK_EQ(pause(), -1);
115+
CHECK_EQ(errno, EINTR);
116+
}
117+
}
118+
#endif // __MVS__
119+
93120
inline void* StartIoThreadMain(void* unused) {
94121
uv_thread_setname("SignalInspector");
95122
for (;;) {
@@ -136,7 +163,30 @@ static int StartWatchdogThread() {
136163
uv_sem_destroy(&start_io_thread_semaphore);
137164
return -err;
138165
}
166+
#ifdef __MVS__
167+
// Start the thread that receives SIGUSR1 (see SigUsr1Posting()) instead of
168+
// registering the handler here: this thread's mask blocks SIGUSR1 on z/OS.
169+
// The new thread is created while all signals are still blocked, so it
170+
// starts with SIGUSR1 blocked.
171+
pthread_t thr;
172+
pthread_attr_t thrattr;
173+
CHECK_EQ(0, pthread_attr_init(&thrattr));
174+
CHECK_EQ(0, pthread_attr_setdetachstate(&thrattr, PTHREAD_CREATE_DETACHED));
175+
const int thr_err =
176+
pthread_create(&thr, &thrattr, SigUsr1Posting, nullptr);
177+
CHECK_EQ(0, pthread_attr_destroy(&thrattr));
178+
if (thr_err != 0) {
179+
// Leave SIGUSR1 blocked, as no handler was installed. The semaphore is
180+
// not destroyed: the thread started above is waiting on it.
181+
CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, nullptr));
182+
fprintf(stderr, "node[%u]: pthread_create: %s\n",
183+
uv_os_getpid(), strerror(thr_err));
184+
fflush(stderr);
185+
return -thr_err;
186+
}
187+
#else
139188
RegisterSignalHandler(SIGUSR1, StartIoThreadWakeup);
189+
#endif // __MVS__
140190
// Restore original mask
141191
CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, nullptr));
142192
return 0;
@@ -152,12 +202,19 @@ static int StartDebugSignalHandler() {
152202
debug_signal_handler_started = true;
153203
}
154204
}
205+
#ifndef __MVS__
155206
// Unblock SIGUSR1 on this thread; PlatformInit() left it blocked. A pending
156207
// SIGUSR1 signal will now be delivered.
157208
sigset_t sigmask;
158209
sigemptyset(&sigmask);
159210
sigaddset(&sigmask, SIGUSR1);
160211
CHECK_EQ(0, pthread_sigmask(SIG_UNBLOCK, &sigmask, nullptr));
212+
#endif // !__MVS__
213+
// On z/OS, SIGUSR1 is deliberately left blocked on this thread: the thread
214+
// started by StartWatchdogThread() (SigUsr1Posting()) unblocks it and
215+
// receives it, including a signal that is already pending. Unblocking it here
216+
// would also make this thread, whose signals zosCreateSignalHandler()
217+
// blocks, a receiver of SIGUSR1.
161218
return 0;
162219
}
163220
#endif // __POSIX__

‎src/node.cc‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ void RegisterSignalHandler(int signal,
495495
sa.sa_sigaction = handler;
496496
sa.sa_flags = reset_handler ? SA_RESETHAND : 0;
497497
#ifdef __MVS__
498-
// ON_STACK: to use alt stack (created in zosStart()) to not interfere
498+
// SA_ONSTACK: to use alt stack (created in zosStart()) to not interfere
499499
// with z/OS LE; SA_SIGINFO: to use handler specified by sa_sigaction.
500500
sa.sa_flags |= SA_ONSTACK | SA_SIGINFO;
501501
#endif
@@ -648,6 +648,7 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) {
648648
RegisterSignalHandler(SIGINT, SignalExit, true);
649649
RegisterSignalHandler(SIGTERM, SignalExit, true);
650650
#ifdef __MVS__
651+
// See zosSignalExit() (called by SignalExit()) for its SIGABRT handling.
651652
RegisterSignalHandler(SIGABRT, SignalExit, true);
652653
#endif
653654
}
@@ -870,6 +871,8 @@ static ExitCode ProcessGlobalArgsInternal(std::vector<std::string>* args,
870871
}
871872

872873
#ifdef __MVS__
874+
// A trap with V8 frames on the stack ends with SIGKILL on z/OS. Those
875+
// frames lack XPLINK metadata, so LE can't traverse the stack.
873876
V8::SetFlagsFromString("--nohard_abort", sizeof("--nohard_abort") - 1);
874877
#endif
875878

@@ -1866,9 +1869,6 @@ static ExitCode StartInternal(int argc, char** argv) {
18661869
});
18671870

18681871
uv_loop_configure(uv_default_loop(), UV_METRICS_IDLE_TIME);
1869-
#ifdef __MVS__
1870-
zosIgnorePipeChildSignals();
1871-
#endif
18721872
std::string sea_config = per_process::cli_options->experimental_sea_config;
18731873
if (!sea_config.empty()) {
18741874
#if defined(DISABLE_SINGLE_EXECUTABLE_APPLICATION)

‎src/node_zos.cc‎

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
// Copyright Joyent, Inc. and other Node contributors.
2-
//
3-
// Permission is hereby granted, free of charge, to any person obtaining a
4-
// copy of this software and associated documentation files (the
5-
// "Software"), to deal in the Software without restriction, including
6-
// without limitation the rights to use, copy, modify, merge, publish,
7-
// distribute, sublicense, and/or sell copies of the Software, and to permit
8-
// persons to whom the Software is furnished to do so, subject to the
9-
// following conditions:
10-
//
11-
// The above copyright notice and this permission notice shall be included
12-
// in all copies or substantial portions of the Software.
13-
//
14-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15-
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16-
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17-
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18-
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19-
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20-
// USE OR OTHER DEALINGS IN THE SOFTWARE.
1+
// z/OS-specific process setup: alternate signal stack, signal handler
2+
// threads and product registration. Used by node.cc and inspector_agent.cc;
3+
// compiled on z/OS only.
214

225
#include "node_zos.h"
236
#include "env-inl.h"
@@ -28,6 +11,8 @@
2811
#include <signal.h>
2912
#include <sys/msg.h>
3013

14+
#include <string>
15+
3116
#ifndef NJS_PRODUCT_OWNER
3217
#error "NJS_PRODUCT_OWNER must be defined"
3318
#endif
@@ -48,6 +33,9 @@ namespace {
4833

4934
uv_thread_t signalHandlerThread;
5035
char* galtStack = nullptr;
36+
// Size of the alternate signal stack of the main thread and of the threads
37+
// that receive signals (see zosSetThreadAltStack()).
38+
const unsigned int kAltStackSize = SIGSTKSZ + 1024 * 1024;
5139

5240
void __zosAtExit() {
5341
if (galtStack != nullptr) {
@@ -67,29 +55,57 @@ void zosDestroyThread() {
6755
__mainTerminating();
6856
}
6957

58+
void zosSetThreadAltStack() {
59+
// sigaltstack() is per-thread, so a thread that may receive signals needs
60+
// its own alternate stack for SA_ONSTACK handlers to run on it.
61+
// The stack is intentionally never freed: the callers live until process
62+
// exit.
63+
char* alt_stack = static_cast<char*>(malloc(kAltStackSize));
64+
CHECK_NOT_NULL(alt_stack);
65+
stack_t ss = {.ss_sp = alt_stack, .ss_size = kAltStackSize, .ss_flags = 0};
66+
CHECK_EQ(0, sigaltstack(&ss, nullptr));
67+
}
68+
7069
static void SignalHandlerThread(void* data) {
71-
while (1) {
70+
// Process-directed signals are delivered to this thread (all signals are
71+
// blocked on the others), so it needs its own alternate stack.
72+
zosSetThreadAltStack();
73+
74+
// This thread was created with all signals blocked, so that none could be
75+
// delivered before the alternate stack was installed. Unblock them now.
76+
sigset_t set;
77+
sigfillset(&set);
78+
#if HAVE_INSPECTOR
79+
// Keep SIGUSR1 blocked, as PlatformInit() left it, until the inspector
80+
// installs its handler (see SigUsr1Posting() in inspector_agent.cc).
81+
sigdelset(&set, SIGUSR1);
82+
#endif
83+
CHECK_EQ(0, pthread_sigmask(SIG_UNBLOCK, &set, nullptr));
84+
85+
while (true) {
7286
CHECK_EQ(pause(), -1);
7387
CHECK_EQ(errno, EINTR);
7488
}
7589
}
7690

7791
void zosCreateSignalHandler() {
78-
uv_thread_create(&signalHandlerThread, SignalHandlerThread, nullptr);
79-
// Block all signals on the main thread
92+
// Block all signals on the main thread. The signal handler thread inherits
93+
// this mask and unblocks the signals once its alternate stack is set.
8094
sigset_t set;
8195
sigfillset(&set);
8296
CHECK_EQ(0, pthread_sigmask(SIG_BLOCK, &set, nullptr));
97+
CHECK_EQ(
98+
0, uv_thread_create(&signalHandlerThread, SignalHandlerThread, nullptr));
8399
}
84100

85101
void zosCancelSignalHandler() {
86-
if (pthread_cancel(signalHandlerThread) == -1) abort();
102+
if (pthread_cancel(signalHandlerThread) != 0) abort();
87103
}
88104

89105
void zosSignalExit(int signo, siginfo_t* info, void* ucontext) {
90106
zosDestroyThread();
91107
// Use alternate stack in case LE uses main thread's sp upon termination.
92-
struct sigaction new_action;
108+
struct sigaction new_action = {};
93109
new_action.sa_flags = SA_ONSTACK;
94110
new_action.sa_handler = SIG_DFL;
95111
sigaction(signo, &new_action, nullptr);
@@ -99,16 +115,6 @@ void zosSignalExit(int signo, siginfo_t* info, void* ucontext) {
99115
raise(signo);
100116
}
101117

102-
void zosIgnorePipeChildSignals() {
103-
struct sigaction new_action;
104-
new_action.sa_handler = SIG_IGN;
105-
sigfillset(&new_action.sa_mask);
106-
sigaction(SIGPIPE, &new_action, nullptr);
107-
new_action.sa_handler = SIG_IGN;
108-
sigfillset(&new_action.sa_mask);
109-
sigaction(SIGCHLD, &new_action, nullptr);
110-
}
111-
112118
static void RegisterProduct() {
113119
std::string major_version = std::to_string(NODE_MAJOR_VERSION);
114120
std::string product_owner = NJS_PRODUCT_OWNER;
@@ -163,16 +169,25 @@ ExitCode zosStart() {
163169
}
164170

165171
// Create an alternate stack for signal processing
166-
const unsigned int STACK_SIZE = SIGSTKSZ + 1024 * 1024;
167-
galtStack = reinterpret_cast<char*>(malloc(STACK_SIZE));
172+
galtStack = reinterpret_cast<char*>(malloc(kAltStackSize));
168173
if (galtStack == nullptr) {
174+
fprintf(stderr, "Error: could not allocate the alternate signal stack.\n");
175+
return ExitCode::kGenericUserError;
176+
}
177+
178+
stack_t ss = {.ss_sp = galtStack, .ss_size = kAltStackSize, .ss_flags = 0};
179+
if (sigaltstack(&ss, 0) != 0) {
180+
fprintf(stderr,
181+
"Error: sigaltstack() failed, errno=%d.\n",
182+
errno);
169183
return ExitCode::kGenericUserError;
170184
}
171-
stack_t ss = {.ss_sp = galtStack, .ss_size = STACK_SIZE, .ss_flags = 0};
172-
sigaltstack(&ss, 0);
173185

174186
if (__doLogMemoryUsage()) atexit(__zosAtExit);
175187

188+
// Register Node.js for Tailored Fit Pricing.
189+
RegisterProduct();
190+
176191
return ExitCode::kNoFailure;
177192
}
178193

‎src/node_zos.h‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,3 @@
1-
// Copyright Joyent, Inc. and other Node contributors.
2-
//
3-
// Permission is hereby granted, free of charge, to any person obtaining a
4-
// copy of this software and associated documentation files (the
5-
// "Software"), to deal in the Software without restriction, including
6-
// without limitation the rights to use, copy, modify, merge, publish,
7-
// distribute, sublicense, and/or sell copies of the Software, and to permit
8-
// persons to whom the Software is furnished to do so, subject to the
9-
// following conditions:
10-
//
11-
// The above copyright notice and this permission notice shall be included
12-
// in all copies or substantial portions of the Software.
13-
//
14-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15-
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16-
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17-
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18-
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19-
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20-
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21-
221
#ifndef SRC_NODE_ZOS_H_
232
#define SRC_NODE_ZOS_H_
243

@@ -33,8 +12,9 @@ ExitCode zosStart();
3312
void zosCreateSignalHandler();
3413
void zosCancelSignalHandler();
3514
void zosSignalExit(int signo, siginfo_t* info, void* ucontext);
36-
void zosIgnorePipeChildSignals();
3715
void zosDestroyThread();
16+
// used by inspector_agent.cc:
17+
void zosSetThreadAltStack();
3818

3919
} // namespace node
4020
#endif // SRC_NODE_ZOS_H_

‎src/zos_setlibpath.cc‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
///////////////////////////////////////////////////////////////////////////////
2-
// Licensed Materials - Property of IBM
3-
// (C) Copyright IBM Corp. 2024. All Rights Reserved.
4-
// US Government Users Restricted Rights - Use, duplication
5-
// or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
6-
///////////////////////////////////////////////////////////////////////////////
7-
81
// This is linked to the node executable, so the global libpath object's
92
// constructor searches for libnode DLL and sets LIBPATH, so libnode.so can be
103
// loaded.
114

125
#include <errno.h>
136
#include <libgen.h>
147
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <string.h>
1510
#include <sys/ps.h>
1611
#include <unistd.h>
1712

18-
#include <sstream>
13+
#include <string>
1914

2015
namespace {
16+
/* Append dir to the colon-separated list unless it's already in list. */
17+
void append_unique(std::string& list, const std::string& dir) {
18+
if ((":" + list + ":").find(":" + dir + ":") != std::string::npos) return;
19+
if (!list.empty()) list += ':';
20+
list += dir;
21+
}
22+
2123
class __setlibpath {
2224
public:
2325
__setlibpath() {
@@ -75,14 +77,13 @@ class __setlibpath {
7577
return;
7678
}
7779

78-
/* Append new paths to libpath. */
79-
std::ostringstream libpath;
80-
const char* lpenv = getenv("LIBPATH");
81-
libpath << (lpenv != nullptr ? lpenv : "");
82-
libpath << ":" << p2 << "/lib";
83-
libpath << ":" << p << "/lib";
84-
libpath << ":" << p << "/lib.target";
85-
setenv("LIBPATH", libpath.str().c_str(), 1);
80+
/* Append new paths to libpath, skipping any already present. */
81+
const char* env = getenv("LIBPATH");
82+
std::string libpath = env != nullptr ? env : "";
83+
append_unique(libpath, std::string(p2) + "/lib");
84+
append_unique(libpath, std::string(p) + "/lib");
85+
append_unique(libpath, std::string(p) + "/lib.target");
86+
setenv("LIBPATH", libpath.c_str(), 1);
8687
return;
8788
}
8889
}

0 commit comments

Comments
 (0)