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.

service.cpp 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // \file service.cpp
  2. //
  3. // Copyright (C) 2014 MicroNeil Research Corporation.
  4. //
  5. // This program is part of the MicroNeil Research Open Library Project. For
  6. // more information go to http://www.microneil.com/OpenLibrary/index.html
  7. //
  8. // This program is free software; you can redistribute it and/or modify it
  9. // under the terms of the GNU General Public License as published by the
  10. // Free Software Foundation; either version 2 of the License, or (at your
  11. // option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful, but WITHOUT
  14. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. // more details.
  17. //
  18. // You should have received a copy of the GNU General Public License along with
  19. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. // Place, Suite 330, Boston, MA 02111-1307 USA
  21. //==============================================================================
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <signal.h>
  27. #include <iostream>
  28. #include <cstdio>
  29. #include <cstdlib>
  30. #include <thread>
  31. #include <chrono>
  32. #include "service.hpp"
  33. // Main program for *nix daemon.
  34. int main(int argc, char *argv[]) {
  35. CodeDweller::Service &service = CodeDweller::Service::getInstance();
  36. return service.main(argc, argv);
  37. }
  38. namespace CodeDweller {
  39. std::mutex Service::objectMutex;
  40. Service::Service() :
  41. pauseReceived(false),
  42. resumeReceived(false),
  43. restartReceived(false),
  44. stopReceived(false) {
  45. }
  46. Service &Service::getInstance() {
  47. std::lock_guard<std::mutex> scopeMutex(objectMutex);
  48. static Service service;
  49. return service;
  50. }
  51. int Service::main(int argc, char *argv[]) {
  52. // Load the arguments.
  53. loadArguments(argc, argv);
  54. pid_t pid;
  55. // Fork the process.
  56. pid = fork();
  57. if (pid < 0) {
  58. perror("Error from first fork");
  59. return(EXIT_FAILURE);
  60. }
  61. // Terminate the parent.
  62. if (pid > 0) {
  63. return(EXIT_SUCCESS);
  64. }
  65. // This is the child. Become the session leader.
  66. if (setsid() < 0) {
  67. perror("Error from setsid");
  68. return(EXIT_FAILURE);
  69. }
  70. // Fork again.
  71. pid = fork();
  72. if (pid < 0) {
  73. perror("Error from second fork");
  74. return(EXIT_FAILURE);
  75. }
  76. // Terminate the parent.
  77. if (pid > 0) {
  78. return(EXIT_SUCCESS);
  79. }
  80. // Set default file permissions. This call always succeeds.
  81. umask(0);
  82. // Disassociate the process from the default directory of the
  83. // grandparent.
  84. if (chdir("/") != 0) {
  85. perror("Error from chdir");
  86. return(EXIT_FAILURE);
  87. }
  88. // Initialize the set of signals to wait for.
  89. if (sigemptyset(&signalSet) != 0) {
  90. perror("Error from sigemptyset");
  91. return(EXIT_FAILURE);
  92. }
  93. if ((sigaddset(&signalSet, SIGTSTP) != 0) ||
  94. (sigaddset(&signalSet, SIGCONT) != 0) ||
  95. (sigaddset(&signalSet, SIGHUP) != 0) ||
  96. (sigaddset(&signalSet, SIGTERM) != 0)) {
  97. perror("Error from sigaddset");
  98. return(EXIT_FAILURE);
  99. }
  100. // Block the signals.
  101. if (sigprocmask(SIG_BLOCK, &signalSet, NULL) != 0) {
  102. perror("Error from sigprocmask");
  103. return(EXIT_FAILURE);
  104. }
  105. // Close all open file descriptors.
  106. for (int fd = 2; fd >= 0; fd--) {
  107. if (close(fd) != 0) {
  108. // There is no controlling terminal to send any message to.
  109. return(EXIT_FAILURE);
  110. }
  111. }
  112. // Connect standard I/O to the null device.
  113. int fd;
  114. // stdin.
  115. fd = open("/dev/null", O_RDONLY);
  116. if (fd < 0) {
  117. return(EXIT_FAILURE);
  118. }
  119. // stdout.
  120. fd = open("/dev/null", O_WRONLY);
  121. if (fd < 0) {
  122. return(EXIT_FAILURE);
  123. }
  124. // stderr.
  125. fd = open("/dev/null", O_WRONLY);
  126. if (fd < 0) {
  127. return(EXIT_FAILURE);
  128. }
  129. // Start the thread to process messages.
  130. std::thread messageThread(&Service::processMessages, this);
  131. // Run the service.
  132. int status;
  133. status = run();
  134. // Send a Stop message so that messageThread exits.
  135. pthread_kill(messageThread.native_handle(), SIGTERM);
  136. messageThread.join();
  137. return(status);
  138. }
  139. void Service::loadArguments(int argc, char *argv[]) {
  140. for (int i = 0; i < argc; i++) {
  141. cmdLineArgs.push_back(argv[i]);
  142. }
  143. }
  144. const std::vector<std::string> &Service::arguments() {
  145. return cmdLineArgs;
  146. }
  147. void Service::onPauseCall(Callback &pauseFunctor) {
  148. std::lock_guard<std::mutex> scopeMutex(objectMutex);
  149. pauseCallbacks.push_back(&pauseFunctor);
  150. }
  151. void Service::onResumeCall(Callback &resumeFunctor) {
  152. std::lock_guard<std::mutex> scopeMutex(objectMutex);
  153. resumeCallbacks.push_back(&resumeFunctor);
  154. }
  155. void Service::onRestartCall(Callback &restartFunctor) {
  156. std::lock_guard<std::mutex> scopeMutex(objectMutex);
  157. restartCallbacks.push_back(&restartFunctor);
  158. }
  159. void Service::onStopCall(Callback &stopFunctor) {
  160. std::lock_guard<std::mutex> scopeMutex(objectMutex);
  161. stopCallbacks.push_back(&stopFunctor);
  162. }
  163. bool Service::receivedPause() {
  164. return (pauseReceived);
  165. }
  166. bool Service::receivedResume() {
  167. return (resumeReceived);
  168. }
  169. bool Service::receivedRestart() {
  170. return (restartReceived);
  171. }
  172. bool Service::receivedStop() {
  173. return (stopReceived);
  174. }
  175. void Service::clearReceivedPause() {
  176. pauseReceived = false;
  177. }
  178. void Service::clearReceivedResume() {
  179. resumeReceived = false;
  180. }
  181. void Service::clearReceivedRestart() {
  182. restartReceived = false;
  183. }
  184. void Service::clearReceivedStop() {
  185. stopReceived = false;
  186. }
  187. void Service::processMessages() {
  188. int sigNum;
  189. int status;
  190. while (true) {
  191. // Wait for a signal.
  192. status = sigwait(&signalSet, &sigNum);
  193. if (0 != status) {
  194. perror("Error from sigwait");
  195. // TODO: Implement recovery.
  196. ;
  197. }
  198. // Process the message.
  199. switch (sigNum) {
  200. case SIGTSTP:
  201. pauseReceived = true;
  202. for (auto callback : pauseCallbacks) {
  203. (*callback)();
  204. }
  205. break;
  206. case SIGCONT:
  207. resumeReceived = true;
  208. for (auto callback : resumeCallbacks) {
  209. (*callback)();
  210. }
  211. break;
  212. case SIGHUP:
  213. restartReceived = true;
  214. for (auto callback : restartCallbacks) {
  215. (*callback)();
  216. }
  217. break;
  218. case SIGTERM:
  219. stopReceived = true;
  220. for (auto callback : stopCallbacks) {
  221. (*callback)();
  222. }
  223. // Exit.
  224. return;
  225. break;
  226. default:
  227. ;
  228. } // switch.
  229. } // while.
  230. }
  231. }