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.

The executable files that gcc creates have execution permissions

-rwxrwxr-x

which are different than the permissions that the source file has.

-rw-rw-r--

How does gcc set these permissions ?

share|improve this question
    
I don't really understand what you're asking. Why would the output file have the same permissions as the input? –  Mat Mar 1 '13 at 7:08
    
@Mat Why would it not have ? Also, how does it get permissions that are different from the default permissions that new files get ? –  AsheeshR Mar 1 '13 at 12:57
    
The input and output files are completely unrelated. When you ask your compiler to build an executable, it would be pretty "dumb" for the compiler not to produce an executable file. If you request only to build .os, similarly it wouldn't really make sense to have those be executable. The compiler creates the output, why would it voluntarily not produce the right permissions when it creates the files? –  Mat Mar 1 '13 at 14:42

3 Answers 3

up vote 2 down vote accepted

Four things intervene to determine the permission of a file.

  1. When an application creates a file, it specifies a set of initial permissions. These initial permissions are passed as an argument of the system call that creates the file (open for regular files, mkdir for directories, etc.).
  2. The permissions are masked with the umask, which is an attribute of the running process. The umask indicates permission bits that are removed from the permissions specified by the application. For example, an umask of 022 removes the group-write and other-write permission. An umask of 007 leaves the group-write permission but makes the file completely off-limits to others.
  3. The permissions may be modified further by access control lists. I won't discuss these further in this post.
  4. The application may call chmod explicitly to change the permissions to whatever it wants. The user who owns a file can set its permissions freely.

Some popular choices of permission sets for step 1 are:

  • 666 (i.e. read and write for everybody) for a regular file.
  • 600 (i.e. read and write, only for the owner) for a regular file that must be remain private (e.g. an email, or a temporary file).
  • 777 (i.e. read, write and execute for everybody) for a directory, or for an executable regular file.

It's the umask that causes files not to be world-readable even though applications can and usually do include the others-write permission in the file creation permissions.

In the case of gcc, the output file is first created with permissions 666 (masked by the umask), then later chmod'ed to make it executable. Gcc could create an executable directly, but doesn't: it only makes the file executable when it's finished writing it, so that you don't risk starting to execute the program while it's incomplete.

share|improve this answer
    
Thanks for the detailed explanation! –  Kent Pawar Mar 6 '13 at 6:17

I'm not positive what you're asking; you mean how does it have permission to set them? A file's owner can set the permissions to whatever they want. gcc is running under your user account, so the file it creates is owned by you, and it has permission to do anything your account can do, including setting permissions on your files

share|improve this answer
    
@Gilles Yes, thats what I was (mainly) asking. Although, going by responses, it appears that there is something wrong in this question itself. How so ? –  AsheeshR Mar 2 '13 at 2:28
    
@AshRj You came up with a pretty specific question ("how does gcc create executable files") for a generic situation ("how does any program create any file"). Why would gcc be special? When any program creates a file it can set the permissions to whatever it wants, the open() system call takes a mode argument when creating a new file that specifies the permissions the file should have; so does creat(). If you already have a file, the chmod() function can change its permissions. This happens constantly when programs create files, so asking about +x from gcc is kind of confusing –  Michael Mrozek Mar 2 '13 at 2:34
    
@AshRj I don't think there's anything wrong with the question. I think answerers have assumed you were familiar with how things work under the hood, even though your question shows that you aren't and you're asking how they work. P.S. Ignore my previous, wrong, now-deleted comment. –  Gilles Mar 2 '13 at 2:43

As @AshRj's comment above says, this depends on the default permissions the account has set. Essentially what is done is to start with all permissions (that includes, specifically, x for user/group/others) and turn off permissions specified by umask(2) (also see bash(1)). Today (each user has their own private group) umask is typically set to 002 (only deny wfor others), so the permissions of the executable turn out rwxrwxr-x, independent of the permissions on the sources. If other files are created (e.g. by the editor) the starting permissions lack x at all (a random text file isn't supposed to be run), so permissions would turn out rw-rw-r--.

share|improve this answer

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.