You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dedaemon.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env node
  2. var syscheck = require("./js/syscheck");
  3. var parseConf = require("./js/parse-conf");
  4. var async = require("./js/async");
  5. var udev = require("./udev");
  6. var execSync = require("child_process").execSync
  7. var fs = require("fs");
  8. var modules = {
  9. display: require("./modules/display"),
  10. input: require("./modules/input"),
  11. wallpaper: require("./modules/wallpaper"),
  12. process: require("./modules/process"),
  13. };
  14. if (!process.argv[2]) {
  15. console.error("Usage:", process.argv[1], "<config file>");
  16. console.error(" ", process.argv[1], "list -- List display and input devices");
  17. console.error(" ", process.argv[1], "reload -- Reload the running daemon");
  18. process.exit(1);
  19. }
  20. var config;
  21. var fatalError = false;
  22. function createLogger(name) {
  23. function log(pre, msg) {
  24. var str = pre+msg.join(" ");
  25. console.error(str);
  26. try {
  27. fs.appendFileSync(config.general.log, str+"\n");
  28. } catch (err) {
  29. if (!fatalError) {
  30. fatalError = true;
  31. console.error("FATAL: Failed to write to log file!!");
  32. console.error(err.toString());
  33. stopAll(() => {
  34. process.exit(1);
  35. });
  36. }
  37. };
  38. }
  39. return {
  40. info: (...msg) => log(name+": INFO: ", msg),
  41. warn: (...msg) => log(name+": WARNING: ", msg),
  42. error: (...msg) => log(name+": ERROR: ", msg),
  43. }
  44. }
  45. var logger = createLogger("dedaemon");
  46. function startAll() {
  47. try {
  48. fs.renameSync(config.general.log, config.general.log+".old");
  49. } catch (err) {}
  50. fs.writeFileSync(config.general.log, "");
  51. Object.keys(modules).forEach(i => {
  52. var mod = modules[i];
  53. var conf = config[i];
  54. if (conf instanceof Array && conf.length === 0)
  55. return;
  56. mod.start(conf, createLogger(i), modules);
  57. });
  58. }
  59. function stopAll(cb) {
  60. var keys = Object.keys(modules);
  61. var next = async(keys.length, cb);
  62. keys.forEach(i => modules[i].stop(next));
  63. }
  64. function onTerm() {
  65. logger.info("Exiting...");
  66. stopAll(() => {
  67. udev.exit();
  68. process.exit(1)
  69. });
  70. }
  71. function reload() {
  72. try {
  73. config = parseConf(process.argv[2]);
  74. } catch (err) {
  75. logger.error(
  76. "Tried to reload, but parsing the config file failed:",
  77. err.toString());
  78. return;
  79. }
  80. logger.info("Reloading.");
  81. stopAll(() => {
  82. startAll();
  83. });
  84. }
  85. function loglol(sig) {
  86. return function() {
  87. logger.info("Received signal", sig);
  88. }
  89. }
  90. function killRunningDaemon(signal, once) {
  91. var cmd =
  92. "pgrep -a node | "+
  93. "grep dedaemon | "+
  94. "grep -ve stop -e reload | "+
  95. "cut -d' ' -f1";
  96. var out = execSync(cmd)
  97. .toString();
  98. var lines = out.split("\n").filter(l => l !== "");
  99. if (lines.length > 1 && once) {
  100. console.error("There are multiple dedaemon processes running.");
  101. process.exit(1);
  102. }
  103. if (lines.length === 0) {
  104. console.error("No dedaemon process is running.");
  105. process.exit(1);
  106. }
  107. lines.forEach(line => {
  108. execSync("kill -s "+signal+" "+line);
  109. console.error("Sent", "SIG"+signal, "to process", line);
  110. });
  111. process.exit(0);
  112. }
  113. if (process.argv[2] === "list") {
  114. console.error("display:");
  115. modules.display.list(() => {
  116. console.error("input:");
  117. modules.input.list(() => {
  118. udev.exit();
  119. process.exit(0);
  120. });
  121. });
  122. } else if (process.argv[2] === "reload") {
  123. killRunningDaemon("USR1", true);
  124. } else if (process.argv[2] === "stop") {
  125. killRunningDaemon("TERM");
  126. } else {
  127. var config = parseConf(process.argv[2]);
  128. syscheck(ok => {
  129. if (ok)
  130. setTimeout(startAll, 1000);
  131. else
  132. console.error("Missing binaries, exiting.");
  133. });
  134. process.on("SIGTERM", onTerm);
  135. process.on("SIGINT", onTerm);
  136. process.on("SIGUSR1", reload);
  137. }