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.

148 lines
4.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2009, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/posix.h"
  28. #include <sys/wait.h>
  29. #include <errno.h>
  30. #include <unistd.h>
  31. #ifdef LINUX
  32. #include "talk/base/linuxfdwalk.h"
  33. #endif
  34. #include "talk/base/logging.h"
  35. namespace talk_base {
  36. #ifdef LINUX
  37. static void closefds(void *close_errors, int fd) {
  38. if (fd <= 2) {
  39. // We leave stdin/out/err open to the browser's terminal, if any.
  40. return;
  41. }
  42. if (close(fd) < 0) {
  43. *static_cast<bool *>(close_errors) = true;
  44. }
  45. }
  46. #endif
  47. enum {
  48. EXIT_FLAG_CHDIR_ERRORS = 1 << 0,
  49. #ifdef LINUX
  50. EXIT_FLAG_FDWALK_ERRORS = 1 << 1,
  51. EXIT_FLAG_CLOSE_ERRORS = 1 << 2,
  52. #endif
  53. EXIT_FLAG_SECOND_FORK_FAILED = 1 << 3,
  54. };
  55. bool RunAsDaemon(const char *file, const char *const argv[]) {
  56. // Fork intermediate child to daemonize.
  57. pid_t pid = fork();
  58. if (pid < 0) {
  59. LOG_ERR(LS_ERROR) << "fork()";
  60. return false;
  61. } else if (!pid) {
  62. // Child.
  63. // We try to close all fds and change directory to /, but if that fails we
  64. // keep going because it's not critical.
  65. int exit_code = 0;
  66. if (chdir("/") < 0) {
  67. exit_code |= EXIT_FLAG_CHDIR_ERRORS;
  68. }
  69. #ifdef LINUX
  70. bool close_errors = false;
  71. if (fdwalk(&closefds, &close_errors) < 0) {
  72. exit_code |= EXIT_FLAG_FDWALK_ERRORS;
  73. }
  74. if (close_errors) {
  75. exit_code |= EXIT_FLAG_CLOSE_ERRORS;
  76. }
  77. #endif
  78. // Fork again to become a daemon.
  79. pid = fork();
  80. // It is important that everything here use _exit() and not exit(), because
  81. // exit() would call the destructors of all global variables in the whole
  82. // process, which is both unnecessary and unsafe.
  83. if (pid < 0) {
  84. exit_code |= EXIT_FLAG_SECOND_FORK_FAILED;
  85. _exit(exit_code); // if second fork failed
  86. } else if (!pid) {
  87. // Child.
  88. // Successfully daemonized. Run command.
  89. // POSIX requires the args to be typed as non-const for historical
  90. // reasons, but it mandates that the actual implementation be const, so
  91. // the cast is safe.
  92. execvp(file, const_cast<char *const *>(argv));
  93. _exit(255); // if execvp failed
  94. }
  95. // Parent.
  96. // Successfully spawned process, but report any problems to the parent where
  97. // we can log them.
  98. _exit(exit_code);
  99. }
  100. // Parent. Reap intermediate child.
  101. int status;
  102. pid_t child = waitpid(pid, &status, 0);
  103. if (child < 0) {
  104. LOG_ERR(LS_ERROR) << "Error in waitpid()";
  105. return false;
  106. }
  107. if (child != pid) {
  108. // Should never happen (see man page).
  109. LOG(LS_ERROR) << "waitpid() chose wrong child???";
  110. return false;
  111. }
  112. if (!WIFEXITED(status)) {
  113. LOG(LS_ERROR) << "Intermediate child killed uncleanly"; // Probably crashed
  114. return false;
  115. }
  116. int exit_code = WEXITSTATUS(status);
  117. if (exit_code & EXIT_FLAG_CHDIR_ERRORS) {
  118. LOG(LS_WARNING) << "Child reported probles calling chdir()";
  119. }
  120. #ifdef LINUX
  121. if (exit_code & EXIT_FLAG_FDWALK_ERRORS) {
  122. LOG(LS_WARNING) << "Child reported problems calling fdwalk()";
  123. }
  124. if (exit_code & EXIT_FLAG_CLOSE_ERRORS) {
  125. LOG(LS_WARNING) << "Child reported problems calling close()";
  126. }
  127. #endif
  128. if (exit_code & EXIT_FLAG_SECOND_FORK_FAILED) {
  129. LOG(LS_ERROR) << "Failed to daemonize";
  130. // This means the command was not launched, so failure.
  131. return false;
  132. }
  133. return true;
  134. }
  135. } // namespace talk_base