Examen Unix, septembre 1997

1. Commande rm

Voici une suite de commandes exécutées par l'utilisateur betrema :
almira% pwd
/home/betrema/tmp
almira% ls -l Of*
-rw-r--r--  2 labesse     36573 Nov 20  1996 OffreDell.ps
almira% rm OffreDell.ps
rm: override protection 644 for OffreDell.ps? y
almira%
Comment expliquer que betrema puisse effacer un fichier qui ne lui appartient pas ? Que signifie la ``protection 644'' ? Quelle est précisément l'action effectuée par le systèmeme lorsqu'il exécute cette commande rm ?

2. Appel-système stat

Lire en annexe (un extrait de) la page de manuel de stat, puis répondre aux questions suivantes :
  1. Où se trouve, dans un système de fichiers Unix, l'information retournée par stat ?

  2. Expliquer la signification et le rôle des champs st_ino, st_mode et st_nlink.

  3. Expliquer pourquoi le champ st_blocks ne peut pas être calculé à partir du champ st_size.

  4. Les autorisations d'accès sont définies par trois bits : read, write, execute ; pourquoi le bit execute est-il nommé execute/search dans la page de manuel ?

  5. Expliquer brièvement le rôle de chacun des sept types de fichiers.

  6. Dans quel fichier la macro S_ISDIR est-elle définie ? Proposer une version de cette macro. Ecrire une courte fonction repertoire (char *chemin) qui teste si le fichier nommé chemin est un répertoire.

3. Mémoire

Voici un programme C :
#include <unistd.h>
#include <stdlib.h>

void *allouer (int n) {
  void *p = malloc (n);
  void *b = sbrk (0);
  printf ("malloc : %u (0x%x) brk : %u (0x%x)\n", p, p, b, b);
  return p;
}

void liberer (void *p) {
  void *b;
  free (p);
  b = sbrk (0);
  printf ("   brk : %u (0x%x)\n", b, b);
}

void main (void) {
  void *p, *q;
  allouer (100);
  p = allouer (10000);
  q = allouer (1000);
  liberer (p);
  liberer (q);
  allouer (500);
}
et le résultat de son exécution :
malloc : 133672 (0x20a28) brk : 141856 (0x22a20)
malloc : 133784 (0x20a98) brk : 150048 (0x24a20)
malloc : 143792 (0x231b0) brk : 150048 (0x24a20)
   brk : 150048 (0x24a20)
   brk : 150048 (0x24a20)
malloc : 143792 (0x231b0) brk : 150048 (0x24a20)
On rappelle que sbrk(0) retourne l'adresse de la fin du segment de données (on trouvera en annexe un extrait de la page de manuel). Commenter en détail cette exécution.

4. Annexe

NAME
     stat - get file status

SYNOPSIS
     #include 
     #include 

     int stat(path, buf)
     char *path;
     struct stat *buf;

DESCRIPTION
     stat() obtains information about the  file  named  by  path.
     buf is a pointer to a stat structure into which  information
     is  placed  concerning  the file.  A stat structure includes
     the following members:
     
          dev_t     st_dev;        /* device file resides on */
          ino_t     st_ino;        /* the file serial number */
          mode_t    st_mode;       /* file mode */
          nlink_t   st_nlink;      /* number of hard links to the file */
          uid_t     st_uid;        /* user ID of owner */
          gid_t     st_gid;        /* group ID of owner */
          [...]
          off_t     st_size;       /* total size of file, in bytes */
          long      st_blocks;     /* actual number of blocks allocated */
          time_t    st_atime;      /* file last access time */
          time_t    st_mtime;      /* file last modify time */
          time_t    st_ctime;      /* file last status change time */

     The following macros test whether a file is of the specified
     type.   The  value  m  is  the value of st_mode.  Each macro
     evaluates to a non-zero value if the test is true or to zero
     if the test is false.

     S_ISDIR(m)     Test for directory file.
     S_ISCHR(m)     Test for character special file.
     S_ISBLK(m)     Test for block special file.
     S_ISREG(m)     Test for regular file.
     S_ISLNK(m)     Test for a symbolic link.
     S_ISSOCK(m)    Test for a socket.
     S_ISFIFO(m)    Test for pipe or FIFO special file.
     
     [...]

     In addition, the following bits and masks are made available
     for backward compatibility:
     #define    S_IFMT    0170000  /* type of file */
     #define    S_IFIFO   0010000  /* FIFO special */
     #define    S_IFCHR   0020000  /* character special */
     #define    S_IFDIR   0040000  /* directory */
     #define    S_IFBLK   0060000  /* block special */
     #define    S_IFREG   0100000  /* regular file */
     #define    S_IFLNK   0120000  /* symbolic link */
     #define    S_IFSOCK  0140000  /* socket */
     
     #define    S_IREAD   0000400  /* read permission, owner */
     #define    S_IWRITE  0000200  /* write permission, owner */
     #define    S_IEXEC   0000100  /* execute/search permission, owner */

--------------------------------------------------------------------
NAME
     brk, sbrk - change the amount of  space  allocated  for  the
     calling process's data segment

SYNOPSIS
     #include 

     int brk(void *endds);

     void *sbrk(int incr);

DESCRIPTION
     brk() and sbrk() are used to change dynamically  the  amount
     of  space  allocated  for the calling process's data segment
     (see  exec(2)).   The  change  is  made  by  resetting   the
     process's  break value and allocating the appropriate amount
     of space.  The break value is the address of the first loca-
     tion  beyond  the  end  of  the data segment.
     [...]
     brk() sets the break value to endds and  changes  the  allo-
     cated space accordingly.

     sbrk() adds incr bytes to the break value  and  changes  the
     allocated space accordingly.  incr can be negative, in which
     case the amount of allocated space is decreased.

     Upon successful completion, brk() returns a value of  0  and
     sbrk() returns the old break value.