Blame gnulib-tests/test-sigaction.c

Packit Service fdd496
/* Test of sigaction() function.
Packit Service fdd496
   Copyright (C) 2008-2017 Free Software Foundation, Inc.
Packit Service fdd496
Packit Service fdd496
   This program is free software: you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation; either version 3 of the License, or
Packit Service fdd496
   (at your option) any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
/* Written by Eric Blake <ebb9@byu.net>, 2008.  */
Packit Service fdd496
Packit Service fdd496
#include <config.h>
Packit Service fdd496
Packit Service fdd496
#include <signal.h>
Packit Service fdd496
Packit Service fdd496
#include "signature.h"
Packit Service fdd496
SIGNATURE_CHECK (sigaction, int, (int, struct sigaction const *,
Packit Service fdd496
                                  struct sigaction *));
Packit Service fdd496
Packit Service fdd496
#include <stddef.h>
Packit Service fdd496
Packit Service fdd496
#include "macros.h"
Packit Service fdd496
Packit Service fdd496
#ifndef SA_NOCLDSTOP
Packit Service fdd496
# define SA_NOCLDSTOP 0
Packit Service fdd496
#endif
Packit Service fdd496
#ifndef SA_ONSTACK
Packit Service fdd496
# define SA_ONSTACK 0
Packit Service fdd496
#endif
Packit Service fdd496
#ifndef SA_RESETHAND
Packit Service fdd496
# define SA_RESETHAND 0
Packit Service fdd496
#endif
Packit Service fdd496
#ifndef SA_RESTART
Packit Service fdd496
# define SA_RESTART 0
Packit Service fdd496
#endif
Packit Service fdd496
#ifndef SA_SIGINFO
Packit Service fdd496
# define SA_SIGINFO 0
Packit Service fdd496
#endif
Packit Service fdd496
#ifndef SA_NOCLDWAIT
Packit Service fdd496
# define SA_NOCLDWAIT 0
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
/* Define a mask of flags required by POSIX.  Some implementations
Packit Service fdd496
   provide other flags as extensions, such as SA_RESTORER, that we
Packit Service fdd496
   must ignore in this test.  */
Packit Service fdd496
#define MASK_SA_FLAGS (SA_NOCLDSTOP | SA_ONSTACK | SA_RESETHAND | SA_RESTART \
Packit Service fdd496
                       | SA_SIGINFO | SA_NOCLDWAIT | SA_NODEFER)
Packit Service fdd496
Packit Service fdd496
/* This test is unsafe in the presence of an asynchronous SIGABRT,
Packit Service fdd496
   because we install a signal-handler that is intentionally not
Packit Service fdd496
   async-safe.  Hopefully, this does not lead to too many reports of
Packit Service fdd496
   false failures, since people don't generally use 'kill -s SIGABRT'
Packit Service fdd496
   to end a runaway program.  */
Packit Service fdd496
Packit Service fdd496
static void
Packit Service fdd496
handler (int sig)
Packit Service fdd496
{
Packit Service fdd496
  static int entry_count;
Packit Service fdd496
  struct sigaction sa;
Packit Service fdd496
  ASSERT (sig == SIGABRT);
Packit Service fdd496
  ASSERT (sigaction (SIGABRT, NULL, &sa) == 0);
Packit Service fdd496
  ASSERT ((sa.sa_flags & SA_SIGINFO) == 0);
Packit Service fdd496
  switch (entry_count++)
Packit Service fdd496
    {
Packit Service fdd496
    case 0:
Packit Service fdd496
      ASSERT ((sa.sa_flags & SA_RESETHAND) == 0);
Packit Service fdd496
      ASSERT (sa.sa_handler == handler);
Packit Service fdd496
      break;
Packit Service fdd496
    case 1:
Packit Service fdd496
      /* This assertion fails on glibc-2.3.6 systems with LinuxThreads,
Packit Service fdd496
         when this program is linked with -lpthread, due to the sigaction()
Packit Service fdd496
         override in libpthread.so.  */
Packit Service fdd496
#if !(defined __GLIBC__ || defined __UCLIBC__)
Packit Service fdd496
      ASSERT (sa.sa_handler == SIG_DFL);
Packit Service fdd496
#endif
Packit Service fdd496
      break;
Packit Service fdd496
    default:
Packit Service fdd496
      ASSERT (0);
Packit Service fdd496
    }
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
int
Packit Service fdd496
main (void)
Packit Service fdd496
{
Packit Service fdd496
  struct sigaction sa;
Packit Service fdd496
  struct sigaction old_sa;
Packit Service fdd496
  sa.sa_handler = handler;
Packit Service fdd496
Packit Service fdd496
  sa.sa_flags = 0;
Packit Service fdd496
  ASSERT (sigemptyset (&sa.sa_mask) == 0);
Packit Service fdd496
  ASSERT (sigaction (SIGABRT, &sa, NULL) == 0);
Packit Service fdd496
  ASSERT (raise (SIGABRT) == 0);
Packit Service fdd496
Packit Service fdd496
  sa.sa_flags = SA_RESETHAND | SA_NODEFER;
Packit Service fdd496
  ASSERT (sigaction (SIGABRT, &sa, &old_sa) == 0);
Packit Service fdd496
  ASSERT ((old_sa.sa_flags & MASK_SA_FLAGS) == 0);
Packit Service fdd496
  ASSERT (old_sa.sa_handler == handler);
Packit Service fdd496
  ASSERT (raise (SIGABRT) == 0);
Packit Service fdd496
Packit Service fdd496
  sa.sa_handler = SIG_DFL;
Packit Service fdd496
  ASSERT (sigaction (SIGABRT, &sa, &old_sa) == 0);
Packit Service fdd496
  ASSERT ((old_sa.sa_flags & SA_SIGINFO) == 0);
Packit Service fdd496
#if !(defined __GLIBC__ || defined __UCLIBC__) /* see above */
Packit Service fdd496
  ASSERT (old_sa.sa_handler == SIG_DFL);
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
  sa.sa_handler = SIG_IGN;
Packit Service fdd496
  ASSERT (sigaction (SIGABRT, &sa, NULL) == 0);
Packit Service fdd496
  ASSERT (raise (SIGABRT) == 0);
Packit Service fdd496
  ASSERT (sigaction (SIGABRT, NULL, &old_sa) == 0);
Packit Service fdd496
  ASSERT (old_sa.sa_handler == SIG_IGN);
Packit Service fdd496
  ASSERT (raise (SIGABRT) == 0);
Packit Service fdd496
Packit Service fdd496
  return 0;
Packit Service fdd496
}