nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * Further, this software is distributed without any warranty that it is |
| 13 | * free of the rightful claim of any third person regarding infringement |
| 14 | * or the like. Any license provided herein, whether implied or |
| 15 | * otherwise, applies only to this software file. Patent licenses, if |
| 16 | * any, provided herein do not apply to combinations of this program with |
| 17 | * other software, or any other product whatsoever. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write the Free Software Foundation, Inc., 59 |
| 21 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 22 | * |
| 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
| 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
| 30 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 31 | * |
robbiew | c170673 | 2002-02-19 18:06:31 +0000 | [diff] [blame] | 32 | * Changelog: |
| 33 | * |
| 34 | * Added timer options: William Jay Huie, IBM |
iyermanoj | 6631451 | 2003-01-27 22:56:23 +0000 | [diff] [blame] | 35 | * 01/27/03 - Added: Manoj Iyer, manjo@mail.utexas.edu |
| 36 | * - option '-p' (pretty printing)i to enabled formatted printing |
| 37 | * of results. |
robbiew | c170673 | 2002-02-19 18:06:31 +0000 | [diff] [blame] | 38 | * |
iyermanoj | 3251858 | 2003-01-28 19:44:34 +0000 | [diff] [blame] | 39 | * 01/27/03 - Added: Manoj Iyer, manjo@mail.utexas.edu |
| 40 | * - added code to print system information |
| 41 | * |
iyermanoj | 8c484e9 | 2003-01-29 02:09:13 +0000 | [diff] [blame] | 42 | * 01/28/03 - Added: Manoj Iyer, manjo@mail.utexas.edu |
| 43 | * - added code to print test exit value. |
iyermanoj | 492e88e | 2003-01-30 06:22:31 +0000 | [diff] [blame] | 44 | * |
| 45 | * 01/29/03 - Added: Manoj Iyer, manjo@mail.utexas.edu |
| 46 | * - added code supresses test start and test end tags. |
subrata_modak | bc833d3 | 2007-07-25 10:12:02 +0000 | [diff] [blame] | 47 | * |
| 48 | * 07/22/07 - Added: Ricardo Salveti de Araujo, rsalveti@linux.vnet.ibm.com |
| 49 | * - added option to create a command file with all failed tests. |
| 50 | * |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 51 | */ |
subrata_modak | 1cad5c0 | 2008-05-19 13:37:14 +0000 | [diff] [blame] | 52 | /* $Id: pan.c,v 1.26 2008/05/19 13:37:14 subrata_modak Exp $ */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 53 | |
| 54 | #include <errno.h> |
| 55 | #include <string.h> |
plars | 404bea3 | 2002-09-04 13:15:55 +0000 | [diff] [blame] | 56 | #include <sys/param.h> |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 57 | #include <sys/types.h> |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 58 | #include <sys/times.h> |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 59 | #include <sys/wait.h> |
| 60 | #include <sys/stat.h> |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 61 | #include <time.h> |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 62 | #include <stdlib.h> |
| 63 | #include <limits.h> |
iyermanoj | 3251858 | 2003-01-28 19:44:34 +0000 | [diff] [blame] | 64 | #include <sys/utsname.h> |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 65 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 66 | #include "splitstr.h" |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 67 | #include "zoolib.h" |
| 68 | |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 69 | /* One entry in the command line collection. */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 70 | struct coll_entry |
| 71 | { |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 72 | char *name; /* tag name */ |
| 73 | char *cmdline; /* command line */ |
| 74 | char *pcnt_f; /* location of %f in the command line args, flag */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 75 | struct coll_entry *next; |
| 76 | }; |
| 77 | |
| 78 | struct collection |
| 79 | { |
| 80 | int cnt; |
| 81 | struct coll_entry **ary; |
| 82 | }; |
| 83 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 84 | struct tag_pgrp |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 85 | { |
| 86 | int pgrp; |
| 87 | int stopping; |
vapier | 9e78ade | 2006-12-13 22:55:21 +0000 | [diff] [blame] | 88 | time_t mystime; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 89 | struct coll_entry *cmd; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 90 | char output[PATH_MAX]; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | struct orphan_pgrp |
| 94 | { |
| 95 | int pgrp; |
| 96 | struct orphan_pgrp *next; |
| 97 | }; |
| 98 | |
iyermanoj | 492e88e | 2003-01-30 06:22:31 +0000 | [diff] [blame] | 99 | static pid_t run_child(struct coll_entry *colle, struct tag_pgrp *active, |
| 100 | int quiet_mode); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 101 | static char *slurp(char *file); |
| 102 | static struct collection *get_collection(char *file, int optind, int argc, |
| 103 | char **argv); |
| 104 | static void pids_running(struct tag_pgrp *running, int keep_active); |
| 105 | static int check_pids(struct tag_pgrp *running, int *num_active, |
subrata_modak | bc833d3 | 2007-07-25 10:12:02 +0000 | [diff] [blame] | 106 | int keep_active, FILE * logfile, FILE * failcmdfile, |
| 107 | struct orphan_pgrp *orphans, int fmt_print, |
| 108 | int *failcnt, int quiet_mode); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 109 | static void propagate_signal(struct tag_pgrp *running, int keep_active, |
| 110 | struct orphan_pgrp *orphans); |
| 111 | static void dump_coll(struct collection *coll); |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 112 | static char *subst_pcnt_f(struct coll_entry *colle); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 113 | static void mark_orphan(struct orphan_pgrp *orphans, pid_t cpid); |
| 114 | static void orphans_running(struct orphan_pgrp *orphans); |
| 115 | static void check_orphans(struct orphan_pgrp *orphans, int sig); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 116 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 117 | static void copy_buffered_output(struct tag_pgrp *running); |
| 118 | static void write_test_start(struct tag_pgrp *running, const char *init_status); |
| 119 | static void write_test_end(struct tag_pgrp *running, |
| 120 | time_t exit_time, char *term_type, int stat_loc, |
| 121 | int term_id, struct tms *tms1, struct tms *tms2); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 122 | |
robbiew | c170673 | 2002-02-19 18:06:31 +0000 | [diff] [blame] | 123 | //wjh |
| 124 | static char PAN_STOP_FILE[] = "PAN_STOP_FILE"; |
| 125 | |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 126 | static char *panname = NULL; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 127 | static char *test_out_dir = NULL; /* dir to buffer output to */ |
| 128 | zoo_t zoofile; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 129 | static char *reporttype = NULL; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 130 | |
vapier | 53ef45c | 2006-02-16 05:56:51 +0000 | [diff] [blame] | 131 | /* zoolib */ |
| 132 | int rec_signal; /* received signal */ |
| 133 | int send_signal; /* signal to send */ |
| 134 | |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 135 | /* Debug Bits */ |
| 136 | int Debug = 0; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 137 | #define Dbuffile 0x000400 /* buffer file use */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 138 | #define Dsetup 0x000200 /* one-time set-up */ |
| 139 | #define Dshutdown 0x000100 /* killed by signal */ |
| 140 | #define Dexit 0x000020 /* exit status */ |
| 141 | #define Drunning 0x000010 /* current pids running */ |
| 142 | #define Dstartup 0x000004 /* started command */ |
| 143 | #define Dstart 0x000002 /* started command */ |
| 144 | #define Dwait 0x000001 /* wait interrupted */ |
| 145 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 146 | int |
| 147 | main(int argc, char **argv) |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 148 | { |
| 149 | extern char *optarg; |
| 150 | extern int optind; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 151 | char *zooname = NULL; /* name of the zoo file to use */ |
| 152 | char *filename = "/dev/null"; /* filename to read test tags from */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 153 | char *logfilename = NULL; |
subrata_modak | bc833d3 | 2007-07-25 10:12:02 +0000 | [diff] [blame] | 154 | char *failcmdfilename = NULL; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 155 | char *outputfilename = NULL; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 156 | struct collection *coll = NULL; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 157 | struct tag_pgrp *running; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 158 | struct orphan_pgrp *orphans, *orph; |
iyermanoj | 3251858 | 2003-01-28 19:44:34 +0000 | [diff] [blame] | 159 | struct utsname unamebuf; |
| 160 | FILE *logfile = NULL; |
subrata_modak | bc833d3 | 2007-07-25 10:12:02 +0000 | [diff] [blame] | 161 | FILE *failcmdfile = NULL; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 162 | int keep_active = 1; |
| 163 | int num_active = 0; |
iyermanoj | 3251858 | 2003-01-28 19:44:34 +0000 | [diff] [blame] | 164 | int failcnt = 0; /* count of total testcases that failed. */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 165 | int err, i; |
| 166 | int starts = -1; |
robbiew | c170673 | 2002-02-19 18:06:31 +0000 | [diff] [blame] | 167 | int run_time = -1; char modifier = 'm'; int ret = 0; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 168 | int stop; |
| 169 | int go_idle; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 170 | int has_brakes = 0; /* stop everything if a test case fails */ |
| 171 | int sequential = 0; /* run tests sequentially */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 172 | int fork_in_road = 0; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 173 | int exit_stat; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 174 | int track_exit_stats = 0; /* exit non-zero if any test exits non-zero */ |
iyermanoj | 6631451 | 2003-01-27 22:56:23 +0000 | [diff] [blame] | 175 | int fmt_print = 0; /* enables formatted printing of logfiles. */ |
iyermanoj | 492e88e | 2003-01-30 06:22:31 +0000 | [diff] [blame] | 176 | int quiet_mode = 0; /* supresses test start and test end tags. */ |
iyermanoj | 3251858 | 2003-01-28 19:44:34 +0000 | [diff] [blame] | 177 | int c; |
| 178 | pid_t cpid; |
nstraz | b755b28 | 2003-03-03 23:35:55 +0000 | [diff] [blame] | 179 | struct sigaction sa; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 180 | |
subrata_modak | bc833d3 | 2007-07-25 10:12:02 +0000 | [diff] [blame] | 181 | while ((c = getopt(argc, argv, "AO:Sa:C:d:ef:hl:n:o:pqr:s:t:x:y")) != -1) { |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 182 | switch (c) { |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 183 | case 'A': /* all-stop flag */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 184 | has_brakes = 1; |
| 185 | track_exit_stats = 1; |
| 186 | break; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 187 | case 'O': /* output buffering directory */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 188 | test_out_dir = strdup(optarg); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 189 | break; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 190 | case 'S': /* run tests sequentially */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 191 | sequential = 1; |
| 192 | break; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 193 | case 'a': /* name of the zoo file to use */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 194 | zooname = strdup(optarg); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 195 | break; |
subrata_modak | bc833d3 | 2007-07-25 10:12:02 +0000 | [diff] [blame] | 196 | case 'C': /* name of the file where all failed commands will be */ |
| 197 | failcmdfilename = strdup(optarg); |
| 198 | break; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 199 | case 'd': /* debug options */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 200 | sscanf(optarg, "%i", &Debug); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 201 | break; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 202 | case 'e': /* exit non-zero if any test exists non-zero */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 203 | track_exit_stats = 1; |
| 204 | break; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 205 | case 'f': /* filename to read test tags from */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 206 | filename = strdup(optarg); |
| 207 | break; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 208 | case 'h': /* help */ |
iyermanoj | 6631451 | 2003-01-27 22:56:23 +0000 | [diff] [blame] | 209 | fprintf(stdout, "Usage: pan -n name [ -SyAehp ] [ -s starts ]" |
| 210 | " [-t time[s|m|h|d] [ -x nactive ] [ -l logfile ]\n\t" |
| 211 | "[ -a active-file ] [ -f command-file ] " |
subrata_modak | bc833d3 | 2007-07-25 10:12:02 +0000 | [diff] [blame] | 212 | "[ -C fail-command-file ] " |
iyermanoj | 6631451 | 2003-01-27 22:56:23 +0000 | [diff] [blame] | 213 | "[ -d debug-level ]\n\t[-o output-file] " |
| 214 | "[-O output-buffer-directory] [cmd]\n"); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 215 | exit(0); |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 216 | case 'l': /* log file */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 217 | logfilename = strdup(optarg); |
| 218 | break; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 219 | case 'n': /* tag given to pan */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 220 | panname = strdup(optarg); |
| 221 | break; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 222 | case 'o': /* send test output here */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 223 | outputfilename = strdup(optarg); |
| 224 | break; |
iyermanoj | 6631451 | 2003-01-27 22:56:23 +0000 | [diff] [blame] | 225 | case 'p': /* formatted printing. */ |
| 226 | fmt_print = 1; |
| 227 | break; |
iyermanoj | 492e88e | 2003-01-30 06:22:31 +0000 | [diff] [blame] | 228 | case 'q': /* supress test start and test end messages */ |
| 229 | quiet_mode = 1; |
| 230 | break; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 231 | case 'r': /* reporting type: none, rts */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 232 | reporttype = strdup(optarg); |
| 233 | break; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 234 | case 's': /* number of tags to run */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 235 | starts = atoi(optarg); |
| 236 | break; |
robbiew | c170673 | 2002-02-19 18:06:31 +0000 | [diff] [blame] | 237 | case 't': /* run_time to run */ |
| 238 | ret = sscanf(optarg, "%d%c", &run_time, &modifier); |
| 239 | if (ret == 0) { printf("Need proper time input: ####x where" |
| 240 | "x is one of s,m,h,d\n"); break; } |
| 241 | else if (ret == 1) { printf("Only got a time value of %d " |
| 242 | "modifiers need to come immediately after #" |
| 243 | " assuming %c\n", run_time, modifier); } |
| 244 | else |
| 245 | { |
| 246 | switch (modifier) |
| 247 | { |
| 248 | case 's': run_time = run_time; break; |
| 249 | case 'm': run_time = run_time * 60; break; |
| 250 | case 'h': run_time = run_time * 60 * 60; break; |
| 251 | case 'd': run_time = run_time * 60 * 60 * 24; break; |
| 252 | default: |
| 253 | printf("Invalid time modifier, try: s|h|m|d\n"); exit(-1); |
| 254 | } |
| 255 | printf("PAN will run for %d seconds\n", run_time); |
| 256 | } |
| 257 | starts = 0; //-t implies run as many starts as possible |
| 258 | break; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 259 | case 'x': /* number of tags to keep running */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 260 | keep_active = atoi(optarg); |
| 261 | break; |
nstraz | fef21be | 2001-03-15 21:23:27 +0000 | [diff] [blame] | 262 | case 'y': /* restart on failure or signal */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 263 | fork_in_road = 1; |
| 264 | break; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | if (panname == NULL) { |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 269 | fprintf(stderr, "pan: Must supply -n\n"); |
| 270 | exit(1); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 271 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 272 | if (zooname == NULL) { |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 273 | zooname = zoo_getname(); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 274 | if (zooname == NULL) { |
| 275 | fprintf(stderr, |
| 276 | "pan(%s): Must supply -a or set ZOO env variable\n", |
| 277 | panname); |
| 278 | exit(1); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 281 | if (reporttype) { |
| 282 | /* make sure we understand the report type */ |
| 283 | if (strcasecmp(reporttype, "rts") |
| 284 | && strcasecmp(reporttype, "none") |
| 285 | /* && strcasecmp(reporttype, "xml")*/) |
| 286 | reporttype = "rts"; |
| 287 | } else { |
| 288 | /* set the default */ |
| 289 | reporttype = "rts"; |
| 290 | } |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 291 | |
| 292 | if (logfilename != NULL) { |
| 293 | time_t startup; |
| 294 | char *s; |
| 295 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 296 | if (!strcmp(logfilename, "-")) { |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 297 | logfile = stdout; |
| 298 | } else { |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 299 | if ((logfile = fopen(logfilename, "a+")) == NULL) { |
| 300 | fprintf(stderr, |
| 301 | "pan(%s): Error %s (%d) opening log file '%s'\n", |
| 302 | panname, strerror(errno), errno, logfilename); |
| 303 | exit(1); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 307 | time(&startup); |
| 308 | s = ctime(&startup); |
| 309 | *(s + strlen(s) - 1) = '\0'; |
iyermanoj | 6631451 | 2003-01-27 22:56:23 +0000 | [diff] [blame] | 310 | if (!fmt_print) |
| 311 | fprintf(logfile, "startup='%s'\n", s); |
| 312 | else |
| 313 | { |
iyermanoj | 3251858 | 2003-01-28 19:44:34 +0000 | [diff] [blame] | 314 | fprintf(logfile, "Test Start Time: %s\n", s); |
| 315 | fprintf(logfile, "-----------------------------------------\n"); |
iyermanoj | 8c484e9 | 2003-01-29 02:09:13 +0000 | [diff] [blame] | 316 | fprintf(logfile, "%-30.20s %-10.10s %-10.10s\n", |
| 317 | "Testcase", "Result", "Exit Value"); |
| 318 | fprintf(logfile, "%-30.20s %-10.10s %-10.10s\n", |
| 319 | "--------", "------", "------------"); |
iyermanoj | 6631451 | 2003-01-27 22:56:23 +0000 | [diff] [blame] | 320 | } |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 321 | } |
| 322 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 323 | coll = get_collection(filename, optind, argc, argv); |
subrata_modak | 1cad5c0 | 2008-05-19 13:37:14 +0000 | [diff] [blame] | 324 | if(!coll) |
| 325 | exit(1); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 326 | if (coll->cnt == 0) { |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 327 | fprintf(stderr, |
| 328 | "pan(%s): Must supply a file collection or a command\n", |
| 329 | panname); |
| 330 | exit(1); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | if (Debug & Dsetup) |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 334 | dump_coll(coll); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 335 | |
| 336 | /* a place to store the pgrps we're watching */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 337 | running = (struct tag_pgrp *)malloc((keep_active + 1) * sizeof(struct tag_pgrp)); |
| 338 | memset(running, 0, keep_active * sizeof(struct tag_pgrp)); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 339 | running[keep_active].pgrp = -1; /* end sentinel */ |
| 340 | |
| 341 | /* a head to the orphaned pgrp list */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 342 | orphans = (struct orphan_pgrp *) malloc(sizeof(struct orphan_pgrp)); |
| 343 | memset(orphans, 0, sizeof(struct orphan_pgrp)); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 344 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 345 | srand48(time(NULL) ^ (getpid() + (getpid() << 15))); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 346 | |
| 347 | /* Supply a default for starts. If we are in sequential mode, use |
| 348 | * the number of commands available; otherwise 1. |
| 349 | */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 350 | if (starts == -1) { |
| 351 | if (sequential) { |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 352 | starts = coll->cnt; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 353 | } else { |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 354 | starts = 1; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 355 | } |
| 356 | } else if (starts == 0) { /* if the user specified infinite, set it */ |
| 357 | starts = -1; |
| 358 | } else { /* else, make sure we are starting at least keep_active processes */ |
| 359 | if (starts < keep_active) |
| 360 | starts = keep_active; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 361 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 362 | |
| 363 | /* if we're buffering output, but we're only running on process at a time, |
| 364 | * then essentially "turn off buffering" |
| 365 | */ |
| 366 | if (test_out_dir && (keep_active == 1)) { |
| 367 | free(test_out_dir); |
| 368 | test_out_dir = NULL; |
| 369 | } |
| 370 | |
| 371 | if (test_out_dir) { |
| 372 | struct stat sbuf; |
| 373 | |
| 374 | if (stat(test_out_dir, &sbuf) < 0) { |
| 375 | fprintf(stderr, |
| 376 | "pan(%s): stat of -O arg '%s' failed. errno: %d %s\n", |
| 377 | panname, test_out_dir, errno, strerror(errno)); |
| 378 | exit(1); |
| 379 | } |
| 380 | if (!S_ISDIR(sbuf.st_mode)) { |
| 381 | fprintf(stderr, "pan(%s): -O arg '%s' must be a directory.\n", |
| 382 | panname, test_out_dir); |
| 383 | exit(1); |
| 384 | } |
| 385 | if (access(test_out_dir, W_OK | R_OK | X_OK) < 0) { |
| 386 | fprintf(stderr, |
| 387 | "pan(%s): permission denied on -O arg '%s'. errno: %d %s\n", |
| 388 | panname, test_out_dir, errno, strerror(errno)); |
| 389 | exit(1); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | if (outputfilename) { |
| 394 | if (!freopen(outputfilename, "a+", stdout)) { |
| 395 | fprintf(stderr, |
| 396 | "pan(%s): Error %s (%d) openning output file '%s'\n", |
| 397 | panname, strerror(errno), errno, outputfilename); |
| 398 | exit(1); |
| 399 | } |
| 400 | } |
| 401 | |
subrata_modak | bc833d3 | 2007-07-25 10:12:02 +0000 | [diff] [blame] | 402 | if (failcmdfilename) { |
| 403 | if (!(failcmdfile = fopen(failcmdfilename, "a+"))) { |
| 404 | fprintf(stderr, |
| 405 | "pan(%s): Error %s (%d) opening fail cmd file '%s'\n", |
| 406 | panname, strerror(errno), errno, failcmdfilename); |
| 407 | exit(1); |
| 408 | } |
| 409 | } |
| 410 | |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 411 | if ((zoofile = zoo_open(zooname)) == NULL) { |
| 412 | fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 413 | exit(1); |
| 414 | } |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 415 | if (zoo_mark_args(zoofile, getpid(), panname, argc, argv)) { |
| 416 | fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 417 | exit(1); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* Allocate N spaces for max-arg commands. |
| 421 | * this is an "active file cleanliness" thing |
| 422 | */ |
| 423 | { |
| 424 | char *av[2], bigarg[82]; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 425 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 426 | memset(bigarg, '.', 81); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 427 | bigarg[81] = '\0'; |
| 428 | av[0] = bigarg; |
| 429 | av[1] = NULL; |
| 430 | |
| 431 | for (c = 0; c < keep_active; c++) { |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 432 | if (zoo_mark_cmdline(zoofile, c, panname, "")) { |
| 433 | fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 434 | exit(1); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | for (c = 0; c < keep_active; c++) { |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 438 | if (zoo_clear(zoofile, c)) { |
| 439 | fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 440 | exit(1); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | rec_signal = send_signal = 0; |
robbiew | cba8970 | 2002-09-05 20:48:35 +0000 | [diff] [blame] | 446 | if (run_time != -1) { alarm(run_time); } |
nstraz | b755b28 | 2003-03-03 23:35:55 +0000 | [diff] [blame] | 447 | sa.sa_handler = wait_handler; |
| 448 | sigaction(SIGALRM, &sa, NULL); |
| 449 | sigaction(SIGINT, &sa, NULL); |
| 450 | sigaction(SIGTERM, &sa, NULL); |
| 451 | sigaction(SIGHUP, &sa, NULL); |
| 452 | sigaction(SIGUSR1, &sa, NULL); /* ignore fork_in_road */ |
| 453 | sigaction(SIGUSR2, &sa, NULL); /* stop the scheduler */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 454 | |
| 455 | c = 0; /* in this loop, c is the command index */ |
| 456 | stop = 0; |
| 457 | exit_stat = 0; |
| 458 | go_idle = 0; |
| 459 | while (1) { |
| 460 | |
| 461 | while ((num_active < keep_active) && (starts != 0)) { |
| 462 | if (stop || rec_signal || go_idle) |
| 463 | break; |
| 464 | |
| 465 | if (!sequential) |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 466 | c = lrand48() % coll->cnt; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 467 | |
| 468 | /* find a slot for the child */ |
| 469 | for (i = 0; i < keep_active; ++i) { |
| 470 | if (running[i].pgrp == 0) |
| 471 | break; |
| 472 | } |
| 473 | if (i == keep_active) { |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 474 | fprintf(stderr, "pan(%s): Aborting: i == keep_active = %d\n", |
| 475 | panname, i); |
| 476 | wait_handler(SIGINT); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 477 | exit_stat++; |
| 478 | break; |
| 479 | } |
| 480 | |
iyermanoj | 492e88e | 2003-01-30 06:22:31 +0000 | [diff] [blame] | 481 | cpid = run_child(coll->ary[c], running + i, quiet_mode); |
mreed10 | 3bed8b0 | 2006-07-10 15:46:10 +0000 | [diff] [blame] | 482 | if (cpid != -1) |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 483 | ++num_active; |
mreed10 | 3bed8b0 | 2006-07-10 15:46:10 +0000 | [diff] [blame] | 484 | if ((cpid != -1 || sequential) && starts > 0) |
| 485 | --starts; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 486 | |
| 487 | if (sequential) |
| 488 | if (++c >= coll->cnt) |
| 489 | c = 0; |
| 490 | |
robbiew | c170673 | 2002-02-19 18:06:31 +0000 | [diff] [blame] | 491 | } /* while( (num_active < keep_active) && (starts != 0) ) */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 492 | |
| 493 | if (starts == 0) |
iyermanoj | 8a3f929 | 2003-01-30 06:28:39 +0000 | [diff] [blame] | 494 | { |
| 495 | if (!quiet_mode) |
| 496 | printf("incrementing stop\n"); |
| 497 | ++stop; |
| 498 | } |
| 499 | else if (starts == -1) //wjh |
| 500 | { |
| 501 | FILE *f = (FILE*)-1; |
| 502 | if ((f = fopen(PAN_STOP_FILE, "r")) != 0) |
| 503 | { printf("Got %s Stopping!\n", PAN_STOP_FILE); |
| 504 | fclose(f); unlink(PAN_STOP_FILE); stop++; |
| 505 | } |
| 506 | } |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 507 | |
| 508 | if (rec_signal) { |
| 509 | /* propagate everything except sigusr2 */ |
| 510 | |
| 511 | if (rec_signal == SIGUSR2) { |
| 512 | if (fork_in_road) |
| 513 | ++go_idle; |
| 514 | else |
| 515 | ++stop; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 516 | rec_signal = send_signal = 0; |
| 517 | } else { |
| 518 | if (rec_signal == SIGUSR1) |
| 519 | fork_in_road = 0; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 520 | propagate_signal(running, keep_active, orphans); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 521 | if (fork_in_road) |
| 522 | ++go_idle; |
| 523 | else |
| 524 | ++stop; |
| 525 | } |
| 526 | } |
| 527 | |
subrata_modak | bc833d3 | 2007-07-25 10:12:02 +0000 | [diff] [blame] | 528 | err = check_pids(running, &num_active, keep_active, logfile, |
| 529 | failcmdfile, orphans, fmt_print, &failcnt, quiet_mode); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 530 | if (Debug & Drunning) { |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 531 | pids_running(running, keep_active); |
| 532 | orphans_running(orphans); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 533 | } |
| 534 | if (err) { |
| 535 | if (fork_in_road) |
| 536 | ++go_idle; |
| 537 | if (track_exit_stats) |
| 538 | exit_stat++; |
| 539 | if (has_brakes) { |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 540 | fprintf(stderr, "pan(%s): All stop!%s\n", panname, |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 541 | go_idle ? " (idling)" : ""); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 542 | wait_handler(SIGINT); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
| 546 | if (stop && (num_active == 0)) |
| 547 | break; |
| 548 | |
| 549 | if (go_idle && (num_active == 0)) { |
| 550 | go_idle = 0; /* It is idle, now resume scheduling. */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 551 | wait_handler(0); /* Reset the signal ratchet. */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
| 555 | /* Wait for orphaned pgrps */ |
| 556 | while (1) { |
| 557 | for (orph = orphans; orph != NULL; orph = orph->next) { |
| 558 | if (orph->pgrp == 0) |
| 559 | continue; |
| 560 | /* Yes, we have orphaned pgrps */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 561 | sleep(5); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 562 | if (!rec_signal) { |
| 563 | /* force an artificial signal, move us |
| 564 | * through the signal ratchet. |
| 565 | */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 566 | wait_handler(SIGINT); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 567 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 568 | propagate_signal(running, keep_active, orphans); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 569 | if (Debug & Drunning) |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 570 | orphans_running(orphans); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 571 | break; |
| 572 | } |
| 573 | if (orph == NULL) |
| 574 | break; |
| 575 | } |
| 576 | |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 577 | if (zoo_clear(zoofile, getpid())) { |
| 578 | fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 579 | ++exit_stat; |
| 580 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 581 | fclose(zoofile); |
subrata_modak | 1cad5c0 | 2008-05-19 13:37:14 +0000 | [diff] [blame] | 582 | if (logfile && fmt_print) |
iyermanoj | 45e9d98 | 2003-01-28 22:33:34 +0000 | [diff] [blame] | 583 | { |
| 584 | if (uname(&unamebuf) == -1) |
| 585 | fprintf(stderr, "ERROR: uname(): %s\n", strerror(errno)); |
iyermanoj | 492e88e | 2003-01-30 06:22:31 +0000 | [diff] [blame] | 586 | fprintf(logfile, "\n-----------------------------------------------\n"); |
iyermanoj | 45e9d98 | 2003-01-28 22:33:34 +0000 | [diff] [blame] | 587 | fprintf(logfile, "Total Tests: %d\n", coll->cnt); |
| 588 | fprintf(logfile, "Total Failures: %d\n", failcnt); |
| 589 | fprintf(logfile, "Kernel Version: %s\n", unamebuf.release); |
| 590 | fprintf(logfile, "Machine Architecture: %s\n", unamebuf.machine); |
| 591 | fprintf(logfile, "Hostname: %s\n\n", unamebuf.nodename); |
| 592 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 593 | if (logfile && (logfile != stdout)) |
| 594 | fclose(logfile); |
| 595 | |
| 596 | exit(exit_stat); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 600 | |
| 601 | static void |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 602 | propagate_signal(struct tag_pgrp *running, int keep_active, |
| 603 | struct orphan_pgrp *orphans) |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 604 | { |
| 605 | int i; |
| 606 | |
| 607 | if (Debug & Dshutdown) |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 608 | fprintf(stderr, "pan was signaled with sig %d...\n", rec_signal); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 609 | |
robbiew | c170673 | 2002-02-19 18:06:31 +0000 | [diff] [blame] | 610 | if (rec_signal == SIGALRM) |
| 611 | { |
| 612 | printf("PAN stop Alarm was received\n"); |
| 613 | rec_signal = SIGTERM; |
| 614 | } |
| 615 | |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 616 | for (i = 0; i < keep_active; ++i) { |
| 617 | if (running[i].pgrp == 0) |
| 618 | continue; |
| 619 | |
| 620 | if (Debug & Dshutdown) |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 621 | fprintf(stderr, " propagating sig %d to %d\n", |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 622 | send_signal, -running[i].pgrp); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 623 | if (kill(-running[i].pgrp, send_signal) != 0) { |
| 624 | fprintf(stderr, |
| 625 | "pan(%s): kill(%d,%d) failed on tag (%s). errno:%d %s\n", |
| 626 | panname, -running[i].pgrp, send_signal, |
| 627 | running[i].cmd->name, errno, strerror(errno)); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 628 | } |
| 629 | running[i].stopping = 1; |
| 630 | } |
| 631 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 632 | check_orphans(orphans, send_signal); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 633 | |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 634 | rec_signal = send_signal = 0; |
| 635 | } |
| 636 | |
| 637 | |
| 638 | static int |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 639 | check_pids(struct tag_pgrp *running, int *num_active, int keep_active, |
subrata_modak | bc833d3 | 2007-07-25 10:12:02 +0000 | [diff] [blame] | 640 | FILE * logfile, FILE * failcmdfile, struct orphan_pgrp *orphans, |
| 641 | int fmt_print, int *failcnt, int quiet_mode) |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 642 | { |
| 643 | int w; |
| 644 | pid_t cpid; |
| 645 | int stat_loc; |
| 646 | int ret = 0; |
| 647 | int i; |
| 648 | time_t t; |
| 649 | char *status; |
| 650 | int signaled = 0; |
| 651 | struct tms tms1, tms2; |
| 652 | clock_t tck; |
| 653 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 654 | check_orphans(orphans, 0); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 655 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 656 | tck = times(&tms1); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 657 | if (tck == -1) { |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 658 | fprintf(stderr, "pan(%s): times(&tms1) failed. errno:%d %s\n", |
| 659 | panname, errno, strerror(errno)); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 660 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 661 | cpid = wait(&stat_loc); |
| 662 | tck = times(&tms2); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 663 | if (tck == -1) { |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 664 | fprintf(stderr, "pan(%s): times(&tms2) failed. errno:%d %s\n", |
| 665 | panname, errno, strerror(errno)); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | if (cpid < 0) { |
| 669 | if (errno == EINTR) { |
| 670 | if (Debug) |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 671 | fprintf(stderr, "pan(%s): wait() interrupted\n", panname); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 672 | } else if (errno != ECHILD) { |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 673 | fprintf(stderr, "pan(%s): wait() failed. errno:%d %s\n", |
| 674 | panname, errno, strerror(errno)); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 675 | } |
| 676 | } else if (cpid > 0) { |
| 677 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 678 | if (WIFSIGNALED(stat_loc)) { |
| 679 | w = WTERMSIG(stat_loc); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 680 | status = "signaled"; |
| 681 | if (Debug & Dexit) |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 682 | fprintf(stderr, "child %d terminated with signal %d\n", cpid, |
| 683 | w); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 684 | --*num_active; |
| 685 | signaled = 1; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 686 | } else if (WIFEXITED(stat_loc)) { |
| 687 | w = WEXITSTATUS(stat_loc); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 688 | status = "exited"; |
| 689 | if (Debug & Dexit) |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 690 | fprintf(stderr, "child %d exited with status %d\n", cpid, w); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 691 | --*num_active; |
| 692 | if (w != 0) |
| 693 | ret++; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 694 | } else if (WIFSTOPPED(stat_loc)) { /* should never happen */ |
| 695 | w = WSTOPSIG(stat_loc); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 696 | status = "stopped"; |
| 697 | ret++; |
| 698 | } else { /* should never happen */ |
| 699 | w = 0; |
| 700 | status = "unknown"; |
| 701 | ret++; |
| 702 | } |
| 703 | |
| 704 | for (i = 0; i < keep_active; ++i) { |
| 705 | if (running[i].pgrp == cpid) { |
| 706 | if ((w == 130) && running[i].stopping && |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 707 | (strcmp(status, "exited") == 0)) { |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 708 | /* The child received sigint, but |
| 709 | * did not trap for it? Compensate |
| 710 | * for it here. |
| 711 | */ |
| 712 | w = 0; |
| 713 | ret--; /* undo */ |
| 714 | if (Debug & Drunning) |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 715 | fprintf(stderr, |
| 716 | "pan(%s): tag=%s exited 130, known to be signaled; will give it an exit 0.\n", |
| 717 | panname, running[i].cmd->name); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 718 | } |
plars | debdbd1 | 2001-11-30 19:30:29 +0000 | [diff] [blame] | 719 | time(&t); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 720 | if (logfile != NULL) { |
iyermanoj | 6631451 | 2003-01-27 22:56:23 +0000 | [diff] [blame] | 721 | if (!fmt_print) |
| 722 | fprintf(logfile, |
| 723 | "tag=%s stime=%d dur=%d exit=%s stat=%d core=%s cu=%d cs=%d\n", |
vapier | 9e78ade | 2006-12-13 22:55:21 +0000 | [diff] [blame] | 724 | running[i].cmd->name, (int) (running[i].mystime), |
| 725 | (int) (t - running[i].mystime), status, w, |
iyermanoj | 6631451 | 2003-01-27 22:56:23 +0000 | [diff] [blame] | 726 | (stat_loc & 0200) ? "yes" : "no", |
| 727 | (int) (tms2.tms_cutime - tms1.tms_cutime), |
| 728 | (int) (tms2.tms_cstime - tms1.tms_cstime)); |
| 729 | else |
| 730 | { |
iyermanoj | 3251858 | 2003-01-28 19:44:34 +0000 | [diff] [blame] | 731 | if (w != 0) |
| 732 | ++*failcnt; |
robbiew | e126a0d | 2004-07-15 19:13:51 +0000 | [diff] [blame] | 733 | fprintf(logfile, "%-30.30s %-10.10s %-5d\n", |
iyermanoj | 8c484e9 | 2003-01-29 02:09:13 +0000 | [diff] [blame] | 734 | running[i].cmd->name, ((w != 0) ? "FAIL" : "PASS"), |
| 735 | w); |
iyermanoj | 6631451 | 2003-01-27 22:56:23 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | fflush(logfile); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 739 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 740 | |
subrata_modak | bc833d3 | 2007-07-25 10:12:02 +0000 | [diff] [blame] | 741 | if ((failcmdfile != NULL) && (w !=0)) { |
| 742 | fprintf(failcmdfile, "%s %s\n", running[i].cmd->name, running[i].cmd->cmdline); |
| 743 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 744 | |
| 745 | if (running[i].stopping) |
| 746 | status = "driver_interrupt"; |
| 747 | |
| 748 | if (test_out_dir) { |
iyermanoj | 492e88e | 2003-01-30 06:22:31 +0000 | [diff] [blame] | 749 | if (!quiet_mode) |
| 750 | write_test_start(running+i, "ok"); |
| 751 | copy_buffered_output(running + i); |
| 752 | unlink(running[i].output); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 753 | } |
iyermanoj | 492e88e | 2003-01-30 06:22:31 +0000 | [diff] [blame] | 754 | if (!quiet_mode) |
| 755 | write_test_end(running+i, t, status, |
| 756 | stat_loc, w, &tms1, &tms2); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 757 | |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 758 | /* If signaled and we weren't expecting |
| 759 | * this to be stopped then the proc |
| 760 | * had a problem. |
| 761 | */ |
| 762 | if (signaled && !running[i].stopping) |
| 763 | ret++; |
| 764 | |
| 765 | running[i].pgrp = 0; |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 766 | if (zoo_clear(zoofile, cpid)) { |
| 767 | fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 768 | exit(1); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | /* Check for orphaned pgrps */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 772 | if ((kill(-cpid, 0) == 0) || (errno == EPERM)) { |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 773 | if (zoo_mark_cmdline(zoofile, cpid, "panorphan", |
| 774 | running[i].cmd->cmdline)) { |
| 775 | fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 776 | exit(1); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 777 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 778 | mark_orphan(orphans, cpid); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 779 | /* status of kill doesn't matter */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 780 | kill(-cpid, SIGTERM); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | break; |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | return ret; |
| 788 | } |
| 789 | |
| 790 | |
| 791 | static pid_t |
iyermanoj | 492e88e | 2003-01-30 06:22:31 +0000 | [diff] [blame] | 792 | run_child(struct coll_entry *colle, struct tag_pgrp *active, int quiet_mode) |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 793 | { |
| 794 | int cpid; |
robbiew | f3a83d5 | 2002-05-28 16:26:16 +0000 | [diff] [blame] | 795 | int c_stdout = -1; /* child's stdout, stderr */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 796 | int capturing = 0; /* output is going to a file instead of stdout */ |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 797 | char *c_cmdline; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 798 | static long cmdno = 0; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 799 | int errpipe[2]; /* way to communicate to parent that the tag */ |
| 800 | char errbuf[1024]; /* didn't actually start */ |
| 801 | int errlen; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 802 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 803 | /* Try to open the file that will be stdout for the test */ |
| 804 | if (test_out_dir) { |
| 805 | capturing = 1; |
| 806 | do { |
| 807 | sprintf(active->output, "%s/%s.%ld", |
| 808 | test_out_dir, colle->name, cmdno++); |
nstraz | 566e8c8 | 2000-09-22 19:52:17 +0000 | [diff] [blame] | 809 | c_stdout = open(active->output, O_CREAT | O_RDWR | O_EXCL | O_SYNC, 0666); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 810 | } while (c_stdout < 0 && errno == EEXIST); |
| 811 | if (c_stdout < 0) { |
| 812 | fprintf(stderr, |
| 813 | "pan(%s): open of stdout file failed (tag %s). errno: %d %s\n file: %s\n", |
| 814 | panname, colle->name, errno, strerror(errno), |
| 815 | active->output); |
| 816 | return -1; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | /* get the tag's command line arguments ready. subst_pcnt_f() uses a |
| 821 | * static counter, that's why we do it here instead of after we fork. |
| 822 | */ |
| 823 | if (colle->pcnt_f) { |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 824 | c_cmdline = subst_pcnt_f(colle); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 825 | } else { |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 826 | c_cmdline = colle->cmdline; |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | if (pipe(errpipe) < 0) { |
| 830 | fprintf(stderr, "pan(%s): pipe() failed. errno:%d %s\n", |
| 831 | panname, errno, strerror(errno)); |
nstraz | 6c3f66b | 2001-09-28 21:56:46 +0000 | [diff] [blame] | 832 | if (capturing) { |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 833 | close(c_stdout); |
nstraz | 6c3f66b | 2001-09-28 21:56:46 +0000 | [diff] [blame] | 834 | unlink(active->output); |
| 835 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 836 | return -1; |
| 837 | } |
| 838 | |
| 839 | if ((cpid = fork()) < 0) { |
| 840 | fprintf(stderr, "pan(%s): fork failed (tag %s). errno:%d %s\n", |
| 841 | panname, colle->name, errno, strerror(errno)); |
nstraz | 6c3f66b | 2001-09-28 21:56:46 +0000 | [diff] [blame] | 842 | if (capturing) { |
| 843 | unlink(active->output); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 844 | close(c_stdout); |
nstraz | 6c3f66b | 2001-09-28 21:56:46 +0000 | [diff] [blame] | 845 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 846 | close(errpipe[0]); |
| 847 | close(errpipe[1]); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 848 | return -1; |
| 849 | } else if (cpid == 0) { |
| 850 | /* child */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 851 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 852 | fclose(zoofile); |
| 853 | close(errpipe[0]); |
| 854 | fcntl(errpipe[1], F_SETFD, 1); /* close the pipe if we succeed */ |
| 855 | setpgrp(); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 856 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 857 | umask(0); |
| 858 | |
| 859 | /* if we're putting output into a buffer file, we need to do the |
| 860 | * redirection now. If we fail |
| 861 | */ |
| 862 | if (capturing) { |
| 863 | if (dup2(c_stdout, fileno(stdout)) == -1) { |
| 864 | errlen = sprintf(errbuf, "pan(%s): couldn't redirect stdout for tag %s. errno:%d %s", |
| 865 | panname, colle->name, errno, strerror(errno)); |
| 866 | write(errpipe[1], &errlen, sizeof(errlen)); |
| 867 | write(errpipe[1], errbuf, errlen); |
| 868 | exit(2); |
| 869 | } |
| 870 | if (dup2(c_stdout, fileno(stderr)) == -1) { |
| 871 | errlen = sprintf(errbuf, "pan(%s): couldn't redirect stderr for tag %s. errno:%d %s", |
| 872 | panname, colle->name, errno, strerror(errno)); |
| 873 | write(errpipe[1], &errlen, sizeof(errlen)); |
| 874 | write(errpipe[1], errbuf, errlen); |
| 875 | exit(2); |
| 876 | } |
| 877 | } else { /* stderr still needs to be redirected */ |
| 878 | if (dup2(fileno(stdout), fileno(stderr)) == -1) { |
| 879 | errlen = sprintf(errbuf, "pan(%s): couldn't redirect stderr for tag %s. errno:%d %s", |
| 880 | panname, colle->name, errno, strerror(errno)); |
| 881 | write(errpipe[1], &errlen, sizeof(errlen)); |
| 882 | write(errpipe[1], errbuf, errlen); |
| 883 | exit(2); |
| 884 | } |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 885 | } |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 886 | /* If there are any shell-type characters in the cmdline |
| 887 | * such as '>', '<', '$', '|', etc, then we exec a shell and |
| 888 | * run the cmd under a shell. |
| 889 | * |
| 890 | * Otherwise, break the cmdline at white space and exec the |
| 891 | * cmd directly. |
| 892 | */ |
| 893 | if (strpbrk(c_cmdline, "\"';|<>$\\")) { |
| 894 | execlp("sh", "sh", "-c", c_cmdline, 0); |
| 895 | errlen = sprintf(errbuf, |
| 896 | "pan(%s): execlp of '%s' (tag %s) failed. errno:%d %s", |
| 897 | panname, c_cmdline, colle->name, errno, strerror(errno)); |
| 898 | } else { |
| 899 | char **arg_v; |
| 900 | |
| 901 | arg_v = (char **)splitstr(c_cmdline, NULL, NULL); |
| 902 | |
| 903 | execvp(arg_v[0], arg_v); |
| 904 | errlen = sprintf(errbuf, |
| 905 | "pan(%s): execvp of '%s' (tag %s) failed. errno:%d %s", |
| 906 | panname, arg_v[0], colle->name, errno, strerror(errno)); |
| 907 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 908 | write(errpipe[1], &errlen, sizeof(errlen)); |
| 909 | write(errpipe[1], errbuf, errlen); |
| 910 | exit(errno); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | /* parent */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 914 | |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 915 | /* subst_pcnt_f() allocates the command line dynamically |
| 916 | * free the malloc to prevent a memory leak |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 917 | */ |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 918 | if (colle->pcnt_f) free(c_cmdline); |
| 919 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 920 | close(errpipe[1]); |
vapier | 9e78ade | 2006-12-13 22:55:21 +0000 | [diff] [blame] | 921 | time(&active->mystime); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 922 | active->cmd = colle; |
| 923 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 924 | /* if the child couldn't go through with the exec, |
| 925 | * clean up the mess, note it, and move on |
| 926 | */ |
| 927 | if(read(errpipe[0], &errlen, sizeof(errlen))) { |
| 928 | int status; |
| 929 | time_t end_time; |
| 930 | int termid; |
| 931 | char *termtype; |
| 932 | struct tms notime = {0, 0, 0, 0}; |
| 933 | |
| 934 | read(errpipe[0], errbuf, errlen); |
| 935 | close(errpipe[0]); |
| 936 | errbuf[errlen] = '\0'; |
| 937 | /* fprintf(stderr, "%s", errbuf); */ |
| 938 | waitpid(cpid, &status, 0); |
| 939 | if (WIFSIGNALED(status)) { |
| 940 | termid = WTERMSIG(status); |
| 941 | termtype = "signaled"; |
| 942 | } else if (WIFEXITED(status)) { |
| 943 | termid = WEXITSTATUS(status); |
| 944 | termtype = "exited"; |
| 945 | } else if (WIFSTOPPED(status)) { |
| 946 | termid = WSTOPSIG(status); |
| 947 | termtype = "stopped"; |
| 948 | } else { |
| 949 | termid = 0; |
| 950 | termtype = "unknown"; |
| 951 | } |
| 952 | time(&end_time); |
iyermanoj | 492e88e | 2003-01-30 06:22:31 +0000 | [diff] [blame] | 953 | if (!quiet_mode) |
| 954 | { |
| 955 | write_test_start(active, errbuf); |
| 956 | write_test_end(active, end_time, termtype, status, |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 957 | termid, ¬ime, ¬ime); |
iyermanoj | 492e88e | 2003-01-30 06:22:31 +0000 | [diff] [blame] | 958 | } |
nstraz | 6c3f66b | 2001-09-28 21:56:46 +0000 | [diff] [blame] | 959 | if (capturing) { |
| 960 | close(c_stdout); |
| 961 | unlink(active->output); |
| 962 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 963 | return -1; |
| 964 | } |
| 965 | |
| 966 | close(errpipe[0]); |
nstraz | 6c3f66b | 2001-09-28 21:56:46 +0000 | [diff] [blame] | 967 | if (capturing) close(c_stdout); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 968 | |
| 969 | if (!test_out_dir) |
iyermanoj | 492e88e | 2003-01-30 06:22:31 +0000 | [diff] [blame] | 970 | if (!quiet_mode) |
| 971 | write_test_start(active, "ok"); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 972 | |
| 973 | active->pgrp = cpid; |
| 974 | active->stopping = 0; |
| 975 | |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 976 | if (zoo_mark_cmdline(zoofile, cpid, colle->name, colle->cmdline)) { |
| 977 | fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 978 | exit(1); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | if (Debug & Dstartup) |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 982 | fprintf(stderr, "started %s cpid=%d at %s", |
vapier | 9e78ade | 2006-12-13 22:55:21 +0000 | [diff] [blame] | 983 | colle->name, cpid, ctime(&active->mystime)); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 984 | |
| 985 | if (Debug & Dstart) { |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 986 | fprintf(stderr, "Executing test = %s as %s", colle->name, colle->cmdline); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 987 | if (capturing) |
| 988 | fprintf(stderr, "with output file = %s\n", active->output); |
| 989 | else |
| 990 | fprintf(stderr, "\n"); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | return cpid; |
| 994 | } |
| 995 | |
| 996 | |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 997 | static char * |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 998 | subst_pcnt_f(struct coll_entry *colle) |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 999 | { |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 1000 | static int counter = 1; |
| 1001 | char pid_and_counter[20]; |
| 1002 | char new_cmdline[1024]; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1003 | |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 1004 | /* if we get called falsely, do the right thing anyway */ |
| 1005 | if (!colle->pcnt_f) |
| 1006 | return colle->cmdline; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1007 | |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 1008 | snprintf(pid_and_counter, 20, "%d_%d", getpid(), counter++); |
| 1009 | snprintf(new_cmdline, 1024, colle->cmdline, pid_and_counter); |
| 1010 | return strdup(new_cmdline); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | static struct collection * |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1014 | get_collection(char *file, int optind, int argc, char **argv) |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1015 | { |
| 1016 | char *buf, *a, *b; |
| 1017 | struct coll_entry *head, *p, *n; |
| 1018 | struct collection *coll; |
| 1019 | int i; |
| 1020 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1021 | buf = slurp(file); |
subrata_modak | 1cad5c0 | 2008-05-19 13:37:14 +0000 | [diff] [blame] | 1022 | if(!buf) |
| 1023 | return NULL; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1024 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1025 | coll = (struct collection *) malloc(sizeof(struct collection)); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1026 | coll->cnt = 0; |
| 1027 | |
| 1028 | head = p = n = NULL; |
| 1029 | a = b = buf; |
| 1030 | while (*b != '\0') { |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 1031 | /* set b to the start of the next line and add a NULL character |
| 1032 | * to separate the two lines */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1033 | if ((b = strchr(a, '\n')) != NULL) |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1034 | *b = '\0'; |
| 1035 | |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 1036 | /* If this is line isn't a comment */ |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1037 | if ((*a != '#') && (*a != '\0') && (*a != ' ')) { |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 1038 | n = (struct coll_entry *) malloc(sizeof(struct coll_entry)); |
| 1039 | if ((n->pcnt_f = strstr(a, "%f"))) { |
| 1040 | n->pcnt_f[1] = 's'; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1041 | } |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 1042 | n->name = strdup(strsep(&a, " \t")); |
| 1043 | n->cmdline = strdup(a); |
| 1044 | n->next = NULL; |
| 1045 | |
| 1046 | if (p) { |
| 1047 | p->next = n; |
| 1048 | } |
| 1049 | if (head == NULL) { |
| 1050 | head = n; |
| 1051 | } |
| 1052 | p = n; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1053 | coll->cnt++; |
| 1054 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1055 | a += strlen(a) + 1; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1056 | b = a; |
| 1057 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1058 | free(buf); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1059 | |
| 1060 | /* is there something on the commandline to be counted? */ |
| 1061 | if (optind < argc) { |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 1062 | char workstr[1024] = ""; |
| 1063 | int workstr_left = 1023; |
| 1064 | |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1065 | /* fill arg list */ |
| 1066 | for (i = 0; optind < argc; ++optind, ++i) { |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 1067 | strncat(workstr, argv[optind], workstr_left); |
| 1068 | workstr_left = workstr_left - strlen(argv[optind]); |
| 1069 | strncat(workstr, " ", workstr_left); |
| 1070 | workstr_left--; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1071 | } |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1072 | |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 1073 | n = (struct coll_entry *) malloc(sizeof(struct coll_entry)); |
| 1074 | if ((n->pcnt_f = strstr(workstr, "%f"))) { |
| 1075 | n->pcnt_f[1] = 's'; |
| 1076 | } |
| 1077 | n->cmdline = strdup(workstr); |
| 1078 | n->name = "cmdln"; |
| 1079 | n->next = NULL; |
| 1080 | if (p) { |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1081 | p->next = n; |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 1082 | } |
| 1083 | if (head == NULL) { |
| 1084 | head = n; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1085 | } |
| 1086 | coll->cnt++; |
| 1087 | } |
| 1088 | |
| 1089 | /* get an array */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1090 | coll->ary = (struct coll_entry **) malloc(coll->cnt * |
| 1091 | sizeof(struct coll_entry *)); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1092 | |
| 1093 | /* fill the array */ |
| 1094 | i = 0; |
| 1095 | n = head; |
| 1096 | while (n != NULL) { |
| 1097 | coll->ary[i] = n; |
| 1098 | n = n->next; |
| 1099 | ++i; |
| 1100 | } |
| 1101 | if (i != coll->cnt) |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1102 | fprintf(stderr, "pan(%s): i doesn't match cnt\n", panname); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1103 | |
| 1104 | return coll; |
| 1105 | } |
| 1106 | |
| 1107 | |
| 1108 | static char * |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1109 | slurp(char *file) |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1110 | { |
| 1111 | char *buf; |
| 1112 | int fd; |
| 1113 | struct stat sbuf; |
| 1114 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1115 | if ((fd = open(file, O_RDONLY)) < 0) { |
| 1116 | fprintf(stderr, "pan(%s): open(%s,O_RDONLY) failed. errno:%d %s\n", |
| 1117 | panname, file, errno, strerror(errno)); |
| 1118 | return NULL; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1121 | if (fstat(fd, &sbuf) < 0) { |
| 1122 | fprintf(stderr, "pan(%s): fstat(%s) failed. errno:%d %s\n", |
| 1123 | panname, file, errno, strerror(errno)); |
| 1124 | return NULL; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1127 | buf = (char *) malloc(sbuf.st_size + 1); |
| 1128 | if (read(fd, buf, sbuf.st_size) != sbuf.st_size) { |
| 1129 | fprintf(stderr, "pan(%s): slurp failed. errno:%d %s\n", |
| 1130 | panname, errno, strerror(errno)); |
| 1131 | return NULL; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1132 | } |
| 1133 | buf[sbuf.st_size] = '\0'; |
| 1134 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1135 | close(fd); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1136 | return buf; |
| 1137 | } |
| 1138 | |
| 1139 | static void |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1140 | check_orphans(struct orphan_pgrp *orphans, int sig) |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1141 | { |
| 1142 | struct orphan_pgrp *orph; |
| 1143 | |
| 1144 | for (orph = orphans; orph != NULL; orph = orph->next) { |
| 1145 | if (orph->pgrp == 0) |
| 1146 | continue; |
| 1147 | |
| 1148 | if (Debug & Dshutdown) |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1149 | fprintf(stderr, " propagating sig %d to orphaned pgrp %d\n", |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1150 | sig, -(orph->pgrp)); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1151 | if (kill(-(orph->pgrp), sig) != 0) { |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1152 | if (errno == ESRCH) { |
| 1153 | /* This pgrp is now empty */ |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 1154 | if (zoo_clear(zoofile, orph->pgrp)) { |
| 1155 | fprintf(stderr, "pan(%s): %s\n", panname, zoo_error); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1156 | } |
| 1157 | orph->pgrp = 0; |
| 1158 | } else { |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1159 | fprintf(stderr, |
| 1160 | "pan(%s): kill(%d,%d) on orphaned pgrp failed. errno:%d %s\n", |
| 1161 | panname, -(orph->pgrp), sig, errno, strerror(errno)); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | |
| 1168 | static void |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1169 | mark_orphan(struct orphan_pgrp *orphans, pid_t cpid) |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1170 | { |
| 1171 | struct orphan_pgrp *orph; |
| 1172 | |
| 1173 | for (orph = orphans; orph != NULL; orph = orph->next) { |
| 1174 | if (orph->pgrp == 0) |
| 1175 | break; |
| 1176 | } |
| 1177 | if (orph == NULL) { |
| 1178 | /* make a new struct */ |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1179 | orph = (struct orphan_pgrp *) malloc(sizeof(struct orphan_pgrp)); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1180 | |
| 1181 | /* plug in the new struct just after the head */ |
| 1182 | orph->next = orphans->next; |
| 1183 | orphans->next = orph; |
| 1184 | } |
| 1185 | orph->pgrp = cpid; |
| 1186 | } |
| 1187 | |
| 1188 | |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1189 | |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1190 | static void |
| 1191 | copy_buffered_output(struct tag_pgrp *running) |
| 1192 | { |
| 1193 | char *tag_output; |
| 1194 | |
| 1195 | tag_output = slurp(running->output); |
| 1196 | if (tag_output) { |
| 1197 | printf("%s", tag_output); |
| 1198 | /* make sure the output ends with a newline */ |
| 1199 | if (tag_output[strlen(tag_output) - 1] != '\n') |
| 1200 | printf("\n"); |
| 1201 | fflush(stdout); |
| 1202 | free(tag_output); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1203 | } |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | |
| 1207 | static void |
| 1208 | write_test_start(struct tag_pgrp *running, const char *init_status) |
| 1209 | { |
| 1210 | if (!strcmp(reporttype, "rts")) { |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1211 | |
| 1212 | printf("%s\ntag=%s stime=%ld\ncmdline=\"%s\"\ncontacts=\"%s\"\nanalysis=%s\ninitiation_status=\"%s\"\n%s\n", |
| 1213 | "<<<test_start>>>", |
vapier | 9e78ade | 2006-12-13 22:55:21 +0000 | [diff] [blame] | 1214 | running->cmd->name, running->mystime, running->cmd->cmdline, "", |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1215 | "exit", init_status, |
| 1216 | "<<<test_output>>>"); |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1217 | } |
| 1218 | fflush(stdout); |
| 1219 | } |
| 1220 | |
| 1221 | |
| 1222 | static void |
| 1223 | write_test_end(struct tag_pgrp *running, time_t exit_time, |
| 1224 | char *term_type, int stat_loc, int term_id, |
| 1225 | struct tms *tms1, struct tms *tms2) |
| 1226 | { |
| 1227 | if (!strcmp(reporttype, "rts")) { |
| 1228 | printf("%s\nduration=%ld termination_type=%s termination_id=%d corefile=%s\ncutime=%d cstime=%d\n%s\n", |
| 1229 | "<<<execution_status>>>", |
vapier | 9e78ade | 2006-12-13 22:55:21 +0000 | [diff] [blame] | 1230 | (long) (exit_time - running->mystime), |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1231 | term_type, term_id, (stat_loc & 0200) ? "yes" : "no", |
| 1232 | (int) (tms2->tms_cutime - tms1->tms_cutime), |
| 1233 | (int) (tms2->tms_cstime - tms1->tms_cstime), |
| 1234 | "<<<test_end>>>"); |
| 1235 | } |
| 1236 | fflush(stdout); |
| 1237 | } |
| 1238 | |
| 1239 | /* The functions below are all debugging related */ |
| 1240 | |
| 1241 | static void |
| 1242 | pids_running(struct tag_pgrp *running, int keep_active) |
| 1243 | { |
| 1244 | int i; |
| 1245 | |
| 1246 | fprintf(stderr, "pids still running: "); |
| 1247 | for (i = 0; i < keep_active; ++i) { |
| 1248 | if (running[i].pgrp != 0) |
| 1249 | fprintf(stderr, "%d ", running[i].pgrp); |
| 1250 | } |
| 1251 | fprintf(stderr, "\n"); |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | static void |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1255 | orphans_running(struct orphan_pgrp *orphans) |
| 1256 | { |
| 1257 | struct orphan_pgrp *orph; |
| 1258 | |
| 1259 | fprintf(stderr, "orphans still running: "); |
| 1260 | for (orph = orphans; orph != NULL; orph = orph->next) { |
| 1261 | if (orph->pgrp != 0) |
| 1262 | fprintf(stderr, "%d ", -(orph->pgrp)); |
| 1263 | } |
| 1264 | fprintf(stderr, "\n"); |
| 1265 | } |
| 1266 | |
| 1267 | static void |
| 1268 | dump_coll(struct collection *coll) |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1269 | { |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 1270 | int i; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1271 | |
| 1272 | for (i = 0; i < coll->cnt; ++i) { |
nstraz | cd87d68 | 2000-09-21 20:42:31 +0000 | [diff] [blame] | 1273 | fprintf(stderr, "coll %d\n", i); |
nstraz | cde46c8 | 2001-03-08 19:13:21 +0000 | [diff] [blame] | 1274 | fprintf(stderr, " name=%s cmdline=%s\n", coll->ary[i]->name, |
| 1275 | coll->ary[i]->cmdline); |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | void |
| 1280 | wait_handler( int sig ) |
| 1281 | { |
| 1282 | static int lastsent = 0; |
| 1283 | |
| 1284 | if( sig == 0 ){ |
| 1285 | lastsent = 0; |
| 1286 | } else { |
| 1287 | rec_signal = sig; |
| 1288 | if( sig == SIGUSR2 ) |
| 1289 | return; |
| 1290 | if( lastsent == 0 ) |
| 1291 | send_signal = sig; |
| 1292 | else if( lastsent == SIGUSR1 ) |
| 1293 | send_signal = SIGINT; |
| 1294 | else if( lastsent == sig ) |
| 1295 | send_signal = SIGTERM; |
| 1296 | else if( lastsent == SIGTERM ) |
| 1297 | send_signal = SIGHUP; |
| 1298 | else if( lastsent == SIGHUP ) |
| 1299 | send_signal = SIGKILL; |
| 1300 | lastsent = send_signal; |
nstraz | f307d5f | 2000-09-14 21:54:44 +0000 | [diff] [blame] | 1301 | } |
| 1302 | } |