2 LuCI - Lua Development Framework
4 Copyright 2009 Steven Barth <steven@midlink.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
15 local nixio = require "nixio"
16 local table = require "table"
17 local uci = require "luci.model.uci"
18 local os = require "os"
19 local io = require "io"
21 local pairs, require, pcall, assert, type = pairs, require, pcall, assert, type
22 local ipairs, tonumber, collectgarbage = ipairs, tonumber, collectgarbage
32 local ifaddrs = nixio.getifaddrs()
35 state = uci.cursor_state()
40 local UCINAME = UCINAME
41 local SSTATE = "/tmp/.lucid_store"
44 --- Starts a new LuCId superprocess.
48 local detach = cursor:get(UCINAME, "main", "daemonize")
50 local stat, code, msg = daemonize()
52 nixio.syslog("crit", "Unable to detach process: " .. msg .. "\n")
57 state:set(UCINAME, "main", "pid", nixio.getpid())
63 --- Stops any running LuCId superprocess.
65 local pid = tonumber(state:get(UCINAME, "main", "pid"))
67 return nixio.kill(pid, nixio.const.SIGTERM)
72 --- Prepares the slaves, daemons and publishers, allocate resources.
74 local debug = tonumber((cursor:get(UCINAME, "main", "debug")))
76 nixio.openlog("lucid", "pid", "perror")
78 nixio.setlogmask("warning")
81 cursor:foreach(UCINAME, "daemon", function(config)
82 if config.enabled ~= "1" then
86 local key = config[".name"]
87 if not config.slave then
88 nixio.syslog("crit", "Daemon "..key.." is missing a slave\n")
91 nixio.syslog("info", "Initializing daemon " .. key)
94 state:revert(UCINAME, key)
96 local daemon, code, err = prepare_daemon(config)
98 state:set(UCINAME, key, "status", "started")
99 nixio.syslog("info", "Prepared daemon " .. key)
101 state:set(UCINAME, key, "status", "error")
102 state:set(UCINAME, key, "error", err)
103 nixio.syslog("err", "Failed to initialize daemon "..key..": "..
109 --- Run the superprocess if prepared before.
110 -- This main function of LuCId will wait for events on given file descriptors.
112 local pollint = tonumber((cursor:get(UCINAME, "main", "pollinterval")))
115 local stat, code = nixio.poll(pollt, pollint)
117 if stat and stat > 0 then
118 for _, polle in ipairs(pollt) do
119 if polle.revents ~= 0 and polle.handler then
123 elseif stat == 0 then
124 ifaddrs = nixio.getifaddrs()
125 collectgarbage("collect")
128 for _, cb in ipairs(tickt) do
132 local pid, stat, code = nixio.wait(-1, "nohang")
133 while pid and pid > 0 do
135 if tpids[pid] and tpids[pid] ~= true then
136 tpids[pid](pid, stat, code)
138 pid, stat, code = nixio.wait(-1, "nohang")
143 --- Add a file descriptor for the main loop and associate handler functions.
144 -- @param polle Table containing: {fd = FILE DESCRIPTOR, events = POLL EVENTS,
145 -- handler = EVENT HANDLER CALLBACK}
146 -- @see unregister_pollfd
147 -- @return boolean status
148 function register_pollfd(polle)
149 pollt[#pollt+1] = polle
153 --- Unregister a file desciptor and associate handler from the main loop.
154 -- @param polle Poll descriptor
155 -- @see register_pollfd
156 -- @return boolean status
157 function unregister_pollfd(polle)
158 for k, v in ipairs(pollt) do
160 table.remove(pollt, k)
167 --- Close all registered file descriptors from main loop.
168 -- This is useful for forked child processes.
169 function close_pollfds()
170 for k, v in ipairs(pollt) do
171 if v.fd and v.fd.close then
177 --- Register a tick function that will be called at each cycle of the main loop.
178 -- @param cb Callback
179 -- @see unregister_tick
180 -- @return boolean status
181 function register_tick(cb)
186 --- Unregister a tick function from the main loop.
187 -- @param cb Callback
188 -- @see register_tick
189 -- @return boolean status
190 function unregister_tick(cb)
191 for k, v in ipairs(tickt) do
193 table.remove(tickt, k)
200 --- Create a new child process from a Lua function and assign a destructor.
201 -- @param threadcb main function of the new process
202 -- @param waitcb destructor callback
203 -- @return process identifier or nil, error code, error message
204 function create_process(threadcb, waitcb)
205 local threadlimit = tonumber(cursor:get(UCINAME, "main", "threadlimit"))
206 if threadlimit and tcount >= threadlimit then
207 nixio.syslog("warning", "Cannot create thread: process limit reached")
210 local pid, code, err = nixio.fork()
211 if pid and pid ~= 0 then
215 local code = threadcb()
218 nixio.syslog("err", "Unable to fork(): " .. err)
220 return pid, code, err
223 --- Prepare a daemon from a given configuration table.
224 -- @param config Configuration data.
225 -- @return boolean status or nil, error code, error message
226 function prepare_daemon(config)
227 nixio.syslog("info", "Preparing daemon " .. config[".name"])
228 local modname = cursor:get(UCINAME, config.slave)
230 return nil, -1, "invalid slave"
233 local stat, module = pcall(require, _NAME .. "." .. modname)
234 if not stat or not module.prepare_daemon then
235 return nil, -2, "slave type not supported"
238 config.slave = prepare_slave(config.slave)
240 return module.prepare_daemon(config, _M)
244 -- @param name slave name
245 -- @return table containing slave module and configuration or nil, error message
246 function prepare_slave(name)
247 local slave = slaves[name]
249 local config = cursor:get_all(UCINAME, name)
251 local stat, module = pcall(require, config and config.entrypoint)
253 slave = {module = module, config = config}
264 --- Return a list of available network interfaces on the host.
265 -- @return table returned by nixio.getifaddrs()
266 function get_interfaces()
270 --- Revoke process privileges.
271 -- @param user new user name or uid
272 -- @param group new group name or gid
273 -- @return boolean status or nil, error code, error message
274 function revoke_privileges(user, group)
275 if nixio.getuid() == 0 then
276 return nixio.setgid(group) and nixio.setuid(user)
280 --- Return a secure UCI cursor.
281 -- @return UCI cursor
282 function securestate()
283 local stat = nixio.fs.stat(SSTATE) or {}
284 local uid = nixio.getuid()
285 if stat.type ~= "dir" or (stat.modedec % 100) ~= 0 or stat.uid ~= uid then
286 nixio.fs.remover(SSTATE)
287 if not nixio.fs.mkdir(SSTATE, 700) then
288 local errno = nixio.errno()
289 nixio.syslog("err", "Integrity check on secure state failed!")
290 return nil, errno, nixio.perror(errno)
294 return uci.cursor(nil, SSTATE)
297 --- Daemonize the process.
298 -- @return boolean status or nil, error code, error message
300 if nixio.getppid() == 1 then
304 local pid, code, msg = nixio.fork()
306 return nil, code, msg
314 local devnull = nixio.open("/dev/null", nixio.open_flags("rdwr"))
315 nixio.dup(devnull, nixio.stdin)
316 nixio.dup(devnull, nixio.stdout)
317 nixio.dup(devnull, nixio.stderr)