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 <[email protected]>
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 "safe_macros.h"
52 #include "usctest.h"
53 #include "ltp_priv.h"
54 #include "tst_ansi_color.h"
55
56 long TEST_RETURN;
57 int TEST_ERRNO;
58 void *TST_RET_PTR;
59
60 #define VERBOSE 1
61 #define NOPASS 3
62 #define DISCARD 4
63
64 #define MAXMESG 80 /* max length of internal messages */
65 #define USERMESG 2048 /* max length of user message */
66 #define TRUE 1
67 #define FALSE 0
68
69 /*
70 * EXPAND_VAR_ARGS - Expand the variable portion (arg_fmt) of a result
71 * message into the specified string.
72 *
73 * NOTE (garrcoop): arg_fmt _must_ be the last element in each function
74 * argument list that employs this.
75 */
76 #define EXPAND_VAR_ARGS(buf, arg_fmt, buf_len) do {\
77 va_list ap; \
78 assert(arg_fmt != NULL); \
79 va_start(ap, arg_fmt); \
80 vsnprintf(buf, buf_len, arg_fmt, ap); \
81 va_end(ap); \
82 assert(strlen(buf) > 0); \
83 } while (0)
84
85 #if !defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) && defined(__ANDROID__)
86 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP PTHREAD_RECURSIVE_MUTEX_INITIALIZER
87 #endif
88
89 #ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
90 static pthread_mutex_t tmutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
91 #else
92 static pthread_mutex_t tmutex;
93
94 __attribute__((constructor))
init_tmutex(void)95 static void init_tmutex(void)
96 {
97 pthread_mutexattr_t mutattr = {0};
98
99 pthread_mutexattr_init(&mutattr);
100 pthread_mutexattr_settype(&mutattr, PTHREAD_MUTEX_RECURSIVE);
101 pthread_mutex_init(&tmutex, &mutattr);
102 pthread_mutexattr_destroy(&mutattr);
103 }
104 #endif
105
106 static void check_env(void);
107 static void tst_condense(int tnum, int ttype, const char *tmesg);
108 static void tst_print(const char *tcid, int tnum, int ttype, const char *tmesg);
109
110 static int T_exitval = 0; /* exit value used by tst_exit() */
111 static int passed_cnt;
112 static int T_mode = VERBOSE; /* flag indicating print mode: VERBOSE, */
113 /* NOPASS, DISCARD */
114
115 static char Warn_mesg[MAXMESG]; /* holds warning messages */
116
117 /*
118 * These are used for condensing output when NOT in verbose mode.
119 */
120 static int Buffered = FALSE; /* TRUE if condensed output is currently */
121 /* buffered (i.e. not yet printed) */
122 static char *Last_tcid; /* previous test case id */
123 static int Last_num; /* previous test case number */
124 static int Last_type; /* previous test result type */
125 static char *Last_mesg; /* previous test result message */
126
127 int tst_count = 0;
128
129 /*
130 * These globals must be defined in the test.
131 */
132 extern char *TCID; /* Test case identifier from the test source */
133 extern int TST_TOTAL; /* Total number of test cases from the test */
134
135
136 struct pair {
137 const char *name;
138 int val;
139 };
140
141 #define PAIR(def) [def] = {.name = #def, .val = def},
142 #define STRPAIR(key, value) [key] = {.name = value, .val = key},
143
144 #define PAIR_LOOKUP(pair_arr, idx) do { \
145 if (idx < 0 || (size_t)idx >= ARRAY_SIZE(pair_arr) || \
146 pair_arr[idx].name == NULL) \
147 return "???"; \
148 return pair_arr[idx].name; \
149 } while (0)
150
strttype(int ttype)151 const char *strttype(int ttype)
152 {
153 static const struct pair ttype_pairs[] = {
154 PAIR(TPASS)
155 PAIR(TFAIL)
156 PAIR(TBROK)
157 PAIR(TCONF)
158 PAIR(TWARN)
159 PAIR(TINFO)
160 };
161
162 PAIR_LOOKUP(ttype_pairs, TTYPE_RESULT(ttype));
163 }
164
165 #include "errnos.h"
166 #include "signame.h"
167
tst_res__(const char * file,const int lineno,int ttype,const char * arg_fmt,...)168 static void tst_res__(const char *file, const int lineno, int ttype,
169 const char *arg_fmt, ...)
170 {
171 pthread_mutex_lock(&tmutex);
172
173 char tmesg[USERMESG];
174 int len = 0;
175 int ttype_result = TTYPE_RESULT(ttype);
176
177 if (ttype_result == TDEBUG) {
178 printf("%s: %i: TDEBUG is not supported\n", __func__, __LINE__);
179 abort();
180 }
181
182 if (file && (ttype_result != TPASS && ttype_result != TINFO))
183 len = sprintf(tmesg, "%s:%d: ", file, lineno);
184 EXPAND_VAR_ARGS(tmesg + len, arg_fmt, USERMESG - len);
185
186 /*
187 * Save the test result type by ORing ttype into the current exit
188 * value (used by tst_exit()).
189 */
190 T_exitval |= ttype_result;
191
192 if (ttype_result == TPASS)
193 passed_cnt++;
194
195 check_env();
196
197 /*
198 * Set the test case number and print the results, depending on the
199 * display type.
200 */
201 if (ttype_result == TWARN || ttype_result == TINFO) {
202 tst_print(TCID, 0, ttype, tmesg);
203 } else {
204 if (tst_count < 0)
205 tst_print(TCID, 0, TWARN,
206 "tst_res(): tst_count < 0 is not valid");
207
208 /*
209 * Process each display type.
210 */
211 switch (T_mode) {
212 case DISCARD:
213 break;
214 case NOPASS: /* filtered by tst_print() */
215 tst_condense(tst_count + 1, ttype, tmesg);
216 break;
217 default: /* VERBOSE */
218 tst_print(TCID, tst_count + 1, ttype, tmesg);
219 break;
220 }
221
222 tst_count++;
223 }
224
225 pthread_mutex_unlock(&tmutex);
226 }
227
tst_condense(int tnum,int ttype,const char * tmesg)228 static void tst_condense(int tnum, int ttype, const char *tmesg)
229 {
230 int ttype_result = TTYPE_RESULT(ttype);
231
232 /*
233 * If this result is the same as the previous result, return.
234 */
235 if (Buffered == TRUE) {
236 if (strcmp(Last_tcid, TCID) == 0 && Last_type == ttype_result &&
237 strcmp(Last_mesg, tmesg) == 0)
238 return;
239
240 /*
241 * This result is different from the previous result. First,
242 * print the previous result.
243 */
244 tst_print(Last_tcid, Last_num, Last_type, Last_mesg);
245 free(Last_tcid);
246 free(Last_mesg);
247 }
248
249 /*
250 * If a file was specified, print the current result since we have no
251 * way of retaining the file contents for comparing with future
252 * results. Otherwise, buffer the current result info for next time.
253 */
254 Last_tcid = malloc(strlen(TCID) + 1);
255 strcpy(Last_tcid, TCID);
256 Last_num = tnum;
257 Last_type = ttype_result;
258 Last_mesg = malloc(strlen(tmesg) + 1);
259 strcpy(Last_mesg, tmesg);
260 Buffered = TRUE;
261 }
262
tst_old_flush(void)263 void tst_old_flush(void)
264 {
265 NO_NEWLIB_ASSERT("Unknown", 0);
266
267 pthread_mutex_lock(&tmutex);
268
269 /*
270 * Print out last line if in NOPASS mode.
271 */
272 if (Buffered == TRUE && T_mode == NOPASS) {
273 tst_print(Last_tcid, Last_num, Last_type, Last_mesg);
274 Buffered = FALSE;
275 }
276
277 fflush(stdout);
278
279 pthread_mutex_unlock(&tmutex);
280 }
281
tst_print(const char * tcid,int tnum,int ttype,const char * tmesg)282 static void tst_print(const char *tcid, int tnum, int ttype, const char *tmesg)
283 {
284 int err = errno;
285 const char *type;
286 int ttype_result = TTYPE_RESULT(ttype);
287 char message[USERMESG];
288 size_t size = 0;
289
290 /*
291 * Save the test result type by ORing ttype into the current exit value
292 * (used by tst_exit()). This is already done in tst_res(), but is
293 * also done here to catch internal warnings. For internal warnings,
294 * tst_print() is called directly with a case of TWARN.
295 */
296 T_exitval |= ttype_result;
297
298 /*
299 * If output mode is DISCARD, or if the output mode is NOPASS and this
300 * result is not one of FAIL, BROK, or WARN, just return. This check
301 * is necessary even though we check for DISCARD mode inside of
302 * tst_res(), since occasionally we get to this point without going
303 * through tst_res() (e.g. internal TWARN messages).
304 */
305 if (T_mode == DISCARD || (T_mode == NOPASS && ttype_result != TFAIL &&
306 ttype_result != TBROK
307 && ttype_result != TWARN))
308 return;
309
310 /*
311 * Build the result line and print it.
312 */
313 type = strttype(ttype);
314
315 if (T_mode == VERBOSE) {
316 size += snprintf(message + size, sizeof(message) - size,
317 "%-8s %4d ", tcid, tnum);
318 } else {
319 size += snprintf(message + size, sizeof(message) - size,
320 "%-8s %4d ", tcid, tnum);
321 }
322
323 if (size >= sizeof(message)) {
324 printf("%s: %i: line too long\n", __func__, __LINE__);
325 abort();
326 }
327
328 if (tst_color_enabled(STDOUT_FILENO))
329 size += snprintf(message + size, sizeof(message) - size,
330 "%s%s%s : %s", tst_ttype2color(ttype), type, ANSI_COLOR_RESET, tmesg);
331 else
332 size += snprintf(message + size, sizeof(message) - size,
333 "%s : %s", type, tmesg);
334
335 if (size >= sizeof(message)) {
336 printf("%s: %i: line too long\n", __func__, __LINE__);
337 abort();
338 }
339
340 if (ttype & TERRNO) {
341 size += snprintf(message + size, sizeof(message) - size,
342 ": errno=%s(%i): %s", tst_strerrno(err),
343 err, strerror(err));
344 }
345
346 if (size >= sizeof(message)) {
347 printf("%s: %i: line too long\n", __func__, __LINE__);
348 abort();
349 }
350
351 if (ttype & TTERRNO) {
352 size += snprintf(message + size, sizeof(message) - size,
353 ": TEST_ERRNO=%s(%i): %s",
354 tst_strerrno(TEST_ERRNO), (int)TEST_ERRNO,
355 strerror(TEST_ERRNO));
356 }
357
358 if (size >= sizeof(message)) {
359 printf("%s: %i: line too long\n", __func__, __LINE__);
360 abort();
361 }
362
363 if (ttype & TRERRNO) {
364 err = TEST_RETURN < 0 ? -(int)TEST_RETURN : (int)TEST_RETURN;
365 size += snprintf(message + size, sizeof(message) - size,
366 ": TEST_RETURN=%s(%i): %s",
367 tst_strerrno(err), err, strerror(err));
368 }
369
370 if (size + 1 >= sizeof(message)) {
371 printf("%s: %i: line too long\n", __func__, __LINE__);
372 abort();
373 }
374
375 message[size] = '\n';
376 message[size + 1] = '\0';
377
378 fputs(message, stdout);
379 }
380
check_env(void)381 static void check_env(void)
382 {
383 static int first_time = 1;
384 char *value;
385
386 if (!first_time)
387 return;
388
389 first_time = 0;
390
391 /* BTOUTPUT not defined, use default */
392 if ((value = getenv(TOUTPUT)) == NULL) {
393 T_mode = VERBOSE;
394 return;
395 }
396
397 if (strcmp(value, TOUT_NOPASS_S) == 0) {
398 T_mode = NOPASS;
399 return;
400 }
401
402 if (strcmp(value, TOUT_DISCARD_S) == 0) {
403 T_mode = DISCARD;
404 return;
405 }
406
407 T_mode = VERBOSE;
408 return;
409 }
410
tst_exit(void)411 void tst_exit(void)
412 {
413 NO_NEWLIB_ASSERT("Unknown", 0);
414
415 pthread_mutex_lock(&tmutex);
416
417 tst_old_flush();
418
419 T_exitval &= ~TINFO;
420
421 if (T_exitval == TCONF && passed_cnt)
422 T_exitval &= ~TCONF;
423
424 exit(T_exitval);
425 }
426
tst_fork(void)427 pid_t tst_fork(void)
428 {
429 pid_t child;
430
431 NO_NEWLIB_ASSERT("Unknown", 0);
432
433 tst_old_flush();
434
435 child = fork();
436 if (child == 0)
437 T_exitval = 0;
438
439 return child;
440 }
441
tst_record_childstatus(void (* cleanup)(void),pid_t child)442 void tst_record_childstatus(void (*cleanup)(void), pid_t child)
443 {
444 int status, ttype_result;
445
446 NO_NEWLIB_ASSERT("Unknown", 0);
447
448 SAFE_WAITPID(cleanup, child, &status, 0);
449
450 if (WIFEXITED(status)) {
451 ttype_result = WEXITSTATUS(status);
452 ttype_result = TTYPE_RESULT(ttype_result);
453 T_exitval |= ttype_result;
454
455 if (ttype_result == TPASS)
456 tst_resm(TINFO, "Child process returned TPASS");
457
458 if (ttype_result & TFAIL)
459 tst_resm(TINFO, "Child process returned TFAIL");
460
461 if (ttype_result & TBROK)
462 tst_resm(TINFO, "Child process returned TBROK");
463
464 if (ttype_result & TCONF)
465 tst_resm(TINFO, "Child process returned TCONF");
466
467 } else {
468 tst_brkm(TBROK, cleanup, "child process(%d) killed by "
469 "unexpected signal %s(%d)", child,
470 tst_strsig(WTERMSIG(status)), WTERMSIG(status));
471 }
472 }
473
474 /*
475 * Make tst_brk reentrant so that one can call the SAFE_* macros from within
476 * user-defined cleanup functions.
477 */
478 static int tst_brk_entered = 0;
479
tst_brk__(const char * file,const int lineno,int ttype,void (* func)(void),const char * arg_fmt,...)480 static void tst_brk__(const char *file, const int lineno, int ttype,
481 void (*func)(void), const char *arg_fmt, ...)
482 {
483 pthread_mutex_lock(&tmutex);
484
485 char tmesg[USERMESG];
486 int ttype_result = TTYPE_RESULT(ttype);
487
488 EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG);
489
490 /*
491 * Only FAIL, BROK, CONF, and RETR are supported by tst_brk().
492 */
493 if (ttype_result != TFAIL && ttype_result != TBROK &&
494 ttype_result != TCONF) {
495 sprintf(Warn_mesg, "%s: Invalid Type: %d. Using TBROK",
496 __func__, ttype_result);
497 tst_print(TCID, 0, TWARN, Warn_mesg);
498 /* Keep TERRNO, TTERRNO, etc. */
499 ttype = (ttype & ~ttype_result) | TBROK;
500 }
501
502 tst_res__(file, lineno, ttype, "%s", tmesg);
503 if (tst_brk_entered == 0) {
504 if (ttype_result == TCONF) {
505 tst_res__(file, lineno, ttype,
506 "Remaining cases not appropriate for "
507 "configuration");
508 } else if (ttype_result == TBROK) {
509 tst_res__(file, lineno, TBROK,
510 "Remaining cases broken");
511 }
512 }
513
514 /*
515 * If no cleanup function was specified, just return to the caller.
516 * Otherwise call the specified function.
517 */
518 if (func != NULL) {
519 tst_brk_entered++;
520 (*func) ();
521 tst_brk_entered--;
522 }
523 if (tst_brk_entered == 0)
524 tst_exit();
525
526 pthread_mutex_unlock(&tmutex);
527 }
528
tst_resm_(const char * file,const int lineno,int ttype,const char * arg_fmt,...)529 void tst_resm_(const char *file, const int lineno, int ttype,
530 const char *arg_fmt, ...)
531 {
532 char tmesg[USERMESG];
533
534 EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG);
535
536 if (tst_test)
537 tst_res_(file, lineno, ttype, "%s", tmesg);
538 else
539 tst_res__(file, lineno, ttype, "%s", tmesg);
540 }
541
542 typedef void (*tst_res_func_t)(const char *file, const int lineno,
543 int ttype, const char *fmt, ...);
544
tst_resm_hexd_(const char * file,const int lineno,int ttype,const void * buf,size_t size,const char * arg_fmt,...)545 void tst_resm_hexd_(const char *file, const int lineno, int ttype,
546 const void *buf, size_t size, const char *arg_fmt, ...)
547 {
548 char tmesg[USERMESG];
549 static const size_t symb_num = 2; /* xx */
550 static const size_t size_max = 16;
551 size_t offset;
552 size_t i;
553 char *pmesg = tmesg;
554 tst_res_func_t res_func;
555
556 if (tst_test)
557 res_func = tst_res_;
558 else
559 res_func = tst_res__;
560
561 EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG);
562 offset = strlen(tmesg);
563
564 if (size > size_max || size == 0 ||
565 (offset + size * (symb_num + 1)) >= USERMESG)
566 res_func(file, lineno, ttype, "%s", tmesg);
567 else
568 pmesg += offset;
569
570 for (i = 0; i < size; ++i) {
571 /* add space before byte except first one */
572 if (pmesg != tmesg)
573 *(pmesg++) = ' ';
574
575 sprintf(pmesg, "%02x", ((unsigned char *)buf)[i]);
576 pmesg += symb_num;
577 if ((i + 1) % size_max == 0 || i + 1 == size) {
578 res_func(file, lineno, ttype, "%s", tmesg);
579 pmesg = tmesg;
580 }
581 }
582 }
583
tst_brkm__(const char * file,const int lineno,int ttype,void (* func)(void),const char * arg_fmt,...)584 void tst_brkm__(const char *file, const int lineno, int ttype,
585 void (*func)(void), const char *arg_fmt, ...)
586 {
587 char tmesg[USERMESG];
588
589 EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG);
590
591 if (tst_test) {
592 if (func) {
593 tst_brk_(file, lineno, TBROK,
594 "Non-NULL cleanup in newlib!");
595 }
596
597 tst_brk_(file, lineno, ttype, "%s", tmesg);
598 }
599
600 tst_brk__(file, lineno, ttype, func, "%s", tmesg);
601
602 /* Shouldn't be reached, but fixes build time warnings about noreturn. */
603 abort();
604 }
605
tst_require_root(void)606 void tst_require_root(void)
607 {
608 NO_NEWLIB_ASSERT("Unknown", 0);
609
610 if (geteuid() != 0)
611 tst_brkm(TCONF, NULL, "Test needs to be run as root");
612 }
613