summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Reichel <sre@ring0.de>2012-08-08 04:10:18 +0200
committerSebastian Reichel <sre@ring0.de>2012-08-08 04:10:18 +0200
commita6d444fc08eabcf81b3d6e64ff2272039298b798 (patch)
treefb393d15a8d5c171b5a5b9ee733f027938551a1d
parentb33ca54964b823da8764710dbee141a548240baf (diff)
downloadserial-barcode-scanner-a6d444fc08eabcf81b3d6e64ff2272039298b798.tar.bz2
lock serial device
-rw-r--r--Makefile2
-rw-r--r--db.vala2
-rw-r--r--device.vala38
-rw-r--r--main.vala5
4 files changed, 41 insertions, 6 deletions
diff --git a/Makefile b/Makefile
index 680783b..2c7d106 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/db.vala b/db.vala
index b6218a9..8adf81d 100644
--- a/db.vala
+++ b/db.vala
@@ -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;
diff --git a/main.vala b/main.vala
index cd441e3..1f2c404 100644
--- a/main.vala
+++ b/main.vala
@@ -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;
}