Fork me on GitHub

root/kernel/syscalls/unlink.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sys_unlink

   1 /*
   2  * fiwix/kernel/syscalls/unlink.c
   3  *
   4  * Copyright 2018, Jordi Sanfeliu. All rights reserved.
   5  * Distributed under the terms of the Fiwix License.
   6  */
   7 
   8 #include <fiwix/fs.h>
   9 #include <fiwix/syscalls.h>
  10 #include <fiwix/stat.h>
  11 #include <fiwix/errno.h>
  12 #include <fiwix/string.h>
  13 
  14 #ifdef __DEBUG__
  15 #include <fiwix/stdio.h>
  16 #include <fiwix/process.h>
  17 #endif /*__DEBUG__ */
  18 
  19 int sys_unlink(const char *filename)
  20 {
  21         struct inode *i, *dir;
  22         char *tmp_name, *basename;
  23         int errno;
  24 
  25 #ifdef __DEBUG__
  26         printk("(pid %d) sys_unlink('%s')\n", current->pid, filename);
  27 #endif /*__DEBUG__ */
  28 
  29         if((errno = malloc_name(filename, &tmp_name)) < 0) {
  30                 return errno;
  31         }
  32         if((errno = namei(tmp_name, &i, &dir, !FOLLOW_LINKS))) {
  33                 if(dir) {
  34                         iput(dir);
  35                 }
  36                 free_name(tmp_name);
  37                 return errno;
  38         }
  39         if(S_ISDIR(i->i_mode)) {
  40                 iput(i);
  41                 iput(dir);
  42                 free_name(tmp_name);
  43                 return -EPERM;  /* Linux returns -EISDIR */
  44         }
  45         if(IS_RDONLY_FS(i)) {
  46                 iput(i);
  47                 iput(dir);
  48                 free_name(tmp_name);
  49                 return -EROFS;
  50         }
  51         if(check_permission(TO_EXEC | TO_WRITE, dir) < 0) {
  52                 iput(i);
  53                 iput(dir);
  54                 free_name(tmp_name);
  55                 return -EACCES;
  56         }
  57 
  58         /* check sticky permission bit */
  59         if(dir->i_mode & S_ISVTX) {
  60                 if(check_user_permission(i)) {
  61                         iput(i);
  62                         iput(dir);
  63                         free_name(tmp_name);
  64                         return -EPERM;
  65                 }
  66         }
  67 
  68         basename = get_basename(filename);
  69         if(dir->fsop && dir->fsop->unlink) {
  70                 errno = dir->fsop->unlink(dir, i, basename);
  71         } else {
  72                 errno = -EPERM;
  73         }
  74         iput(i);
  75         iput(dir);
  76         free_name(tmp_name);
  77         return errno;
  78 }

/* [previous][next][first][last][top][bottom][index][help] */