/source3/smbd/perfcount.c

https://github.com/theresahalloran/samba-tool-time-doc · C · 193 lines · 112 code · 41 blank · 40 comment · 19 complexity · 0365fd15b0293915d1a723cfb9819831 MD5 · raw file

  1. /*
  2. Unix SMB/Netbios implementation.
  3. Perfcounter initialization and support functions
  4. Copyright (C) Todd Stecher 2009
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "includes.h"
  17. #include "smbd/smbd.h"
  18. static struct smb_perfcount_handlers *g_smb_perfcount_handlers = NULL;
  19. struct smb_perfcount_module {
  20. char *name;
  21. struct smb_perfcount_handlers *handlers;
  22. struct smb_perfcount_module *prev, *next;
  23. };
  24. struct smb_perfcount_module *modules = NULL;
  25. /*
  26. * a module is registered before it is actually loaded - keep a list
  27. *
  28. * @todo - currently perfcount modules are globally configured, so
  29. * building the list is not strictly required.
  30. * However, its a proven concept in VFS, and is here to allow a
  31. * move to eventual per-service perfcount configuration.
  32. *
  33. * Note many pre-connection statistics are interesting
  34. * (e.g. before binding to an individual share).
  35. *
  36. */
  37. static struct smb_perfcount_module *smb_perfcount_find_module(const char *name)
  38. {
  39. struct smb_perfcount_module *entry = modules;
  40. while (entry) {
  41. if (strcmp(entry->name, name)==0)
  42. return entry;
  43. entry = entry->next;
  44. }
  45. return NULL;
  46. }
  47. NTSTATUS smb_register_perfcounter(int interface_version, const char *name,
  48. const struct smb_perfcount_handlers *handlers)
  49. {
  50. struct smb_perfcount_module *entry = modules;
  51. if (interface_version != SMB_PERFCOUNTER_INTERFACE_VERSION) {
  52. DEBUG(0, ("Failed to register perfcount module.\n"
  53. "The module was compiled against "
  54. "SMB_PERFCOUNTER_INTERFACE_VERSION %d,\n"
  55. "current SMB_PERFCOUNTER_INTERFACE_VERSION is %d.\n"
  56. "Please recompile against the current Samba Version!\n",
  57. interface_version, SMB_PERFCOUNTER_INTERFACE_VERSION));
  58. return NT_STATUS_OBJECT_TYPE_MISMATCH;
  59. }
  60. if (!name || !name[0] || !handlers) {
  61. DEBUG(0,("smb_register_perfcounter() called with NULL pointer "
  62. "or empty name!\n"));
  63. return NT_STATUS_INVALID_PARAMETER;
  64. }
  65. if (smb_perfcount_find_module(name)) {
  66. DEBUG(3,("Perfcount Module %s already loaded!\n", name));
  67. return NT_STATUS_OK;
  68. }
  69. entry = SMB_XMALLOC_P(struct smb_perfcount_module);
  70. entry->name = smb_xstrdup(name);
  71. entry->handlers = discard_const_p(struct smb_perfcount_handlers, handlers);
  72. DLIST_ADD(modules, entry);
  73. DEBUG(3, ("Successfully added perfcounter module '%s'\n", name));
  74. return NT_STATUS_OK;
  75. }
  76. /****************************************************************************
  77. initialise smb perf counters
  78. ****************************************************************************/
  79. static bool smb_load_perfcount_module(const char *name)
  80. {
  81. char *module_path = NULL;
  82. char *module_name = NULL;
  83. char *module_param = NULL, *p;
  84. const struct smb_perfcount_module *entry;
  85. DEBUG(3, ("Initialising perfcounter module [%s]\n", name));
  86. if (g_smb_perfcount_handlers) {
  87. DEBUG(3,("Only 1 perfcount handler may be registered in "
  88. "smb.conf\n"));
  89. return true;
  90. }
  91. module_path = smb_xstrdup(name);
  92. p = strchr_m(module_path, ':');
  93. if (p) {
  94. *p = 0;
  95. module_param = p+1;
  96. trim_char(module_param, ' ', ' ');
  97. }
  98. trim_char(module_path, ' ', ' ');
  99. module_name = smb_xstrdup(module_path);
  100. if (module_name[0] == '/') {
  101. /*
  102. * Extract the module name from the path. Just use the base
  103. * name of the last path component.
  104. */
  105. SAFE_FREE(module_name);
  106. module_name = smb_xstrdup(strrchr_m(module_path, '/')+1);
  107. p = strchr_m(module_name, '.');
  108. if (p != NULL) {
  109. *p = '\0';
  110. }
  111. }
  112. /* load the perfcounter module */
  113. if((entry = smb_perfcount_find_module(module_name)) ||
  114. (NT_STATUS_IS_OK(smb_probe_module("perfcount", module_path)) &&
  115. (entry = smb_perfcount_find_module(module_name)))) {
  116. DEBUG(3,("Successfully loaded perfcounter module [%s] \n", name));
  117. } else {
  118. DEBUG(0,("Can't find a perfcounter module [%s]\n",name));
  119. goto fail;
  120. }
  121. g_smb_perfcount_handlers = entry->handlers;
  122. SAFE_FREE(module_path);
  123. SAFE_FREE(module_name);
  124. return True;
  125. fail:
  126. SAFE_FREE(module_path);
  127. SAFE_FREE(module_name);
  128. return False;
  129. }
  130. void smb_init_perfcount_data(struct smb_perfcount_data *pcd)
  131. {
  132. ZERO_STRUCTP(pcd);
  133. pcd->handlers = g_smb_perfcount_handlers;
  134. }
  135. bool smb_perfcount_init(void)
  136. {
  137. char *perfcount_object;
  138. perfcount_object = lp_perfcount_module();
  139. /* don't init */
  140. if (!perfcount_object || !perfcount_object[0])
  141. return True;
  142. if (!smb_load_perfcount_module(perfcount_object)) {
  143. DEBUG(0, ("smbd_load_percount_module failed for %s\n",
  144. perfcount_object));
  145. return False;
  146. }
  147. return True;
  148. }