Skip to content

Commit

Permalink
Merge pull request #113 from Galfurian/develop
Browse files Browse the repository at this point in the history
Add Error Checks to PCI Functions and Reorganize libc Structure
  • Loading branch information
Galfurian authored Oct 30, 2024
2 parents 3ed113d + 98f2c48 commit 5b0fcbb
Show file tree
Hide file tree
Showing 137 changed files with 1,852 additions and 988 deletions.
4 changes: 2 additions & 2 deletions doc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ if (DOXYGEN_FOUND)
${CMAKE_SOURCE_DIR}/libc/inc/sys/wait.h
${CMAKE_SOURCE_DIR}/libc/inc/sys/list_head.h
${CMAKE_SOURCE_DIR}/libc/inc/sys/kernel_levels.h
${CMAKE_SOURCE_DIR}/libc/inc/sys/dirent.h
${CMAKE_SOURCE_DIR}/libc/inc/sys/ioctl.h
${CMAKE_SOURCE_DIR}/libc/inc/sys/mman.h
${CMAKE_SOURCE_DIR}/libc/inc/sys/utsname.h
${CMAKE_SOURCE_DIR}/libc/inc/sys/unistd.h
${CMAKE_SOURCE_DIR}/libc/inc/sys/stat.h
${CMAKE_SOURCE_DIR}/libc/inc/sys/list_head_algorithm.h
${CMAKE_SOURCE_DIR}/libc/inc/grp.h
${CMAKE_SOURCE_DIR}/libc/inc/unistd.h
${CMAKE_SOURCE_DIR}/libc/inc/dirent.h
${CMAKE_SOURCE_DIR}/libc/inc/io/ansi_colors.h
${CMAKE_SOURCE_DIR}/libc/inc/io/mm_io.h
${CMAKE_SOURCE_DIR}/libc/inc/io/debug.h
Expand Down
568 changes: 372 additions & 196 deletions doc/syscall.md

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions libc/inc/sys/dirent.h → libc/inc/dirent.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,12 @@ typedef struct dirent_t {
unsigned short d_type; ///< type of the directory entry.
char d_name[NAME_MAX]; ///< Filename (null-terminated)
} dirent_t;

/// Provide access to the directory entries.
/// @param fd The fd pointing to the opened directory.
/// @param dirp The buffer where de data should be placed.
/// @param count The size of the buffer.
/// @return On success, the number of bytes read is returned. On end of
/// directory, 0 is returned. On error, -1 is returned, and errno is set
/// appropriately.
ssize_t getdents(int fd, dirent_t *dirp, unsigned int count);
4 changes: 4 additions & 0 deletions libc/inc/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ unsigned randuint(unsigned lb, unsigned ub);
/// @param ub the upper-bound value.
/// @return the random value.
float randfloat(float lb, float ub);

/// @brief Wrapper for exit system call.
/// @param status The exit status.
void exit(int status);
11 changes: 11 additions & 0 deletions libc/inc/sys/reboot.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@
#define LINUX_REBOOT_CMD_SW_SUSPEND 0xD000FCE2
/// Restart system using a previously loaded Linux kernel
#define LINUX_REBOOT_CMD_KEXEC 0x45584543

/// @brief Reboots the system, or enables/disables the reboot keystroke.
/// @param magic1 fails (with the error EINVAL) unless equals LINUX_REBOOT_MAGIC1.
/// @param magic2 fails (with the error EINVAL) unless equals LINUX_REBOOT_MAGIC2.
/// @param cmd The command to send to the reboot.
/// @param arg Argument passed with some specific commands.
/// @return For the values of cmd that stop or restart the system, a
/// successful call to reboot() does not return. For the other cmd
/// values, zero is returned on success. In all cases, -1 is
/// returned on failure, and errno is set appropriately.
int reboot(int magic1, int magic2, unsigned int cmd, void *arg);
263 changes: 222 additions & 41 deletions libc/inc/system/syscall_types.h

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions libc/inc/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ typedef struct timespec {
/// @return The current time.
time_t time(time_t *t);

/// @brief Converts the given time to a string representing the local time.
/// @details Converts the value pointed to by timer, representing the time in
/// seconds since the Unix epoch (1970-01-01 00:00:00 UTC), to a string in the
/// format: Www Mmm dd hh:mm:ss yyyy.
/// @param timer A pointer to a time_t object representing the time to be
/// converted.
/// @return A pointer to a statically allocated string containing the formatted
/// date and time. The string is overwritten with each call to ctime().
char *ctime(const time_t *timer);

/// @brief Return the difference between the two time values.
/// @param time1 The first time value.
/// @param time2 The second time value.
Expand Down
52 changes: 14 additions & 38 deletions libc/inc/sys/unistd.h → libc/inc/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "sys/types.h"
#include "stddef.h"
#include "sys/dirent.h"
#include "dirent.h"

#define STDIN_FILENO 0 ///< Standard input file descriptor.
#define STDOUT_FILENO 1 ///< Standard output file descriptor.
Expand Down Expand Up @@ -71,21 +71,17 @@ int symlink(const char *linkname, const char *path);
/// @return The number of read characters on success, -1 otherwise and errno is set to indicate the error.
int readlink(const char *path, char *buffer, size_t bufsize);

/// @brief Wrapper for exit system call.
/// @param status The exit status.
extern void exit(int status);

/// @brief Returns the process ID (PID) of the calling process.
/// @return pid_t process identifier.
extern pid_t getpid(void);
pid_t getpid(void);

///@brief Return session id of the given process.
/// If pid == 0 return the SID of the calling process
/// If pid != 0 return the SID corresponding to the process having identifier == pid
///@param pid process identifier from wich we want the SID
///@return On success return SID of the session
/// Otherwise return -1 with errno set on: EPERM or ESRCH
extern pid_t getsid(pid_t pid);
pid_t getsid(pid_t pid);

///@brief creates a new session if the calling process is not a
/// process group leader. The calling process is the leader of the
Expand All @@ -95,7 +91,7 @@ extern pid_t getsid(pid_t pid);
/// is made the same as its process ID).
///@return On success return SID of the session just created
/// Otherwise return -1 with errno : EPERM
extern pid_t setsid(void);
pid_t setsid(void);

///@brief returns the Process Group ID (PGID) of the process specified by pid.
/// If pid is zero, the process ID of the calling process is used.
Expand All @@ -112,56 +108,56 @@ int setpgid(pid_t pid, pid_t pgid);

///@brief returns the real group ID of the calling process.
///@return GID of the current process
extern gid_t getgid(void);
gid_t getgid(void);

///@brief returns the effective group ID of the calling process.
///@return GID of the current process
extern gid_t getegid(void);
gid_t getegid(void);

///@brief sets the group IDs of the calling process.
///@param gid the Group ID to set
///@return On success, zero is returned.
/// Otherwise returns -1 with errno set to :EINVAL or EPERM
extern int setgid(gid_t gid);
int setgid(gid_t gid);

///@brief sets the real and effective group IDs of the calling process.
///@param rgid the new real Group ID.
///@param egid the effective real Group ID.
///@return On success, zero is returned.
/// Otherwise returns -1 with errno set EPERM
extern int setregid(gid_t rgid, gid_t egid);
int setregid(gid_t rgid, gid_t egid);

///@brief Returns the real User ID of the calling process.
///@return User ID of the current process.
extern uid_t getuid(void);
uid_t getuid(void);

///@brief Returns the effective User ID of the calling process.
///@return User ID of the current process.
extern uid_t geteuid(void);
uid_t geteuid(void);

///@brief Sets the User IDs of the calling process.
///@param uid the new User ID.
///@return On success, zero is returned.
/// Otherwise returns -1 with errno set to :EINVAL or EPERM
extern int setuid(uid_t uid);
int setuid(uid_t uid);

///@brief Sets the effective and real User IDs of the calling process.
///@param ruid the new real User ID.
///@param euid the effective real User ID.
///@return On success, zero is returned.
/// Otherwise returns -1 with errno set to EPERM
extern int setreuid(uid_t ruid, uid_t euid);
int setreuid(uid_t ruid, uid_t euid);

/// @brief Returns the parent process ID (PPID) of the calling process.
/// @return pid_t parent process identifier.
extern pid_t getppid(void);
pid_t getppid(void);

/// @brief Clone the calling process, but without copying the whole address space.
/// The calling process is suspended until the new process exits or is
/// replaced by a call to `execve'.
/// @return Return -1 for errors, 0 to the new process, and the process ID of
/// the new process to the old process.
extern pid_t fork(void);
pid_t fork(void);

/// @brief Replaces the current process image with a new process image (argument list).
/// @param path The absolute path to the binary file to execute.
Expand Down Expand Up @@ -241,17 +237,6 @@ int execvpe(const char *file, char *const argv[], char *const envp[]);
/// returned, and errno is set appropriately.
int nice(int inc);

/// @brief Reboots the system, or enables/disables the reboot keystroke.
/// @param magic1 fails (with the error EINVAL) unless equals LINUX_REBOOT_MAGIC1.
/// @param magic2 fails (with the error EINVAL) unless equals LINUX_REBOOT_MAGIC2.
/// @param cmd The command to send to the reboot.
/// @param arg Argument passed with some specific commands.
/// @return For the values of cmd that stop or restart the system, a
/// successful call to reboot() does not return. For the other cmd
/// values, zero is returned on success. In all cases, -1 is
/// returned on failure, and errno is set appropriately.
int reboot(int magic1, int magic2, unsigned int cmd, void *arg);

/// @brief Get current working directory.
/// @param buf The array where the CWD will be copied.
/// @param size The size of the array.
Expand All @@ -270,15 +255,6 @@ int chdir(char const *path);
/// @return 0 on success, -1 on failure and errno is set to indicate the error.
int fchdir(int fd);

/// Provide access to the directory entries.
/// @param fd The fd pointing to the opened directory.
/// @param dirp The buffer where de data should be placed.
/// @param count The size of the buffer.
/// @return On success, the number of bytes read is returned. On end of
/// directory, 0 is returned. On error, -1 is returned, and errno is set
/// appropriately.
ssize_t getdents(int fd, dirent_t *dirp, unsigned int count);

/// @brief Return a new file descriptor
/// @param fd The fd pointing to the opened file.
/// @return On success, a new file descriptor is returned.
Expand Down
4 changes: 3 additions & 1 deletion libc/src/abort.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
/// @copyright (c) 2014-2024 This file is distributed under the MIT License.
/// See LICENSE.md for details.

#include "sys/unistd.h"
#include "unistd.h"
#include "signal.h"
#include "stdio.h"
#include "string.h"
#include "grp.h"
#include "stdlib.h"

/// @brief Since there could be signal handlers listening for the abort, we need
/// to keep track at which stage of the abort we are.
Expand Down
38 changes: 20 additions & 18 deletions libc/src/err.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,43 @@
/// @brief Contains err functions
/// @copyright (c) 2024 This file is distributed under the MIT License.
/// See LICENSE.md for details.

#include <err.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/unistd.h>
#include <unistd.h>
#include <stdlib.h>

void verr(int status, const char *fmt, va_list ap)
{
if (fmt) {
vfprintf(STDERR_FILENO, fmt, ap);
fprintf(STDERR_FILENO, ": ");
}
perror(0);
exit(status);
if (fmt) {
vfprintf(STDERR_FILENO, fmt, ap);
fprintf(STDERR_FILENO, ": ");
}
perror(0);
exit(status);
}

void verrx(int status, const char *fmt, va_list ap)
{
if (fmt) vfprintf(STDERR_FILENO, fmt, ap);
fprintf(STDERR_FILENO, "\n");
exit(status);
if (fmt) vfprintf(STDERR_FILENO, fmt, ap);
fprintf(STDERR_FILENO, "\n");
exit(status);
}

void err(int status, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verr(status, fmt, ap);
va_end(ap);
va_list ap;
va_start(ap, fmt);
verr(status, fmt, ap);
va_end(ap);
}

void errx(int status, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verrx(status, fmt, ap);
va_end(ap);
va_list ap;
va_start(ap, fmt);
verrx(status, fmt, ap);
va_end(ap);
}
2 changes: 1 addition & 1 deletion libc/src/grp.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "stdio.h"
#include "string.h"
#include "sys/errno.h"
#include "sys/unistd.h"
#include "unistd.h"

/// Holds the file descriptor while we are working with `/etc/group`.
static int __fd = -1;
Expand Down
2 changes: 1 addition & 1 deletion libc/src/libc_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// @copyright (c) 2014-2024 This file is distributed under the MIT License.
/// See LICENSE.md for details.

#include "sys/unistd.h"
#include "unistd.h"
#include "assert.h"
#include "stdlib.h"
#include "string.h"
Expand Down
2 changes: 1 addition & 1 deletion libc/src/libgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "string.h"
#include <assert.h>
#include <stdlib.h>
#include <sys/unistd.h>
#include <unistd.h>

int dirname(const char *path, char *buffer, size_t buflen)
{
Expand Down
2 changes: 1 addition & 1 deletion libc/src/pwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "stdio.h"
#include "string.h"
#include "sys/errno.h"
#include "sys/unistd.h"
#include "unistd.h"

/// @brief Parses the input buffer and fills pwd with its details.
/// @param pwd the structure we need to fill.
Expand Down
2 changes: 1 addition & 1 deletion libc/src/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include "stdio.h"
#include "string.h"
#include "sys/unistd.h"
#include "unistd.h"

int readline(int fd, char *buffer, size_t buflen, ssize_t *read_len)
{
Expand Down
19 changes: 10 additions & 9 deletions libc/src/shadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
/// @copyright (c) 2005-2020 Rich Felker, et al.
/// This file is based on the code from libmusl.

#include <shadow.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/errno.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include <ctype.h>
#include "shadow.h"
#include "fcntl.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "sys/errno.h"
#include "unistd.h"
#include "sys/stat.h"
#include "ctype.h"
#include "limits.h"

/// Defines the buffer size for reading lines from the shadow file.
#define LINE_LIM 256
Expand Down
3 changes: 2 additions & 1 deletion libc/src/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#include "stdio.h"
#include "strerror.h"
#include "string.h"
#include "sys/unistd.h"
#include "unistd.h"
#include "limits.h"

void putchar(int character)
{
Expand Down
2 changes: 1 addition & 1 deletion libc/src/sys/mman.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "sys/mman.h"
#include "sys/errno.h"
#include "sys/unistd.h"
#include "unistd.h"
#include "system/syscall_types.h"

_syscall6(void *, mmap, void *, addr, size_t, length, int, prot, int, flags, int, fd, off_t, offset)
Expand Down
2 changes: 1 addition & 1 deletion libc/src/sys/unistd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// @copyright (c) 2014-2024 This file is distributed under the MIT License.
/// See LICENSE.md for details.

#include "sys/unistd.h"
#include "unistd.h"
#include "limits.h"
#include "stdio.h"
#include "string.h"
Expand Down
Loading

0 comments on commit 5b0fcbb

Please sign in to comment.