diff options
author | Frediano Ziglio <fziglio@redhat.com> | 2016-11-15 16:03:23 +0000 |
---|---|---|
committer | Frediano Ziglio <fziglio@redhat.com> | 2016-11-15 16:03:23 +0000 |
commit | bb81bbac972b6df99906c3c2aaae3a41717dd07b (patch) | |
tree | 5d0eae1cea90b78e442898446f4582ba89152b6b | |
parent | 31774993fabf9aa85b7b7e16c1fd8593dbf4df24 (diff) |
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 <fziglio@redhat.com>
-rw-r--r-- | latency.c | 13 | ||||
-rw-r--r-- | tun.c | 37 | ||||
-rw-r--r-- | utils.c | 13 |
3 files changed, 23 insertions, 40 deletions
@@ -14,6 +14,7 @@ #include <limits.h> #include <getopt.h> #include <stdbool.h> +#include <err.h> #include <sys/wait.h> #include <arpa/inet.h> @@ -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); @@ -5,6 +5,7 @@ #include <unistd.h> #include <poll.h> #include <errno.h> +#include <err.h> #include <stdbool.h> #include <sys/socket.h> #include <linux/if.h> @@ -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) @@ -3,6 +3,7 @@ #include <string.h> #include <unistd.h> #include <errno.h> +#include <err.h> #include <sys/ioctl.h> #include <netinet/tcp.h> #include <arpa/inet.h> @@ -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 |