Top Banner
http://codeschool.org/ This work is licensed under a Creative Commons Attribution- ShareAlike 3.0 Unported License. Unix system calls (part 1) history and usage of Python basic data types and the type hierarchy syntax modules and variable scopes
55

Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

Dec 14, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

http://codeschool.org/This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Unix system calls (part 1)

• history and usage of Python• basic data types and the type hierarchy• syntax• modules and variable scopes

Page 2: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

http://codeschool.org/This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Unix system calls(part 1)

Page 3: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

http://codeschool.org/This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

This is one part of a larger series. You may need to view previous parts to understand this material.

Page 4: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

It’s a Unix system!

Page 5: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

System V BSD

1980’s

Page 6: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

Linux Mac OS X

FreeBSD, OpenBSD

today

Page 7: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

POSIX (Portable Operating System Interface for Unix)

SUS (Single Unix Specification)

Page 8: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

Process C

Process B

kernel

Process A

jump to system call code via special instruction

RAM

Page 9: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

0x76 00 00 00system call 0

0x20 15 10 00system call 1

0x82 87 95 94system call 2

0xA2 22 00 10system call 3

0xFF 31 21 14system call 4

0xFF 31 01 11system call 5

0xFF 90 44 44system call 6

0xFF 31 01 11system call 7

… …

Page 10: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

stack

code

heap

heap

heap

kernel code pages only accessible in system calls

jump to system call code via

special instruction

Page 11: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

frame of main

frame of cat

frame of dog

frame of fishstack space

frame of syscall

Page 12: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

created

waiting running

blocked

terminated

Page 13: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

• processes• files• networking sockets• signals• inter-process communication• terminals• threads• I/O devices

Page 14: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

ssize_t read(int fd, void *buf, size_t count);

Page 15: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

ssize_t read(int fd, void *buf, size_t count);

read(fd)

Page 16: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

process:address spaceuser idsfile descriptorsenvironmentcurrent and root directory

stack

heap

code

heap

Page 17: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

stack

code

initialized data

heap

heap

kernel code

uninitialized data

global variables with initial values

global variables without initial values

Page 18: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

stack

code

initialized data

heap

heap

kernel code

uninitialized data

a.k.a. the “text”

global variables with initial values

global variables without initial values

starts empty, grows automatically

explicitly allocated during execution

Page 19: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

mmap(‘memory map’ pages to the process address space)

munmap(‘memory unmap’ pages from the process address space)

Page 20: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

mmap(‘memory map’ pages to the process address space)

munmap(‘memory unmap’ pages from the process address space)

address = mmap(5000)… # do stuff with memory at addressmunmap(address)

Page 21: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

stack

code

initialized data

heap

heap

kernel code

uninitialized data

heap

heap

mmap fails when not enough space

Page 22: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

garbage collection

Page 23: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

if fork() == 0: … // new (child) processelse: … // original (parent) process

Page 24: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

RAM

byte 0

byte n

HD

stack

heap

code

heap

fork

Page 25: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

stack

heap

code

heap

RAM

byte 0

byte n

HD

stack

heap

code

heap

fork

Page 26: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

stack

heap

code

heap

RAM

byte 0

byte n

HD

stack

heap

code

heap

fork

Page 27: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

stack

heap

code

heap

RAM

byte 0

byte n

HD

stack

heap

code

heap

write

fork

Page 28: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

stack

heap

code

heap

RAM

byte 0

byte n

HD

stack

heap

code

heap

copy

write

fork

Page 29: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

stack

heap

code

heap

exec

Page 30: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

code

exec

(executable)

Page 31: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

if fork() == 0: // new (child) process exec(‘/games/pong’)else: … // original (parent) process

Page 32: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

pid 1 (init)

pid 85 pid 17

pid 24pid 230

pid 104

pid 34

pid 50

Page 33: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

_exit(terminate the process)

_exit(0)

Page 34: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

wait(block the process until child process terminates)

pid = fork()if pid == 0: // new (child) process exec(‘/games/pong’)else: // original (parent) process code = wait(pid)

Page 35: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

TERM=xtermSHELL=/bin/bashUSER=greysMAIL=/var/mail/ted PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/binPWD=/home/tedEDITOR=vim

name=value

Page 36: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

pid 1 (init), user 0

pid 85, user 8 pid 17, user 4

pid 24, user 33pid 230, user 8

pid 104, user 33

pid 34, user 4

pid 50, user 4

Page 37: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

user accounts:

/etc/passwd

Page 38: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

user accounts:

/etc/passwd

superuser/root = user id 0

privileged to do anything it wants

Page 39: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

each process has three user ids:

each file and directory is owned by a single user

“real” id:the owning user

“effective” id:determines privileges

“saved” id:set by exec to match the effective id

Page 40: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

exec (sets effective and saved ids when binary file has setuid bit)

Page 41: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

exec (sets effective and saved ids when binary file has setuid bit)

seteuid (sets effective user id)

setuid (sets real, effective, and saved user ids)

Page 42: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

exec (sets effective and saved ids when binary file has setuid bit)

seteuid (sets effective user id)

setuid (sets real, effective, and saved user ids)

non-superuser can only directly set effective id to match the real or saved id

Page 43: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

pid 1 (init), user 0

pid 3 (shell), user 1780

pid 2 (login), user 0

Page 44: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

pid 1 (init), user 0

pid 3 (shell), user 1780

pid 2 (login), user 0

fork, exec

Page 45: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

pid 1 (init), user 0

pid 3 (shell), user 1780

pid 2 (login), user 0

fork, exec

fork, setuid, exec

Page 46: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

user groups:

/etc/group

• user may belong to multiple groups but has one “primary” group• each file and directory is owned by one group• each process has a real, effective, and saved group id• binary files have setgid bit• setegid and setgid

Page 47: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

rwx rwx rwxuser group other

Page 48: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

rwx rwx rwxuser group other

if file_user_id == effective_user_id:user class

else if file_group_id == effective_group_id:group class

else:other

Page 49: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

file permissions:read: can read bytes of filewrite: can modify bytes of fileexecute: can exec file

Page 50: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

directory permissions:read: can get names of fileswrite: can add/remove/rename filesexecute: can use in file paths

Page 51: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

directory permissions:read: can get names of fileswrite: can add/remove/rename filesexecute: can use in file paths

/adams/taft/garfield/eisenhower

Page 52: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

directory permissions:read: can get names of fileswrite: can add/remove/rename filesexecute: can use in file paths

/adams/taft/garfield/eisenhower/adams/taft/ (OK)

Page 53: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

r-xr-xr-xrw-r-----r-x--x--xrwx------

/adams/lincoln/adams/cleveland/roosevelt/fillmore

rwx rwx rwxuser group other

Page 54: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

dr-xrw-r-x /adams/

rwx rwx rwxuser group other

Page 55: Http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.

http://codeschool.org/

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.