main.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <dlfcn.h>
  2. #include "my_application.h"
  3. #define RUSTDESK_LIB_PATH "librustdesk.so"
  4. typedef bool (*RustDeskCoreMain)();
  5. bool gIsConnectionManager = false;
  6. void print_help_install_pkg(const char* so);
  7. bool flutter_rustdesk_core_main() {
  8. void* librustdesk = dlopen(RUSTDESK_LIB_PATH, RTLD_LAZY);
  9. if (!librustdesk) {
  10. fprintf(stderr,"Failed to load \"librustdesk.so\"\n");
  11. char* error;
  12. if ((error = dlerror()) != nullptr) {
  13. fprintf(stderr, "%s\n", error);
  14. char* libmissed = strstr(error, ": cannot open shared object file: No such file or directory");
  15. if (libmissed != nullptr) {
  16. *libmissed = '\0';
  17. char* so = strdup(error);
  18. print_help_install_pkg(so);
  19. free(so);
  20. }
  21. }
  22. return false;
  23. }
  24. auto core_main = (RustDeskCoreMain) dlsym(librustdesk,"rustdesk_core_main");
  25. char* error;
  26. if ((error = dlerror()) != nullptr) {
  27. fprintf(stderr, "Program entry \"rustdesk_core_main\" is not found: %s\n", error);
  28. return false;
  29. }
  30. return core_main();
  31. }
  32. int main(int argc, char** argv) {
  33. if (!flutter_rustdesk_core_main()) {
  34. return 0;
  35. }
  36. for (int i = 0; i < argc; i++) {
  37. if (strcmp(argv[i], "--cm") == 0) {
  38. gIsConnectionManager = true;
  39. }
  40. }
  41. g_autoptr(MyApplication) app = my_application_new();
  42. return g_application_run(G_APPLICATION(app), argc, argv);
  43. }
  44. typedef struct {
  45. const char* mgr;
  46. const char* search;
  47. } PkgMgrSearch;
  48. const PkgMgrSearch g_mgrs[] = {
  49. {
  50. "apt",
  51. "apt-file search",
  52. },
  53. {
  54. "dnf",
  55. "dnf provides",
  56. },
  57. {
  58. "yum",
  59. "yum provides",
  60. },
  61. {
  62. "zypper",
  63. "zypper wp",
  64. },
  65. {
  66. "pacman",
  67. "pacman -Qo",
  68. },
  69. {
  70. NULL,
  71. NULL,
  72. },
  73. };
  74. int is_command_exists(const char* command) {
  75. char* path = getenv("PATH");
  76. char* path_copy = strdup(path);
  77. char* dir = strtok(path_copy, ":");
  78. while (dir != NULL) {
  79. char command_path[256];
  80. snprintf(command_path, sizeof(command_path), "%s/%s", dir, command);
  81. if (access(command_path, X_OK) == 0) {
  82. free(path_copy);
  83. return 1;
  84. }
  85. dir = strtok(NULL, ":");
  86. }
  87. free(path_copy);
  88. return 0;
  89. }
  90. // We do not automatically search pkg
  91. // as the search process can be time consuming and update may be required.
  92. void print_help_install_pkg(const char* so)
  93. {
  94. if (strcmp(so, "libnsl.so.1") == 0) {
  95. const char* mgr[] = {"yum", "dnf", NULL};
  96. const char** m = mgr;
  97. while (*m != NULL) {
  98. if (is_command_exists(*m)) {
  99. fprintf(stderr, "Please run \"%s install libnsl\" to install the required package.\n", *m);
  100. return;
  101. }
  102. m++;
  103. }
  104. }
  105. const PkgMgrSearch *mgr_search = g_mgrs;
  106. while (mgr_search->mgr != NULL) {
  107. if (is_command_exists(mgr_search->mgr) == 1) {
  108. fprintf(stderr, "Please run \"%s %s\" to search and install the pkg.\n", mgr_search->search, so);
  109. break;
  110. }
  111. mgr_search++;
  112. }
  113. }