Code

non-zero process exit status when too many packets are unreturned
authorBarak A. Pearlmutter <barak+git@cs.nuim.ie>
Thu, 1 Sep 2011 12:22:05 +0000 (13:22 +0100)
committerBarak A. Pearlmutter <barak+git@cs.nuim.ie>
Mon, 5 Sep 2011 08:01:05 +0000 (09:01 +0100)
The number of unreturned packets in excess of 50% is summed over all
hosts.  If this is nonzero, this count plus 1 is the process exit
status.

src/oping.c

index 79d7569db279b8a1d04eeb8b2ced4e3669179188..5b75dae02efe461849361c8716d9e493b5c6690e 100644 (file)
@@ -951,9 +951,14 @@ static void update_host_hook (pingobj_iter_t *iter, /* {{{ */
 #endif
 } /* }}} void update_host_hook */
 
+/* returns the number of significant failures: sum over hosts of
+   unreturned packets exceeding 50% of the number sent, rounding up. */
 static int post_loop_hook (pingobj_t *ping) /* {{{ */
 {
        pingobj_iter_t *iter;
+       /* failures to report: sum over hosts of number of failed
+          returns above 50% */
+       int failure_count = 0;
 
 #if USE_NCURSES
        endwin ();
@@ -973,6 +978,13 @@ static int post_loop_hook (pingobj_t *ping) /* {{{ */
                                context_get_packet_loss (context),
                                context->latency_total);
 
+               {
+                 /* threshold for counting failed returns is 50%, rounding up */
+                 int threshold = (context->req_sent + 1) / 2;
+                 if (context->req_rcvd < threshold)
+                   failure_count += threshold - context->req_rcvd;
+               }
+
                if (context->req_rcvd != 0)
                {
                        double average;
@@ -992,7 +1004,7 @@ static int post_loop_hook (pingobj_t *ping) /* {{{ */
                context_destroy (context);
        }
 
-       return (0);
+       return (failure_count);
 } /* }}} int post_loop_hook */
 
 int main (int argc, char **argv) /* {{{ */
@@ -1281,11 +1293,14 @@ int main (int argc, char **argv) /* {{{ */
                        opt_count--;
        } /* while (opt_count != 0) */
 
-       post_loop_hook (ping);
+       status = post_loop_hook (ping);
 
        ping_destroy (ping);
 
-       return (0);
+       if (status)
+         return (EXIT_FAILURE + status);
+       else
+         return (EXIT_SUCCESS);
 } /* }}} int main */
 
 /* vim: set fdm=marker : */