signal_handler.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* $Id: signal_handler.c 767 2004-10-06 12:48:49Z 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 <signal.h>
  39. #include <sys/time.h>
  40. #include <stdio.h>
  41. #include <sys/types.h>
  42. #include <unistd.h>
  43. #include "config.h"
  44. #include "signal_handler.h"
  45. #include "timer.h"
  46. #include "err.h"
  47. struct timeval suspend_time;
  48. static struct timeval suspend_start;
  49. static struct timeval suspend_end;
  50. /*
  51. * init_signal_handlers -
  52. * Initialize signal handlers to be used in tcpreplay.
  53. */
  54. void
  55. init_signal_handlers()
  56. {
  57. signal(SIGUSR1, suspend_handler);
  58. signal(SIGCONT, continue_handler);
  59. reset_suspend_time();
  60. }
  61. /*
  62. * reset_suspend_time -
  63. * Reset time values for suspend signal.
  64. */
  65. void
  66. reset_suspend_time()
  67. {
  68. timerclear(&suspend_time);
  69. timerclear(&suspend_start);
  70. timerclear(&suspend_end);
  71. }
  72. /*
  73. * suspend signal handler -
  74. * Signal handler for signal SIGUSR1. SIGSTOP cannot be
  75. * caught, so SIGUSR1 is caught and it throws SIGSTOP.
  76. */
  77. void
  78. suspend_handler(int signo)
  79. {
  80. if (gettimeofday(&suspend_start, NULL) < 0)
  81. err(1, "gettimeofday");
  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 (gettimeofday(&suspend_end, NULL) < 0)
  93. err(1, "gettimeofday");
  94. timersub(&suspend_end, &suspend_start, &suspend_delta);
  95. timeradd(&suspend_time, &suspend_delta, &suspend_time);
  96. }