gluon-geolocator: geolocator drop dep on nixio & use posix lockfile
Signed-off-by: Jan-Tarek Butt <tarek@ring0.de>
This commit is contained in:
parent
791dfe0a2f
commit
22b221a8fb
@ -1,7 +1,9 @@
|
|||||||
#!/usr/bin/lua
|
#!/usr/bin/lua
|
||||||
|
|
||||||
|
local bit = require('bit')
|
||||||
|
local unistd = require('posix.unistd')
|
||||||
|
local fcntl = require('posix.fcntl')
|
||||||
local uci = require('simple-uci').cursor()
|
local uci = require('simple-uci').cursor()
|
||||||
local nixio = require('nixio')
|
|
||||||
local json = require ("jsonc")
|
local json = require ("jsonc")
|
||||||
local ubus = require 'ubus'
|
local ubus = require 'ubus'
|
||||||
local iwinfo = require("iwinfo")
|
local iwinfo = require("iwinfo")
|
||||||
@ -16,9 +18,21 @@ end
|
|||||||
|
|
||||||
-- PID file to ensure the geolocator isn't running parallel
|
-- PID file to ensure the geolocator isn't running parallel
|
||||||
local lockfile = '/var/lock/geolocator.lock'
|
local lockfile = '/var/lock/geolocator.lock'
|
||||||
local lockfd = nixio.open(lockfile, 'w', 'rw-------')
|
local lockfd, err = fcntl.open(lockfile, bit.bor(fcntl.O_WRONLY, fcntl.O_CREAT), 384) -- mode 0600
|
||||||
|
|
||||||
if not lockfd:lock('tlock') then
|
if not lockfd then
|
||||||
|
io.stderr:write(err, '\n')
|
||||||
|
os.exit(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
local ok, _ = fcntl.fcntl(lockfd, fcntl.F_SETLK, {
|
||||||
|
l_start = 0,
|
||||||
|
l_len = 0,
|
||||||
|
l_type = fcntl.F_WRLCK,
|
||||||
|
l_whence = unistd.SEEK_SET,
|
||||||
|
})
|
||||||
|
|
||||||
|
if not ok then
|
||||||
io.stderr:write(string.format(
|
io.stderr:write(string.format(
|
||||||
"Unable to lock file %s. Make sure there is no other instance of the geolocator running.\n",
|
"Unable to lock file %s. Make sure there is no other instance of the geolocator running.\n",
|
||||||
lockfile
|
lockfile
|
||||||
@ -81,7 +95,7 @@ local function locate()
|
|||||||
return true
|
return true
|
||||||
end)
|
end)
|
||||||
|
|
||||||
assert(#found_bssids >= 12, 'insufficient BSSIDs found')
|
assert(#found_bssids > 0, 'insufficient BSSIDs found')
|
||||||
|
|
||||||
local data = receive_json('http://openwifi.su/api/v1/bssids/' .. table.concat(found_bssids, ','))
|
local data = receive_json('http://openwifi.su/api/v1/bssids/' .. table.concat(found_bssids, ','))
|
||||||
assert(type(data) == 'table' and data.lon and data.lat, 'location not available')
|
assert(type(data) == 'table' and data.lon and data.lat, 'location not available')
|
||||||
|
Loading…
Reference in New Issue
Block a user