5
* Copyright 2016 Tony George <teejeetech@gmail.com>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26
using TeeJee.FileSystem;
27
using TeeJee.ProcessHelper;
30
public class AppLock : GLib.Object {
31
public string lock_file = "";
32
public string lock_message = "";
34
public bool create(string app_name, string message){
36
var lock_dir = "/var/run/lock/%s".printf(app_name);
38
lock_file = path_combine(lock_dir, "lock");
41
var file = File.new_for_path(lock_file);
42
if (file.query_exists()) {
44
string txt = file_read(lock_file);
45
string process_id = txt.split(";")[0].strip();
46
lock_message = txt.split(";")[1].strip();
47
long pid = long.parse(process_id);
49
if (process_is_running(pid)){
50
log_msg(_("Another instance of this application is running")
51
+ " (PID=%ld)".printf(pid));
55
log_msg(_("[Warning] Deleted invalid lock"));
57
write_lock_file(message);
62
write_lock_file(message);
67
log_error (e.message);
72
private void write_lock_file(string message){
73
string current_pid = ((long) Posix.getpid()).to_string();
74
file_write(lock_file, "%s;%s".printf(current_pid, message));
79
var file = File.new_for_path (lock_file);
80
if (file.query_exists()) {
85
log_error (e.message);