Fork me on GitHub

root/kernel/syscalls/chown.c

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

DEFINITIONS

This source file includes following definitions.
  1. sys_chown

   1 /*
   2  * fiwix/kernel/syscalls/chown.c
   3  *
   4  * Copyright 2018-2021, Jordi Sanfeliu. All rights reserved.
   5  * Distributed under the terms of the Fiwix License.
   6  */
   7 
   8 #include <fiwix/types.h>
   9 #include <fiwix/kernel.h>
  10 #include <fiwix/fs.h>
  11 #include <fiwix/stat.h>
  12 #include <fiwix/errno.h>
  13 #include <fiwix/string.h>
  14 
  15 #ifdef __DEBUG__
  16 #include <fiwix/stdio.h>
  17 #include <fiwix/process.h>
  18 #endif /*__DEBUG__ */
  19 
  20 int sys_chown(const char *filename, __uid_t owner, __gid_t group)
  21 {
  22         struct inode *i;
  23         char *tmp_name;
  24         int errno;
  25 
  26 #ifdef __DEBUG__
  27         printk("(pid %d) sys_chown('%s', %d, %d)\n", current->pid, filename, owner, group);
  28 #endif /*__DEBUG__ */
  29 
  30         if((errno = malloc_name(filename, &tmp_name)) < 0) {
  31                 return errno;
  32         }
  33         if((errno = namei(tmp_name, &i, NULL, !FOLLOW_LINKS))) {
  34                 free_name(tmp_name);
  35                 return errno;
  36         }
  37 
  38         if(IS_RDONLY_FS(i)) {
  39                 iput(i);
  40                 free_name(tmp_name);
  41                 return -EROFS;
  42         }
  43         if(check_user_permission(i)) {
  44                 iput(i);
  45                 free_name(tmp_name);
  46                 return -EPERM;
  47         }
  48 
  49         if(owner == (__uid_t)-1) {
  50                 owner = i->i_uid;
  51         } else {
  52                 i->i_mode &= ~(S_ISUID);
  53                 i->i_ctime = CURRENT_TIME;
  54         }
  55         if(group == (__gid_t)-1) {
  56                 group = i->i_gid;
  57         } else {
  58                 i->i_mode &= ~(S_ISGID);
  59                 i->i_ctime = CURRENT_TIME;
  60         }
  61 
  62         i->i_uid = owner;
  63         i->i_gid = group;
  64         i->dirty = 1;
  65         iput(i);
  66         free_name(tmp_name);
  67         return 0;
  68 }

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