|
PatchworkOS
da8a090
A non-POSIX operating system.
|
System IO header. More...
System IO header.
The sys/io.h header handles interaction with PatchworkOS's file system, following the philosophy that everything is a file. This means interacting with physical devices, inter-process communication (like shared memory), and much more is handled via files.
Functions like open() do not have a specific argument for flags, instead the filepath itself contains the flags. This means that for example there is no need for a special "truncate" redirect in a shell (>>) instead you can just add the "trunc" flag to the filepath and use a normal redirect (>).
Here is an example filepath: /this/is/a/path:with:some:flags.
Check the 'src/kernel/fs/path.h' file for a list of available flags.
Data Structures | |
| struct | pollfd_t |
| Poll file descriptor structure. More... | |
| struct | stat_t |
| Stat type. More... | |
| struct | dirent_t |
| Directory entry struct. More... | |
| struct | key_t |
| Key type. More... | |
Macros | |
| #define | STDIN_FILENO 0 |
| #define | STDOUT_FILENO 1 |
| #define | STDERR_FILENO 2 |
| #define | PIPE_READ 0 |
| Pipe read end. | |
| #define | PIPE_WRITE 1 |
| Pipe write end. | |
| #define | F_MAX_SIZE 512 |
Maximum buffer size for the F() macro. | |
| #define | F(format, ...) |
| Format string macro. | |
| #define | POLL_SPECIAL (POLLERR | POLLHUP | POLLNVAL) |
| Poll event values that will always be checked and included even if not specified. | |
| #define | KEY_SIZE 16 |
| Size of keys in bytes. | |
Typedefs | |
| typedef uint8_t | seek_origin_t |
Type for the seek() origin argument. | |
| typedef uint64_t | inode_number_t |
| Inode number enum. | |
Enumerations | |
| enum | poll_events_t { POLLNONE = 0 , POLLIN = (1 << 0) , POLLOUT = (1 << 1) , POLLERR = (1 << 2) , POLLHUP = (1 << 3) , POLLNVAL = (1 << 4) } |
| Poll events type. More... | |
| enum | inode_type_t { INODE_FILE , INODE_DIR } |
| Inode type enum. More... | |
| enum | mount_flags_t { MOUNT_NONE = 0 , MOUNT_PROPAGATE_PARENT = 1 << 0 , MOUNT_PROPAGATE_CHILDREN = 1 << 1 , MOUNT_OVERWRITE = 1 << 2 } |
| Mount flags type. More... | |
Functions | |
| fd_t | open (const char *path) |
| System call for opening files. | |
| uint64_t | open2 (const char *path, fd_t fd[2]) |
| System call for opening 2 file descriptors from one file. | |
| fd_t | openat (fd_t from, const char *path) |
| System call for opening files relative to another file descriptor. | |
| uint64_t | close (fd_t fd) |
| System call for closing files. | |
| uint64_t | read (fd_t fd, void *buffer, uint64_t count) |
| System call for reading from files. | |
| char * | sread (fd_t fd) |
| Wrapper for reading a file directly into a null-terminated string. | |
| uint64_t | write (fd_t fd, const void *buffer, uint64_t count) |
| System call for writing to files. | |
| uint64_t | swrite (fd_t fd, const char *string) |
| Wrapper for writing a null-terminated string to a file. | |
| uint64_t | readfile (const char *path, void *buffer, uint64_t count, uint64_t offset) |
| Wrapper for reading a file directly using a path. | |
| char * | sreadfile (const char *path) |
| Wrapper for reading an entire file directly into a null-terminated string. | |
| uint64_t | writefile (const char *path, const void *buffer, uint64_t count, uint64_t offset) |
| Wrapper for writing to a file directly using a path. | |
| uint64_t | swritefile (const char *path, const char *string) |
| Wrapper for writing a null-terminated string directly to a file using a path. | |
| uint64_t | seek (fd_t fd, int64_t offset, seek_origin_t origin) |
| System call for changing the file offset. | |
| uint64_t | chdir (const char *path) |
| System call for changing the cwd. | |
| uint64_t | poll (pollfd_t *fds, uint64_t amount, clock_t timeout) |
| System call for polling files. | |
| poll_events_t | poll1 (fd_t fd, poll_events_t events, clock_t timeout) |
| Wrapper for polling one file. | |
| uint64_t | stat (const char *path, stat_t *stat) |
| System call for retrieving info about a file or directory. | |
| uint64_t | ioctl (fd_t fd, uint64_t request, void *argp, uint64_t size) |
| System call for extended driver behaviour. | |
| fd_t | dup (fd_t oldFd) |
| System call for duplicating file descriptors. | |
| fd_t | dup2 (fd_t oldFd, fd_t newFd) |
| System call for duplicating file descriptors, with a destination. | |
| uint64_t | getdents (fd_t fd, dirent_t *buffer, uint64_t count) |
| System call for reading directory entires. | |
| uint64_t | mkdir (const char *path) |
| Wrapper for creating a directory. | |
| uint64_t | rmdir (const char *path) |
| Wrapper for removing a directory. | |
| uint64_t | link (const char *oldPath, const char *newPath) |
| System call for creating a hardlink. | |
| uint64_t | unlink (const char *path) |
| Wrapper for removing a file. | |
| uint64_t | share (key_t *key, fd_t fd, clock_t timeout) |
| System call for sharing a file descriptor with another process. | |
| fd_t | claim (key_t *key) |
| System call for claiming a shared file descriptor. | |
| uint64_t | bind (fd_t source, const char *mountpoint, mount_flags_t flags) |
| System call for binding a file descriptor to a mountpoint. | |
| #define PIPE_READ 0 |
| #define PIPE_WRITE 1 |
| #define F | ( | format, | |
| ... | |||
| ) |
Format string macro.
This macro is a helper to create formatted strings on the stack. Very useful for functions like open().
alloca(), but we then end up needing to set a maximum limit for how many F() strings can be used simultaneously. Using alloca() means we can use as many as we want, as long as we have enough stack space, even if it is more dangerous.F_MAX_SIZE. | typedef uint8_t seek_origin_t |
| typedef uint64_t inode_number_t |
| enum poll_events_t |
| enum inode_type_t |
| enum mount_flags_t |
Mount flags type.
The propagation flags apply recursively, such that specifying both MOUNT_PROPAGATE_PARENT and MOUNT_PROPAGATE_CHILDREN will propagate the mount to every namespace in the hierarchy.
| fd_t open | ( | const char * | path | ) |
System call for opening files.
The open() function opens a file located at a given path.
| path | The path to the desired file. |
ERR and errno is set. Definition at line 9 of file open.c.
System call for opening 2 file descriptors from one file.
This is intended as a more generic implementation of system calls like pipe() in for example Linux. One example use case of this system call is pipes, if open2 is called on /dev/pipe then fd[0] will store the read end of the pipe and fd[1] will store the write end of the pipe. But if open() is called on /dev/pipe then the returned file descriptor would be both ends.
| path | The path to the desired file. |
| fd | An array of two fd_t where the new file descriptors will be stored. |
ERR and errno is set. Definition at line 9 of file open2.c.
System call for opening files relative to another file descriptor.
| from | The file descriptor to open the file relative to, or FD_NONE to open from the current working directory. |
| path | The path to the desired file. |
ERR and errno is set. System call for reading from files.
| fd | The file descriptor to read from. |
| buffer | A pointer to the buffer where the data will be stored. |
| count | The maximum number of bytes to read. |
ERR and errno is set. Definition at line 9 of file read.c.
| char * sread | ( | fd_t | fd | ) |
Wrapper for reading a file directly into a null-terminated string.
The sread() function reads the entire contents of a file into a newly allocated null-terminated string. The caller is responsible for freeing the returned string.
| fd | The file descriptor to read from. |
NULL and errno is set. Definition at line 5 of file sread.c.
System call for writing to files.
| fd | The file descriptor to write to. |
| buffer | A pointer to the buffer containing the data to write. |
| count | The number of bytes to write. |
ERR and errno is set. Definition at line 9 of file write.c.
Wrapper for writing a null-terminated string to a file.
| fd | The file descriptor to write to. |
| string | The null-terminated string to write. |
ERR and errno is set. Definition at line 4 of file swrite.c.
Wrapper for reading a file directly using a path.
Equivalent to calling open(), seek(), read(), and close() in sequence.
| path | The path to the file. |
| buffer | A pointer to the buffer where the data will be stored. |
| count | The maximum number of bytes to read. |
| offset | The offset in the file to start reading from. |
ERR and errno is set. Definition at line 3 of file readfile.c.
| char * sreadfile | ( | const char * | path | ) |
Wrapper for reading an entire file directly into a null-terminated string.
The sreadfile() function reads the entire contents of a file into a newly allocated null-terminated string. The caller is responsible for freeing the returned string.
Equivalent to calling open(), sread(), and close() in sequence.
| path | The path to the file. |
NULL and errno is set. Definition at line 3 of file sreadfile.c.
Wrapper for writing to a file directly using a path.
Equivalent to calling open(), seek(), write(), and close() in sequence.
| path | The path to the file. |
| buffer | A pointer to the buffer containing the data to write. |
| count | The number of bytes to write. |
| offset | The offset in the file to start writing to. |
ERR and errno is set. Definition at line 3 of file writefile.c.
| uint64_t swritefile | ( | const char * | path, |
| const char * | string | ||
| ) |
Wrapper for writing a null-terminated string directly to a file using a path.
Equivalent to calling open(), swrite(), and close() in sequence.
| path | The path to the file. |
| string | The null-terminated string to write. |
ERR and errno is set. Definition at line 4 of file swritefile.c.
| uint64_t seek | ( | fd_t | fd, |
| int64_t | offset, | ||
| seek_origin_t | origin | ||
| ) |
System call for changing the file offset.
| fd | The file descriptor. |
| offset | The offset to move the file pointer. |
| origin | The origin that the offset is relative to (e.g., SEEK_SET, SEEK_CUR, SEEK_END). |
ERR and errno is set. Definition at line 9 of file seek.c.
| uint64_t chdir | ( | const char * | path | ) |
System call for polling files.
| fds | An array of pollfd_t structures, each specifying a file descriptor to poll in pollfd_t::fd and the events to wait for in pollfd_t::events. |
| amount | The number of pollfd_t structures in the fds array. |
| timeout | The maximum time (in clock ticks) to wait for an event. If CLOCKS_NEVER, it waits forever. |
ERR and errno is set. Definition at line 9 of file poll.c.
| poll_events_t poll1 | ( | fd_t | fd, |
| poll_events_t | events, | ||
| clock_t | timeout | ||
| ) |
Wrapper for polling one file.
The poll1() function waits for events on a single file descriptor. Otherwise it is identical to poll() and exists simply for convenience.
| fd | The file descriptor to poll. |
| events | The events to wait for (e.g., POLLIN, POLLOUT). |
| timeout | The maximum time (in clock ticks) to wait for an event. If CLOCKS_NEVER, it waits forever. |
POLLERR event bit is set and errno is set. Definition at line 9 of file poll1.c.
System call for retrieving info about a file or directory.
| path | The path to the file or directory. |
| stat | A pointer to a stat_t structure where the file information will be stored. |
ERR and errno is set. Definition at line 9 of file stat.c.
System call for extended driver behaviour.
The ioctl() function allows drivers to implement unusual behaviour that would be impossible or impractical with a normal file-based API.
| fd | The file descriptor of the file. |
| request | The driver-dependent request code. |
| argp | A pointer to an argument that depends on the request, can be NULL if size is 0. |
| size | The size of the argument pointed to by argp. |
ERR and errno is set. Definition at line 9 of file ioctl.c.
System call for duplicating file descriptors.
| oldFd | The open file descriptor to duplicate. |
ERR and errno is set. Definition at line 9 of file dup.c.
System call for duplicating file descriptors, with a destination.
| oldFd | The open file descriptor to duplicate. |
| newFd | The desired new file descriptor. |
ERR and errno is set. Definition at line 9 of file dup2.c.
System call for reading directory entires.
| fd | The file descriptor of the directory to read. |
| buffer | The destination buffer. |
| count | The size of the buffer in bytes. |
ERR and errno is set. Definition at line 9 of file getdents.c.
| uint64_t mkdir | ( | const char * | path | ) |
| uint64_t rmdir | ( | const char * | path | ) |
| uint64_t link | ( | const char * | oldPath, |
| const char * | newPath | ||
| ) |
| uint64_t unlink | ( | const char * | path | ) |
System call for sharing a file descriptor with another process.
Note that the file descriptor itself is not whats sent but the underlying file object.
| key | Output pointer to store the generated key. |
| fd | The file descriptor to share. |
| timeout | The time until the shared file descriptor expires. If CLOCKS_NEVER, it never expires. |
0. On failure, ERR and errno is set. Definition at line 6 of file share.c.
System call for claiming a shared file descriptor.
After claiming a shared file descriptor, the key is no longer valid and cannot be used again.
| key | Pointer to the key identifying the shared file descriptor. |
ERR and errno is set. Definition at line 6 of file claim.c.
| uint64_t bind | ( | fd_t | source, |
| const char * | mountpoint, | ||
| mount_flags_t | flags | ||
| ) |
System call for binding a file descriptor to a mountpoint.
| source | The file descriptor to bind, must represent a directory. |
| mountpoint | The mountpoint path. |
| flags | The mount flags. |
0. On failure, ERR and errno is set. Definition at line 5 of file bind.c.