logger.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include <string.h>
  18. #include "daemon/daemon.h"
  19. #include "proc_title.h"
  20. #include "config/config.h"
  21. #include "gdb.h"
  22. #include "log/log.h"
  23. #include "daemons.h"
  24. #include "unit.h"
  25. #include "fault.h"
  26. #include "thread/thread.h"
  27. #define HOOKSO "../bin/faultlogger"
  28. /* 设置环境变量LD_PRELOAD */
  29. static void set_ld_preload()
  30. {
  31. if (access(HOOKSO, R_OK) == 0) {
  32. char *preload = canonicalize_file_name(HOOKSO);
  33. char *p = getenv("LD_PRELOAD");
  34. if (p == NULL) {
  35. setenv("LD_PRELOAD", preload, 1);
  36. } else {
  37. char *concat = NULL;
  38. int unused = 0;
  39. if (unused == 0)
  40. unused = asprintf(&concat, "%s:%s", p, preload);
  41. setenv("LD_PRELOAD", concat, 1);
  42. }
  43. }
  44. }
  45. /* 启动默认日志线程 */
  46. int start_fault_logger(WatchDog *watchdog)
  47. {
  48. /**
  49. * CarshProtect/CrashLog mode:
  50. * 0 -- disabled
  51. * 1 -- log-only
  52. * 2 -- protect
  53. * 3 -- screen
  54. * 4 -- xterm
  55. */
  56. /* default protect */
  57. int mode = 2;
  58. const char *display;
  59. display = g_dtc_config->get_str_val("cache", "FaultLoggerMode");
  60. if (display == NULL || !display[0]) {
  61. /* protect */
  62. mode = 0;
  63. } else if (!strcmp(display, "log")) {
  64. /* log */
  65. mode = 1;
  66. } else if (!strcmp(display, "dump")) {
  67. /* log */
  68. mode = 1;
  69. } else if (!strcmp(display, "protect")) {
  70. /* protect */
  71. mode = 2;
  72. } else if (!strcmp(display, "screen")) {
  73. /* screen */
  74. mode = 3;
  75. } else if (!strcmp(display, "xterm")) {
  76. if (getenv("DISPLAY")) {
  77. /* xterm */
  78. mode = 4;
  79. display = NULL;
  80. } else {
  81. log4cplus_warning(
  82. "FaultLoggerTarget set to \"xterm\", but no DISPLAY found");
  83. /* screen */
  84. mode = 2;
  85. }
  86. } else if (!strncmp(display, "xterm:", 6)) {
  87. mode = 4; // xterm
  88. display += 6;
  89. } else if (!strcasecmp(display, "disable")) {
  90. mode = 0;
  91. } else if (!strcasecmp(display, "disabled")) {
  92. mode = 0;
  93. } else {
  94. log4cplus_warning("unknown FaultLoggerMode \"%s\"", display);
  95. }
  96. log4cplus_info("FaultLoggerMode is %s\n",
  97. ((const char *[]){ "disable", "log", "protect", "screen",
  98. "xterm" })[mode]);
  99. if (mode == 0)
  100. return 0;
  101. int pid = fork();
  102. if (pid < 0)
  103. return -1;
  104. if (pid == 0) {
  105. set_proc_title("FaultLogger");
  106. Thread *loggerThread =
  107. new Thread("faultlogger", Thread::ThreadTypeProcess);
  108. loggerThread->initialize_thread();
  109. gdb_server(mode >= 3, display);
  110. exit(0);
  111. }
  112. char buf[20];
  113. snprintf(buf, sizeof(buf), "%d", pid);
  114. setenv(ENV_FAULT_LOGGER_PID, buf, 1);
  115. FaultHandler::initialize(mode >= 2);
  116. set_ld_preload();
  117. WatchDogObject *obj = new WatchDogObject(watchdog, "FaultLogger", pid);
  118. obj->attach_watch_dog();
  119. return 0;
  120. }