jcs's openbsd hax
openbsd
at jcs 615 lines 20 kB view raw
1/* 2 * client.c 3 * libpkgconf consumer lifecycle management 4 * 5 * Copyright (c) 2016 pkgconf authors (see AUTHORS). 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * This software is provided 'as is' and without any warranty, express or 12 * implied. In no event shall the authors be liable for any damages arising 13 * from the use of this software. 14 */ 15 16#include <libpkgconf/config.h> 17#include <libpkgconf/stdinc.h> 18#include <libpkgconf/libpkgconf.h> 19 20/* 21 * !doc 22 * 23 * libpkgconf `client` module 24 * ========================== 25 * 26 * The libpkgconf `client` module implements the `pkgconf_client_t` "client" object. 27 * Client objects store all necessary state for libpkgconf allowing for multiple instances to run 28 * in parallel. 29 * 30 * Client objects are not thread safe, in other words, a client object should not be shared across 31 * thread boundaries. 32 */ 33 34static void 35trace_path_list(const pkgconf_client_t *client, const char *desc, pkgconf_list_t *list) 36{ 37 const pkgconf_node_t *n; 38 39 PKGCONF_TRACE(client, "%s:", desc); 40 PKGCONF_FOREACH_LIST_ENTRY(list->head, n) 41 { 42 const pkgconf_path_t *p = n->data; 43 44 PKGCONF_TRACE(client, " - '%s'", p->path); 45 } 46} 47 48/* 49 * !doc 50 * 51 * .. c:function:: void pkgconf_client_dir_list_build(pkgconf_client_t *client) 52 * 53 * Bootstraps the package search paths. If the ``PKGCONF_PKG_PKGF_ENV_ONLY`` `flag` is set on the client, 54 * then only the ``PKG_CONFIG_PATH`` environment variable will be used, otherwise both the 55 * ``PKG_CONFIG_PATH`` and ``PKG_CONFIG_LIBDIR`` environment variables will be used. 56 * 57 * :param pkgconf_client_t* client: The pkgconf client object to bootstrap. 58 * :return: nothing 59 */ 60void 61pkgconf_client_dir_list_build(pkgconf_client_t *client, const pkgconf_cross_personality_t *personality) 62{ 63 pkgconf_path_build_from_environ("PKG_CONFIG_PATH", NULL, &client->dir_list, true); 64 65 if (!(client->flags & PKGCONF_PKG_PKGF_ENV_ONLY)) 66 { 67 pkgconf_list_t dir_list = PKGCONF_LIST_INITIALIZER; 68 const pkgconf_list_t *prepend_list = &personality->dir_list; 69 70 if (getenv("PKG_CONFIG_LIBDIR") != NULL) 71 { 72 /* PKG_CONFIG_LIBDIR= should empty the search path entirely. */ 73 (void) pkgconf_path_build_from_environ("PKG_CONFIG_LIBDIR", NULL, &dir_list, true); 74 prepend_list = &dir_list; 75 } 76 77 pkgconf_path_copy_list(&client->dir_list, prepend_list); 78 pkgconf_path_free(&dir_list); 79 } 80} 81 82/* 83 * !doc 84 * 85 * .. c:function:: void pkgconf_client_init(pkgconf_client_t *client, pkgconf_error_handler_func_t error_handler, void *error_handler_data, const pkgconf_cross_personality_t *personality) 86 * 87 * Initialise a pkgconf client object. 88 * 89 * :param pkgconf_client_t* client: The client to initialise. 90 * :param pkgconf_error_handler_func_t error_handler: An optional error handler to use for logging errors. 91 * :param void* error_handler_data: user data passed to optional error handler 92 * :param pkgconf_cross_personality_t* personality: the cross-compile personality to use for defaults 93 * :return: nothing 94 */ 95void 96pkgconf_client_init(pkgconf_client_t *client, pkgconf_error_handler_func_t error_handler, void *error_handler_data, const pkgconf_cross_personality_t *personality) 97{ 98 client->error_handler_data = error_handler_data; 99 client->error_handler = error_handler; 100 client->auditf = NULL; 101 client->cache_table = NULL; 102 client->cache_count = 0; 103 104#ifndef PKGCONF_LITE 105 if (client->trace_handler == NULL) 106 pkgconf_client_set_trace_handler(client, NULL, NULL); 107#endif 108 109 pkgconf_client_set_error_handler(client, error_handler, error_handler_data); 110 pkgconf_client_set_warn_handler(client, NULL, NULL); 111 112 pkgconf_client_set_sysroot_dir(client, personality->sysroot_dir); 113 pkgconf_client_set_buildroot_dir(client, NULL); 114 pkgconf_client_set_prefix_varname(client, NULL); 115 116 if(getenv("PKG_CONFIG_SYSTEM_LIBRARY_PATH") == NULL) 117 pkgconf_path_copy_list(&client->filter_libdirs, &personality->filter_libdirs); 118 else 119 pkgconf_path_build_from_environ("PKG_CONFIG_SYSTEM_LIBRARY_PATH", NULL, &client->filter_libdirs, false); 120 121 if(getenv("PKG_CONFIG_SYSTEM_INCLUDE_PATH") == NULL) 122 pkgconf_path_copy_list(&client->filter_includedirs, &personality->filter_includedirs); 123 else 124 pkgconf_path_build_from_environ("PKG_CONFIG_SYSTEM_INCLUDE_PATH", NULL, &client->filter_includedirs, false); 125 126 /* GCC uses these environment variables to define system include paths, so we should check them. */ 127#ifdef __HAIKU__ 128 pkgconf_path_build_from_environ("BELIBRARIES", NULL, &client->filter_libdirs, false); 129#else 130 pkgconf_path_build_from_environ("LIBRARY_PATH", NULL, &client->filter_libdirs, false); 131#endif 132 pkgconf_path_build_from_environ("CPATH", NULL, &client->filter_includedirs, false); 133 pkgconf_path_build_from_environ("C_INCLUDE_PATH", NULL, &client->filter_includedirs, false); 134 pkgconf_path_build_from_environ("CPLUS_INCLUDE_PATH", NULL, &client->filter_includedirs, false); 135 pkgconf_path_build_from_environ("OBJC_INCLUDE_PATH", NULL, &client->filter_includedirs, false); 136 137#ifdef _WIN32 138 /* also use the path lists that MSVC uses on windows */ 139 pkgconf_path_build_from_environ("INCLUDE", NULL, &client->filter_includedirs, false); 140#endif 141 142 PKGCONF_TRACE(client, "initialized client @%p", client); 143 144 trace_path_list(client, "filtered library paths", &client->filter_libdirs); 145 trace_path_list(client, "filtered include paths", &client->filter_includedirs); 146} 147 148/* 149 * !doc 150 * 151 * .. c:function:: pkgconf_client_t* pkgconf_client_new(pkgconf_error_handler_func_t error_handler, void *error_handler_data, const pkgconf_cross_personality_t *personality) 152 * 153 * Allocate and initialise a pkgconf client object. 154 * 155 * :param pkgconf_error_handler_func_t error_handler: An optional error handler to use for logging errors. 156 * :param void* error_handler_data: user data passed to optional error handler 157 * :param pkgconf_cross_personality_t* personality: cross-compile personality to use 158 * :return: A pkgconf client object. 159 * :rtype: pkgconf_client_t* 160 */ 161pkgconf_client_t * 162pkgconf_client_new(pkgconf_error_handler_func_t error_handler, void *error_handler_data, const pkgconf_cross_personality_t *personality) 163{ 164 pkgconf_client_t *out = calloc(1, sizeof(pkgconf_client_t)); 165 pkgconf_client_init(out, error_handler, error_handler_data, personality); 166 return out; 167} 168 169/* 170 * !doc 171 * 172 * .. c:function:: void pkgconf_client_deinit(pkgconf_client_t *client) 173 * 174 * Release resources belonging to a pkgconf client object. 175 * 176 * :param pkgconf_client_t* client: The client to deinitialise. 177 * :return: nothing 178 */ 179void 180pkgconf_client_deinit(pkgconf_client_t *client) 181{ 182 PKGCONF_TRACE(client, "deinit @%p", client); 183 184 if (client->prefix_varname != NULL) 185 free(client->prefix_varname); 186 187 if (client->sysroot_dir != NULL) 188 free(client->sysroot_dir); 189 190 if (client->buildroot_dir != NULL) 191 free(client->buildroot_dir); 192 193 pkgconf_path_free(&client->filter_libdirs); 194 pkgconf_path_free(&client->filter_includedirs); 195 196 pkgconf_tuple_free_global(client); 197 pkgconf_path_free(&client->dir_list); 198 pkgconf_cache_free(client); 199} 200 201/* 202 * !doc 203 * 204 * .. c:function:: void pkgconf_client_free(pkgconf_client_t *client) 205 * 206 * Release resources belonging to a pkgconf client object and then free the client object itself. 207 * 208 * :param pkgconf_client_t* client: The client to deinitialise and free. 209 * :return: nothing 210 */ 211void 212pkgconf_client_free(pkgconf_client_t *client) 213{ 214 pkgconf_client_deinit(client); 215 free(client); 216} 217 218/* 219 * !doc 220 * 221 * .. c:function:: const char *pkgconf_client_get_sysroot_dir(const pkgconf_client_t *client) 222 * 223 * Retrieves the client's sysroot directory (if any). 224 * 225 * :param pkgconf_client_t* client: The client object being accessed. 226 * :return: A string containing the sysroot directory or NULL. 227 * :rtype: const char * 228 */ 229const char * 230pkgconf_client_get_sysroot_dir(const pkgconf_client_t *client) 231{ 232 return client->sysroot_dir; 233} 234 235/* 236 * !doc 237 * 238 * .. c:function:: void pkgconf_client_set_sysroot_dir(pkgconf_client_t *client, const char *sysroot_dir) 239 * 240 * Sets or clears the sysroot directory on a client object. Any previous sysroot directory setting is 241 * automatically released if one was previously set. 242 * 243 * Additionally, the global tuple ``$(pc_sysrootdir)`` is set as appropriate based on the new setting. 244 * 245 * :param pkgconf_client_t* client: The client object being modified. 246 * :param char* sysroot_dir: The sysroot directory to set or NULL to unset. 247 * :return: nothing 248 */ 249void 250pkgconf_client_set_sysroot_dir(pkgconf_client_t *client, const char *sysroot_dir) 251{ 252 if (client->sysroot_dir != NULL) 253 free(client->sysroot_dir); 254 255 client->sysroot_dir = sysroot_dir != NULL ? strdup(sysroot_dir) : NULL; 256 257 PKGCONF_TRACE(client, "set sysroot_dir to: %s", client->sysroot_dir != NULL ? client->sysroot_dir : "<default>"); 258 259 pkgconf_tuple_add_global(client, "pc_sysrootdir", client->sysroot_dir != NULL ? client->sysroot_dir : "/"); 260} 261 262/* 263 * !doc 264 * 265 * .. c:function:: const char *pkgconf_client_get_buildroot_dir(const pkgconf_client_t *client) 266 * 267 * Retrieves the client's buildroot directory (if any). 268 * 269 * :param pkgconf_client_t* client: The client object being accessed. 270 * :return: A string containing the buildroot directory or NULL. 271 * :rtype: const char * 272 */ 273const char * 274pkgconf_client_get_buildroot_dir(const pkgconf_client_t *client) 275{ 276 return client->buildroot_dir; 277} 278 279/* 280 * !doc 281 * 282 * .. c:function:: void pkgconf_client_set_buildroot_dir(pkgconf_client_t *client, const char *buildroot_dir) 283 * 284 * Sets or clears the buildroot directory on a client object. Any previous buildroot directory setting is 285 * automatically released if one was previously set. 286 * 287 * Additionally, the global tuple ``$(pc_top_builddir)`` is set as appropriate based on the new setting. 288 * 289 * :param pkgconf_client_t* client: The client object being modified. 290 * :param char* buildroot_dir: The buildroot directory to set or NULL to unset. 291 * :return: nothing 292 */ 293void 294pkgconf_client_set_buildroot_dir(pkgconf_client_t *client, const char *buildroot_dir) 295{ 296 if (client->buildroot_dir != NULL) 297 free(client->buildroot_dir); 298 299 client->buildroot_dir = buildroot_dir != NULL ? strdup(buildroot_dir) : NULL; 300 301 PKGCONF_TRACE(client, "set buildroot_dir to: %s", client->buildroot_dir != NULL ? client->buildroot_dir : "<default>"); 302 303 pkgconf_tuple_add_global(client, "pc_top_builddir", client->buildroot_dir != NULL ? client->buildroot_dir : "$(top_builddir)"); 304} 305 306/* 307 * !doc 308 * 309 * .. c:function:: bool pkgconf_error(const pkgconf_client_t *client, const char *format, ...) 310 * 311 * Report an error to a client-registered error handler. 312 * 313 * :param pkgconf_client_t* client: The pkgconf client object to report the error to. 314 * :param char* format: A printf-style format string to use for formatting the error message. 315 * :return: true if the error handler processed the message, else false. 316 * :rtype: bool 317 */ 318bool 319pkgconf_error(const pkgconf_client_t *client, const char *format, ...) 320{ 321 char errbuf[PKGCONF_BUFSIZE]; 322 va_list va; 323 324 va_start(va, format); 325 vsnprintf(errbuf, sizeof errbuf, format, va); 326 va_end(va); 327 328 return client->error_handler(errbuf, client, client->error_handler_data); 329} 330 331/* 332 * !doc 333 * 334 * .. c:function:: bool pkgconf_warn(const pkgconf_client_t *client, const char *format, ...) 335 * 336 * Report an error to a client-registered warn handler. 337 * 338 * :param pkgconf_client_t* client: The pkgconf client object to report the error to. 339 * :param char* format: A printf-style format string to use for formatting the warning message. 340 * :return: true if the warn handler processed the message, else false. 341 * :rtype: bool 342 */ 343bool 344pkgconf_warn(const pkgconf_client_t *client, const char *format, ...) 345{ 346 char errbuf[PKGCONF_BUFSIZE]; 347 va_list va; 348 349 va_start(va, format); 350 vsnprintf(errbuf, sizeof errbuf, format, va); 351 va_end(va); 352 353 return client->warn_handler(errbuf, client, client->warn_handler_data); 354} 355 356/* 357 * !doc 358 * 359 * .. c:function:: bool pkgconf_trace(const pkgconf_client_t *client, const char *filename, size_t len, const char *funcname, const char *format, ...) 360 * 361 * Report a message to a client-registered trace handler. 362 * 363 * :param pkgconf_client_t* client: The pkgconf client object to report the trace message to. 364 * :param char* filename: The file the function is in. 365 * :param size_t lineno: The line number currently being executed. 366 * :param char* funcname: The function name to use. 367 * :param char* format: A printf-style format string to use for formatting the trace message. 368 * :return: true if the trace handler processed the message, else false. 369 * :rtype: bool 370 */ 371bool 372pkgconf_trace(const pkgconf_client_t *client, const char *filename, size_t lineno, const char *funcname, const char *format, ...) 373{ 374 char errbuf[PKGCONF_BUFSIZE]; 375 size_t len; 376 va_list va; 377 378 if (client == NULL || client->trace_handler == NULL) 379 return false; 380 381 len = snprintf(errbuf, sizeof errbuf, "%s:" SIZE_FMT_SPECIFIER " [%s]: ", filename, lineno, funcname); 382 383 va_start(va, format); 384 vsnprintf(errbuf + len, sizeof(errbuf) - len, format, va); 385 va_end(va); 386 387 pkgconf_strlcat(errbuf, "\n", sizeof errbuf); 388 389 return client->trace_handler(errbuf, client, client->trace_handler_data); 390} 391 392/* 393 * !doc 394 * 395 * .. c:function:: bool pkgconf_default_error_handler(const char *msg, const pkgconf_client_t *client, const void *data) 396 * 397 * The default pkgconf error handler. 398 * 399 * :param char* msg: The error message to handle. 400 * :param pkgconf_client_t* client: The client object the error originated from. 401 * :param void* data: An opaque pointer to extra data associated with the client for error handling. 402 * :return: true (the function does nothing to process the message) 403 * :rtype: bool 404 */ 405bool 406pkgconf_default_error_handler(const char *msg, const pkgconf_client_t *client, void *data) 407{ 408 (void) msg; 409 (void) client; 410 (void) data; 411 412 return true; 413} 414 415/* 416 * !doc 417 * 418 * .. c:function:: unsigned int pkgconf_client_get_flags(const pkgconf_client_t *client) 419 * 420 * Retrieves resolver-specific flags associated with a client object. 421 * 422 * :param pkgconf_client_t* client: The client object to retrieve the resolver-specific flags from. 423 * :return: a bitfield of resolver-specific flags 424 * :rtype: uint 425 */ 426unsigned int 427pkgconf_client_get_flags(const pkgconf_client_t *client) 428{ 429 return client->flags; 430} 431 432/* 433 * !doc 434 * 435 * .. c:function:: void pkgconf_client_set_flags(pkgconf_client_t *client, unsigned int flags) 436 * 437 * Sets resolver-specific flags associated with a client object. 438 * 439 * :param pkgconf_client_t* client: The client object to set the resolver-specific flags on. 440 * :return: nothing 441 */ 442void 443pkgconf_client_set_flags(pkgconf_client_t *client, unsigned int flags) 444{ 445 client->flags = flags; 446} 447 448/* 449 * !doc 450 * 451 * .. c:function:: const char *pkgconf_client_get_prefix_varname(const pkgconf_client_t *client) 452 * 453 * Retrieves the name of the variable that should contain a module's prefix. 454 * In some cases, it is necessary to override this variable to allow proper path relocation. 455 * 456 * :param pkgconf_client_t* client: The client object to retrieve the prefix variable name from. 457 * :return: the prefix variable name as a string 458 * :rtype: const char * 459 */ 460const char * 461pkgconf_client_get_prefix_varname(const pkgconf_client_t *client) 462{ 463 return client->prefix_varname; 464} 465 466/* 467 * !doc 468 * 469 * .. c:function:: void pkgconf_client_set_prefix_varname(pkgconf_client_t *client, const char *prefix_varname) 470 * 471 * Sets the name of the variable that should contain a module's prefix. 472 * If the variable name is ``NULL``, then the default variable name (``prefix``) is used. 473 * 474 * :param pkgconf_client_t* client: The client object to set the prefix variable name on. 475 * :param char* prefix_varname: The prefix variable name to set. 476 * :return: nothing 477 */ 478void 479pkgconf_client_set_prefix_varname(pkgconf_client_t *client, const char *prefix_varname) 480{ 481 if (prefix_varname == NULL) 482 prefix_varname = "prefix"; 483 484 if (client->prefix_varname != NULL) 485 free(client->prefix_varname); 486 487 client->prefix_varname = strdup(prefix_varname); 488 489 PKGCONF_TRACE(client, "set prefix_varname to: %s", client->prefix_varname); 490} 491 492/* 493 * !doc 494 * 495 * .. c:function:: pkgconf_client_get_warn_handler(const pkgconf_client_t *client) 496 * 497 * Returns the warning handler if one is set, else ``NULL``. 498 * 499 * :param pkgconf_client_t* client: The client object to get the warn handler from. 500 * :return: a function pointer to the warn handler or ``NULL`` 501 */ 502pkgconf_error_handler_func_t 503pkgconf_client_get_warn_handler(const pkgconf_client_t *client) 504{ 505 return client->warn_handler; 506} 507 508/* 509 * !doc 510 * 511 * .. c:function:: pkgconf_client_set_warn_handler(pkgconf_client_t *client, pkgconf_error_handler_func_t warn_handler, void *warn_handler_data) 512 * 513 * Sets a warn handler on a client object or uninstalls one if set to ``NULL``. 514 * 515 * :param pkgconf_client_t* client: The client object to set the warn handler on. 516 * :param pkgconf_error_handler_func_t warn_handler: The warn handler to set. 517 * :param void* warn_handler_data: Optional data to associate with the warn handler. 518 * :return: nothing 519 */ 520void 521pkgconf_client_set_warn_handler(pkgconf_client_t *client, pkgconf_error_handler_func_t warn_handler, void *warn_handler_data) 522{ 523 client->warn_handler = warn_handler; 524 client->warn_handler_data = warn_handler_data; 525 526 if (client->warn_handler == NULL) 527 { 528 PKGCONF_TRACE(client, "installing default warn handler"); 529 client->warn_handler = pkgconf_default_error_handler; 530 } 531} 532 533/* 534 * !doc 535 * 536 * .. c:function:: pkgconf_client_get_error_handler(const pkgconf_client_t *client) 537 * 538 * Returns the error handler if one is set, else ``NULL``. 539 * 540 * :param pkgconf_client_t* client: The client object to get the error handler from. 541 * :return: a function pointer to the error handler or ``NULL`` 542 */ 543pkgconf_error_handler_func_t 544pkgconf_client_get_error_handler(const pkgconf_client_t *client) 545{ 546 return client->error_handler; 547} 548 549/* 550 * !doc 551 * 552 * .. c:function:: pkgconf_client_set_error_handler(pkgconf_client_t *client, pkgconf_error_handler_func_t error_handler, void *error_handler_data) 553 * 554 * Sets a warn handler on a client object or uninstalls one if set to ``NULL``. 555 * 556 * :param pkgconf_client_t* client: The client object to set the error handler on. 557 * :param pkgconf_error_handler_func_t error_handler: The error handler to set. 558 * :param void* error_handler_data: Optional data to associate with the error handler. 559 * :return: nothing 560 */ 561void 562pkgconf_client_set_error_handler(pkgconf_client_t *client, pkgconf_error_handler_func_t error_handler, void *error_handler_data) 563{ 564 client->error_handler = error_handler; 565 client->error_handler_data = error_handler_data; 566 567 if (client->error_handler == NULL) 568 { 569 PKGCONF_TRACE(client, "installing default error handler"); 570 client->error_handler = pkgconf_default_error_handler; 571 } 572} 573 574#ifndef PKGCONF_LITE 575/* 576 * !doc 577 * 578 * .. c:function:: pkgconf_client_get_trace_handler(const pkgconf_client_t *client) 579 * 580 * Returns the error handler if one is set, else ``NULL``. 581 * 582 * :param pkgconf_client_t* client: The client object to get the error handler from. 583 * :return: a function pointer to the error handler or ``NULL`` 584 */ 585pkgconf_error_handler_func_t 586pkgconf_client_get_trace_handler(const pkgconf_client_t *client) 587{ 588 return client->trace_handler; 589} 590 591/* 592 * !doc 593 * 594 * .. c:function:: pkgconf_client_set_trace_handler(pkgconf_client_t *client, pkgconf_error_handler_func_t trace_handler, void *trace_handler_data) 595 * 596 * Sets a warn handler on a client object or uninstalls one if set to ``NULL``. 597 * 598 * :param pkgconf_client_t* client: The client object to set the error handler on. 599 * :param pkgconf_error_handler_func_t trace_handler: The error handler to set. 600 * :param void* trace_handler_data: Optional data to associate with the error handler. 601 * :return: nothing 602 */ 603void 604pkgconf_client_set_trace_handler(pkgconf_client_t *client, pkgconf_error_handler_func_t trace_handler, void *trace_handler_data) 605{ 606 client->trace_handler = trace_handler; 607 client->trace_handler_data = trace_handler_data; 608 609 if (client->trace_handler == NULL) 610 { 611 client->trace_handler = pkgconf_default_error_handler; 612 PKGCONF_TRACE(client, "installing default trace handler"); 613 } 614} 615#endif