luci-base: enable uci session isolation
[project/luci.git] / modules / luci-base / luasrc / controller / admin / servicectl.lua
1 -- Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.controller.admin.servicectl", package.seeall)
5
6 function index()
7         entry({"servicectl"}, alias("servicectl", "status")).sysauth = "root"
8         entry({"servicectl", "status"}, call("action_status")).leaf = true
9         entry({"servicectl", "restart"}, post("action_restart")).leaf = true
10 end
11
12 function action_status()
13         local data = nixio.fs.readfile("/var/run/luci-reload-status")
14         if data then
15                 luci.http.write("/etc/config/")
16                 luci.http.write(data)
17         else
18                 luci.http.write("finish")
19         end
20 end
21
22 function action_restart(args)
23         local uci = require "luci.model.uci".cursor()
24         if args then
25                 local service
26                 local services = { }
27
28                 for service in args:gmatch("[%w_-]+") do
29                         services[#services+1] = service
30                 end
31
32                 local command = uci:apply(services, true)
33                 if nixio.fork() == 0 then
34                         local i = nixio.open("/dev/null", "r")
35                         local o = nixio.open("/dev/null", "w")
36
37                         nixio.dup(i, nixio.stdin)
38                         nixio.dup(o, nixio.stdout)
39
40                         i:close()
41                         o:close()
42
43                         nixio.exec("/bin/sh", unpack(command))
44                 else
45                         luci.http.write("OK")
46                         os.exit(0)
47                 end
48         end
49 end