diff options
author | Sebastian Reichel <sre@ring0.de> | 2012-08-08 04:10:18 +0200 |
---|---|---|
committer | Sebastian Reichel <sre@ring0.de> | 2012-08-08 04:10:18 +0200 |
commit | a6d444fc08eabcf81b3d6e64ff2272039298b798 (patch) | |
tree | fb393d15a8d5c171b5a5b9ee733f027938551a1d | |
parent | b33ca54964b823da8764710dbee141a548240baf (diff) | |
download | serial-barcode-scanner-a6d444fc08eabcf81b3d6e64ff2272039298b798.tar.bz2 |
lock serial device
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | db.vala | 2 | ||||
-rw-r--r-- | device.vala | 38 | ||||
-rw-r--r-- | main.vala | 5 |
4 files changed, 41 insertions, 6 deletions
@@ -2,7 +2,7 @@ PREFIX=/usr/local # web.vala (currently disabled) barcode-scanner: main.vala device.vala db.vala ui.vala - valac-0.16 --output $@ --pkg posix --pkg linux --pkg libsoup-2.4 --pkg sqlite3 --pkg gtk+-3.0 --pkg gee-1.0 --pkg gmodule-2.0 $^ + valac-0.16 --output $@ --pkg posix --pkg linux --pkg libsoup-2.4 --pkg sqlite3 --pkg gtk+-3.0 --pkg gee-1.0 --pkg gmodule-2.0 --pkg gio-2.0 $^ shop.db: create_db.sql sqlite3 shop.db < create_db.sql @@ -41,7 +41,7 @@ public class Database { public Database(string file) { int rc; - rc = Sqlite.Database.open (file, out db); + rc = Sqlite.Database.open(file, out db); if(rc != Sqlite.OK) { error("could not open database!"); } diff --git a/device.vala b/device.vala index 0bbc806..6355107 100644 --- a/device.vala +++ b/device.vala @@ -19,19 +19,37 @@ public class Device { public int fd=-1; private IOChannel io_read; public int byterate; + private File lockfile; public signal void received_barcode(string barcode); public Device(string device, int rate, int bits, int stopbits) { Posix.speed_t baudrate = Posix.B9600; - fd = Posix.open(device, Posix.O_RDWR /*| Posix.O_NONBLOCK*/); + /* 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 { + var pid = "%d\n".printf(Posix.getpid()); + lockfile.replace_contents(pid.data, null, false, FileCreateFlags.NONE, null); + + fd = Posix.open(device, Posix.O_RDWR /*| Posix.O_NONBLOCK*/); + + if(fd < 0) { + fd = -1; + lockfile.delete(); + error("Could not open device!\n"); + } - if(fd < 0) { - fd = -1; - error("Could not open device!\n"); + } catch(Error e) { + error("Could not create lock file: %s!\n", e.message); } + Posix.tcflush(fd, Posix.TCIOFLUSH); Posix.tcgetattr(fd, out restoretio); @@ -140,6 +158,18 @@ public class Device { error("IOChannel: %s", e.message); } } + + ~Device() { + /* restore old tty config */ + Posix.tcsetattr(fd, Posix.TCSANOW, restoretio); + + /* close file */ + Posix.close(fd); + + /* remove lock */ + lockfile.delete(); + } + private bool device_read(IOChannel gio, IOCondition cond) { IOStatus ret; string msg; @@ -48,6 +48,11 @@ public static int main(string[] args) { write_to_log("KtT Shop System has been started"); Gtk.main(); + + /* call destructors */ + dev = null; + db = null; + return 0; } |