proc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #include "portab.h"
  12. /**
  13. * @file
  14. * Process management
  15. */
  16. #include "imp.h"
  17. #include <assert.h>
  18. #include <errno.h>
  19. #include <signal.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include "log.h"
  24. #include "io.h"
  25. #include "conn.h"
  26. #include "exp.h"
  27. #include "sighandlers.h"
  28. #include "proc.h"
  29. /**
  30. * Initialize process structure.
  31. */
  32. GLOBAL void
  33. Proc_InitStruct (PROC_STAT *proc)
  34. {
  35. assert(proc != NULL);
  36. proc->pid = 0;
  37. proc->pipe_fd = -1;
  38. }
  39. /**
  40. * Fork a child process.
  41. */
  42. GLOBAL pid_t
  43. Proc_Fork(PROC_STAT *proc, int *pipefds, void (*cbfunc)(int, short), int timeout)
  44. {
  45. pid_t pid;
  46. #ifndef HAVE_ARC4RANDOM
  47. unsigned int seed;
  48. #endif
  49. assert(proc != NULL);
  50. assert(pipefds != NULL);
  51. assert(cbfunc != NULL);
  52. if (pipe(pipefds) != 0) {
  53. Log(LOG_ALERT, "Can't create output pipe for child process: %s!",
  54. strerror(errno));
  55. return -1;
  56. }
  57. #ifndef HAVE_ARC4RANDOM
  58. seed = (unsigned int)rand();
  59. #endif
  60. pid = fork();
  61. switch (pid) {
  62. case -1:
  63. /* Error on fork: */
  64. Log(LOG_CRIT, "Can't fork child process: %s!", strerror(errno));
  65. close(pipefds[0]);
  66. close(pipefds[1]);
  67. return -1;
  68. case 0:
  69. /* New child process: */
  70. #ifdef HAVE_ARC4RANDOM_STIR
  71. arc4random_stir();
  72. #endif
  73. #ifndef HAVE_ARC4RANDOM
  74. srand(seed ^ (unsigned int)time(NULL) ^ getpid());
  75. #endif
  76. Signals_Exit();
  77. signal(SIGTERM, Proc_GenericSignalHandler);
  78. signal(SIGALRM, Proc_GenericSignalHandler);
  79. close(pipefds[0]);
  80. alarm(timeout);
  81. return 0;
  82. }
  83. /* Old parent process: */
  84. close(pipefds[1]);
  85. if (!io_setnonblock(pipefds[0])
  86. || !io_event_create(pipefds[0], IO_WANTREAD, cbfunc)) {
  87. Log(LOG_CRIT, "Can't register callback for child process: %s!",
  88. strerror(errno));
  89. close(pipefds[0]);
  90. return -1;
  91. }
  92. proc->pid = pid;
  93. proc->pipe_fd = pipefds[0];
  94. return pid;
  95. }
  96. /**
  97. * Generic signal handler for forked child processes.
  98. */
  99. GLOBAL void
  100. Proc_GenericSignalHandler(int Signal)
  101. {
  102. switch(Signal) {
  103. case SIGTERM:
  104. #ifdef DEBUG
  105. Log_Subprocess(LOG_DEBUG, "Child got TERM signal, exiting.");
  106. #endif
  107. exit(1);
  108. case SIGALRM:
  109. #ifdef DEBUG
  110. Log_Subprocess(LOG_DEBUG, "Child got ALARM signal, exiting.");
  111. #endif
  112. exit(1);
  113. }
  114. }
  115. /**
  116. * Read bytes from a pipe of a forked child process.
  117. * In addition, this function makes sure that the child process is ignored
  118. * after all data has been read or a fatal error occurred.
  119. */
  120. GLOBAL size_t
  121. Proc_Read(PROC_STAT *proc, void *buffer, size_t buflen)
  122. {
  123. ssize_t bytes_read = 0;
  124. assert(buffer != NULL);
  125. assert(buflen > 0);
  126. bytes_read = read(proc->pipe_fd, buffer, buflen);
  127. if (bytes_read < 0) {
  128. if (errno == EAGAIN)
  129. return 0;
  130. Log(LOG_CRIT, "Can't read from child process %ld: %s",
  131. proc->pid, strerror(errno));
  132. Proc_Close(proc);
  133. bytes_read = 0;
  134. } else if (bytes_read == 0) {
  135. /* EOF: clean up */
  136. LogDebug("Child process %ld: EOF reached, closing pipe.",
  137. proc->pid);
  138. Proc_Close(proc);
  139. }
  140. return (size_t)bytes_read;
  141. }
  142. /**
  143. * Close pipe to a forked child process.
  144. */
  145. GLOBAL void
  146. Proc_Close(PROC_STAT *proc)
  147. {
  148. /* Close socket, if it exists */
  149. if (proc->pipe_fd >= 0)
  150. io_close(proc->pipe_fd);
  151. Proc_InitStruct(proc);
  152. }
  153. /* -eof- */