From 9baee483352ad3a47e748eb33e6efaca81df13c7 Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Wed, 8 Aug 2012 04:23:18 +0200 Subject: check pid in lock file --- device.vala | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/device.vala b/device.vala index 6355107..f624d70 100644 --- a/device.vala +++ b/device.vala @@ -26,14 +26,23 @@ public class Device { public Device(string device, int rate, int bits, int stopbits) { Posix.speed_t baudrate = Posix.B9600; - /* check lock file */ - lockfile = File.new_for_path("/var/lock/LCK.." + device.replace("/dev/", "")); - if(lockfile.query_exists()) { - error("device is locked!\n"); - /* TODO: check pid */ - } - try { + /* check lock file */ + lockfile = File.new_for_path("/var/lock/LCK.." + device.replace("/dev/", "")); + if(lockfile.query_exists()) { + int pid = -1; + uint8[] data; + if(lockfile.load_contents(null, out data, null)) { + pid = int.parse((string) data); + } else { + error("Can't read lock file!\n"); + } + + if(Posix.kill(pid, 0) == 0) { + error("serial device is locked!\n"); + } + } + var pid = "%d\n".printf(Posix.getpid()); lockfile.replace_contents(pid.data, null, false, FileCreateFlags.NONE, null); -- cgit v1.2.3