3
0

signalpipe.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Signal pipe infrastructure. A reliable way of delivering signals.
  4. *
  5. * Russ Dill <Russ.Dill@asu.edu> December 2003
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include "common.h"
  22. #define READ_FD 3
  23. #define WRITE_FD 4
  24. static void signal_handler(int sig)
  25. {
  26. int sv = errno;
  27. unsigned char ch = sig; /* use char, avoid dealing with partial writes */
  28. if (write(WRITE_FD, &ch, 1) != 1)
  29. bb_simple_perror_msg("can't send signal");
  30. errno = sv;
  31. }
  32. /* Call this before doing anything else. Sets up the socket pair
  33. * and installs the signal handler */
  34. void FAST_FUNC udhcp_sp_setup(void)
  35. {
  36. struct fd_pair signal_pipe;
  37. /* All callers also want this, so... */
  38. bb_sanitize_stdio();
  39. /* was socketpair, but it needs AF_UNIX in kernel */
  40. xpiped_pair(signal_pipe);
  41. /* usually we get fds 3 and 4, but if we get higher ones... */
  42. if (signal_pipe.rd != READ_FD)
  43. xmove_fd(signal_pipe.rd, READ_FD);
  44. if (signal_pipe.wr != WRITE_FD)
  45. xmove_fd(signal_pipe.wr, WRITE_FD);
  46. close_on_exec_on(READ_FD);
  47. close_on_exec_on(WRITE_FD);
  48. ndelay_on(READ_FD);
  49. ndelay_on(WRITE_FD);
  50. bb_signals(0
  51. + (1 << SIGUSR1)
  52. + (1 << SIGUSR2)
  53. + (1 << SIGTERM)
  54. , signal_handler);
  55. }
  56. /* Quick little function to setup the pfds.
  57. * Limited in that you can only pass one extra fd.
  58. */
  59. void FAST_FUNC udhcp_sp_fd_set(struct pollfd *pfds, int extra_fd)
  60. {
  61. pfds[0].fd = READ_FD;
  62. pfds[0].events = POLLIN;
  63. pfds[1].fd = -1;
  64. if (extra_fd >= 0) {
  65. close_on_exec_on(extra_fd);
  66. pfds[1].fd = extra_fd;
  67. pfds[1].events = POLLIN;
  68. }
  69. /* this simplifies "is extra_fd ready?" tests elsewhere: */
  70. pfds[1].revents = 0;
  71. }
  72. /* Read a signal from the signal pipe. Returns 0 if there is
  73. * no signal, -1 on error (and sets errno appropriately), and
  74. * your signal on success */
  75. int FAST_FUNC udhcp_sp_read(void)
  76. {
  77. unsigned char sig;
  78. /* Can't block here, fd is in nonblocking mode */
  79. if (safe_read(READ_FD, &sig, 1) != 1)
  80. return 0;
  81. return sig;
  82. }