From fa9670a85b8dead29b01f395024000e54dcbd2b9 Mon Sep 17 00:00:00 2001 From: guinux Date: Sat, 13 May 2017 17:33:47 +0200 Subject: [PATCH] remove lockfile only if it's older than boot time --- src/daemon.vala | 67 +++++++++++++++++++++++++++++++++++-------------- src/tray.vala | 4 +-- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/src/daemon.vala b/src/daemon.vala index 3882dc1..1a20fa7 100644 --- a/src/daemon.vala +++ b/src/daemon.vala @@ -163,7 +163,7 @@ namespace Pamac { timer = new Timer (); extern_lock = false; refresh_handle (); - Timeout.add (500, check_pacman_running); + Timeout.add (500, check_extern_lock); create_thread_pool (); cancellable = new Cancellable (); curl = new Curl.Easy (); @@ -226,7 +226,7 @@ namespace Pamac { files_handle = alpm_config.get_files_handle (); } - private bool check_pacman_running () { + private bool check_extern_lock () { if (extern_lock) { if (!lockfile.query_exists ()) { extern_lock = false; @@ -236,28 +236,57 @@ namespace Pamac { } else { if (lockfile.query_exists ()) { if (databases_lock_mutex.trylock ()) { - // Functions trans_prepare, sysupgrade_prepare and refresh threads are blocked until unlock. - // An extern lock appears, check if pacman running. + extern_lock = true; + // Functions trans_init, build_prepare and refresh threads are blocked until unlock. + // An extern lock appears, check if it is not a too old lock. int exit_status; + string output; + uint64 lockfile_time; try { - Process.spawn_command_line_sync ("pidof pacman", - null, - null, - out exit_status); + // get lockfile modification time since epoch + Process.spawn_command_line_sync ("stat -c %Y %s".printf (alpm_handle.lockfile), + out output, + null, + out exit_status); + if (exit_status == 0) { + string[] splitted = output.split ("\n"); + if (splitted.length == 2) { + if (uint64.try_parse (splitted[0], out lockfile_time)) { + uint64 boot_time; + // get boot time since epoch + Process.spawn_command_line_sync ("cat /proc/stat", + out output, + null, + out exit_status); + if (exit_status == 0) { + splitted = output.split ("\n"); + foreach (unowned string line in splitted) { + if ("btime" in line) { + string[] space_splitted = line.split (" "); + if (space_splitted.length == 2) { + if (uint64.try_parse (space_splitted[1], out boot_time)) { + // check if lock file is older than boot time + if (lockfile_time < boot_time) { + // remove the unneeded lock file. + try { + lockfile.delete (); + } catch (Error e) { + stderr.printf ("Error: %s\n", e.message); + } + extern_lock = false; + databases_lock_mutex.unlock (); + } + } + } + } + } + } + } + } + } } catch (SpawnError e) { stderr.printf ("Error: %s\n", e.message); } - if (exit_status == 0) { - extern_lock = true; - } else { - // Pacman is not running: remove the unneeded lock file. - try { - lockfile.delete (); - } catch (Error e) { - stderr.printf ("Error: %s\n", e.message); - } - databases_lock_mutex.unlock (); - } } } } diff --git a/src/tray.vala b/src/tray.vala index d47f85a..416ae3d 100644 --- a/src/tray.vala +++ b/src/tray.vala @@ -252,7 +252,7 @@ namespace Pamac { return run; } - bool check_pacman_running () { + bool check_extern_lock () { if (extern_lock) { if (!lockfile.query_exists ()) { extern_lock = false; @@ -309,7 +309,7 @@ namespace Pamac { //try standard lock file lockfile = GLib.File.new_for_path ("var/lib/pacman/db.lck"); } - Timeout.add (200, check_pacman_running); + Timeout.add (200, check_extern_lock); start_refresh (); launch_refresh_timeout (pamac_config.refresh_period);