Change most error comparisons from == -1 to < 0. Some were not changed,

as the different negative values equate to different errors.

Tests against syscall returns and fds are very common and mostly fit
the pattern of this change.

The gain is increased range-exclusion.
This commit is contained in:
Nicholas J. Kain 2014-04-06 06:33:14 -04:00
parent c03be059f5
commit b511d45c2f
9 changed files with 51 additions and 51 deletions

View File

@ -91,7 +91,7 @@ static ssize_t send_dhcp_cooked(struct client_state_t *cs,
{
ssize_t ret = -1;
int fd = get_udp_unicast_socket();
if (fd == -1)
if (fd < 0)
goto out;
struct sockaddr_in raddr = {
@ -99,7 +99,7 @@ static ssize_t send_dhcp_cooked(struct client_state_t *cs,
.sin_port = htons(DHCP_SERVER_PORT),
.sin_addr.s_addr = cs->serverAddr,
};
if (connect(fd, (struct sockaddr *)&raddr, sizeof(struct sockaddr)) == -1) {
if (connect(fd, (struct sockaddr *)&raddr, sizeof(struct sockaddr)) < 0) {
log_error("%s: (%s) connect failed: %s", client_config.interface,
__func__, strerror(errno));
goto out_fd;
@ -283,7 +283,7 @@ static ssize_t send_dhcp_raw(struct dhcpmsg *payload)
{
ssize_t ret = -1;
int fd = get_raw_broadcast_socket();
if (fd == -1)
if (fd < 0)
return ret;
// Send packets that are as short as possible.

View File

@ -92,7 +92,7 @@ static void write_resolve_conf(void)
if (strlen(cl.namesvrs) == 0)
return;
if (lseek(resolv_conf_fd, 0, SEEK_SET) == -1)
if (lseek(resolv_conf_fd, 0, SEEK_SET) < 0)
return;
char *p = cl.namesvrs;
@ -151,14 +151,14 @@ static void write_resolve_conf(void)
writeordie(resolv_conf_fd, "\n", 1);
off = lseek(resolv_conf_fd, 0, SEEK_CUR);
if (off == -1) {
if (off < 0) {
log_line("write_resolve_conf: lseek returned error: %s",
strerror(errno));
return;
}
retry:
r = ftruncate(resolv_conf_fd, off);
if (r == -1) {
if (r < 0) {
if (errno == EINTR)
goto retry;
log_line("write_resolve_conf: ftruncate returned error: %s",
@ -166,7 +166,7 @@ static void write_resolve_conf(void)
return;
}
r = fsync(resolv_conf_fd);
if (r == -1) {
if (r < 0) {
log_line("write_resolve_conf: fsync returned error: %s",
strerror(errno));
return;
@ -183,7 +183,7 @@ void perform_timezone(const char *str, size_t len)
/* Add a dns server to the /etc/resolv.conf -- we already have a fd. */
void perform_dns(const char *str, size_t len)
{
if (!str || resolv_conf_fd == -1)
if (!str || resolv_conf_fd < 0)
return;
if (len > sizeof cl.namesvrs) {
log_line("DNS server list is too long: %zu > %zu", len, cl.namesvrs);
@ -210,7 +210,7 @@ void perform_hostname(const char *str, size_t len)
{
if (!allow_hostname || !str)
return;
if (sethostname(str, len) == -1)
if (sethostname(str, len) < 0)
log_line("sethostname returned %s", strerror(errno));
else
log_line("Set hostname: '%s'", str);
@ -219,7 +219,7 @@ void perform_hostname(const char *str, size_t len)
/* update "domain" and "search" in /etc/resolv.conf */
void perform_domain(const char *str, size_t len)
{
if (!str || resolv_conf_fd == -1)
if (!str || resolv_conf_fd < 0)
return;
if (len > sizeof cl.domains) {
log_line("DNS domain list is too long: %zu > %zu", len, cl.namesvrs);
@ -332,7 +332,7 @@ static void process_client_pipe(void)
client_config.interface, __func__, strerror(errno));
}
if (execute_buffer(buf) == -1) {
if (execute_buffer(buf) < 0) {
inform_execute('-');
suicide("%s: (%s) received invalid commands: '%s'",
client_config.interface, __func__, buf);
@ -343,7 +343,7 @@ static void process_client_pipe(void)
static void do_ifch_work(void)
{
epollfd = epoll_create1(0);
if (epollfd == -1)
if (epollfd < 0)
suicide("epoll_create1 failed");
if (enforce_seccomp_ifch())
@ -359,7 +359,7 @@ static void do_ifch_work(void)
for (;;) {
int r = epoll_wait(epollfd, events, 2, -1);
if (r == -1) {
if (r < 0) {
if (errno == EINTR)
continue;
else
@ -391,7 +391,7 @@ void ifch_main(void)
umask(022);
resolv_conf_fd = open(resolv_conf_d, O_RDWR | O_CREAT, 644);
umask(077);
if (resolv_conf_fd == -1) {
if (resolv_conf_fd < 0) {
suicide("FATAL - unable to open resolv.conf");
}
}

View File

@ -315,10 +315,10 @@ static int link_flags_get(int fd, uint32_t *flags)
do {
ret = nl_recv_buf(fd, nlbuf, sizeof nlbuf);
if (ret == -1)
if (ret < 0)
return -2;
if (nl_foreach_nlmsg(nlbuf, ret, seq, 0, link_flags_get_do,
&ipx) == -1)
&ipx) < 0)
return -3;
} while (ret > 0);
if (ipx.got_flags) {
@ -424,10 +424,10 @@ static int ipbcpfx_clear_others(int fd, uint32_t ipaddr, uint32_t bcast,
do {
ret = nl_recv_buf(fd, nlbuf, sizeof nlbuf);
if (ret == -1)
if (ret < 0)
return -2;
if (nl_foreach_nlmsg(nlbuf, ret, seq, 0,
ipbcpfx_clear_others_do, &ipx) == -1)
ipbcpfx_clear_others_do, &ipx) < 0)
return -3;
} while (ret > 0);
return ipx.already_ok ? 1 : 0;

View File

@ -264,7 +264,7 @@ static void do_ndhc_work(void)
int timeout;
cs.epollFd = epoll_create1(0);
if (cs.epollFd == -1)
if (cs.epollFd < 0)
suicide("epoll_create1 failed");
if (enforce_seccomp_ndhc())
@ -280,7 +280,7 @@ static void do_ndhc_work(void)
for (;;) {
int r = epoll_wait(cs.epollFd, events, NDHC_NUM_EP_FDS, timeout);
if (r == -1) {
if (r < 0) {
if (errno == EINTR)
continue;
else
@ -306,7 +306,7 @@ static void do_ndhc_work(void)
nowts = curms();
long long arp_wake_ts = arp_get_wake_ts();
long long dhcp_wake_ts = dhcp_get_wake_ts();
if (arp_wake_ts == -1) {
if (arp_wake_ts < 0) {
if (dhcp_wake_ts != -1) {
timeout = dhcp_wake_ts - nowts;
if (timeout < 0)
@ -417,7 +417,7 @@ static void ndhc_main(void) {
suicide("%s: failed to open netlink socket", __func__);
if (client_config.foreground && !client_config.background_if_no_lease) {
if (file_exists(pidfile, "w") == -1)
if (file_exists(pidfile, "w") < 0)
suicide("%s: can't open pidfile '%s' for write!",
__func__, pidfile);
write_pid(pidfile);
@ -440,12 +440,12 @@ void background(void)
static char called;
if (!called) {
called = 1; // Do not fork again.
if (daemon(0, 0) == -1) {
if (daemon(0, 0) < 0) {
perror("fork");
exit(EXIT_SUCCESS);
}
}
if (file_exists(pidfile, "w") == -1) {
if (file_exists(pidfile, "w") < 0) {
log_warning("Cannot open pidfile for write!");
} else
write_pid(pidfile);
@ -486,7 +486,7 @@ static void parse_program_options(int argc, char *argv[])
int c;
c = getopt_long(argc, argv, "c:bp:P:h:i:nqr:V:u:U:D:C:s:Sdw:W:m:M:t:R:Hv?",
arg_options, NULL);
if (c == -1) break;
if (c < 0) break;
switch (c) {
case 'c':

View File

@ -92,10 +92,10 @@ void handle_nl_message(struct client_state_t *cs)
assert(cs->nlFd != -1);
do {
ret = nl_recv_buf(cs->nlFd, nlbuf, sizeof nlbuf);
if (ret == -1)
if (ret < 0)
break;
if (nl_foreach_nlmsg(nlbuf, ret, 0, cs->nlPortId, nl_process_msgs, cs)
== -1)
< 0)
break;
} while (ret > 0);
}
@ -147,10 +147,10 @@ static int handle_getifdata(int fd, uint32_t seq)
int got_ifdata = 0;
do {
ret = nl_recv_buf(fd, nlbuf, sizeof nlbuf);
if (ret == -1)
if (ret < 0)
return -1;
if (nl_foreach_nlmsg(nlbuf, ret, seq, 0,
do_handle_getifdata, &got_ifdata) == -1)
do_handle_getifdata, &got_ifdata) < 0)
return -1;
} while (ret > 0);
return got_ifdata ? 0 : -1;
@ -183,7 +183,7 @@ int nl_getifdata(void)
pr = poll(&((struct pollfd){.fd=fd,.events=POLLIN}), 1, -1);
if (pr == 1)
ret = handle_getifdata(fd, seq);
else if (pr == -1 && errno != EINTR)
else if (pr < 0 && errno != EINTR)
goto fail_fd;
}
fail_fd:

View File

@ -96,7 +96,7 @@ ssize_t nl_recv_buf(int fd, char *buf, size_t blen)
ssize_t ret;
retry:
ret = recvmsg(fd, &msg, MSG_DONTWAIT);
if (ret == -1) {
if (ret < 0) {
if (errno == EINTR)
goto retry;
if (errno != EAGAIN && errno != EWOULDBLOCK) {
@ -264,11 +264,11 @@ int nl_open(int nltype, int nlgroup, int *nlportid)
{
int fd;
fd = socket(AF_NETLINK, SOCK_RAW, nltype);
if (fd == -1) {
if (fd < 0) {
log_error("%s: socket failed: %s", __func__, strerror(errno));
return -1;
}
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) == -1) {
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) < 0) {
log_error("%s: Set non-blocking failed: %s",
__func__, strerror(errno));
goto err_close;
@ -283,13 +283,13 @@ int nl_open(int nltype, int nlgroup, int *nlportid)
.nl_family = AF_NETLINK,
.nl_groups = nlgroup,
};
if (bind(fd, (struct sockaddr *)&nlsock, sizeof nlsock) == -1) {
if (bind(fd, (struct sockaddr *)&nlsock, sizeof nlsock) < 0) {
log_error("%s: bind to group failed: %s",
__func__, strerror(errno));
goto err_close;
}
al = sizeof nlsock;
if (getsockname(fd, (struct sockaddr *)&nlsock, &al) == -1) {
if (getsockname(fd, (struct sockaddr *)&nlsock, &al) < 0) {
log_error("%s: getsockname failed: %s",
__func__, strerror(errno));
goto err_close;

View File

@ -120,19 +120,19 @@ int request_sockd_fd(char *buf, size_t buflen, char *response)
static int create_arp_socket(void)
{
int fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
if (fd == -1) {
if (fd < 0) {
log_error("%s: (%s) socket failed: %s", client_config.interface,
__func__, strerror(errno));
goto out;
}
int opt = 1;
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof opt) == -1) {
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof opt) < 0) {
log_error("%s: (%s) setsockopt failed: %s", client_config.interface,
__func__, strerror(errno));
goto out_fd;
}
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) == -1) {
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) < 0) {
log_error("%s: (%s) fcntl failed: %s", client_config.interface,
__func__, strerror(errno));
goto out_fd;
@ -164,12 +164,12 @@ static int create_udp_socket(uint32_t ip, uint16_t port, char *iface)
goto out;
}
int opt = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) == -1) {
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) < 0) {
log_error("%s: (%s) Set reuse addr failed: %s",
client_config.interface, __func__, strerror(errno));
goto out_fd;
}
if (setsockopt(fd, SOL_SOCKET, SO_DONTROUTE, &opt, sizeof opt) == -1) {
if (setsockopt(fd, SOL_SOCKET, SO_DONTROUTE, &opt, sizeof opt) < 0) {
log_error("%s: (%s) Set don't route failed: %s",
client_config.interface, __func__, strerror(errno));
goto out_fd;
@ -187,7 +187,7 @@ static int create_udp_socket(uint32_t ip, uint16_t port, char *iface)
client_config.interface, __func__, strerror(errno));
goto out_fd;
}
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) == -1) {
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) < 0) {
log_error("%s: (%s) Set non-blocking failed: %s",
client_config.interface, __func__, strerror(errno));
goto out_fd;
@ -198,7 +198,7 @@ static int create_udp_socket(uint32_t ip, uint16_t port, char *iface)
.sin_port = htons(port),
.sin_addr.s_addr = ip,
};
if (bind(fd, (struct sockaddr *)&sa, sizeof sa) == -1)
if (bind(fd, (struct sockaddr *)&sa, sizeof sa) < 0)
goto out_fd;
return fd;
@ -225,12 +225,12 @@ static int create_raw_socket(struct sockaddr_ll *sa, bool *using_bpf,
}
int opt = 1;
if (setsockopt(fd, SOL_SOCKET, SO_DONTROUTE, &opt, sizeof opt) == -1) {
if (setsockopt(fd, SOL_SOCKET, SO_DONTROUTE, &opt, sizeof opt) < 0) {
log_error("create_raw_socket: Failed to set don't route: %s",
strerror(errno));
goto out_fd;
}
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) == -1) {
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) < 0) {
log_error("create_raw_socket: Set non-blocking failed: %s",
strerror(errno));
goto out_fd;
@ -252,10 +252,10 @@ static int create_udp_listen_socket(void)
{
int fd = create_udp_socket(INADDR_ANY, DHCP_CLIENT_PORT,
client_config.interface);
if (fd == -1)
if (fd < 0)
return -1;
int opt = 1;
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof opt) == -1) {
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof opt) < 0) {
log_error("%s: (%s) Set broadcast failed: %s",
client_config.interface, __func__, strerror(errno));
close(fd);
@ -590,7 +590,7 @@ static void process_client_pipe(void)
static void do_sockd_work(void)
{
epollfd = epoll_create1(0);
if (epollfd == -1)
if (epollfd < 0)
suicide("epoll_create1 failed");
if (enforce_seccomp_sockd())
@ -601,7 +601,7 @@ static void do_sockd_work(void)
for (;;) {
int r = epoll_wait(epollfd, events, 2, -1);
if (r == -1) {
if (r < 0) {
if (errno == EINTR)
continue;
else

View File

@ -246,7 +246,7 @@ static void an_packet(struct client_state_t *cs, struct dhcpmsg *packet,
inet_ntop(AF_INET, &(struct in_addr){.s_addr=cs->clientAddr},
clibuf, sizeof clibuf);
log_line("Accepted a firm offer for %s. Validating...", clibuf);
if (arp_check(cs, packet) == -1) {
if (arp_check(cs, packet) < 0) {
log_warning("Failed to make arp socket. Searching for new lease...");
reinit_selecting(cs, 3000);
}

View File

@ -41,7 +41,7 @@ void epoll_add(int epfd, int fd)
ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP;
ev.data.fd = fd;
r = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
if (r == -1)
if (r < 0)
suicide("epoll_add failed %s", strerror(errno));
}
@ -52,6 +52,6 @@ void epoll_del(int epfd, int fd)
ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP;
ev.data.fd = fd;
r = epoll_ctl(epfd, EPOLL_CTL_DEL, fd, &ev);
if (r == -1)
if (r < 0)
suicide("epoll_del failed %s", strerror(errno));
}