1 /* 2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. 3 * AUTHOR : Kent Rogers (from Dave Fenner's original) 4 * CO-PILOT : Rich Logan 5 * DATE STARTED : 05/01/90 (rewritten 1/96) 6 * Copyright (c) 2009-2016 Cyril Hrubis <chrubis (at) suse.cz> 7 * 8 * This program is free software; you can redistribute it and/or modify it 9 * under the terms of version 2 of the GNU General Public License as 10 * published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope that it would be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 15 * 16 * Further, this software is distributed without any warranty that it is 17 * free of the rightful claim of any third person regarding infringement 18 * or the like. Any license provided herein, whether implied or 19 * otherwise, applies only to this software file. Patent licenses, if 20 * any, provided herein do not apply to combinations of this program with 21 * other software, or any other product whatsoever. 22 * 23 * You should have received a copy of the GNU General Public License along 24 * with this program; if not, write the Free Software Foundation, Inc., 25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 26 * 27 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, 28 * Mountain View, CA 94043, or: 29 * 30 * http://www.sgi.com 31 * 32 * For further information regarding this notice, see: 33 * 34 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ 35 */ 36 37 #define _GNU_SOURCE 38 39 #include <pthread.h> 40 #include <assert.h> 41 #include <errno.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <stdarg.h> 45 #include <string.h> 46 #include <unistd.h> 47 #include <sys/types.h> 48 #include <sys/wait.h> 49 50 #include "test.h" 51 #include "usctest.h" 52 #include "ltp_priv.h" 53 54 long TEST_RETURN; 55 int TEST_ERRNO; 56 57 #define VERBOSE 1 58 #define NOPASS 3 59 #define DISCARD 4 60 61 #define MAXMESG 80 /* max length of internal messages */ 62 #define USERMESG 2048 /* max length of user message */ 63 #define TRUE 1 64 #define FALSE 0 65 66 /* 67 * EXPAND_VAR_ARGS - Expand the variable portion (arg_fmt) of a result 68 * message into the specified string. 69 * 70 * NOTE (garrcoop): arg_fmt _must_ be the last element in each function 71 * argument list that employs this. 72 */ 73 #define EXPAND_VAR_ARGS(buf, arg_fmt, buf_len) do {\ 74 va_list ap; \ 75 assert(arg_fmt != NULL); \ 76 va_start(ap, arg_fmt); \ 77 vsnprintf(buf, buf_len, arg_fmt, ap); \ 78 va_end(ap); \ 79 assert(strlen(buf) > 0); \ 80 } while (0) 81 82 static pthread_mutex_t tmutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; 83 84 static void check_env(void); 85 static void tst_condense(int tnum, int ttype, const char *tmesg); 86 static void tst_print(const char *tcid, int tnum, int ttype, const char *tmesg); 87 88 static int T_exitval = 0; /* exit value used by tst_exit() */ 89 static int T_mode = VERBOSE; /* flag indicating print mode: VERBOSE, */ 90 /* NOPASS, DISCARD */ 91 92 static char Warn_mesg[MAXMESG]; /* holds warning messages */ 93 94 /* 95 * These are used for condensing output when NOT in verbose mode. 96 */ 97 static int Buffered = FALSE; /* TRUE if condensed output is currently */ 98 /* buffered (i.e. not yet printed) */ 99 static char *Last_tcid; /* previous test case id */ 100 static int Last_num; /* previous test case number */ 101 static int Last_type; /* previous test result type */ 102 static char *Last_mesg; /* previous test result message */ 103 104 int tst_count = 0; 105 106 /* 107 * These globals must be defined in the test. 108 */ 109 extern char *TCID; /* Test case identifier from the test source */ 110 extern int TST_TOTAL; /* Total number of test cases from the test */ 111 112 113 struct pair { 114 const char *name; 115 int val; 116 }; 117 118 #define PAIR(def) [def] = {.name = #def, .val = def}, 119 #define STRPAIR(key, value) [key] = {.name = value, .val = key}, 120 121 #define PAIR_LOOKUP(pair_arr, idx) do { \ 122 if (idx < 0 || (size_t)idx >= ARRAY_SIZE(pair_arr) || \ 123 pair_arr[idx].name == NULL) \ 124 return "???"; \ 125 return pair_arr[idx].name; \ 126 } while (0) 127 128 const char *strttype(int ttype) 129 { 130 static const struct pair ttype_pairs[] = { 131 PAIR(TPASS) 132 PAIR(TFAIL) 133 PAIR(TBROK) 134 PAIR(TCONF) 135 PAIR(TWARN) 136 PAIR(TINFO) 137 }; 138 139 PAIR_LOOKUP(ttype_pairs, TTYPE_RESULT(ttype)); 140 } 141 142 #include "errnos.h" 143 #include "signame.h" 144 145 static void tst_res__(const char *file, const int lineno, int ttype, 146 const char *arg_fmt, ...) 147 { 148 pthread_mutex_lock(&tmutex); 149 150 char tmesg[USERMESG]; 151 int len = 0; 152 int ttype_result = TTYPE_RESULT(ttype); 153 154 if (file && (ttype_result != TPASS && ttype_result != TINFO)) 155 len = sprintf(tmesg, "%s:%d: ", file, lineno); 156 EXPAND_VAR_ARGS(tmesg + len, arg_fmt, USERMESG - len); 157 158 /* 159 * Save the test result type by ORing ttype into the current exit 160 * value (used by tst_exit()). 161 */ 162 T_exitval |= ttype_result; 163 164 check_env(); 165 166 /* 167 * Set the test case number and print the results, depending on the 168 * display type. 169 */ 170 if (ttype_result == TWARN || ttype_result == TINFO) { 171 tst_print(TCID, 0, ttype, tmesg); 172 } else { 173 if (tst_count < 0) 174 tst_print(TCID, 0, TWARN, 175 "tst_res(): tst_count < 0 is not valid"); 176 177 /* 178 * Process each display type. 179 */ 180 switch (T_mode) { 181 case DISCARD: 182 break; 183 case NOPASS: /* filtered by tst_print() */ 184 tst_condense(tst_count + 1, ttype, tmesg); 185 break; 186 default: /* VERBOSE */ 187 tst_print(TCID, tst_count + 1, ttype, tmesg); 188 break; 189 } 190 191 tst_count++; 192 } 193 194 pthread_mutex_unlock(&tmutex); 195 } 196 197 static void tst_condense(int tnum, int ttype, const char *tmesg) 198 { 199 int ttype_result = TTYPE_RESULT(ttype); 200 201 /* 202 * If this result is the same as the previous result, return. 203 */ 204 if (Buffered == TRUE) { 205 if (strcmp(Last_tcid, TCID) == 0 && Last_type == ttype_result && 206 strcmp(Last_mesg, tmesg) == 0) 207 return; 208 209 /* 210 * This result is different from the previous result. First, 211 * print the previous result. 212 */ 213 tst_print(Last_tcid, Last_num, Last_type, Last_mesg); 214 free(Last_tcid); 215 free(Last_mesg); 216 } 217 218 /* 219 * If a file was specified, print the current result since we have no 220 * way of retaining the file contents for comparing with future 221 * results. Otherwise, buffer the current result info for next time. 222 */ 223 Last_tcid = malloc(strlen(TCID) + 1); 224 strcpy(Last_tcid, TCID); 225 Last_num = tnum; 226 Last_type = ttype_result; 227 Last_mesg = malloc(strlen(tmesg) + 1); 228 strcpy(Last_mesg, tmesg); 229 Buffered = TRUE; 230 } 231 232 void tst_flush(void) 233 { 234 NO_NEWLIB_ASSERT("Unknown", 0); 235 236 pthread_mutex_lock(&tmutex); 237 238 /* 239 * Print out last line if in NOPASS mode. 240 */ 241 if (Buffered == TRUE && T_mode == NOPASS) { 242 tst_print(Last_tcid, Last_num, Last_type, Last_mesg); 243 Buffered = FALSE; 244 } 245 246 fflush(stdout); 247 248 pthread_mutex_unlock(&tmutex); 249 } 250 251 static void tst_print(const char *tcid, int tnum, int ttype, const char *tmesg) 252 { 253 int err = errno; 254 const char *type; 255 int ttype_result = TTYPE_RESULT(ttype); 256 char message[USERMESG]; 257 size_t size; 258 259 /* 260 * Save the test result type by ORing ttype into the current exit value 261 * (used by tst_exit()). This is already done in tst_res(), but is 262 * also done here to catch internal warnings. For internal warnings, 263 * tst_print() is called directly with a case of TWARN. 264 */ 265 T_exitval |= ttype_result; 266 267 /* 268 * If output mode is DISCARD, or if the output mode is NOPASS and this 269 * result is not one of FAIL, BROK, or WARN, just return. This check 270 * is necessary even though we check for DISCARD mode inside of 271 * tst_res(), since occasionally we get to this point without going 272 * through tst_res() (e.g. internal TWARN messages). 273 */ 274 if (T_mode == DISCARD || (T_mode == NOPASS && ttype_result != TFAIL && 275 ttype_result != TBROK 276 && ttype_result != TWARN)) 277 return; 278 279 /* 280 * Build the result line and print it. 281 */ 282 type = strttype(ttype); 283 if (T_mode == VERBOSE) { 284 size = snprintf(message, sizeof(message), 285 "%-8s %4d %s : %s", tcid, tnum, type, tmesg); 286 } else { 287 size = snprintf(message, sizeof(message), 288 "%-8s %4d %s : %s", 289 tcid, tnum, type, tmesg); 290 } 291 292 if (size >= sizeof(message)) { 293 printf("%s: %i: line too long\n", __func__, __LINE__); 294 abort(); 295 } 296 297 if (ttype & TERRNO) { 298 size += snprintf(message + size, sizeof(message) - size, 299 ": errno=%s(%i): %s", tst_strerrno(err), 300 err, strerror(err)); 301 } 302 303 if (size >= sizeof(message)) { 304 printf("%s: %i: line too long\n", __func__, __LINE__); 305 abort(); 306 } 307 308 if (ttype & TTERRNO) { 309 size += snprintf(message + size, sizeof(message) - size, 310 ": TEST_ERRNO=%s(%i): %s", 311 tst_strerrno(TEST_ERRNO), (int)TEST_ERRNO, 312 strerror(TEST_ERRNO)); 313 } 314 315 if (size >= sizeof(message)) { 316 printf("%s: %i: line too long\n", __func__, __LINE__); 317 abort(); 318 } 319 320 if (ttype & TRERRNO) { 321 size += snprintf(message + size, sizeof(message) - size, 322 ": TEST_RETURN=%s(%i): %s", 323 tst_strerrno(TEST_RETURN), (int)TEST_RETURN, 324 strerror(TEST_RETURN)); 325 } 326 327 if (size + 1 >= sizeof(message)) { 328 printf("%s: %i: line too long\n", __func__, __LINE__); 329 abort(); 330 } 331 332 message[size] = '\n'; 333 message[size + 1] = '\0'; 334 335 fputs(message, stdout); 336 } 337 338 static void check_env(void) 339 { 340 static int first_time = 1; 341 char *value; 342 343 if (!first_time) 344 return; 345 346 first_time = 0; 347 348 /* BTOUTPUT not defined, use default */ 349 if ((value = getenv(TOUTPUT)) == NULL) { 350 T_mode = VERBOSE; 351 return; 352 } 353 354 if (strcmp(value, TOUT_NOPASS_S) == 0) { 355 T_mode = NOPASS; 356 return; 357 } 358 359 if (strcmp(value, TOUT_DISCARD_S) == 0) { 360 T_mode = DISCARD; 361 return; 362 } 363 364 T_mode = VERBOSE; 365 return; 366 } 367 368 void tst_exit(void) 369 { 370 NO_NEWLIB_ASSERT("Unknown", 0); 371 372 pthread_mutex_lock(&tmutex); 373 374 tst_flush(); 375 376 exit(T_exitval & ~TINFO); 377 } 378 379 pid_t tst_fork(void) 380 { 381 pid_t child; 382 383 NO_NEWLIB_ASSERT("Unknown", 0); 384 385 tst_flush(); 386 387 child = fork(); 388 if (child == 0) 389 T_exitval = 0; 390 391 return child; 392 } 393 394 void tst_record_childstatus(void (*cleanup)(void), pid_t child) 395 { 396 int status, ttype_result; 397 398 NO_NEWLIB_ASSERT("Unknown", 0); 399 400 if (waitpid(child, &status, 0) < 0) 401 tst_brkm(TBROK | TERRNO, cleanup, "waitpid(%d) failed", child); 402 403 if (WIFEXITED(status)) { 404 ttype_result = WEXITSTATUS(status); 405 ttype_result = TTYPE_RESULT(ttype_result); 406 T_exitval |= ttype_result; 407 408 if (ttype_result == TPASS) 409 tst_resm(TINFO, "Child process returned TPASS"); 410 411 if (ttype_result & TFAIL) 412 tst_resm(TINFO, "Child process returned TFAIL"); 413 414 if (ttype_result & TBROK) 415 tst_resm(TINFO, "Child process returned TBROK"); 416 417 if (ttype_result & TCONF) 418 tst_resm(TINFO, "Child process returned TCONF"); 419 420 } else { 421 tst_brkm(TBROK, cleanup, "child process(%d) killed by " 422 "unexpected signal %s(%d)", child, 423 tst_strsig(WTERMSIG(status)), WTERMSIG(status)); 424 } 425 } 426 427 pid_t tst_vfork(void) 428 { 429 NO_NEWLIB_ASSERT("Unknown", 0); 430 431 tst_flush(); 432 return vfork(); 433 } 434 435 /* 436 * Make tst_brk reentrant so that one can call the SAFE_* macros from within 437 * user-defined cleanup functions. 438 */ 439 static int tst_brk_entered = 0; 440 441 static void tst_brk__(const char *file, const int lineno, int ttype, 442 void (*func)(void), const char *arg_fmt, ...) 443 { 444 pthread_mutex_lock(&tmutex); 445 446 char tmesg[USERMESG]; 447 int ttype_result = TTYPE_RESULT(ttype); 448 449 EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG); 450 451 /* 452 * Only FAIL, BROK, CONF, and RETR are supported by tst_brk(). 453 */ 454 if (ttype_result != TFAIL && ttype_result != TBROK && 455 ttype_result != TCONF) { 456 sprintf(Warn_mesg, "%s: Invalid Type: %d. Using TBROK", 457 __func__, ttype_result); 458 tst_print(TCID, 0, TWARN, Warn_mesg); 459 /* Keep TERRNO, TTERRNO, etc. */ 460 ttype = (ttype & ~ttype_result) | TBROK; 461 } 462 463 tst_res__(file, lineno, ttype, "%s", tmesg); 464 if (tst_brk_entered == 0) { 465 if (ttype_result == TCONF) { 466 tst_res__(file, lineno, ttype, 467 "Remaining cases not appropriate for " 468 "configuration"); 469 } else if (ttype_result == TBROK) { 470 tst_res__(file, lineno, TBROK, 471 "Remaining cases broken"); 472 } 473 } 474 475 /* 476 * If no cleanup function was specified, just return to the caller. 477 * Otherwise call the specified function. 478 */ 479 if (func != NULL) { 480 tst_brk_entered++; 481 (*func) (); 482 tst_brk_entered--; 483 } 484 if (tst_brk_entered == 0) 485 tst_exit(); 486 487 pthread_mutex_unlock(&tmutex); 488 } 489 490 void tst_resm_(const char *file, const int lineno, int ttype, 491 const char *arg_fmt, ...) 492 { 493 char tmesg[USERMESG]; 494 495 EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG); 496 497 if (tst_test) 498 tst_res_(file, lineno, ttype, "%s", tmesg); 499 else 500 tst_res__(file, lineno, ttype, "%s", tmesg); 501 } 502 503 void tst_resm_hexd_(const char *file, const int lineno, int ttype, 504 const void *buf, size_t size, const char *arg_fmt, ...) 505 { 506 NO_NEWLIB_ASSERT(file, lineno); 507 508 pthread_mutex_lock(&tmutex); 509 510 char tmesg[USERMESG]; 511 512 EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG); 513 514 static const size_t symb_num = 2; /* xx */ 515 static const size_t size_max = 16; 516 size_t offset = strlen(tmesg); 517 char *pmesg = tmesg; 518 519 if (size > size_max || size == 0 || 520 (offset + size * (symb_num + 1)) >= USERMESG) 521 tst_res__(file, lineno, ttype, "%s", tmesg); 522 else 523 pmesg += offset; 524 525 size_t i; 526 for (i = 0; i < size; ++i) { 527 /* add space before byte except first one */ 528 if (pmesg != tmesg) 529 *(pmesg++) = ' '; 530 531 sprintf(pmesg, "%02x", ((unsigned char *)buf)[i]); 532 pmesg += symb_num; 533 if ((i + 1) % size_max == 0 || i + 1 == size) { 534 tst_res__(file, lineno, ttype, "%s", tmesg); 535 pmesg = tmesg; 536 } 537 } 538 539 pthread_mutex_unlock(&tmutex); 540 } 541 542 void tst_brkm_(const char *file, const int lineno, int ttype, 543 void (*func)(void), const char *arg_fmt, ...) 544 { 545 char tmesg[USERMESG]; 546 547 EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG); 548 549 if (tst_test) { 550 if (func) { 551 tst_brk_(file, lineno, TBROK, 552 "Non-NULL cleanup in newlib!"); 553 } 554 555 tst_brk_(file, lineno, ttype, "%s", tmesg); 556 } else { 557 tst_brk__(file, lineno, ttype, func, "%s", tmesg); 558 } 559 560 /* Shouldn't be reached, but fixes build time warnings about noreturn. */ 561 abort(); 562 } 563 564 void tst_require_root(void) 565 { 566 NO_NEWLIB_ASSERT("Unknown", 0); 567 568 if (geteuid() != 0) 569 tst_brkm(TCONF, NULL, "Test needs to be run as root"); 570 } 571