summaryrefslogtreecommitdiffstats
path: root/usr.sbin/pwd_mkdb/pwd_mkdb.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.sbin/pwd_mkdb/pwd_mkdb.c')
-rw-r--r--usr.sbin/pwd_mkdb/pwd_mkdb.c25
1 files changed, 20 insertions, 5 deletions
diff --git a/usr.sbin/pwd_mkdb/pwd_mkdb.c b/usr.sbin/pwd_mkdb/pwd_mkdb.c
index 0ba68a5..c75804a 100644
--- a/usr.sbin/pwd_mkdb/pwd_mkdb.c
+++ b/usr.sbin/pwd_mkdb/pwd_mkdb.c
@@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$");
#include <err.h>
#include <errno.h>
#include <fcntl.h>
+#include <libgen.h>
#include <limits.h>
#include <pwd.h>
#include <signal.h>
@@ -224,14 +225,14 @@ main(int argc, char *argv[])
clean = FILE_INSECURE;
cp(buf2, buf, PERM_INSECURE);
dp = dbopen(buf,
- O_RDWR|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
+ O_RDWR|O_EXCL|O_SYNC, PERM_INSECURE, DB_HASH, &openinfo);
if (dp == NULL)
error(buf);
clean = FILE_SECURE;
cp(sbuf2, sbuf, PERM_SECURE);
sdp = dbopen(sbuf,
- O_RDWR|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
+ O_RDWR|O_EXCL|O_SYNC, PERM_SECURE, DB_HASH, &openinfo);
if (sdp == NULL)
error(sbuf);
@@ -288,13 +289,13 @@ main(int argc, char *argv[])
method = 0;
} else {
dp = dbopen(buf,
- O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
+ O_RDWR|O_CREAT|O_EXCL|O_SYNC, PERM_INSECURE, DB_HASH, &openinfo);
if (dp == NULL)
error(buf);
clean = FILE_INSECURE;
sdp = dbopen(sbuf,
- O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
+ O_RDWR|O_CREAT|O_EXCL|O_SYNC, PERM_SECURE, DB_HASH, &openinfo);
if (sdp == NULL)
error(sbuf);
clean = FILE_SECURE;
@@ -714,13 +715,27 @@ void
mv(char *from, char *to)
{
char buf[MAXPATHLEN];
+ char *to_dir;
+ int to_dir_fd = -1;
- if (rename(from, to)) {
+ /*
+ * Make sure file is safe on disk. To improve performance we will call
+ * fsync() to the directory where file lies
+ */
+ if (rename(from, to) != 0 ||
+ (to_dir = dirname(to)) == NULL ||
+ (to_dir_fd = open(to_dir, O_RDONLY|O_DIRECTORY)) == -1 ||
+ fsync(to_dir_fd) != 0) {
int sverrno = errno;
(void)snprintf(buf, sizeof(buf), "%s to %s", from, to);
errno = sverrno;
+ if (to_dir_fd != -1)
+ close(to_dir_fd);
error(buf);
}
+
+ if (to_dir_fd != -1)
+ close(to_dir_fd);
}
void
OpenPOWER on IntegriCloud