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