PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/boost_1_57_0/libs/asio/example/cpp03/fork/daemon.cpp

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