summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKen Smith <kensmith@FreeBSD.org>2004-03-08 20:31:37 +0000
committerKen Smith <kensmith@FreeBSD.org>2004-03-08 20:31:37 +0000
commit8d5a556f139a260e3b175e1feeb12f919b1e988e (patch)
treea3abcb7190e947894eec0fe0d7e87b9740f4acf1
parentb27d926ad7395341206965caca6afdd6978787a6 (diff)
downloadpw-darwin-8d5a556f139a260e3b175e1feeb12f919b1e988e.tar.gz
pw-darwin-8d5a556f139a260e3b175e1feeb12f919b1e988e.tar.zst
pw-darwin-8d5a556f139a260e3b175e1feeb12f919b1e988e.zip
Shift file locking to source file instead of temp file. This fixes
data buffering issue that corrupts files if two pw(8)'s run at the same time as well as changing pw(8) so it uses the same locking mechanism as PAM, vipw(8), pwd_mkdb(8), etc. PR: bin/23501 Submitted by: Alex Kapranoff <alex (at) kapran (dot) bitmcnit (dot) bryansk (dot) su> Approved by: rwatson (mentor) MFC after: 5 days
-rw-r--r--pw/edgroup.c7
-rw-r--r--pw/fileupd.c6
2 files changed, 5 insertions, 8 deletions
diff --git a/pw/edgroup.c b/pw/edgroup.c
index 40313d2..1cc46b4 100644
--- a/pw/edgroup.c
+++ b/pw/edgroup.c
@@ -68,7 +68,7 @@ editgroups(char *name, char **groups)
strcpy(grouptmp, groupfile);
strcat(grouptmp, ".new");
- if ((infd = open(groupfile, O_RDWR | O_CREAT, 0644)) != -1) {
+ if ((infd = open(groupfile, O_RDWR | O_CREAT | O_EXLOCK, 0644)) != -1) {
FILE *infp;
if ((infp = fdopen(infd, "r+")) == NULL)
@@ -76,7 +76,7 @@ editgroups(char *name, char **groups)
else {
int outfd;
- if ((outfd = open(grouptmp, O_RDWR | O_CREAT | O_TRUNC | O_EXLOCK, 0644)) != -1) {
+ if ((outfd = open(grouptmp, O_RDWR | O_CREAT | O_TRUNC, 0644)) != -1) {
FILE *outfp;
if ((outfp = fdopen(outfd, "w+")) == NULL)
@@ -207,8 +207,7 @@ editgroups(char *name, char **groups)
/*
* This is a gross hack, but we may have corrupted the
- * original file. Unfortunately, it will lose preservation
- * of the inode.
+ * original file.
*/
if (fflush(infp) == EOF || ferror(infp))
rc = rename(grouptmp, groupfile) == 0;
diff --git a/pw/fileupd.c b/pw/fileupd.c
index a846513..b88f4fa 100644
--- a/pw/fileupd.c
+++ b/pw/fileupd.c
@@ -76,7 +76,7 @@ fileupdate(char const * filename, mode_t fmode, char const * newline, char const
if (pfxlen <= 1)
rc = EINVAL;
else {
- int infd = open(filename, O_RDWR | O_CREAT, fmode);
+ int infd = open(filename, O_RDWR | O_CREAT | O_EXLOCK, fmode);
if (infd == -1)
rc = errno;
@@ -92,7 +92,7 @@ fileupdate(char const * filename, mode_t fmode, char const * newline, char const
strcpy(file, filename);
strcat(file, ".new");
- outfd = open(file, O_RDWR | O_CREAT | O_TRUNC | O_EXLOCK, fmode);
+ outfd = open(file, O_RDWR | O_CREAT | O_TRUNC, fmode);
if (outfd == -1)
rc = errno;
else {
@@ -183,8 +183,6 @@ fileupdate(char const * filename, mode_t fmode, char const * newline, char const
* to 'file'.
* This is a gross hack, but we may have
* corrupted the original file
- * Unfortunately, it will lose the inode
- * and hence the lock.
*/
if (fflush(infp) == EOF || ferror(infp))
rename(file, filename);