/contrib/bind9/lib/dns/lib.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 168 lines · 96 code · 36 blank · 36 comment · 18 complexity · 39e0a07d10d3836282473846086fc7c0 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2001 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: lib.c,v 1.19 2009/09/03 00:12:23 each Exp $ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <stddef.h>
  21. #include <isc/hash.h>
  22. #include <isc/mem.h>
  23. #include <isc/msgcat.h>
  24. #include <isc/mutex.h>
  25. #include <isc/once.h>
  26. #include <isc/util.h>
  27. #include <dns/db.h>
  28. #include <dns/ecdb.h>
  29. #include <dns/lib.h>
  30. #include <dns/result.h>
  31. #include <dst/dst.h>
  32. /***
  33. *** Globals
  34. ***/
  35. LIBDNS_EXTERNAL_DATA unsigned int dns_pps = 0U;
  36. LIBDNS_EXTERNAL_DATA isc_msgcat_t * dns_msgcat = NULL;
  37. /***
  38. *** Private
  39. ***/
  40. static isc_once_t msgcat_once = ISC_ONCE_INIT;
  41. /***
  42. *** Functions
  43. ***/
  44. static void
  45. open_msgcat(void) {
  46. isc_msgcat_open("libdns.cat", &dns_msgcat);
  47. }
  48. void
  49. dns_lib_initmsgcat(void) {
  50. /*
  51. * Initialize the DNS library's message catalog, dns_msgcat, if it
  52. * has not already been initialized.
  53. */
  54. RUNTIME_CHECK(isc_once_do(&msgcat_once, open_msgcat) == ISC_R_SUCCESS);
  55. }
  56. static isc_once_t init_once = ISC_ONCE_INIT;
  57. static isc_mem_t *dns_g_mctx = NULL;
  58. #ifndef BIND9
  59. static dns_dbimplementation_t *dbimp = NULL;
  60. #endif
  61. static isc_boolean_t initialize_done = ISC_FALSE;
  62. static isc_mutex_t reflock;
  63. static unsigned int references = 0;
  64. static void
  65. initialize(void) {
  66. isc_result_t result;
  67. REQUIRE(initialize_done == ISC_FALSE);
  68. result = isc_mem_create(0, 0, &dns_g_mctx);
  69. if (result != ISC_R_SUCCESS)
  70. return;
  71. dns_result_register();
  72. #ifndef BIND9
  73. result = dns_ecdb_register(dns_g_mctx, &dbimp);
  74. if (result != ISC_R_SUCCESS)
  75. goto cleanup_mctx;
  76. #endif
  77. result = isc_hash_create(dns_g_mctx, NULL, DNS_NAME_MAXWIRE);
  78. if (result != ISC_R_SUCCESS)
  79. goto cleanup_db;
  80. result = dst_lib_init(dns_g_mctx, NULL, 0);
  81. if (result != ISC_R_SUCCESS)
  82. goto cleanup_hash;
  83. result = isc_mutex_init(&reflock);
  84. if (result != ISC_R_SUCCESS)
  85. goto cleanup_dst;
  86. initialize_done = ISC_TRUE;
  87. return;
  88. cleanup_dst:
  89. dst_lib_destroy();
  90. cleanup_hash:
  91. isc_hash_destroy();
  92. cleanup_db:
  93. #ifndef BIND9
  94. dns_ecdb_unregister(&dbimp);
  95. cleanup_mctx:
  96. #endif
  97. isc_mem_detach(&dns_g_mctx);
  98. }
  99. isc_result_t
  100. dns_lib_init(void) {
  101. isc_result_t result;
  102. /*
  103. * Since this routine is expected to be used by a normal application,
  104. * it should be better to return an error, instead of an emergency
  105. * abort, on any failure.
  106. */
  107. result = isc_once_do(&init_once, initialize);
  108. if (result != ISC_R_SUCCESS)
  109. return (result);
  110. if (!initialize_done)
  111. return (ISC_R_FAILURE);
  112. LOCK(&reflock);
  113. references++;
  114. UNLOCK(&reflock);
  115. return (ISC_R_SUCCESS);
  116. }
  117. void
  118. dns_lib_shutdown(void) {
  119. isc_boolean_t cleanup_ok = ISC_FALSE;
  120. LOCK(&reflock);
  121. if (--references == 0)
  122. cleanup_ok = ISC_TRUE;
  123. UNLOCK(&reflock);
  124. if (!cleanup_ok)
  125. return;
  126. dst_lib_destroy();
  127. isc_hash_destroy();
  128. #ifndef BIND9
  129. dns_ecdb_unregister(&dbimp);
  130. #endif
  131. isc_mem_detach(&dns_g_mctx);
  132. }