gluon-geolocator: use a volatile lock file
This commit is contained in:
parent
cd1f2b6910
commit
20661a2356
@ -1,42 +1,31 @@
|
|||||||
#!/usr/bin/lua
|
#!/usr/bin/lua
|
||||||
|
|
||||||
local uci = require('simple-uci').cursor()
|
local uci = require('simple-uci').cursor()
|
||||||
local json = require ("luci.jsonc")
|
local nixio = require('nixio')
|
||||||
|
local json = require ("jsonc")
|
||||||
local ubus = require 'ubus'
|
local ubus = require 'ubus'
|
||||||
local iwinfo = require("iwinfo")
|
local iwinfo = require("iwinfo")
|
||||||
|
|
||||||
local LOC="gluon-node-info"
|
local LOC="gluon-node-info"
|
||||||
local GLC="geolocator"
|
local GLC="geolocator"
|
||||||
|
local TIME_STAMP="/tmp/geolocator_timestamp"
|
||||||
|
|
||||||
if not uci:get_bool(GLC, "settings", "auto_location") then
|
if not uci:get_bool(GLC, "settings", "auto_location") then
|
||||||
os.exit(0)
|
os.exit(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- PID file to ensure the geolocator isn't running parallel
|
-- PID file to ensure the geolocator isn't running parallel
|
||||||
local PID_PART="/var/run/geolocator.pid"
|
local lockfile = '/var/lock/geolocator.lock'
|
||||||
local TIME_STAMP="/tmp/geolocator_timestamp"
|
local lockfd = nixio.open(lockfile, 'w', 'rw-------')
|
||||||
|
|
||||||
local function file_exsist(file)
|
if not lockfd:lock('tlock') then
|
||||||
return io.open(file, "r") ~= nil
|
io.stderr:write(string.format(
|
||||||
end
|
"Unable to lock file %s. Make sure there is no other instance of the geolocator running.\n",
|
||||||
|
lockfile
|
||||||
if file_exsist(PID_PART) then
|
))
|
||||||
io.stdout:write("The geolocator is still running.\n")
|
|
||||||
os.exit(0)
|
|
||||||
end
|
|
||||||
if io.open(PID_PART, "w") == nil then
|
|
||||||
io.stdout:write("Can`t create pid file on " .. PID_PART .. "\n")
|
|
||||||
os.exit(1)
|
os.exit(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Program terminating function including removing of PID file
|
|
||||||
local function exit(exc)
|
|
||||||
if file_exsist(PID_PART) then
|
|
||||||
os.remove(PID_PART)
|
|
||||||
end
|
|
||||||
os.exit(exc)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Iterates over all active WLAN interfaces
|
-- Iterates over all active WLAN interfaces
|
||||||
-- Returning true from the callback function will skip all remaining
|
-- Returning true from the callback function will skip all remaining
|
||||||
-- interfaces of the same radio
|
-- interfaces of the same radio
|
||||||
@ -101,15 +90,15 @@ local function locate()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Check if interval over or not exist
|
-- Check if interval over or not exist
|
||||||
if file_exsist(TIME_STAMP) then
|
if io.open(TIME_STAMP) ~= nil then
|
||||||
if os.time() - tonumber(io.open(TIME_STAMP):read("*a")) < uci:get(GLC, "settings", "refresh_interval") * 60 then
|
if os.time() - tonumber(io.open(TIME_STAMP):read("*a")) < uci:get(GLC, "settings", "refresh_interval") * 60 then
|
||||||
exit(0)
|
os.exit(0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local pos = locate()
|
local pos = locate()
|
||||||
if not next(pos) then
|
if not next(pos) then
|
||||||
exit(1)
|
os.exit(1)
|
||||||
end
|
end
|
||||||
if not uci:get_bool(GLC, "settings", "static_location") then
|
if not uci:get_bool(GLC, "settings", "static_location") then
|
||||||
uci:set(LOC, uci:get_first(LOC, 'location'), 'latitude', pos.lat)
|
uci:set(LOC, uci:get_first(LOC, 'location'), 'latitude', pos.lat)
|
||||||
@ -121,7 +110,7 @@ local timestap = io.open(TIME_STAMP, "w")
|
|||||||
if timestap ~= nil then
|
if timestap ~= nil then
|
||||||
timestap:write(os.time())
|
timestap:write(os.time())
|
||||||
timestap:close()
|
timestap:close()
|
||||||
exit(0)
|
os.exit(0)
|
||||||
end
|
end
|
||||||
io.stdout:write("Can`t create file on " .. TIME_STAMP .. "\n")
|
io.stdout:write("Can`t create file on " .. TIME_STAMP .. "\n")
|
||||||
exit(1)
|
os.exit(1)
|
||||||
|
Loading…
Reference in New Issue
Block a user