luci-0.10: merge r8127 and r8128
[project/luci.git] / modules / admin-core / luasrc / controller / admin / servicectl.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
5
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
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 module("luci.controller.admin.servicectl", package.seeall)
16
17 function index()
18 entry({"servicectl"}, alias("servicectl", "status")).sysauth = "root"
19 entry({"servicectl", "status"}, call("action_status")).leaf = true
20 entry({"servicectl", "restart"}, call("action_restart")).leaf = true
21 end
22
23 function action_status()
24 local data = nixio.fs.readfile("/var/run/luci-reload-status")
25 if data then
26 luci.http.write("/etc/config/")
27 luci.http.write(data)
28 else
29 luci.http.write("finish")
30 end
31 end
32
33 function action_restart()
34 local uci = require "luci.model.uci".cursor()
35 local rqp = luci.dispatcher.context.requestpath
36
37 if rqp[3] then
38 local service
39 local services = { }
40
41 for service in rqp[3]:gmatch("[%w_-]+") do
42 services[#services+1] = service
43 end
44
45 local command = uci:apply(services, true)
46 if nixio.fork() == 0 then
47 local i = nixio.open("/dev/null", "r")
48 local o = nixio.open("/dev/null", "w")
49
50 nixio.dup(i, nixio.stdin)
51 nixio.dup(o, nixio.stdout)
52
53 i:close()
54 o:close()
55
56 nixio.exec("/bin/sh", unpack(command))
57 else
58 luci.http.write("OK")
59 os.exit(0)
60 end
61 end
62 end