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