|
|
207ed4b |
From 251c3931fedc9275bd57bc75c2153674a540bbce Mon Sep 17 00:00:00 2001
|
|
|
207ed4b |
From: "Dmitry V. Levin" <ldv@altlinux.org>
|
|
|
207ed4b |
Date: Thu, 18 May 2017 17:19:25 +0300
|
|
|
207ed4b |
Subject: [PATCH] quotaops: check return code of ftruncate and lseek calls
|
|
|
207ed4b |
MIME-Version: 1.0
|
|
|
207ed4b |
Content-Type: text/plain; charset=UTF-8
|
|
|
207ed4b |
Content-Transfer-Encoding: 8bit
|
|
|
207ed4b |
|
|
|
207ed4b |
ftruncate and lseek syscalls may fail for different reasons,
|
|
|
207ed4b |
do not ignore these errors. Create a helper function and use it
|
|
|
207ed4b |
instead of duplicating error checks and diagnostic messages.
|
|
|
207ed4b |
|
|
|
207ed4b |
Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
|
|
|
207ed4b |
Signed-off-by: Jan Kara <jack@suse.cz>
|
|
|
207ed4b |
Signed-off-by: Petr Písař <ppisar@redhat.com>
|
|
|
207ed4b |
---
|
|
|
207ed4b |
quotaops.c | 53 +++++++++++++++++++++++++++++------------------------
|
|
|
207ed4b |
1 file changed, 29 insertions(+), 24 deletions(-)
|
|
|
207ed4b |
|
|
|
207ed4b |
diff --git a/quotaops.c b/quotaops.c
|
|
|
207ed4b |
index 6f245b7..81d7be0 100644
|
|
|
207ed4b |
--- a/quotaops.c
|
|
|
207ed4b |
+++ b/quotaops.c
|
|
|
207ed4b |
@@ -252,6 +252,28 @@ int editprivs(char *tmpfile)
|
|
|
207ed4b |
}
|
|
|
207ed4b |
|
|
|
207ed4b |
/*
|
|
|
207ed4b |
+ * Duplicate a file descriptor, resetting the file offset.
|
|
|
207ed4b |
+ * If it's a write descriptor, also truncate the file.
|
|
|
207ed4b |
+ */
|
|
|
207ed4b |
+static FILE *dup_file(int fd, int is_write)
|
|
|
207ed4b |
+{
|
|
|
207ed4b |
+ FILE *fp;
|
|
|
207ed4b |
+
|
|
|
207ed4b |
+ if (is_write && ftruncate(fd, 0))
|
|
|
207ed4b |
+ die(1, _("Cannot truncate a file: %s\n"), strerror(errno));
|
|
|
207ed4b |
+ if (lseek(fd, 0, SEEK_SET))
|
|
|
207ed4b |
+ die(1, _("Cannot reset a file offset: %s\n"), strerror(errno));
|
|
|
207ed4b |
+ if ((fd = dup(fd)) < 0)
|
|
|
207ed4b |
+ die(1, _("Cannot duplicate a file descriptor: %s\n"), strerror(errno));
|
|
|
207ed4b |
+ if (!(fp = fdopen(fd, is_write ? "w" : "r")))
|
|
|
207ed4b |
+ die(1, is_write ? _("Cannot open a stream to write to: %s\n")
|
|
|
207ed4b |
+ : _("Cannot open a stream to read from: %s\n"),
|
|
|
207ed4b |
+ strerror(errno));
|
|
|
207ed4b |
+
|
|
|
207ed4b |
+ return fp;
|
|
|
207ed4b |
+}
|
|
|
207ed4b |
+
|
|
|
207ed4b |
+/*
|
|
|
207ed4b |
* Convert a dquot list to an ASCII file.
|
|
|
207ed4b |
*/
|
|
|
207ed4b |
int writeprivs(struct dquot *qlist, int outfd, char *name, int quotatype)
|
|
|
207ed4b |
@@ -259,10 +281,7 @@ int writeprivs(struct dquot *qlist, int outfd, char *name, int quotatype)
|
|
|
207ed4b |
struct dquot *q;
|
|
|
207ed4b |
FILE *fd;
|
|
|
207ed4b |
|
|
|
207ed4b |
- ftruncate(outfd, 0);
|
|
|
207ed4b |
- lseek(outfd, 0, SEEK_SET);
|
|
|
207ed4b |
- if (!(fd = fdopen(dup(outfd), "w")))
|
|
|
207ed4b |
- die(1, _("Cannot duplicate descriptor of file to write to: %s\n"), strerror(errno));
|
|
|
207ed4b |
+ fd = dup_file(outfd, 1);
|
|
|
207ed4b |
|
|
|
207ed4b |
fprintf(fd, _("Disk quotas for %s %s (%cid %d):\n"),
|
|
|
207ed4b |
_(type2name(quotatype)), name, *type2name(quotatype), qlist->dq_id);
|
|
|
207ed4b |
@@ -323,9 +342,7 @@ int readprivs(struct dquot *qlist, int infd)
|
|
|
207ed4b |
char inodesstring[BUFSIZ], isoftstring[BUFSIZ], ihardstring[BUFSIZ];
|
|
|
207ed4b |
const char *error;
|
|
|
207ed4b |
|
|
|
207ed4b |
- lseek(infd, 0, SEEK_SET);
|
|
|
207ed4b |
- if (!(fd = fdopen(dup(infd), "r")))
|
|
|
207ed4b |
- die(1, _("Cannot duplicate descriptor of temp file: %s\n"), strerror(errno));
|
|
|
207ed4b |
+ fd = dup_file(infd, 0);
|
|
|
207ed4b |
|
|
|
207ed4b |
/*
|
|
|
207ed4b |
* Discard title lines, then read lines to process.
|
|
|
207ed4b |
@@ -435,10 +452,7 @@ int writeindividualtimes(struct dquot *qlist, int outfd, char *name, int quotaty
|
|
|
207ed4b |
time_t now;
|
|
|
207ed4b |
char btimestr[MAXTIMELEN], itimestr[MAXTIMELEN];
|
|
|
207ed4b |
|
|
|
207ed4b |
- ftruncate(outfd, 0);
|
|
|
207ed4b |
- lseek(outfd, 0, SEEK_SET);
|
|
|
207ed4b |
- if (!(fd = fdopen(dup(outfd), "w")))
|
|
|
207ed4b |
- die(1, _("Cannot duplicate descriptor of file to write to: %s\n"), strerror(errno));
|
|
|
207ed4b |
+ fd = dup_file(outfd, 1);
|
|
|
207ed4b |
|
|
|
207ed4b |
fprintf(fd, _("Times to enforce softlimit for %s %s (%cid %d):\n"),
|
|
|
207ed4b |
_(type2name(quotatype)), name, *type2name(quotatype), qlist->dq_id);
|
|
|
207ed4b |
@@ -478,9 +492,7 @@ int readindividualtimes(struct dquot *qlist, int infd)
|
|
|
207ed4b |
char iunits[BUFSIZ], bunits[BUFSIZ];
|
|
|
207ed4b |
time_t now, bseconds, iseconds;
|
|
|
207ed4b |
|
|
|
207ed4b |
- lseek(infd, 0, SEEK_SET);
|
|
|
207ed4b |
- if (!(fd = fdopen(dup(infd), "r")))
|
|
|
207ed4b |
- die(1, _("Cannot duplicate descriptor of temp file: %s\n"), strerror(errno));
|
|
|
207ed4b |
+ fd = dup_file(infd, 0);
|
|
|
207ed4b |
|
|
|
207ed4b |
/*
|
|
|
207ed4b |
* Discard title lines, then read lines to process.
|
|
|
207ed4b |
@@ -543,10 +555,7 @@ int writetimes(struct quota_handle **handles, int outfd)
|
|
|
207ed4b |
if (!handles[0])
|
|
|
207ed4b |
return 0;
|
|
|
207ed4b |
|
|
|
207ed4b |
- ftruncate(outfd, 0);
|
|
|
207ed4b |
- lseek(outfd, 0, SEEK_SET);
|
|
|
207ed4b |
- if ((fd = fdopen(dup(outfd), "w")) == NULL)
|
|
|
207ed4b |
- die(1, _("Cannot duplicate descriptor of file to edit: %s\n"), strerror(errno));
|
|
|
207ed4b |
+ fd = dup_file(outfd, 1);
|
|
|
207ed4b |
|
|
|
207ed4b |
fprintf(fd, _("Grace period before enforcing soft limits for %ss:\n"),
|
|
|
207ed4b |
_(type2name(handles[0]->qh_type)));
|
|
|
207ed4b |
@@ -575,12 +584,8 @@ int readtimes(struct quota_handle **handles, int infd)
|
|
|
207ed4b |
|
|
|
207ed4b |
if (!handles[0])
|
|
|
207ed4b |
return 0;
|
|
|
207ed4b |
- lseek(infd, 0, SEEK_SET);
|
|
|
207ed4b |
- if (!(fd = fdopen(dup(infd), "r"))) {
|
|
|
207ed4b |
- errstr(_("Cannot reopen temp file: %s\n"),
|
|
|
207ed4b |
- strerror(errno));
|
|
|
207ed4b |
- return -1;
|
|
|
207ed4b |
- }
|
|
|
207ed4b |
+
|
|
|
207ed4b |
+ fd = dup_file(infd, 0);
|
|
|
207ed4b |
|
|
|
207ed4b |
/* Set all grace times to default values */
|
|
|
207ed4b |
for (i = 0; handles[i]; i++) {
|
|
|
207ed4b |
--
|
|
|
207ed4b |
2.9.4
|
|
|
207ed4b |
|