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 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 (file && (ttype_result != TPASS && ttype_result != TINFO))
178 len = sprintf(tmesg, "%s:%d: ", file, lineno);
179 EXPAND_VAR_ARGS(tmesg + len, arg_fmt, USERMESG - len);
180
181 /*
182 * Save the test result type by ORing ttype into the current exit
183 * value (used by tst_exit()).
184 */
185 T_exitval |= ttype_result;
186
187 if (ttype_result == TPASS)
188 passed_cnt++;
189
190 check_env();
191
192 /*
193 * Set the test case number and print the results, depending on the
194 * display type.
195 */
196 if (ttype_result == TWARN || ttype_result == TINFO) {
197 tst_print(TCID, 0, ttype, tmesg);
198 } else {
199 if (tst_count < 0)
200 tst_print(TCID, 0, TWARN,
201 "tst_res(): tst_count < 0 is not valid");
202
203 /*
204 * Process each display type.
205 */
206 switch (T_mode) {
207 case DISCARD:
208 break;
209 case NOPASS: /* filtered by tst_print() */
210 tst_condense(tst_count + 1, ttype, tmesg);
211 break;
212 default: /* VERBOSE */
213 tst_print(TCID, tst_count + 1, ttype, tmesg);
214 break;
215 }
216
217 tst_count++;
218 }
219
220 pthread_mutex_unlock(&tmutex);
221 }
222
tst_condense(int tnum,int ttype,const char * tmesg)223 static void tst_condense(int tnum, int ttype, const char *tmesg)
224 {
225 int ttype_result = TTYPE_RESULT(ttype);
226
227 /*
228 * If this result is the same as the previous result, return.
229 */
230 if (Buffered == TRUE) {
231 if (strcmp(Last_tcid, TCID) == 0 && Last_type == ttype_result &&
232 strcmp(Last_mesg, tmesg) == 0)
233 return;
234
235 /*
236 * This result is different from the previous result. First,
237 * print the previous result.
238 */
239 tst_print(Last_tcid, Last_num, Last_type, Last_mesg);
240 free(Last_tcid);
241 free(Last_mesg);
242 }
243
244 /*
245 * If a file was specified, print the current result since we have no
246 * way of retaining the file contents for comparing with future
247 * results. Otherwise, buffer the current result info for next time.
248 */
249 Last_tcid = malloc(strlen(TCID) + 1);
250 strcpy(Last_tcid, TCID);
251 Last_num = tnum;
252 Last_type = ttype_result;
253 Last_mesg = malloc(strlen(tmesg) + 1);
254 strcpy(Last_mesg, tmesg);
255 Buffered = TRUE;
256 }
257
tst_old_flush(void)258 void tst_old_flush(void)
259 {
260 NO_NEWLIB_ASSERT("Unknown", 0);
261
262 pthread_mutex_lock(&tmutex);
263
264 /*
265 * Print out last line if in NOPASS mode.
266 */
267 if (Buffered == TRUE && T_mode == NOPASS) {
268 tst_print(Last_tcid, Last_num, Last_type, Last_mesg);
269 Buffered = FALSE;
270 }
271
272 fflush(stdout);
273
274 pthread_mutex_unlock(&tmutex);
275 }
276
tst_print(const char * tcid,int tnum,int ttype,const char * tmesg)277 static void tst_print(const char *tcid, int tnum, int ttype, const char *tmesg)
278 {
279 int err = errno;
280 const char *type;
281 int ttype_result = TTYPE_RESULT(ttype);
282 char message[USERMESG];
283 size_t size = 0;
284
285 /*
286 * Save the test result type by ORing ttype into the current exit value
287 * (used by tst_exit()). This is already done in tst_res(), but is
288 * also done here to catch internal warnings. For internal warnings,
289 * tst_print() is called directly with a case of TWARN.
290 */
291 T_exitval |= ttype_result;
292
293 /*
294 * If output mode is DISCARD, or if the output mode is NOPASS and this
295 * result is not one of FAIL, BROK, or WARN, just return. This check
296 * is necessary even though we check for DISCARD mode inside of
297 * tst_res(), since occasionally we get to this point without going
298 * through tst_res() (e.g. internal TWARN messages).
299 */
300 if (T_mode == DISCARD || (T_mode == NOPASS && ttype_result != TFAIL &&
301 ttype_result != TBROK
302 && ttype_result != TWARN))
303 return;
304
305 /*
306 * Build the result line and print it.
307 */
308 type = strttype(ttype);
309
310 if (T_mode == VERBOSE) {
311 size += snprintf(message + size, sizeof(message) - size,
312 "%-8s %4d ", tcid, tnum);
313 } else {
314 size += snprintf(message + size, sizeof(message) - size,
315 "%-8s %4d ", tcid, tnum);
316 }
317
318 if (size >= sizeof(message)) {
319 printf("%s: %i: line too long\n", __func__, __LINE__);
320 abort();
321 }
322
323 if (tst_color_enabled(STDOUT_FILENO))
324 size += snprintf(message + size, sizeof(message) - size,
325 "%s%s%s : %s", tst_ttype2color(ttype), type, ANSI_COLOR_RESET, tmesg);
326 else
327 size += snprintf(message + size, sizeof(message) - size,
328 "%s : %s", type, tmesg);
329
330 if (size >= sizeof(message)) {
331 printf("%s: %i: line too long\n", __func__, __LINE__);
332 abort();
333 }
334
335 if (ttype & TERRNO) {
336 size += snprintf(message + size, sizeof(message) - size,
337 ": errno=%s(%i): %s", tst_strerrno(err),
338 err, strerror(err));
339 }
340
341 if (size >= sizeof(message)) {
342 printf("%s: %i: line too long\n", __func__, __LINE__);
343 abort();
344 }
345
346 if (ttype & TTERRNO) {
347 size += snprintf(message + size, sizeof(message) - size,
348 ": TEST_ERRNO=%s(%i): %s",
349 tst_strerrno(TEST_ERRNO), (int)TEST_ERRNO,
350 strerror(TEST_ERRNO));
351 }
352
353 if (size >= sizeof(message)) {
354 printf("%s: %i: line too long\n", __func__, __LINE__);
355 abort();
356 }
357
358 if (ttype & TRERRNO) {
359 err = TEST_RETURN < 0 ? -(int)TEST_RETURN : (int)TEST_RETURN;
360 size += snprintf(message + size, sizeof(message) - size,
361 ": TEST_RETURN=%s(%i): %s",
362 tst_strerrno(err), err, strerror(err));
363 }
364
365 if (size + 1 >= sizeof(message)) {
366 printf("%s: %i: line too long\n", __func__, __LINE__);
367 abort();
368 }
369
370 message[size] = '\n';
371 message[size + 1] = '\0';
372
373 fputs(message, stdout);
374 }
375
check_env(void)376 static void check_env(void)
377 {
378 static int first_time = 1;
379 char *value;
380
381 if (!first_time)
382 return;
383
384 first_time = 0;
385
386 /* BTOUTPUT not defined, use default */
387 if ((value = getenv(TOUTPUT)) == NULL) {
388 T_mode = VERBOSE;
389 return;
390 }
391
392 if (strcmp(value, TOUT_NOPASS_S) == 0) {
393 T_mode = NOPASS;
394 return;
395 }
396
397 if (strcmp(value, TOUT_DISCARD_S) == 0) {
398 T_mode = DISCARD;
399 return;
400 }
401
402 T_mode = VERBOSE;
403 return;
404 }
405
tst_exit(void)406 void tst_exit(void)
407 {
408 NO_NEWLIB_ASSERT("Unknown", 0);
409
410 pthread_mutex_lock(&tmutex);
411
412 tst_old_flush();
413
414 T_exitval &= ~TINFO;
415
416 if (T_exitval == TCONF && passed_cnt)
417 T_exitval &= ~TCONF;
418
419 exit(T_exitval);
420 }
421
tst_fork(void)422 pid_t tst_fork(void)
423 {
424 pid_t child;
425
426 NO_NEWLIB_ASSERT("Unknown", 0);
427
428 tst_old_flush();
429
430 child = fork();
431 if (child == 0)
432 T_exitval = 0;
433
434 return child;
435 }
436
tst_record_childstatus(void (* cleanup)(void),pid_t child)437 void tst_record_childstatus(void (*cleanup)(void), pid_t child)
438 {
439 int status, ttype_result;
440
441 NO_NEWLIB_ASSERT("Unknown", 0);
442
443 SAFE_WAITPID(cleanup, child, &status, 0);
444
445 if (WIFEXITED(status)) {
446 ttype_result = WEXITSTATUS(status);
447 ttype_result = TTYPE_RESULT(ttype_result);
448 T_exitval |= ttype_result;
449
450 if (ttype_result == TPASS)
451 tst_resm(TINFO, "Child process returned TPASS");
452
453 if (ttype_result & TFAIL)
454 tst_resm(TINFO, "Child process returned TFAIL");
455
456 if (ttype_result & TBROK)
457 tst_resm(TINFO, "Child process returned TBROK");
458
459 if (ttype_result & TCONF)
460 tst_resm(TINFO, "Child process returned TCONF");
461
462 } else {
463 tst_brkm(TBROK, cleanup, "child process(%d) killed by "
464 "unexpected signal %s(%d)", child,
465 tst_strsig(WTERMSIG(status)), WTERMSIG(status));
466 }
467 }
468
tst_vfork(void)469 pid_t tst_vfork(void)
470 {
471 NO_NEWLIB_ASSERT("Unknown", 0);
472
473 tst_old_flush();
474 return vfork();
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