]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
Replace perror(...); exit(...); by error(...); 276/head
authorTobias Rittweiler <trittweiler@gmail.com>
Wed, 14 Nov 2018 12:46:08 +0000 (13:46 +0100)
committerTobias Rittweiler <trittweiler@gmail.com>
Wed, 14 Nov 2018 12:46:08 +0000 (13:46 +0100)
Rationale is that error(...) will prefix the error message with the
program name which is useful information now that mtr is split into
two executables (mtr and mtr-packet.)

packet/command_unix.c
packet/packet.c
packet/probe.c
packet/probe_unix.c
packet/wait_unix.c

index ac7a426284166899e8d2a3d7b83ae807596d8478..34b96f1362c1f9a0a7fed9f0380a7788811ae989 100644 (file)
 #include "command.h"
 
 #include <errno.h>
+#ifdef HAVE_ERROR_H
+#include <error.h>
+#else
+#include "portability/error.h"
+#endif
 #include <fcntl.h>
 #include <string.h>
 #include <stdio.h>
@@ -41,14 +46,12 @@ void init_command_buffer(
     /*  Get the current command stream flags  */
     flags = fcntl(command_stream, F_GETFL, 0);
     if (flags == -1) {
-        perror("Unexpected command stream error");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, errno, "Unexpected command stream error");
     }
 
     /*  Set the O_NONBLOCK bit  */
     if (fcntl(command_stream, F_SETFL, flags | O_NONBLOCK)) {
-        perror("Unexpected command stream error");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, errno, "Unexpected command stream error");
     }
 }
 
@@ -80,8 +83,7 @@ int read_commands(
         /*  EAGAIN simply means there is no available data to read  */
         /*  EINTR indicates we received a signal during read  */
         if (errno != EINTR && errno != EAGAIN) {
-            perror("Unexpected command buffer read error");
-            exit(EXIT_FAILURE);
+            error(EXIT_FAILURE, errno, "Unexpected command buffer read error");
         }
     }
 
index a8bc5e1d12c79bcdedc8d381bb1dfc0d405c69e3..a8c2b6e3c7598dcc2f565a28d3a80a3ca379a28e 100644 (file)
 #include "config.h"
 
 #include <errno.h>
+#ifdef HAVE_ERROR_H
+#include <error.h>
+#else
+#include "portability/error.h"
+#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -83,8 +88,7 @@ int main(
      */
     init_net_state_privileged(&net_state);
     if (drop_elevated_permissions()) {
-        perror("Unable to drop elevated permissions");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, errno, "Unable to drop elevated permissions");
     }
     init_net_state(&net_state);
 
index cf95f8a3f1f3f6d655f60ecd85098ff3fb956e39..21ec91890086e19e00b9d05ea96d57569f5f0b6d 100644 (file)
 #include <arpa/inet.h>
 #include <assert.h>
 #include <errno.h>
+#ifdef HAVE_ERROR_H
+#include <error.h>
+#else
+#include "portability/error.h"
+#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -284,9 +289,7 @@ void respond_to_probe(
 
     if (inet_ntop(remote_addr->ss_family, addr, ip_text, IP_TEXT_LENGTH) ==
         NULL) {
-
-        perror("inet_ntop failure");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, errno, "inet_ntop failure");
     }
 
     snprintf(response, COMMAND_BUFFER_SIZE,
index 2804ecbfc5ae1907a8d32d222d3b49b9749936ac..fec3d472c76dc087fb5c65a75e643bea36b25cab 100644 (file)
 #include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
+#ifdef HAVE_ERROR_H
+#include <error.h>
+#else
+#include "portability/error.h"
+#endif
 #ifdef HAVE_LINUX_ERRQUEUE_H
 #include <linux/errqueue.h>
 #endif
@@ -154,8 +159,7 @@ void check_length_order(
                                    packet, PACKET_BUFFER_SIZE,
                                    &dest_sockaddr, &src_sockaddr, &param);
     if (packet_size < 0) {
-        perror("Unable to send to localhost");
-        exit(EXIT_FAILURE);
+      error(EXIT_FAILURE, errno, "Unable to send to localhost");
     }
 
     bytes_sent =
@@ -172,16 +176,14 @@ void check_length_order(
                                    packet, PACKET_BUFFER_SIZE,
                                    &dest_sockaddr, &src_sockaddr, &param);
     if (packet_size < 0) {
-        perror("Unable to send to localhost");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, errno, "Unable to send to localhost");
     }
 
     bytes_sent =
         send_packet(net_state, &param, MIN_PORT, packet, packet_size,
                     &dest_sockaddr);
     if (bytes_sent < 0) {
-        perror("Unable to send with swapped length");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, errno, "Unable to send with swapped length");
     }
 }
 
@@ -215,13 +217,11 @@ void set_socket_nonblocking(
 
     flags = fcntl(socket, F_GETFL, 0);
     if (flags == -1) {
-        perror("Unexpected socket F_GETFL error");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, errno, "Unexpected socket F_GETFL error");
     }
 
     if (fcntl(socket, F_SETFL, flags | O_NONBLOCK)) {
-        perror("Unexpected socket F_SETFL O_NONBLOCK error");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, errno, "Unexpected socket F_SETFL O_NONBLOCK error");
     }
 }
 
@@ -443,13 +443,8 @@ void init_net_state_privileged(
      */
     if (!net_state->platform.ip4_present
         && !net_state->platform.ip6_present) {
-
-        errno = ip4_err;
-        perror("Failure to open IPv4 sockets");
-
-        errno = ip6_err;
-        perror("Failure to open IPv6 sockets");
-
+        error(0, ip4_err, "Failure to open IPv4 sockets");
+        error(0, ip6_err, "Failure to open IPv6 sockets");
         exit(EXIT_FAILURE);
     }
 }
@@ -572,8 +567,7 @@ void send_probe(
     }
 
     if (gettimeofday(&probe->platform.departure_time, NULL)) {
-        perror("gettimeofday failure");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, errno, "gettimeofday failure");
     }
 
     // there might be an off-by-one in the number of tries here. 
@@ -674,8 +668,7 @@ void receive_probe(
 
     if (timestamp == NULL) {
         if (gettimeofday(&now, NULL)) {
-            perror("gettimeofday failure");
-            exit(EXIT_FAILURE);
+            error(EXIT_FAILURE, errno, "gettimeofday failure");
         }
 
         timestamp = &now;
@@ -734,8 +727,7 @@ void receive_replies_from_recv_socket(
            keep the timing as precise as we can.
          */
         if (gettimeofday(&timestamp, NULL)) {
-            perror("gettimeofday failure");
-            exit(EXIT_FAILURE);
+            error(EXIT_FAILURE, errno, "gettimeofday failure");
         }
 
         if (packet_length == -1) {
@@ -782,8 +774,7 @@ void receive_replies_from_recv_socket(
                 continue;
             }
 
-            perror("Failure receiving replies");
-            exit(EXIT_FAILURE);
+            error(EXIT_FAILURE, errno, "Failure receiving replies");
         }
 
 #ifdef HAVE_LINUX_ERRQUEUE_H
@@ -819,8 +810,7 @@ void receive_replies_from_recv_socket(
             int proto, length = sizeof(int);
 
             if (getsockopt(socket, SOL_SOCKET, SO_PROTOCOL, &proto, &length) < 0) {
-                perror("getsockopt SO_PROTOCOL error");
-                exit(EXIT_FAILURE);
+                error(EXIT_FAILURE, errno, "getsockopt SO_PROTOCOL error");
             }
             handle_error_queue_packet(net_state, &remote_addr, ICMP_TIME_EXCEEDED, proto,
                     packet, packet_length, &timestamp);
@@ -866,8 +856,7 @@ void receive_replies_from_probe_socket(
         if (errno == EAGAIN) {
             return;
         } else {
-            perror("probe socket select error");
-            exit(EXIT_FAILURE);
+            error(EXIT_FAILURE, errno, "probe socket select error");
         }
     }
 
@@ -879,8 +868,7 @@ void receive_replies_from_probe_socket(
     }
 
     if (getsockopt(probe_socket, SOL_SOCKET, SO_ERROR, &err, &err_length)) {
-        perror("probe socket SO_ERROR");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, errno, "probe socket SO_ERROR");
     }
 
     /*
@@ -988,8 +976,7 @@ void check_probe_timeouts(
     struct probe_t *probe_safe_iter;
 
     if (gettimeofday(&now, NULL)) {
-        perror("gettimeofday failure");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, errno, "gettimeofday failure");
     }
 
     LIST_FOREACH_SAFE(probe, &net_state->outstanding_probes,
@@ -1022,8 +1009,7 @@ bool get_next_probe_timeout(
     struct timeval probe_timeout;
 
     if (gettimeofday(&now, NULL)) {
-        perror("gettimeofday failure");
-        exit(EXIT_FAILURE);
+        error(EXIT_FAILURE, errno, "gettimeofday failure");
     }
 
     have_timeout = false;
index 91831505e9763fcbc36a56bef03a7abc72572f13..06d2ea7973888c67712a3902df5e6f4e28049f5c 100644 (file)
 
 #include <assert.h>
 #include <errno.h>
+#ifdef HAVE_ERROR_H
+#include <error.h>
+#else
+#include "portability/error.h"
+#endif
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -144,8 +149,7 @@ void wait_for_activity(
          */
         if (errno != EINTR && errno != EAGAIN) {
             /*  We don't expect other errors, so report them  */
-            perror("unexpected select error");
-            exit(EXIT_FAILURE);
+            error(EXIT_FAILURE, errno, "unexpected select error");
         }
     }
 }