20 Commits

Author SHA1 Message Date
Enrico Fraccaroli ee2f95e51d Merge pull request #26 from fischerling/fix-ext2-write
Fix some regressions in ext2_write
2024-02-28 12:09:56 -05:00
Enrico Fraccaroli b617fccee4 Merge pull request #25 from fischerling/improve-text-printing
Improve text printing
2024-02-26 10:02:01 -05:00
Enrico Fraccaroli 14474130ad Merge pull request #24 from fischerling/improve-ls-long-output
ls: improve --long output format
2024-02-26 10:00:24 -05:00
Enrico Fraccaroli 18504170ec Merge pull request #23 from fischerling/add-some-man-pages
add simple manual pages for rmdir, mkdir, showpid
2024-02-26 09:59:16 -05:00
Enrico Fraccaroli 404f4c20f0 Merge pull request #22 from fischerling/fix-execvp
libc: prevent execvp to search for files containing '/'
2024-02-26 09:57:46 -05:00
Florian Fischer dea54c7dae libc: prevent execvpe to search for files containing '/'
The current implementation always searches for the file in PATH
if it is not an absolute path.
This prevents executing relative paths like ./executable.

As specified by exec(3) the p-family of exec functions only search if
the filename does not contain slash '/' characters.
2024-02-14 11:37:06 +01:00
Florian Fischer 44b1acf33c ext2: update the vfs_file_t length after writing
The vfs_file_t->length field is initialized but never used afterwards.
It seams correct to updated it after the inode size possibly changed.
2024-02-13 21:59:57 +01:00
Florian Fischer 4d4504486d ext2: write the inode back if its size is updated
If there is enough space in an inode but the file grows the
inode is never written back to the device and thus the new size is
not persisted.

	int fd = creat("foo", 660);
	write(fd, "foo", 3); // New blocks are allocated and inode written back
	write(fd, "\n", 1); // size is adapted but not written back
	close(fd);
	// Reading the file afterwards will only return "foo".

The code snippet reproduces the bug.

This is fixed by always writing the inode back to the device if
the size is updated.
2024-02-13 21:59:57 +01:00
Florian Fischer f7e53f9b00 ext2: do not update inode size when allocating new blocks
New blocks for an inode are allocated when data blocks
should be written and not enough blocks are allocated yet
or during allocating new direntries in ext2_allocate_direntry.

When writing the first time to a newly created file (inode.size==0),
new blocks must be allocated and the inode size is erroneously set to
(blocks_count / fs->blocks_per_block_count) * fs->block_size
regardless of the actually written data.

	int fd = creat("foo", 0660);
	write(fd, "foo", 3);

This code snippet will create a new inode with size 4096 instead
of the correct size 3.

ext2_write_inode_data will correctly set the size to 3.
However, ext2_allocate_block called by ext2_write_inode_block will
set the inode size to 4096.

This is fixed by removing the size update during ext2_allocate_block.
2024-02-13 21:51:04 +01:00
Florian Fischer beacbde52e man: use cat as pager to print man pages
This reduces the required code in man and additionally,
replaces the old broken read/puts implementation.

Puts expects a null-terminated string, but write does not return
a null-terminted string.
2024-02-13 13:15:58 +01:00
Florian Fischer cad4804a95 cat: improve exist status
Exit with status 1 if one or more arguments could not be printed.
2024-02-13 13:15:36 +01:00
Florian Fischer e2e608b073 cat: properly output the read data
Using puts to output data retrieved through write is broken.
Puts expects a null-terminated string, which is not returned by read.
Use write to exactly output the data previously read.
2024-02-13 13:15:29 +01:00
Florian Fischer bb1bca458e ls: improve --long output format
Add padding using zeros instead of whitespace to the date values.
2024-02-13 13:11:03 +01:00
Florian Fischer b489df26ba add simple manual pages for rmdir, mkdir, showpid 2024-02-13 13:10:01 +01:00
Enrico Fraccaroli 1832c77fdb Merge pull request #21 from fischerling/fix-creat-fd-flags
creat: set flags in opened file descriptor
2024-01-29 05:24:58 +01:00
Enrico Fraccaroli ce98a0d3de Merge pull request #20 from fischerling/fix-dir-permission-check
vfs: fix permission check when creating files
2024-01-29 05:24:06 +01:00
Florian Fischer 89e6d75627 creat: set flags in opened file descriptor
creat.2 is supposed to be equivalent to open.2 with the
flags O_WRONLY|O_CREAT|O_TRUNC.

However, in MentOS the fd returned by creat.2 has no flags whatsoever
and is thus not writable.

Set the flags of the file descriptor before returning it.
2024-01-28 21:50:30 +01:00
Florian Fischer 4c6cbc3360 vfs: fix permission check when creating files
When creating new files in a directory the creating process
must be allowed to write the directory not to read it.
2024-01-28 21:47:50 +01:00
Enrico Fraccaroli c3c2ba824e Merge pull request #19 from fischerling/fix-grub-entry
Fix grub entry
2024-01-25 16:00:59 +01:00
Florian Fischer 015768793b fix bootloader file name in GRUB config
Fixes: 0b75cd9c
2024-01-24 19:52:16 +01:00
10 changed files with 47 additions and 41 deletions
+8
View File
@@ -0,0 +1,8 @@
SYNOPSIS
mkdir DIRECTORY
DESCRIPTION
Create the DIRECTORY, if it does not exist.
OPTIONS
--help : shows command help.
+8
View File
@@ -0,0 +1,8 @@
SYNOPSIS
rmdir DIRECTORY
DESCRIPTION
Remove the DIRECTORY, if it is empty.
OPTIONS
--help : shows command help.
+5
View File
@@ -0,0 +1,5 @@
SYNOPSIS
showpid
DESCRIPTION
showpid uses getppid() to print the ID of the parent process.
+1 -1
View File
@@ -2,6 +2,6 @@ set timeout=5
set default=0
menuentry "MentOS" {
multiboot /boot/kernel-bootloader.bin
multiboot /boot/bootloader.bin
boot
}
+2 -16
View File
@@ -72,21 +72,7 @@ int execv(const char *path, char *const argv[])
int execvp(const char *file, char *const argv[])
{
if (!file || !argv || !environ) {
errno = ENOENT;
return -1;
}
if (file[0] == '/') {
return execve(file, argv, environ);
}
// Prepare a buffer for the absolute path.
char absolute_path[PATH_MAX] = { 0 };
// Find the file inside the entries of the PATH variable.
if (__find_in_path(file, absolute_path, PATH_MAX) == 0) {
return execve(absolute_path, argv, environ);
}
errno = ENOENT;
return -1;
return execvpe(file, argv, environ);
}
int execvpe(const char *file, char *const argv[], char *const envp[])
@@ -95,7 +81,7 @@ int execvpe(const char *file, char *const argv[], char *const envp[])
errno = ENOENT;
return -1;
}
if (file[0] == '/') {
if (strchr(file, '/')) {
return execve(file, argv, envp);
}
// Prepare a buffer for the absolute path.
+9 -4
View File
@@ -1413,8 +1413,6 @@ static int ext2_allocate_inode_block(ext2_filesystem_t *fs, ext2_inode_t *inode,
if (inode->blocks_count < blocks_count) {
// Set the blocks count.
inode->blocks_count = blocks_count;
// Update the size.
inode->size = (blocks_count / fs->blocks_per_block_count) * fs->block_size;
pr_debug("Setting the block count for inode `%d` to `%d` blocks.\n", inode_index, blocks_count / fs->blocks_per_block_count);
}
// Update the inode.
@@ -1557,6 +1555,10 @@ static ssize_t ext2_write_inode_data(ext2_filesystem_t *fs, ext2_inode_t *inode,
uint32_t end = offset + nbyte;
if (end > inode->size) {
inode->size = end;
if (ext2_write_inode(fs, inode, inode_index) == -1) {
pr_err("Failed to write the inode `%d`\n", inode_index);
return -1;
}
}
uint32_t start_block = offset / fs->block_size;
uint32_t end_block = end / fs->block_size;
@@ -2267,7 +2269,7 @@ static vfs_file_t *ext2_creat(const char *path, mode_t permission)
return NULL;
}
// Get the parent VFS node.
vfs_file_t *parent = vfs_open(parent_path, O_RDONLY, 0);
vfs_file_t *parent = vfs_open(parent_path, O_WRONLY, 0);
if (parent == NULL) {
errno = ENOENT;
return NULL;
@@ -2587,7 +2589,10 @@ static ssize_t ext2_write(vfs_file_t *file, const void *buffer, off_t offset, si
pr_err("Failed to read the inode `%s`.\n", file->name);
return -1;
}
return ext2_write_inode_data(fs, &inode, file->ino, offset, nbyte, (char *)buffer);
ssize_t written = ext2_write_inode_data(fs, &inode, file->ino, offset, nbyte, (char *)buffer);
// Update the file length
file->length = inode.size;
return written;
}
/// @brief Repositions the file offset inside a file.
+1
View File
@@ -58,6 +58,7 @@ int sys_creat(const char *path, mode_t mode)
// Set the file descriptor id.
task->fd_list[fd].file_struct = file;
task->fd_list[fd].flags_mask = O_WRONLY|O_CREAT|O_TRUNC;
// Return the file descriptor and increment it.
return fd;
+7 -3
View File
@@ -18,9 +18,10 @@ static inline void print_content(const char *path, char *buffer, unsigned buflen
// Open the file.
int fd = open(path, O_RDONLY, 42);
if (fd >= 0) {
ssize_t bytes_read = 0;
// Put on the standard output the characters.
while (read(fd, buffer, buflen) > 0) {
puts(buffer);
while ((bytes_read = read(fd, buffer, buflen)) > 0) {
write(STDOUT_FILENO, buffer, bytes_read);
}
// Close the file descriptor.
close(fd);
@@ -45,6 +46,7 @@ int main(int argc, char **argv)
return 0;
}
}
int status = 0;
// Prepare the buffer for reading.
char buffer[BUFSIZ];
// Iterate the arguments.
@@ -60,16 +62,18 @@ int main(int argc, char **argv)
} else if (S_ISDIR(statbuf.st_mode)) {
printf("cat: %s: Is a directory\n\n", argv[i]);
status = 1;
} else if (S_ISLNK(statbuf.st_mode)) {
if (readlink(argv[i], buffer, BUFSIZ)) {
print_content(buffer, buffer, BUFSIZ);
} else {
printf("cat: %s: %s\n\n", argv[i], strerror(errno));
status = 1;
}
}
}
putchar('\n');
putchar('\n');
return 0;
return status;
}
+1 -1
View File
@@ -77,7 +77,7 @@ static inline void print_dir_entry(dirent_t *dirent, const char *path, unsigned
// Add a space.
putchar(' ');
// Print the rest.
printf("%4d %4d %11s %2d/%2d %2d:%2d %s\n",
printf("%4d %4d %11s %02d/%02d %02d:%02d %s\n",
dstat.st_uid,
dstat.st_gid,
to_human_size(dstat.st_size),
+5 -16
View File
@@ -40,6 +40,7 @@ int main(int argc, char *argv[])
}
else if (argc == 2)
{
char *pager = "cat";
char filepath[PATH_MAX];
strcpy(filepath, "/usr/share/man/");
strcat(filepath, argv[1]);
@@ -47,23 +48,11 @@ int main(int argc, char *argv[])
int fd = open(filepath, O_RDONLY, 42);
if (fd < 0)
{
printf("%s: No manual entry for %s\n\n", argv[0], argv[1]);
}
else
{
// Prepare the buffer for reading the man file.
char buffer[BUFSIZ];
// Put on the standard output the characters.
while (read(fd, buffer, BUFSIZ) > 0)
{
puts(buffer);
}
// Close the file descriptor.
close(fd);
// Terminate with a pair of newlines.
putchar('\n');
putchar('\n');
printf("%s: No manual entry for %s\n", argv[0], argv[1]);
exit(1);
}
close(fd);
execlp(pager, pager, filepath);
}
return 0;
}