signal_handler.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* $Id: signal_handler.c 1898 2007-08-25 05:10:51Z 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. #include "config.h"
  32. #include "defines.h"
  33. #include "common.h"
  34. #include <signal.h>
  35. #include <sys/time.h>
  36. #include <stdio.h>
  37. #include <sys/types.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <errno.h>
  41. #include "tcpreplay.h"
  42. #include "signal_handler.h"
  43. struct timeval suspend_time;
  44. static struct timeval suspend_start;
  45. static struct timeval suspend_end;
  46. /**
  47. * init_signal_handlers -
  48. * Initialize signal handlers to be used in tcpreplay.
  49. */
  50. void
  51. init_signal_handlers()
  52. {
  53. signal(SIGUSR1, suspend_handler);
  54. signal(SIGCONT, continue_handler);
  55. reset_suspend_time();
  56. }
  57. /**
  58. * reset_suspend_time -
  59. * Reset time values for suspend signal.
  60. */
  61. void
  62. reset_suspend_time()
  63. {
  64. timerclear(&suspend_time);
  65. timerclear(&suspend_start);
  66. timerclear(&suspend_end);
  67. }
  68. /**
  69. * suspend signal handler -
  70. * Signal handler for signal SIGUSR1. SIGSTOP cannot be
  71. * caught, so SIGUSR1 is caught and it throws SIGSTOP.
  72. */
  73. void
  74. suspend_handler(int signo)
  75. {
  76. if (signo != SIGUSR1) {
  77. warnx("suspend_handler() got the wrong signal: %d", signo);
  78. return;
  79. }
  80. if (gettimeofday(&suspend_start, NULL) < 0)
  81. errx(1, "gettimeofday(): %s", strerror(errno));
  82. kill(getpid(), SIGSTOP);
  83. }
  84. /**
  85. * continue_handler -
  86. * Signal handler for continue signal.
  87. */
  88. void
  89. continue_handler(int signo)
  90. {
  91. struct timeval suspend_delta;
  92. if (signo != SIGCONT) {
  93. warnx("continue_handler() got the wrong signal: %d", signo);
  94. return;
  95. }
  96. if (gettimeofday(&suspend_end, NULL) < 0)
  97. errx(1, "gettimeofday(): %s", strerror(errno));
  98. timersub(&suspend_end, &suspend_start, &suspend_delta);
  99. timeradd(&suspend_time, &suspend_delta, &suspend_time);
  100. }
  101. /*
  102. Local Variables:
  103. mode:c
  104. indent-tabs-mode:nil
  105. c-basic-offset:4
  106. End:
  107. */