| 1 |
/* |
| 2 |
* Check: a unit test framework for C |
| 3 |
* Copyright (C) 2001, 2002 Arien Malec |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the |
| 17 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
| 18 |
* MA 02110-1301, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "libcompat/libcompat.h" |
| 22 |
|
| 23 |
#include <string.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <stdarg.h> |
| 27 |
#include <math.h> |
| 28 |
|
| 29 |
#include <glib.h> |
| 30 |
|
| 31 |
#include "internal-check.h" |
| 32 |
#include "check_error.h" |
| 33 |
#include "check_list.h" |
| 34 |
#include "check_impl.h" |
| 35 |
#include "check_msg.h" |
| 36 |
|
| 37 |
#ifdef HAVE_UNISTD_H |
| 38 |
#include <unistd.h> /* for _POSIX_VERSION */ |
| 39 |
#endif |
| 40 |
|
| 41 |
#ifndef DEFAULT_TIMEOUT |
| 42 |
#define DEFAULT_TIMEOUT 4 |
| 43 |
#endif |
| 44 |
|
| 45 |
/* |
| 46 |
* When a process exits either normally, with exit(), or |
| 47 |
* by an uncaught signal, The lower 0x377 bits are passed |
| 48 |
* to the parent. Of those, only the lower 8 bits are |
| 49 |
* returned by the WEXITSTATUS() macro. |
| 50 |
*/ |
| 51 |
#define WEXITSTATUS_MASK 0xFF |
| 52 |
|
| 53 |
int check_major_version = CHECK_MAJOR_VERSION; |
| 54 |
int check_minor_version = CHECK_MINOR_VERSION; |
| 55 |
int check_micro_version = CHECK_MICRO_VERSION; |
| 56 |
|
| 57 |
const char *current_test_name = NULL; |
| 58 |
|
| 59 |
static int non_pass (int val); |
| 60 |
static Fixture *fixture_create (SFun fun, int ischecked); |
| 61 |
static void tcase_add_fixture (TCase * tc, SFun setup, SFun teardown, |
| 62 |
int ischecked); |
| 63 |
static void tr_init (TestResult * tr); |
| 64 |
static void suite_free (Suite * s); |
| 65 |
static void tcase_free (TCase * tc); |
| 66 |
|
| 67 |
Suite * |
| 68 |
suite_create (const char *name) |
| 69 |
{ |
| 70 |
Suite *s; |
| 71 |
|
| 72 |
s = (Suite *) emalloc (sizeof (Suite)); /* freed in suite_free */ |
| 73 |
if (name == NULL) |
| 74 |
s->name = ""; |
| 75 |
else |
| 76 |
s->name = name; |
| 77 |
s->tclst = check_list_create (); |
| 78 |
return s; |
| 79 |
} |
| 80 |
|
| 81 |
int |
| 82 |
suite_tcase (Suite * s, const char *tcname) |
| 83 |
{ |
| 84 |
List *l; |
| 85 |
TCase *tc; |
| 86 |
|
| 87 |
if (s == NULL) |
| 88 |
return 0; |
| 89 |
|
| 90 |
l = s->tclst; |
| 91 |
for (check_list_front (l); !check_list_at_end (l); check_list_advance (l)) { |
| 92 |
tc = (TCase *) check_list_val (l); |
| 93 |
if (strcmp (tcname, tc->name) == 0) |
| 94 |
return 1; |
| 95 |
} |
| 96 |
|
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
static void |
| 101 |
suite_free (Suite * s) |
| 102 |
{ |
| 103 |
List *l; |
| 104 |
|
| 105 |
if (s == NULL) |
| 106 |
return; |
| 107 |
l = s->tclst; |
| 108 |
for (check_list_front (l); !check_list_at_end (l); check_list_advance (l)) { |
| 109 |
tcase_free ((TCase *) check_list_val (l)); |
| 110 |
} |
| 111 |
check_list_free (s->tclst); |
| 112 |
free (s); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
TCase * |
| 117 |
tcase_create (const char *name) |
| 118 |
{ |
| 119 |
char *env; |
| 120 |
double timeout_sec = DEFAULT_TIMEOUT; |
| 121 |
|
| 122 |
TCase *tc = (TCase *) emalloc (sizeof (TCase)); /*freed in tcase_free */ |
| 123 |
|
| 124 |
if (name == NULL) |
| 125 |
tc->name = ""; |
| 126 |
else |
| 127 |
tc->name = name; |
| 128 |
|
| 129 |
env = getenv ("CK_DEFAULT_TIMEOUT"); |
| 130 |
if (env != NULL) { |
| 131 |
char *endptr = NULL; |
| 132 |
double tmp = strtod (env, &endptr); |
| 133 |
|
| 134 |
if (tmp >= 0 && endptr != env && (*endptr) == '\0') { |
| 135 |
timeout_sec = tmp; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
env = getenv ("CK_TIMEOUT_MULTIPLIER"); |
| 140 |
if (env != NULL) { |
| 141 |
char *endptr = NULL; |
| 142 |
double tmp = strtod (env, &endptr); |
| 143 |
|
| 144 |
if (tmp >= 0 && endptr != env && (*endptr) == '\0') { |
| 145 |
timeout_sec = timeout_sec * tmp; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
tc->timeout.tv_sec = (time_t) floor (timeout_sec); |
| 150 |
tc->timeout.tv_nsec = |
| 151 |
(long) ((timeout_sec - floor (timeout_sec)) * (double) NANOS_PER_SECONDS); |
| 152 |
|
| 153 |
tc->tflst = check_list_create (); |
| 154 |
tc->unch_sflst = check_list_create (); |
| 155 |
tc->ch_sflst = check_list_create (); |
| 156 |
tc->unch_tflst = check_list_create (); |
| 157 |
tc->ch_tflst = check_list_create (); |
| 158 |
tc->tags = check_list_create (); |
| 159 |
|
| 160 |
return tc; |
| 161 |
} |
| 162 |
|
| 163 |
/* |
| 164 |
* Helper function to create a list of tags from |
| 165 |
* a space separated string. |
| 166 |
*/ |
| 167 |
List * |
| 168 |
tag_string_to_list (const char *tags_string) |
| 169 |
{ |
| 170 |
List *list; |
| 171 |
char *tags; |
| 172 |
char *tag; |
| 173 |
|
| 174 |
list = check_list_create (); |
| 175 |
|
| 176 |
if (NULL == tags_string) { |
| 177 |
return list; |
| 178 |
} |
| 179 |
|
| 180 |
tags = strdup (tags_string); |
| 181 |
tag = strtok (tags, " "); |
| 182 |
while (tag) { |
| 183 |
check_list_add_end (list, strdup (tag)); |
| 184 |
tag = strtok (NULL, " "); |
| 185 |
} |
| 186 |
free (tags); |
| 187 |
return list; |
| 188 |
} |
| 189 |
|
| 190 |
void |
| 191 |
tcase_set_tags (TCase * tc, const char *tags_orig) |
| 192 |
{ |
| 193 |
/* replace any pre-existing list */ |
| 194 |
if (tc->tags) { |
| 195 |
check_list_apply (tc->tags, free); |
| 196 |
check_list_free (tc->tags); |
| 197 |
} |
| 198 |
tc->tags = tag_string_to_list (tags_orig); |
| 199 |
} |
| 200 |
|
| 201 |
static void |
| 202 |
tcase_free (TCase * tc) |
| 203 |
{ |
| 204 |
check_list_apply (tc->tflst, free); |
| 205 |
check_list_apply (tc->unch_sflst, free); |
| 206 |
check_list_apply (tc->ch_sflst, free); |
| 207 |
check_list_apply (tc->unch_tflst, free); |
| 208 |
check_list_apply (tc->ch_tflst, free); |
| 209 |
check_list_apply (tc->tags, free); |
| 210 |
check_list_free (tc->tflst); |
| 211 |
check_list_free (tc->unch_sflst); |
| 212 |
check_list_free (tc->ch_sflst); |
| 213 |
check_list_free (tc->unch_tflst); |
| 214 |
check_list_free (tc->ch_tflst); |
| 215 |
check_list_free (tc->tags); |
| 216 |
free (tc); |
| 217 |
} |
| 218 |
|
| 219 |
unsigned int |
| 220 |
tcase_matching_tag (TCase * tc, List * check_for) |
| 221 |
{ |
| 222 |
|
| 223 |
if (NULL == check_for) { |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
for (check_list_front (check_for); !check_list_at_end (check_for); |
| 228 |
check_list_advance (check_for)) { |
| 229 |
for (check_list_front (tc->tags); !check_list_at_end (tc->tags); |
| 230 |
check_list_advance (tc->tags)) { |
| 231 |
if (0 == strcmp ((const char *) check_list_val (tc->tags), |
| 232 |
(const char *) check_list_val (check_for))) { |
| 233 |
return 1; |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
return 0; |
| 238 |
} |
| 239 |
|
| 240 |
void |
| 241 |
suite_add_tcase (Suite * s, TCase * tc) |
| 242 |
{ |
| 243 |
if (s == NULL || tc == NULL || check_list_contains (s->tclst, tc)) { |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
check_list_add_end (s->tclst, tc); |
| 248 |
} |
| 249 |
|
| 250 |
void |
| 251 |
_tcase_add_test (TCase * tc, TFun fn, const char *name, int _signal, |
| 252 |
int allowed_exit_value, int start, int end) |
| 253 |
{ |
| 254 |
TF *tf; |
| 255 |
|
| 256 |
if (tc == NULL || fn == NULL || name == NULL) |
| 257 |
return; |
| 258 |
tf = (TF *) emalloc (sizeof (TF)); /* freed in tcase_free */ |
| 259 |
tf->fn = fn; |
| 260 |
tf->loop_start = start; |
| 261 |
tf->loop_end = end; |
| 262 |
tf->signal = _signal; /* 0 means no signal expected */ |
| 263 |
tf->allowed_exit_value = (WEXITSTATUS_MASK & allowed_exit_value); /* 0 is default successful exit */ |
| 264 |
tf->name = name; |
| 265 |
check_list_add_end (tc->tflst, tf); |
| 266 |
} |
| 267 |
|
| 268 |
static Fixture * |
| 269 |
fixture_create (SFun fun, int ischecked) |
| 270 |
{ |
| 271 |
Fixture *f; |
| 272 |
|
| 273 |
f = (Fixture *) emalloc (sizeof (Fixture)); |
| 274 |
f->fun = fun; |
| 275 |
f->ischecked = ischecked; |
| 276 |
|
| 277 |
return f; |
| 278 |
} |
| 279 |
|
| 280 |
void |
| 281 |
tcase_add_unchecked_fixture (TCase * tc, SFun setup, SFun teardown) |
| 282 |
{ |
| 283 |
tcase_add_fixture (tc, setup, teardown, 0); |
| 284 |
} |
| 285 |
|
| 286 |
void |
| 287 |
tcase_add_checked_fixture (TCase * tc, SFun setup, SFun teardown) |
| 288 |
{ |
| 289 |
tcase_add_fixture (tc, setup, teardown, 1); |
| 290 |
} |
| 291 |
|
| 292 |
static void |
| 293 |
tcase_add_fixture (TCase * tc, SFun setup, SFun teardown, int ischecked) |
| 294 |
{ |
| 295 |
if (setup) { |
| 296 |
if (ischecked) |
| 297 |
check_list_add_end (tc->ch_sflst, fixture_create (setup, ischecked)); |
| 298 |
else |
| 299 |
check_list_add_end (tc->unch_sflst, fixture_create (setup, ischecked)); |
| 300 |
} |
| 301 |
|
| 302 |
/* Add teardowns at front so they are run in reverse order. */ |
| 303 |
if (teardown) { |
| 304 |
if (ischecked) |
| 305 |
check_list_add_front (tc->ch_tflst, fixture_create (teardown, ischecked)); |
| 306 |
else |
| 307 |
check_list_add_front (tc->unch_tflst, |
| 308 |
fixture_create (teardown, ischecked)); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
void |
| 313 |
tcase_set_timeout (TCase * tc, double timeout) |
| 314 |
{ |
| 315 |
#if defined(HAVE_FORK) |
| 316 |
if (timeout >= 0) { |
| 317 |
char *env = getenv ("CK_TIMEOUT_MULTIPLIER"); |
| 318 |
|
| 319 |
if (env != NULL) { |
| 320 |
char *endptr = NULL; |
| 321 |
double tmp = strtod (env, &endptr); |
| 322 |
|
| 323 |
if (tmp >= 0 && endptr != env && (*endptr) == '\0') { |
| 324 |
timeout = timeout * tmp; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
tc->timeout.tv_sec = (time_t) floor (timeout); |
| 329 |
tc->timeout.tv_nsec = |
| 330 |
(long) ((timeout - floor (timeout)) * (double) NANOS_PER_SECONDS); |
| 331 |
} |
| 332 |
#else |
| 333 |
(void) tc; |
| 334 |
(void) timeout; |
| 335 |
eprintf |
| 336 |
("This version does not support timeouts, as fork is not supported", |
| 337 |
__FILE__, __LINE__); |
| 338 |
/* Ignoring, as Check is not compiled with fork support. */ |
| 339 |
#endif /* HAVE_FORK */ |
| 340 |
} |
| 341 |
|
| 342 |
void |
| 343 |
tcase_fn_start (const char *fname, const char *file, int line) |
| 344 |
{ |
| 345 |
send_ctx_info (CK_CTX_TEST); |
| 346 |
send_loc_info (file, line); |
| 347 |
|
| 348 |
current_test_name = fname; |
| 349 |
} |
| 350 |
|
| 351 |
const char * |
| 352 |
tcase_name () |
| 353 |
{ |
| 354 |
return current_test_name; |
| 355 |
} |
| 356 |
|
| 357 |
void |
| 358 |
_mark_point (const char *file, int line) |
| 359 |
{ |
| 360 |
send_loc_info (file, line); |
| 361 |
} |
| 362 |
|
| 363 |
void |
| 364 |
_ck_assert_failed (const char *file, int line, const char *expr, ...) |
| 365 |
{ |
| 366 |
const char *msg; |
| 367 |
va_list ap; |
| 368 |
char buf[BUFSIZ]; |
| 369 |
const char *to_send; |
| 370 |
|
| 371 |
send_loc_info (file, line); |
| 372 |
|
| 373 |
va_start (ap, expr); |
| 374 |
msg = (const char *) va_arg (ap, char *); |
| 375 |
|
| 376 |
/* |
| 377 |
* If a message was passed, format it with vsnprintf. |
| 378 |
* Otherwise, print the expression as is. |
| 379 |
*/ |
| 380 |
if (msg != NULL) { |
| 381 |
vsnprintf (buf, BUFSIZ, msg, ap); |
| 382 |
to_send = buf; |
| 383 |
} else { |
| 384 |
to_send = expr; |
| 385 |
} |
| 386 |
|
| 387 |
va_end (ap); |
| 388 |
send_failure_info (to_send); |
| 389 |
#if defined(HAVE_FORK) && HAVE_FORK==1 |
| 390 |
if (cur_fork_status () == CK_FORK) { |
| 391 |
g_thread_pool_stop_unused_threads (); |
| 392 |
_exit (1); |
| 393 |
} |
| 394 |
#endif /* HAVE_FORK */ |
| 395 |
longjmp (error_jmp_buffer, 1); |
| 396 |
} |
| 397 |
|
| 398 |
SRunner * |
| 399 |
srunner_create (Suite * s) |
| 400 |
{ |
| 401 |
SRunner *sr = (SRunner *) emalloc (sizeof (SRunner)); /* freed in srunner_free */ |
| 402 |
|
| 403 |
sr->slst = check_list_create (); |
| 404 |
if (s != NULL) |
| 405 |
check_list_add_end (sr->slst, s); |
| 406 |
sr->stats = (TestStats *) emalloc (sizeof (TestStats)); /* freed in srunner_free */ |
| 407 |
sr->stats->n_checked = sr->stats->n_failed = sr->stats->n_errors = 0; |
| 408 |
sr->resultlst = check_list_create (); |
| 409 |
sr->log_fname = NULL; |
| 410 |
sr->xml_fname = NULL; |
| 411 |
sr->tap_fname = NULL; |
| 412 |
sr->loglst = NULL; |
| 413 |
|
| 414 |
#if defined(HAVE_FORK) |
| 415 |
sr->fstat = CK_FORK_GETENV; |
| 416 |
#else |
| 417 |
/* |
| 418 |
* Overriding the default of running tests in fork mode, |
| 419 |
* as this system does not have fork() |
| 420 |
*/ |
| 421 |
sr->fstat = CK_NOFORK; |
| 422 |
#endif /* HAVE_FORK */ |
| 423 |
|
| 424 |
return sr; |
| 425 |
} |
| 426 |
|
| 427 |
void |
| 428 |
srunner_add_suite (SRunner * sr, Suite * s) |
| 429 |
{ |
| 430 |
if (s == NULL) |
| 431 |
return; |
| 432 |
|
| 433 |
check_list_add_end (sr->slst, s); |
| 434 |
} |
| 435 |
|
| 436 |
void |
| 437 |
srunner_free (SRunner * sr) |
| 438 |
{ |
| 439 |
List *l; |
| 440 |
TestResult *tr; |
| 441 |
|
| 442 |
if (sr == NULL) |
| 443 |
return; |
| 444 |
|
| 445 |
free (sr->stats); |
| 446 |
l = sr->slst; |
| 447 |
for (check_list_front (l); !check_list_at_end (l); check_list_advance (l)) { |
| 448 |
suite_free ((Suite *) check_list_val (l)); |
| 449 |
} |
| 450 |
check_list_free (sr->slst); |
| 451 |
|
| 452 |
l = sr->resultlst; |
| 453 |
for (check_list_front (l); !check_list_at_end (l); check_list_advance (l)) { |
| 454 |
tr = (TestResult *) check_list_val (l); |
| 455 |
tr_free (tr); |
| 456 |
} |
| 457 |
check_list_free (sr->resultlst); |
| 458 |
|
| 459 |
free (sr); |
| 460 |
} |
| 461 |
|
| 462 |
int |
| 463 |
srunner_ntests_failed (SRunner * sr) |
| 464 |
{ |
| 465 |
return sr->stats->n_failed + sr->stats->n_errors; |
| 466 |
} |
| 467 |
|
| 468 |
int |
| 469 |
srunner_ntests_run (SRunner * sr) |
| 470 |
{ |
| 471 |
return sr->stats->n_checked; |
| 472 |
} |
| 473 |
|
| 474 |
TestResult ** |
| 475 |
srunner_failures (SRunner * sr) |
| 476 |
{ |
| 477 |
int i = 0; |
| 478 |
TestResult **trarray; |
| 479 |
List *rlst; |
| 480 |
|
| 481 |
trarray = |
| 482 |
(TestResult **) emalloc (sizeof (trarray[0]) * |
| 483 |
srunner_ntests_failed (sr)); |
| 484 |
|
| 485 |
rlst = sr->resultlst; |
| 486 |
for (check_list_front (rlst); !check_list_at_end (rlst); |
| 487 |
check_list_advance (rlst)) { |
| 488 |
TestResult *tr = (TestResult *) check_list_val (rlst); |
| 489 |
|
| 490 |
if (non_pass (tr->rtype)) |
| 491 |
trarray[i++] = tr; |
| 492 |
|
| 493 |
} |
| 494 |
return trarray; |
| 495 |
} |
| 496 |
|
| 497 |
TestResult ** |
| 498 |
srunner_results (SRunner * sr) |
| 499 |
{ |
| 500 |
int i = 0; |
| 501 |
TestResult **trarray; |
| 502 |
List *rlst; |
| 503 |
|
| 504 |
trarray = |
| 505 |
(TestResult **) emalloc (sizeof (trarray[0]) * srunner_ntests_run (sr)); |
| 506 |
|
| 507 |
rlst = sr->resultlst; |
| 508 |
for (check_list_front (rlst); !check_list_at_end (rlst); |
| 509 |
check_list_advance (rlst)) { |
| 510 |
trarray[i++] = (TestResult *) check_list_val (rlst); |
| 511 |
} |
| 512 |
return trarray; |
| 513 |
} |
| 514 |
|
| 515 |
static int |
| 516 |
non_pass (int val) |
| 517 |
{ |
| 518 |
return val != CK_PASS; |
| 519 |
} |
| 520 |
|
| 521 |
TestResult * |
| 522 |
tr_create (void) |
| 523 |
{ |
| 524 |
TestResult *tr; |
| 525 |
|
| 526 |
tr = (TestResult *) emalloc (sizeof (TestResult)); |
| 527 |
tr_init (tr); |
| 528 |
return tr; |
| 529 |
} |
| 530 |
|
| 531 |
static void |
| 532 |
tr_init (TestResult * tr) |
| 533 |
{ |
| 534 |
tr->ctx = CK_CTX_INVALID; |
| 535 |
tr->line = -1; |
| 536 |
tr->rtype = CK_TEST_RESULT_INVALID; |
| 537 |
tr->msg = NULL; |
| 538 |
tr->file = NULL; |
| 539 |
tr->tcname = NULL; |
| 540 |
tr->tname = NULL; |
| 541 |
tr->duration = -1; |
| 542 |
} |
| 543 |
|
| 544 |
void |
| 545 |
tr_free (TestResult * tr) |
| 546 |
{ |
| 547 |
free (tr->file); |
| 548 |
free (tr->msg); |
| 549 |
free (tr); |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
const char * |
| 554 |
tr_msg (TestResult * tr) |
| 555 |
{ |
| 556 |
return tr->msg; |
| 557 |
} |
| 558 |
|
| 559 |
int |
| 560 |
tr_lno (TestResult * tr) |
| 561 |
{ |
| 562 |
return tr->line; |
| 563 |
} |
| 564 |
|
| 565 |
const char * |
| 566 |
tr_lfile (TestResult * tr) |
| 567 |
{ |
| 568 |
return tr->file; |
| 569 |
} |
| 570 |
|
| 571 |
int |
| 572 |
tr_rtype (TestResult * tr) |
| 573 |
{ |
| 574 |
return tr->rtype; |
| 575 |
} |
| 576 |
|
| 577 |
enum ck_result_ctx |
| 578 |
tr_ctx (TestResult * tr) |
| 579 |
{ |
| 580 |
return tr->ctx; |
| 581 |
} |
| 582 |
|
| 583 |
const char * |
| 584 |
tr_tcname (TestResult * tr) |
| 585 |
{ |
| 586 |
return tr->tcname; |
| 587 |
} |
| 588 |
|
| 589 |
static enum fork_status _fstat = CK_FORK; |
| 590 |
|
| 591 |
void |
| 592 |
set_fork_status (enum fork_status fstat) |
| 593 |
{ |
| 594 |
if (fstat == CK_FORK || fstat == CK_NOFORK || fstat == CK_FORK_GETENV) |
| 595 |
_fstat = fstat; |
| 596 |
else |
| 597 |
eprintf ("Bad status in set_fork_status", __FILE__, __LINE__); |
| 598 |
} |
| 599 |
|
| 600 |
enum fork_status |
| 601 |
cur_fork_status (void) |
| 602 |
{ |
| 603 |
return _fstat; |
| 604 |
} |
| 605 |
|
| 606 |
/** |
| 607 |
* Not all systems support the same clockid_t's. This call checks |
| 608 |
* if the CLOCK_MONOTONIC clockid_t is valid. If so, that is returned, |
| 609 |
* otherwise, CLOCK_REALTIME is returned. |
| 610 |
* |
| 611 |
* The clockid_t that was found to work on the first call is |
| 612 |
* cached for subsequent calls. |
| 613 |
*/ |
| 614 |
clockid_t |
| 615 |
check_get_clockid () |
| 616 |
{ |
| 617 |
static clockid_t clockid = -1; |
| 618 |
|
| 619 |
if (clockid == -1) { |
| 620 |
/* |
| 621 |
* Only check if we have librt available. Otherwise, the clockid |
| 622 |
* will be ignored anyway, as the clock_gettime() and |
| 623 |
* timer_create() functions will be re-implemented in libcompat. |
| 624 |
* Worse, if librt and alarm() are unavailable, this check |
| 625 |
* will result in an assert(0). |
| 626 |
*/ |
| 627 |
#if defined(HAVE_POSIX_TIMERS) && defined(HAVE_MONOTONIC_CLOCK) |
| 628 |
timer_t timerid; |
| 629 |
|
| 630 |
if (timer_create (CLOCK_MONOTONIC, NULL, &timerid) == 0) { |
| 631 |
timer_delete (timerid); |
| 632 |
clockid = CLOCK_MONOTONIC; |
| 633 |
} else { |
| 634 |
clockid = CLOCK_REALTIME; |
| 635 |
} |
| 636 |
#else |
| 637 |
clockid = CLOCK_MONOTONIC; |
| 638 |
#endif |
| 639 |
} |
| 640 |
|
| 641 |
return clockid; |
| 642 |
} |