The GNU C Library: System & Network...

624
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

Transcript of The GNU C Library: System & Network...

  • The GNU C Library:System & Network Applications

    For GNU C Libraries version 2.3.x

    by Sandra Loosemorewith 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.orga division of the General: [email protected] Software Foundation Orders: [email protected] Franklin St, Fifth Floor Tel: 617-542-5942Boston, 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 theterms of the GNU Free Documentation License, Version 1.2, or any later versionpublished by the Free Software Foundation; with the Invariant Sections being FreeSoftware and Free Manuals, the GNU Free Documentation License," and theGNU Lesser General Public License, with the Front Cover Texts being A GNUManual, and with the Back Cover Texts as in (a) below. A copy of the license isincluded 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 promotingsoftware freedom.

    Cover art by Etienne Suvasa. Cover design by Jonathan Richard. Printed in USA.

  • i

    Short Contents

    1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Low-Level Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 File-System Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714 Pipes and FIFOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1195 Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1256 Low-Level Terminal Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1797 Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2098 Job Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2219 System Databases and Name-Service Switch . . . . . . . . . . . . . . . . 24310 Users and Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25311 System Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28512 System-Configuration Parameters . . . . . . . . . . . . . . . . . . . . . . . . . 30313 DES Encryption and Password Handling . . . . . . . . . . . . . . . . . . . 32714 Resource Usage and Limitation . . . . . . . . . . . . . . . . . . . . . . . . . . . 33515 Syslog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35916 Nonlocal Exits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36717 Signal Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37718 POSIX Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429A C Language Facilities in the Library . . . . . . . . . . . . . . . . . . . . . . . 455B Summary of Library Facilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475C Installing the GNU C Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533D Library Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543E Contributors to the GNU C Library . . . . . . . . . . . . . . . . . . . . . . . . 551F Free Software Needs Free Documentation . . . . . . . . . . . . . . . . . . . 555G GNU Lesser General Public License . . . . . . . . . . . . . . . . . . . . . . . 557H GNU Free Documentation License . . . . . . . . . . . . . . . . . . . . . . . . 567Concept Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575Type Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583Function and Macro Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585Variable and Constant Macro Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591Program and File Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599

  • ii The GNU C Library: System & Network Applications

  • iii

    Table of Contents

    1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Standards and Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

    1.2.1 ISO C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.2.2 POSIX (The Portable Operating System Interface) . . . . . . 21.2.3 Berkeley Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.2.4 SVID (The System V Interface Description) . . . . . . . . . . . . 31.2.5 XPG (The X/Open Portability Guide) . . . . . . . . . . . . . . . . . . 3

    1.3 Using the Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.3.1 Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.3.2 Macro Definitions of Functions . . . . . . . . . . . . . . . . . . . . . . . . 51.3.3 Reserved Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.3.4 Feature-Test Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

    1.4 Road Map to the Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

    2 Low-Level Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172.1 Opening and Closing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172.2 Input and Output Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202.3 Setting the File Position of a Descriptor . . . . . . . . . . . . . . . . . . . . . . . . 252.4 Descriptors and Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282.5 Dangers of Mixing Streams and Descriptors . . . . . . . . . . . . . . . . . . . . 29

    2.5.1 Linked Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292.5.2 Independent Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302.5.3 Cleaning Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

    2.6 Fast Scatter-Gather I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312.7 Memory-Mapped I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322.8 Waiting for Input or Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372.9 Synchronizing I/O Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402.10 Perform I/O Operations in Parallel . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

    2.10.1 Asynchronous Read and Write Operations . . . . . . . . . . . 452.10.2 Getting the Status of AIO Operations . . . . . . . . . . . . . . . . 492.10.3 Getting into a Consistent State . . . . . . . . . . . . . . . . . . . . . . 502.10.4 Cancellation of AIO Operations . . . . . . . . . . . . . . . . . . . . . 522.10.5 How to Optimize the AIO Implementation . . . . . . . . . . . 53

    2.11 Control Operations on Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542.12 Duplicating Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552.13 File-Descriptor Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572.14 File Status Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

    2.14.1 File-Access Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592.14.2 Open-Time Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602.14.3 I/O Operating Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622.14.4 Getting and Setting File Status Flags . . . . . . . . . . . . . . . . 63

  • iv The GNU C Library: System & Network Applications

    2.15 File Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642.16 Interrupt-Driven Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682.17 Generic I/O Control Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

    3 File-System Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713.1 Working Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713.2 Accessing Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

    3.2.1 Format of a Directory Entry . . . . . . . . . . . . . . . . . . . . . . . . . . 733.2.2 Opening a Directory Stream . . . . . . . . . . . . . . . . . . . . . . . . . . 753.2.3 Reading and Closing a Directory Stream . . . . . . . . . . . . . . 763.2.4 Simple Program to List a Directory . . . . . . . . . . . . . . . . . . . 773.2.5 Random Access in a Directory Stream . . . . . . . . . . . . . . . . 783.2.6 Scanning the Content of a Directory . . . . . . . . . . . . . . . . . . 793.2.7 Simple Program to List a Directory, Mark II . . . . . . . . . . 80

    3.3 Working with Directory Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813.4 Hard Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853.5 Symbolic Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873.6 Deleting Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 903.7 Renaming Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 913.8 Creating Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923.9 File Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

    3.9.1 The Meaning of the File Attributes . . . . . . . . . . . . . . . . . . . 933.9.2 Reading the Attributes of a File . . . . . . . . . . . . . . . . . . . . . . 973.9.3 Testing the Type of a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993.9.4 File Owner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1013.9.5 The Mode Bits for Access Permission . . . . . . . . . . . . . . . 1023.9.6 How Your Access to a File is Decided . . . . . . . . . . . . . . . 1043.9.7 Assigning File Permissions . . . . . . . . . . . . . . . . . . . . . . . . . 1043.9.8 Testing Permission to Access a File . . . . . . . . . . . . . . . . . 1063.9.9 File Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083.9.10 File Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

    3.10 Making Special Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1133.11 Temporary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

    4 Pipes and FIFOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1194.1 Creating a Pipe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1194.2 Pipe to a Subprocess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1214.3 FIFO Special Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1234.4 Atomicity of Pipe I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

  • v

    5 Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1255.1 Socket Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1255.2 Communication Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1265.3 Socket Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

    5.3.1 Address Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1285.3.2 Setting the Address of a Socket . . . . . . . . . . . . . . . . . . . . . 1295.3.3 Reading the Address of a Socket . . . . . . . . . . . . . . . . . . . . 130

    5.4 Interface Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1305.5 The Local Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

    5.5.1 Local-Namespace Concepts . . . . . . . . . . . . . . . . . . . . . . . . . 1325.5.2 Details of Local Namespace . . . . . . . . . . . . . . . . . . . . . . . . 1325.5.3 Example of Local-Namespace Sockets . . . . . . . . . . . . . . 133

    5.6 The Internet Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1345.6.1 Internet Socket Address Formats . . . . . . . . . . . . . . . . . . . . 1355.6.2 Host Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

    5.6.2.1 Internet Host-Addresses . . . . . . . . . . . . . . . . . . . 1365.6.2.2 Host-Address Data Type . . . . . . . . . . . . . . . . . . 1385.6.2.3 Host-Address Functions . . . . . . . . . . . . . . . . . . . 1395.6.2.4 Host Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

    5.6.3 Internet Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1445.6.4 The Services Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1455.6.5 Byte-Order Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1475.6.6 Protocols Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1475.6.7 Internet Socket Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

    5.7 Other Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1505.8 Opening and Closing Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

    5.8.1 Creating a Socket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1515.8.2 Closing a Socket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1525.8.3 Socket Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

    5.9 Using Sockets with Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1535.9.1 Making a Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1535.9.2 Listening for Connections . . . . . . . . . . . . . . . . . . . . . . . . . . 1555.9.3 Accepting Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1555.9.4 Who Is Connected to Me? . . . . . . . . . . . . . . . . . . . . . . . . . . 1575.9.5 Transferring Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

    5.9.5.1 Sending Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1575.9.5.2 Receiving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 1585.9.5.3 Socket Data Options . . . . . . . . . . . . . . . . . . . . . . 159

    5.9.6 Byte-Stream Socket Example . . . . . . . . . . . . . . . . . . . . . . . 1605.9.7 Byte-Stream Connection Server Example . . . . . . . . . . . . 1615.9.8 Out-of-Band Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

    5.10 Datagram Socket Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1675.10.1 Sending Datagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1675.10.2 Receiving Datagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1685.10.3 Datagram Socket Example . . . . . . . . . . . . . . . . . . . . . . . . . 1695.10.4 Example of Reading Datagrams . . . . . . . . . . . . . . . . . . . . 170

  • vi The GNU C Library: System & Network Applications

    5.11 The inetd Daemon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1725.11.1 inetd Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1725.11.2 Configuring inetd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

    5.12 Socket Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1735.12.1 Socket Option Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 1735.12.2 Socket-Level Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174

    5.13 Networks Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176

    6 Low-Level Terminal Interface . . . . . . . . . . . . . . . . . . . . . 1796.1 Identifying Terminals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1796.2 I/O Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1806.3 Two Styles of Input: Canonical or Not . . . . . . . . . . . . . . . . . . . . . . . . 1806.4 Terminal Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

    6.4.1 Terminal Mode Data Types . . . . . . . . . . . . . . . . . . . . . . . . . 1816.4.2 Terminal Mode Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 1826.4.3 Setting Terminal Modes Properly . . . . . . . . . . . . . . . . . . . . 1836.4.4 Input Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1856.4.5 Output Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1876.4.6 Control Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1876.4.7 Local Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1896.4.8 Line Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1926.4.9 Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

    6.4.9.1 Characters for Input Editing . . . . . . . . . . . . . . . 1946.4.9.2 Characters that Cause Signals . . . . . . . . . . . . . 1966.4.9.3 Special Characters for Flow Control . . . . . . . 1976.4.9.4 Other Special Characters . . . . . . . . . . . . . . . . . . 198

    6.4.10 Noncanonical Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1986.5 BSD Terminal Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2006.6 Line Control Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2016.7 Noncanonical-Mode Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2036.8 Pseudoterminals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

    6.8.1 Allocating Pseudoterminals . . . . . . . . . . . . . . . . . . . . . . . . . 2056.8.2 Opening a Pseudoterminal Pair . . . . . . . . . . . . . . . . . . . . . . 207

    7 Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2097.1 Running a Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2097.2 Process-Creation Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2107.3 Process Identification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2107.4 Creating a Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2117.5 Executing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2127.6 Process Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2157.7 Process-Completion Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2187.8 BSD Process Wait Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2187.9 Process-Creation Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219

  • vii

    8 Job Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2218.1 Concepts of Job Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2218.2 Job Control Is Optional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2228.3 Controlling Terminal of a Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2228.4 Access to the Controlling Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2238.5 Orphaned Process-Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2238.6 Implementing a Job-Control Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

    8.6.1 Data Structures for the Shell . . . . . . . . . . . . . . . . . . . . . . . . 2248.6.2 Initializing the Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2268.6.3 Launching Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2288.6.4 Foreground and Background . . . . . . . . . . . . . . . . . . . . . . . . 2328.6.5 Stopped and Terminated Jobs . . . . . . . . . . . . . . . . . . . . . . . 2338.6.6 Continuing Stopped Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . 2378.6.7 The Missing Pieces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

    8.7 Functions for Job Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2388.7.1 Identifying the Controlling Terminal . . . . . . . . . . . . . . . . 2388.7.2 Process-Group Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 2398.7.3 Functions for Controlling-Terminal Access . . . . . . . . . . 241

    9 System Databases and Name-Service Switch . . . . . . . 2439.1 NSS Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2439.2 The NSS Configuration File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

    9.2.1 Services in the NSS Configuration File . . . . . . . . . . . . . . . 2459.2.2 Actions in the NSS Configuration . . . . . . . . . . . . . . . . . . . . 2459.2.3 Notes on the NSS Configuration File . . . . . . . . . . . . . . . . . 246

    9.3 NSS Module Internals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2479.3.1 The Naming Scheme of the NSS Modules . . . . . . . . . . . . 2479.3.2 The Interface of the Function in NSS Modules . . . . . . . . 248

    9.4 Extending NSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2509.4.1 Adding Another Service to NSS . . . . . . . . . . . . . . . . . . . . . 2509.4.2 Internals of the NSS Module Functions . . . . . . . . . . . . . . 251

  • viii The GNU C Library: System & Network Applications

    10 Users and Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25310.1 User- and Group-IDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25310.2 The Persona of a Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25310.3 Why Change the Persona of a Process? . . . . . . . . . . . . . . . . . . . . . . 25410.4 How an Application Can Change Persona . . . . . . . . . . . . . . . . . . . . 25410.5 Reading the Persona of a Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25510.6 Setting the User ID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25610.7 Setting the Group IDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25710.8 Enabling and Disabling Setuid Access . . . . . . . . . . . . . . . . . . . . . . . 26010.9 Setuid Program Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26010.10 Tips for Writing Setuid Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . 26310.11 Identifying Who Is Logged In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26410.12 The User-Accounting Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265

    10.12.1 Manipulating the User-Accounting Database . . . . . . 26510.12.2 XPG User-Accounting Database Functions . . . . . . . . 27010.12.3 Logging In and Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

    10.13 User Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27410.13.1 The Data Structure That Describes a User . . . . . . . . . 27410.13.2 Looking Up One User . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27410.13.3 Scanning the List of All Users . . . . . . . . . . . . . . . . . . . . 27510.13.4 Writing a User Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276

    10.14 Group Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27710.14.1 The Data Structure for a Group . . . . . . . . . . . . . . . . . . . 27710.14.2 Looking Up One Group . . . . . . . . . . . . . . . . . . . . . . . . . . 27710.14.3 Scanning the List of All Groups . . . . . . . . . . . . . . . . . . 278

    10.15 User- and Group- Database Example . . . . . . . . . . . . . . . . . . . . . . . 27910.16 Netgroup Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

    10.16.1 Netgroup Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28110.16.2 Looking Up One Netgroup . . . . . . . . . . . . . . . . . . . . . . . 28210.16.3 Testing for Netgroup Membership . . . . . . . . . . . . . . . . 283

    11 System Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28511.1 Host Identification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28511.2 Platform-Type Identification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28711.3 Controlling and Querying Mounts . . . . . . . . . . . . . . . . . . . . . . . . . . . 289

    11.3.1 Mount Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28911.3.1.1 The fstab File . . . . . . . . . . . . . . . . . . . . . . . 29011.3.1.2 The mtab File . . . . . . . . . . . . . . . . . . . . . . . . . 29211.3.1.3 Other (Non-libc) Sources of Mount

    Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29611.3.2 Mount, Unmount, Remount . . . . . . . . . . . . . . . . . . . . . . . . 296

    11.4 System Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300

  • ix

    12 System-Configuration Parameters . . . . . . . . . . . . . . . . 30312.1 General Capacity-Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30312.2 Overall System Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30512.3 Which Version of POSIX is Supported . . . . . . . . . . . . . . . . . . . . . . . . 30612.4 Using sysconf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306

    12.4.1 Definition of sysconf . . . . . . . . . . . . . . . . . . . . . . . . . . . 30712.4.2 Constants for sysconf Parameters . . . . . . . . . . . . . . . . 30712.4.3 Examples of sysconf . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316

    12.5 Minimum Values for General Capacity-Limits . . . . . . . . . . . . . . . . 31712.6 Limits on File-System Capacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31812.7 Optional Features in File Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31912.8 Minimum Values for File-System Limits . . . . . . . . . . . . . . . . . . . . . 32012.9 Using pathconf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32112.10 Utility Program Capacity-Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32312.11 Minimum Values for Utility Limits . . . . . . . . . . . . . . . . . . . . . . . . . 32412.12 String-Valued Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324

    13 DES Encryption and Password Handling . . . . . . . . . 32713.1 Legal Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32713.2 Reading Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32813.3 Encrypting Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32913.4 DES Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331

    14 Resource Usage and Limitation . . . . . . . . . . . . . . . . . . . 33514.1 Resource Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33514.2 Limiting Resource Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33814.3 Process CPU Priority and Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . 342

    14.3.1 Absolute Priority . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34314.3.1.1 Using Absolute Priority . . . . . . . . . . . . . . . . . . 344

    14.3.2 Real-Time Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34514.3.3 Basic Scheduling Functions . . . . . . . . . . . . . . . . . . . . . . . . 34614.3.4 Traditional Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349

    14.3.4.1 Introduction to Traditional Scheduling . . . . 34914.3.4.2 Functions for Traditional Scheduling . . . . . 350

    14.3.5 Limiting Execution to Certain CPUs . . . . . . . . . . . . . . . . 35214.4 Querying Memory-Available Resources . . . . . . . . . . . . . . . . . . . . . . 354

    14.4.1 Overview of Traditional Unix Memory-Handling . . . 35414.4.2 How to Get Information About the Memory Subsystem?

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35514.5 Learn About the Processors Available . . . . . . . . . . . . . . . . . . . . . . . . 356

  • x The GNU C Library: System & Network Applications

    15 Syslog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35915.1 Overview of Syslog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35915.2 Submitting Syslog Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

    15.2.1 openlog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36015.2.2 syslog, vsyslog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36215.2.3 closelog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36515.2.4 setlogmask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36515.2.5 Syslog Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366

    16 Nonlocal Exits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36716.1 Introduction to Nonlocal Exits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36716.2 Details of Nonlocal Exits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36916.3 Nonlocal Exits and Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37016.4 Complete Context Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370

    17 Signal Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37717.1 Basic Concepts of Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377

    17.1.1 Some Kinds of Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37717.1.2 Concepts of Signal Generation . . . . . . . . . . . . . . . . . . . . . 37817.1.3 How Signals Are Delivered . . . . . . . . . . . . . . . . . . . . . . . . 378

    17.2 Standard Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37917.2.1 Program-Error Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37917.2.2 Termination Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38217.2.3 Alarm Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38417.2.4 Asynchronous-I/O Signals . . . . . . . . . . . . . . . . . . . . . . . . . 38417.2.5 Job Control Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38517.2.6 Operation-Error Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . 38717.2.7 Miscellaneous Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38717.2.8 Signal Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388

    17.3 Specifying Signal Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38917.3.1 Basic Signal-Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38917.3.2 Advanced Signal-Handling . . . . . . . . . . . . . . . . . . . . . . . . 39217.3.3 Interaction of signal and sigaction . . . . . . . . . . 39317.3.4 sigaction Function Example . . . . . . . . . . . . . . . . . . . 39317.3.5 Flags for sigaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39517.3.6 Initial Signal Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396

    17.4 Defining Signal-Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39617.4.1 Signal Handlers That Return . . . . . . . . . . . . . . . . . . . . . . . 39717.4.2 Handlers That Terminate the Process . . . . . . . . . . . . . . . 39817.4.3 Nonlocal Control-Transfer in Handlers . . . . . . . . . . . . . 39917.4.4 Signals Arriving While a Handler Runs . . . . . . . . . . . . 40017.4.5 Signals Close Together Merge into One . . . . . . . . . . . . 40117.4.6 Signal Handling and Nonreentrant Functions . . . . . . . 40417.4.7 Atomic Data-Access and Signal-Handling . . . . . . . . . . 406

    17.4.7.1 Problems with Nonatomic Access . . . . . . . . 406

  • xi

    17.4.7.2 Atomic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 40717.4.7.3 Atomic Usage-Patterns . . . . . . . . . . . . . . . . . . 407

    17.5 Primitives Interrupted by Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40817.6 Generating Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409

    17.6.1 Signaling Yourself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40917.6.2 Signaling Another Process . . . . . . . . . . . . . . . . . . . . . . . . . 41017.6.3 Permission for Using kill . . . . . . . . . . . . . . . . . . . . . . . . 41117.6.4 Using kill for Communication . . . . . . . . . . . . . . . . . . . 412

    17.7 Blocking Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41417.7.1 Why Blocking Signals Is Useful . . . . . . . . . . . . . . . . . . . 41417.7.2 Signal Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41417.7.3 Process Signal-Mask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41617.7.4 Blocking to Test for Delivery of a Signal . . . . . . . . . . . 41717.7.5 Blocking Signals for a Handler . . . . . . . . . . . . . . . . . . . . 41817.7.6 Checking for Pending Signals . . . . . . . . . . . . . . . . . . . . . . 41917.7.7 Remembering a Signal to Act on Later . . . . . . . . . . . . . 420

    17.8 Waiting for a Signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42117.8.1 Using pause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42117.8.2 Problems with pause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42217.8.3 Using sigsuspend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423

    17.9 Using a Separate Signal-Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42417.10 BSD Signal-Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

    17.10.1 BSD Function to Establish a Handler . . . . . . . . . . . . . . 42617.10.2 BSD Functions for Blocking Signals . . . . . . . . . . . . . . . 427

    18 POSIX Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42918.1 Basic Thread Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42918.2 Thread Attributessection Thread Attributes . . . . . . . . . . . . . . . . . . . 43018.3 Cancellation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43318.4 Clean-Up Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43518.5 Mutexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43718.6 Condition Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44118.7 POSIX Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44418.8 Thread-Specific Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44518.9 Threads and Signal-Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44718.10 Threads and Fork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44818.11 Streams and Fork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45018.12 Miscellaneous Thread Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451

  • xii The GNU C Library: System & Network Applications

    Appendix A C Language Facilities in the Library . . . . 455A.1 Explicitly Checking Internal Consistency . . . . . . . . . . . . . . . . . . . . . 455A.2 Variadic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456

    A.2.1 Why Variadic Functions Are Used . . . . . . . . . . . . . . . . . . 457A.2.2 How Variadic Functions Are Defined and Used . . . . . . 457

    A.2.2.1 Syntax for Variable Arguments . . . . . . . . . . . 458A.2.2.2 Receiving the Argument Values . . . . . . . . . . . 458A.2.2.3 How Many Arguments Were Supplied . . . . . 459A.2.2.4 Calling Variadic Functions . . . . . . . . . . . . . . . . 460A.2.2.5 Argument-Access Macros . . . . . . . . . . . . . . . . 460

    A.2.3 Example of a Variadic Function . . . . . . . . . . . . . . . . . . . . 462A.2.3.1 Old-Style Variadic Functions . . . . . . . . . . . . . 462

    A.3 Null-Pointer Constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463A.4 Important Data-Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464A.5 Data-Type Measurements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464

    A.5.1 Computing the Width of an Integer Data Type . . . . . . . 465A.5.2 Range of an Integer Type . . . . . . . . . . . . . . . . . . . . . . . . . . . 465A.5.3 Floating-Type Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467

    A.5.3.1 Floating-Point Representation Concepts . . . 467A.5.3.2 Floating-Point Parameters . . . . . . . . . . . . . . . . 468A.5.3.3 IEEE Floating-Point . . . . . . . . . . . . . . . . . . . . . . 472

    A.5.4 Structure Field Offset Measurement . . . . . . . . . . . . . . . . 472

    Appendix B Summary of Library Facilities . . . . . . . . . . 475

    Appendix C Installing the GNU C Library . . . . . . . . . . . 533C.1 Configuring and Compiling GNU libc . . . . . . . . . . . . . . . . . . . . . . . . . 533C.2 Installing the C Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536C.3 Recommended Tools for Compilation . . . . . . . . . . . . . . . . . . . . . . . . . 538C.4 Supported Configurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539C.5 Specific Advice for GNU/Linux Systems . . . . . . . . . . . . . . . . . . . . . . 540C.6 Reporting Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541

    Appendix D Library Maintenance . . . . . . . . . . . . . . . . . . . 543D.1 Adding New Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543D.2 Porting the GNU C Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544

    D.2.1 Layout of the sysdeps Directory Hierarchy . . . . . . 547D.2.2 Porting the GNU C Library to Unix Systems . . . . . . . . . 549

    Appendix E Contributors to the GNU C Library . . . . . 551

    Appendix F Free Software Needs Free Documentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555

  • xiii

    Appendix G GNU Lesser General Public License . . . . . 557G.0.1 Preamble . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557G.0.2 TERMS AND CONDITIONS FOR COPYING,

    DISTRIBUTION AND MODIFICATION . . . . . . . . . . . . . 559G.0.3 How to Apply These Terms to Your New Libraries . . 566

    Appendix H GNU Free Documentation License . . . . . . 567H.0.1 ADDENDUM: How to Use This License for Your

    Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573

    Concept Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575

    Type Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583

    Function and Macro Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585

    Variable and Constant Macro Index . . . . . . . . . . . . . . . . . . . 591

    Program and File Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599

  • xiv The GNU C Library: System & Network Applications

  • The GNU C Library: System and Network Applications

  • Chapter 1: Introduction 1

    1 IntroductionThe C language provides no built-in facilities for performing such common op-

    erations as input/output, memory management, string manipulation and the like.Instead, these facilities are defined in a standard library, which you compile andlink with your programs.

    The GNU C Library, described in this document, defines all of the library func-tions that are specified by the ISO C standard, as well as additional features specificto POSIX and other derivatives of the Unix operating system, and extensions spe-cific to the GNU system.

    The purpose of this manual is to tell you how to use the facilities of the GNUlibrary. We have mentioned which features belong to which standards to help youidentify things that are potentially nonportable. But the emphasis in this manual isnot on strict portability.

    1.1 Getting StartedThis manual is written with the assumption that you are at least somewhat fa-

    miliar with the C programming language and basic programming concepts. Specif-ically, familiarity with ISO standard C (see Section 1.2.1 [ISO C], page 2), ratherthan traditional pre-ISO C dialects, is assumed.

    The GNU C Library includes several header files, each of which provides def-initions and declarations for a group of related facilities; this information is usedby the C compiler when processing your program. For example, the header filestdio.h declares facilities for performing input and output, and the header filestring.h declares string-processing utilities. The organization of this manualgenerally follows the same division as the header files.

    If you are reading this manual for the first time, you should read all of the in-troductory material and skim the remaining chapters. There are a lot of functionsin the GNU C Library and it is not realistic to expect that you will be able to re-member exactly how to use each and every one of them. It is more important tobecome generally familiar with the kinds of facilities that the library provides, sothat when you are writing your programs you can recognize when to make use oflibrary functions, and where in this manual you can find more specific informationabout them.

    1.2 Standards and PortabilityThis section discusses the various standards and other sources that the GNU C

    Library is based upon. These sources include the ISO C and POSIX standards, andthe System V and Berkeley Unix implementations.

    The primary focus of this manual is to tell you how to make effective use ofthe GNU library facilities. But if you are concerned about making your programscompatible with these standards, or portable to operating systems other than GNU,

  • 2 The GNU C Library: System & Network Applications

    this can affect how you use the library. This section gives you an overview of thesestandards, so that you will know what they are when they are mentioned in otherparts of the manual.

    See Appendix B [Summary of Library Facilities], page 475, for an alphabeticallist of the functions and other symbols provided by the library. This list also stateswhich standards each function or symbol comes from.

    1.2.1 ISO C

    The GNU C Library is compatible with the C standard adopted by the Amer-ican National Standards Institute (ANSI)as American National Standard X3.159-1989"ANSI C" and later by the International Standardization Organization (ISO)as ISO/IEC 9899:1990, "Programming languagesC". In this manual, we refer tothe standard as ISO C since this is the more general standard with respect to rati-fication. The header files and library facilities that make up the GNU library are asuperset of those specified by the ISO C standard.

    If you are concerned about strict adherence to the ISO C standard, you should usethe -ansi option when you compile your programs with the GNU C Compiler.This tells the compiler to define only ISO standard features from the library headerfiles, unless you explicitly ask for additional features. See Section 1.3.4 [Feature-Test Macros], page 8, for information on how to do this.

    Being able to restrict the library to include only ISO C features is important be-cause ISO C puts limitations on what names can be defined by the library imple-mentation, and the GNU extensions dont fit these limitations. See Section 1.3.3[Reserved Names], page 6, for more information about these restrictions.

    This manual does not attempt to give you complete details on the differencesbetween ISO C and older dialects. It gives advice on how to write programs towork portably under multiple C dialects, but does not aim for completeness.

    1.2.2 POSIX (The Portable Operating System Interface)

    The GNU library is also compatible with the ISO POSIX family of standards,known more formally as the Portable Operating System Interface for ComputerEnvironments (ISO/IEC 9945). They were also published as ANSI/IEEE Std 1003.POSIX is derived mostly from various versions of the Unix operating system.

    The library facilities specified by the POSIX standards are a superset of thoserequired by ISO C; POSIX specifies additional features for ISO C functions, as wellas specifying new additional functions. In general, the additional requirements andfunctionality defined by the POSIX standards are aimed at providing lower-levelsupport for a particular kind of operating system environment, rather than generalprogramming language support that can run in many diverse operating system en-vironments.

    The GNU C Library implements all of the functions specified in ISO/IEC 9945-1:1996, the POSIX System Application Program Interface, commonly referred toas POSIX.1. The primary extensions to the ISO C facilities specified by this stan-

  • Chapter 1: Introduction 3

    dard include file-system interface primitives (see Chapter 3 [File-System Interface],page 71), device-specific terminal control functions (see Chapter 6 [Low-LevelTerminal Interface], page 179) and process control functions (see Chapter 7 [Pro-cesses], page 209).

    Some facilities from ISO/IEC 9945-2:1993, the POSIX Shell and Utilities standard(POSIX.2) are also implemented in the GNU library. These include utilities fordealing with regular expressions and other pattern-matching facilities.1

    1.2.3 Berkeley Unix

    The GNU C Library defines facilities from some versions of Unix that are notformally standardized, specifically from the 4.2 BSD, 4.3 BSD and 4.4 BSD Unixsystems (also known as Berkeley Unix) and from SunOS (a popular 4.2 BSD deriva-tive that includes some Unix System V functionality). These systems support mostof the ISO C and POSIX facilities, and 4.4 BSD and newer releases of SunOS in factsupport them all.

    The BSD facilities include symbolic links (see Section 3.5 [Symbolic Links],page 87), the select function (see Section 2.8 [Waiting for Input or Output],page 37), the BSD signal functions (see Section 17.10 [BSD Signal-Handling],page 426) and sockets (see Chapter 5 [Sockets], page 125).

    1.2.4 SVID (The System V Interface Description)

    The System V Interface Description (SVID) is a document describing the AT&TUnix System V operating system. It is to some extent a superset of the POSIXstandard.

    The GNU C Library defines most of the facilities required by the SVID that arenot also required by the ISO C or POSIX standards, for compatibility with SystemV Unix and other Unix systems (such as SunOS) that include these facilities. How-ever, many of the more obscure and less generally useful facilities required by theSVID are not included. (In fact, Unix System V itself does not provide them all.)

    The supported facilities from System V include the methods for inter-processcommunication and shared memory, the hsearch and drand48 families of func-tions, fmtmsg and several of the mathematical functions.

    1.2.5 XPG (The X/Open Portability Guide)

    The X/Open Portability Guide2 is a more general standard than POSIX. X/Openowns the Unix copyright and the XPG specifies the requirements for systems thatare intended to be Unix systems.

    1 See Sandra Loosemore et al., Pattern-Matching in GNU C Library: Application Fundamen-tals (Boston: GNU Press, 2004), available online at http:// www.gnu.org/ manual/manual.html.

    2 X/Open Company, X/Open Portability Guide, Issue 4 (Reading, UK: X/Open Company, Ltd.,1992).

  • 4 The GNU C Library: System & Network Applications

    The GNU C Library complies with the X/Open Portability Guide, Issue 4.2, withall extensions common to XSI (X/Open System Interface) compliant systems andalso all X/Open Unix extensions.

    The additions on top of POSIX are mainly derived from functionality available inSystem V and BSD systems, though some of the really bad mistakes in System Vsystems were corrected. Since fulfilling the XPG standard with the Unix extensionsis a precondition for getting the Unix brand, chances are good that the functionalityis available on commercial systems.

    1.3 Using the Library

    This section describes some of the practical issues involved in using the GNU CLibrary.

    1.3.1 Header Files

    Libraries for use by C programs really consist of two parts: header files thatdefine types and macros and declare variables and functions, and the actual libraryor archive that contains the definitions of the variables and functions.

    (Recall that in C, a declaration merely provides information that a function orvariable exists and gives its type. For a function declaration, information about thetypes of its arguments might be provided as well. The purpose of declarations isto allow the compiler to correctly process references to the declared variables andfunctions. A definition, on the other hand, actually allocates storage for a variableor says what a function does.)

    In order to use the facilities in the GNU C Library, you should be sure that yourprogram source files include the appropriate header files. This is so that the com-piler has declarations of these facilities available and can correctly process refer-ences to them. Once your program has been compiled, the linker resolves thesereferences to the actual definitions provided in the archive file.

    Header files are included into a program source file by the #include prepro-cessor directive. The C language supports two forms of this directive; the first,

    #include "header"

    is typically used to include a header file header that you write yourself; this wouldcontain definitions and declarations describing the interfaces between the differentparts of your particular application. By contrast,

    #include

    is typically used to include a header file file.h that contains definitions anddeclarations for a standard library. This file would normally be installed in a stan-dard place by your system administrator. You should use this second form for theC library header files.

  • Chapter 1: Introduction 5

    Typically, #include directives are placed at the top of the C source file, be-fore any other code.3 If you begin your source files with some comments explainingwhat the code in the file does (a good idea), put the #include directives im-mediately afterward, following the feature-test macro definition (see Section 1.3.4[Feature-Test Macros], page 8).

    The GNU C Library provides several header files, each of which contains the typeand macro definitions and variable and function declarations for a group of relatedfacilities. This means that your programs may need to include several header files,depending on exactly which facilities you are using.

    Some library header files include other library header files automatically. How-ever, as a matter of programming style, you should not rely on this; it is better toexplicitly include all the header files required for the library facilities you are using.The GNU C Library header files have been written in such a way that it doesnt mat-ter if a header file is accidentally included more than once; including a header filea second time has no effect. Likewise, if your program needs to include multipleheader files, the order in which they are included doesnt matter.

    Compatibility Note: Inclusion of standard header files in any order and anynumber of times works in any ISO C implementation. However, this has tradition-ally not been the case in many older C implementations.

    Strictly speaking, you dont have to include a header file to use a function itdeclares; you could declare the function explicitly yourself, according to the spec-ifications in this manual. But it is usually better to include the header file becauseit may define types and macros that are not otherwise available and because it maydefine more efficient macro replacements for some functions. It is also a sure wayto have the correct declaration.

    1.3.2 Macro Definitions of Functions

    If we describe something as a function in this manual, it may have a macrodefinition as well. This normally has no effect on how your program runsthemacro definition does the same thing as the function would. In particular, macroequivalents for library functions evaluate arguments exactly once, in the same waythat a function call would. The main reason for these macro definitions is thatsometimes they can produce an in-line expansion that is considerably faster than anactual function call.

    Taking the address of a library function works even if it is also defined as amacro. This is because, in this context, the name of the function isnt followed bythe left parenthesis that is syntactically necessary to recognize a macro call.

    You might occasionally want to avoid using the macro definition of a functionperhaps to make your program easier to debug. There are two ways you can dothis:

    3 For more information about the use of header files and #include directives, see Richard M.Stallman and the GCC Developer Community, Header Files in The GNU C Preprocesser Manual(2003), http:// gcc.gnu.org/ onlinedocs/ gcc-3.3.2/ cpp/.

    http:// gcc.gnu.org/ onlinedocs/ gcc-3.3.2/ cpp/

  • 6 The GNU C Library: System & Network Applications

    1. You can avoid a macro definition in a specific use by enclosing the name ofthe function in parentheses. This works because the name of the function doesnot appear in a syntactic context where it is recognizable as a macro call.

    2. You can suppress any macro definition for a whole source file by using the#undef preprocessor directive, unless otherwise stated explicitly in the de-scription of that facility.

    For example, suppose the header file stdlib.h declares a function namedabs with:

    extern int abs (int);

    and also provides a macro definition for abs. Then, in:#include

    int f (int *i) { return abs (++*i); }

    the reference to abs might refer to either a macro or a function. On the other hand,in each of the following examples, the reference is to a function and not a macro:

    #include

    int g (int *i) { return (abs) (++*i); }

    #undef abs

    int h (int *i) { return abs (++*i); }

    Since macro definitions that double for a function behave in exactly the same wayas the actual function version, there is usually no need for any of these methods. Infact, removing macro definitions usually just makes your program slower.

    1.3.3 Reserved Names

    The names of all library types, macros, variables and functions that come fromthe ISO C standard are reserved unconditionally; your program may not redefinethese names. All other library names are reserved if your program explicitly in-cludes the header file that defines or declares them. There are several reasons forthese restrictions:

    Other people reading your code could get very confused if, for example, youwere using a function named exit to do something completely different fromwhat the standard exit function does. Preventing this situation helps to makeyour programs easier to understand and contributes to modularity and main-tainability.

    It avoids the possibility of a user accidentally redefining a library function thatis called by other library functions. If redefinition were allowed, those otherfunctions would not work properly.

    It allows the compiler to do whatever special optimizations it pleases on callsto these functions, without the possibility that they may have been redefinedby the user. Some library facilities, such as those for dealing with variadicarguments (see Section A.2 [Variadic Functions], page 456) and nonlocal exits(see Chapter 16 [Nonlocal Exits], page 367), actually require a considerable

  • Chapter 1: Introduction 7

    amount of cooperation on the part of the C compiler, and with respect to theimplementation, it might be easier for the compiler to treat these as built-inparts of the language.

    In addition to the names documented in this manual, reserved names include allexternal identifiers (global functions and variables) that begin with an underscore(_) and all identifiers regardless of use that begin with either two underscores oran underscore followed by a capital letter. This is so that the library and headerfiles can define functions, variables, and macros for internal purposes without riskof conflict with names in user programs.

    Some additional classes of identifier names are reserved for future extensions tothe C language or the POSIX.1 environment. While using these names for your ownpurposes right now might not cause a problem, there is the possibility of conflictwith future versions of the C or POSIX standards, so you should avoid using them:

    Names beginning with a capital E followed by a digit or uppercase letter maybe used for additional error-code names.4

    Names that begin with either is or to followed by a lowercase letter maybe used for additional character-testing and conversion functions.5

    Names that begin with LC_ followed by an uppercase letter may be used foradditional macros specifying locale attributes.6

    Names of all existing mathematics functions suffixed with f or l arereserved for corresponding functions that operate on float and longdouble arguments, respectively.7

    Names that begin with SIG followed by an uppercase letter are reserved foradditional signal names (see Section 17.2 [Standard Signals], page 379).

    Names that begin with SIG_ followed by an uppercase letter are reservedfor additional signal actions (see Section 17.3.1 [Basic Signal-Handling],page 389).

    Names beginning with str, mem, or wcs followed by a lowercase letterare reserved for additional string and array functions.8

    Names that end with _t are reserved for additional type names.

    In addition, some individual header files reserve names beyond those that theyactually define. You only need to worry about these restrictions if your programincludes that particular header file.

    The header file dirent.h reserves names prefixed with d_. The header file fcntl.h reserves names prefixed with l_, F_, O_,

    and S_. The header file grp.h reserves names prefixed with gr_.

    4 Loosemore et al., Error-Reporting (see chap. 1, n.1).5 Ibid., Character Handling.6 Ibid., Locales and Internationalization.7 Ibid., Mathematics.8 Ibid., String and Array Utilities.

  • 8 The GNU C Library: System & Network Applications

    The header file limits.h reserves names suffixed with _MAX. The header file pwd.h reserves names prefixed with pw_. The header file signal.h reserves names prefixed with sa_ and SA_. The header file sys/stat.h reserves names prefixed with st_ and S_.

    The header file sys/times.h reserves names prefixed with tms_. The header file termios.h reserves names prefixed with c_, V, I,

    O, and TC; and names prefixed with B followed by a digit.

    1.3.4 Feature-Test Macros

    The exact set of features available when you compile a source file is controlledby which feature-test macros you define.

    If you compile your programs using gcc -ansi, you get only the ISO C li-brary features, unless you explicitly request additional features by defining one ormore of the feature macros.9

    You should define these macros by using #define preprocessor directivesat the top of your source code files. These directives must come before any#include of a system header file. It is best to make them the very first thingin the file, preceded only by comments. You could also use the -D option toGCC, but it is better if you make the source files indicate their own meaning in aself-contained way.

    This system exists to allow the library to conform to multiple standards. Al-though the different standards are often described as supersets of each other, theyare usually incompatible because larger standards require functions with names thatsmaller ones reserve to the user program. This is not mere pedantryit has been aproblem in practice. For instance, some non-GNU programs define functions namedgetline that have nothing to do with this librarys getline. They would notbe compilable if all features were enabled indiscriminately.

    This should not be used to verify that a program conforms to a limited standard.It is insufficient for this purpose, as it will not protect you from including headerfiles outside the standard, or relying on semantics undefined within the standard.

    MacroPOSIX SOURCEIf you define this macro, then the functionality from the POSIX.1 standard (IEEEStandard 1003.1) is available, as well as all of the ISO C facilities.The state of _POSIX_SOURCE is irrelevant if you define the macro _POSIX_C_SOURCE to a positive integer.

    9 See Richard M. Stallman and the GCC Developer Community, Invoking GCC in Using GCC:The GNU Compiler Collection Reference Manual (Boston, MA: GNU Press, October 2003),http:// gcc.gnu.org/ onlinedocs/ gcc-3.3.2/ gcc/, for more information aboutGCC options.

    http:// gcc.gnu.org/ onlinedocs/ gcc-3.3.2/ gcc/

  • Chapter 1: Introduction 9

    MacroPOSIX C SOURCEDefine this macro to a positive integer to control which POSIX functionality ismade available. The greater the value of this macro, the more functionality ismade available.If you define this macro to a value greater than or equal to 1, then the functional-ity from the 1990 edition of the POSIX.1 standard (IEEE Standard 1003.1-1990)is made available.If you define this macro to a value greater than or equal to 2, then the functional-ity from the 1992 edition of the POSIX.2 standard (IEEE Standard 1003.2-1992)is made available.If you define this macro to a value greater than or equal to 199309L, then thefunctionality from the 1993 edition of the POSIX.1b standard (IEEE Standard1003.1b-1993) is made available.Greater values for _POSIX_C_SOURCE will enable future extensions. ThePOSIX standards process will define these values as necessary, and the GNU CLibrary should support them some time after they become standardized. The1996 edition of POSIX.1 (ISO/IEC 9945-1: 1996) states that if you define _POSIX_C_SOURCE to a value greater than or equal to 199506L, then thefunctionality from the 1996 edition is made available.

    MacroBSD SOURCEIf you define this macro, functionality derived from 4.3 BSD Unix is included aswell as the ISO C, POSIX.1, and POSIX.2 material.Some of the features derived from 4.3 BSD Unix conflict with the correspondingfeatures specified by the POSIX.1 standard. If this macro is defined, the 4.3 BSDdefinitions take precedence over the POSIX definitions.Due to the nature of some of the conflicts between 4.3 BSD and POSIX.1, youneed to use a special BSD compatibility library when linking programs com-piled for BSD compatibility. This is because some functions must be definedin two different ways, one in the normal C library, and one in the compatibil-ity library. If your program defines _BSD_SOURCE, you must give the option-lbsd-compat to the compiler or linker when linking the program, to tellit to find functions in this special compatibility library before looking for themin the normal C library.

    MacroSVID SOURCEIf you define this macro, functionality derived from SVID is included as well asthe ISO C, POSIX.1, POSIX.2 and X/Open material.

    MacroXOPEN SOURCEMacroXOPEN SOURCE EXTENDED

    If you define this macro, functionality described in the X/Open PortabilityGuide10 is included. This is a superset of the POSIX.1 and POSIX.2 functional-

    10 X/Open Company, X/Open Portability Guide, Issue 4, Version 2 (Reading, UK: X/Open Company,Ltd., 1994).

  • 10 The GNU C Library: System & Network Applications

    ity and in fact _POSIX_SOURCE and _POSIX_C_SOURCE are automaticallydefined.As the unification of all Unices, functionality only available in BSD and SVID isalso included.If the macro _XOPEN_SOURCE_EXTENDED is also defined, even more func-tionality is available. The extra functions will make all functions available thatare necessary for the X/Open Unix brand.If the macro _XOPEN_SOURCE has the value 500, this includes all functionalitydescribed so far plus some new definitions from the Single Unix Specification,version 2.

    MacroLARGEFILE SOURCEIf this macro is defined, some extra functions are available that rectify a fewshortcomings in all previous standards. Specifically, the functions fseekoand ftello are available. Without these functions, the difference between theISO C interface (fseek, ftell) and the low-level POSIX interface (lseek)would lead to problems.This macro was introduced as part of the Large File Support extension (LFS).

    MacroLARGEFILE64 SOURCEIf you define this macro, an additional set of functions is made available thatenables 32-bit systems to use files of sizes beyond the usual limit of 2GB. Thisinterface is not available if the system does not support files that large. Onsystems where the natural file size limit is greater than 2GB (i.e., on 64-bitsystems), the new functions are identical to the replaced functions.The new functionality is made available by a new set of types and functions thatreplace the existing ones. The names of these new objects contain 64 to indicatethe intention, e.g., off_t vs. off64_t and fseeko vs. fseeko64.This macro was introduced as part of the Large File Support extension (LFS). Itis a transition interface for the period when 64-bit offsets are not generally used(see _FILE_OFFSET_BITS).

    MacroFILE OFFSET BITSThis macro determines which file-system interface will be used, one replac-ing the other. Whereas _LARGEFILE64_SOURCE makes the 64-bit interfaceavailable as an additional interface, _FILE_OFFSET_BITS allows the 64-bitinterface to replace the old interface.If _FILE_OFFSET_BITS is undefined, or if it is defined to the value 32,nothing changes. The 32-bit interface is used and types like off_t have a sizeof 32 bits on 32-bit systems.If the macro is defined to the value 64, the large file interface replaces the oldinterface. The functions are not made available under different names (as theyare with _LARGEFILE64_SOURCE); instead, the old function names now ref-erence the new functions, e.g., a call to fseeko now indeed calls fseeko64.

  • Chapter 1: Introduction 11

    This macro should only be selected if the system provides mechanisms for han-dling large files. On 64-bit systems this macro has no effect since the *64functions are identical to the normal functions.This macro was introduced as part of the Large File Support extension (LFS).

    MacroISOC99 SOURCEUntil the revised ISO C standard is widely adopted the new features are not au-tomatically enabled. The GNU libc nevertheless has a complete implementationof the new standard. To enable the new features the macro _ISOC99_SOURCEshould be defined.

    MacroGNU SOURCEIf you define this macro, everything is included: ISO C89, ISO C99, POSIX.1,POSIX.2, BSD, SVID, X/Open, LFS, and GNU extensions. In the cases wherePOSIX.1 conflicts with BSD, the POSIX definitions take precedence.If you want to get the full effect of _GNU_SOURCE but make the BSD definitionstake precedence over the POSIX definitions, use this sequence of definitions:

    #define _GNU_SOURCE

    #define _BSD_SOURCE

    #define _SVID_SOURCE

    If you do this, you must link your program with the BSD compatibility library bypassing the -lbsd-compat option to the compiler or linker. If you forget,you may get very strange errors at run time.

    MacroREENTRANTMacroTHREAD SAFE

    If you define one of these macros, reentrant versions of several functions getdeclared. Some of the functions are specified in POSIX.1c, but many others areonly available on a few other systems or are unique to GNU libc. The problemis the delay in the standardization of the thread safe C library interface.Unlike on some other systems, no special version of the C library must be usedfor linking. There is only one versionbut while compiling this, it must havebeen specified to compile as thread safe.

    We recommend you use _GNU_SOURCE in new programs. If you dont specifythe -ansi option to GCC and do not define any of these macros explicitly, theeffect is the same as defining _POSIX_C_SOURCE to 2 and _POSIX_SOURCE,_SVID_SOURCE and _BSD_SOURCE to 1.

    When you define a feature-test macro to request a larger class of features, itis harmless to define, in addition, a feature-test macro for a subset of those fea-tures. For example, if you define _POSIX_C_SOURCE, then defining _POSIX_SOURCE as well has no effect. Likewise, if you define _GNU_SOURCE, definingeither _POSIX_SOURCE, _POSIX_C_SOURCE, or _SVID_SOURCE as well hasno effect.

  • 12 The GNU C Library: System & Network Applications

    Note, however, that the features of _BSD_SOURCE are not a subset of any ofthe other feature-test macros supported. This is because it defines BSD features thattake precedence over the POSIX features that are requested by the other macros. Forthis reason, defining _BSD_SOURCE in addition to the other feature-test macrosdoes have an effectit causes the BSD features to take priority over the conflictingPOSIX features.

    1.4 Road Map to the ManualHere is an overview of the contents of the remaining chapters of this manual.The following chapters are found in the first volume, Sandra Loosemore et al.,

    GNU C Library: Application Fundamentals (Boston: GNU Press, 2004), availableonline at http:// www.gnu.org/ manual/ manual.html.

    Error Reporting describes how errors detected by the library are reported. Virtual Memory Allocation and Paging describes the GNU librarys facilities

    for managing and using virtual and real memory, including dynamic allocationof virtual memory. If you do not know in advance how much memory yourprogram needs, you can allocate it dynamically instead, and manipulate it viapointers.

    Character Handling contains information about character-classificationfunctions (such as isspace) and functions for performing case conversion.

    String and Array Utilities has descriptions of functions for manipulatingstrings (null-terminated character arrays) and general byte arrays, includingoperations such as copying and comparison.

    Character-Set Handling contains information about manipulating charactersand strings using character sets larger than will fit in the usual char data type.

    Locales and Internationalization describes how selecting a particular coun-try or language affects the behavior of the library. For example, the localeaffects collation sequences for strings and how monetary values are formatted.

    Mathematics contains information about the math library functions. Theseinclude things like random-number generators and remainder functions on in-tegers as well as the usual trigonometric and exponential functions on floating-point numbers.

    Arithmetic Functions describes functions for simple arithmetic, analysis offloating-point values, and reading numbers from strings.

    Date and Time describes functions for measuring both calendar time andCPU time, as well as functions for setting alarms and timers.

    Message Translation describes how to write programs that are capable ofdelivering messages in whatever language the user selects without filling thesource code with sets of translations.

    Searching and Sorting contains information about functions for searchingand sorting arrays. You can use these functions on any kind of array by pro-viding an appropriate comparison function.

  • Chapter 1: Introduction 13

    Pattern Matching presents functions for matching regular expressions andshell file-name patterns, and for expanding words as the shell does.

    The Basic Program/System Interface tells how your programs can accesstheir command-line arguments and environment variables.

    Input/Output Overview gives an overall look at the input and output facili-ties in the library, and contains information about basic concepts such as filenames.

    Debugging Support describes functions provided by the library to make thedebugging process easier, whether or not a dedicated debugger program isbeing used.

    Input/Output on Streams describes I/O operations involving streams (orFILE * objects). These are the normal C library functions from stdio.h.

    Summary of Library Facilities gives a summary of all the functions, vari-ables, and macros in the library, with complete data types and function proto-types, and says what standard or system each is derived from. This section isalso found in the second volume, for convenient reference.

    The following chapters are found in the second volume, Sandra Loosemore etal., GNU C Library: System & Network Applications (Boston: GNU Press, 2004),available online at http:// www.gnu.org/ manual/ manual.html.

    Low-Level Input/Output contains information about I/O operations on filedescriptors. File descriptors are a lower-level mechanism specific to the Unixfamily of operating systems.

    File-System Interface has descriptions of operations on entire files, suchas functions for deleting and renaming them and for creating new directories.This chapter also contains information about how you can access the attributesof a file, such as its owner and file-protection modes.

    Pipes and FIFOs contains information about simple interprocess-communication mechanisms. Pipes allow communication between tworelated processes (such as between a parent and child), while FIFOs allowcommunication between processes sharing a common file-system on the samemachine.

    Sockets describes a more complicated interprocess-communication mech-anism that allows processes running on different machines to communicateover a network. This chapter also contains information about Internet host-addressing and how to use the system network databases.

    Low-Level Terminal Interface describes how you can change the attributesof a terminal device. If you want to disable echo of characters typed by theuser, for example, read this chapter.

    Processes contains information about how to start new processes and runprograms.

    Job Control describes functions for manipulating process groups and thecontrolling terminal. This material is probably only of interest if you are writ-ing a shell or other program that handles job control specially.

  • 14 The GNU C Library: System & Network Applications

    System Databases and Name-Service Switch describes the services that areavailable for looking up names in the system databases, how to determinewhich service is used for which database, and how these services are imple-mented so that contributors can design their own services.

    Users and Groups tells you how to access the system user- and group-databases.

    System Management describes functions for controlling and getting infor-mation about the hardware and software configuration your program is exe-cuting under.

    System-Configuration Parameters tells you how you can get informationabout various operating system limits. Most of these parameters are providedfor compatibility with POSIX.

    DES Encryption and Password Handling discusses the legal and technicalissues related to password encryption and security, as well as the functionsnecessary to implement effective encryption.

    Resource Usage and Limitation tells you how to monitor the memory andother resource usage totals of processes, and how to regulate this usage. It alsocovers prioritization and scheduling.

    Syslog describes facilities for issuing and logging messages of system ad-ministration interest.

    Nonlocal Exits contains descriptions of the setjmp and longjmp func-tions. These functions provide a facility for goto-like jumps that can jumpfrom one function to another.

    Signal Handling tells you all about signalswhat they are, how to establisha handler that is called when a particular kind of signal is delivered, and howto prevent signals from arriving during critical sections of your program.

    POSIX Threads describes the pthreads (POSIX threads) library. This libraryprovides support functions for multithreaded programs: thread primitives, syn-chronization objects, etc. It also implements POSIX 1003.1b semaphores.

    C Language Facilities in the Library contains information about library sup-port for standard parts of the C language, including things like the sizeofoperator and the symbolic constant NULL, how to write functions acceptingvariable numbers of arguments, and constants describing the ranges and otherproperties of the numerical types. There is also a simple debugging mecha-nism that allows you to put assertions in your code and have diagnostic mes-sages printed if the tests fail.

    Installing the GNU C Library provides a detailed reference for installing,compiling and configuring the GNU C Library. Configuration and optimizationcommand-line options are covered here.

    Library Maintenance explains how to port and enhance the GNU C Libraryand how to report any bugs you might find.

    If you already know the name of the facility you are interested in, you can lookit up in Appendix B [Summary of Library Facilities], page 475. This gives you a

  • Chapter 1: Introduction 15

    summary of its syntax and a pointer to where you can find a more detailed descrip-tion. This appendix is particularly useful if you just want to verify the order andtype of arguments to a function, for example. It also tells you what standard orsystem each function, variable, or macro is derived from.

  • 16 The GNU C Library: System & Network Applications

  • Chapter 2: Low-Level Input/Output 17

    2 Low-Level Input/OutputThis chapter describes functions for performing low-level input/output opera-

    tions on file descriptors. These functions include the primitives for the higher-levelI/O functions,1 as well as functions for performing low-level control operations forwhich there are no equivalents on streams.

    Stream-level I/O is more flexible and usually more convenient; therefore, pro-grammers generally use the descriptor-level functions only when necessary. Theseare some of the usual reasons:

    For reading binary files in large chunks For reading an entire file into core before parsing it To perform operations other than data transfer, which can only be done with

    a descriptor; you can use fileno to get the descriptor corresponding to astream.

    To pass descriptors to a child process; the child can create its own stream touse a descriptor that it inherits, but it cannot inherit a stream directly.

    2.1 Opening and Closing FilesThis section describes the primitives for opening and closing files using file

    descriptors. The open and creat functions are declared in the header filefcntl.h, while close is declared in unistd.h.

    Functionint open (const char *filename, int flags[, mode_tmode])

    The open function creates and returns a new file-descriptor for the file namedby filename. Initially, the file position indicator for the file is at the beginning ofthe file. The argument mode is used only when a file is created, but it doesnthurt to supply the argument in any case.The flags argument controls how the file is to be opened. This is a bit mask; youcreate the value by the bit-wise OR of the appropriate parameters, using the |operator in C (for the parameters available, see Section 2.14 [File Status Flags],page 59).The normal return value from open is a nonnegative integer file descriptor.In the case of an error, a value of 1 is returned instead. In addition to theusual file-name errors, the following errno error conditions are defined forthis function:2

    EACCES The file exists but is not readable or writable as requested by theflags argument; the file does not exist and the directory is un-writable, so it cannot be created.

    1 See Sandra Loosemore et al., Input/Output on Streams (see chap. 1, n. 1).2 Ibid., File-Name Errors.

  • 18 The GNU C Library: System & Network Applications

    EEXIST Both O_CREAT and O_EXCL are set, and the named file alreadyexists.

    EINTR The open operation was interrupted by a signal (see Section 17.5[Primitives Interrupted by Signals], page 408).

    EISDIR The flags argument specified write access, and the file is a direc-tory.

    EMFILE The process has too many files open. The maximum number of filedescriptors is controlled by the RLIMIT_NOFILE resource limit(see Section 14.2 [Limiting Resource Usage], page 338).

    ENFILE The entire system, or perhaps the file system that contains the di-rectory, cannot support any additional open files at the moment.This problem cannot happen on the GNU system.

    ENOENT The named file does not exist, and O_CREAT is not specified.

    ENOSPC The directory or file system that would contain the new file cannotbe extended, because there is no disk space left.

    ENXIO O_NONBLOCK and O_WRONLY are both set in the flags argument,the file named by filename is a FIFO (see Chapter 4 [Pipes andFIFOs], page 119), and no process has the file open for reading.

    EROFS The file resides on a read-only file system and any of O_WRONLY,O_RDWR or O_TRUNC are set in the flags argument; or O_CREATis set and the file does not already exist.

    If on a 32-bit machine the sources are translated with _FILE_OFFSET_BITS== 64, the function open returns a file descriptor opened in the large file modethat enables the file-handling functions to use files up to 263 bytes in size andoffset from263 to 263. This happens transparently for the user, since all of thelow-level file-handling functions are equally replaced.This function is a cancellation point in multithreaded programs. This is aproblem if the thread allocates some resources (like memory, file descriptors,semaphores, etc.) at the time open is called. If the thread gets canceled, theseresources stay allocated until the program ends. To avoid this, calls to openshould be protected using cancellation handlers.The open function is the underlying primitive for the fopen and freopenfunctions, which create streams.

    Functionint open64 (const char *filename, int flags[, mode_tmode])

    This function is similar to open. It returns a file descriptor that can be used toaccess the file named by filename. The only difference is that on 32-bit systems,the file is opened in the large file mode, so file length and file offsets can exceed31 bits.

  • Chapter 2: Low-Level Input/Output 19

    When the sources are translated with _FILE_OFFSET_BITS == 64, thisfunction is actually available under the name openthe new, extended APIusing 64-bit file sizes and offsets transparently replaces the old API.

    Obsolete functionint creat (const char *filename, mode_t mode)This function is obsolete. The call:

    creat (filename, mode)

    is equivalent to:open (filename, O_WRONLY | O_CREAT | O_TRUNC, mode)

    If on a 32-bit machine the sources are translated with _FILE_OFFSET_BITS== 64, the function creat returns a file descriptor opened in the large filemode that enables the file-handling functions to use files up to 263 in size andoffset from263 to 263. This happens transparently for the user, since all of thelow-level file-handling functions are equally replaced.

    Obsolete functionint creat64 (const char *filename, mode_t mode)This function is similar to creat. It returns a file descriptor that can be used toaccess the file named by filename. The only difference is that on 32-bit systemsthe file is opened in the large file mode, so file length and file offsets can exceed31 bits.To use this file descriptor, you must not use the normal operations but insteadthe counterparts named *64, such as read64.When the sources are translated with _FILE_OFFSET_BITS == 64, thisfunction is actually available under the name openthe new, extended APIusing 64-bit file sizes and offsets transparently replaces the old API.

    Functionint close (int filedes)The function close closes the file descriptor filedes. Closing a file has thefollowing consequences:

    The file descriptor is deallocated. Any record locks owned by the process on the file are unlocked. When all file descriptors associated with a pipe or FIFO have been closed,

    any unread data is discarded.

    This function is a cancellation point in multithreaded programs. This is aproblem if the thread allocates some resources (like memory, file descriptors,semaphores, etc.) at the time close is called. If the thread gets canceled, theseresources stay allocated until the program ends. To avoid this, calls to closeshould be protected using cancellation handlers.The normal return value from close is 0; a value of 1 is returned in case offailure. The following errno error conditions are defined for this function:

    EBADF The filedes argument is not a valid file-descriptor.

  • 20 The GNU C Library: System & Network Applications

    EINTR The close call was interrupted by a signal (see Section 17.5[Primitives Interrupted by Signals], page 408). Here is an exampleof how to handle EINTR properly:

    TEMP_FAILURE_RETRY (close (desc));

    ENOSPCEIOEDQUOT When the file is accessed by NFS, these errors from write can

    sometimes go undetected until close (see Section 2.2 [Input andOutput Primitives], page 20 for details on their meaning).

    There is no separate close64 function. This is not necessary, since this func-tion does not determine nor depend on the mode of the file. The kernel thatperforms the close operation knows which mode the descriptor is used forand can handle this situation.

    To close a stream, call fclose instead of trying to close its underlying file-descriptor with close.3 Thi