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