Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a strange issue with directory permissions.

From within a C++ app, I create a folder with: mkdir( "foldername", 777 );

But I got into an issue when attempting to create a file in that folder, fopen() returned NULL, and errno told me Permission denied. So I checked, and indeed, I had the following permissions on the folder that got created: dr----x--t

(The root folder has drwxrwxr-x)

I checked, and this unusual t means "temporary", but I have no clue on what that means.

chmod 777 foldername from the shell does the job and sets attributes to drwxrwxrwx, but I can't do it manually each time.

Question: any clue on what is going on ? Why doesn't my app correctly sets the folders attributes ? What is the meaning of this 'temporary' attribute ?

(system is Ubuntu 12.04)

share|improve this question
    
Thank you all for your responses, this is so stupid I didn't realize. The fact is, it worked before, so I assume the 0 was in the code at some time, but somebody (me ?) probably removed it at one point in the SVN, and this part of the code never got checked since! –  kebs Jun 23 at 9:50
add comment

4 Answers 4

up vote 4 down vote accepted

t is not "temporary", it means that the sticky bit is set. From man ls:

t [means that the] sticky bit is set (mode 1000), and is searchable or executable. (See chmod(1) or sticky(8).)

The sticky bit is set here because you set decimal 777 (octal 1411), not octal 777 (decimal 511). You need to write 0777 to use octal, not 777.

You should also note that the ultimate effect of the mode argument to mkdir also involves ANDing against your umask. From man 2 mkdir:

The argument mode specifies the permissions to use. It is modified by the process's umask in the usual way: the permissions of the created directory are (mode & ~umask & 0777).

I would suggest that if this affects you, you chmod after mkdir instead of using the mode argument.

A final word of warning: mode 777 is almost never what you really want to do. Instead of opening up the directory globally to all users, consider setting a proper mode and owner/group on the directory. If you need more complicated rules, consider using ACLs.

share|improve this answer
    
t is not "temporary", it means that the sticky bit is set: true, I was fooled by this, but this one appears to have the correct information. –  kebs Jun 23 at 9:48
    
Oh, and sure, I agree that 777 isn't really a correct choice. But lets say, the context makes this acceptable, it is just some scientific computation and some log/data files that are written in that folder. Thanks for your detailed answer. –  kebs Jun 23 at 9:54
    
@kebs That seems to be the manual for some kind of Windows port. It also mentions NTFS and various Windows versions a lot, and “system” and “hidden” flags in the mode… –  Blacklight Shining Jun 24 at 0:44
add comment

777 for C is decimal. You should use 0777 so that it is treated as octal.

share|improve this answer
add comment

You probably want to use an octal number for mode, rather than a decimal one:

mkdir( "foldername", 0777 );

Secondly, you need to check your umask settings to make sure you actually can create an inode with these rights.

References:

share|improve this answer
add comment

Additional to others nice answers, you can set umask to 000 before create folder, so you can have right permssion that you want:

#include <stdio.h>                                                              
#include <sys/stat.h>                                                           

int main(void) {
    mode_t old_umask;                                                                
    old_umask = umask(0);                                                                   
    mkdir("tests", 0777);
    umask(old_umask);                                                       
}

Example:

$ g++ -Wall mkdir.cpp 
$ ./a.out 
$ ls -ld tests
drwxrwxrwx. 2 cuonglm cuonglm 4096 Jun 23 16:55 tests

Note

Instead of using octal mode, you can use POSIX macros in mkdir:

mkdir("tests", S_IRWXU | S_IRWXG | S_IRWXO);
share|improve this answer
    
you can use POSIX macros in mkdir: sure, but a lot harder to remember than octal flags ! ;-) –  kebs Jun 23 at 10:17
1  
@kebs: Maybe, but it prevents you from trouble like in your question. –  Gnouc Jun 23 at 10:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.