Back to index

glibc  2.9
tst-exec4.c
Go to the documentation of this file.
00001 /* Signal handler and mask set in thread which calls exec.
00002    Copyright (C) 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <pthread.h>
00022 #include <signal.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <unistd.h>
00026 
00027 
00028 static void *
00029 tf (void *arg)
00030 {
00031   /* Ignore SIGUSR1 and block SIGUSR2.  */
00032   if (sigignore (SIGUSR1) != 0)
00033     {
00034       puts ("sigignore failed");
00035       exit (1);
00036     }
00037 
00038   sigset_t ss;
00039   sigemptyset (&ss);
00040   sigaddset (&ss, SIGUSR2);
00041   if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
00042     {
00043       puts ("1st run: sigmask failed");
00044       exit (1);
00045     }
00046 
00047   char **oldargv = (char **) arg;
00048   size_t n = 1;
00049   while (oldargv[n] != NULL)
00050     ++n;
00051 
00052   char **argv = (char **) alloca ((n + 1) * sizeof (char *));
00053   for (n = 0; oldargv[n + 1] != NULL; ++n)
00054     argv[n] = oldargv[n + 1];
00055   argv[n++] = (char *) "--direct";
00056   argv[n] = NULL;
00057 
00058   execv (argv[0], argv);
00059 
00060   puts ("execv failed");
00061 
00062   exit (1);
00063 }
00064 
00065 
00066 static int
00067 do_test (int argc, char *argv[])
00068 {
00069   if (argc == 1)
00070     {
00071       /* This is the second call.  Perform the test.  */
00072       struct sigaction sa;
00073 
00074       if (sigaction (SIGUSR1, NULL, &sa) != 0)
00075        {
00076          puts ("2nd run: sigaction failed");
00077          return 1;
00078        }
00079       if (sa.sa_handler != SIG_IGN)
00080        {
00081          puts ("SIGUSR1 not ignored");
00082          return 1;
00083        }
00084 
00085       sigset_t ss;
00086       if (pthread_sigmask (SIG_SETMASK, NULL, &ss) != 0)
00087        {
00088          puts ("2nd run: sigmask failed");
00089          return 1;
00090        }
00091       if (! sigismember (&ss, SIGUSR2))
00092        {
00093          puts ("SIGUSR2 not blocked");
00094          return 1;
00095        }
00096 
00097       return 0;
00098     }
00099 
00100   pthread_t th;
00101   if (pthread_create (&th, NULL, tf, argv) != 0)
00102     {
00103       puts ("create failed");
00104       exit (1);
00105     }
00106 
00107   /* This call should never return.  */
00108   pthread_join (th, NULL);
00109 
00110   puts ("join returned");
00111 
00112   return 1;
00113 }
00114 
00115 #define TEST_FUNCTION do_test (argc, argv)
00116 #include "../test-skeleton.c"