Update MentOs code to the latest development version.

This commit is contained in:
Enrico Fraccaroli
2021-10-04 11:44:02 +02:00
parent 6fd063984a
commit b01eccca2e
484 changed files with 42358 additions and 20285 deletions
+13
View File
@@ -0,0 +1,13 @@
/// MentOS, The Mentoring Operating system project
/// @file chdir.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall1(int, chdir, const char *, path)
_syscall1(int, fchdir, int, fd)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file close.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall1(int, close, int, fd)
+243
View File
@@ -0,0 +1,243 @@
/// MentOS, The Mentoring Operating system project
/// @file exec.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include <debug.h>
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "sys/stat.h"
#include "stdlib.h"
#include "string.h"
#include "stdarg.h"
#include "fcntl.h"
extern char **environ;
/// @brief Default `PATH`.
#define DEFAULT_PATH "/bin:/usr/bin"
/// @brief Finds an executable inside the PATH entries.
/// @param file The file to search.
/// @param buf The buffer where we will store the absolute path.
/// @param buf_len The length of the buffer.
/// @return 0 if we have found the file inside the entries of PATH,
/// -1 otherwise.
static inline int __find_in_path(const char *file, char *buf, size_t buf_len)
{
// Determine the search path.
char *PATH_VAR = getenv("PATH");
if (PATH_VAR == NULL) {
PATH_VAR = DEFAULT_PATH;
}
// Prepare a stat object for later.
stat_t stat_buf;
// Copy the path.
char *path = strdup(PATH_VAR);
// Iterate through the path entries.
char *token = strtok(path, ":");
while (token != NULL) {
strcpy(buf, token);
strcat(buf, "/");
strcat(buf, file);
if (stat(buf, &stat_buf) == 0) {
if (stat_buf.st_mode & S_IXUSR) {
// TODO: Check why `init` has problems with this free.
// To reproduce the problem use this in init.c:
// execvp("login", _argv);
free(path);
return 0;
}
}
token = strtok(NULL, ":");
}
free(path);
// We did not find the file inside PATH.
errno = ENOENT;
return -1;
}
_syscall3(int, execve, const char *, path, char *const *, argv, char *const *, envp)
int execv(const char *path, char *const argv[])
{
return execve(path, argv, environ);
}
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;
}
int execvpe(const char *file, char *const argv[], char *const envp[])
{
if (!file || !argv || !envp) {
errno = ENOENT;
return -1;
}
if (file[0] == '/') {
return execve(file, argv, envp);
}
// Prepare a buffer for the absolute path.
char absolute_path[PATH_MAX];
// 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, envp);
}
errno = ENOENT;
return -1;
}
int execl(const char *path, const char *arg, ...)
{
va_list ap;
int argc;
// Phase 1: Count the arguments.
va_start(ap, arg);
for (argc = 1; va_arg(ap, const char *); ++argc) {
if (argc == INT_MAX) {
va_end(ap);
errno = E2BIG;
return -1;
}
}
va_end(ap);
// Phase 2: Store the arguments inside a vector.
char *argv[argc + 1];
va_start(ap, arg);
argv[0] = (char *)arg;
for (int i = 1; i <= argc; ++i)
argv[i] = va_arg(ap, char *);
va_end(ap);
// Now, we can call `execve` plain and simple.
return execve(path, argv, environ);
}
int execlp(const char *file, const char *arg, ...)
{
va_list ap;
int argc;
// Phase 1: Count the arguments.
va_start(ap, arg);
for (argc = 1; va_arg(ap, const char *); ++argc) {
if (argc == INT_MAX) {
va_end(ap);
errno = E2BIG;
return -1;
}
}
va_end(ap);
// Phase 2: Store the arguments inside a vector.
char *argv[argc + 1];
va_start(ap, arg);
argv[0] = (char *)arg;
for (int i = 1; i < argc; ++i)
argv[i] = va_arg(ap, char *);
va_end(ap);
// Close argv.
argv[argc] = NULL;
// Now, we can call `execve` plain and simple.
return execvpe(file, argv, environ);
}
int execle(const char *path, const char *arg, ...)
{
va_list ap;
int argc;
// Phase 1: Count the arguments.
va_start(ap, arg);
for (argc = 1; va_arg(ap, const char *); ++argc) {
if (argc == INT_MAX) {
va_end(ap);
errno = E2BIG;
return -1;
}
}
va_end(ap);
argc -= 1;
if (argc < 0) {
errno = EINVAL;
return -1;
}
// Phase 2: Store the arguments inside a vector.
char *argv[argc + 1];
va_start(ap, arg);
argv[0] = (char *)arg;
for (int i = 1; i < argc; ++i)
argv[i] = va_arg(ap, char *);
// Close argv.
argv[argc] = NULL;
// Phase 3: Store the pointer to the environ.
char **envp = va_arg(ap, char **);
va_end(ap);
// Now, we can call `execve` plain and simple.
return execve(path, argv, envp);
}
int execlpe(const char *file, const char *arg, ...)
{
va_list ap;
int argc;
// Phase 1: Count the arguments.
va_start(ap, arg);
for (argc = 1; va_arg(ap, const char *); ++argc) {
if (argc == INT_MAX) {
va_end(ap);
errno = E2BIG;
return -1;
}
}
va_end(ap);
// We don't need to count `envp` among the arguments.
if ((argc -= 1) < 0) {
errno = EINVAL;
return -1;
}
// Phase 2: Store the arguments inside a vector.
char *argv[argc + 1];
va_start(ap, arg);
argv[0] = (char *)arg;
for (int i = 1; i < argc; ++i)
argv[i] = va_arg(ap, char *);
// Close argv.
argv[argc] = NULL;
// Phase 3: Store the pointer to the environ.
char **envp = va_arg(ap, char **);
va_end(ap);
// Now, we can call `execve` plain and simple.
return execvpe(file, argv, envp);
}
+15
View File
@@ -0,0 +1,15 @@
/// MentOS, The Mentoring Operating system project
/// @file exit.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
void exit(int status)
{
long __res;
__inline_syscall1(__res, exit, status);
// The process never returns from this system call!
}
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file fork.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall0(pid_t, fork)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file getcwd.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall2(char *, getcwd, char *, buf, size_t, size)
+12
View File
@@ -0,0 +1,12 @@
/// MentOS, The Mentoring Operating system project
/// @file getdents.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall3(int, getdents, int, fd, dirent_t *, dirp, unsigned int, count)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file getgid.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall0(pid_t, getgid)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file getpid.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall0(pid_t, getpid)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file getppid.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall0(pid_t, getppid)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file getsid.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall1(pid_t, getsid, pid_t, pid)
+6
View File
@@ -0,0 +1,6 @@
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall1(int, alarm, int, seconds)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file kill.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall2(int, kill, pid_t, pid, int, sig)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file open.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall3(off_t, lseek, int, fd, off_t, offset, int, whence)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file mkdir.c
/// @brief Make directory functions.
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall2(int, mkdir, const char *, path, mode_t, mode)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file nice.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall1(int, nice, int, inc)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file open.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall3(int, open, const char *, pathname, int, flags, mode_t, mode)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file read.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall3(ssize_t, read, int, fd, void *, buf, size_t, nbytes)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file reboot.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd, void *, arg)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file rmdir.c
/// @brief Make directory functions.
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall1(int, rmdir, const char *, path)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file setgid.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall1(int, setgid, pid_t, pid)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file setsid.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall0(pid_t, setsid)
+103
View File
@@ -0,0 +1,103 @@
/// MentOS, The Mentoring Operating system project
/// @file signal.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "signal.h"
#include "sys/bitops.h"
static const char *sys_siglist[] = {
"HUP",
"INT",
"QUIT",
"ILL",
"TRAP",
"ABRT",
"EMT",
"FPE",
"KILL",
"BUS",
"SEGV",
"SYS",
"PIPE",
"ALRM",
"TERM",
"USR1",
"USR2",
"CHLD",
"PWR",
"WINCH",
"URG",
"POLL",
"STOP",
"TSTP",
"CONT",
"TTIN",
"TTOU",
"VTALRM",
"PROF",
"XCPU",
"XFSZ",
NULL,
};
_syscall2(sighandler_t, signal, int, signum, sighandler_t, handler)
_syscall3(int, sigaction, int, signum, const sigaction_t *, act, sigaction_t *, oldact)
_syscall3(int, sigprocmask, int, how, const sigset_t *, set, sigset_t *, oldset)
const char *strsignal(int sig)
{
if ((sig >= SIGHUP) && (sig < NSIG))
return sys_siglist[sig - 1];
return NULL;
}
int sigemptyset(sigset_t *set)
{
if (set) {
set->sig[0] = 0;
return 0;
}
return -1;
}
int sigfillset(sigset_t *set)
{
if (set) {
set->sig[0] = ~0UL;
return 0;
}
return -1;
}
int sigaddset(sigset_t *set, int signum)
{
if (set && ((signum))) {
bit_set_assign(set->sig[(signum - 1) / 32], ((signum - 1) % 32));
return 0;
}
return -1;
}
int sigdelset(sigset_t *set, int signum)
{
if (set) {
bit_clear_assign(set->sig[(signum - 1) / 32], ((signum - 1) % 32));
return 0;
}
return -1;
}
int sigismember(sigset_t *set, int signum)
{
if (set)
return bit_check(set->sig[(signum - 1) / 32], (signum - 1) % 32);
return -1;
}
+15
View File
@@ -0,0 +1,15 @@
/// MentOS, The Mentoring Operating system project
/// @file stat.c
/// @brief Stat functions.
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "sys/stat.h"
_syscall2(int, stat, const char *, path, stat_t *, buf)
_syscall2(int, fstat, int, fd, stat_t *, buf)
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file unlink.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall1(int, unlink, const char *, path)
+37
View File
@@ -0,0 +1,37 @@
/// MentOS, The Mentoring Operating system project
/// @file waitpid.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "sys/wait.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
pid_t waitpid(pid_t pid, int *status, int options)
{
pid_t __res;
int __status = 0;
do {
__inline_syscall3(__res, waitpid, pid, &__status, options);
if (__res < 0)
break;
if (__status == EXIT_ZOMBIE)
break;
if (options && WNOHANG)
break;
} while (1);
if (status)
*status = __status;
__syscall_return(pid_t, __res);
}
pid_t wait(int *status)
{
return waitpid(-1, status, 0);
}
+11
View File
@@ -0,0 +1,11 @@
/// MentOS, The Mentoring Operating system project
/// @file write.c
/// @brief
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
_syscall3(ssize_t, write, int, fd, void *, buf, size_t, nbytes)