0

I'm currently learning about operating system kernels and how they are built from the source code (I'm using Minix).

I'm trying to figure out where the shell commands (ls for example) get executed. I know where to locate the ls.c file (src/bin/ls) I'm just not sure where it gets called when the user types it in the terminal.

My goal is to 'hijack' the ls command to accomplish a different result without editing the command file itself ls.c (for example, the ls command now shuts down the computer or echos a string out). In order to do that I need to know where the text from the user gets parsed and the ls command gets executed.

I looked around in the source and I believe it's located inside the process manager (src/minix/servers/pm) however, this was as far as I got before I got lost.

I know this is a very specific question but hopefully I get get it solved.
Thanks in advance

Cyberboy1551
  • 345
  • 1
  • 13
  • The shell will call one of the `exec` family of functions to load and execute programs. Please learn a little bit more about Unix systems programming first before you try to work on a Unix-like kernel. – Some programmer dude Oct 21 '21 at 06:08
  • If you want to "hijack", the simplest way: use shell alias, the second simplest way: move /bin/ls into /bin/ls.orig, and put a command or script in /bin/ls (which will call itself /bin/ls.orig on most cases). Note shells may implement ls built-in (for most common cases) and programs may call system directly. – Giacomo Catenazzi Oct 21 '21 at 06:38
  • @GiacomoCatenazzi Indeed shell builtins have aided in some of the most miraculous recoveries in UNIX history :) – user426 Oct 21 '21 at 07:55

1 Answers1

3

You are mixing two different questions together: where is the ls binary, and where is its source code. For the former question, you can use which to determine its absolute path. For example, on my FreeBSD box, which ls outputs /bin/ls. However, ls is a compiled binary file, so it cannot be "hijacked" easily without changing its source code and compiling it again, and that is the later question. You had already determined the correct path to ls: src/bin/ls, so you need to modify ls.c according to your needs and compile it, then optionally install it to your system. I am not quite familiar with Minix build system, but you can always consult the Minix documents to know how to install an updated userspace program.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Yuuta Liang
  • 118
  • 2
  • 7
  • Yeah, sorry if my question was kinda confusing. Ls was just an example. I've been looking around some more and found the file I was looking for (src/bin/sh/exec.c) which handles executing the commands from the cmdloop function in main.c within the same directory. My clarified question is: Where is the 'dictionary' of sorts that maps 'ls ...' to ls.c? How does the shell parse the text I type into the command? – Cyberboy1551 Oct 21 '21 at 06:20
  • @Cyberboy1551 There's no such "dictionary" to map commands to source files. You do know that C source files are *compiled* and *linked* into separate executable files, which are then totally unrelated to the source file? – Some programmer dude Oct 21 '21 at 06:34
  • @Cyberboy1551 As for the `exec` functions, it will look for the *executable* program to load and execute. I really implore you to take a few steps back and learn the basics of C programming first, and then the basics of Unix systems programming, before even attempting to work at the kernel-level. – Some programmer dude Oct 21 '21 at 06:36
  • @Cyberboy1551 There is dictionary mapping. The shell firstly parses your commands and arguments into an array with shell variables and control statements (this is a complex process). For example, `ls -al -h` is parsed into `ls` and an array of `["-al", "-h"]`. Then, the shell `fork()` and `exec(ls, )` the program. The location of `ls` is found using the `PATH` environment variable. – Yuuta Liang Oct 21 '21 at 06:40