Part 1 Getting Started 1
Chapter 1 History of Linux Development 3
1.1 A Short History of Free Unix Software 4
1.2 Development of Linux 6
1.3 Notional Lineage of Unix Systems 7
1.4 Linux Lineage 9
Chapter 2 Licenses and Copyright 11
2.1 Copyright 11
2.2 Licensing 13
2.3 Free Software Licenses 14
2.3.1 The GNU General Public License 15
2.3.2 The GNU Library General Public License 15
2.3.3 MIT/X/BSD-Style Licenses 16
2.3.4 Old BSD-Style Licensses 16
2.3.5 Artistic License 16
2.3.6 License Incompatibilities 17
Chapter 3 Online System Documentation 19
3.1 The man Pages 19
3.2 The Info Pages 20
3.3 Other Documentation 21
Part 2 Development Tools and Environment 23
Chapter 4 Development Tools 25
4.1 Editors 26
4.1.1 Emacs 27
4.1.2 vi 28
4.2 Make 29
4.2.1 Complex Command Lines 32
4.2.2 Variables 33
4.2.3 Suffix Rules 34
4.3 The GNU Debugger 35
4.4 Tracing Program Actions 40
Chapter 5 gcc Options and Extensions 43
5.1 gcc Options 44
5.2 Header Files 47
5.2.1 long long 47
5.2.2 Inline Functions 47
5.2.3 Alternative Extended Keywords 47
5.2.4 Attributes 48
Chapter 6 The GNU C Library 49
6.1 Feature Selection 49
6.2 POSIX Interfaces 52
6.2.1 POSIX Required Types 52
6.2.2 Discovering Run-Time Capabilities 54
6.2.3 Finding and Setting Basic System Information 55
6.3 Compatibility 57
Chapter 7 Memory Debugging Tools 59
7.1 Buggy Code 59
7.2 Memory-Checking Tools Included in glibc 62
7.2.1 Finding Memory Heap Corruption 62
7.2.2 Using mtrace()to Track Allocations 65
7.3 Finding Memory Leaks with mpr 66
7.4 Investigating Memory Errors with Valgrind 69
7.5 Electric Fence 74
7.5.1 Using Electric Fence 75
7.5.2 Memory Alignment 76
7.5.3 Other Features 77
7.5.4 Limitations 78
7.5.5 Resource Consumption 78
Chapter 8 Creating and Using Libraries 79
8.1 Static Libraries 79
8.2 Shared Libraries 80
8.3 Designing Shared Libraries 81
8.3.1 Managing Compatibility 82
8.3.2 Incompatible Libraries 83
8.3.3 Designing Compatible Libraries 83
8.4 Building Shared Libraries 84
8.5 Installing Shared Libraries 85
8.5.1 Example 86
8.6 Using Shared Libraries 88
8.6.1 Using Noninstalled Libraries 88
8.6.2 Preloading Libraries 89
Chapter 9 Linux System Environment 91
9.1 The Process Environment 91
9.2 Understanding System Calls 92
9.2.1 System Call Limitations 93
9.2.2 System Call Return Codes 94
9.2.3 Using System Calls 96
9.2.4 Common Error Return Codes 97
9.3 Finding Header and Library Files 102
Part 3 System Programming 103
Chapter 10 The Process Model 105
10.1 Defining a Process 105
10.1.1 Complicating Things with Threads 106
10.1.2 The Linux Approach 106
10.2 Process Attributes 107
10.2.1 The pid and Parentage 107
10.2.2 Credentials 108
10.2.3 The filesystem uid 113
10.2.4 User and Group ID Summary 113
10.3 Process Information 115
10.3.1 Program Arguments 115
10.3.2 Resource Usage 118
10.3.3 Establishing Usage Limits 120
10.4 Process Primitives 121
10.4.1 Having Children 122
10.4.2 Watching Your Children Die 123
10.4.3 Running New Programs 125
10.4.4 Faster Process Creation with vfork() 127
10.4.5 Killing Yourself 128
10.4.6 Killing Others 129
10.4.7 Dumping Core 130
10.5 Simple Children 131
10.5.1 Running and Waiting with sy stem() 131
10.5.2 Reading or Writing from a Process 132
10.6 Sessions and Process Groups 134
10.6.1 Sessions 135
10.6.2 Controlling Terminal 136
10.6.3 Process Groups 136
10.6.4 Orphaned Process Groups 138
10.7 Introduction to ladsh 139
10.7.1 Running External Programs with ladsh 140
10.8 Creating Clones 153
Chapter 11 Simple File Handling 155
11.1 The File Mode 158
11.1.1 File Access Permissions 159
11.1.2 File Permission Modifiers 161
11.1.3 File Types 162
11.1.4 The Process's umask 163
11.2 Basic File Operations 165
11.2.1 File Descriptors 165
11.2.2 Closing Files 166
11.2.3 Opening Filesin the FileSystem 166
11.2.4 Reading,Writing,and Moving Around 168
11.2.5 Partial Reads and Writes 173
11.2.6 Shortening Files 175
11.2.7 Synchronizing Files 175
11.2.8 Other Operations 176
11.3 Querying and Changing Inode Information 177
11.3.1 Finding Inode Information 177
11.3.2 A Simple Example of stat() 178
11.3.3 Easily Determining Access Rights 181
11.3.4 Changing a File's Access Permissions 182
11.3.5 Changing a File's Owner and Group 182
11.3.6 Changing a File's Timestamps 183
11.3.7 Ext3 Extended Attributes 184
11.4 Manipulating Directory Entries 188
11.4.1 Creating Device and Named Pipe Entries 189
11.4.2 Creating Hard Links 191
11.4.3 Using Symbolic Links 192
11.4.4 Removing Files 194
11.4.5 Renaming Files 194
11.5 Manipulating File Descriptors 195
11.5.1 Changing the Access Mode for an Open File 195
11.5.2 Modifiying the close-on-exec Flag 196
11.5.3 Duplicating File Descriptors 196
11.6 Creating Unnamed Pipes 198
11.7 Adding Redirection to ladsh 198
11.7.1 The Data Structures 199
11.7.2 Changing the Code 200
Chapter 12 Signal Processing 203
12.1 Signal Concepts 204
12.1.1 Life Cycle of a Signal 204
12.1.2 Simple Signals 205
12.1.3 Reliable Signals 207
12.1.4 Signals and System Calls 208
12.2 The Linux(and POSIX)Signal API 209
12.2.1 Sending Signals 209
12.2.2 Using sigset_t 210
12.2.3 Catching Signals 211
12.2.4 Manipulating a Process's Signal Mask 213
12.2.5 Finding the Set of Pending Signals 215
12.2.6 Waiting for Signals 216
12.3 Available Signals 217
12.3.1 Describing Signals 221
12.4 Writing Signal Handlers 222
12.5 Reopening Log Files 224
12.6 Real-Time Signals 227
12.6.1 Signal Queueing and Ordering 228
12.7 Learning About a Signal 231
12.7.1 Getting a Signal's Context 231
12.7.2 Sending Data with a Signal 237
Chapter 13 Advanced File Handling 241
13.1 Input and Output Multiplexing 241
13.1.1 Nonblocking I/O 244
13.1.2 Multiplexing with poll() 245
13.1.3 Multiplexing with select() 249
13.1.4 Comparing poll()and select() 253
13.1.5 Multiplexing with epoll 256
13.1.6 Comparing poll()and epoll 263
13.2 Memory Mapping 266
13.2.1 Page Alignment 267
13.2.2 Establishing Memory Mappings 268
13.2.3 Unmapping Regions 273
13.2.4 Syncing Memory Regions to Disk 274
13.2.5 Locking Memory Regions 275
13.3 File Locking 276
13.3.1 Lock Files 276
13.3.2 Record Locking 279
13.3.3 Mandatory Locks 285
13.3.4 Leasing a File 285
13.4 Alternatives to read()and write() 289
13.4.1 Scatter/Gather Reads and Writes 290
13.4.2 Ignoring the File Pointer 291
Chapter 14 Directory Operations 293
14.1 The Current Working Directory 293
14.1.1 Finding the Current Working Directory 293
14.1.2 The.and..Special Files 295
14.1.3 Changing the Current Directory 295
14.2 Changing the Root Directory 296
14.3 Creating and Removing Directories 297
14.3.1 Creating New Directories 297
14.3.2 Removing Directories 297
14.4 Reading a Directory's Contents 297
14.4.1 Starting Over 299
14.5 File Name Globbing 300
14.5.1 Use a Subprocess 300
14.5.2 Internal Globbing 301
14.6 Adding Directories and Globbing to ladsh 306
14.6.1 Adding cd and pwd 306
14.6.2 Adding File Name Globbing 307
14.7 Walking File System Trees 311
14.7.1 Using ftw() 311
14.7.2 File Tree Walks with nftw() 313
14.7.3 Implementing find 315
14.8 Directory Change Notification 317
Chapter 15 Job Control 325
15.1 Job Control Basics 325
15.1.1 Restarting Processes 325
15.1.2 Stopping Processes 326
15.1.3 Handling Job Control Signals 327
15.2 Job Control in ladsh 328
Chapter 16 Terminals and Pseudo Terminals 335
16.1 tty Operations 336
16.1.1 Terminal Utility Functions 337
16.1.2 Controlling Terminals 338
16.1.3 Terminal Ownership 339
16.1.4 Recording with utempter 340
16.1.5 Recording by Hand 341
16.2 termios Overview 351
16.3 termios Examples 353
16.3.1 Passwords 353
16.3.2 Serial Communications 355
16.4 termios Debugging 370
16.5 termios Reference 371
16.5.1 Functions 372
16.5.2 Window Sizes 376
16.5.3 Flags 377
16.5.4 Input Flags 378
16.5.5 Output Flags 380
16.5.6 Control Flags 381
16.5.7 Control Characters 383
16.5.8 Local Flags 385
16.5.9 Controlling read() 387
16.6 Pseudo ttys 389
16.6.1 Opening Pseudo ttys 389
16.6.2 Opening Pseudo ttys the Easy Ways 391
16.6.3 Opening Pseudo ttys the Hard Ways 392
16.6.4 Pseudo tty Examples 396
Chapter 17 Networking with Sockets 407
17.1 Protocol Support 407
17.1.1 Nice Networking 408
17.1.2 Real Networking 408
17.1.3 Making Reality Play Nice 409
17.1.4 Addresses 410
17.2 Utility Functions 411
17.3 Basic Socket Operations 412
17.3.1 Creating a Socket 412
17.3.2 Establishing Connections 414
17.3.3 Binding an Address to a Socket 414
17.3.4 Waiting for Connections 415
17.3.5 Connecting to a Server 416
17.3.6 Finding Connection Addresses 417
17.4 Unix Domain Sockets 418
17.4.1 Unix Domain Addresses 418
17.4.2 Waiting for a Connection 419
17.4.3 Connecting to a Server 422
17.4.4 Running the Unix Domain Examples 423
17.4.5 Unnamed Unix Domain Sockets 423
17.4.6 Passing File Descriptors 424
17.5 Networking Machines with TCP/IP 429
17.5.1 Byte Ordering 430
17.5.2 IPv4 Addressing 431
17.5.3 IPv6 Addressing 433
17.5.4 Manipulating IP Addresses 435
17.5.5 Turning Names into Addresses 437
17.5.6 Turning Addresses into Names 449
17.5.7 Listening for TCP Connections 455
17.5.8 TCP Client Applications 457
17.6 Using UDP Datagrams 459
17.6.1 Creating a UDP Socket 460
17.6.2 Sending and Receiving Datagrams 461
17.6.3 A Simple tftp Server 463
17.7 Socket Errors 469
17.8 Legacy Networking Functions 471
17.8.1 IPv4 Address Manipulation 471
17.8.2 Hostname Resolution 473
17.8.3 Legacy Host Information Lookup Example 475
17.8.4 Looking Up Port Numbers 476
Chapter 18 Time 481
18.1 Telling Time and Dates 481
18.1.1 Representing Time 481
18.1.2 Converting,Formatting,and Parsing Times 484
18.1.3 The Limits of Time 489
18.2 Using Timers 490
18.2.1 Sleeping 491
18.2.2 Interval Timers 492
Chapter 19 Random Numbers 497
19.1 Pseudo-Random Numbers 497
19.2 Cryptography and Random Numbers 498
Chapter 20 Programming Virtual Consoles 501
20.1 Getting Started 502
20.2 Beeping 505
20.3 Determining Whetherthe Terminal Is a VC 506
20.4 Finding the Current VC 506
20.5 Managing VC Switching 507
20.6 Example:The open Command 510
Chapter 21 The Linux Console 513
21.1 Capability Databases 514
21.2 Glyphs,Characters,and Maps 516
21.3 Linux Console Capabilities 516
21.3.1 Control Characters 518
21.3.2 Escape Sequences 519
21.3.3 Testing Sequences 521
21.3.4 Complex Escape Sequences 521
21.4 Direct Screen Writing 526
Chapter 22 Writing Secure Programs 531
22.1 When Security Matters 531
22.1.1 When Security Fails 533
22.2 Minimizing the Opportunity for Attack 534
22.2.1 Giving Up Permissions 534
22.2.2 Getting a Helping Hand 535
22.2.3 Restricting File System Access 536
22.3 Common Security Holes 537
22.3.1 Buffer Overflows 537
22.3.2 Parsing Filenames 543
22.3.3 Environment Variables 544
22.3.4 Running the Shell 545
22.3.5 Creating Temporary Files 545
22.3.6 Race Conditions and Signal Handlers 548
22.3.7 Closing File Descriptors 549
22.4 Running as a Daemon 550
Part 4 Development Libraries 553
Chapter 23 String Matching 555
23.1 Globbing Arbitrary Strings 555
23.2 Regular Expressions 556
23.2.1 Linux Regular Expressions 557
23.2.2 Regular Expression Matching 557
23.2.3 A Simple grep 562
Chapter 24 Terminal Handling with S-Lang 569
24.1 Input Handling 570
24.1.1 Initializing S-Lang Input Handling 571
24.1.2 Restoring the Terminal State 572
24.1.3 Reading Characters from the Terminal 572
24.1.4 Checking for Pending Input 573
24.2 Output Handling 573
24.2.1 Initializing Screen Management 574
24.2.2 Updating the Display 574
24.2.3 Moving the Cursor 575
24.2.4 Finishing Screen Management 575
24.2.5 Skeleton Screen Management 575
24.2.6 Switching Character Sets 576
24.2.7 Writing to the Screen 577
24.2.8 Drawing Lines and Boxes 579
24.2.9 Using Color 581
Chapter 25 A Hashed Database Library 583
25.1 Overview 584
25.2 Basic Operations 585
25.2.1 Opening a qdbm File 585
25.2.2 Closing a Database 586
25.2.3 Obtaining the File Descriptor 587
25.2.4 Syncing the Database 587
25.3 Reading Records 587
25.3.1 Reading a Particular Record 587
25.3.2 Reading Records Sequentially 589
25.4 Modifying the Database 589
25.4.1 Adding Records 590
25.4.2 Removing Records 590
25.5 Example 591
Chapter 26 Parsing Command-Line Options 597
26.1 The Option Table 598
26.1.1 Defining the Options 598
26.1.2 Nesting Option Tables 601
26.2 Using the Option Table 602
26.2.1 Creating a Context 602
26.2.2 Parsing the Command Line 604
26.2.3 Leftover Arguments 605
26.2.4 Automatic Help Messages 606
26.3 Using Callbacks 608
26.4 Error Handling 612
26.5 Option Aliasing 614
26.5.1 Specifying Aliases 615
26.5.2 Enabling Aliases 615
26.6 Parsing Argument Strings 616
26.7 Handling Extra Arguments 617
26.8 Sample Application 617
Chapter 27 Dynamic Loading at Run Time 619
27.1 The dl Interface 620
27.1.1 Example 624
Chapter 28 User Identification and Authentication 627
28.1 ID-to-Name Translation 627
28.1.1 Example:The id Command 630
28.2 Pluggable Authentication Modules 635
28.2.1 PAM Conversations 636
28.2.2 PAM Actions 638
Appendices 647
Appendix A Header Files 649
Appendix B ladsh Source Code 651
Glossary 671
Bibliography 679
Index 683