Back to index

glibc  2.9
tst-sigset2.c
Go to the documentation of this file.
00001 /* sigset_SIG_HOLD_bug.c [BZ #1951] */
00002 #include <errno.h>
00003 #include <error.h>
00004 #include <inttypes.h>
00005 #include <signal.h>
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <string.h>
00009 #include <unistd.h>
00010 #include <sys/types.h>
00011 #include <sys/wait.h>
00012 
00013 #define TEST_SIG SIGINT
00014 
00015 
00016 /* Print mask of blocked signals for this process */
00017 static void
00018 printSigMask (const char *msg)
00019 {
00020   sigset_t currMask;
00021   int sig;
00022   int cnt;
00023 
00024   if (msg != NULL)
00025     printf ("%s", msg);
00026 
00027   if (sigprocmask (SIG_BLOCK, NULL, &currMask) == -1)
00028     error (1, errno, "sigaction");
00029 
00030   cnt = 0;
00031   for (sig = 1; sig < NSIG; sig++)
00032     {
00033       if (sigismember (&currMask, sig))
00034        {
00035          cnt++;
00036          printf ("\t\t%d (%s)\n", sig, strsignal (sig));
00037         }
00038     }
00039 
00040   if (cnt == 0)
00041     printf ("\t\t<empty signal set>\n");
00042 } /* printSigMask */
00043 
00044 static void
00045 handler (int sig)
00046 {
00047   printf ("Caught signal %d\n", sig);
00048   printSigMask ("Signal mask in handler\n");
00049   printf ("Handler returning\n");
00050   _exit (1);
00051 } /* handler */
00052 
00053 static void
00054 printDisposition (sighandler_t disp)
00055 {
00056   if (disp == SIG_HOLD)
00057     printf ("SIG_HOLD");
00058   else if (disp == SIG_DFL)
00059     printf ("SIG_DFL");
00060   else if (disp == SIG_IGN)
00061     printf ("SIG_IGN");
00062   else
00063     printf ("handled at %" PRIxPTR, (uintptr_t) disp);
00064 } /* printDisposition */
00065 
00066 static int
00067 returnTest1 (void)
00068 {
00069   sighandler_t prev;
00070 
00071   printf ("===== TEST 1 =====\n");
00072   printf ("Blocking signal with sighold()\n");
00073   if (sighold (TEST_SIG) == -1)
00074     error (1, errno, "sighold");
00075   printSigMask ("Signal mask after sighold()\n");
00076 
00077   printf ("About to use sigset() to establish handler\n");
00078   prev = sigset (TEST_SIG, handler);
00079   if (prev == SIG_ERR)
00080     error(1, errno, "sigset");
00081 
00082   printf ("Previous disposition: ");
00083   printDisposition (prev);
00084   printf (" (should be SIG_HOLD)\n");
00085   if (prev != SIG_HOLD)
00086     {
00087       printf("TEST FAILED!!!\n");
00088       return 1;
00089     }
00090   return 0;
00091 } /* returnTest1 */
00092 
00093 static int
00094 returnTest2 (void)
00095 {
00096   sighandler_t prev;
00097 
00098   printf ("\n===== TEST 2 =====\n");
00099 
00100   printf ("About to use sigset() to set SIG_HOLD\n");
00101   prev = sigset (TEST_SIG, SIG_HOLD);
00102   if (prev == SIG_ERR)
00103     error (1, errno, "sigset");
00104 
00105   printf ("Previous disposition: ");
00106   printDisposition (prev);
00107   printf (" (should be SIG_DFL)\n");
00108   if (prev != SIG_DFL)
00109     {
00110       printf("TEST FAILED!!!\n");
00111       return 1;
00112     }
00113   return 0;
00114 } /* returnTest2 */
00115 
00116 static int
00117 returnTest3 (void)
00118 {
00119   sighandler_t prev;
00120 
00121   printf ("\n===== TEST 3 =====\n");
00122 
00123   printf ("About to use sigset() to set SIG_HOLD\n");
00124   prev = sigset (TEST_SIG, SIG_HOLD);
00125   if (prev == SIG_ERR)
00126     error (1, errno, "sigset");
00127 
00128   printf ("About to use sigset() to set SIG_HOLD (again)\n");
00129   prev = sigset (TEST_SIG, SIG_HOLD);
00130   if (prev == SIG_ERR)
00131     error (1, errno, "sigset");
00132 
00133   printf ("Previous disposition: ");
00134   printDisposition (prev);
00135   printf (" (should be SIG_HOLD)\n");
00136   if (prev != SIG_HOLD)
00137     {
00138       printf("TEST FAILED!!!\n");
00139       return 1;
00140     }
00141   return 0;
00142 } /* returnTest3 */
00143 
00144 int
00145 main (int argc, char *argv[])
00146 {
00147   pid_t childPid;
00148 
00149   childPid = fork();
00150   if (childPid == -1)
00151     error (1, errno, "fork");
00152 
00153   if (childPid == 0)
00154     exit (returnTest1 ());
00155 
00156   int status;
00157   if (TEMP_FAILURE_RETRY (waitpid (childPid, &status, 0)) != childPid)
00158     error (1, errno, "waitpid");
00159   int result = !WIFEXITED (status) || WEXITSTATUS (status) != 0;
00160 
00161   childPid = fork();
00162   if (childPid == -1)
00163     error (1, errno, "fork");
00164 
00165   if (childPid == 0)
00166     exit (returnTest2 ());
00167 
00168   if (TEMP_FAILURE_RETRY (waitpid (childPid, &status, 0)) != childPid)
00169     error (1, errno, "waitpid");
00170   result |= !WIFEXITED (status) || WEXITSTATUS (status) != 0;
00171 
00172   childPid = fork();
00173   if (childPid == -1)
00174     error (1, errno, "fork");
00175 
00176   if (childPid == 0)
00177     exit (returnTest3 ());
00178 
00179   if (TEMP_FAILURE_RETRY (waitpid (childPid, &status, 0)) != childPid)
00180     error (1, errno, "waitpid");
00181   result |= !WIFEXITED (status) || WEXITSTATUS (status) != 0;
00182 
00183   return result;
00184 } /* main */