proc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. unsigned int seed;
  47. assert(proc != NULL);
  48. assert(pipefds != NULL);
  49. assert(cbfunc != NULL);
  50. if (pipe(pipefds) != 0) {
  51. Log(LOG_ALERT, "Can't create output pipe for child process: %s!",
  52. strerror(errno));
  53. return -1;
  54. }
  55. seed = (unsigned int)rand();
  56. pid = fork();
  57. switch (pid) {
  58. case -1:
  59. /* Error on fork: */
  60. Log(LOG_CRIT, "Can't fork child process: %s!", strerror(errno));
  61. close(pipefds[0]);
  62. close(pipefds[1]);
  63. return -1;
  64. case 0:
  65. /* New child process: */
  66. srand(seed ^ (unsigned int)time(NULL) ^ getpid());
  67. Signals_Exit();
  68. signal(SIGTERM, Proc_GenericSignalHandler);
  69. signal(SIGALRM, Proc_GenericSignalHandler);
  70. close(pipefds[0]);
  71. alarm(timeout);
  72. return 0;
  73. }
  74. /* Old parent process: */
  75. close(pipefds[1]);
  76. if (!io_setnonblock(pipefds[0])
  77. || !io_event_create(pipefds[0], IO_WANTREAD, cbfunc)) {
  78. Log(LOG_CRIT, "Can't register callback for child process: %s!",
  79. strerror(errno));
  80. close(pipefds[0]);
  81. return -1;
  82. }
  83. proc->pid = pid;
  84. proc->pipe_fd = pipefds[0];
  85. return pid;
  86. }
  87. /**
  88. * Generic signal handler for forked child processes.
  89. */
  90. GLOBAL void
  91. Proc_GenericSignalHandler(int Signal)
  92. {
  93. switch(Signal) {
  94. case SIGTERM:
  95. #ifdef DEBUG
  96. Log_Subprocess(LOG_DEBUG, "Child got TERM signal, exiting.");
  97. #endif
  98. exit(1);
  99. case SIGALRM:
  100. #ifdef DEBUG
  101. Log_Subprocess(LOG_DEBUG, "Child got ALARM signal, exiting.");
  102. #endif
  103. exit(1);
  104. }
  105. }
  106. /**
  107. * Read bytes from a pipe of a forked child process.
  108. * In addition, this function makes sure that the child process is ignored
  109. * after all data has been read or a fatal error occurred.
  110. */
  111. GLOBAL size_t
  112. Proc_Read(PROC_STAT *proc, void *buffer, size_t buflen)
  113. {
  114. ssize_t bytes_read = 0;
  115. assert(buffer != NULL);
  116. assert(buflen > 0);
  117. bytes_read = read(proc->pipe_fd, buffer, buflen);
  118. if (bytes_read < 0) {
  119. if (errno == EAGAIN)
  120. return 0;
  121. Log(LOG_CRIT, "Can't read from child process %ld: %s",
  122. proc->pid, strerror(errno));
  123. Proc_Close(proc);
  124. bytes_read = 0;
  125. } else if (bytes_read == 0) {
  126. /* EOF: clean up */
  127. LogDebug("Child process %ld: EOF reached, closing pipe.",
  128. proc->pid);
  129. Proc_Close(proc);
  130. }
  131. return (size_t)bytes_read;
  132. }
  133. /**
  134. * Close pipe to a forked child process.
  135. */
  136. GLOBAL void
  137. Proc_Close(PROC_STAT *proc)
  138. {
  139. /* Close socket, if it exists */
  140. if (proc->pipe_fd >= 0)
  141. io_close(proc->pipe_fd);
  142. Proc_InitStruct(proc);
  143. }
  144. /* -eof- */