proc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
  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. Conn_CloseAllSockets();
  73. return 0;
  74. }
  75. /* Old parent process: */
  76. close(pipefds[1]);
  77. if (!io_setnonblock(pipefds[0])
  78. || !io_event_create(pipefds[0], IO_WANTREAD, cbfunc)) {
  79. Log(LOG_CRIT, "Can't register callback for child process: %s!",
  80. strerror(errno));
  81. close(pipefds[0]);
  82. return -1;
  83. }
  84. proc->pid = pid;
  85. proc->pipe_fd = pipefds[0];
  86. return pid;
  87. }
  88. /**
  89. * Generic signal handler for forked child processes.
  90. */
  91. GLOBAL void
  92. Proc_GenericSignalHandler(int Signal)
  93. {
  94. switch(Signal) {
  95. case SIGTERM:
  96. #ifdef DEBUG
  97. Log_Subprocess(LOG_DEBUG, "Child got TERM signal, exiting.");
  98. #endif
  99. exit(1);
  100. case SIGALRM:
  101. #ifdef DEBUG
  102. Log_Subprocess(LOG_DEBUG, "Child got ALARM signal, exiting.");
  103. #endif
  104. exit(1);
  105. }
  106. }
  107. /**
  108. * Read bytes from a pipe of a forked child process.
  109. * In addition, this function makes sure that the child process is ignored
  110. * after all data has been read or a fatal error occurred.
  111. */
  112. GLOBAL size_t
  113. Proc_Read(PROC_STAT *proc, void *buffer, size_t buflen)
  114. {
  115. ssize_t bytes_read = 0;
  116. assert(buffer != NULL);
  117. assert(buflen > 0);
  118. bytes_read = read(proc->pipe_fd, buffer, buflen);
  119. if (bytes_read < 0) {
  120. if (errno == EAGAIN)
  121. return 0;
  122. Log(LOG_CRIT, "Can't read from child process %ld: %s",
  123. proc->pid, strerror(errno));
  124. bytes_read = 0;
  125. }
  126. #if DEBUG
  127. else if (bytes_read == 0)
  128. LogDebug("Can't read from child process %ld: EOF", proc->pid);
  129. #endif
  130. Proc_InitStruct(proc);
  131. return (size_t)bytes_read;
  132. }
  133. /* -eof- */