1 /* 2 * Automated Testing Framework (atf) 3 * 4 * Copyright (c) 2008, 2009, 2010 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/wait.h> 31 32 #include <errno.h> 33 #include <fcntl.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <unistd.h> 38 39 #include "atf-c/build.h" 40 #include "atf-c/check.h" 41 #include "atf-c/config.h" 42 #include "atf-c/defs.h" 43 #include "atf-c/error.h" 44 #include "atf-c/utils.h" 45 46 #include "detail/dynstr.h" 47 #include "detail/fs.h" 48 #include "detail/list.h" 49 #include "detail/process.h" 50 #include "detail/sanity.h" 51 52 /* --------------------------------------------------------------------- 53 * Auxiliary functions. 54 * --------------------------------------------------------------------- */ 55 56 static 57 atf_error_t 58 create_tmpdir(atf_fs_path_t *dir) 59 { 60 atf_error_t err; 61 62 err = atf_fs_path_init_fmt(dir, "%s/check.XXXXXX", 63 atf_config_get("atf_workdir")); 64 if (atf_is_error(err)) 65 goto out; 66 67 err = atf_fs_mkdtemp(dir); 68 if (atf_is_error(err)) { 69 atf_fs_path_fini(dir); 70 goto out; 71 } 72 73 INV(!atf_is_error(err)); 74 out: 75 return err; 76 } 77 78 static 79 void 80 cleanup_tmpdir(const atf_fs_path_t *dir, const atf_fs_path_t *outfile, 81 const atf_fs_path_t *errfile) 82 { 83 { 84 atf_error_t err = atf_fs_unlink(outfile); 85 if (atf_is_error(err)) { 86 INV(atf_error_is(err, "libc") && 87 atf_libc_error_code(err) == ENOENT); 88 atf_error_free(err); 89 } else 90 INV(!atf_is_error(err)); 91 } 92 93 { 94 atf_error_t err = atf_fs_unlink(errfile); 95 if (atf_is_error(err)) { 96 INV(atf_error_is(err, "libc") && 97 atf_libc_error_code(err) == ENOENT); 98 atf_error_free(err); 99 } else 100 INV(!atf_is_error(err)); 101 } 102 103 { 104 atf_error_t err = atf_fs_rmdir(dir); 105 INV(!atf_is_error(err)); 106 } 107 } 108 109 static 110 int 111 const_execvp(const char *file, const char *const *argv) 112 { 113 #define UNCONST(a) ((void *)(unsigned long)(const void *)(a)) 114 return execvp(file, UNCONST(argv)); 115 #undef UNCONST 116 } 117 118 static 119 atf_error_t 120 init_sb(const atf_fs_path_t *path, atf_process_stream_t *sb) 121 { 122 atf_error_t err; 123 124 if (path == NULL) 125 err = atf_process_stream_init_inherit(sb); 126 else 127 err = atf_process_stream_init_redirect_path(sb, path); 128 129 return err; 130 } 131 132 static 133 atf_error_t 134 init_sbs(const atf_fs_path_t *outfile, atf_process_stream_t *outsb, 135 const atf_fs_path_t *errfile, atf_process_stream_t *errsb) 136 { 137 atf_error_t err; 138 139 err = init_sb(outfile, outsb); 140 if (atf_is_error(err)) 141 goto out; 142 143 err = init_sb(errfile, errsb); 144 if (atf_is_error(err)) { 145 atf_process_stream_fini(outsb); 146 goto out; 147 } 148 149 out: 150 return err; 151 } 152 153 struct exec_data { 154 const char *const *m_argv; 155 }; 156 157 static void exec_child(void *) ATF_DEFS_ATTRIBUTE_NORETURN; 158 159 static 160 void 161 exec_child(void *v) 162 { 163 struct exec_data *ea = v; 164 165 const_execvp(ea->m_argv[0], ea->m_argv); 166 fprintf(stderr, "execvp(%s) failed: %s\n", ea->m_argv[0], strerror(errno)); 167 exit(127); 168 } 169 170 static 171 atf_error_t 172 fork_and_wait(const char *const *argv, const atf_fs_path_t *outfile, 173 const atf_fs_path_t *errfile, atf_process_status_t *status) 174 { 175 atf_error_t err; 176 atf_process_child_t child; 177 atf_process_stream_t outsb, errsb; 178 struct exec_data ea = { argv }; 179 180 err = init_sbs(outfile, &outsb, errfile, &errsb); 181 if (atf_is_error(err)) 182 goto out; 183 184 err = atf_process_fork(&child, exec_child, &outsb, &errsb, &ea); 185 if (atf_is_error(err)) 186 goto out_sbs; 187 188 err = atf_process_child_wait(&child, status); 189 190 out_sbs: 191 atf_process_stream_fini(&errsb); 192 atf_process_stream_fini(&outsb); 193 out: 194 return err; 195 } 196 197 static 198 void 199 update_success_from_status(const char *progname, 200 const atf_process_status_t *status, bool *success) 201 { 202 bool s = atf_process_status_exited(status) && 203 atf_process_status_exitstatus(status) == EXIT_SUCCESS; 204 205 if (atf_process_status_exited(status)) { 206 if (atf_process_status_exitstatus(status) == EXIT_SUCCESS) 207 INV(s); 208 else { 209 INV(!s); 210 fprintf(stderr, "%s failed with exit code %d\n", progname, 211 atf_process_status_exitstatus(status)); 212 } 213 } else if (atf_process_status_signaled(status)) { 214 INV(!s); 215 fprintf(stderr, "%s failed due to signal %d%s\n", progname, 216 atf_process_status_termsig(status), 217 atf_process_status_coredump(status) ? " (core dumped)" : ""); 218 } else { 219 INV(!s); 220 fprintf(stderr, "%s failed due to unknown reason\n", progname); 221 } 222 223 *success = s; 224 } 225 226 static 227 atf_error_t 228 array_to_list(const char *const *a, atf_list_t *l) 229 { 230 atf_error_t err; 231 232 err = atf_list_init(l); 233 if (atf_is_error(err)) 234 goto out; 235 236 while (*a != NULL) { 237 char *item = strdup(*a); 238 if (item == NULL) { 239 err = atf_no_memory_error(); 240 goto out; 241 } 242 243 err = atf_list_append(l, item, true); 244 if (atf_is_error(err)) 245 goto out; 246 247 a++; 248 } 249 250 out: 251 return err; 252 } 253 254 static void 255 print_array(const char *const *array, const char *pfx) 256 { 257 const char *const *ptr; 258 259 printf("%s", pfx); 260 for (ptr = array; *ptr != NULL; ptr++) 261 printf(" %s", *ptr); 262 printf("\n"); 263 } 264 265 static 266 atf_error_t 267 check_build_run(const char *const *argv, bool *success) 268 { 269 atf_error_t err; 270 atf_process_status_t status; 271 272 print_array(argv, ">"); 273 274 err = fork_and_wait(argv, NULL, NULL, &status); 275 if (atf_is_error(err)) 276 goto out; 277 278 update_success_from_status(argv[0], &status, success); 279 atf_process_status_fini(&status); 280 281 INV(!atf_is_error(err)); 282 out: 283 return err; 284 } 285 286 /* --------------------------------------------------------------------- 287 * The "atf_check_result" type. 288 * --------------------------------------------------------------------- */ 289 290 struct atf_check_result_impl { 291 atf_list_t m_argv; 292 atf_fs_path_t m_dir; 293 atf_fs_path_t m_stdout; 294 atf_fs_path_t m_stderr; 295 atf_process_status_t m_status; 296 }; 297 298 static 299 atf_error_t 300 atf_check_result_init(atf_check_result_t *r, const char *const *argv, 301 const atf_fs_path_t *dir) 302 { 303 atf_error_t err; 304 const char *workdir; 305 306 r->pimpl = malloc(sizeof(struct atf_check_result_impl)); 307 if (r->pimpl == NULL) 308 return atf_no_memory_error(); 309 310 workdir = atf_config_get("atf_workdir"); 311 312 err = array_to_list(argv, &r->pimpl->m_argv); 313 if (atf_is_error(err)) 314 goto out; 315 316 err = atf_fs_path_copy(&r->pimpl->m_dir, dir); 317 if (atf_is_error(err)) 318 goto err_argv; 319 320 err = atf_fs_path_init_fmt(&r->pimpl->m_stdout, "%s/stdout", 321 atf_fs_path_cstring(dir)); 322 if (atf_is_error(err)) 323 goto err_dir; 324 325 err = atf_fs_path_init_fmt(&r->pimpl->m_stderr, "%s/stderr", 326 atf_fs_path_cstring(dir)); 327 if (atf_is_error(err)) 328 goto err_stdout; 329 330 INV(!atf_is_error(err)); 331 goto out; 332 333 err_stdout: 334 atf_fs_path_fini(&r->pimpl->m_stdout); 335 err_dir: 336 atf_fs_path_fini(&r->pimpl->m_dir); 337 err_argv: 338 atf_list_fini(&r->pimpl->m_argv); 339 out: 340 return err; 341 } 342 343 void 344 atf_check_result_fini(atf_check_result_t *r) 345 { 346 atf_process_status_fini(&r->pimpl->m_status); 347 348 cleanup_tmpdir(&r->pimpl->m_dir, &r->pimpl->m_stdout, 349 &r->pimpl->m_stderr); 350 atf_fs_path_fini(&r->pimpl->m_stdout); 351 atf_fs_path_fini(&r->pimpl->m_stderr); 352 atf_fs_path_fini(&r->pimpl->m_dir); 353 354 atf_list_fini(&r->pimpl->m_argv); 355 356 free(r->pimpl); 357 } 358 359 const char * 360 atf_check_result_stdout(const atf_check_result_t *r) 361 { 362 return atf_fs_path_cstring(&r->pimpl->m_stdout); 363 } 364 365 const char * 366 atf_check_result_stderr(const atf_check_result_t *r) 367 { 368 return atf_fs_path_cstring(&r->pimpl->m_stderr); 369 } 370 371 bool 372 atf_check_result_exited(const atf_check_result_t *r) 373 { 374 return atf_process_status_exited(&r->pimpl->m_status); 375 } 376 377 int 378 atf_check_result_exitcode(const atf_check_result_t *r) 379 { 380 return atf_process_status_exitstatus(&r->pimpl->m_status); 381 } 382 383 bool 384 atf_check_result_signaled(const atf_check_result_t *r) 385 { 386 return atf_process_status_signaled(&r->pimpl->m_status); 387 } 388 389 int 390 atf_check_result_termsig(const atf_check_result_t *r) 391 { 392 return atf_process_status_termsig(&r->pimpl->m_status); 393 } 394 395 /* --------------------------------------------------------------------- 396 * Free functions. 397 * --------------------------------------------------------------------- */ 398 399 /* XXX: This function shouldn't be in this module. It messes with stdout 400 * and stderr, and it provides a very high-end interface. This belongs, 401 * probably, somewhere related to test cases (such as in the tc module). */ 402 atf_error_t 403 atf_check_build_c_o(const char *sfile, 404 const char *ofile, 405 const char *const optargs[], 406 bool *success) 407 { 408 atf_error_t err; 409 char **argv; 410 411 err = atf_build_c_o(sfile, ofile, optargs, &argv); 412 if (atf_is_error(err)) 413 goto out; 414 415 err = check_build_run((const char *const *)argv, success); 416 417 atf_utils_free_charpp(argv); 418 out: 419 return err; 420 } 421 422 atf_error_t 423 atf_check_build_cpp(const char *sfile, 424 const char *ofile, 425 const char *const optargs[], 426 bool *success) 427 { 428 atf_error_t err; 429 char **argv; 430 431 err = atf_build_cpp(sfile, ofile, optargs, &argv); 432 if (atf_is_error(err)) 433 goto out; 434 435 err = check_build_run((const char *const *)argv, success); 436 437 atf_utils_free_charpp(argv); 438 out: 439 return err; 440 } 441 442 atf_error_t 443 atf_check_build_cxx_o(const char *sfile, 444 const char *ofile, 445 const char *const optargs[], 446 bool *success) 447 { 448 atf_error_t err; 449 char **argv; 450 451 err = atf_build_cxx_o(sfile, ofile, optargs, &argv); 452 if (atf_is_error(err)) 453 goto out; 454 455 err = check_build_run((const char *const *)argv, success); 456 457 atf_utils_free_charpp(argv); 458 out: 459 return err; 460 } 461 462 atf_error_t 463 atf_check_exec_array(const char *const *argv, atf_check_result_t *r) 464 { 465 atf_error_t err; 466 atf_fs_path_t dir; 467 468 err = create_tmpdir(&dir); 469 if (atf_is_error(err)) 470 goto out; 471 472 err = atf_check_result_init(r, argv, &dir); 473 if (atf_is_error(err)) { 474 atf_error_t err2 = atf_fs_rmdir(&dir); 475 INV(!atf_is_error(err2)); 476 goto out; 477 } 478 479 err = fork_and_wait(argv, &r->pimpl->m_stdout, &r->pimpl->m_stderr, 480 &r->pimpl->m_status); 481 if (atf_is_error(err)) { 482 atf_check_result_fini(r); 483 goto out; 484 } 485 486 INV(!atf_is_error(err)); 487 488 atf_fs_path_fini(&dir); 489 out: 490 return err; 491 } 492