180 likes | 192 Views
Learn how to build and run Linux in VMWare, explore the kernel, create a simple shell in C, add system calls, due Jan. 19. Understand system calls, implement a new syscall for statistics tracking in the Linux kernel, integrate it into your shell. Dive into Linux syscall implementation details. Programming in kernel mode requires specific rules and conventions.
E N D
Reminders • Homework 2 due tomorrow • Project 1 is out (as of yesterday) • Start thinking about project groups (3 people) for the rest of the quarter • You’ll be using those for the rest of the projects! • Today: project 1 overview
Project 1 • Teaches how to build and run Linux in VMWare • Introduction to digging around in the kernel • Two main parts: • Write a simple shell in C • Add a simple system call to Linux kernel • Due: Thurs, Jan 19, before section (1:00pm) • Electronic turnin: code + writeup
What does a shell do? • Print out prompt • Accept input • Parse input • If built-in command • do it directly • Else create new process • Launch specified program there • Wait for it to finish • Repeat CSE451Shell%/bin/date Fri Jan 16 00:05:39 PST 2004 CSE451Shell%pwd /root CSE451Shell%cd / CSE451Shell%pwd / CSE451Shell% exit
Writing a new shell • Just a C program that executes a big while loop • Has 2 internal commands - cd, exit • All other commands are invokable executables (given with full path names like /bin/ls) • Use fork to create a child process that executes the executable • Child should use ‘execv’ or one of its related versions (execvp, etc). See man pages for help. • Parent (shell) waits for child to finish • Use ‘wait’ function (man wait for help) • Useful version is ‘waitpid’ • Last step: add your execcounts syscall as a 3rd internal command
The shell + using C strings • Your shell will need to manipulate/parse C strings. You only need to use: • strncmp(src,dest,n) – compare first n chars of strings, 0 if equal, not 0 o.w. Do not do str1 == str2!!! • strtok: parse string into tokens • 1st use: tok = strtok(buf, “delimiters”); • Subsequent uses: tok = strtok(NULL, “delimiters”); • fgets(buf, n, stdin) – read a line (up to n=256 chars) from stdin (or getline) • (maybe) strncpy(dest, src, n) – copy up to n=256 chars from src to dest. • (maybe) Allocate memory with malloc, free with free • Fine to assume: • A maximum length for a shell command (say, n=256) • Maximum number of arguments (say, 256 again)
System Calls • What’s a system call? • Examples? • …? • Examples used in your shell: • Use fork to create a child process • Use execvp to execute a specified program • Use wait to wait until child process terminates
Part 2: Adding a System Call • Add execcounts system call to Linux: • Purpose: collect statistics • Count number of times you call fork, vfork, clone, and exec system calls. • Steps: • Modify kernel to keep track of this information • We give you the kernel code • Add execcounts wrapper to return the counts to the user • Use execcounts in your shell to get this data from kernel and print it out.
Example of execcounts CSE451Shell%execcounts clear CSE451Shell%cd / CSE451Shell%pwd / CSE451Shell%date Wed Sep 29 16:52:41 PDT 2004 CSE451Shell%time Usage: time [-apvV] [-f format] [-o file] [--append] [--verbose] [--portability] [--format=format] [--output=file] [--version] [--help] command [arg...] CSE451Shell%execcounts Statistics: Fork: 3 27% Clone: 0 0% VFork: 0 0% Exec: 8 72% CSE451Shell%exit
Linux syscall implementation • Called indirectly via interrupt, looked up in system call table • New syscall => new entry in table • How do they work in Linux? Consider a syscall: foo • foo is a wrapper function that calls syscall() with foo’s system call number • System call numbers defined in <asm/unistd.h> • Look for #define __NR_foo num • syscall() loads the number and parameters into registers, saves process state, raises interrupt (0x80 on x86 architecture) • Control flow jumps to kernel at location ‘system_call’ • Looks up number in system call table to find kernel function (in entry.S assembly file) (Added 1-13-06: in linux-2.6.13.2, use syscall_table.S) • Kernel function ‘sys_foo()’ executes • Control/process state restored back to user • This is a 50,000 foot aerial view of linux sys calls • You’ll need to dive in for a ground-level view • Suggestion: google. There are lots of tutorials out there for adding sys calls to linux.
Howto: add linux syscall • What you’ll need to do: • Add new system call to the sys_call_table • Define unique sys call number for execcounts (the “stub”) • Write the actual function: sys_execcounts • Modify any kernel code necessary to track info needed by sys_execcounts • Add appropriate header files • Probably need a structure to hold execcounts info! • Finally, integrate as internal command ‘execcounts’ for your new shell
Programming in kernel mode • Your shell will operate in user mode • Your system call code will be in the Linux kernel, which operates in kernel mode • Be careful - different programming rules, conventions, etc.
Programming in kernel mode • Can’t use application libraries (e.g. libc) • E.g. can’t use printf • Use only functions defined by the kernel • E.g. use printk instead • Include files are different in the kernel • Don’t forget you’re in kernel space • E.g. unsafe to access a pointer from user space directly, use fn’s that perform checks • Example: copy_to_user, copy_from_user functions (don’t use user pointers explicitly! -- unsafe) • Best way to learn – look at existing code • Look how the other syscalls are implemented
Computing Resources • Develop your code on dedicated 451 Linux hosts: • forkbomb • Each of you has a scratch space at: /cse451/<username> • Test your code on VMWare PCs in 006 • Do not use attu • Work through the lab information on the web page • “Linux information” on web page is helpful
VMWare • Software simulation of x86 architecture • Run an OS in a sandbox • Easily reset to known good state
Using VMWare Power on/off, reset VMWare config Don’t change! • All disks are nonpersistent • Powering off loses your changes! Use “shutdown –r now” instead • Network adapter is host-only
Linux && VMWare • There is only one user: root • The password is rootpassword • You will need to: • Build a kernel image on forkbomb • Transfer it to Linux running inside VMWare • Boot your new Linux kernel in VMWare • Use ftp to get your files into VMWare • FTP to 192.168.93.2 from the host running VMWare. • E.g. using IE, go to ftp://root:rootpassword@192.168.93.2
UNIX & C help • How do I find stuff in the kernel source? • Use grep –r search_string * • Use LXR (Linux Cross Reference): http://lxr.linux.no/ • Which library functions does C have to simplify my shell code? • man strncmp, gets, fgets, strtok, strchr, perror