You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.3 KiB

  1. From 1ee70571e0cae37f155f59d4382bc7109138cf09 Mon Sep 17 00:00:00 2001
  2. From: Bart Van Assche <bvanassche@acm.org>
  3. Date: Sat, 15 Aug 2020 17:29:25 -0700
  4. Subject: [PATCH] apps/snmpnetstat: Stop using obsolete signal functions
  5. This was reported by Rosen Penev. See also
  6. https://github.com/net-snmp/net-snmp/pull/162.
  7. ---
  8. apps/snmpnetstat/if.c | 111 +++++++++++-------------------------------
  9. 1 file changed, 28 insertions(+), 83 deletions(-)
  10. mode change 100644 => 100755 apps/snmpnetstat/if.c
  11. diff --git a/apps/snmpnetstat/if.c b/apps/snmpnetstat/if.c
  12. old mode 100644
  13. new mode 100755
  14. index 16768151d..84b87b531
  15. --- a/apps/snmpnetstat/if.c
  16. +++ b/apps/snmpnetstat/if.c
  17. @@ -64,8 +64,6 @@
  18. #define NO 0
  19. static void sidewaysintpr(u_int);
  20. -static void timerSet(int interval_seconds);
  21. -static void timerPause(void);
  22. struct _if_info {
  23. char name[128];
  24. @@ -92,6 +90,34 @@ static void timerPause(void);
  25. };
  26. +static struct timeval deadline;
  27. +
  28. +static void
  29. +timerSet(int interval_seconds)
  30. +{
  31. + const struct timeval interval = { interval_seconds, 0 };
  32. +
  33. + netsnmp_get_monotonic_clock(&deadline);
  34. + NETSNMP_TIMERADD(&deadline, &interval, &deadline);
  35. +}
  36. +
  37. +static void
  38. +timerPause(void)
  39. +{
  40. + struct timeval now, delta;
  41. +
  42. + netsnmp_get_monotonic_clock(&now);
  43. + NETSNMP_TIMERSUB(&deadline, &now, &delta);
  44. + if (delta.tv_sec < 0)
  45. + return;
  46. +#ifdef WIN32
  47. + Sleep(delta.tv_sec * 1000 + delta.tv_usec / 1000);
  48. +#else
  49. + if (select(0, NULL, NULL, NULL, &delta) < 0)
  50. + snmp_perror("select");
  51. +#endif
  52. +}
  53. +
  54. /*
  55. * Retrieve the interface addressing information
  56. * XXX - This could also be extended to handle non-IP interfaces
  57. @@ -845,84 +871,3 @@ sidewaysintpr(unsigned int interval)
  58. goto loop;
  59. /*NOTREACHED*/
  60. }
  61. -
  62. -
  63. -/*
  64. - * timerSet sets or resets the timer to fire in "interval" seconds.
  65. - * timerPause waits only if the timer has not fired.
  66. - * timing precision is not considered important.
  67. - */
  68. -
  69. -#if (defined(WIN32) || defined(cygwin))
  70. -static int sav_int;
  71. -static time_t timezup;
  72. -static void
  73. -timerSet(int interval_seconds)
  74. -{
  75. - sav_int = interval_seconds;
  76. - timezup = time(0) + interval_seconds;
  77. -}
  78. -
  79. -/*
  80. - * you can do better than this !
  81. - */
  82. -static void
  83. -timerPause(void)
  84. -{
  85. - time_t now;
  86. - while (time(&now) < timezup)
  87. -#ifdef WIN32
  88. - Sleep(400);
  89. -#else
  90. - {
  91. - struct timeval tx;
  92. - tx.tv_sec = 0;
  93. - tx.tv_usec = 400 * 1000; /* 400 milliseconds */
  94. - select(0, 0, 0, 0, &tx);
  95. - }
  96. -#endif
  97. -}
  98. -
  99. -#else
  100. -
  101. -/*
  102. - * Called if an interval expires before sidewaysintpr has completed a loop.
  103. - * Sets a flag to not wait for the alarm.
  104. - */
  105. -RETSIGTYPE
  106. -catchalarm(int sig)
  107. -{
  108. - signalled = YES;
  109. -}
  110. -
  111. -static void
  112. -timerSet(int interval_seconds)
  113. -{
  114. -#ifdef HAVE_SIGSET
  115. - (void) sigset(SIGALRM, catchalarm);
  116. -#else
  117. - (void) signal(SIGALRM, catchalarm);
  118. -#endif
  119. - signalled = NO;
  120. - (void) alarm(interval_seconds);
  121. -}
  122. -
  123. -static void
  124. -timerPause(void)
  125. -{
  126. -#ifdef HAVE_SIGHOLD
  127. - sighold(SIGALRM);
  128. - if (!signalled) {
  129. - sigpause(SIGALRM);
  130. - }
  131. -#else
  132. - int oldmask;
  133. - oldmask = sigblock(sigmask(SIGALRM));
  134. - if (!signalled) {
  135. - sigpause(0);
  136. - }
  137. - sigsetmask(oldmask);
  138. -#endif
  139. -}
  140. -
  141. -#endif /* !WIN32 && !cygwin */