From bb81bbac972b6df99906c3c2aaae3a41717dd07b Mon Sep 17 00:00:00 2001 From: Frediano Ziglio Date: Tue, 15 Nov 2016 16:03:23 +0000 Subject: Use err instead of perror/exit More or less perror+exit does the same of err call so simplify code using err function. Signed-off-by: Frediano Ziglio --- latency.c | 13 +++++-------- tun.c | 37 +++++++++++++------------------------ utils.c | 13 +++++-------- 3 files changed, 23 insertions(+), 40 deletions(-) diff --git a/latency.c b/latency.c index 7d98ee8..6ccf8ac 100644 --- a/latency.c +++ b/latency.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -111,10 +112,8 @@ main(int argc, char **argv) setenv("HOME", "/root", 1); setenv("PATH", "/sbin:/bin:/usr/sbin:/usr/bin", 1); setenv("SHELL", "/bin/sh", 1); - if (setuid(0)) { - perror("setuid"); - exit(EXIT_FAILURE); - } + if (setuid(0)) + err(EXIT_FAILURE, "setuid"); } enum { MODE_local, MODE_server, MODE_client } mode = MODE_local; @@ -165,10 +164,8 @@ main(int argc, char **argv) tun_setup(mode == MODE_local); if (ruid != euid) { - if (setuid(ruid)) { - perror("setuid"); - exit(EXIT_FAILURE); - } + if (setuid(ruid)) + err(EXIT_FAILURE, "setuid"); } int port = parse_value(str_port, 1, 65535, no_units); diff --git a/tun.c b/tun.c index 456d691..a7d4638 100644 --- a/tun.c +++ b/tun.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -100,22 +101,16 @@ tun_init(const char *ip) tun_name[0] = 0; fd = tun_alloc(tun_name, IFF_TUN|IFF_NO_PI); - if (fd < 0) { - perror("error creating TUN device"); - exit(EXIT_FAILURE); - } + if (fd < 0) + err(EXIT_FAILURE, "error creating TUN device"); sprintf(cmd, "ip link set %s up", tun_name); - if (system(cmd) != 0) { - perror("system"); - exit(EXIT_FAILURE); - } + if (system(cmd) != 0) + err(EXIT_FAILURE, "system"); sprintf(cmd, "ip addr add %s dev %s", ip, tun_name); - if (system(cmd) != 0) { - perror("system"); - exit(EXIT_FAILURE); - } + if (system(cmd) != 0) + err(EXIT_FAILURE, "system"); return fd; } @@ -141,10 +136,8 @@ create_remote_socket(void) } remote_sock = socket(AF_INET, SOCK_DGRAM, 0); - if (remote_sock < 0) { - perror("socket"); - exit(EXIT_FAILURE); - } + if (remote_sock < 0) + err(EXIT_FAILURE, "socket"); } void @@ -198,10 +191,8 @@ tun_set_server(int port) sin.sin_port = htons((short) port); sin.sin_addr.s_addr = INADDR_ANY; - if (bind(remote_sock, (struct sockaddr *) &sin, sizeof(sin)) < 0) { - perror("bind"); - exit(EXIT_FAILURE); - } + if (bind(remote_sock, (struct sockaddr *) &sin, sizeof(sin)) < 0) + err(EXIT_FAILURE, "bind"); remote_connected = false; is_server = true; } @@ -532,10 +523,8 @@ handle_tun(void) if (tun_log_filename) { pcap = pcap_open(tun_log_filename); - if (!pcap) { - perror("error opening log file"); - exit(EXIT_FAILURE); - } + if (!pcap) + err(EXIT_FAILURE, "error opening log file"); } if (remote_sock >= 0) diff --git a/utils.c b/utils.c index 6389ced..6d58b8b 100644 --- a/utils.c +++ b/utils.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -13,20 +14,16 @@ void set_nonblocking(int sock) { int on = 1; - if (ioctl(sock, FIONBIO, &on) == -1) { - perror("ioctl"); - exit(EXIT_FAILURE); - } + if (ioctl(sock, FIONBIO, &on) == -1) + err(EXIT_FAILURE, "ioctl"); } void set_nodelay(int fd) { int nodelay = 1; - if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) == -1) { - perror("ioctl"); - exit(EXIT_FAILURE); - } + if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) == -1) + err(EXIT_FAILURE, "ioctl"); } void -- cgit v1.2.3