From e2ea7911ba563171afc3768b23fbbc1f53f367d8 Mon Sep 17 00:00:00 2001 From: Tobias Rittweiler Date: Wed, 14 Nov 2018 13:43:58 +0100 Subject: [PATCH 1/2] Add a .dir-locals.el file for Emacs. It specifies not to use TABs and to indent by 4 spaces. --- .dir-locals.el | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .dir-locals.el diff --git a/.dir-locals.el b/.dir-locals.el new file mode 100644 index 00000000..bb7424e1 --- /dev/null +++ b/.dir-locals.el @@ -0,0 +1,3 @@ +((c-mode . ((indent-tabs-mode . nil) + (c-file-style . "gnu") + (c-basic-offset . 4)))) From 817f171deaeddebfbcdcdd0b27969fd27c665f58 Mon Sep 17 00:00:00 2001 From: Tobias Rittweiler Date: Wed, 14 Nov 2018 13:46:08 +0100 Subject: [PATCH 2/2] Replace perror(...); exit(...); by error(...); 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 | 14 ++++++----- packet/packet.c | 8 +++++-- packet/probe.c | 9 ++++--- packet/probe_unix.c | 56 ++++++++++++++++--------------------------- packet/wait_unix.c | 8 +++++-- 5 files changed, 47 insertions(+), 48 deletions(-) diff --git a/packet/command_unix.c b/packet/command_unix.c index ac7a4262..34b96f13 100644 --- a/packet/command_unix.c +++ b/packet/command_unix.c @@ -19,6 +19,11 @@ #include "command.h" #include +#ifdef HAVE_ERROR_H +#include +#else +#include "portability/error.h" +#endif #include #include #include @@ -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"); } } diff --git a/packet/packet.c b/packet/packet.c index a8bc5e1d..a8c2b6e3 100644 --- a/packet/packet.c +++ b/packet/packet.c @@ -19,6 +19,11 @@ #include "config.h" #include +#ifdef HAVE_ERROR_H +#include +#else +#include "portability/error.h" +#endif #include #include #include @@ -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); diff --git a/packet/probe.c b/packet/probe.c index cf95f8a3..21ec9189 100644 --- a/packet/probe.c +++ b/packet/probe.c @@ -21,6 +21,11 @@ #include #include #include +#ifdef HAVE_ERROR_H +#include +#else +#include "portability/error.h" +#endif #include #include #include @@ -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, diff --git a/packet/probe_unix.c b/packet/probe_unix.c index 2804ecbf..fec3d472 100644 --- a/packet/probe_unix.c +++ b/packet/probe_unix.c @@ -21,6 +21,11 @@ #include #include #include +#ifdef HAVE_ERROR_H +#include +#else +#include "portability/error.h" +#endif #ifdef HAVE_LINUX_ERRQUEUE_H #include #endif @@ -154,8 +159,7 @@ void check_length_order( packet, PACKET_BUFFER_SIZE, &dest_sockaddr, &src_sockaddr, ¶m); 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, ¶m); 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, ¶m, 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(×tamp, 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, ×tamp); @@ -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; diff --git a/packet/wait_unix.c b/packet/wait_unix.c index 91831505..06d2ea79 100644 --- a/packet/wait_unix.c +++ b/packet/wait_unix.c @@ -20,6 +20,11 @@ #include #include +#ifdef HAVE_ERROR_H +#include +#else +#include "portability/error.h" +#endif #include #include #include @@ -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"); } } }