Commit Graph

383 Commits

Author SHA1 Message Date
Jonas 'Sortie' Termansen 3c43f71084 Implement file descriptor passing.
This change refactors the Unix socket / pipe backend to have a ring buffer
containing segments, where each segment has an optional leading ancillary
buffer containing control messages followed by a normal data buffer.

The SCM_RIGHTS control message has been implemented which transfers file
descriptors to the receiving process. File descriptors are reference counted
and cycles are prevented using the following restrictions:

1) Unix sockets cannot be sent on themselves (on either end).
2) Unix sockets themselves being sent cannot be sent on.
3) Unix sockets cannot send a Unix socket being sent on.

This is a compatible ABI change.
2021-12-31 22:24:11 +01:00
Jonas 'Sortie' Termansen b9898086c6 Add file descriptor table reservations.
The file descriptor table now allows reserving room for multiple file
descriptors without assigning their numbers. This functionality means
any error conditions happen up front and the subsequent number
assignment will never fail.

This change uses the new functionality to fix troublesome error handling
when allocating multiple file descriptors. One pty allocation error path
was even wrong.

There were subtle race conditions where one (kernel) thread may have
allocated one file descriptor, and another thread spuciously replaces it
with something else, and then the second file descriptor allocation
failed in the first thread, and it closes the first file descriptor now
pointing to a different file description. This case seems harmless but
it's not a great class of bugs to exist in the first place. The new
behavior means the file descriptions appear in the file descriptor table
without fail and never needs to be cleaned up midway and is certainly
immune to shenangians from other threads.

Reviewed-by: Pedro Falcato <pedro.falcato@gmail.com>
2021-12-31 22:24:07 +01:00
Jonas 'Sortie' Termansen 20648e03d7 Send SIGCHLD to init even for reparented processes. 2021-12-12 22:13:05 +01:00
Juhani Krekelä c6e989909f Add header for working with the PS/2 mouse protocol. 2021-12-12 20:34:07 +02:00
Jonas 'Sortie' Termansen db5b51d673 Fix spurious ptable assertion in the fork error path. 2021-10-10 00:07:06 +02:00
Jonas 'Sortie' Termansen 63ce55e7e9 Fix reading directories not failing with EISDIR. 2021-07-28 22:21:41 +02:00
Jonas 'Sortie' Termansen 16bdb2ba84 Fix setuid(2) and setgid(2) not setting the effective user and group.
Thanks to samis for discovering this problem and the initial attempt at
fixing it.
2021-07-27 00:46:42 +02:00
Juhani Krekelä 6385ea1957 Fix pipes reporting themselves as character devices through fstat(2). 2021-07-16 01:43:33 +03:00
Jonas 'Sortie' Termansen 2d841bae7c Fix kernel deadlock in ppoll(2). 2021-07-14 15:41:28 +02:00
Jonas 'Sortie' Termansen 80f5ca398a Add ATAPI support to ata(4). 2021-06-27 13:54:56 +02:00
Jonas 'Sortie' Termansen 5e7605fad2 Implement threading primitives that truly sleep.
The idle thread is now actually run when the system is idle because it
truly goes idle. The idle thread is made power efficient by using the hlt
instruction rather than a busy loop.

The new futex(2) system call is used to implement fast user-space mutexes,
condition variables, and semaphores. The same backend and design is used as
kutexes for truly sleeping kernel mutexes and condition variables.

The new exit_thread(2) flag EXIT_THREAD_FUTEX_WAKE wakes a futex.

Sleeping on clocks in the kernel now uses timers for true sleep.

The interrupt worker thread now truly sleeps when idle.

Kernel threads are now named.

This is a compatible ABI change.
2021-06-23 22:10:47 +02:00
Jonas 'Sortie' Termansen 4daedc31f7 Fix handling of overflow and non-canonical values in timespec APIs.
Support zero relative and absolute times in the timer API.
2021-06-22 21:48:27 +02:00
Jonas 'Sortie' Termansen 3b036b6c5d Add getdnsconfig(2) and setdnsconfig(2). 2021-06-13 23:27:52 +02:00
Jonas 'Sortie' Termansen 9d87d76957 Abort on overlapping memcpy. 2021-05-30 23:48:19 +02:00
Juhani Krekelä 7f9a62d916 Implement Alt + key mapping to Esc followed by key in kernel tty 2021-05-05 00:19:56 +02:00
Jonas 'Sortie' Termansen c25f36cd53 Fix faccessat(2) not supporting root access. 2021-02-17 23:11:31 +01:00
Jonas 'Sortie' Termansen a6295e6d90 Allow poll on all kinds of files. 2021-02-17 23:11:31 +01:00
Jonas 'Sortie' Termansen fb106181a9 Fix sigsuspend(2) not running signal handlers with the temporary mask. 2021-02-13 22:35:39 +01:00
Jonas 'Sortie' Termansen aadeef6a7c Fix psctl(2) not setting uid, euid, gid, and egid. 2021-02-11 20:47:49 +01:00
Jonas 'Sortie' Termansen fff849b151 Fix improper uses of ScopedLockSignal. 2021-02-11 20:47:47 +01:00
Jonas 'Sortie' Termansen 7139de4a53 Add stty(1). 2020-11-25 20:47:19 +01:00
Jonas 'Sortie' Termansen 20d4c09e26 Undefine the eol character in the default terminal settings. 2020-11-22 17:39:55 +01:00
Jonas 'Sortie' Termansen be02a7996b Fix kramfs not initializing the owner of new directories. 2020-10-18 13:36:28 +02:00
Jonas 'Sortie' Termansen 245ec6458a Fix poll(2) not just clearing revents on negative fds. 2020-03-15 23:04:28 +01:00
Jonas 'Sortie' Termansen 20c1f1d0d4 Add signal mask support to ppoll(2). 2018-12-08 22:54:28 +01:00
Jonas 'Sortie' Termansen 9993a1c0fc Add --append-to kernel(7) multiboot module option. 2018-12-08 22:54:28 +01:00
Jonas 'Sortie' Termansen d0ab651fbd Document the options for the kernel(7) multiboot modules. 2018-12-08 22:54:28 +01:00
Jonas 'Sortie' Termansen b3e9865e53 Handle SOCK_NONBLOCK at the file descriptor level. 2018-12-08 22:34:50 +01:00
Jonas 'Sortie' Termansen 1f659c49c6 Fix multiboot header not coming first in the executable.
The default linker script places .text.unlikely prior to .text.
2018-12-08 22:34:49 +01:00
Jonas 'Sortie' Termansen d3a64e1163 Fix chmod(2), chown(2), and utimens(2) opening the path for writing.
The change 9d29e96c3b "Fix open(2) allowing
opening directories invalidly and check O_TRUNC errors." broke the chmod(2),
chown(2), and utimens(2) system calls on directories, because they can no
longer be opened for writing.

This changes fixes the regression by opening such paths for reading. There
is currently no filesystem permission checks for those system calls. However,
those system calls should check the permissions at the time of the operation
rather than relying on the file having been opened for writing previously.
2018-09-01 17:24:40 +02:00
Jonas 'Sortie' Termansen b1ad606303 Fix system calls panicing when creating threads in the kernel process.
This fixes a regression in 62bd9bf901.
2018-08-12 23:24:42 +02:00
Kartik Agaram 9d29e96c3b Fix open(2) allowing opening directories invalidly and check O_TRUNC errors.
Among other things, redirecting to a directory will now display an error
as it should.

Also fix a bug when opening /dev/pts: O_WRITE on a directory is a POSIX
violation.
2018-08-09 23:19:07 +02:00
Jonas 'Sortie' Termansen b767063c9a Fix deadlock when resizing display when a graphical process is running. 2018-08-08 23:46:19 +02:00
Jonas 'Sortie' Termansen 969a3e599b Fix signal dispatch if sigreturn is NULL. 2018-08-07 23:55:57 +02:00
Jonas 'Sortie' Termansen ab228e1062 Fix Clock::LockLock setting we_disabled_interrupts before disabling interrupts. 2018-08-07 23:55:57 +02:00
Jonas 'Sortie' Termansen 5be7b52422 Fix LFBTextBuffer GetChar starting worker thread when paused. 2018-08-06 23:59:35 +02:00
Jonas 'Sortie' Termansen 62bd9bf901 Fix pid 1 deadlocking when exiting with children.
The child processes of pid 1 were being reparented to pid 1, causing an
infinite loop. This change fixes the problem by adding a hook that runs in
the last thread about to exit in a process. When pid 1 exits, the hook will
prevent more processes and threads from being created, and then broadcast
kill all processes and threads. The hook is not run in LastPrayer(), as that
function runs in a worker thread and it can't block waiting for another
thread to run LastPrayer() in the same thread.
2018-08-06 23:59:35 +02:00
Jonas 'Sortie' Termansen c14e6c05b9 Fix waitpid(2) when the status pointer is NULL. 2018-08-06 23:59:35 +02:00
Jonas 'Sortie' Termansen 29555d72bb Fix <fcntl.h> including <sys/stat.h> namespace pollution.
POSIX allows <fcntl.h> to include <sys/stat.h>, but doesn't require it.
There's little reason to do that, since they are separate headers, and
<fcntl.h> just needs the mode_t constants. Fix the code accidentally
relying on <fcntl.h> including <sys/stat.h>. The mode_t constants are now
provided in their own kernel header <sortix/mode.h>.

Additionally fix <sys/stat.h> pulling in all of <sys/types.h>, which is not
allowed by POSIX, which only requires a few types to be declared. Fix the
code accidentally relying on <sys/stat.h> including <sys/types.h>.

Finally fix <dirent.h> pulling in <stdint.h> through <sortix/dirent.h>.

The <sortix/__/dt.h> and <sortix/__/stat.h> headers are no longer required
and their contents have been merged into <sortix/__/dirent.h>.
2018-08-06 23:59:34 +02:00
Jonas 'Sortie' Termansen d393b67d72 Fix st_blksize and st_blocks kernel values. 2018-03-31 00:55:33 +02:00
Jonas 'Sortie' Termansen 66b35fb74c Fix incompatibilities in the kernel tar extraction. 2018-03-30 23:42:37 +02:00
Jonas 'Sortie' Termansen 65a960dab0 Fix trailing slashes in manifests created by the kernel. 2017-12-04 23:56:46 +01:00
Jonas 'Sortie' Termansen 568c97c77f Fix SEEK_END, file offset overflow, and read/write/mkpartition syscall bugs.
Fix SEEK_END seeking twice as far as requested. Centralize lseek handling in
one place and avoid overflow bugs. Inode lseek handlers now only need to
handle SEEK_END with offset 0. Prevent the file offset from ever going below
zero or overflowing.

Character devices are now not seekable, but lseek will pretend they are, yet
always stay at the file offset 0. pread/pwrite on character devices will now
ignore the file offset and call read/write.

This change prevents character devices from being memory mapped, notably
/dev/zero can no longer be memory mapped. None of the current ports seem
to rely on this behavior and will work with just MAP_ANONYMOUS.

Refactor read and write system calls to have a shared return statement for
both seekable and non-seekable IO.

Fix file offset overflow bugs in read and write system calls.

Fix system calls returning EPERM instead of properly returning EBADF when
the file has not been opened in the right mode.

Truncate IO counts and total vector IO length so the IO operation does not
do any IO beyond OFF_MAX. Truncate also total vector IO length for recvmsg
and sendmsg. Fail with EINVAL if total vector IO length exceeds SSIZE_MAX.

Don't stop early if the total IO length is zero, so zero length IO now block
on any locks internal to the inode.

Handle reads at the maximum file offset with an end of file condition and
handle writes of at least one byte at the maximum file offset by failing
with EFBIG.

Refactor UtilMemoryBuffer to store the file size using off_t instead of
size_t to avoid casts and keep file sizes in the off_t type. Properly
handle errors in the code, such as failing with EROFS instead of EBADF if
the backing memory is not writeable, and failing with EFBIG if writing
beyond the end of the file.

Fix mkpartition not rejecting invalid partition start offsets and lengths.
Strictly enforce partition start and length checks in the partition code.
Enforce partitions exist within regular files or block devices.

Fix a few indention issues.
2017-12-04 23:56:46 +01:00
Jonas 'Sortie' Termansen 9ee05a5afc Fix kernel/addralloc.cpp file header comment. 2017-08-20 12:44:59 +02:00
Jonas 'Sortie' Termansen c14d7fa3b8 Fix <signal.h> namespace pollution for strict C. 2017-08-20 12:44:33 +02:00
Jonas 'Sortie' Termansen 33dcd40c6d Fix system headers using C89 incompatible comments. 2017-07-10 13:37:46 +02:00
Jonas 'Sortie' Termansen 9321f4d78e Fix initrd_get_inode return value not being checked for error. 2017-07-02 21:55:37 +02:00
Pedro Falcato 087e3b3fda
Fail with ENOSYS on bad system calls. 2017-06-14 20:00:50 +01:00
Pedro Falcato c0a44e72a6
Clear the direction flag upon entry to the kernel. 2017-06-12 21:26:26 +01:00
Jonas 'Sortie' Termansen 9f9f4d4e3f Print an kernel error rather than panicing if init(8) exits abnormally.
This avoids the misimpression that a kernel bug happened and it avoids
clearing the screen of potentially useful information about an init(8)
crash.
2017-05-21 23:28:12 +02:00