Statistics
2
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-03-04

AuthorMichael Kerrisk

The Linux Programming Interface is the definitive guide to the Linux and UNIX programming interface—the interface employed by nearly every application that runs on a Linux or UNIX system. In this authoritative work, Linux programming expert Michael Kerrisk provides detailed descriptions of the system calls and library functions that you need in order to master the craft of system programming, and accompanies his explanations with clear, complete example programs. You'll find descriptions of over 500 system calls and library functions, and more than 200 example programs, 88 tables, and 115 diagrams. You'll learn how to: • Read and write files efficiently • Use signals, clocks, and timers • Create processes and execute programs • Write secure programs • Write multithreaded programs using POSIX threads • Build and use shared libraries • Perform interprocess communication using pipes, message queues, shared memory, and semaphores • Write network applications with the sockets API While The Linux Programming Interface covers a wealth of Linux-specific features, including epoll, inotify, and the /proc file system, its emphasis on UNIX standards (POSIX.1-2001/SUSv3 and POSIX.1-2008/SUSv4) makes it equally valuable to programmers working on other UNIX platforms. The Linux Programming Interface is the most comprehensive single-volume work on the Linux and UNIX programming interface, and a book that's destined to become a new classic.

Tags
No tags
ISBN: 159327291X
Publisher: No Starch Press
Publish Year: 2010
Language: 英文
Pages: 1556
File Format: PDF
File Size: 6.9 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

The Linux Programming inTerface A Linux and UNIX® System Programming Handbook Michael KerrisK KerrisK Th e Lin u x Pr o g r a m m in g in Ter fa c e The Linux Programming Interface is the definitive guide to the Linux and UNIX programming interface—the interface employed by nearly every application that runs on a Linux or UNIX system. In this authoritative work, Linux programming expert Michael Kerrisk provides detailed descriptions of the system calls and library functions that you need in order to master the craft of system programming, and accompanies his explanations with clear, complete example programs. You’ll find descriptions of over 500 system calls and library functions, and more than 200 example pro- grams, 88 tables, and 115 diagrams. You’ll learn how to: f Read and write files efficiently f Use signals, clocks, and timers f Create processes and execute programs f Write secure programs f Write multithreaded programs using POSIX threads f Build and use shared libraries f Perform interprocess communication using pipes, message queues, shared memory, and semaphores f Write network applications with the sockets API While The Linux Programming Interface covers a wealth of Linux-specific features, including epoll, inotify, and the /proc file system, its emphasis on UNIX standards (POSIX.1-2001/SUSv3 and POSIX.1-2008/SUSv4) makes it equally valuable to programmers working on other UNIX platforms. The Linux Programming Interface is the most com- prehensive single-volume work on the Linux and UNIX programming interface, and a book that’s destined to become a new classic. about the author Michael Kerrisk (http://man7.org/) has been using and programming UNIX systems for more than 20 years, and has taught many week-long courses on UNIX system programming. Since 2004, he has maintained the man-pages project, which produces the manual pages describing the Linux kernel and glibc programming APIs. He has written or cowritten more than 250 of the manual pages and is actively involved in the testing and design review of new Linux kernel-userspace interfaces. Michael lives with his family in Munich, Germany. The definiTive guide To Linux and unix® sysTem Programming covers current uNiX standards (PosiX.1-2001/susv3 and PosiX.1-2008/susv4) 5 9 9 9 5 9 7 81 5 93 2 72 20 3 ISBN: 978-1-59327-220-3 6 89 1 45 7 22 00 0 $99.95 ($114.95 CDN) Shelve In: linux/programmingTH E F I N EST I N G E E K E NTE RTA I N M E NT™ www.nostarch.com This logo applies only to the text stock.
(This page has no text content)
PRAISE FOR THE LINUX PROGRAMMING INTERFACE “If I had to choose a single book to sit next to my machine when writing software for Linux, this would be it.” —MARTIN LANDERS, SOFTWARE ENGINEER, GOOGLE “This book, with its detailed descriptions and examples, contains everything you need to understand the details and nuances of the low-level programming APIs in Linux . . . no matter what the level of reader, there will be something to be learnt from this book.” —MEL GORMAN, AUTHOR OF Understanding the Linux Virtual Memory Manager “Michael Kerrisk has not only written a great book about Linux programming and how it relates to various standards, but has also taken care that bugs he noticed got fixed and the man pages were (greatly) improved. In all three ways, he has made Linux programming easier. The in-depth treatment of topics in The Linux Programming Interface . . . makes it a must-have reference for both new and experienced Linux programmers.” —ANDREAS JAEGER, PROGRAM MANAGER, OPENSUSE, NOVELL “Michael’s inexhaustible determination to get his information right, and to express it clearly and concisely, has resulted in a strong reference source for programmers. While this work is targeted at Linux programmers, it will be of value to any programmer working in the UNIX/POSIX ecosystem.” —DAVID BUTENHOF, AUTHOR OF Programming with POSIX Threads AND CONTRIBUTOR TO THE POSIX AND UNIX STANDARDS “ . . . a very thorough—yet easy to read—explanation of UNIX system and network programming, with an emphasis on Linux systems. It’s certainly a book I’d recommend to anybody wanting to get into UNIX programming (in general) or to experienced UNIX programmers wanting to know ‘what’s new’ in the popular GNU/Linux system.” —FERNANDO GONT, NETWORK SECURITY RESEARCHER, IETF PARTICIPANT, AND RFC AUTHOR
“ . . . encyclopedic in the breadth and depth of its coverage, and textbook- like in its wealth of worked examples and exercises. Each topic is clearly and comprehensively covered, from theory to hands-on working code. Professionals, students, educators, this is the Linux/UNIX reference that you have been waiting for.” —ANTHONY ROBINS, ASSOCIATE PROFESSOR OF COMPUTER SCIENCE, THE UNIVERSITY OF OTAGO “I’ve been very impressed by the precision, the quality and the level of detail Michael Kerrisk put in his book. He is a great expert of Linux system calls and lets us share his knowledge and understanding of the Linux APIs.” —CHRISTOPHE BLAESS, AUTHOR OF Programmation système en C sous Linux “ . . . an essential resource for the serious or professional Linux and UNIX systems programmer. Michael Kerrisk covers the use of all the key APIs across both the Linux and UNIX system interfaces with clear descriptions and tutorial examples and stresses the importance and benefits of following standards such as the Single UNIX Specification and POSIX 1003.1.” —ANDREW JOSEY, DIRECTOR, STANDARDS, THE OPEN GROUP, AND CHAIR OF THE POSIX 1003.1 WORKING GROUP “What could be better than an encyclopedic reference to the Linux system, from the standpoint of the system programmer, written by none other than the maintainer of the man pages himself? The Linux Programming Interface is comprehensive and detailed. I firmly expect it to become an indispensable addition to my programming bookshelf.” —BILL GALLMEISTER, AUTHOR OF POSIX.4 Programmer’s Guide: Programming for the Real World “ . . . the most complete and up-to-date book about Linux and UNIX system programming. If you’re new to Linux system programming, if you’re a UNIX veteran focused on portability while interested in learning the Linux way, or if you’re simply looking for an excellent reference about the Linux pro- gramming interface, then Michael Kerrisk’s book is definitely the companion you want on your bookshelf.” —LOÏC DOMAIGNÉ, CHIEF SOFTWARE ARCHITECT (EMBEDDED), CORPULS.COM
San Francisco
THE LINUX PROGRAMMING INTERFACE. Copyright © 2010 by Michael Kerrisk. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. 14 13 12 11 10 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-220-0 ISBN-13: 978-1-59327-220-3 Publisher: William Pollock Production Editor: Riley Hoffman Cover Design: Octopod Studios Front Cover Photo: Rob Suisted Back Cover Photo: Lynley Cook Copyeditor: Marilyn Smith Compositor: Susan Glinert Stevens Proofreader: Linda Seifert For technical reviewers, please refer to the author’s acknowledgements For information on book distributors or translations, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 38 Ringold Street, San Francisco, CA 94103 phone: 415.863.9900; fax: 415.863.9950; info@nostarch.com; www.nostarch.com Library of Congress Cataloging-in-Publication Data Kerrisk, Michael, 1961- The Linux programming interface : a Linux and UNIX system programming handbook / by Michael Kerrisk. p. cm. Includes bibliographical references and index. ISBN-13: 978-1-59327-220-3 (hardcover) ISBN-10: 1-59327-220-0 (hardcover) 1. Linux. 2. UNIX (Computer file) 3. Operating systems (Computers) I. Title. QA76.76.O63K496 2010 005.4'32--dc22 2010001947 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. UNIX® is a registered trademark of The Open Group. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. This logo applies only to the text stock.
For Cecilia, who lights up my world.
(This page has no text content)
B R I E F C O N T E N T S Preface ..................................................................................................................... xxxi Chapter 1: History and Standards.................................................................................... 1 Chapter 2: Fundamental Concepts ................................................................................. 21 Chapter 3: System Programming Concepts...................................................................... 43 Chapter 4: File I/O: The Universal I/O Model................................................................. 69 Chapter 5: File I/O: Further Details ................................................................................ 89 Chapter 6: Processes .................................................................................................. 113 Chapter 7: Memory Allocation..................................................................................... 139 Chapter 8: Users and Groups...................................................................................... 153 Chapter 9: Process Credentials .................................................................................... 167 Chapter 10: Time....................................................................................................... 185 Chapter 11: System Limits and Options......................................................................... 211 Chapter 12: System and Process Information ................................................................. 223 Chapter 13: File I/O Buffering..................................................................................... 233 Chapter 14: File Systems ............................................................................................ 251 Chapter 15: File Attributes .......................................................................................... 279 Chapter 16: Extended Attributes .................................................................................. 311 Chapter 17: Access Control Lists .................................................................................. 319 Chapter 18: Directories and Links ................................................................................ 339 Chapter 19: Monitoring File Events .............................................................................. 375
Chapter 20: Signals: Fundamental Concepts ................................................................. 387 Chapter 21: Signals: Signal Handlers........................................................................... 421 Chapter 22: Signals: Advanced Features ...................................................................... 447 Chapter 23: Timers and Sleeping................................................................................. 479 Chapter 24: Process Creation...................................................................................... 513 Chapter 25: Process Termination.................................................................................. 531 Chapter 26: Monitoring Child Processes ....................................................................... 541 Chapter 27: Program Execution ................................................................................... 563 Chapter 28: Process Creation and Program Execution in More Detail ............................... 591 Chapter 29: Threads: Introduction................................................................................ 617 Chapter 30: Threads: Thread Synchronization............................................................... 631 Chapter 31: Threads: Thread Safety and Per-Thread Storage........................................... 655 Chapter 32: Threads: Thread Cancellation.................................................................... 671 Chapter 33: Threads: Further Details ............................................................................ 681 Chapter 34: Process Groups, Sessions, and Job Control ................................................. 699 Chapter 35: Process Priorities and Scheduling ............................................................... 733 Chapter 36: Process Resources .................................................................................... 753 Chapter 37: Daemons ................................................................................................ 767 Chapter 38: Writing Secure Privileged Programs ........................................................... 783 Chapter 39: Capabilities ............................................................................................ 797 Chapter 40: Login Accounting ..................................................................................... 817 Chapter 41: Fundamentals of Shared Libraries .............................................................. 833 Chapter 42: Advanced Features of Shared Libraries....................................................... 859 Chapter 43: Interprocess Communication Overview ....................................................... 877 Chapter 44: Pipes and FIFOs....................................................................................... 889 Chapter 45: Introduction to System V IPC...................................................................... 921 Chapter 46: System V Message Queues ....................................................................... 937viii Br ie f Contents
Chapter 47: System V Semaphores .............................................................................. 965 Chapter 48: System V Shared Memory ......................................................................... 997 Chapter 49: Memory Mappings ................................................................................ 1017 Chapter 50: Virtual Memory Operations ..................................................................... 1045 Chapter 51: Introduction to POSIX IPC........................................................................ 1057 Chapter 52: POSIX Message Queues ......................................................................... 1063 Chapter 53: POSIX Semaphores ................................................................................ 1089 Chapter 54: POSIX Shared Memory........................................................................... 1107 Chapter 55: File Locking........................................................................................... 1117 Chapter 56: Sockets: Introduction .............................................................................. 1149 Chapter 57: Sockets: UNIX Domain ........................................................................... 1165 Chapter 58: Sockets: Fundamentals of TCP/IP Networks............................................... 1179 Chapter 59: Sockets: Internet Domains ....................................................................... 1197 Chapter 60: Sockets: Server Design ........................................................................... 1239 Chapter 61: Sockets: Advanced Topics....................................................................... 1253 Chapter 62: Terminals .............................................................................................. 1289 Chapter 63: Alternative I/O Models........................................................................... 1325 Chapter 64: Pseudoterminals..................................................................................... 1375 Appendix A: Tracing System Calls ............................................................................. 1401 Appendix B: Parsing Command-Line Options............................................................... 1405 Appendix C: Casting the NULL Pointer ......................................................................... 1413 Appendix D: Kernel Configuration ............................................................................. 1417 Appendix E: Further Sources of Information ................................................................. 1419 Appendix F: Solutions to Selected Exercises................................................................. 1425 Bibliography............................................................................................................ 1437 Index .......................................................................................................................1447Br ie f Contents ix
(This page has no text content)
C O N T E N T S I N D E T A I L PREFACE xxxi 1 HISTORY AND STANDARDS 1 1.1 A Brief History of UNIX and C ........................................................................................2 1.2 A Brief History of Linux ...................................................................................................5 1.2.1 The GNU Project ......................................................................................5 1.2.2 The Linux Kernel .......................................................................................6 1.3 Standardization .......................................................................................................... 10 1.3.1 The C Programming Language ................................................................. 10 1.3.2 The First POSIX Standards........................................................................ 11 1.3.3 X/Open Company and The Open Group .................................................. 13 1.3.4 SUSv3 and POSIX.1-2001....................................................................... 13 1.3.5 SUSv4 and POSIX.1-2008....................................................................... 15 1.3.6 UNIX Standards Timeline ......................................................................... 16 1.3.7 Implementation Standards........................................................................ 17 1.3.8 Linux, Standards, and the Linux Standard Base........................................... 18 1.4 Summary.................................................................................................................... 19 2 FUNDAMENTAL CONCEPTS 21 2.1 The Core Operating System: The Kernel ......................................................................... 21 2.2 The Shell .................................................................................................................... 24 2.3 Users and Groups ....................................................................................................... 26 2.4 Single Directory Hierarchy, Directories, Links, and Files ................................................... 27 2.5 File I/O Model ........................................................................................................... 29 2.6 Programs.................................................................................................................... 30 2.7 Processes ................................................................................................................... 31 2.8 Memory Mappings...................................................................................................... 35 2.9 Static and Shared Libraries........................................................................................... 35 2.10 Interprocess Communication and Synchronization ........................................................... 36 2.11 Signals....................................................................................................................... 37 2.12 Threads...................................................................................................................... 38 2.13 Process Groups and Shell Job Control............................................................................ 38 2.14 Sessions, Controlling Terminals, and Controlling Processes ............................................... 39 2.15 Pseudoterminals .......................................................................................................... 39 2.16 Date and Time ............................................................................................................ 40 2.17 Client-Server Architecture ............................................................................................. 40 2.18 Realtime..................................................................................................................... 41 2.19 The /proc File System................................................................................................... 42 2.20 Summary.................................................................................................................... 42 3 SYSTEM PROGRAMMING CONCEPTS 43 3.1 System Calls ............................................................................................................... 43 3.2 Library Functions ......................................................................................................... 46 3.3 The Standard C Library; The GNU C Library (glibc) ......................................................... 47 3.4 Handling Errors from System Calls and Library Functions.................................................. 48 3.5 Notes on the Example Programs in This Book.................................................................. 50 3.5.1 Command-Line Options and Arguments ..................................................... 50 3.5.2 Common Functions and Header Files......................................................... 51
3.6 Portability Issues.......................................................................................................... 61 3.6.1 Feature Test Macros ................................................................................ 61 3.6.2 System Data Types.................................................................................. 63 3.6.3 Miscellaneous Portability Issues................................................................. 66 3.7 Summary.................................................................................................................... 68 3.8 Exercise ..................................................................................................................... 68 4 FILE I/O: THE UNIVERSAL I/O MODEL 69 4.1 Overview ................................................................................................................... 69 4.2 Universality of I/O ...................................................................................................... 72 4.3 Opening a File: open()................................................................................................. 72 4.3.1 The open() flags Argument........................................................................ 74 4.3.2 Errors from open() ................................................................................... 77 4.3.3 The creat() System Call ............................................................................ 78 4.4 Reading from a File: read()........................................................................................... 79 4.5 Writing to a File: write()............................................................................................... 80 4.6 Closing a File: close() ................................................................................................... 80 4.7 Changing the File Offset: lseek().................................................................................... 81 4.8 Operations Outside the Universal I/O Model: ioctl() ....................................................... 86 4.9 Summary.................................................................................................................... 86 4.10 Exercises .................................................................................................................... 87 5 FILE I/O: FURTHER DETAILS 89 5.1 Atomicity and Race Conditions ..................................................................................... 90 5.2 File Control Operations: fcntl() ..................................................................................... 92 5.3 Open File Status Flags ................................................................................................. 93 5.4 Relationship Between File Descriptors and Open Files ...................................................... 94 5.5 Duplicating File Descriptors .......................................................................................... 96 5.6 File I/O at a Specified Offset: pread() and pwrite() ......................................................... 98 5.7 Scatter-Gather I/O: readv() and writev() ........................................................................ 99 5.8 Truncating a File: truncate() and ftruncate() ................................................................. 103 5.9 Nonblocking I/O ...................................................................................................... 103 5.10 I/O on Large Files ..................................................................................................... 104 5.11 The /dev/fd Directory ................................................................................................ 107 5.12 Creating Temporary Files ........................................................................................... 108 5.13 Summary.................................................................................................................. 109 5.14 Exercises .................................................................................................................. 110 6 PROCESSES 113 6.1 Processes and Programs............................................................................................. 113 6.2 Process ID and Parent Process ID................................................................................. 114 6.3 Memory Layout of a Process ....................................................................................... 115 6.4 Virtual Memory Management ..................................................................................... 118 6.5 The Stack and Stack Frames ....................................................................................... 121 6.6 Command-Line Arguments (argc, argv) ......................................................................... 122 6.7 Environment List ........................................................................................................ 125 6.8 Performing a Nonlocal Goto: setjmp() and longjmp() .................................................... 131 6.9 Summary.................................................................................................................. 138 6.10 Exercises .................................................................................................................. 138xii Contents in Detai l
7 MEMORY ALLOCATION 139 7.1 Allocating Memory on the Heap ................................................................................. 139 7.1.1 Adjusting the Program Break: brk() and sbrk() .......................................... 139 7.1.2 Allocating Memory on the Heap: malloc() and free() ................................. 140 7.1.3 Implementation of malloc() and free() ...................................................... 144 7.1.4 Other Methods of Allocating Memory on the Heap ................................... 147 7.2 Allocating Memory on the Stack: alloca() ..................................................................... 150 7.3 Summary.................................................................................................................. 151 7.4 Exercises .................................................................................................................. 152 8 USERS AND GROUPS 153 8.1 The Password File: /etc/passwd ................................................................................... 153 8.2 The Shadow Password File: /etc/shadow ...................................................................... 155 8.3 The Group File: /etc/group......................................................................................... 155 8.4 Retrieving User and Group Information ........................................................................ 157 8.5 Password Encryption and User Authentication............................................................... 162 8.6 Summary.................................................................................................................. 166 8.7 Exercises .................................................................................................................. 166 9 PROCESS CREDENTIALS 167 9.1 Real User ID and Real Group ID.................................................................................. 167 9.2 Effective User ID and Effective Group ID....................................................................... 168 9.3 Set-User-ID and Set-Group-ID Programs ........................................................................ 168 9.4 Saved Set-User-ID and Saved Set-Group-ID ................................................................... 170 9.5 File-System User ID and File-System Group ID................................................................ 171 9.6 Supplementary Group IDs .......................................................................................... 172 9.7 Retrieving and Modifying Process Credentials............................................................... 172 9.7.1 Retrieving and Modifying Real, Effective, and Saved Set IDs ...................... 172 9.7.2 Retrieving and Modifying File-System IDs ................................................. 178 9.7.3 Retrieving and Modifying Supplementary Group IDs ................................. 178 9.7.4 Summary of Calls for Modifying Process Credentials ................................. 180 9.7.5 Example: Displaying Process Credentials ................................................. 182 9.8 Summary.................................................................................................................. 183 9.9 Exercises .................................................................................................................. 184 10 TIME 185 10.1 Calendar Time .......................................................................................................... 186 10.2 Time-Conversion Functions.......................................................................................... 187 10.2.1 Converting time_t to Printable Form ........................................................ 188 10.2.2 Converting Between time_t and Broken-Down Time................................... 189 10.2.3 Converting Between Broken-Down Time and Printable Form ....................... 191 10.3 Timezones ................................................................................................................ 197 10.4 Locales..................................................................................................................... 200 10.5 Updating the System Clock......................................................................................... 204 10.6 The Software Clock (Jiffies) ......................................................................................... 205 10.7 Process Time............................................................................................................. 206 10.8 Summary.................................................................................................................. 209 10.9 Exercise ................................................................................................................... 210Contents in Detai l xiii
11 SYSTEM LIMITS AND OPTIONS 211 11.1 System Limits............................................................................................................. 212 11.2 Retrieving System Limits (and Options) at Run Time ........................................................ 215 11.3 Retrieving File-Related Limits (and Options) at Run Time.................................................. 217 11.4 Indeterminate Limits ................................................................................................... 219 11.5 System Options ......................................................................................................... 219 11.6 Summary.................................................................................................................. 221 11.7 Exercises .................................................................................................................. 222 12 SYSTEM AND PROCESS INFORMATION 223 12.1 The /proc File System................................................................................................. 223 12.1.1 Obtaining Information About a Process: /proc/PID................................... 224 12.1.2 System Information Under /proc.............................................................. 226 12.1.3 Accessing /proc Files ............................................................................ 226 12.2 System Identification: uname() .................................................................................... 229 12.3 Summary.................................................................................................................. 231 12.4 Exercises .................................................................................................................. 231 13 FILE I/O BUFFERING 233 13.1 Kernel Buffering of File I/O: The Buffer Cache .............................................................. 233 13.2 Buffering in the stdio Library ....................................................................................... 237 13.3 Controlling Kernel Buffering of File I/O........................................................................ 239 13.4 Summary of I/O Buffering .......................................................................................... 243 13.5 Advising the Kernel About I/O Patterns........................................................................ 244 13.6 Bypassing the Buffer Cache: Direct I/O........................................................................ 246 13.7 Mixing Library Functions and System Calls for File I/O .................................................. 248 13.8 Summary.................................................................................................................. 249 13.9 Exercises .................................................................................................................. 250 14 FILE SYSTEMS 251 14.1 Device Special Files (Devices) ..................................................................................... 252 14.2 Disks and Partitions ................................................................................................... 253 14.3 File Systems .............................................................................................................. 254 14.4 I-nodes ..................................................................................................................... 256 14.5 The Virtual File System (VFS) ....................................................................................... 259 14.6 Journaling File Systems............................................................................................... 260 14.7 Single Directory Hierarchy and Mount Points ................................................................ 261 14.8 Mounting and Unmounting File Systems ....................................................................... 262 14.8.1 Mounting a File System: mount() ............................................................ 264 14.8.2 Unmounting a File System: umount() and umount2() ................................ 269 14.9 Advanced Mount Features .......................................................................................... 271 14.9.1 Mounting a File System at Multiple Mount Points....................................... 271 14.9.2 Stacking Multiple Mounts on the Same Mount Point................................... 271 14.9.3 Mount Flags That Are Per-Mount Options ................................................. 272 14.9.4 Bind Mounts......................................................................................... 272 14.9.5 Recursive Bind Mounts........................................................................... 273 14.10 A Virtual Memory File System: tmpfs ............................................................................ 274 14.11 Obtaining Information About a File System: statvfs() ...................................................... 276 14.12 Summary.................................................................................................................. 277 14.13 Exercise ................................................................................................................... 278xiv Contents in Detai l
15 FILE ATTRIBUTES 279 15.1 Retrieving File Information: stat() ................................................................................. 279 15.2 File Timestamps......................................................................................................... 285 15.2.1 Changing File Timestamps with utime() and utimes()................................. 287 15.2.2 Changing File Timestamps with utimensat() and futimens() ........................ 289 15.3 File Ownership ......................................................................................................... 291 15.3.1 Ownership of New Files ........................................................................ 291 15.3.2 Changing File Ownership: chown(), fchown(), and lchown()....................... 291 15.4 File Permissions......................................................................................................... 294 15.4.1 Permissions on Regular Files ................................................................... 294 15.4.2 Permissions on Directories...................................................................... 297 15.4.3 Permission-Checking Algorithm ............................................................... 297 15.4.4 Checking File Accessibility: access() ......................................................... 299 15.4.5 Set-User-ID, Set-Group-ID, and Sticky Bits ................................................. 300 15.4.6 The Process File Mode Creation Mask: umask()........................................ 301 15.4.7 Changing File Permissions: chmod() and fchmod()..................................... 303 15.5 I-node Flags (ext2 Extended File Attributes) ................................................................... 304 15.6 Summary.................................................................................................................. 308 15.7 Exercises .................................................................................................................. 309 16 EXTENDED ATTRIBUTES 311 16.1 Overview ................................................................................................................. 311 16.2 Extended Attribute Implementation Details .................................................................... 313 16.3 System Calls for Manipulating Extended Attributes......................................................... 314 16.4 Summary.................................................................................................................. 318 16.5 Exercise ................................................................................................................... 318 17 ACCESS CONTROL LISTS 319 17.1 Overview ................................................................................................................. 320 17.2 ACL Permission-Checking Algorithm............................................................................. 321 17.3 Long and Short Text Forms for ACLs............................................................................. 323 17.4 The ACL_MASK Entry and the ACL Group Class................................................................ 324 17.5 The getfacl and setfacl Commands ............................................................................... 325 17.6 Default ACLs and File Creation ................................................................................... 327 17.7 ACL Implementation Limits .......................................................................................... 328 17.8 The ACL API ............................................................................................................. 329 17.9 Summary.................................................................................................................. 337 17.10 Exercise ................................................................................................................... 337 18 DIRECTORIES AND LINKS 339 18.1 Directories and (Hard) Links........................................................................................ 339 18.2 Symbolic (Soft) Links .................................................................................................. 342 18.3 Creating and Removing (Hard) Links: link() and unlink()............................................... 344 18.4 Changing the Name of a File: rename() ....................................................................... 348 18.5 Working with Symbolic Links: symlink() and readlink().................................................. 349 18.6 Creating and Removing Directories: mkdir() and rmdir() ............................................... 350 18.7 Removing a File or Directory: remove() ......................................................................... 352 18.8 Reading Directories: opendir() and readdir() ................................................................ 352 18.9 File Tree Walking: nftw() ........................................................................................... 358 18.10 The Current Working Directory of a Process ................................................................. 363 18.11 Operating Relative to a Directory File Descriptor ........................................................... 365 18.12 Changing the Root Directory of a Process: chroot() ........................................................ 367 18.13 Resolving a Pathname: realpath() ................................................................................ 369Contents in Detai l xv
18.14 Parsing Pathname Strings: dirname() and basename() .................................................... 370 18.15 Summary.................................................................................................................. 372 18.16 Exercises .................................................................................................................. 373 19 MONITORING FILE EVENTS 375 19.1 Overview ................................................................................................................. 376 19.2 The inotify API .......................................................................................................... 376 19.3 inotify Events ............................................................................................................ 378 19.4 Reading inotify Events................................................................................................ 379 19.5 Queue Limits and /proc Files....................................................................................... 385 19.6 An Older System for Monitoring File Events: dnotify....................................................... 386 19.7 Summary.................................................................................................................. 386 19.8 Exercise ................................................................................................................... 386 20 SIGNALS: FUNDAMENTAL CONCEPTS 387 20.1 Concepts and Overview............................................................................................. 388 20.2 Signal Types and Default Actions ................................................................................ 390 20.3 Changing Signal Dispositions: signal()......................................................................... 397 20.4 Introduction to Signal Handlers ................................................................................... 398 20.5 Sending Signals: kill() ............................................................................................... 401 20.6 Checking for the Existence of a Process........................................................................ 403 20.7 Other Ways of Sending Signals: raise() and killpg() ..................................................... 404 20.8 Displaying Signal Descriptions .................................................................................... 406 20.9 Signal Sets ............................................................................................................... 406 20.10 The Signal Mask (Blocking Signal Delivery) .................................................................. 410 20.11 Pending Signals ........................................................................................................ 411 20.12 Signals Are Not Queued............................................................................................ 412 20.13 Changing Signal Dispositions: sigaction()..................................................................... 416 20.14 Waiting for a Signal: pause()...................................................................................... 418 20.15 Summary.................................................................................................................. 418 20.16 Exercises .................................................................................................................. 419 21 SIGNALS: SIGNAL HANDLERS 421 21.1 Designing Signal Handlers ......................................................................................... 422 21.1.1 Signals Are Not Queued (Revisited) ........................................................ 422 21.1.2 Reentrant and Async-Signal-Safe Functions............................................... 422 21.1.3 Global Variables and the sig_atomic_t Data Type..................................... 428 21.2 Other Methods of Terminating a Signal Handler ........................................................... 428 21.2.1 Performing a Nonlocal Goto from a Signal Handler .................................. 429 21.2.2 Terminating a Process Abnormally: abort() ............................................... 433 21.3 Handling a Signal on an Alternate Stack: sigaltstack() ................................................... 434 21.4 The SA_SIGINFO Flag................................................................................................... 437 21.5 Interruption and Restarting of System Calls ................................................................... 442 21.6 Summary.................................................................................................................. 445 21.7 Exercise ................................................................................................................... 446 22 SIGNALS: ADVANCED FEATURES 447 22.1 Core Dump Files ....................................................................................................... 448 22.2 Special Cases for Delivery, Disposition, and Handling ................................................... 450 22.3 Interruptible and Uninterruptible Process Sleep States..................................................... 451 22.4 Hardware-Generated Signals...................................................................................... 452 22.5 Synchronous and Asynchronous Signal Generation ....................................................... 452xvi Contents in Detai l
22.6 Timing and Order of Signal Delivery ........................................................................... 453 22.7 Implementation and Portability of signal()..................................................................... 454 22.8 Realtime Signals........................................................................................................ 456 22.8.1 Sending Realtime Signals....................................................................... 458 22.8.2 Handling Realtime Signals ..................................................................... 460 22.9 Waiting for a Signal Using a Mask: sigsuspend() .......................................................... 464 22.10 Synchronously Waiting for a Signal............................................................................. 468 22.11 Fetching Signals via a File Descriptor........................................................................... 471 22.12 Interprocess Communication with Signals ..................................................................... 474 22.13 Earlier Signal APIs (System V and BSD) ........................................................................ 475 22.14 Summary.................................................................................................................. 477 22.15 Exercises .................................................................................................................. 478 23 TIMERS AND SLEEPING 479 23.1 Interval Timers........................................................................................................... 479 23.2 Scheduling and Accuracy of Timers ............................................................................. 485 23.3 Setting Timeouts on Blocking Operations...................................................................... 486 23.4 Suspending Execution for a Fixed Interval (Sleeping) ..................................................... 487 23.4.1 Low-Resolution Sleeping: sleep() .............................................................. 487 23.4.2 High-Resolution Sleeping: nanosleep()...................................................... 488 23.5 POSIX Clocks............................................................................................................ 491 23.5.1 Retrieving the Value of a Clock: clock_gettime() ........................................ 491 23.5.2 Setting the Value of a Clock: clock_settime() ............................................. 492 23.5.3 Obtaining the Clock ID of a Specific Process or Thread ............................. 493 23.5.4 Improved High-Resolution Sleeping: clock_nanosleep() ............................... 493 23.6 POSIX Interval Timers................................................................................................. 495 23.6.1 Creating a Timer: timer_create() ............................................................. 495 23.6.2 Arming and Disarming a Timer: timer_settime() ........................................ 498 23.6.3 Retrieving the Current Value of a Timer: timer_gettime() ............................. 499 23.6.4 Deleting a Timer: timer_delete() .............................................................. 499 23.6.5 Notification via a Signal........................................................................ 499 23.6.6 Timer Overruns..................................................................................... 503 23.6.7 Notification via a Thread ....................................................................... 504 23.7 Timers That Notify via File Descriptors: the timerfd API ................................................... 507 23.8 Summary.................................................................................................................. 511 23.9 Exercises .................................................................................................................. 512 24 PROCESS CREATION 513 24.1 Overview of fork(), exit(), wait(), and execve() .............................................................. 513 24.2 Creating a New Process: fork()................................................................................... 515 24.2.1 File Sharing Between Parent and Child .................................................... 517 24.2.2 Memory Semantics of fork() ................................................................... 520 24.3 The vfork() System Call .............................................................................................. 522 24.4 Race Conditions After fork() ....................................................................................... 525 24.5 Avoiding Race Conditions by Synchronizing with Signals............................................... 527 24.6 Summary.................................................................................................................. 529 24.7 Exercises .................................................................................................................. 530 25 PROCESS TERMINATION 531 25.1 Terminating a Process: _exit() and exit()....................................................................... 531 25.2 Details of Process Termination..................................................................................... 533 25.3 Exit Handlers ............................................................................................................ 533 25.4 Interactions Between fork(), stdio Buffers, and _exit() ..................................................... 537Contents in Detai l xvii
25.5 Summary.................................................................................................................. 538 25.6 Exercise ................................................................................................................... 539 26 MONITORING CHILD PROCESSES 541 26.1 Waiting on a Child Process ........................................................................................ 541 26.1.1 The wait() System Call........................................................................... 541 26.1.2 The waitpid() System Call ...................................................................... 544 26.1.3 The Wait Status Value ........................................................................... 545 26.1.4 Process Termination from a Signal Handler .............................................. 549 26.1.5 The waitid() System Call ........................................................................ 550 26.1.6 The wait3() and wait4() System Calls ...................................................... 552 26.2 Orphans and Zombies ............................................................................................... 553 26.3 The SIGCHLD Signal .................................................................................................... 555 26.3.1 Establishing a Handler for SIGCHLD .......................................................... 555 26.3.2 Delivery of SIGCHLD for Stopped Children ................................................. 559 26.3.3 Ignoring Dead Child Processes ............................................................... 559 26.4 Summary.................................................................................................................. 561 26.5 Exercises .................................................................................................................. 562 27 PROGRAM EXECUTION 563 27.1 Executing a New Program: execve() ............................................................................. 563 27.2 The exec() Library Functions......................................................................................... 567 27.2.1 The PATH Environment Variable ............................................................... 568 27.2.2 Specifying Program Arguments as a List................................................... 570 27.2.3 Passing the Caller’s Environment to the New Program ............................... 570 27.2.4 Executing a File Referred to by a Descriptor: fexecve()................................ 571 27.3 Interpreter Scripts ...................................................................................................... 572 27.4 File Descriptors and exec() .......................................................................................... 575 27.5 Signals and exec() ..................................................................................................... 578 27.6 Executing a Shell Command: system() .......................................................................... 579 27.7 Implementing system() ................................................................................................ 582 27.8 Summary.................................................................................................................. 588 27.9 Exercises .................................................................................................................. 589 28 PROCESS CREATION AND PROGRAM EXECUTION IN MORE DETAIL 591 28.1 Process Accounting.................................................................................................... 591 28.2 The clone() System Call .............................................................................................. 598 28.2.1 The clone() flags Argument ..................................................................... 603 28.2.2 Extensions to waitpid() for Cloned Children ............................................. 609 28.3 Speed of Process Creation.......................................................................................... 610 28.4 Effect of exec() and fork() on Process Attributes.............................................................. 612 28.5 Summary.................................................................................................................. 616 28.6 Exercise ................................................................................................................... 616 29 THREADS: INTRODUCTION 617 29.1 Overview ................................................................................................................. 617 29.2 Background Details of the Pthreads API ........................................................................ 620 29.3 Thread Creation........................................................................................................ 622 29.4 Thread Termination.................................................................................................... 623 29.5 Thread IDs................................................................................................................ 624 29.6 Joining with a Terminated Thread................................................................................ 625 29.7 Detaching a Thread................................................................................................... 627xviii Contents in Detai l