Processes and the File System

When a process starts, it is assigned a current working directory that is inherited from its parent process, unless it is a shell process which gets its assignment from the home directory of the user who creates the shell. The home directory is part of the information stored for every user in the password file for the system. Processes use their current working directory to discover the location of files within the file hierarchy.

There are two ways to indicate the placement of files in the file system. The absolute pathname describes the position of a file starting at the root. For example, in the figure above, the absolute pathname for the file myprog.c is /home/master/myprog.c . Notice that an absolute pathname always begins with the slash (/) for the root directory. That is its give away. In an absolute pathname we write the name of every directory in the path from root to the required file, each name separated by slashes. The other way to indicate file placement is with the relative pathname, and this is where the current working directory is needed. A relative pathname for a process is based on its current working directory. For example, if the user named master opens a shell, and its home directory is /home/master, then this will also be its current working directory. Now, under these circumstances, all that the shell needs to do to refer to the file myprog.c is to name it, because it is already placed in its current working directory. There is no need to add any directory name.If on the other hand, the shell wants to refer to the file named data under the margaret directory, it will have to indicate the path to this file from the current working directory. This will go something like this: ../margaret/data . Notice this time that the relative pathname does not begin with the slash for the root directory, instead it begins with the dot-dot (..), indicating that from the master directory (the current working directory), the search must go up to its parent directory (home), and from there it should go down to the directory named margaret to find the file data.

While in the shell, the user may change its current working directory with the command cd. This command takes a pathname as a parameter (absolute or relative) which becomes the new current working directory. Used without parameters, cd moves the current working directory backs to the home directory. If a C program wants to retrieve the current working directory, it may use the getcwd system call that returns it as string (char *). The parameters for this function call are a string with enough space to receive the pathname and the size of this string. If that string is not long enough, the function call will fail. To change this current working directory, the C program may use the chdir system call that takes the new pathname as a parameter and returns the integer 0 for success or -1 in error. The prototypes for both system calls are as follows:

char * getcwd (char *cwdbuf, size_t size )
int chdir ( const char *pathname )

To access a file, it is not enough to know the placement of a file in a file system, we must also be aware of the file permissions granted to the file. File permissions are divided in three groups: permissions for the owner of a file, permissions for the group to which the file belongs, and permissions for anyone else. Each one of these groups may have the following permissions: permission to read the file, permission to write on the file and permission to execute the file (used if the file is a compiled program or a script). The same permissions are granted for directories, but with slight different meanings. Reading a directory means to be able to see its contents, writing in a directory means to be able to modify its contents, but the execution permission is actually a search permission. It allows for the search of a particular entity inside the directory.

We can observe the permissions on every file or directory if we use the command ls -la. The following figure shows samples of this command applied to the directories master and margaret in the file system depicted before, by user named master:

$ ls -la
drwx------   21 master   master    4096 Jun  5 22:14 .
drwx-xr--x    3 root     root        20 Apr  2 16:40 ..
-rw-r--r–     1 master   master    1167 Jul 26 12:36 myprog.c

$ ls -la ../margaret
drwx------    3 margaret margaret  4096 May  6 11:10 .
drwx-xr--x    3 root     root        20 Apr  2 16:40 ..
-rw-r--r–     1 margaret margaret 10423 Jun  6 17:14 data

$

The results presented by this command show the permissions on the left side. The first character indicates that kind of file being observed (- for a regular file, d for a directory). This is followed by three sets of three characters, for user, group and other users. For each of these sets there is a character for the read permission (r), the write permission (w) or the execute/search permission(x). If any of these permissions is not granted, then a dash (-) is being displayed instead. For example, the myprog.c and data files both allow read and write permissions for the owners (master and margaret, respectively) , and read only permissions for their group and everyone else.

After the permissions, the results of the command also show the number of links the file has, the owner of the file, the group to which it belongs, the size in bytes, the last date and time that was modified and the file name and extension. With the permissions above, if the master user wants to read the data file in the margaret directory, it may do so because read permissions have been granted to everyone. However, if it tries to modify it, it cannot, because it is neither the owner, and it does not belong to the same group as the file, and the permissions for everyone do not include the write permission.

Owners and privileged users may change the permissions of a file with the chmod shell command. The most basic structure of this command is as follows:

chmod [OPTION]... MODE FILE...

This command will change the permissions of the files mentioned in the files parameter as indicated by the mode. There are two ways to indicate the mode, explicitly and implicitly. The explicit mode mentions the users affected (u for user, g for group, o for other, or a for all) followed by an operation symbol (+ to add, - to remove, or = to set and remove unspecified) followed by the previously mentioned symbols for the permissions (r to read, w to write, and x to execute/search). For example if the margaret user is going to allow everyone to read and write in her data file, not just the owner, it may use any of the following alternatives:

chmod g+w,o+w data
chmod g=rw,o=rw data
chmod =rw data

The implicit mode can only be used to assign all permissions at once. It assigns values to the various permissions: 4 to read, 2 to write and 1 to executer/search. Each one of these values is used at face value if we are given permissions to other users, but they are multiplied by 10 if we are giving permissions to the group, or by 100 if the permissions are granted to the owner. All these values must be added and the final number is used as the mode. For example if the margaret user is going to grant read and write permissions to everyone we need to add 4+2 for everyone with 40+20 for the group and 400+200 for the owner, giving a total of 666 that can be used as the mode.

When creating a file (with the open system call) or a directory (with mkdir) we can specify their permissions with a parameter, however, these permissions are modified by the process’s file mode creation mask, also known as the umask. Every process inherits a umask from its parent process. This contains a set of permissions that will be eliminated in every file the process creates. A typical umask contains the value 022, representing the write permission for group and other users. This eliminates this permissions from all new files and directories the process creates.