Operating system for OpenComputers
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

86 satır
2.3KB

  1. local serial = require "serialization"
  2. local rc = {}
  3. rc.pids = {}
  4. local service = {}
  5. local cfg = {}
  6. cfg.enabled = {"getty","minitel"}
  7. local function loadConfig()
  8. local f = io.open("/boot/cfg/rc.cfg","rb")
  9. if not f then return false end
  10. cfg = serial.unserialize(f:read("*a")) or {}
  11. f:close()
  12. cfg.enabled = cfg.enabled or {}
  13. return true
  14. end
  15. local function saveConfig()
  16. local f = io.open("/boot/cfg/rc.cfg","wb")
  17. if not f then return false end
  18. f:write(serial.serialize(cfg))
  19. f:close()
  20. return true
  21. end
  22. function rc.load(name,force) -- string boolean -- table -- Attempts to load service *name*, and if *force* is true, replaces the current instance.
  23. if force then
  24. rc.stop(name)
  25. service[name] = nil
  26. end
  27. if service[name] then return true end
  28. service[name] = setmetatable({},{__index=_G})
  29. local f = io.open("/boot/service/"..name..".lua","rb")
  30. local res = load(f:read("*a"),name,"t",service[name])()
  31. f:close()
  32. return res
  33. end
  34. function rc.stop(name,...) -- string -- boolean string -- Stops service *name*, supplying *...* to the stop function. Returns false and a reason if this fails.
  35. if not service[name] then return false, "service not found" end
  36. if service[name].stop then
  37. service[name].stop(...)
  38. coroutine.yield()
  39. end
  40. if rc.pids[name] then
  41. os.kill(rc.pids[name])
  42. end
  43. rc.pids[name] = nil
  44. end
  45. function rc.start(name,...) -- string -- boolean string -- Stops service *name*, supplying *...* to the stop function. Returns false and a reason if this fails.
  46. rc.load(name)
  47. if not service[name] then return false, "service not found" end
  48. local rv = {service[name].start(...)}
  49. if type(rv[1]) == "number" then
  50. rc.pids[name] = rv[1]
  51. end
  52. end
  53. function rc.restart(name) -- string -- -- Restarts service *name* using rc.stop and rc.start.
  54. rc.stop(name)
  55. rc.start(name)
  56. end
  57. function rc.enable(name) -- string -- -- Enables service *name* being started on startup.
  58. for k,v in pairs(cfg.enabled) do
  59. if v == name then return false end
  60. end
  61. cfg.enabled[#cfg.enabled+1] = name
  62. saveConfig()
  63. end
  64. function rc.disable(name) -- string -- -- Disables service *name* being started on startup.
  65. local disabled = false
  66. for k,v in pairs(cfg.enabled) do
  67. if v == name then table.remove(cfg.enabled,k) disabled = true break end
  68. end
  69. saveConfig()
  70. return disabled
  71. end
  72. loadConfig()
  73. _G.service = service
  74. rc.cfg = cfg
  75. return rc