From c88219b7696013b31b0a62c34a1865e303c1619c Mon Sep 17 00:00:00 2001 From: Sahil Yeole Date: Sun, 30 Jul 2023 15:07:17 +0530 Subject: [PATCH 1/4] retry _get_values_of_seat0 function to fix connection refused error Signed-off-by: Sahil Yeole --- libs/hbb_common/src/platform/linux.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/libs/hbb_common/src/platform/linux.rs b/libs/hbb_common/src/platform/linux.rs index 89c96799d..2b14fcd67 100644 --- a/libs/hbb_common/src/platform/linux.rs +++ b/libs/hbb_common/src/platform/linux.rs @@ -133,6 +133,8 @@ pub fn get_values_of_seat0_with_gdm_wayland(indices: &[usize]) -> Vec { } fn _get_values_of_seat0(indices: &[usize], ignore_gdm_wayland: bool) -> Vec { + let mut retry_attempt = 0; + loop{ if let Ok(output) = run_loginctl(None) { for line in String::from_utf8_lossy(&output.stdout).lines() { if line.contains("seat0") { @@ -172,7 +174,13 @@ fn _get_values_of_seat0(indices: &[usize], ignore_gdm_wayland: bool) -> Vec= 1 { + return line_values(indices, ""); + } + // Increment the retry_attempt counter and wait for 5 seconds before retrying + retry_attempt += 1; + std::thread::sleep(Duration::from_secs(5)); + } } pub fn is_active(sid: &str) -> bool { From 30c002831ec38faf6236dffb56bf1755dbcfaa78 Mon Sep 17 00:00:00 2001 From: Sahil Yeole Date: Sun, 30 Jul 2023 17:45:36 +0530 Subject: [PATCH 2/4] make multiple attempts for _get_values_of_seat0 Signed-off-by: Sahil Yeole --- libs/hbb_common/src/platform/linux.rs | 66 +++++++++++++-------------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/libs/hbb_common/src/platform/linux.rs b/libs/hbb_common/src/platform/linux.rs index 2b14fcd67..4aeeefb58 100644 --- a/libs/hbb_common/src/platform/linux.rs +++ b/libs/hbb_common/src/platform/linux.rs @@ -124,7 +124,7 @@ fn line_values(indices: &[usize], line: &str) -> Vec { #[inline] pub fn get_values_of_seat0(indices: &[usize]) -> Vec { - _get_values_of_seat0(indices, true) + _get_values_of_seat0(indices, true, 20) } #[inline] @@ -132,55 +132,51 @@ pub fn get_values_of_seat0_with_gdm_wayland(indices: &[usize]) -> Vec { _get_values_of_seat0(indices, false) } -fn _get_values_of_seat0(indices: &[usize], ignore_gdm_wayland: bool) -> Vec { - let mut retry_attempt = 0; - loop{ - if let Ok(output) = run_loginctl(None) { - for line in String::from_utf8_lossy(&output.stdout).lines() { - if line.contains("seat0") { +fn _get_values_of_seat0(indices: &[usize], ignore_gdm_wayland: bool, attempts: usize) -> Vec { + for _ in 0..attempts { + if let Ok(output) = run_loginctl(None) { + for line in String::from_utf8_lossy(&output.stdout).lines() { + if line.contains("seat0") { + if let Some(sid) = line.split_whitespace().next() { + if is_active(sid) { + if ignore_gdm_wayland { + if is_gdm_user(line.split_whitespace().nth(2).unwrap_or("")) + && get_display_server_of_session(sid) == DISPLAY_SERVER_WAYLAND + { + continue; + } + } + return line_values(indices, line); + } + } + } + } + + // some case, there is no seat0 https://github.com/rustdesk/rustdesk/issues/73 + for line in String::from_utf8_lossy(&output.stdout).lines() { if let Some(sid) = line.split_whitespace().next() { if is_active(sid) { + let d = get_display_server_of_session(sid); if ignore_gdm_wayland { if is_gdm_user(line.split_whitespace().nth(2).unwrap_or("")) - && get_display_server_of_session(sid) == DISPLAY_SERVER_WAYLAND + && d == DISPLAY_SERVER_WAYLAND { continue; } } + if d == "tty" { + continue; + } return line_values(indices, line); } } } } - // some case, there is no seat0 https://github.com/rustdesk/rustdesk/issues/73 - for line in String::from_utf8_lossy(&output.stdout).lines() { - if let Some(sid) = line.split_whitespace().next() { - if is_active(sid) { - let d = get_display_server_of_session(sid); - if ignore_gdm_wayland { - if is_gdm_user(line.split_whitespace().nth(2).unwrap_or("")) - && d == DISPLAY_SERVER_WAYLAND - { - continue; - } - } - if d == "tty" { - continue; - } - return line_values(indices, line); - } - } - } - } - - if retry_attempt >= 1 { - return line_values(indices, ""); - } - // Increment the retry_attempt counter and wait for 5 seconds before retrying - retry_attempt += 1; - std::thread::sleep(Duration::from_secs(5)); + // Wait for 300ms and try again + std::thread::sleep(std::time::Duration::from_millis(300)); } + return line_values(indices, ""); } pub fn is_active(sid: &str) -> bool { From cf6fbae30a10aa1f70eb63b54d0de52e99073a25 Mon Sep 17 00:00:00 2001 From: Sahil Yeole Date: Sun, 30 Jul 2023 17:58:14 +0530 Subject: [PATCH 3/4] add attempts for getting values of seat0 for gdm_wayland Signed-off-by: Sahil Yeole --- libs/hbb_common/src/platform/linux.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/hbb_common/src/platform/linux.rs b/libs/hbb_common/src/platform/linux.rs index 4aeeefb58..65d18c701 100644 --- a/libs/hbb_common/src/platform/linux.rs +++ b/libs/hbb_common/src/platform/linux.rs @@ -129,7 +129,7 @@ pub fn get_values_of_seat0(indices: &[usize]) -> Vec { #[inline] pub fn get_values_of_seat0_with_gdm_wayland(indices: &[usize]) -> Vec { - _get_values_of_seat0(indices, false) + _get_values_of_seat0(indices, false, 20) } fn _get_values_of_seat0(indices: &[usize], ignore_gdm_wayland: bool, attempts: usize) -> Vec { From cc9f69d63bce5dc779564531ece4d6fc752ab510 Mon Sep 17 00:00:00 2001 From: Sahil Yeole Date: Mon, 31 Jul 2023 20:54:05 +0530 Subject: [PATCH 4/4] add wrapper function _get_values_of_seat0_tries for attempts Signed-off-by: Sahil Yeole --- libs/hbb_common/src/platform/linux.rs | 70 +++++++++++++++------------ 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/libs/hbb_common/src/platform/linux.rs b/libs/hbb_common/src/platform/linux.rs index 65d18c701..1d33c36be 100644 --- a/libs/hbb_common/src/platform/linux.rs +++ b/libs/hbb_common/src/platform/linux.rs @@ -124,59 +124,67 @@ fn line_values(indices: &[usize], line: &str) -> Vec { #[inline] pub fn get_values_of_seat0(indices: &[usize]) -> Vec { - _get_values_of_seat0(indices, true, 20) + _get_values_of_seat0_tries(indices, true, 20) } #[inline] pub fn get_values_of_seat0_with_gdm_wayland(indices: &[usize]) -> Vec { - _get_values_of_seat0(indices, false, 20) + _get_values_of_seat0_tries(indices, false, 20) } -fn _get_values_of_seat0(indices: &[usize], ignore_gdm_wayland: bool, attempts: usize) -> Vec { - for _ in 0..attempts { - if let Ok(output) = run_loginctl(None) { - for line in String::from_utf8_lossy(&output.stdout).lines() { - if line.contains("seat0") { - if let Some(sid) = line.split_whitespace().next() { - if is_active(sid) { - if ignore_gdm_wayland { - if is_gdm_user(line.split_whitespace().nth(2).unwrap_or("")) - && get_display_server_of_session(sid) == DISPLAY_SERVER_WAYLAND - { - continue; - } - } - return line_values(indices, line); - } - } - } - } +fn _get_values_of_seat0_tries(indices: &[usize], ignore_gdm_wayland: bool, attempts: usize) -> Vec { + for _ in 0..attempts{ + let value = _get_values_of_seat0(indices, ignore_gdm_wayland); + if value != line_values(indices, "") { + return value; + } + // Wait for 300ms and try again + std::thread::sleep(std::time::Duration::from_millis(300)); + } + line_values(indices, "") +} - // some case, there is no seat0 https://github.com/rustdesk/rustdesk/issues/73 - for line in String::from_utf8_lossy(&output.stdout).lines() { +fn _get_values_of_seat0(indices: &[usize], ignore_gdm_wayland: bool) -> Vec { + if let Ok(output) = run_loginctl(None) { + for line in String::from_utf8_lossy(&output.stdout).lines() { + if line.contains("seat0") { if let Some(sid) = line.split_whitespace().next() { if is_active(sid) { - let d = get_display_server_of_session(sid); if ignore_gdm_wayland { if is_gdm_user(line.split_whitespace().nth(2).unwrap_or("")) - && d == DISPLAY_SERVER_WAYLAND + && get_display_server_of_session(sid) == DISPLAY_SERVER_WAYLAND { continue; } } - if d == "tty" { - continue; - } return line_values(indices, line); } } } } - // Wait for 300ms and try again - std::thread::sleep(std::time::Duration::from_millis(300)); + // some case, there is no seat0 https://github.com/rustdesk/rustdesk/issues/73 + for line in String::from_utf8_lossy(&output.stdout).lines() { + if let Some(sid) = line.split_whitespace().next() { + if is_active(sid) { + let d = get_display_server_of_session(sid); + if ignore_gdm_wayland { + if is_gdm_user(line.split_whitespace().nth(2).unwrap_or("")) + && d == DISPLAY_SERVER_WAYLAND + { + continue; + } + } + if d == "tty" { + continue; + } + return line_values(indices, line); + } + } + } } - return line_values(indices, ""); + + line_values(indices, "") } pub fn is_active(sid: &str) -> bool {