Back to index

glibc  2.9
eintr.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <pthread.h>
00021 #include <signal.h>
00022 #include <unistd.h>
00023 
00024 
00025 static int the_sig;
00026 
00027 
00028 static void
00029 eintr_handler (int sig)
00030 {
00031   if (sig != the_sig)
00032     {
00033       write (STDOUT_FILENO, "eintr_handler: signal number wrong\n", 35);
00034       _exit (1);
00035     }
00036   write (STDOUT_FILENO, ".", 1);
00037 }
00038 
00039 
00040 static void *
00041 eintr_source (void *arg)
00042 {
00043   struct timespec ts = { .tv_sec = 0, .tv_nsec = 500000 };
00044 
00045   if (arg == NULL)
00046     {
00047       sigset_t ss;
00048       sigemptyset (&ss);
00049       sigaddset (&ss, the_sig);
00050       pthread_sigmask (SIG_BLOCK, &ss, NULL);
00051     }
00052 
00053   while (1)
00054     {
00055       if (arg != NULL)
00056        pthread_kill (*(pthread_t *) arg, the_sig);
00057       else
00058        kill (getpid (), the_sig);
00059 
00060       nanosleep (&ts, NULL);
00061     }
00062 
00063   /* NOTREACHED */
00064   return NULL;
00065 }
00066 
00067 
00068 static void
00069 setup_eintr (int sig, pthread_t *thp)
00070 {
00071   struct sigaction sa;
00072   sigemptyset (&sa.sa_mask);
00073   sa.sa_flags = 0;
00074   sa.sa_handler = eintr_handler;
00075   if (sigaction (sig, &sa, NULL) != 0)
00076     {
00077       puts ("setup_eintr: sigaction failed");
00078       exit (1);
00079     }
00080   the_sig = sig;
00081 
00082   /* Create the thread which will fire off the signals.  */
00083   pthread_t th;
00084   if (pthread_create (&th, NULL, eintr_source, thp) != 0)
00085     {
00086       puts ("setup_eintr: pthread_create failed");
00087       exit (1);
00088     }
00089 }