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); off = lseek(resolv_conf_fd, 0, SEEK_CUR);
if (off < 0) { if (off < 0) {
log_line("write_resolve_conf: lseek returned error: %s", log_line("%s: (%s) lseek returned error: %s", client_config.interface,
strerror(errno)); __func__, strerror(errno));
return -1; return -1;
} }
retry: if (safe_ftruncate(resolv_conf_fd, off) < 0) {
if (ftruncate(resolv_conf_fd, off) < 0) { log_line("%s: (%s) ftruncate returned error: %s", client_config.interface,
if (errno == EINTR) __func__, strerror(errno));
goto retry;
log_line("write_resolve_conf: ftruncate returned error: %s",
strerror(errno));
return -1; return -1;
} }
if (fsync(resolv_conf_fd) < 0) { if (fsync(resolv_conf_fd) < 0) {
log_line("write_resolve_conf: fsync returned error: %s", log_line("%s: (%s) fsync returned error: %s", client_config.interface,
strerror(errno)); __func__, strerror(errno));
return -1; return -1;
} }
return 0; return 0;

View File

@ -62,15 +62,14 @@ void open_leasefile(void)
get_leasefile_path(leasefile, sizeof leasefile, client_config.interface); get_leasefile_path(leasefile, sizeof leasefile, client_config.interface);
leasefilefd = open(leasefile, O_WRONLY|O_TRUNC|O_CREAT, 0644); leasefilefd = open(leasefile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (leasefilefd < 0) if (leasefilefd < 0)
suicide("%s: Failed to create lease file '%s': %s", suicide("%s: (%s) Failed to create lease file '%s': %s",
client_config.interface, leasefile, strerror(errno)); client_config.interface, __func__, leasefile, strerror(errno));
} }
void write_leasefile(struct in_addr ipnum) void write_leasefile(struct in_addr ipnum)
{ {
char ip[INET_ADDRSTRLEN]; char ip[INET_ADDRSTRLEN];
char out[INET_ADDRSTRLEN*2]; char out[INET_ADDRSTRLEN*2];
ssize_t ret;
if (leasefilefd < 0) { if (leasefilefd < 0) {
log_error("%s: (%s) leasefile fd < 0; no leasefile will be written", log_error("%s: (%s) leasefile fd < 0; no leasefile will be written",
client_config.interface, __func__); client_config.interface, __func__);
@ -83,23 +82,21 @@ void write_leasefile(struct in_addr ipnum)
client_config.interface, __func__, olen); client_config.interface, __func__, olen);
return; return;
} }
retry_trunc: if (safe_ftruncate(leasefilefd, 0)) {
ret = ftruncate(leasefilefd, 0); log_warning("%s: (%s) Failed to truncate lease file: %s",
switch (ret) { client_config.interface, __func__, strerror(errno));
default: break; return;
case -1: }
if (errno == EINTR) if (lseek(leasefilefd, 0, SEEK_SET) == (off_t)-1) {
goto retry_trunc; log_warning("%s: (%s) Failed to seek to start of lease file: %s",
log_warning("%s: Failed to truncate lease file: %s", client_config.interface, __func__, strerror(errno));
client_config.interface, strerror(errno)); return;
return;
} }
lseek(leasefilefd, 0, SEEK_SET);
size_t outlen = strlen(out); 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) if (ret < 0 || (size_t)ret != outlen)
log_warning("%s: Failed to write ip to lease file.", log_warning("%s: (%s) Failed to write ip to lease file.",
client_config.interface); client_config.interface, __func__);
else else
fsync(leasefilefd); 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 safe_recvmsg(int fd, struct msghdr *msg, int flags)
{ {
ssize_t r; ssize_t r;
retry: for (;;) {
r = recvmsg(fd, msg, flags); r = recvmsg(fd, msg, flags);
if (r < 0 && errno == EINTR) if (r >= 0 || errno != EINTR) break;
goto retry; }
return r; 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); const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t safe_recv(int fd, char *buf, size_t len, int flags); 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 safe_recvmsg(int fd, struct msghdr *msg, int flags);
int safe_ftruncate(int fd, off_t length);
#endif /* NCM_IO_H_ */ #endif /* NCM_IO_H_ */