signal_handler.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* $Id: signal_handler.c 1757 2007-03-22 05:38:56Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2007 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 <errno.h>
  48. #include "tcpreplay.h"
  49. #include "signal_handler.h"
  50. struct timeval suspend_time;
  51. static struct timeval suspend_start;
  52. static struct timeval suspend_end;
  53. /*
  54. * init_signal_handlers -
  55. * Initialize signal handlers to be used in tcpreplay.
  56. */
  57. void
  58. init_signal_handlers()
  59. {
  60. signal(SIGUSR1, suspend_handler);
  61. signal(SIGCONT, continue_handler);
  62. reset_suspend_time();
  63. }
  64. /*
  65. * reset_suspend_time -
  66. * Reset time values for suspend signal.
  67. */
  68. void
  69. reset_suspend_time()
  70. {
  71. timerclear(&suspend_time);
  72. timerclear(&suspend_start);
  73. timerclear(&suspend_end);
  74. }
  75. /*
  76. * suspend signal handler -
  77. * Signal handler for signal SIGUSR1. SIGSTOP cannot be
  78. * caught, so SIGUSR1 is caught and it throws SIGSTOP.
  79. */
  80. void
  81. suspend_handler(int signo)
  82. {
  83. if (signo != SIGUSR1) {
  84. warnx("suspend_handler() got the wrong signal: %d", signo);
  85. return;
  86. }
  87. if (gettimeofday(&suspend_start, NULL) < 0)
  88. errx(1, "gettimeofday(): %s", strerror(errno));
  89. kill(getpid(), SIGSTOP);
  90. }
  91. /*
  92. * continue_handler -
  93. * Signal handler for continue signal.
  94. */
  95. void
  96. continue_handler(int signo)
  97. {
  98. struct timeval suspend_delta;
  99. if (signo != SIGCONT) {
  100. warnx("continue_handler() got the wrong signal: %d", signo);
  101. return;
  102. }
  103. if (gettimeofday(&suspend_end, NULL) < 0)
  104. errx(1, "gettimeofday(): %s", strerror(errno));
  105. timersub(&suspend_end, &suspend_start, &suspend_delta);
  106. timeradd(&suspend_time, &suspend_delta, &suspend_time);
  107. }
  108. /*
  109. Local Variables:
  110. mode:c
  111. indent-tabs-mode:nil
  112. c-basic-offset:4
  113. End:
  114. */