ddb_script.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2007 Robert N. M. Watson
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <sys/types.h>
  29. #include <sys/sysctl.h>
  30. #include <err.h>
  31. #include <errno.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <sysexits.h>
  36. #include "ddb.h"
  37. /*
  38. * These commands manage DDB(4) scripts from user space. For better or worse,
  39. * the setting and unsetting of scripts is only poorly represented using
  40. * sysctl(8), and this interface provides a more user-friendly way to
  41. * accomplish this management, wrapped around lower-level sysctls. For
  42. * completeness, listing of scripts is also included.
  43. */
  44. #define SYSCTL_SCRIPT "debug.ddb.scripting.script"
  45. #define SYSCTL_SCRIPTS "debug.ddb.scripting.scripts"
  46. #define SYSCTL_UNSCRIPT "debug.ddb.scripting.unscript"
  47. /*
  48. * Print all scripts (scriptname==NULL) or a specific script.
  49. */
  50. static void
  51. ddb_list_scripts(const char *scriptname)
  52. {
  53. char *buffer, *line, *nextline;
  54. char *line_script, *line_scriptname;
  55. size_t buflen, len;
  56. int ret;
  57. repeat:
  58. if (sysctlbyname(SYSCTL_SCRIPTS, NULL, &buflen, NULL, 0) < 0)
  59. err(EX_OSERR, "sysctl: %s", SYSCTL_SCRIPTS);
  60. if (buflen == 0)
  61. return;
  62. buffer = malloc(buflen);
  63. if (buffer == NULL)
  64. err(EX_OSERR, "malloc");
  65. bzero(buffer, buflen);
  66. len = buflen;
  67. ret = sysctlbyname(SYSCTL_SCRIPTS, buffer, &len, NULL, 0);
  68. if (ret < 0 && errno != ENOMEM)
  69. err(EX_OSERR, "sysctl: %s", SYSCTL_SCRIPTS);
  70. if (ret < 0) {
  71. free(buffer);
  72. goto repeat;
  73. }
  74. /*
  75. * We nul'd the buffer before calling sysctl(), so at worst empty.
  76. *
  77. * If a specific script hasn't been requested, print it all.
  78. */
  79. if (scriptname == NULL) {
  80. printf("%s", buffer);
  81. free(buffer);
  82. return;
  83. }
  84. /*
  85. * If a specific script has been requested, we have to parse the
  86. * string to find it.
  87. */
  88. nextline = buffer;
  89. while ((line = strsep(&nextline, "\n")) != NULL) {
  90. line_script = line;
  91. line_scriptname = strsep(&line_script, "=");
  92. if (line_script == NULL)
  93. continue;
  94. if (strcmp(scriptname, line_scriptname) != 0)
  95. continue;
  96. printf("%s\n", line_script);
  97. break;
  98. }
  99. if (line == NULL) {
  100. errno = ENOENT;
  101. err(EX_DATAERR, "%s", scriptname);
  102. }
  103. free(buffer);
  104. }
  105. /*
  106. * "ddb script" can be used to either print or set a script.
  107. */
  108. void
  109. ddb_script(int argc, char *argv[])
  110. {
  111. if (argc != 2)
  112. usage();
  113. argv++;
  114. argc--;
  115. if (strchr(argv[0], '=') != 0) {
  116. if (sysctlbyname(SYSCTL_SCRIPT, NULL, NULL, argv[0],
  117. strlen(argv[0]) + 1) < 0)
  118. err(EX_OSERR, "sysctl: %s", SYSCTL_SCRIPTS);
  119. } else
  120. ddb_list_scripts(argv[0]);
  121. }
  122. void
  123. ddb_scripts(int argc, char *argv[] __unused)
  124. {
  125. if (argc != 1)
  126. usage();
  127. ddb_list_scripts(NULL);
  128. }
  129. void
  130. ddb_unscript(int argc, char *argv[])
  131. {
  132. int ret;
  133. if (argc != 2)
  134. usage();
  135. argv++;
  136. argc--;
  137. ret = sysctlbyname(SYSCTL_UNSCRIPT, NULL, NULL, argv[0],
  138. strlen(argv[0]) + 1);
  139. if (ret < 0 && errno == EINVAL) {
  140. errno = ENOENT;
  141. err(EX_DATAERR, "sysctl: %s", argv[0]);
  142. } else if (ret < 0)
  143. err(EX_OSERR, "sysctl: %s", SYSCTL_UNSCRIPT);
  144. }