@@ -159,11 +159,11 @@ NAN_METHOD(CauseSegfault) {
159
159
}
160
160
161
161
NAN_METHOD (RegisterHandler) {
162
+ int sigArg = -1 , signal = SIGSEGV;
162
163
// if passed a path, we'll set the log name to whatever is provided
163
- // this will allow users to use the logs in error reporting without redirecting
164
- // sdterr
165
- logPath[0 ] = ' \0 ' ;
166
- if (info.Length () == 1 ) {
164
+ // this will allow users to use the logs in error reporting without
165
+ // redirecting stderr
166
+ if (info.Length () >= 1 ) {
167
167
if (info[0 ]->IsString ()) {
168
168
v8::String::Utf8Value utf8Value (info[0 ]->ToString ());
169
169
@@ -172,11 +172,19 @@ NAN_METHOD(RegisterHandler) {
172
172
len = len > BUFF_SIZE ? BUFF_SIZE : len;
173
173
174
174
strncpy (logPath, *utf8Value, len);
175
- logPath[127 ] = ' \0 ' ;
175
+ logPath[BUFF_SIZE-1 ] = ' \0 ' ;
176
+ if (info.Length () >= 2 && info[1 ]->IsNumber ()) {
177
+ sigArg = 1 ;
178
+ }
179
+ } else if (info[0 ]->IsNumber ()) {
180
+ sigArg = 0 ;
176
181
} else {
177
182
return ThrowError (" First argument must be a string." );
178
183
}
179
184
}
185
+ if (sigArg >= 0 ) {
186
+ signal = Nan::To<int32_t >(info[sigArg]).FromMaybe (SIGSEGV);
187
+ }
180
188
181
189
#ifdef _WIN32
182
190
AddVectoredExceptionHandler (1 , segfault_handler);
@@ -186,14 +194,54 @@ NAN_METHOD(RegisterHandler) {
186
194
sigemptyset (&sa.sa_mask );
187
195
sa.sa_sigaction = segfault_handler;
188
196
sa.sa_flags = SA_SIGINFO;
189
- sigaction (SIGSEGV , &sa, NULL );
197
+ sigaction (signal , &sa, NULL );
190
198
#endif
191
199
}
192
200
193
201
extern " C" {
194
202
NAN_MODULE_INIT (init) {
203
+ logPath[0 ] = ' \0 ' ;
195
204
Nan::SetMethod (target, " registerHandler" , RegisterHandler);
196
205
Nan::SetMethod (target, " causeSegfault" , CauseSegfault);
206
+ // Export signal names and values.
207
+ #define EXPORT (signal ) \
208
+ Nan::ForceSet (target, Nan::New<v8::String>(#signal ).ToLocalChecked (), Nan::New (signal ), v8::ReadOnly)
209
+ // Not all of these make sense to register handlers on, but we'll let
210
+ // the user decide that. Presumably you're using this package because
211
+ // you're seeing an unexpected signal of some sort. Hopefully it's
212
+ // included below. (And if not, just pass it by integer value.)
213
+ EXPORT (SIGHUP);
214
+ EXPORT (SIGINT);
215
+ EXPORT (SIGQUIT);
216
+ EXPORT (SIGILL);
217
+ EXPORT (SIGTRAP);
218
+ EXPORT (SIGABRT);
219
+ EXPORT (SIGBUS);
220
+ EXPORT (SIGFPE);
221
+ EXPORT (SIGKILL);
222
+ EXPORT (SIGUSR1);
223
+ EXPORT (SIGUSR2);
224
+ EXPORT (SIGSEGV);
225
+ EXPORT (SIGUSR2);
226
+ EXPORT (SIGPIPE);
227
+ EXPORT (SIGALRM);
228
+ EXPORT (SIGTERM);
229
+ EXPORT (SIGSTKFLT);
230
+ EXPORT (SIGCHLD);
231
+ EXPORT (SIGCONT);
232
+ EXPORT (SIGSTOP);
233
+ EXPORT (SIGTSTP);
234
+ EXPORT (SIGTTIN);
235
+ EXPORT (SIGTTOU);
236
+ EXPORT (SIGURG);
237
+ EXPORT (SIGXCPU);
238
+ EXPORT (SIGXFSZ);
239
+ EXPORT (SIGVTALRM);
240
+ EXPORT (SIGPROF);
241
+ EXPORT (SIGWINCH);
242
+ EXPORT (SIGIO);
243
+ EXPORT (SIGPWR);
244
+ EXPORT (SIGSYS);
197
245
}
198
246
199
247
NODE_MODULE (segfault_handler, init)
0 commit comments