2
0

procsup.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "apr_arch_threadproc.h"
  17. APR_DECLARE(apr_status_t) apr_proc_detach(int daemonize)
  18. {
  19. int x;
  20. if (chdir("/") == -1) {
  21. return errno;
  22. }
  23. #if !defined(MPE) && !defined(OS2) && !defined(TPF) && !defined(BEOS)
  24. /* Don't detach for MPE because child processes can't survive the death of
  25. * the parent. */
  26. if (daemonize) {
  27. if ((x = fork()) > 0) {
  28. exit(0);
  29. }
  30. else if (x == -1) {
  31. perror("fork");
  32. fprintf(stderr, "unable to fork new process\n");
  33. exit(1); /* we can't do anything here, so just exit. */
  34. }
  35. /* RAISE_SIGSTOP(DETACH); */
  36. }
  37. #endif
  38. #ifdef HAVE_SETSID
  39. /* A setsid() failure is not fatal if we didn't just fork().
  40. * The calling process may be the process group leader, in
  41. * which case setsid() will fail with EPERM.
  42. */
  43. if (setsid() == -1 && daemonize) {
  44. return errno;
  45. }
  46. #elif defined(NEXT) || defined(NEWSOS)
  47. if (setpgrp(0, getpid()) == -1) {
  48. return errno;
  49. }
  50. #elif defined(OS2) || defined(TPF) || defined(MPE)
  51. /* do nothing */
  52. #else
  53. if (setpgid(0, 0) == -1) {
  54. return errno;
  55. }
  56. #endif
  57. /* close out the standard file descriptors */
  58. if (freopen("/dev/null", "r", stdin) == NULL) {
  59. return errno;
  60. /* continue anyhow -- note we can't close out descriptor 0 because we
  61. * have nothing to replace it with, and if we didn't have a descriptor
  62. * 0 the next file would be created with that value ... leading to
  63. * havoc.
  64. */
  65. }
  66. if (freopen("/dev/null", "w", stdout) == NULL) {
  67. return errno;
  68. }
  69. /* We are going to reopen this again in a little while to the error
  70. * log file, but better to do it twice and suffer a small performance
  71. * hit for consistancy than not reopen it here.
  72. */
  73. if (freopen("/dev/null", "w", stderr) == NULL) {
  74. return errno;
  75. }
  76. return APR_SUCCESS;
  77. }
  78. #if (!HAVE_WAITPID)
  79. /* From ikluft@amdahl.com
  80. * this is not ideal but it works for SVR3 variants
  81. * Modified by dwd@bell-labs.com to call wait3 instead of wait because
  82. * apache started to use the WNOHANG option.
  83. */
  84. int waitpid(pid_t pid, int *statusp, int options)
  85. {
  86. int tmp_pid;
  87. if (kill(pid, 0) == -1) {
  88. errno = ECHILD;
  89. return -1;
  90. }
  91. while (((tmp_pid = wait3(statusp, options, 0)) != pid) &&
  92. (tmp_pid != -1) && (tmp_pid != 0) && (pid != -1))
  93. ;
  94. return tmp_pid;
  95. }
  96. #endif