Đây là bộ sách tiếng anh cho dân công nghệ thông tin chuyên về bảo mật,lập trình.Thích hợp cho những ai đam mê về công nghệ thông tin,tìm hiểu về bảo mật và lập trình.
The Linux Programming inTerface A Linux and UNIX ® System Programming Handbook The Linux Programming inTerface 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. 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 1-2001 31-2008 4 5 9 9 9 5 9 7 8 1 5 9 3 2 7 2 2 0 3 ISBN: 978-1-59327-220-3 6 8 9 1 4 5 7 2 2 0 0 0 $99.95 ($114.95 CDN) Shelve In: linux/programming THE FINEST IN GEEK ENTERTAINMENT™ w w w.nos tarc h.com 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.” —M ARTIN 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.” —M EL 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.” —A NDREAS 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.” —D AVID 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.” —F ERNANDO 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.” —A NTHONY ROBINS, ASSOCIATE PROFESSOR OF COMPUTER SCIENCE, THE U NIVERSITY 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.” —C HRISTOPHE 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.” —A NDREW 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.” —B ILL 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.” —L OÏ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. BRIEF CONTENTS 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 viii Brief Contents 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 937 [...]... applications between Linux and other UNIX implementations or between Linux and other operating systems; instructors and advanced students teaching or learning Linux or UNIX system programming; and system managers and “power users” wishing to gain a greater understanding of the Linux/ UNIX programming interface and of how various pieces of system software are implemented I assume you have some prior programming. .. appear in the book The purpose and details of these programs are described in comments in the source code Makefiles are provided for building the programs, and an accompanying README file gives further details about the programs The source code is freely redistributable and modifiable under the terms of the GNU Affero General Public License (Affero GPL) version 3, a copy of which is provided in the source... to the main text Linux and UNIX This book could have been purely about standard UNIX (that is, POSIX) system programming because most features found on other UNIX implementations are also present on Linux and vice versa However, while writing portable applications is a worthy goal, it is also important to describe Linux extensions to the standard UNIX programming interface One reason for this is the. .. experience, but no previous system programming experience is required I also assume you have a reading knowledge of the C programming language, and know how to use the shell and common Linux or UNIX commands If you are new to Linux or UNIX, you will find it helpful to read the programmer-oriented review of fundamental concepts of Linux and UNIX systems in Chapter 2 The standard tutorial reference for... via a network This set of low-level interfaces is sometimes also known as the system programming interface Although I focus on Linux, I give careful attention to standards and portability issues, and clearly distinguish the discussion of Linux- specific details from the discussion of features that are common to most UNIX implementations and standardized by POSIX and the Single UNIX Specification Thus,... programming interface One reason for this is the popularity of Linux Another is that the use of nonstandard extensions is sometimes essential, either for performance reasons or to access functionality that is unavailable in the standard UNIX programming interface (All UNIX implementations provide nonstandard extensions for these reasons.) Therefore, while I’ve designed this book to be useful to programmers... C CAS T IN G THE NULL P O I N TE R 1 41 3 D KERNE L CONFIGURATION 1417 E F UR T H E R SOU R C E S O F I N F O R M A T I O N 1 41 9 F S OLUTIONS TO SELECTED EXERCISES 1425 BIBLIOGRAPHY 1437 INDEX 1447 C o n t e n ts i n D e t a i l xxix PREFACE Subject In this book, I describe the Linux programming interface the system calls, library functions, and other low-level interfaces provided by Linux, a free... a comprehensive description of the UNIX/POSIX programming interface and can be used by programmers writing applications targeted at other UNIX systems or intended to be portable across multiple systems Intended audience This book is aimed primarily at the following audience: programmers and software designers building applications for Linux, other UNIX systems, or other POSIX-conformant systems; programmers... of programming features that are specific to Linux These features include: epoll, a mechanism for obtaining notification of file I/O events; inotify, a mechanism for monitoring changes in files and directories; capabilities, a mechanism for granting a process a subset of the powers of the superuser; xxxii Preface extended attributes; i-node flags; the clone() system call; the /proc file system; and Linux- specific... possible As a comprehensive reference to the Linux/ UNIX programming interface An extensive index and frequent cross-references allow topics to be read in random order I’ve grouped the chapters of this book into the following parts: 1 Background and concepts: history of UNIX, C, and Linux and overview of UNIX standards (Chapter 1); a programmer-oriented introduction to Linux and UNIX concepts (Chapter 2); . The Linux Programming inTerface A Linux and UNIX ® System Programming Handbook The Linux Programming inTerface The Linux Programming. working on other UNIX platforms. The Linux Programming Interface is the most com- prehensive single-volume work on the Linux and UNIX programming interface,