THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.6KB

  1. #include "cache.h"
  2. #include "config.h"
  3. #include "dir.h"
  4. #include "tr2_sysenv.h"
  5. /*
  6. * Each entry represents a trace2 setting.
  7. * See Documentation/technical/api-trace2.txt
  8. */
  9. struct tr2_sysenv_entry {
  10. const char *env_var_name;
  11. const char *git_config_name;
  12. char *value;
  13. unsigned int getenv_called : 1;
  14. };
  15. /*
  16. * This table must match "enum tr2_sysenv_variable" in tr2_sysenv.h.
  17. *
  18. * The strings in this table are constant and must match the published
  19. * config and environment variable names as described in the documentation.
  20. *
  21. * We do not define entries for the GIT_TRACE2_PARENT_* environment
  22. * variables because they are transient and used to pass information
  23. * from parent to child git processes, rather than settings.
  24. */
  25. /* clang-format off */
  26. static struct tr2_sysenv_entry tr2_sysenv_settings[] = {
  27. [TR2_SYSENV_CFG_PARAM] = { "GIT_TRACE2_CONFIG_PARAMS",
  28. "trace2.configparams" },
  29. [TR2_SYSENV_DST_DEBUG] = { "GIT_TRACE2_DST_DEBUG",
  30. "trace2.destinationdebug" },
  31. [TR2_SYSENV_NORMAL] = { "GIT_TRACE2",
  32. "trace2.normaltarget" },
  33. [TR2_SYSENV_NORMAL_BRIEF] = { "GIT_TRACE2_BRIEF",
  34. "trace2.normalbrief" },
  35. [TR2_SYSENV_EVENT] = { "GIT_TRACE2_EVENT",
  36. "trace2.eventtarget" },
  37. [TR2_SYSENV_EVENT_BRIEF] = { "GIT_TRACE2_EVENT_BRIEF",
  38. "trace2.eventbrief" },
  39. [TR2_SYSENV_EVENT_NESTING] = { "GIT_TRACE2_EVENT_NESTING",
  40. "trace2.eventnesting" },
  41. [TR2_SYSENV_PERF] = { "GIT_TRACE2_PERF",
  42. "trace2.perftarget" },
  43. [TR2_SYSENV_PERF_BRIEF] = { "GIT_TRACE2_PERF_BRIEF",
  44. "trace2.perfbrief" },
  45. [TR2_SYSENV_MAX_FILES] = { "GIT_TRACE2_MAX_FILES",
  46. "trace2.maxfiles" },
  47. };
  48. /* clang-format on */
  49. static int tr2_sysenv_cb(const char *key, const char *value, void *d)
  50. {
  51. int k;
  52. if (!starts_with(key, "trace2."))
  53. return 0;
  54. for (k = 0; k < ARRAY_SIZE(tr2_sysenv_settings); k++) {
  55. if (!strcmp(key, tr2_sysenv_settings[k].git_config_name)) {
  56. free(tr2_sysenv_settings[k].value);
  57. tr2_sysenv_settings[k].value = xstrdup(value);
  58. return 0;
  59. }
  60. }
  61. return 0;
  62. }
  63. /*
  64. * Load Trace2 settings from the system config (usually "/etc/gitconfig"
  65. * unless we were built with a runtime-prefix). These are intended to
  66. * define the default values for Trace2 as requested by the administrator.
  67. *
  68. * Then override with the Trace2 settings from the global config.
  69. */
  70. void tr2_sysenv_load(void)
  71. {
  72. if (ARRAY_SIZE(tr2_sysenv_settings) != TR2_SYSENV_MUST_BE_LAST)
  73. BUG("tr2_sysenv_settings size is wrong");
  74. read_very_early_config(tr2_sysenv_cb, NULL);
  75. }
  76. /*
  77. * Return the value for the requested Trace2 setting from these sources:
  78. * the system config, the global config, and the environment.
  79. */
  80. const char *tr2_sysenv_get(enum tr2_sysenv_variable var)
  81. {
  82. if (var >= TR2_SYSENV_MUST_BE_LAST)
  83. BUG("tr2_sysenv_get invalid var '%d'", var);
  84. if (!tr2_sysenv_settings[var].getenv_called) {
  85. const char *v = getenv(tr2_sysenv_settings[var].env_var_name);
  86. if (v && *v) {
  87. free(tr2_sysenv_settings[var].value);
  88. tr2_sysenv_settings[var].value = xstrdup(v);
  89. }
  90. tr2_sysenv_settings[var].getenv_called = 1;
  91. }
  92. return tr2_sysenv_settings[var].value;
  93. }
  94. /*
  95. * Return a friendly name for this setting that is suitable for printing
  96. * in an error messages.
  97. */
  98. const char *tr2_sysenv_display_name(enum tr2_sysenv_variable var)
  99. {
  100. if (var >= TR2_SYSENV_MUST_BE_LAST)
  101. BUG("tr2_sysenv_get invalid var '%d'", var);
  102. return tr2_sysenv_settings[var].env_var_name;
  103. }
  104. void tr2_sysenv_release(void)
  105. {
  106. int k;
  107. for (k = 0; k < ARRAY_SIZE(tr2_sysenv_settings); k++)
  108. free(tr2_sysenv_settings[k].value);
  109. }