1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/* Event-Based Branch Facility Tests. Skeleton framwork.
*
* Copyright IBM Corp. 2013
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Contributors:
* IBM Corporation, Adhemerval Zanella - Initial implementation.
*/
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/wait.h>
#ifndef TIMEOUT
# define TIMEOUT 10
#endif
static const char *program = NULL;
static pid_t pid;
static void
__attribute__ ((noreturn))
signal_handler (int sig __attribute__ ((unused)))
{
int killed;
int status;
/* Send signal. */
kill (pid, SIGKILL);
/* Wait for it to terminate. */
int i;
for (i = 0; i < 5; ++i)
{
killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
if (killed != 0)
break;
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 100000000;
nanosleep (&ts, NULL);
}
if (killed != 0 && killed != pid)
{
fprintf (stderr, "%s: error: failed to kill test process: %s",
program, strerror (errno));
exit (EXIT_FAILURE);
}
if (killed == 0 || (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL))
fprintf (stderr, "%s: error: timed out, killed the child process\n",
program);
else if (WIFSTOPPED (status))
fprintf (stderr, "%s: error: timed out: the child process was %s\n",
program, strsignal (WSTOPSIG (status)));
else if (WIFSIGNALED (status))
fprintf (stderr, "%s: timed out: the child process got signal %s\n",
program, strsignal (WTERMSIG (status)));
else
fprintf (stderr, "%s: timed out: killed the child process but it exited %d\n",
program, WEXITSTATUS (status));
exit (EXIT_FAILURE);
}
static struct option options[] =
{
#define OPT_DIRECT 1000
{ "direct", no_argument, NULL, OPT_DIRECT },
{ NULL, 0, NULL, 0 }
};
int
main (int argc, char *argv[])
{
int direct = 0;
pid_t termpid;
int opt;
int status;
program = argv[0];
/* Make sure we see all message, even those on stdout. */
setvbuf (stdout, NULL, _IONBF, 0);
while ((opt = getopt_long (argc, argv, "+", options, NULL)) != -1)
switch (opt)
{
case '?':
exit (1);
case OPT_DIRECT:
direct = 1;
break;
}
if (direct)
return EBB_TEST ();
/* Set up the test environment:
* 1. set up the timer
* 2. fork and execute the function. */
pid = fork ();
if (pid == 0)
{
/* We put the test process in its own group so that if it bogusly
generates any job control signals, they won't hit the whole build. */
setpgid (0, 0);
/* Execute the test function and exit with the return value. */
exit (EBB_TEST ());
}
else if (pid < 0)
{
printf ("%s: error: fork failed: %s\n", program, strerror (errno));
exit (EXIT_FAILURE);
}
signal (SIGALRM, signal_handler);
alarm (TIMEOUT);
/* Make sure we clean up if the wrapper gets interrupted. */
signal (SIGINT, signal_handler);
do {
termpid = waitpid (pid, &status, 0);
} while (termpid == -1 && errno == EINTR);
if (termpid == -1)
{
fprintf (stderr, "%s: error: waiting for test program failed: %m\n",
program);
exit (EXIT_FAILURE);
}
else if (termpid != pid)
{
fprintf (stderr, "%s: error: wrong test program terminated: expected "
"%ld, got %ld\n", program,
(long int) pid, (long int) termpid);
exit (EXIT_FAILURE);
}
return WEXITSTATUS (status);
}
|