signal_handler.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* $Id: signal_handler.c 1462 2006-04-13 05:10:27Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2004 Aaron Turner, Jeff Guttenfelder, Nathan Monteleone
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * Description:
  33. * This file contains routines relating to signals.
  34. *
  35. * Modifications:
  36. * 01/24/2003 Added suspend signal support.
  37. */
  38. #include "config.h"
  39. #include "defines.h"
  40. #include "common.h"
  41. #include <signal.h>
  42. #include <sys/time.h>
  43. #include <stdio.h>
  44. #include <sys/types.h>
  45. #include <unistd.h>
  46. #include <string.h>
  47. #include "tcpreplay.h"
  48. #include "signal_handler.h"
  49. struct timeval suspend_time;
  50. static struct timeval suspend_start;
  51. static struct timeval suspend_end;
  52. /*
  53. * init_signal_handlers -
  54. * Initialize signal handlers to be used in tcpreplay.
  55. */
  56. void
  57. init_signal_handlers()
  58. {
  59. signal(SIGUSR1, suspend_handler);
  60. signal(SIGCONT, continue_handler);
  61. reset_suspend_time();
  62. }
  63. /*
  64. * reset_suspend_time -
  65. * Reset time values for suspend signal.
  66. */
  67. void
  68. reset_suspend_time()
  69. {
  70. timerclear(&suspend_time);
  71. timerclear(&suspend_start);
  72. timerclear(&suspend_end);
  73. }
  74. /*
  75. * suspend signal handler -
  76. * Signal handler for signal SIGUSR1. SIGSTOP cannot be
  77. * caught, so SIGUSR1 is caught and it throws SIGSTOP.
  78. */
  79. void
  80. suspend_handler(int signo)
  81. {
  82. if (signo != SIGUSR1) {
  83. warnx("suspend_handler() got the wrong signal: %d", signo);
  84. return;
  85. }
  86. if (gettimeofday(&suspend_start, NULL) < 0)
  87. errx(1, "gettimeofday(): %s", strerror(errno));
  88. kill(getpid(), SIGSTOP);
  89. }
  90. /*
  91. * continue_handler -
  92. * Signal handler for continue signal.
  93. */
  94. void
  95. continue_handler(int signo)
  96. {
  97. struct timeval suspend_delta;
  98. if (signo != SIGCONT) {
  99. warnx("continue_handler() got the wrong signal: %d", signo);
  100. return;
  101. }
  102. if (gettimeofday(&suspend_end, NULL) < 0)
  103. errx(1, "gettimeofday(): %s", strerror(errno));
  104. timersub(&suspend_end, &suspend_start, &suspend_delta);
  105. timeradd(&suspend_time, &suspend_delta, &suspend_time);
  106. }
  107. /*
  108. Local Variables:
  109. mode:c
  110. indent-tabs-mode:nil
  111. c-basic-offset:4
  112. End:
  113. */