procsup.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "fspr_arch_threadproc.h"
  17. fspr_status_t fspr_proc_detach(int daemonize)
  18. {
  19. #if 0
  20. int x;
  21. pid_t pgrp;
  22. chdir("/");
  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 ((x = fork()) > 0)
  27. exit(0);
  28. else if (x == -1) {
  29. perror("fork");
  30. fprintf(stderr, "unable to fork new process\n");
  31. exit(1); /* we can't do anything here, so just exit. */
  32. }
  33. /* RAISE_SIGSTOP(DETACH);*/
  34. #endif
  35. #if APR_HAVE_SETSID
  36. if ((pgrp = setsid()) == -1) {
  37. return errno;
  38. }
  39. #elif defined(NEXT) || defined(NEWSOS)
  40. if (setpgrp(0, getpid()) == -1 || (pgrp = getpgrp(0)) == -1) {
  41. return errno;
  42. }
  43. #elif defined(OS2) || defined(TPF)
  44. /* OS/2 don't support process group IDs */
  45. pgrp = getpid();
  46. #elif defined(MPE)
  47. /* MPE uses negative pid for process group */
  48. pgrp = -getpid();
  49. #else
  50. if ((pgrp = setpgid(0, 0)) == -1) {
  51. return errno;
  52. }
  53. #endif
  54. /* close out the standard file descriptors */
  55. if (freopen("/dev/null", "r", stdin) == NULL) {
  56. return errno;
  57. /* continue anyhow -- note we can't close out descriptor 0 because we
  58. * have nothing to replace it with, and if we didn't have a descriptor
  59. * 0 the next file would be created with that value ... leading to
  60. * havoc.
  61. */
  62. }
  63. if (freopen("/dev/null", "w", stdout) == NULL) {
  64. return errno;
  65. }
  66. /* We are going to reopen this again in a little while to the error
  67. * log file, but better to do it twice and suffer a small performance
  68. * hit for consistancy than not reopen it here.
  69. */
  70. if (freopen("/dev/null", "w", stderr) == NULL) {
  71. return errno;
  72. }
  73. #endif
  74. return APR_SUCCESS;
  75. }
  76. #if 0
  77. #if (!HAVE_WAITPID)
  78. /* From ikluft@amdahl.com
  79. * this is not ideal but it works for SVR3 variants
  80. * Modified by dwd@bell-labs.com to call wait3 instead of wait because
  81. * apache started to use the WNOHANG option.
  82. */
  83. int waitpid(pid_t pid, int *statusp, int options)
  84. {
  85. int tmp_pid;
  86. if (kill(pid, 0) == -1) {
  87. errno = ECHILD;
  88. return -1;
  89. }
  90. while (((tmp_pid = wait3(statusp, options, 0)) != pid) &&
  91. (tmp_pid != -1) && (tmp_pid != 0) && (pid != -1))
  92. ;
  93. return tmp_pid;
  94. }
  95. #endif
  96. #endif