summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Reichel <sre@ring0.de>2012-08-08 04:23:18 +0200
committerSebastian Reichel <sre@ring0.de>2012-08-08 04:23:18 +0200
commit9baee483352ad3a47e748eb33e6efaca81df13c7 (patch)
tree2592806ee9ee6be98cc438a5a61773d77428d6c3
parenta6d444fc08eabcf81b3d6e64ff2272039298b798 (diff)
downloadserial-barcode-scanner-9baee483352ad3a47e748eb33e6efaca81df13c7.tar.bz2
check pid in lock file
-rw-r--r--device.vala23
1 files 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);