diff options
author | 2002-02-22 00:18:37 +0000 | |
---|---|---|
committer | 2002-02-22 00:18:37 +0000 | |
commit | 504797403c447bceaef5c43ac839c8ed99cf56fc (patch) | |
tree | 3d8ce28fa490a08e79d19536cd504041faeb5916 | |
parent | note KTRFAC_EMUL (diff) | |
download | wireguard-openbsd-504797403c447bceaef5c43ac839c8ed99cf56fc.tar.xz wireguard-openbsd-504797403c447bceaef5c43ac839c8ed99cf56fc.zip |
if file mode known, try to avoid a race.. i think. millert spotted a bug in my first draft
-rw-r--r-- | usr.bin/mg/fileio.c | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/usr.bin/mg/fileio.c b/usr.bin/mg/fileio.c index 81ee0077331..66847e06086 100644 --- a/usr.bin/mg/fileio.c +++ b/usr.bin/mg/fileio.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fileio.c,v 1.23 2002/02/14 22:58:20 vincent Exp $ */ +/* $OpenBSD: fileio.c,v 1.24 2002/02/22 00:18:37 deraadt Exp $ */ /* * POSIX fileio.c @@ -45,16 +45,29 @@ ffwopen(fn, bp) char *fn; BUFFER *bp; { + int fd; + mode_t mode = DEFFILEMODE; - if ((ffp = fopen(fn, "w")) == NULL) { + if (bp && bp->b_fi.fi_mode) + mode = bp->b_fi.fi_mode & 07777; + + fd = open(fn, O_RDWR | O_CREAT | O_TRUNC, mode); + if (fd == -1) { + ffp = NULL; ewprintf("Cannot open file for writing : %s", strerror(errno)); return (FIOERR); + } + + if ((ffp = fdopen(fd, "w")) == NULL) { + ewprintf("Cannot open file for writing : %s", strerror(errno)); + close(fd); + return (FIOERR); } /* * If we have file information, use it. We don't bother to check for * errors, because there's no a lot we can do about it. Certainly - * trying to change ownership will fail if we aren' root. That's + * trying to change ownership will fail if we aren't root. That's * probably OK. If we don't have info, no need to get it, since any * future writes will do the same thing. */ |