From 2e3c504cc50dad35ecc6a97434309b5e8b09301c Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Fri, 6 Nov 2020 18:47:51 -0500 Subject: [PATCH] Add and use safe_ftruncate() wrapper. --- src/ifchd.c | 17 +++++++---------- src/leasefile.c | 31 ++++++++++++++----------------- src/lib/io.c | 17 +++++++++++++---- src/nk/io.h | 1 + 4 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/ifchd.c b/src/ifchd.c index e5def3a..ff96cab 100644 --- a/src/ifchd.c +++ b/src/ifchd.c @@ -181,21 +181,18 @@ static int write_resolve_conf(void) off = lseek(resolv_conf_fd, 0, SEEK_CUR); if (off < 0) { - log_line("write_resolve_conf: lseek returned error: %s", - strerror(errno)); + log_line("%s: (%s) lseek returned error: %s", client_config.interface, + __func__, strerror(errno)); return -1; } - retry: - if (ftruncate(resolv_conf_fd, off) < 0) { - if (errno == EINTR) - goto retry; - log_line("write_resolve_conf: ftruncate returned error: %s", - strerror(errno)); + if (safe_ftruncate(resolv_conf_fd, off) < 0) { + log_line("%s: (%s) ftruncate returned error: %s", client_config.interface, + __func__, strerror(errno)); return -1; } if (fsync(resolv_conf_fd) < 0) { - log_line("write_resolve_conf: fsync returned error: %s", - strerror(errno)); + log_line("%s: (%s) fsync returned error: %s", client_config.interface, + __func__, strerror(errno)); return -1; } return 0; diff --git a/src/leasefile.c b/src/leasefile.c index c7bba88..17637bb 100644 --- a/src/leasefile.c +++ b/src/leasefile.c @@ -62,15 +62,14 @@ void open_leasefile(void) get_leasefile_path(leasefile, sizeof leasefile, client_config.interface); leasefilefd = open(leasefile, O_WRONLY|O_TRUNC|O_CREAT, 0644); if (leasefilefd < 0) - suicide("%s: Failed to create lease file '%s': %s", - client_config.interface, leasefile, strerror(errno)); + suicide("%s: (%s) Failed to create lease file '%s': %s", + client_config.interface, __func__, leasefile, strerror(errno)); } void write_leasefile(struct in_addr ipnum) { char ip[INET_ADDRSTRLEN]; char out[INET_ADDRSTRLEN*2]; - ssize_t ret; if (leasefilefd < 0) { log_error("%s: (%s) leasefile fd < 0; no leasefile will be written", client_config.interface, __func__); @@ -83,23 +82,21 @@ void write_leasefile(struct in_addr ipnum) client_config.interface, __func__, olen); return; } - retry_trunc: - ret = ftruncate(leasefilefd, 0); - switch (ret) { - default: break; - case -1: - if (errno == EINTR) - goto retry_trunc; - log_warning("%s: Failed to truncate lease file: %s", - client_config.interface, strerror(errno)); - return; + if (safe_ftruncate(leasefilefd, 0)) { + log_warning("%s: (%s) Failed to truncate lease file: %s", + client_config.interface, __func__, strerror(errno)); + return; + } + if (lseek(leasefilefd, 0, SEEK_SET) == (off_t)-1) { + log_warning("%s: (%s) Failed to seek to start of lease file: %s", + client_config.interface, __func__, strerror(errno)); + return; } - lseek(leasefilefd, 0, SEEK_SET); size_t outlen = strlen(out); - ret = safe_write(leasefilefd, out, outlen); + ssize_t ret = safe_write(leasefilefd, out, outlen); if (ret < 0 || (size_t)ret != outlen) - log_warning("%s: Failed to write ip to lease file.", - client_config.interface); + log_warning("%s: (%s) Failed to write ip to lease file.", + client_config.interface, __func__); else fsync(leasefilefd); } diff --git a/src/lib/io.c b/src/lib/io.c index f842421..bdb93be 100644 --- a/src/lib/io.c +++ b/src/lib/io.c @@ -122,10 +122,19 @@ ssize_t safe_recv(int fd, char *buf, size_t len, int flags) ssize_t safe_recvmsg(int fd, struct msghdr *msg, int flags) { ssize_t r; - retry: - r = recvmsg(fd, msg, flags); - if (r < 0 && errno == EINTR) - goto retry; + for (;;) { + r = recvmsg(fd, msg, flags); + if (r >= 0 || errno != EINTR) break; + } return r; } +int safe_ftruncate(int fd, off_t length) +{ + int r; + for (;;) { + r = ftruncate(fd, length); + if (!r || errno != EINTR) break; + } + return r; +} diff --git a/src/nk/io.h b/src/nk/io.h index 3ecc148..985522d 100644 --- a/src/nk/io.h +++ b/src/nk/io.h @@ -37,5 +37,6 @@ ssize_t safe_sendto(int fd, const char *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); ssize_t safe_recv(int fd, char *buf, size_t len, int flags); ssize_t safe_recvmsg(int fd, struct msghdr *msg, int flags); +int safe_ftruncate(int fd, off_t length); #endif /* NCM_IO_H_ */