utils.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "utils.h"
  2. #include <util/generic/yexception.h>
  3. #include <util/string/subst.h>
  4. #include <google/protobuf/text_format.h>
  5. #include <library/cpp/protobuf/json/proto2json.h>
  6. #include <library/cpp/json/yson/json2yson.h>
  7. #include <string.h>
  8. extern char** environ;
  9. namespace NYql {
  10. static char** g_OriginalArgv = nullptr;
  11. static char* g_OriginalArgvLast = nullptr;
  12. /*
  13. * To change the process title in Linux and Darwin we have to set argv[1]
  14. * to NULL and to copy the title to the same place where the argv[0] points to.
  15. * However, argv[0] may be too small to hold a new title. Fortunately, Linux
  16. * and Darwin store argv[] and environ[] one after another. So we should
  17. * ensure that is the continuous memory and then we allocate the new memory
  18. * for environ[] and copy it. After this we could use the memory starting
  19. * from argv[0] for our process title.
  20. *
  21. * continuous memory block for process title
  22. * ________________________________/\____________________________________
  23. * / \
  24. * +---------+---------+-----+------+------------+------------+-----+------+
  25. * | argv[0] | argv[1] | ... | NULL | environ[0] | environ[1] | ... | NULL |
  26. * +---------+---------+-----+------+------------+------------+-----+------+
  27. * \_________________ _________________/
  28. * \/
  29. * must be relocated elsewhere
  30. */
  31. void ProcTitleInit(int argc, const char* argv[])
  32. {
  33. Y_UNUSED(argc);
  34. Y_ABORT_UNLESS(!g_OriginalArgv, "ProcTitleInit() was already called");
  35. g_OriginalArgv = const_cast<char**>(argv);
  36. size_t size = 0;
  37. for (int i = 0; environ[i]; i++) {
  38. size += strlen(environ[i]) + 1;
  39. }
  40. char* newEnviron = new char[size];
  41. g_OriginalArgvLast = g_OriginalArgv[0];
  42. for (int i = 0; g_OriginalArgv[i]; i++) {
  43. if (g_OriginalArgvLast == g_OriginalArgv[i]) {
  44. g_OriginalArgvLast = g_OriginalArgv[i] + strlen(g_OriginalArgv[i]) + 1;
  45. }
  46. }
  47. for (int i = 0; environ[i]; i++) {
  48. if (g_OriginalArgvLast == environ[i]) {
  49. size_t size = strlen(environ[i]) + 1;
  50. g_OriginalArgvLast = environ[i] + size;
  51. strncpy(newEnviron, environ[i], size);
  52. environ[i] = newEnviron;
  53. newEnviron += size;
  54. }
  55. }
  56. g_OriginalArgvLast--;
  57. }
  58. void SetProcTitle(const char* title)
  59. {
  60. if (!g_OriginalArgv) return;
  61. char* p = g_OriginalArgv[0];
  62. p += strlcpy(p, "yqlworker: ", g_OriginalArgvLast - p);
  63. p += strlcpy(p, title, g_OriginalArgvLast - p);
  64. if (g_OriginalArgvLast - p > 0) {
  65. memset(p, 0, g_OriginalArgvLast - p);
  66. }
  67. g_OriginalArgv[1] = nullptr;
  68. }
  69. void AddProcTitleSuffix(const char* suffix)
  70. {
  71. if (!g_OriginalArgv) return;
  72. char* p = g_OriginalArgv[0];
  73. p += strlcat(p, " ", g_OriginalArgvLast - p);
  74. p += strlcat(p, suffix, g_OriginalArgvLast - p);
  75. }
  76. const char* GetProcTitle()
  77. {
  78. return g_OriginalArgv ? g_OriginalArgv[0] : "UNKNOWN";
  79. }
  80. TString PbMessageToStr(const google::protobuf::Message& msg)
  81. {
  82. TString str;
  83. ::google::protobuf::TextFormat::Printer printer;
  84. printer.SetSingleLineMode(true);
  85. printer.PrintToString(msg, &str);
  86. return str;
  87. }
  88. TString Proto2Yson(const google::protobuf::Message& proto) {
  89. NJson::TJsonValue json;
  90. NProtobufJson::Proto2Json(proto, json);
  91. TString ysonResult;
  92. TStringOutput stream(ysonResult);
  93. NJson2Yson::SerializeJsonValueAsYson(json, &stream);
  94. return ysonResult;
  95. }
  96. } // namespace NYql