posix_daemon_control.cpp 970 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. //
  2. // $Id$
  3. //
  4. #include<__vic/posix/daemon_control.h>
  5. #include<__vic/posix/process.h>
  6. namespace __vic { namespace posix {
  7. //----------------------------------------------------------------------------
  8. daemon_control::daemon_control(std::string pid_file_name)
  9. : pid_file(__VIC_STD_MOVE(pid_file_name))
  10. {
  11. try
  12. {
  13. pid_file.create();
  14. }
  15. catch(const pidfile::already_exists &)
  16. {
  17. throw already_running();
  18. }
  19. }
  20. //----------------------------------------------------------------------------
  21. void daemon_control::daemonize()
  22. {
  23. pid_file.prepare_to_daemon();
  24. posix::daemon(true);
  25. pid_file.rewrite_pid();
  26. }
  27. //----------------------------------------------------------------------------
  28. const char *daemon_control::already_running::what() const noexcept
  29. {
  30. return "PID-file already exists. Stop (Another instance is running?)";
  31. }
  32. //----------------------------------------------------------------------------
  33. }} // namespace