11cb0ef41Sopenharmony_ci#ifndef _WIN32
21cb0ef41Sopenharmony_ci#include <node.h>
31cb0ef41Sopenharmony_ci#include <v8.h>
41cb0ef41Sopenharmony_ci#include <uv.h>
51cb0ef41Sopenharmony_ci#include <assert.h>
61cb0ef41Sopenharmony_ci#include <unistd.h>
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciusing v8::Boolean;
91cb0ef41Sopenharmony_ciusing v8::FunctionCallbackInfo;
101cb0ef41Sopenharmony_ciusing v8::Int32;
111cb0ef41Sopenharmony_ciusing v8::Value;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_civoid Handler(int signo, siginfo_t* siginfo, void* ucontext) {
141cb0ef41Sopenharmony_ci  char signo_char = signo;
151cb0ef41Sopenharmony_ci  int written;
161cb0ef41Sopenharmony_ci  do {
171cb0ef41Sopenharmony_ci    written = write(1, &signo_char, 1);  // write() is signal-safe.
181cb0ef41Sopenharmony_ci  } while (written == -1 && errno == EINTR);
191cb0ef41Sopenharmony_ci  assert(written == 1);
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_civoid RegisterSignalHandler(const FunctionCallbackInfo<Value>& args) {
231cb0ef41Sopenharmony_ci  assert(args[0]->IsInt32());
241cb0ef41Sopenharmony_ci  assert(args[1]->IsBoolean());
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  int32_t signo = args[0].As<Int32>()->Value();
271cb0ef41Sopenharmony_ci  bool reset_handler = args[1].As<Boolean>()->Value();
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  node::RegisterSignalHandler(signo, Handler, reset_handler);
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciNODE_MODULE_INIT() {
331cb0ef41Sopenharmony_ci  NODE_SET_METHOD(exports, "registerSignalHandler", RegisterSignalHandler);
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci#endif  // _WIN32
37