#! Add shebang support to exec

Hard

xv6 book Sect 2.3 Kernel organization

xv6 book Sect 2.5 Process overview

The exec system call can be used to load an executable file (program) into the current process’s memory and then jump to its so-called entry point. In this exercise, we will add support for shebang interpreted directive.

Unix implementations of exec traditionally include special handling for shell scripts. If the file to execute begins with the text #! (called shebang), then the first line is taken to be a program to run to interpret the file. For example, when you run exec myprog arg1 and myprog’s first line is #!/sh, then exec runs /sh with command line /sh myprog arg1.

In the following exercise, we will implement this convention in xv6.

User space: Modify sh to take an input script as argument

First, we need to extend the shell to take an input script as an argument. Take a look at sh.c. You can see that the shell is just a simple user programs that read its input line by line and for each line, executes the corresponding command in a new process (after calling fork).

  • Extend sh.c to execute a script with a given name if it is passed as an argument to the program. If no argument is given, preserve the original functionality of reading from the standard input. You can conveniently use the existing function execute_script.

    Now you can test your code by creating a simple shell script named hello.sh in the user directory with the following contents:

    #!sh
    echo hello world from a shell script!
    

    You can add this file to xv6 by adding the following line to the Makefile: EXTRA_FS_FILES += $U/hello.sh

  • Try executing the script in the xv6 shell: sh hello.sh. What happens?

The shebang starts with the symbol # and should, hence, be ignored as a commented line by the shell interpreter. However, the xv6 shell interpreter does not yet recognize comments!

  • Modify sh.c to handle comments correctly. Hint: look at the function processline.

Kernel space: Modify exec system call

Modify exec.c to support the shebang convention. Roughly, we need to turn a call from kexec(myscript.sh, [myscript.sh, args...]) to kexec(interpr, [interpr, myscript.sh, args...]). You can start from the code snippet below:

  if(/* TODO: shebang detected && no error */) {

    // Get interpreter name
    // TODO

    // Compute new arguments
    path = /* TODO */;
    argv = /* TODO */;

    iunlockput(ip); // Release the current lock on the file
    end_op();       // You will learn more about locking later in the semester

    return kexec(path, argv);
  }
  1. Check if the first line of the file is a shebang. Hint: Look at how the magic bytes of the ELF header are processed.
  2. Get the name of the interpreter (check what exactly the helper function below returns).
  3. Compute the arguments of kexec: path should be interpr and argv should be [interpr,your_script.sh,other args...]).
  4. You can limit the number of arguments and the length of the shebang but make sure to handle error cases correctly (look at the MAXARG macro).
  5. At the beginning of the program kexec acquired some locks. We have to release them with iunlockput(ip); end_op(). Don’t worry if you don’t understand this part. You will learn more on locks later in the lessons and homeworks,
  6. Now we can recursively call exec with the new arguments kexec(interpr, [interpr, myscript.sh, 0])!

Hint

Feel free to use the following helper functions to implement your shebang parsing:

// Helper function: Parse and extract shebang line
static int
parse_shebang(struct inode *ip, char *buf, int bufsize)
{
  int n_read = readi(ip, 0, (uint64)buf, 0, bufsize);
  if(n_read <= 0)
    return -1;

  // Check for shebang
  if(n_read <= 2 || buf[0] != '#' || buf[1] != '!')
    return -1;

  // Find end of line
  int i;
  for(i = 2; i < n_read; ++i) {
    if(buf[i] == 0 || buf[i] == '\n') {
      buf[i] = 0;
      break;
    }
  }

  // Shebang too long
  if(buf[i] != 0)
    return -1;
  return i;
}

This site uses Just the Docs, a documentation theme for Jekyll.