1 /* 2 * Automated Testing Framework (atf) 3 * 4 * Copyright (c) 2008 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 305 r->pimpl = malloc(sizeof(struct atf_check_result_impl)); 306 if (r->pimpl == NULL) 307 return atf_no_memory_error(); 308 309 err = array_to_list(argv, &r->pimpl->m_argv); 310 if (atf_is_error(err)) 311 goto out; 312 313 err = atf_fs_path_copy(&r->pimpl->m_dir, dir); 314 if (atf_is_error(err)) 315 goto err_argv; 316 317 err = atf_fs_path_init_fmt(&r->pimpl->m_stdout, "%s/stdout", 318 atf_fs_path_cstring(dir)); 319 if (atf_is_error(err)) 320 goto err_dir; 321 322 err = atf_fs_path_init_fmt(&r->pimpl->m_stderr, "%s/stderr", 323 atf_fs_path_cstring(dir)); 324 if (atf_is_error(err)) 325 goto err_stdout; 326 327 INV(!atf_is_error(err)); 328 goto out; 329 330 err_stdout: 331 atf_fs_path_fini(&r->pimpl->m_stdout); 332 err_dir: 333 atf_fs_path_fini(&r->pimpl->m_dir); 334 err_argv: 335 atf_list_fini(&r->pimpl->m_argv); 336 out: 337 return err; 338 } 339 340 void 341 atf_check_result_fini(atf_check_result_t *r) 342 { 343 atf_process_status_fini(&r->pimpl->m_status); 344 345 cleanup_tmpdir(&r->pimpl->m_dir, &r->pimpl->m_stdout, 346 &r->pimpl->m_stderr); 347 atf_fs_path_fini(&r->pimpl->m_stdout); 348 atf_fs_path_fini(&r->pimpl->m_stderr); 349 atf_fs_path_fini(&r->pimpl->m_dir); 350 351 atf_list_fini(&r->pimpl->m_argv); 352 353 free(r->pimpl); 354 } 355 356 const char * 357 atf_check_result_stdout(const atf_check_result_t *r) 358 { 359 return atf_fs_path_cstring(&r->pimpl->m_stdout); 360 } 361 362 const char * 363 atf_check_result_stderr(const atf_check_result_t *r) 364 { 365 return atf_fs_path_cstring(&r->pimpl->m_stderr); 366 } 367 368 bool 369 atf_check_result_exited(const atf_check_result_t *r) 370 { 371 return atf_process_status_exited(&r->pimpl->m_status); 372 } 373 374 int 375 atf_check_result_exitcode(const atf_check_result_t *r) 376 { 377 return atf_process_status_exitstatus(&r->pimpl->m_status); 378 } 379 380 bool 381 atf_check_result_signaled(const atf_check_result_t *r) 382 { 383 return atf_process_status_signaled(&r->pimpl->m_status); 384 } 385 386 int 387 atf_check_result_termsig(const atf_check_result_t *r) 388 { 389 return atf_process_status_termsig(&r->pimpl->m_status); 390 } 391 392 /* --------------------------------------------------------------------- 393 * Free functions. 394 * --------------------------------------------------------------------- */ 395 396 /* XXX: This function shouldn't be in this module. It messes with stdout 397 * and stderr, and it provides a very high-end interface. This belongs, 398 * probably, somewhere related to test cases (such as in the tc module). */ 399 atf_error_t 400 atf_check_build_c_o(const char *sfile, 401 const char *ofile, 402 const char *const optargs[], 403 bool *success) 404 { 405 atf_error_t err; 406 char **argv; 407 408 err = atf_build_c_o(sfile, ofile, optargs, &argv); 409 if (atf_is_error(err)) 410 goto out; 411 412 err = check_build_run((const char *const *)argv, success); 413 414 atf_utils_free_charpp(argv); 415 out: 416 return err; 417 } 418 419 atf_error_t 420 atf_check_build_cpp(const char *sfile, 421 const char *ofile, 422 const char *const optargs[], 423 bool *success) 424 { 425 atf_error_t err; 426 char **argv; 427 428 err = atf_build_cpp(sfile, ofile, optargs, &argv); 429 if (atf_is_error(err)) 430 goto out; 431 432 err = check_build_run((const char *const *)argv, success); 433 434 atf_utils_free_charpp(argv); 435 out: 436 return err; 437 } 438 439 atf_error_t 440 atf_check_build_cxx_o(const char *sfile, 441 const char *ofile, 442 const char *const optargs[], 443 bool *success) 444 { 445 atf_error_t err; 446 char **argv; 447 448 err = atf_build_cxx_o(sfile, ofile, optargs, &argv); 449 if (atf_is_error(err)) 450 goto out; 451 452 err = check_build_run((const char *const *)argv, success); 453 454 atf_utils_free_charpp(argv); 455 out: 456 return err; 457 } 458 459 atf_error_t 460 atf_check_exec_array(const char *const *argv, atf_check_result_t *r) 461 { 462 atf_error_t err; 463 atf_fs_path_t dir; 464 465 err = create_tmpdir(&dir); 466 if (atf_is_error(err)) 467 goto out; 468 469 err = atf_check_result_init(r, argv, &dir); 470 if (atf_is_error(err)) { 471 atf_error_t err2 = atf_fs_rmdir(&dir); 472 INV(!atf_is_error(err2)); 473 goto out; 474 } 475 476 err = fork_and_wait(argv, &r->pimpl->m_stdout, &r->pimpl->m_stderr, 477 &r->pimpl->m_status); 478 if (atf_is_error(err)) { 479 atf_check_result_fini(r); 480 goto out; 481 } 482 483 INV(!atf_is_error(err)); 484 485 atf_fs_path_fini(&dir); 486 out: 487 return err; 488 } 489