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"
48 local detach = cursor:get(UCINAME, "main", "daemonize")
50 local stat, code, msg = daemonize()
52 nixio.syslog("crit", "Unable to detach process: " .. msg .. "\n")
61 local debug = tonumber((cursor:get(UCINAME, "main", "debug")))
63 nixio.openlog("lucid", "pid", "perror")
65 nixio.setlogmask("warning")
68 cursor:foreach(UCINAME, "daemon", function(config)
69 if config.enabled ~= "1" then
73 local key = config[".name"]
74 if not config.slave then
75 nixio.syslog("crit", "Daemon "..key.." is missing a slave\n")
78 nixio.syslog("info", "Initializing daemon " .. key)
81 state:revert(UCINAME, key)
83 local daemon, code, err = prepare_daemon(config)
85 state:set(UCINAME, key, "status", "started")
86 nixio.syslog("info", "Prepared daemon " .. key)
88 state:set(UCINAME, key, "status", "error")
89 state:set(UCINAME, key, "error", err)
90 nixio.syslog("err", "Failed to initialize daemon "..key..": "..
97 local pollint = tonumber((cursor:get(UCINAME, "main", "pollinterval")))
100 local stat, code = nixio.poll(pollt, pollint)
102 if stat and stat > 0 then
103 for _, polle in ipairs(pollt) do
104 if polle.revents ~= 0 and polle.handler then
108 elseif stat == 0 then
109 ifaddrs = nixio.getifaddrs()
110 collectgarbage("collect")
113 for _, cb in ipairs(tickt) do
117 local pid, stat, code = nixio.wait(-1, "nohang")
118 while pid and pid > 0 do
120 if tpids[pid] and tpids[pid] ~= true then
121 tpids[pid](pid, stat, code)
123 pid, stat, code = nixio.wait(-1, "nohang")
128 function register_pollfd(polle)
129 pollt[#pollt+1] = polle
133 function unregister_pollfd(polle)
134 for k, v in ipairs(pollt) do
136 table.remove(pollt, k)
143 function close_pollfds()
144 for k, v in ipairs(pollt) do
145 if v.fd and v.fd.close then
151 function register_tick(cb)
156 function unregister_tick(cb)
157 for k, v in ipairs(tickt) do
159 table.remove(tickt, k)
166 function create_process(threadcb, waitcb)
167 local threadlimit = tonumber(cursor:get(UCINAME, "main", "threadlimit"))
168 if threadlimit and #tpids >= tcount then
169 nixio.syslog("warning", "Unable to create thread: process limit reached")
172 local pid, code, err = nixio.fork()
173 if pid and pid ~= 0 then
177 local code = threadcb()
180 nixio.syslog("err", "Unable to fork(): " .. err)
182 return pid, code, err
185 function prepare_daemon(config)
186 nixio.syslog("info", "Preparing daemon " .. config[".name"])
187 local modname = cursor:get(UCINAME, config.slave)
189 return nil, -1, "invalid slave"
192 local stat, module = pcall(require, _NAME .. "." .. modname)
193 if not stat or not module.prepare_daemon then
194 return nil, -2, "slave type not supported"
197 config.slave = prepare_slave(config.slave)
199 return module.prepare_daemon(config, _M)
202 function prepare_slave(name)
203 local slave = slaves[name]
205 local config = cursor:get_all(UCINAME, name)
207 local stat, module = pcall(require, config and config.entrypoint)
209 slave = {module = module, config = config}
220 function get_interfaces()
224 function revoke_privileges(user, group)
225 if nixio.getuid() == 0 then
226 return nixio.setgid(group) and nixio.setuid(user)
230 function securestate()
231 local stat = nixio.fs.stat(SSTATE) or {}
232 local uid = nixio.getuid()
233 if stat.type ~= "dir" or (stat.modedec % 100) ~= 0 or stat.uid ~= uid then
234 nixio.fs.remover(SSTATE)
235 if not nixio.fs.mkdir(SSTATE, 700) then
236 local errno = nixio.errno()
237 nixio.syslog("err", "Integrity check on secure state failed!")
238 return nil, errno, nixio.perror(errno)
242 return uci.cursor(nil, SSTATE)
246 if nixio.getppid() == 1 then
250 local pid, code, msg = nixio.fork()
252 return nil, code, msg
260 local devnull = nixio.open("/dev/null", nixio.open_flags("rdwr"))
261 nixio.dup(devnull, nixio.stdin)
262 nixio.dup(devnull, nixio.stdout)
263 nixio.dup(devnull, nixio.stderr)