Add and use safe_ftruncate() wrapper.

This commit is contained in:
Nicholas J. Kain 2020-11-06 18:47:51 -05:00
parent a91b1d34ac
commit 2e3c504cc5
4 changed files with 35 additions and 31 deletions

View File

@ -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;

View File

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

View File

@ -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;
}

View File

@ -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_ */