Skip to content

Commit

Permalink
Replace perror(...); exit(...); by error(...);
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
trittweiler committed Nov 14, 2018
1 parent e2ea791 commit 817f171
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 48 deletions.
14 changes: 8 additions & 6 deletions packet/command_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
#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>
Expand All @@ -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");
}
}

Expand Down Expand Up @@ -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");
}
}

Expand Down
8 changes: 6 additions & 2 deletions packet/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
#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>
Expand Down Expand Up @@ -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);

Expand Down
9 changes: 6 additions & 3 deletions packet/probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
#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>
Expand Down Expand Up @@ -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,
Expand Down
56 changes: 21 additions & 35 deletions packet/probe_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
#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
Expand Down Expand Up @@ -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 =
Expand All @@ -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");
}
}

Expand Down Expand Up @@ -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");
}
}

Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}
}

Expand All @@ -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");
}

/*
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions packet/wait_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

#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>
Expand Down Expand Up @@ -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");
}
}
}

0 comments on commit 817f171

Please sign in to comment.