Skip to content

Commit b0bde62

Browse files
authored
Add fchmod (#73)
* Implement & wire up fchmod * Expose fchmod in libcos
1 parent 8dd9b7b commit b0bde62

6 files changed

Lines changed: 63 additions & 0 deletions

File tree

kernel/include/kernel/fs/vfs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ int do_chown (const char* path, uint64_t uid, uint64_t gid);
122122
int do_chroot (const char* path);
123123
int do_close (struct file* fd);
124124
int do_create (char* filename, inode** result, inode* parent);
125+
int do_fchmod (struct file* f, uint16_t mode);
125126
int do_fstat (struct file* fd, stat* buf);
126127
int do_fsync (struct file* fd);
127128
int do_getcwd (char* buf, size_t size);
@@ -149,6 +150,7 @@ uint64_t sys_chroot (uint64_t path);
149150
uint64_t sys_close (uint64_t fd);
150151
uint64_t sys_dup (uint64_t fd);
151152
uint64_t sys_dup2 (uint64_t oldfd, uint64_t newfd);
153+
uint64_t sys_fchmod (uint64_t fd, uint64_t mode);
152154
uint64_t sys_fstat (uint64_t fd, uint64_t buf);
153155
uint64_t sys_fsync (uint64_t fd);
154156
uint64_t sys_getcwd (uint64_t buf, uint64_t size);

kernel/include/kernel/syscall.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#define SYSCALL_SYS_CHOWN 27
5353
#define SYSCALL_SYS_FSYNC 28
5454
#define SYSCALL_SYS_CHROOT 29
55+
#define SYSCALL_SYS_FCHMOD 30
5556

5657
/* Memory syscalls */
5758

kernel/src/kernel/fs/vfs/fchmod.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* fchmod.c
3+
* Copyright (C) 2026 Aditya Kumar
4+
*
5+
* This program is free software; you can redistribute it and/or modify it under the terms of the
6+
* GNU General Public License as published by the Free Software Foundation; either version 2 of the
7+
* License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License along with this program; if
14+
* not, see <https://www.gnu.org/licenses/>.
15+
*/
16+
17+
#include <kernel/error.h>
18+
#include <kernel/fs/vfs.h>
19+
#include <kernel/process.h>
20+
#include <liballoc/liballoc.h>
21+
22+
int do_fchmod (struct file* f, uint16_t mode) {
23+
if (!f) return -EINVAL;
24+
inode* node = f->f_inode;
25+
if (!node) return -EINVAL;
26+
27+
node->i_perms = (node->i_perms & ~07777) | (mode & 07777);
28+
return 0;
29+
}
30+
31+
uint64_t sys_fchmod (uint64_t fd, uint64_t mode) {
32+
process* current = get_current_process ();
33+
if (fd >= MAX_FDS || !current || !current->p_fds[fd]) return -EINVAL;
34+
return (uint64_t)do_fchmod (current->p_fds[fd], mode);
35+
}

kernel/src/kernel/fs/vfs/vfs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ void init_vfs (inode* absolute_root) {
3838
register_syscall (SYSCALL_SYS_CLOSE, SYS1 (sys_close));
3939
register_syscall (SYSCALL_SYS_DUP, SYS1 (sys_dup));
4040
register_syscall (SYSCALL_SYS_DUP2, SYS2 (sys_dup2));
41+
register_syscall (SYSCALL_SYS_FCHMOD, SYS2 (sys_fchmod));
4142
register_syscall (SYSCALL_SYS_FSTAT, SYS2 (sys_fstat));
4243
register_syscall (SYSCALL_SYS_FSYNC, SYS1 (sys_fsync));
4344
register_syscall (SYSCALL_SYS_GETCWD, SYS2 (sys_getcwd));

lib/cos/include/arch/x86_64-cos/syscalls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#define SYSCALL_SYS_CHOWN 27
5151
#define SYSCALL_SYS_FSYNC 28
5252
#define SYSCALL_SYS_CHROOT 29
53+
#define SYSCALL_SYS_FCHMOD 30
5354

5455
/* Memory syscalls */
5556

lib/cos/src/fchmod.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* fchmod.c
3+
* Copyright (C) 2026 Aditya Kumar
4+
*
5+
* This program is free software; you can redistribute it and/or modify it under the terms of the
6+
* GNU General Public License as published by the Free Software Foundation; either version 2 of the
7+
* License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License along with this program; if
14+
* not, see <https://www.gnu.org/licenses/>.
15+
*/
16+
17+
#include <arch/x86_64-cos/syscalls.h>
18+
#include <unistd.h>
19+
20+
int fchmod (int __fildes, mode_t __mode) {
21+
return (int)syscall_ret (
22+
(long)syscall2 (SYSCALL_SYS_FCHMOD, (uint64_t)__fildes, (uint64_t)__mode));
23+
}

0 commit comments

Comments
 (0)