load_unload.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. /// @file This file defines the load and unload hooks library functions.
  15. #include <hooks/hooks.h>
  16. #include <user_registry.h>
  17. #include <user_file.h>
  18. #include <iostream>
  19. #include <fstream>
  20. #include <errno.h>
  21. using namespace isc::hooks;
  22. /// @brief Pointer to the registry instance.
  23. UserRegistryPtr user_registry;
  24. /// @brief Output filestream for recording user check outcomes.
  25. std::fstream user_chk_output;
  26. /// @brief User registry input file name.
  27. /// @todo Hard-coded for now, this should be configurable.
  28. const char* registry_fname = "/tmp/user_chk_registry.txt";
  29. /// @brief User check outcome file name.
  30. /// @todo Hard-coded for now, this should be configurable.
  31. const char* user_chk_output_fname = "/tmp/user_chk_outcome.txt";
  32. // Functions accessed by the hooks framework use C linkage to avoid the name
  33. // mangling that accompanies use of the C++ compiler as well as to avoid
  34. // issues related to namespaces.
  35. extern "C" {
  36. /// @brief Called by the Hooks library manager when the library is loaded.
  37. ///
  38. /// Instantiates the UserRegistry and opens the outcome file. Failure in
  39. /// either results in a failed return code.
  40. ///
  41. /// @param unused library handle parameter required by Hooks API.
  42. ///
  43. /// @return Returns 0 upon success, non-zero upon failure.
  44. int load(LibraryHandle&) {
  45. // non-zero indicates an error.
  46. int ret_val = 0;
  47. try {
  48. // Instantiate the registry.
  49. user_registry.reset(new UserRegistry());
  50. // Create the data source.
  51. UserDataSourcePtr user_file(new UserFile(registry_fname));
  52. // Set the registry's data source
  53. user_registry->setSource(user_file);
  54. // Do an initial load of the registry.
  55. user_registry->refresh();
  56. // Open up the output file for user_chk results.
  57. user_chk_output.open(user_chk_output_fname,
  58. std::fstream::out | std::fstream::app);
  59. if (!user_chk_output) {
  60. // Grab the system error message.
  61. const char* errmsg = strerror(errno);
  62. isc_throw(isc::Unexpected, "Cannot open output file: "
  63. << user_chk_output_fname
  64. << " reason: " << errmsg);
  65. }
  66. }
  67. catch (const std::exception& ex) {
  68. // Log the error and return failure.
  69. std::cout << "DHCP UserCheckHook could not be loaded: "
  70. << ex.what() << std::endl;
  71. ret_val = 1;
  72. }
  73. return (ret_val);
  74. }
  75. /// @brief Called by the Hooks library manager when the library is unloaded.
  76. ///
  77. /// Destroys the UserRegistry and closes the outcome file.
  78. ///
  79. /// @return Always returns 0.
  80. int unload() {
  81. try {
  82. user_registry.reset();
  83. if (user_chk_output.is_open()) {
  84. user_chk_output.close();
  85. }
  86. } catch (const std::exception& ex) {
  87. // On the off chance something goes awry, catch it and log it.
  88. // @todo Not sure if we should return a non-zero result or not.
  89. std::cout << "DHCP UserCheckHook could not be unloaded: "
  90. << ex.what() << std::endl;
  91. }
  92. return (0);
  93. }
  94. }