daemon.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // daemon.cpp
  3. // ~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #include <boost/asio/io_context.hpp>
  11. #include <boost/asio/ip/udp.hpp>
  12. #include <boost/asio/signal_set.hpp>
  13. #include <array>
  14. #include <ctime>
  15. #include <iostream>
  16. #include <syslog.h>
  17. #include <unistd.h>
  18. using boost::asio::ip::udp;
  19. class udp_daytime_server
  20. {
  21. public:
  22. udp_daytime_server(boost::asio::io_context& io_context)
  23. : socket_(io_context, {udp::v4(), 13})
  24. {
  25. receive();
  26. }
  27. private:
  28. void receive()
  29. {
  30. socket_.async_receive_from(
  31. boost::asio::buffer(recv_buffer_), remote_endpoint_,
  32. [this](boost::system::error_code ec, std::size_t /*n*/)
  33. {
  34. if (!ec)
  35. {
  36. using namespace std; // For time_t, time and ctime;
  37. time_t now = time(0);
  38. std::string message = ctime(&now);
  39. boost::system::error_code ignored_ec;
  40. socket_.send_to(boost::asio::buffer(message),
  41. remote_endpoint_, 0, ignored_ec);
  42. }
  43. receive();
  44. });
  45. }
  46. udp::socket socket_;
  47. udp::endpoint remote_endpoint_;
  48. std::array<char, 1> recv_buffer_;
  49. };
  50. int main()
  51. {
  52. try
  53. {
  54. boost::asio::io_context io_context;
  55. // Initialise the server before becoming a daemon. If the process is
  56. // started from a shell, this means any errors will be reported back to the
  57. // user.
  58. udp_daytime_server server(io_context);
  59. // Register signal handlers so that the daemon may be shut down. You may
  60. // also want to register for other signals, such as SIGHUP to trigger a
  61. // re-read of a configuration file.
  62. boost::asio::signal_set signals(io_context, SIGINT, SIGTERM);
  63. signals.async_wait(
  64. [&](boost::system::error_code /*ec*/, int /*signo*/)
  65. {
  66. io_context.stop();
  67. });
  68. // Inform the io_context that we are about to become a daemon. The
  69. // io_context cleans up any internal resources, such as threads, that may
  70. // interfere with forking.
  71. io_context.notify_fork(boost::asio::io_context::fork_prepare);
  72. // Fork the process and have the parent exit. If the process was started
  73. // from a shell, this returns control to the user. Forking a new process is
  74. // also a prerequisite for the subsequent call to setsid().
  75. if (pid_t pid = fork())
  76. {
  77. if (pid > 0)
  78. {
  79. // We're in the parent process and need to exit.
  80. //
  81. // When the exit() function is used, the program terminates without
  82. // invoking local variables' destructors. Only global variables are
  83. // destroyed. As the io_context object is a local variable, this means
  84. // we do not have to call:
  85. //
  86. // io_context.notify_fork(boost::asio::io_context::fork_parent);
  87. //
  88. // However, this line should be added before each call to exit() if
  89. // using a global io_context object. An additional call:
  90. //
  91. // io_context.notify_fork(boost::asio::io_context::fork_prepare);
  92. //
  93. // should also precede the second fork().
  94. exit(0);
  95. }
  96. else
  97. {
  98. syslog(LOG_ERR | LOG_USER, "First fork failed: %m");
  99. return 1;
  100. }
  101. }
  102. // Make the process a new session leader. This detaches it from the
  103. // terminal.
  104. setsid();
  105. // A process inherits its working directory from its parent. This could be
  106. // on a mounted filesystem, which means that the running daemon would
  107. // prevent this filesystem from being unmounted. Changing to the root
  108. // directory avoids this problem.
  109. chdir("/");
  110. // The file mode creation mask is also inherited from the parent process.
  111. // We don't want to restrict the permissions on files created by the
  112. // daemon, so the mask is cleared.
  113. umask(0);
  114. // A second fork ensures the process cannot acquire a controlling terminal.
  115. if (pid_t pid = fork())
  116. {
  117. if (pid > 0)
  118. {
  119. exit(0);
  120. }
  121. else
  122. {
  123. syslog(LOG_ERR | LOG_USER, "Second fork failed: %m");
  124. return 1;
  125. }
  126. }
  127. // Close the standard streams. This decouples the daemon from the terminal
  128. // that started it.
  129. close(0);
  130. close(1);
  131. close(2);
  132. // We don't want the daemon to have any standard input.
  133. if (open("/dev/null", O_RDONLY) < 0)
  134. {
  135. syslog(LOG_ERR | LOG_USER, "Unable to open /dev/null: %m");
  136. return 1;
  137. }
  138. // Send standard output to a log file.
  139. const char* output = "/tmp/asio.daemon.out";
  140. const int flags = O_WRONLY | O_CREAT | O_APPEND;
  141. const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  142. if (open(output, flags, mode) < 0)
  143. {
  144. syslog(LOG_ERR | LOG_USER, "Unable to open output file %s: %m", output);
  145. return 1;
  146. }
  147. // Also send standard error to the same log file.
  148. if (dup(1) < 0)
  149. {
  150. syslog(LOG_ERR | LOG_USER, "Unable to dup output descriptor: %m");
  151. return 1;
  152. }
  153. // Inform the io_context that we have finished becoming a daemon. The
  154. // io_context uses this opportunity to create any internal file descriptors
  155. // that need to be private to the new process.
  156. io_context.notify_fork(boost::asio::io_context::fork_child);
  157. // The io_context can now be used normally.
  158. syslog(LOG_INFO | LOG_USER, "Daemon started");
  159. io_context.run();
  160. syslog(LOG_INFO | LOG_USER, "Daemon stopped");
  161. }
  162. catch (std::exception& e)
  163. {
  164. syslog(LOG_ERR | LOG_USER, "Exception: %s", e.what());
  165. std::cerr << "Exception: " << e.what() << std::endl;
  166. }
  167. }