modules/admin-core: add service control module for interfacing with luci-reload
[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         luci.i18n.loadc("base")
19         local i18n = luci.i18n.translate
20
21         entry({"servicectl"}, alias("servicectl", "status"), "", 1).sysauth = "root"
22         entry({"servicectl", "status"}, call("action_status"), "", 2).leaf = true
23         entry({"servicectl", "restart"}, call("action_restart"), "", 3).leaf = true
24 end
25
26 function action_status()
27         local data = nixio.fs.readfile("/var/run/luci-reload-status")
28         if data then
29                 luci.http.write("/etc/config/")
30                 luci.http.write(data)
31         else
32                 luci.http.write("finish")
33         end
34 end
35
36 function action_restart()
37         if luci.dispatcher.context.requestpath[3] then
38                 local service
39                 local services = { }
40
41                 for service in luci.dispatcher.context.requestpath[3]:gmatch("%w+") do
42                         services[#services+1] = service
43                 end
44
45                 if nixio.fork() == 0 then
46                         local i = nixio.open("/dev/null", "r")
47                         local o = nixio.open("/dev/null", "w")
48
49                         nixio.dup(i, nixio.stdin)
50                         nixio.dup(o, nixio.stdout)
51
52                         i:close()
53                         o:close()
54
55                         nixio.exec("/bin/sh", "/sbin/luci-reload", unpack(services))
56                 else
57                         luci.http.write("OK")
58                         os.exit(0)
59                 end
60         end
61 end