fix: linux, check load dylib in main.cc (#7901)

* fix: linux, check load dylib in main.cc

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* Better error message

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* refact error message

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* Better error message

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* check if "libnsl.so.1" is required, mainly for AppImage and yum

Signed-off-by: fufesou <shuanglongchen@yeah.net>

---------

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2024-05-04 14:07:29 +08:00 committed by GitHub
parent 890b735985
commit b387bc1038
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 106 additions and 34 deletions

View File

@ -108,24 +108,21 @@ class PlatformFFI {
_ffiBind.sessionRegisterGpuTexture(
sessionId: sessionId, display: display, ptr: ptr);
_loadDyLib() async {
/// Init the FFI class, loads the native Rust core library.
Future<void> init(String appType) async {
_appType = appType;
final dylib = isAndroid
? DynamicLibrary.open('librustdesk.so')
: isLinux
? DynamicLibrary.open('librustdesk.so')
: isWindows
? DynamicLibrary.open('librustdesk.dll')
: isMacOS
? DynamicLibrary.open("liblibrustdesk.dylib")
: DynamicLibrary.process();
debugPrint('initializing FFI $_appType');
try {
final dylib = isAndroid
? DynamicLibrary.open('librustdesk.so')
: isLinux
? DynamicLibrary.open('librustdesk.so')
: isWindows
? DynamicLibrary.open('librustdesk.dll')
: isMacOS
? DynamicLibrary.open("liblibrustdesk.dylib")
: DynamicLibrary.process();
try {
_session_get_rgba =
dylib.lookupFunction<F3Dart, F3>("session_get_rgba");
} catch (e) {
print('Failed to lookup function "session_get_rgba": $e');
exit(1);
}
_session_get_rgba = dylib.lookupFunction<F3Dart, F3>("session_get_rgba");
try {
// SYSTEM user failed
_dir = (await getApplicationDocumentsDirectory()).path;
@ -133,19 +130,7 @@ class PlatformFFI {
debugPrint('Failed to get documents directory: $e');
}
_ffiBind = RustdeskImpl(dylib);
} catch (e) {
print('Failed to load dynamic library: $e');
exit(1);
}
}
/// Init the FFI class, loads the native Rust core library.
Future<void> init(String appType) async {
_appType = appType;
await _loadDyLib();
debugPrint('initializing FFI $_appType');
try {
if (isLinux) {
// Start a dbus service, no need to await
_ffiBind.mainStartDbusServer();

View File

@ -6,21 +6,31 @@
typedef bool (*RustDeskCoreMain)();
bool gIsConnectionManager = false;
void print_help_install_pkg(const char* so);
bool flutter_rustdesk_core_main() {
void* librustdesk = dlopen(RUSTDESK_LIB_PATH, RTLD_LAZY);
if (!librustdesk) {
fprintf(stderr,"load librustdesk.so failed\n");
fprintf(stderr,"Failed to load \"librustdesk.so\"\n");
char* error;
if ((error = dlerror()) != nullptr) {
fprintf(stderr, "%s", error);
fprintf(stderr, "%s\n", error);
// libnsl.so.1: cannot open shared object file: No such file or directory
char* libmissed = strstr(error, ": cannot open shared object file: No such file or directory");
if (libmissed != nullptr) {
*libmissed = '\0';
char* so = strdup(error);
print_help_install_pkg(so);
free(so);
}
}
return true;
return false;
}
auto core_main = (RustDeskCoreMain) dlsym(librustdesk,"rustdesk_core_main");
char* error;
if ((error = dlerror()) != nullptr) {
fprintf(stderr, "error finding rustdesk_core_main: %s", error);
return true;
fprintf(stderr, "Program entry \"rustdesk_core_main\" is not found: %s\n", error);
return false;
}
return core_main();
}
@ -37,3 +47,80 @@ int main(int argc, char** argv) {
g_autoptr(MyApplication) app = my_application_new();
return g_application_run(G_APPLICATION(app), argc, argv);
}
typedef struct {
const char* mgr;
const char* search;
} PkgMgrSearch;
const PkgMgrSearch g_mgrs[] = {
{
"apt",
"apt-file search",
},
{
"dnf",
"dnf provides",
},
{
"yum",
"yum provides",
},
{
"zypper",
"zypper wp",
},
{
"pacman",
"pacman -Qo",
},
{
NULL,
NULL,
},
};
int is_command_exists(const char* command) {
char* path = getenv("PATH");
char* path_copy = strdup(path);
char* dir = strtok(path_copy, ":");
while (dir != NULL) {
char command_path[256];
snprintf(command_path, sizeof(command_path), "%s/%s", dir, command);
if (access(command_path, X_OK) == 0) {
free(path_copy);
return 1;
}
dir = strtok(NULL, ":");
}
free(path_copy);
return 0;
}
// We do not automatically search pkg
// as the search process can be time consuming and update may be required.
void print_help_install_pkg(const char* so)
{
if (strcmp(so, "libnsl.so.1") == 0) {
const char* mgr[] = {"yum", "dnf", NULL};
const char** m = mgr;
while (*m != NULL) {
if (is_command_exists(*m)) {
fprintf(stderr, "Please run \"%s install libnsl\" to install the required package.\n", *m);
return;
}
m++;
}
}
const PkgMgrSearch *mgr_search = g_mgrs;
while (mgr_search->mgr != NULL) {
if (is_command_exists(mgr_search->mgr) == 1) {
fprintf(stderr, "Please run \"%s %s\" to search and install the pkg.\n", mgr_search->search, so);
break;
}
mgr_search++;
}
}