The GNU C Library: System & Network Applications
Total Page:16
File Type:pdf, Size:1020Kb
The GNU C Library: System & Network Applications For GNU C Libraries version 2.3.x by Sandra Loosemore with Richard M. Stallman, Roland McGrath, Andrew Oram, and Ulrich Drepper This manual documents the GNU C Libraries version 2.3.x. ISBN 1-882114-24-8, First Printing, March 2004. Published by: GNU Press Website: www.gnupress.org a division of the General: [email protected] Free Software Foundation Orders: [email protected] 51 Franklin St, Fifth Floor Tel: 617-542-5942 Boston, MA 02110-1301 USA Fax: 617-542-2652 Copyright © 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2, or any later version published by the Free Software Foundation; with the Invariant Sections being “Free Software and Free Manuals”, the “GNU Free Documentation License”," and the “GNU Lesser General Public License”, with the Front Cover Texts being “A GNU Manual”, and with the Back Cover Texts as in (a) below. A copy of the license is included in the section entitled “GNU Free Documentation License”. (a) The Back Cover Text is: You are free to copy and modify this GNU Manual. Buying copies from GNU Press supports the FSF in developing GNU and promoting software freedom. Cover art by Etienne Suvasa. Cover design by Jonathan Richard. Printed in USA. i Short Contents 1 Introduction ............................................ 1 2 Low-Level Input/Output .................................. 17 3 File-System Interface .................................... 71 4 Pipes and FIFOs ...................................... 119 5 Sockets .............................................. 125 6 Low-Level Terminal Interface ............................ 179 7 Processes ............................................ 209 8 Job Control ........................................... 221 9 System Databases and Name-Service Switch ................ 243 10 Users and Groups ...................................... 253 11 System Management ................................... 285 12 System-Configuration Parameters ......................... 303 13 DES Encryption and Password Handling ................... 327 14 Resource Usage and Limitation ........................... 335 15 Syslog ............................................... 359 16 Nonlocal Exits ........................................ 367 17 Signal Handling ....................................... 377 18 POSIX Threads ....................................... 429 A C Language Facilities in the Library ....................... 455 B Summary of Library Facilities ............................ 475 C Installing the GNU C Library ............................ 533 D Library Maintenance ................................... 543 E Contributors to the GNU C Library ........................ 551 F Free Software Needs Free Documentation................... 555 G GNU Lesser General Public License ....................... 557 H GNU Free Documentation License ........................ 567 Concept Index............................................. 575 Type Index ............................................... 583 Function and Macro Index ................................... 585 Variable and Constant Macro Index ............................ 591 Program and File Index ..................................... 599 ii The GNU C Library: System & Network Applications iii Table of Contents 1 Introduction ......................................... 1 1.1 Getting Started ................................................. 1 1.2 Standards and Portability ....................................... 1 1.2.1 ISO C ................................................ 2 1.2.2 POSIX (The Portable Operating System Interface) ...... 2 1.2.3 Berkeley Unix ........................................ 3 1.2.4 SVID (The System V Interface Description) ............ 3 1.2.5 XPG (The X/Open Portability Guide) .................. 3 1.3 Using the Library............................................... 4 1.3.1 Header Files .......................................... 4 1.3.2 Macro Definitions of Functions ........................ 5 1.3.3 Reserved Names ...................................... 6 1.3.4 Feature-Test Macros .................................. 8 1.4 Road Map to the Manual ...................................... 12 2 Low-Level Input/Output ............................ 17 2.1 Opening and Closing Files ..................................... 17 2.2 Input and Output Primitives.................................... 20 2.3 Setting the File Position of a Descriptor ........................ 25 2.4 Descriptors and Streams ....................................... 28 2.5 Dangers of Mixing Streams and Descriptors .................... 29 2.5.1 Linked Channels .................................... 29 2.5.2 Independent Channels ............................... 30 2.5.3 Cleaning Streams .................................... 30 2.6 Fast Scatter-Gather I/O ........................................ 31 2.7 Memory-Mapped I/O.......................................... 32 2.8 Waiting for Input or Output .................................... 37 2.9 Synchronizing I/O Operations.................................. 40 2.10 Perform I/O Operations in Parallel ............................ 42 2.10.1 Asynchronous Read and Write Operations ........... 45 2.10.2 Getting the Status of AIO Operations ................ 49 2.10.3 Getting into a Consistent State ...................... 50 2.10.4 Cancellation of AIO Operations ..................... 52 2.10.5 How to Optimize the AIO Implementation ........... 53 2.11 Control Operations on Files................................... 54 2.12 Duplicating Descriptors ...................................... 55 2.13 File-Descriptor Flags ......................................... 57 2.14 File Status Flags ............................................. 59 2.14.1 File-Access Modes ................................. 59 2.14.2 Open-Time Flags ................................... 60 2.14.3 I/O Operating Modes ............................... 62 2.14.4 Getting and Setting File Status Flags ................ 63 iv The GNU C Library: System & Network Applications 2.15 File Locks ................................................... 64 2.16 Interrupt-Driven Input ........................................ 68 2.17 Generic I/O Control Operations ............................... 69 3 File-System Interface ............................... 71 3.1 Working Directory ............................................ 71 3.2 Accessing Directories ......................................... 73 3.2.1 Format of a Directory Entry .......................... 73 3.2.2 Opening a Directory Stream.......................... 75 3.2.3 Reading and Closing a Directory Stream .............. 76 3.2.4 Simple Program to List a Directory ................... 77 3.2.5 Random Access in a Directory Stream ................ 78 3.2.6 Scanning the Content of a Directory .................. 79 3.2.7 Simple Program to List a Directory, Mark II .......... 80 3.3 Working with Directory Trees.................................. 81 3.4 Hard Links.................................................... 85 3.5 Symbolic Links ............................................... 87 3.6 Deleting Files ................................................. 90 3.7 Renaming Files ............................................... 91 3.8 Creating Directories ........................................... 92 3.9 File Attributes................................................. 93 3.9.1 The Meaning of the File Attributes ................... 93 3.9.2 Reading the Attributes of a File ...................... 97 3.9.3 Testing the Type of a File ............................ 99 3.9.4 File Owner......................................... 101 3.9.5 The Mode Bits for Access Permission ............... 102 3.9.6 How Your Access to a File is Decided ............... 104 3.9.7 Assigning File Permissions ......................... 104 3.9.8 Testing Permission to Access a File ................. 106 3.9.9 File Times ......................................... 108 3.9.10 File Size .......................................... 110 3.10 Making Special Files........................................ 113 3.11 Temporary Files ............................................ 114 4 Pipes and FIFOs.................................... 119 4.1 Creating a Pipe .............................................. 119 4.2 Pipe to a Subprocess ......................................... 121 4.3 FIFO Special Files ............................................ 123 4.4 Atomicity of Pipe I/O ........................................ 124 v 5 Sockets ............................................ 125 5.1 Socket Concepts ............................................. 125 5.2 Communication Styles ....................................... 126 5.3 Socket Addresses ............................................ 127 5.3.1 Address Formats ................................... 128 5.3.2 Setting the Address of a Socket ..................... 129 5.3.3 Reading the Address of a Socket .................... 130 5.4 Interface Naming ............................................ 130 5.5 The Local Namespace ........................................ 132 5.5.1 Local-Namespace Concepts......................... 132 5.5.2 Details of Local Namespace ........................ 132 5.5.3 Example of Local-Namespace Sockets .............. 133 5.6 The Internet Namespace ...................................... 134 5.6.1 Internet Socket Address Formats .................... 135 5.6.2 Host Addresses....................................