/* Test that triggers glibc assertion:

     hurdsig.c:804: _hurd_internal_post_signal: Unexpected error: (ipc/send) invalid destination port.

   Warning!  The process hangs after that and is impossible to kill with
   ^C etc., so don't run it in the foreground.  */

#include <errno.h>
#include <error.h>
#include <pthread.h>
#include <signal.h>
#include <unistd.h>

void
handler (int signum)
{
}

void *
thread_fn (void *arg)
{
  return NULL;
}

int
main (int argc, char *argv[])
{
  int err;
  struct sigaction sa;
  pthread_t thread;

  err = sigaction (SIGUSR1, NULL, &sa);
  if (err)
    error (1, errno, "Getting signal handler");

  sa.sa_handler = handler;
  err = sigaction (SIGUSR1, &sa, NULL);
  if (err)
    error (1, errno, "Setting signal handler");

  err = pthread_create (&thread, NULL, thread_fn, NULL);
  if (err)
    error (1, err, "Starting thread");

  /* Give THREAD time to exit.  */
  sleep (1);

  /* Joining or detaching thread before this point avoids triggering
     the assertion, instead pthread_kill properly returns an error.
     Sending the signal to own thread or using raise also avoids it.  */
  err = pthread_kill (thread, SIGUSR1);
  if (err)
    error (1, err, "Raising interrupt signal");

  return 0;
}
