NSWI 0138: Advanced Unix Programming
Total Page:16
File Type:pdf, Size:1020Kb
NSWI 0138: Advanced Unix programming (c) 2011-2016 Vladim´ırKotal (c) 2009-2010 Jan Pechanec, Vladim´ırKotal SISAL MFF UK, Malostransk´en´am.25, 118 00 Praha 1 Charles University Czech Republic Vladim´ırKotal [email protected] March 10, 2016 1 Vladim´ırKotal NSWI 0138 (Advanced Unix programming) Contents 1 Overview 5 1.1 What is this lecture about? . .5 1.2 The lecture will cover... .5 1.3 A few notes on source code files . .6 2 Testing 6 2.1 Why?...........................................6 2.2 When ? . .6 2.3 Types of testing . .7 3 Debugging 8 3.1 Debuging in general . .8 3.2 Observing . .9 3.3 Helper tools . .9 3.3.1 ctags . .9 3.3.2 cscope . 10 3.3.3 OpenGrok . 11 3.3.4 Other tools . 11 3.4 Debugging data . 11 3.4.1 stabs . 11 3.4.2 DWARF . 12 3.4.3 CTF (Compact C Type Format) . 12 3.5 Resource leaks . 13 3.6 libumem . 13 3.6.1 How does libumem work . 14 3.6.2 Using libumem+mdb to find memory leaks . 14 3.6.3 How does ::findleaks work . 16 3.7 watchmalloc . 17 3.8 Call tracing . 18 3.9 Using /proc . 19 3.10 Debugging dynamic libraries . 20 3.11 Debuggers . 20 3.12 Symbol search and interposition . 20 3.13 dtrace . 21 4 Terminals 21 4.1 Terminal I/O Overview . 21 4.2 Terminal I/O Overview (cont.) . 22 4.3 Physical (Hardware) Terminal . 24 4.4 stty(1) command . 24 4.5 TTY Driver Connected To a Phy Terminal . 25 4.6 Pseudo terminals Overview . 26 4.7 PTY Driver Connected To a Process . 27 4.8 More Complex Example . 28 4.9 Reading From a Terminal . 29 4.10 Reading From a Terminal (cont.) . 30 4.11 Sessions and Process Groups (Jobs) . 30 4.12 Sessions and Process Groups (cont.) . 32 Page 2 of 87 Vladim´ırKotal NSWI 0138 (Advanced Unix programming) 4.13 Controlling Terminal . 33 4.14 Job Control . 34 4.15 What Happens If Job Control is Disabled? . 35 4.16 SIGHUP Signal . 35 4.17 So called hang-on-exit problem . 37 4.18 Function calls for sessions and process groups . 38 4.19 Opening a Terminal . 39 4.20 Working with Pseudoterminals . 39 4.21 Setting Terminal Attributes . 40 4.22 Terminfo database . 41 5 Advanced Network programming 42 5.1 Accessing low level structures . 42 5.2 More setsockopt() options . 43 5.3 Raw sockets . 44 5.4 Layer-2 (link) access . 45 5.5 Porting applications to IPv6 . 45 5.6 PF ROUTE socket . 47 5.7 PF KEY socket . 47 6 Advanced Thread Programming 48 6.1 Very Quick Recap of Threads . 49 6.2 Very Quick Recap of Threads (cont.) . 49 6.3 What we know (from POSIX API) . 50 6.4 Other Implementations . 50 6.5 Fork and Threads in POSIX . 51 6.6 Memory Leaks on fork(2) ............................... 52 6.7 Fork handlers - solution to fork-one safety issues . 52 6.8 Fork-Safe Library . 53 6.9 More on thread cancellation . 53 6.10 More on thread cancellation (cont.) . 54 6.11 Cancel-Safety in Libraries . 54 6.12 MT-Level Attribute in General . 55 6.13 Solaris Threads . 55 6.14 Solaris Threads API . 56 6.15 Differences between POSIX and Solaris Threads . 57 6.16 Combining both APIs . 57 6.17 Threads and Performance . 58 7 Advanced IPC and I/O 59 7.1 Advanced IPC and I/O . 59 7.2 Known Means of IPC . 59 7.3 ioctl(2) . 60 7.4 Example: /dev/crypto on Solaris . 60 7.5 Doors Overview . 61 7.6 Doors - how it works . 62 7.7 Sending file descriptors between processes . 63 7.8 Passing fd over a pipe (Solaris) . 63 7.9 Passing file descriptor over a socket . 63 7.10 Real-time signals - motivation . 64 Page 3 of 87 Vladim´ırKotal NSWI 0138 (Advanced Unix programming) 7.11 Using real-time signals . 64 7.12 Using real-time signals (continuted) . 65 7.13 Asynchronous versus Synchronous Programming . 65 7.14 Asynchronous I/O (AIO) { motivation . 66 7.15 Asynchronous POSIX I/O . 67 7.16 The aiocb structure . 67 7.17 Using POSIX Asynchronous I/O . 68 7.18 popen() and pclose() ................................ 68 7.19 popen() and pclose() example code . 69 7.20 Mechanisms for Creating a Process . 69 7.21 posix spawn(), posix spawnp() .......................... 70 7.22 Using posix spawn() ................................. 70 7.23 File actions . ..