Fix the creation of the dumpdir directory in stress_floppy Makefile
[ltp-debian.git] / testcases / pounder21 / infinite_loop.c
blob35b5c2145a88463d1de04ca1b2df01db5c739087
1 /* Repeatedly run a program. */
3 /*
4 * Copyright (C) 2003-2006 IBM
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
22 #include <stdio.h>
23 #include <string.h>
24 #include <strings.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
30 #include "debug.h"
32 static int res = 0;
33 static char *progname;
34 static pid_t test_pgrp;
35 static FILE *tty_fp;
37 static void int_func(int signum) {
38 pounder_fprintf(tty_fp, "%s: Killed by interrupt. Last exit code = %d.\n",
39 progname, res);
40 kill(-test_pgrp, SIGTERM);
41 exit(res);
44 int main(int argc, char *argv[]) {
45 int stat;
46 pid_t pid;
47 struct sigaction zig;
48 unsigned int revs = 0;
50 if (argc < 2) {
51 printf("Usage: %s command [args]\n", argv[0]);
52 exit(1);
55 tty_fp = fdopen(3, "w+");
56 if (tty_fp == NULL) {
57 tty_fp = fopen("/dev/tty", "w+");
58 if (tty_fp == NULL) {
59 perror("stdout");
60 exit(2);
64 progname = rindex(argv[1], '/');
65 if (progname == NULL) {
66 progname = argv[1];
67 } else {
68 progname++;
71 /* Set up signals */
72 memset(&zig, 0x00, sizeof(zig));
73 zig.sa_handler = int_func;
74 sigaction(SIGINT, &zig, NULL);
75 sigaction(SIGTERM, &zig, NULL);
77 /* set up process groups so that we can kill the
78 * loop test and descendants easily */
80 while (1) {
81 pounder_fprintf(tty_fp, "%s: %s loop #%d.\n", progname, start_msg, revs++);
82 pid = fork();
83 if (pid == 0) {
84 if (setpgrp() < 0) {
85 perror("setpgid");
88 // run the program
89 if (argc > 3) {
90 stat = execvp(argv[1], &argv[1]);
91 } else {
92 stat = execvp(argv[1], &argv[1]);
95 perror(argv[1]);
97 exit(-1);
100 /* save the pgrp of the spawned process */
101 test_pgrp = pid;
103 // wait for it to be done
104 if (waitpid(pid, &stat, 0) != pid) {
105 perror("waitpid");
106 exit(1);
109 // interrogate it
110 if (WIFSIGNALED(stat)) {
111 pounder_fprintf(tty_fp, "%s: %s on signal %d.\n",
112 progname, fail_msg, WTERMSIG(stat));
113 res = 255;
114 } else {
115 res = WEXITSTATUS(stat);
116 if (res == 0) {
117 pounder_fprintf(tty_fp, "%s: %s.\n", progname, pass_msg);
118 } else if (res < 0 || res == 255) {
119 pounder_fprintf(tty_fp, "%s: %s with code %d.\n",
120 progname, abort_msg, res);
121 exit(-1);
122 // FIXME: add test to blacklist
123 } else {
124 pounder_fprintf(tty_fp, "%s: %s with code %d.\n",
125 progname, fail_msg, res);