Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 28 Aug 2001 01:48:31 -0600 (MDT)
From:      FreeBSD <freebsd@XtremeDev.com>
To:        Dan Nelson <dnelson@emsphone.com>
Cc:        <freebsd-questions@FreeBSD.ORG>
Subject:   Re: open() in FreeBSD?
Message-ID:  <20010828014650.T44951-100000@Amber.XtremeDev.com>
In-Reply-To: <20010827222534.B17383@dan.emsphone.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Ahh, thanks all! Out of curiosity however, wouldn't it be a good thing to
have it default to umask if no arguments were passed rather than making it
a required arg, and just using random data if none was passed? Seems
awfully iffy doing that.. O'well. Just a thought. Thanks for the rapid
replies all!

On Mon, 27 Aug 2001, Dan Nelson wrote:

> In the last episode (Aug 27), FreeBSD said:
> > Hello, I have the following test program:
> >
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <fcntl.h>
> >
> > int main()
> > {
> >   int fd = open("blah", O_WRONLY|O_CREAT);
> >   close(fd);
> >   return EXIT_SUCCESS;
> > }
> >
> > When I compiled this and run it, ls -l blah gives me:
> >
> > ---------x  1 freebsd  freebsd  - 0 Aug 27 20:46 blah*
> >
> > Looking through man 2 umask, the default umask is 022. Yet the file
> > created by open() gives me a mask of 001? In the shell I checked the
> > umask, and it indeed is at 022. If the default umask is 022, why does
> > leaving out the third argument to open() not follow chmod/umask as
> > implied by the man 2 open?
> >
> > "open requires a third argument mode_t mode, and the file is created
> > with mode mode as described in chmod(2) and modified by the process'
> > umask value (see umask(2))." - man 2 open
> >
> > In this case it wasn't modified by the process' umask value (as it is
> > set to 022 and verified). fopen() works fine. Can anyone shed some
> > light on this subject? I'm sure I'm missing something simple. Or
> > atleast clear up the man page on what's default and what's not.
>
> You're missing the word "requires" in that manpage you quoted :)  If
> you create a file with open() you *must* pass a mode, which is masked
> by the current umask setting.  You didn't, so the system pulled a
> random value off the stack, which when masked with your umask, happened
> to come out 001.  Since the default umask is 022 (filter group and
> other write access), if the value on the stack happened to be 023, the
> resulting mode would be 001.  (all values in octal because we're
> dealing with modes)
>
> --
> 	Dan Nelson
> 	dnelson@emsphone.com
>
>


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010828014650.T44951-100000>