servicectl.lua 1.1 KB

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