Audio plugin host https://kx.studio/carla
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.

115 lines
3.9KB

  1. #include <cstdlib>
  2. #include <cstring>
  3. #include <string>
  4. #include <cstdio>
  5. #include <fstream>
  6. #include <unistd.h>
  7. #include <dirent.h>
  8. #include "Util.h"
  9. #include "TmpFileMgr.h"
  10. std::string TmpFileMgr::get_tmp_nam() const
  11. {
  12. return tmp_nam_prefix + to_s(getpid());
  13. }
  14. void TmpFileMgr::create_tmp_file(unsigned server_port) const
  15. {
  16. std::string tmp_nam = get_tmp_nam();
  17. if(0 == access(tmp_nam.c_str(), F_OK)) {
  18. fprintf(stderr, "Error: Cannot overwrite file %s. "
  19. "You should probably remove it.", tmp_nam.c_str());
  20. exit(EXIT_FAILURE);
  21. }
  22. FILE* tmp_fp = fopen(tmp_nam.c_str(), "w");
  23. if(!tmp_fp)
  24. fprintf(stderr, "Warning: could not create new file %s.\n",
  25. tmp_nam.c_str());
  26. else
  27. fprintf(tmp_fp, "%u", server_port);
  28. fclose(tmp_fp);
  29. }
  30. void TmpFileMgr::clean_up_tmp_nams() const
  31. {
  32. DIR *dir;
  33. struct dirent *entry;
  34. if ((dir = opendir ("/tmp/")) != nullptr)
  35. {
  36. while ((entry = readdir (dir)) != nullptr)
  37. {
  38. std::string name = std::string("/tmp/") + entry->d_name;
  39. if(!name.compare(0, strlen(tmp_nam_prefix),tmp_nam_prefix))
  40. {
  41. std::string pid = name.substr(strlen(tmp_nam_prefix));
  42. std::string proc_file = "/proc/" + std::move(pid) +
  43. "/comm";
  44. std::ifstream ifs(proc_file);
  45. bool remove = false;
  46. if(!ifs.good())
  47. {
  48. fprintf(stderr, "Note: trying to remove %s - the "
  49. "process does not exist anymore.\n",
  50. name.c_str());
  51. remove = true;
  52. }
  53. else
  54. {
  55. std::string comm_name;
  56. ifs >> comm_name;
  57. if(comm_name == "zynaddsubfx")
  58. fprintf(stderr, "Note: detected running "
  59. "zynaddsubfx with PID %s.\n",
  60. name.c_str() + strlen(tmp_nam_prefix));
  61. else {
  62. fprintf(stderr, "Note: trying to remove %s - the "
  63. "PID is owned by\n another "
  64. "process: %s\n",
  65. name.c_str(), comm_name.c_str());
  66. remove = true;
  67. }
  68. }
  69. if(remove)
  70. {
  71. // make sure this file contains only one unsigned
  72. unsigned udp_port;
  73. std::ifstream ifs2(name);
  74. if(!ifs2.good())
  75. fprintf(stderr, "Warning: could not open %s.\n",
  76. name.c_str());
  77. else
  78. {
  79. ifs2 >> udp_port;
  80. if(ifs.good())
  81. fprintf(stderr, "Warning: could not remove "
  82. "%s, \n it has not been "
  83. "written by zynaddsubfx\n",
  84. name.c_str());
  85. else
  86. {
  87. if(std::remove(name.c_str()) != 0)
  88. fprintf(stderr, "Warning: can not remove "
  89. "%s.\n", name.c_str());
  90. }
  91. }
  92. }
  93. /* one might want to connect to zyn here,
  94. but it is not necessary:
  95. lo_address la = lo_address_new(nullptr, udp_port.c_str());
  96. if(lo_send(la, "/echo", nullptr) <= 0)
  97. fputs("Note: found crashed file %s\n", stderr);
  98. lo_address_free(la);*/
  99. }
  100. }
  101. closedir (dir);
  102. } else {
  103. fputs("Warning: can not read /tmp.\n", stderr);
  104. }
  105. }