1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
|
/*
* libslack - https://libslack.org
*
* Copyright (C) 1999-2004, 2010, 2020-2023 raf <raf@raf.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*
* 20230824 raf <raf@raf.org>
*/
/*
=head1 NAME
I<libslack(sig)> - I<ISO C>-compliant signal handling module
=head1 SYNOPSIS
#include <slack/std.h>
#include <slack/sig.h>
typedef void signal_handler_t(int signo);
typedef void signal_siginfo_handler_t(int signo, siginfo_t *siginfo, void *context);
int signal_set_handler(int signo, int flags, signal_handler_t *handler);
int signal_set_siginfo_handler(int signo, int flags, signal_siginfo_handler_t *siginfo_handler);
int signal_addset(int signo_handled, int signo_blocked);
int signal_received(int signo);
int signal_raise(int signo);
int signal_handle(int signo);
void signal_handle_all(void);
=head1 DESCRIPTION
This module provides functions for I<ISO C>-compliant signal handling. I<ISO
C>-compliant signal handlers may only set a single value of type
I<sig_atomic_t>. This is a very restrictive requirement. This module allows
you to specify unrestricted signal handlers while (almost) transparently
enforcing I<ISO C> compliance.
When a handled signal arrives, an I<ISO C>-compliant signal handler is
invoked to merely record the fact that the signal was received. Then, in the
main thread of execution, when I<signal_handle(3)> or
I<signal_handle_all(3)> is invoked, the client supplied signal handlers for
all signals received since the last invocation of I<signal_handle(3)> or
I<signal_handle_all(3)> are invoked.
Since the user-supplied signal handlers execute in the main thread on
execution, they are not subject to the normal restrictions on signal
handlers. Also, they will execute with the same signals blocked as the real
signal handler.
However, this indirection doesn't apply to the more dramatic signals (i.e.
C<SIGILL>, C<SIGABRT>, C<SIGFPE>, C<SIGSEGV>, C<SIGBUS> and C<SIGSYS>) whose
signal I<handler> functions are installed directly as the real signal
handlers. Signal I<siginfo> handler functions installed with
I<signal_set_siginfo_handler(3)> are installed directly as well. In general,
these I<handler> and I<siginfo_handler> functions probably won't be I<ISO
C>-compliant signal handler functions but as long as they are
I<POSIX>-compliant signal handler functions (which is far less restrictive)
it will be fine.
=over 4
=cut
One of the nicest things about I<POSIX> MT programming is how it simplifies
signal handling. A single thread can be devoted to handling signals
synchronously with I<sigwait(3)> while all other threads go about their
business, free from signals, and most importantly, free from the code
clutter of checking every blocking system call to see if it was interrupted
by a signal. To do this, block all signals in all threads except for the
signal-handling thread. Then that thread will be the only one to receive
signals, according to I<POSIX>.
Unfortunately, if you have a I<Linux> system, you may notice that the MT
signal handling is not I<POSIX>-compliant in this way. On the other hand,
I<Linux> provides the I<signalfd(2)> system call which is even better. If
you use that, you won't need this module at all.
*/
#include "config.h"
#include "std.h"
#include "sig.h"
#include "err.h"
#ifdef NSIG
#define SIG_MAX NSIG
#else
#ifdef _NSIG
#define SIG_MAX _NSIG
#else
#define SIG_MAX 32
#endif
#endif
typedef struct real_signal_handler_t real_signal_handler_t;
struct real_signal_handler_t
{
struct sigaction action[1];
signal_handler_t *handler;
};
#ifndef TEST
static real_signal_handler_t g_handler[SIG_MAX];
static volatile sig_atomic_t g_received[SIG_MAX];
/*
C<void signal_catcher(int signo)>
This is an I<ISO C>-compliant signal handler function. It is used to catch all
signals. It records that the signal C<signo> was received.
*/
static void signal_catcher(int signo)
{
++g_received[signo];
}
/*
=item C<int signal_set_handler(int signo, int flags, signal_handler_t *handler)>
Installs C<handler> as the signal handler function for the signal C<signo>.
C<flags> is used as the I<sa_flags> field of the C<struct sigaction *act>
argument to I<sigaction(2)>. The actual function that is set as the signal
handler is not C<handler>. It is an I<ISO C>-compliant signal handler
function that just records the fact that a signal was received. C<handler>
will only be invoked when the client invokes I<signal_handle(3)> or
I<signal_handle_all(3)> from the main thread of execution. So there are no
restrictions on C<handler>. When C<handler> is invoked, the C<signo> signal
will be blocked. Other signals can also be blocked when C<handler> is
invoked using I<signal_addset(3)> or I<sigaddset(3)>. Several signals do not
allow such treatment. Behaviour upon return from their handler function is
undefined (or defined, but not very pleasant). They are C<SIGILL>,
C<SIGABRT>, C<SIGFPE>, C<SIGBUS>, C<SIGSEGV> and C<SIGSYS>. Handler function
supplied for these signals are installed as the real signal handlers. On
success, returns C<0>. On error, returns C<-1> with C<errno> set
appropriately.
=cut
*/
int signal_set_handler(int signo, int flags, signal_handler_t *handler)
{
real_signal_handler_t *h = &g_handler[signo];
sigemptyset(&h->action->sa_mask);
sigaddset(&h->action->sa_mask, signo);
h->action->sa_flags = flags;
if (handler == SIG_DFL || handler == SIG_IGN)
h->action->sa_handler = handler;
else
{
switch (signo)
{
case SIGILL:
case SIGABRT:
case SIGFPE:
case SIGSEGV:
#ifdef SIGBUS
case SIGBUS:
#endif
#ifdef SIGSYS
case SIGSYS:
#endif
h->action->sa_handler = handler;
break;
default:
h->action->sa_handler = signal_catcher;
break;
}
}
h->handler = handler;
g_received[signo] = 0;
return sigaction(signo, h->action, NULL);
}
/*
=item C<int signal_set_siginfo_handler(int signo, int flags, signal_siginfo_handler_t *siginfo_handler)>
Installs C<siginfo_handler> as the signal I<siginfo> handler function for
the signal C<signo>. A I<siginfo> handler takes three arguments (C<int
signo>, C<siginfo_t *siginfo> and C<void *context>), rather than the usual
single argument (C<int signo>). C<flags> is used as the I<sa_flags> field of
the I<struct sigaction *act> argument to I<sigaction(2)> (combined with
C<SA_SIGINFO>). Unlike usual signal C<handler> functions, signal
I<siginfo_hndler> functions are installed directly. So there are
I<POSIX>-imposed restrictions on C<siginfo_handler>. When C<siginfo_handler>
is invoked, the C<signo> signal will be blocked. Other signals can also be
blocked when C<siginfo_handler> is invoked using I<signal_addset(3)> or
I<sigaddset(3)>. On success, returns C<0>. On error, returns C<-1> with
C<errno> set appropriately.
=cut
*/
int signal_set_siginfo_handler(int signo, int flags, signal_siginfo_handler_t *siginfo_handler)
{
#ifdef SA_SIGINFO
real_signal_handler_t *h = &g_handler[signo];
sigemptyset(&h->action->sa_mask);
sigaddset(&h->action->sa_mask, signo);
h->action->sa_flags = flags | SA_SIGINFO;
h->action->sa_sigaction = siginfo_handler;
h->handler = NULL;
g_received[signo] = 0;
return sigaction(signo, h->action, NULL);
#else
return set_errno(ENOSYS);
#endif
}
/*
=item C<int signal_addset(int signo_handled, int signo_blocked)>
Adds C<signo_blocked> to the set of signals that will be blocked when the
handler for signal C<signo_handled> is invoked. This must not be called
before the call to I<signal_set_handler(3)> for C<signo_handled> which
initialises the signal set to include C<signo_handled>. On success, returns
C<0>. On error, returns C<-1> with C<errno> set appropriately.
=cut
*/
int signal_addset(int signo_handled, int signo_blocked)
{
real_signal_handler_t *h = &g_handler[signo_handled];
return sigaddset(&h->action->sa_mask, signo_blocked);
}
/*
=item C<int signal_received(int signo)>
Returns the number of times that the signal C<signo> has been received since
the last call to I<signal_handle(3)> with C<signo> as its argument
or I<signal_handle_all(3)>. On error (i.e. C<signo> is out of
range), returns C<-1> and sets C<errno> set to C<EINVAL>.
=cut
*/
int signal_received(int signo)
{
if (signo < 0 || signo >= SIG_MAX)
return set_errno(EINVAL);
return g_received[signo];
}
/*
=item C<int signal_raise(int signo)>
Simulates the receipt of the signal specified by C<signo>. On success,
returns the number of unhandled C<signo> signals (including this one). On
error (i.e. if C<signo> is out of range), returns C<-1> and sets C<errno> to
C<EINVAL>.
=cut
*/
int signal_raise(int signo)
{
if (signo < 0 || signo >= SIG_MAX)
return set_errno(EINVAL);
return ++g_received[signo];
}
/*
=item C<int signal_handle(int signo)>
Executes the installed signal handler for the signal C<signo>. The C<signo>
signal (and any others added with I<signal_addset(3)>) is blocked during the
execution of the signal handler. Clears the received status of the C<signo>
signal. On success, returns C<0>. On error, returns C<-1> with C<errno> set
appropriately.
=cut
*/
int signal_handle(int signo)
{
real_signal_handler_t *h = &g_handler[signo];
sigset_t origmask[1];
if (!h->handler)
return set_errno(EINVAL);
if (sigprocmask(SIG_BLOCK, &h->action->sa_mask, origmask) == -1)
return -1;
h->handler(signo);
g_received[signo] = 0;
return sigprocmask(SIG_SETMASK, origmask, NULL);
}
/*
=item C<void signal_handle_all(void)>
Executes the installed signal handlers for all signals that have been
received since the last call to I<signal_handle(3)> or
I<signal_handle_all(3)>. During the execution of each signal handler, the
corresponding signal (and possibly others) will be blocked. Clears the
received status of all signals handled.
=cut
*/
void signal_handle_all(void)
{
int signo;
for (signo = 0; signo < SIG_MAX; ++signo)
if (signal_received(signo))
signal_handle(signo);
}
/*
=back
=head1 ERRORS
=over 4
=item C<EINVAL>
When a signal number argument is out of range.
=back
=head1 MT-Level
I<Unsafe>
=head1 EXAMPLE
#include <slack/std.h>
#include <slack/sig.h>
void hup(int signo) { printf("SIGHUP received\n"); }
void term(int signo) { printf("SIGTERM received\n"); exit(EXIT_SUCCESS); }
int main(int ac, char **av)
{
if (signal_set_handler(SIGHUP, 0, hup) == -1)
return EXIT_FAILURE;
if (signal_set_handler(SIGTERM, 0, term) == -1)
return EXIT_FAILURE;
for (;;)
{
char mesg[BUFSIZ];
ssize_t n;
signal_handle_all();
// Signals arriving here are lost
while ((n = read(STDIN_FILENO, mesg, BUFSIZ)) > 0)
fprintf(stderr, "%*.*s", n, n, mesg);
if (n == -1 && errno == EINTR)
continue;
exit((n == -1) ? EXIT_FAILURE : EXIT_SUCCESS);
}
return EXIT_SUCCESS;
}
=head1 SEE ALSO
I<libslack(3)>,
I<prog(3)>
=head1 AUTHOR
20230824 raf <raf@raf.org>
=cut
*/
#endif
#ifdef TEST
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/stat.h>
const char * const results[3] =
{
"Received SIGHUP\n",
"Received SIGTERM\n",
"Received SIGCHLD\n"
};
static void hup(int signo)
{
printf("%s", results[0]);
}
static void term(int signo)
{
printf("%s", results[1]);
exit(EXIT_SUCCESS);
}
static void chld_siginfo(int signo, siginfo_t *siginfo, void *context)
{
printf("%s from pid %d", results[2], (int)siginfo->si_pid);
}
static void child(void)
{
char mesg[BUFSIZ];
ssize_t n;
/* We know signals have arrived so they will be handled immediately */
for (;;)
{
signal_handle_all();
/* Signals arriving here are lost */
while ((n = read(STDIN_FILENO, mesg, BUFSIZ)) > 0)
fprintf(stderr, "%*.*s", (int)n, (int)n, mesg);
switch (n)
{
case -1:
{
if (errno != EINTR)
{
fprintf(stderr, "read error\n");
exit(EXIT_FAILURE);
}
signal_handle_all();
break;
}
default:
{
fprintf(stderr, "read = %d\n", (int)n);
exit(EXIT_FAILURE);
}
}
}
}
static int verify(int test, const char *name, const char *msg1, const char *msg2)
{
char buf[BUFSIZ];
int fd;
ssize_t bytes;
size_t msg1_length;
if ((fd = open(name, O_RDONLY)) == -1)
{
printf("Test %d: failed to create sig file: %s (%s)\n", test, name, strerror(errno));
return 1;
}
memset(buf, 0, BUFSIZ);
bytes = read(fd, buf, BUFSIZ);
close(fd);
unlink(name);
if (bytes == -1)
{
printf("Test %d: failed to read sig file: %s (%s)\n", test, name, strerror(errno));
return 1;
}
msg1_length = strlen(msg1);
if (strncmp(buf, msg1, msg1_length))
{
printf("Test %d: msg file produced incorrect input:\nshould be\n%s\nwas:\n%*.*s\n", test, msg1, (int)msg1_length, (int)msg1_length, buf);
return 1;
}
if (strcmp(buf + msg1_length, msg2))
{
printf("Test %d: msg file produced incorrect input:\nshould be:\n%s\nwas:\n%s\n", test + 1, msg2, buf + msg1_length);
return 1;
}
return 0;
}
int main(int ac, char **av)
{
const char *out = "sig.out";
int sync1[2];
int sync2[2];
pid_t pid;
int errors = 0;
if (ac == 2 && !strcmp(av[1], "help"))
{
printf("usage: %s\n", *av);
return EXIT_SUCCESS;
}
printf("Testing: %s\n", "sig");
if (signal_set_handler(SIGHUP, 0, hup) == -1)
++errors, printf("Test1: failed to set the SIGHUP handler (%s)\n", strerror(errno));
if (signal_set_handler(SIGTERM, 0, term) == -1)
++errors, printf("Test2: failed to set the SIGTERM handler (%s)\n", strerror(errno));
if (pipe(sync1) == -1)
{
printf("Failed to perform test - pipe() failed (%s)\n", strerror(errno));
return EXIT_FAILURE;
}
if (pipe(sync2) == -1)
{
printf("Failed to perform test - pipe() failed (%s)\n", strerror(errno));
return EXIT_FAILURE;
}
if (errors == 0)
{
switch (pid = fork())
{
case -1:
{
printf("Failed to perform test - fork() failed (%s)\n", strerror(errno));
break;
}
case 0:
{
ssize_t rc;
char ack;
/* Send output to a file for the parent to verify */
if (!freopen(out, "w", stdout))
{
fprintf(stderr, "Failed to perform test - freopen() stdout to %s failed (%s)\n", out, strerror(errno));
exit(EXIT_FAILURE);
}
/* Inform the parent that we're ready to receive signals */
close(sync1[0]);
write(sync1[1], "1", 1);
close(sync1[1]);
/* Wait until the parent has sent the signals */
close(sync2[1]);
do { rc = read(sync2[0], &ack, 1); } while (rc == -1 && errno == EINTR);
close(sync2[0]);
/* Child begins, looping over signal_handle_all() and read() */
child();
break; /* unreached */
}
default:
{
int status;
char ack;
/* Wait until the child is ready to receive signals */
close(sync1[1]);
read(sync1[0], &ack, 1);
close(sync1[0]);
/* Send the signals */
if (kill(pid, SIGHUP) == -1)
++errors, printf("Test3: failed to perform test - kill(%d, HUP) failed (%s)\n", (int)pid, strerror(errno));
if (kill(pid, SIGTERM) == -1)
++errors, printf("Test4: failed to perform test - kill(%d, TERM) failed (%s)\n", (int)pid, strerror(errno));
/* Inform the child that we've sent the signals */
close(sync2[0]);
write(sync2[1], &ack, 1);
close(sync2[1]);
/* Wait for the child to terminate */
if (waitpid(pid, &status, 0) == -1)
{
fprintf(stderr, "Failed to evaluate test - waitpid(%d) failed (%s)\n", (int)pid, strerror(errno));
break;
}
if (WIFSIGNALED(status) && WTERMSIG(status) != SIGABRT)
fprintf(stderr, "Failed to evaluate test - child received signal %d\n", WTERMSIG(status));
if (WIFEXITED(status) && WEXITSTATUS(status) != EXIT_SUCCESS)
fprintf(stderr, "Failed to evaluate test - child exit status %d\n", WEXITSTATUS(status));
/* Verify the output */
errors += verify(5, out, results[0], results[1]);
break;
}
}
}
if (signal_set_siginfo_handler(SIGCHLD, 0, chld_siginfo) == -1)
++errors, printf("Test6: failed to set the SIGCHLD sigaction (%s)\n", strerror(errno));
if (errors)
printf("%d/6 tests failed\n", errors);
else
printf("All tests passed\n");
return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
#endif
/* vi:set ts=4 sw=4: */
|