Operating System - HP-UX
1819870 Members
2645 Online
109607 Solutions
New Discussion юеВ

default file permissions to 666

 
Max_4
Frequent Advisor

default file permissions to 666

Hi everybody,

I've been looking throughout man pages to see where I could find the place where the default 666 mode was explicitly specified when creating new files. I was even taking a look at creat(2), but found no answer.

It looks like the system always do a:

/* C jargon */

extern mode_t curr_proc_umask;
...
creat("path_of_my_file", 0666 & ~curr_proc_umask);
...

Where can I find some official documentation stating this or giving details on this?

Thanks in advance,
8 REPLIES 8
Abdul Rahiman
Esteemed Contributor

Re: default file permissions to 666

You didn't metntion about the umask of the user owning the files.
If not alreadey checked, pls check the umask. If the umask is set 000, files created could have 666 permissions.
No unix, no fun
Max_4
Frequent Advisor

Re: default file permissions to 666

For my question I was assumming a curr_proc_mask (umask)of 0. Thus this bitwise AND causes no effect on the final
mode.

Again:

It looks like the system always do a:

/* C jargon */

extern mode_t curr_proc_umask;
...
creat("path_of_my_file", 0666 & ~curr_proc_umask);
...


I mean it looks that *always* there's a 0666 harcoded whenever I creat(2) a file?

Where can I find some official documentation stating this or giving details on this?

Thanks in advance,

Max_4
Frequent Advisor

Re: default file permissions to 666

Well after playing around with the following 2 code snippets.

/* BEGINNING CODE 1 */
#include

int main(void)
{
mode_t mymode = 0666;
(void) creat("this_is_just_a_test", mymode);
return 0;
}
/* END OF CODE 1 */

/* BEGINNING CODE 2 */
#include

int main(void)
{
mode_t mymode = 0;
(void) creat("this_is_just_another_test", mymode);
return 0;
}
/* END OF CODE 2 */

I assume that programs like touch(1) really behave backstage like CODE 1 setting mymode to 0666 when creating files.

On the other hand a program like mkdir(1) set mymode to 0777 when calling mkdir(2).

Any input will be greatly appreciated...
Patrick Wallek
Honored Contributor

Re: default file permissions to 666

I do not know if it is documented anywhere, I'm sure it is but finding it could be a challenge.

When files are created they are created by default with 666 permissions, THEN whatever your umask is set to is essentially subtracted from the default.

Directories are created by default with 777 permissions and the umask is then subtracted from the default to give the actual permissions.

Is there something you are trying to do? Are you trying to change the default file mode?
A. Clay Stephenson
Acclaimed Contributor

Re: default file permissions to 666

You are actually talking about two different things. To the system calls open() with the O_CREAT flag set or creat() there are no "default" MODES. It is the sole responsibility of the programmer to set the mode that is then modified by the existing umask to result in the actual mode set by the system call. To other commands, like the shell, the mode is always 666 for non- directory files and 777 (because that is the way the underlying C code sets them) for directories. Again, this default is modified by the current umask of the process.
If it ain't broke, I can fix that.
Andrew Cowan
Honored Contributor

Re: default file permissions to 666

Are you sure that you want to do this? World-writeable files are an open invitation to the curious, and a gift to hackers. The umask setting is there for good reason, and I'm sure that in nearly all cases you could use group ownership to solve your problem.
Manish Srivastava
Trusted Contributor

Re: default file permissions to 666

Hi,

Files opened with with "a+" argument in fopen libc call enforce 0666 & ~umask may be that is extended to creat(2) too.

You may have to set umask to zero before calling creat() and
restoring it back after the file creation operation.

Manpage of umask(2) man be useful.

manish.
Max_4
Frequent Advisor

Re: default file permissions to 666

Clay,

I think you pinpointed the issue here. As you clearly exposed that is in no way enforced by underlying Operating System API.

It's just the shell that sets 0666 for files and 0777 for dirs. Nevertheless I've been looking in vane for official documentation that states this common (but undocumented)behavior... :(