From c9b5895b40ac822b02afb0ed8cbc56095f0f5dd5 Mon Sep 17 00:00:00 2001 From: Yannis Gerlach <100762533+ygerlach@users.noreply.github.com> Date: Sun, 5 Oct 2025 11:57:15 +0200 Subject: [PATCH 1/4] optimize get_user_id, get_user_id_effective and get_username_from_uid --- src/Utility/TeeJee.System.vala | 39 ++++++++-------------------------- 1 file changed, 9 insertions(+), 30 deletions(-) diff --git a/src/Utility/TeeJee.System.vala b/src/Utility/TeeJee.System.vala index 38e19d1b..4f5b4d9b 100644 --- a/src/Utility/TeeJee.System.vala +++ b/src/Utility/TeeJee.System.vala @@ -46,28 +46,23 @@ namespace TeeJee.System{ return int.parse(pkexec_uid); } - string sudo_user = GLib.Environment.get_variable("SUDO_USER"); + string sudo_user = GLib.Environment.get_variable("SUDO_UID"); if (sudo_user != null){ - return get_user_id_from_username(sudo_user); + return int.parse(sudo_user); } return get_user_id_effective(); // normal user } + private int euid = -1; // cache for get_user_id_effective (its never going to change) public int get_user_id_effective(){ - // returns effective user id (0 for applications executed with sudo and pkexec) - - int uid = -1; - string cmd = "id -u"; - string std_out, std_err; - exec_sync(cmd, out std_out, out std_err); - if ((std_out != null) && (std_out.length > 0)){ - uid = int.parse(std_out); + if (euid < 0) { + euid = (int) Posix.geteuid(); } - return uid; + return euid; } public string get_username(){ @@ -105,25 +100,9 @@ namespace TeeJee.System{ return -1; } - public string get_username_from_uid(int user_id){ - - // check local user accounts in /etc/passwd ------------------- - - foreach(var line in file_read("/etc/passwd").split("\n")){ - - var arr = line.split(":"); - - if ((arr.length >= 3) && (arr[2] == user_id.to_string())){ - - return arr[0]; - } - } - - // not found -------------------- - - log_error("Username not found for uid: %d".printf(user_id)); - - return ""; + public string? get_username_from_uid(int user_id){ + unowned Posix.Passwd? pw = Posix.getpwuid(user_id); + return pw?.pw_name; } public string get_user_home(string username = get_username()){ From 5bdcc0537cf889582c41540bc880b44624f038cb Mon Sep 17 00:00:00 2001 From: Yannis Gerlach <100762533+ygerlach@users.noreply.github.com> Date: Sun, 5 Oct 2025 11:59:17 +0200 Subject: [PATCH 2/4] let xdg_open drop the root user automatically --- src/Utility/TeeJee.System.vala | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Utility/TeeJee.System.vala b/src/Utility/TeeJee.System.vala index 4f5b4d9b..52205e2a 100644 --- a/src/Utility/TeeJee.System.vala +++ b/src/Utility/TeeJee.System.vala @@ -143,18 +143,27 @@ namespace TeeJee.System{ // open ----------------------------- - public bool xdg_open (string file, string user = ""){ - string path = get_cmd_path ("xdg-open"); - if ((path != null) && (path != "")){ - string cmd = "xdg-open '%s'".printf(escape_single_quote(file)); - if (user.length > 0){ - cmd = "pkexec --user %s env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY ".printf(user) + cmd; + public bool xdg_open (string file){ + string path = get_cmd_path("xdg-open"); + if ((path == null) || (path == "")) { + return false; + } + + string cmd = "xdg-open '%s'".printf(escape_single_quote(file)); + + // find correct user + int uid = get_user_id(); + if(uid > 0) { + // non root + string? user = get_username_from_uid(uid); + if(user != null) { + cmd = "pkexec --user %s env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%i/bus ".printf(user, uid) + cmd; } - log_debug(cmd); - int status = exec_script_async(cmd); - return (status == 0); } - return false; + + log_debug(cmd); + int status = exec_script_async(cmd); + return (status == 0); } public bool exo_open_folder (string dir_path, bool xdg_open_try_first = true){ From e6d8d66c4b2f6b866ee7c1bde89e35a920aefc5b Mon Sep 17 00:00:00 2001 From: Yannis Gerlach <100762533+ygerlach@users.noreply.github.com> Date: Sun, 5 Oct 2025 11:59:36 +0200 Subject: [PATCH 3/4] use xdg_open to open links in about dialog --- src/Gtk/MainWindow.vala | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Gtk/MainWindow.vala b/src/Gtk/MainWindow.vala index 4dcf74c3..431867e1 100644 --- a/src/Gtk/MainWindow.vala +++ b/src/Gtk/MainWindow.vala @@ -933,6 +933,9 @@ class MainWindow : Gtk.Window{ dialog.set_license_type(Gtk.License.GPL_2_0); dialog.set_website_label("https://github.com/linuxmint/timeshift"); dialog.set_website("https://github.com/linuxmint/timeshift"); + + // this overwrites the default behaviour of About Dialog + dialog.activate_link.connect(TeeJee.System.xdg_open); dialog.run(); dialog.destroy(); } From a8bbe648a36678c7a10c25b211d3644ae63102dc Mon Sep 17 00:00:00 2001 From: Yannis Gerlach <100762533+ygerlach@users.noreply.github.com> Date: Sun, 5 Oct 2025 12:01:49 +0200 Subject: [PATCH 4/4] remove unused user functions --- src/Utility/TeeJee.System.vala | 60 ---------------------------------- 1 file changed, 60 deletions(-) diff --git a/src/Utility/TeeJee.System.vala b/src/Utility/TeeJee.System.vala index 52205e2a..c3a2be3c 100644 --- a/src/Utility/TeeJee.System.vala +++ b/src/Utility/TeeJee.System.vala @@ -65,71 +65,11 @@ namespace TeeJee.System{ return euid; } - public string get_username(){ - - // returns actual username of current user (even for applications executed with sudo and pkexec) - - return get_username_from_uid(get_user_id()); - } - - public string get_username_effective(){ - - // returns effective user id ('root' for applications executed with sudo and pkexec) - - return get_username_from_uid(get_user_id_effective()); - } - - public int get_user_id_from_username(string username){ - - // check local user accounts in /etc/passwd ------------------- - - foreach(var line in file_read("/etc/passwd").split("\n")){ - - var arr = line.split(":"); - - if ((arr.length >= 3) && (arr[0] == username)){ - - return int.parse(arr[2]); - } - } - - // not found -------------------- - - log_error("UserId not found for userName: %s".printf(username)); - - return -1; - } - public string? get_username_from_uid(int user_id){ unowned Posix.Passwd? pw = Posix.getpwuid(user_id); return pw?.pw_name; } - public string get_user_home(string username = get_username()){ - - // check local user accounts in /etc/passwd ------------------- - - foreach(var line in file_read("/etc/passwd").split("\n")){ - - var arr = line.split(":"); - - if ((arr.length >= 6) && (arr[0] == username)){ - - return arr[5]; - } - } - - // not found -------------------- - - log_error("Home directory not found for user: %s".printf(username)); - - return ""; - } - - public string get_user_home_effective(){ - return get_user_home(get_username_effective()); - } - // system ------------------------------------ public double get_system_uptime_seconds(){