Welcome to The Fiwix Project
Your small UNIX-like kernel
1 /* 2 * fiwix/kernel/syscalls/fchown.c 3 * 4 * Copyright 2018-2022, Jordi Sanfeliu. All rights reserved. 5 * Distributed under the terms of the Fiwix License. 6 */ 7 8 #include <fiwix/kernel.h> 9 #include <fiwix/types.h> 10 #include <fiwix/fs.h> 11 #include <fiwix/stat.h> 12 #include <fiwix/process.h> 13 #include <fiwix/errno.h> 14 15 #ifdef __DEBUG__ 16 #include <fiwix/stdio.h> 17 #endif /*__DEBUG__ */ 18 19 int sys_fchown(unsigned int ufd, __uid_t owner, __gid_t group) 20 { 21 struct inode *i; 22 23 #ifdef __DEBUG__ 24 printk("(pid %d) sys_fchown(%d, %d, %d)\n", current->pid, ufd, owner, group); 25 #endif /*__DEBUG__ */ 26 27 CHECK_UFD(ufd); 28 i = fd_table[current->fd[ufd]].inode; 29 30 if(IS_RDONLY_FS(i)) { 31 return -EROFS; 32 } 33 if(check_user_permission(i)) { 34 return -EPERM; 35 } 36 37 if(owner == (__uid_t)-1) { 38 owner = i->i_uid; 39 } else { 40 i->i_mode &= ~(S_ISUID); 41 } 42 if(group == (__gid_t)-1) { 43 group = i->i_gid; 44 } else { 45 i->i_mode &= ~(S_ISGID); 46 } 47 48 i->i_uid = owner; 49 i->i_gid = group; 50 i->i_ctime = CURRENT_TIME; 51 i->dirty = 1; 52 return 0; 53 }