Updated What Happens When... (markdown)

Kevin Lange 2015-10-02 23:57:02 -07:00
parent 85f8204d45
commit b57aefae09

@ -36,4 +36,6 @@ Forking is the traditional Unix method of starting a new process. Forking involv
The original shell process will [`wait`](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/userspace/core/sh.c#L784-L794) on "all" of its "children" (one child process in this case), and - finding none of them to be finished - will [sleep](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/kernel/sys/process.c#L786) on an [event](https://en.wikipedia.org/wiki/Event_%28synchronization_primitive%29) to be raised when a child finishes later. Meanwhile, the new shell process will call `execvp` to replace itself with the `fetch` binary. `execvp` evaluates the `PATH` environment variable, searching the filesystem with [`open`](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/kernel/fs/vfs.c#L942-L946), [`readdir`](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/modules/ext2.c#L1238-L1256), and [`fstat`](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/kernel/sys/syscall.c#L256-L293) to find a binary named `fetch`. All of those filesystem operations call down into the EXT2 filesystem module, which will [read inodes](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/modules/ext2.c#L1093-L1112), which means [reading blocks](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/modules/ext2.c#L121) which means [reading sectors from the disk](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/modules/ata.c#L382) or memory in a [ramdisk](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/kernel/fs/ramdisk.c#L22-L36). Once `execvp` has found an appropriate binary, it then calls [`execve` system call](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/kernel/sys/syscall.c#L186) which will [load and parse the executable](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/kernel/misc/elf.c#L257-L262) to determine whether it's an ELF binary, shell script with `#!` line, or something else it can execute. Finding `fetch` to be an ELF binary, it will then continue [loading the rest of the binary](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/kernel/misc/elf.c#L16) and [copy it into memory](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/kernel/misc/elf.c#L73). [Arguments](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/kernel/misc/elf.c#L112-L121), environment variables, and ELF auxiliary variables will also be copied into place. Eventually we will [jump back to userspace](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/kernel/misc/elf.c#L156) at the binary's requested start address, which will bring us to [the crt0](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/toolchain/patches/newlib/toaru/crt0.s) startup code, which then jump to [`pre_main`](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/toolchain/patches/newlib/toaru/syscalls.c#L421-L439) which reads the environment variables, calls `init` functions, and then calls `main`.
## `fetch`
Now we're at the point where we can look at the code for `fetch` itself. We're at [`main()`](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/userspace/net/fetch.c#L76).
Now we're at the point where we can look at the code for `fetch` itself. We're at [`main()`](https://github.com/klange/toaruos/blob/e195298e5ba99956ee05a1ce91c0024ea95321db/userspace/net/fetch.c#L76).
_TODO: This is still a work in progress_