Table Of ContentThe definiTive guide To Linux The Linux
and unix® sysTem Programming
Programming
The Linux Programming Interface is the definitive guide f Write secure programs
to the Linux and UNIX programming interface—the f Write multithreaded programs using POSIX threads
interface employed by nearly every application that f Build and use shared libraries
inTerface
P
runs on a Linux or UNIX system. f Perform interprocess communication using pipes,
In this authoritative work, Linux programming message queues, shared memory, and semaphores
r
expert Michael Kerrisk provides detailed descriptions f Write network applications with the sockets API
of the system calls and library functions that you need
in order to master the craft of system programming, While The Linux Programming Interface covers a wealth i o T
of Linux-specific features, including epoll, inotify, and
and accompanies his explanations with clear, complete the /proc file system, its emphasis on UNIX standards n h A Linux and UNIX® System Programming Handbook
example programs. g
(POSIX.1-2001/SUSv3 and POSIX.1-2008/SUSv4)
You’ll find descriptions of over 500 system calls
makes it equally valuable to programmers working on T
and library functions, and more than 200 example pro- e
other UNIX platforms. r
grams, 88 tables, and 115 diagrams. You’ll learn how to: The Linux Programming Interface is the most com- e Michael KerrisK
f Read and write files efficiently prehensive single-volume work on the Linux and UNIX a
r L
f Use signals, clocks, and timers programming interface, and a book that’s destined to
f Create processes and execute programs become a new classic. f m i
n
a
m
u
c
about the author
e i x
Michael Kerrisk (http://man7.org/) has been using and programming UNIX systems
n
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 g
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.
covers current uNiX standards (PosiX.1-2001/susv3 and PosiX.1-2008/susv4)
KerrisK
THE FINEST IN GEEK ENTERTAINMENT™ $99.95 ($114.95 CDN) Shelve In: linux/programming
www.nostarch.com
ISBN: 978-1-59327-220-3
5 999 5
9 781593 272203 6 89145 72200 0
This logo applies only to the text stock.
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
(ingeneral) 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.
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 andScheduling............................................................... 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....................................................................... 937
viii Brief Contents